Codebase list libcryptx-perl / 456bcaf
new Crypt::Stream::Rabbit Karel Miko 6 years ago
12 changed file(s) with 200 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
00 Changes for CryptX
1
2 0.055_1 2017-12-14
3 - new Crypt::Stream::Rabbit
14
25 0.055 2017-11-28
36 - new Crypt::Cipher::IDEA
7272 sosemanuk_state state;
7373 int id;
7474 } *Crypt__Stream__Sosemanuk;
75
76 typedef struct rabbit_struct { /* used by Crypt::Stream::Rabbit */
77 rabbit_state state;
78 int id;
79 } *Crypt__Stream__Rabbit;
7580
7681 typedef struct rc4_struct { /* used by Crypt::Stream::RC4 */
7782 rc4_state state;
692697 INCLUDE: inc/CryptX_Stream_RC4.xs.inc
693698 INCLUDE: inc/CryptX_Stream_Sober128.xs.inc
694699 INCLUDE: inc/CryptX_Stream_Sosemanuk.xs.inc
700 INCLUDE: inc/CryptX_Stream_Rabbit.xs.inc
695701
696702 INCLUDE: inc/CryptX_Mac_F9.xs.inc
697703 INCLUDE: inc/CryptX_Mac_HMAC.xs.inc
0 MODULE = CryptX PACKAGE = Crypt::Stream::Rabbit
1
2 Crypt::Stream::Rabbit
3 new(Class, SV * key, SV * nonce=&PL_sv_undef)
4 CODE:
5 {
6 int rv;
7 STRLEN iv_len=0, k_len=0;
8 unsigned char *iv=NULL, *k=NULL;
9
10 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
11 k = (unsigned char *)SvPVbyte(key, k_len);
12
13 Newz(0, RETVAL, 1, struct rabbit_struct);
14 if (!RETVAL) croak("FATAL: Newz failed");
15
16 rv = rabbit_setup(&RETVAL->state, k, (unsigned long)k_len);
17 if (rv != CRYPT_OK) {
18 Safefree(RETVAL);
19 croak("FATAL: rabbit_setup failed: %s", error_to_string(rv));
20 }
21
22 if (SvOK(nonce)) {
23 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
24 iv = (unsigned char *)SvPVbyte(nonce, iv_len);
25 rv = rabbit_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
26 }
27 else {
28 rv = rabbit_setiv(&RETVAL->state, NULL, 0);
29 }
30 if (rv != CRYPT_OK) {
31 Safefree(RETVAL);
32 croak("FATAL: rabbit_setiv failed: %s", error_to_string(rv));
33 }
34
35 }
36 OUTPUT:
37 RETVAL
38
39 void
40 DESTROY(Crypt::Stream::Rabbit self)
41 CODE:
42 rabbit_done(&self->state);
43 Safefree(self);
44
45 Crypt::Stream::Rabbit
46 clone(Crypt::Stream::Rabbit self)
47 CODE:
48 Newz(0, RETVAL, 1, struct rabbit_struct);
49 if (!RETVAL) croak("FATAL: Newz failed");
50 Copy(&self->state, &RETVAL->state, 1, struct rabbit_struct);
51 OUTPUT:
52 RETVAL
53
54 SV *
55 keystream(Crypt::Stream::Rabbit self, STRLEN out_len)
56 CODE:
57 {
58 int rv;
59 unsigned char *out_data;
60
61 RETVAL = NEWSV(0, out_len);
62 SvPOK_only(RETVAL);
63 SvCUR_set(RETVAL, out_len);
64 out_data = (unsigned char *)SvPVX(RETVAL);
65 rv = rabbit_keystream(&self->state, out_data, (unsigned long)out_len);
66 if (rv != CRYPT_OK) {
67 SvREFCNT_dec(RETVAL);
68 croak("FATAL: rabbit_keystream failed: %s", error_to_string(rv));
69 }
70 }
71 OUTPUT:
72 RETVAL
73
74 SV *
75 crypt(Crypt::Stream::Rabbit self, SV * data)
76 CODE:
77 {
78 int rv;
79 STRLEN in_data_len;
80 unsigned char *in_data, *out_data;
81
82 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
83 if (in_data_len == 0) {
84 RETVAL = newSVpvn("", 0);
85 }
86 else {
87 RETVAL = NEWSV(0, in_data_len);
88 SvPOK_only(RETVAL);
89 SvCUR_set(RETVAL, in_data_len);
90 out_data = (unsigned char *)SvPVX(RETVAL);
91 rv = rabbit_crypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
92 if (rv != CRYPT_OK) {
93 SvREFCNT_dec(RETVAL);
94 croak("FATAL: rabbit_crypt failed: %s", error_to_string(rv));
95 }
96 }
97 }
98 OUTPUT:
99 RETVAL
0 package Crypt::Stream::Rabbit;
1
2 use strict;
3 use warnings;
4 our $VERSION = '0.055_001';
5
6 use CryptX;
7
8 1;
9
10 =pod
11
12 =head1 NAME
13
14 Crypt::Stream::Rabbit - Stream cipher Rabbit
15
16 =head1 SYNOPSIS
17
18 use Crypt::Stream::Rabbit;
19
20 # encrypt
21 $key = "1234567890123456";
22 $iv = "123456789012";
23 $stream = Crypt::Stream::Rabbit->new($key, $iv);
24 $ct = $stream->crypt("plain message");
25
26 # decrypt
27 $key = "1234567890123456";
28 $iv = "123456789012";
29 $stream = Crypt::Stream::Rabbit->new($key, $iv);
30 $pt = $stream->crypt($ct);
31
32 =head1 DESCRIPTION
33
34 Provides an interface to the Rabbit stream cipher.
35
36 =head1 METHODS
37
38 =head2 new
39
40 $stream = Crypt::Stream::Rabbit->new($key, $iv);
41 # $key .. keylen must be multiple of 4 bytes
42 # $iv .. ivlen must be multiple of 4 bytes (OPTIONAL)
43
44 =head2 crypt
45
46 $ciphertext = $stream->crypt($plaintext);
47 #or
48 $plaintext = $stream->crypt($ciphertext);
49
50 =head2 keystream
51
52 $random_key = $stream->keystream($length);
53
54 =head2 clone
55
56 $stream->clone();
57
58 =head1 SEE ALSO
59
60 =over
61
62 =item * L<Crypt::Stream::RC4>, L<Crypt::Stream::ChaCha>, L<Crypt::Stream::Salsa20>, L<Crypt::Stream::Sober128>
63
64 =item * L<https://en.wikipedia.org/wiki/Rabbit_(cipher)>
65
66 =back
67
68 =cut
6161
6262 =item * L<Crypt::Stream::RC4>, L<Crypt::Stream::ChaCha>, L<Crypt::Stream::Salsa20>, L<Crypt::Stream::Sober128>
6363
64 =item * L<https://en.wikipedia.org/wiki/SOBER-128|https://en.wikipedia.org/wiki/SOBER-128>
64 =item * L<https://en.wikipedia.org/wiki/SOSEMANUK>
6565
6666 =back
6767
8484
8585 =item * Stream ciphers
8686
87 L<Crypt::Stream::RC4>, L<Crypt::Stream::ChaCha>, L<Crypt::Stream::Salsa20>, L<Crypt::Stream::Sober128>, L<Crypt::Stream::Sosemanuk>
87 L<Crypt::Stream::RC4>, L<Crypt::Stream::ChaCha>, L<Crypt::Stream::Salsa20>, L<Crypt::Stream::Sober128>,
88 L<Crypt::Stream::Sosemanuk>, L<Crypt::Stream::Rabbit>
8889
8990 =item * Authenticated encryption modes
9091
108108 use Crypt::Stream::Salsa20;
109109 use Crypt::Stream::Sober128;
110110 use Crypt::Stream::Sosemanuk;
111 use Crypt::Stream::Rabbit;
111112 use CryptX;
112113 use Math::BigInt::LTM;
113114
55 plan skip_all => "set TEST_POD to enable this test (developer only!)" unless $ENV{TEST_POD};
66 plan skip_all => "File::Find not installed" unless eval { require File::Find };
77 plan skip_all => "Test::Pod not installed" unless eval { require Test::Pod };
8 plan tests => 102;
8 plan tests => 103;
99
1010 my @files;
1111 File::Find::find({ wanted=>sub { push @files, $_ if /\.pm$/ }, no_chdir=>1 }, 'lib');
99 Test::Pod::Spelling->import(
1010 spelling => {
1111 allow_words => [qw(
12 AES BLAKEb BLAKEs CPAN CRC ChaCha CryptX DCIT DER Diffie EAX ECCDH ECDH ECDSA Flickr HKDF JSON JWA JWK
12 ASN AES BLAKEb BLAKEs CPAN CRC ChaCha CryptX DCIT DER Diffie EAX ECCDH ECDH ECDSA Flickr HKDF JSON JWA JWK
1313 Karel Miko OCB OCBv OID OMAC OO OpenSSL PBKDF PEM PKCS RIPEMD Rijndael SHA UUID RFC
1414 decrypt decrypts interoperability cryptographically cryptographic octects
1515 libtomcrypt libtommath
2020 },
2121 );
2222
23 plan tests => 102;
23 plan tests => 103;
2424
2525 my @files;
2626 File::Find::find({ wanted=>sub { push @files, $_ if /\.pm$/ }, no_chdir=>1 }, 'lib');
55 plan skip_all => "set TEST_POD to enable this test (developer only!)" unless $ENV{TEST_POD};
66 plan skip_all => "Pod::Coverage not installed" unless eval { require Pod::Coverage };
77 plan skip_all => "File::Find not installed" unless eval { require File::Find };
8 plan tests => 102;
8 plan tests => 103;
99
1010 my @files;
1111 File::Find::find({ wanted=>sub { push @files, $_ if /\.pm$/ }, no_chdir=>1 }, 'lib');
00 use strict;
11 use warnings;
22
3 use Test::More tests => 14;
3 use Test::More tests => 16;
44
55 use Crypt::Stream::RC4;
66 use Crypt::Stream::Sober128;
77 use Crypt::Stream::ChaCha;
88 use Crypt::Stream::Salsa20;
9 use Crypt::Stream::Sosemanuk;
10 use Crypt::Stream::Rabbit;
911
1012 {
1113 my $key = pack("H*", "0123456789abcdef");
8385 is(unpack("H*", $dec), unpack("H*", $pt), "Crypt::Stream::Sosemanuk decrypt (no IV)");
8486 }
8587
88 {
89 my $key = pack("H*", "74657374206b65792031323862697473");
90 my $iv = pack("H*", "00000000");
91 my $ct = pack("H*", "442cf424c5da8d78000c6b874050260792ae8ce0");
92 my $pt = pack("H*", "0000000000000000000000000000000000000000");
93 my $enc = Crypt::Stream::Rabbit->new($key, $iv)->crypt($pt);
94 my $dec = Crypt::Stream::Rabbit->new($key, $iv)->crypt($ct);
95 is(unpack("H*", $enc), unpack("H*", $ct), "Crypt::Stream::Rabbit encrypt");
96 is(unpack("H*", $dec), unpack("H*", $pt), "Crypt::Stream::Rabbit decrypt");
97 }
2020 Crypt::Stream::RC4 T_PTROBJ
2121 Crypt::Stream::Sober128 T_PTROBJ
2222 Crypt::Stream::Sosemanuk T_PTROBJ
23 Crypt::Stream::Rabbit T_PTROBJ
2324
2425 Crypt::Mac::F9 T_PTROBJ
2526 Crypt::Mac::HMAC T_PTROBJ