Codebase list libcryptx-perl / 5d3e1d3
increment_octets_be, increment_octets_le Karel Miko 7 years ago
9 changed file(s) with 138 addition(s) and 130 deletion(s). Raw diff Collapse all Expand all
3131 - NEW: Crypt::Stream::ChaCha
3232 - NEW: Crypt::Stream::RC4
3333 - NEW: Crypt::Stream::Sober128
34 - NEW: functions in Crypt::Misc - increment_octets_be, increment_octets_le
3435
3536 0.047 2017/04/05
3637 - fix #32 Compile "ar" step fails when Perl built with -flto (better version)
475475 OUTPUT:
476476 RETVAL
477477
478 SV *
479 CryptX__increment_octets_le(SV * in)
480 CODE:
481 {
482 STRLEN len, i = 0;
483 unsigned char *out_data, *in_data;
484 int rv;
485
486 if (!SvPOK(in)) XSRETURN_UNDEF;
487 in_data = (unsigned char *) SvPVbyte(in, len);
488 if (len == 0) XSRETURN_UNDEF;
489
490 RETVAL = NEWSV(0, len);
491 SvPOK_only(RETVAL);
492 SvCUR_set(RETVAL, len);
493 out_data = (unsigned char *)SvPV_nolen(RETVAL);
494 Copy(in_data, out_data, len, unsigned char);
495 while (i < len) {
496 out_data[i]++;
497 if (0 != out_data[i]) break;
498 i++;
499 }
500 if (i == len) croak("FATAL: increment_octets_le overflow");
501 }
502 OUTPUT:
503 RETVAL
504
505 SV *
506 CryptX__increment_octets_be(SV * in)
507 CODE:
508 {
509 STRLEN len, i = 0;
510 unsigned char *out_data, *in_data;
511 int rv;
512
513 if (!SvPOK(in)) XSRETURN_UNDEF;
514 in_data = (unsigned char *) SvPVbyte(in, len);
515 if (len == 0) XSRETURN_UNDEF;
516
517 RETVAL = NEWSV(0, len);
518 SvPOK_only(RETVAL);
519 SvCUR_set(RETVAL, len);
520 out_data = (unsigned char *)SvPV_nolen(RETVAL);
521 Copy(in_data, out_data, len, unsigned char);
522 while (i < len) {
523 out_data[len - 1 - i]++;
524 if (0 != out_data[len - 1 - i]) break;
525 i++;
526 }
527 if (i == len) croak("FATAL: increment_octets_le overflow");
528 }
529 OUTPUT:
530 RETVAL
531
478532 ###############################################################################
479533
480534 INCLUDE: inc/CryptX_Digest.xs.inc
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::ChaCha20Poly1305
11
22 Crypt::AuthEnc::ChaCha20Poly1305
3 _new(SV * key, SV * nonce)
3 _new(SV * key, SV * nonce = NULL)
44 CODE:
55 {
66 int rv;
88 unsigned char *iv=NULL, *k=NULL;
99
1010 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
11 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
1211 k = (unsigned char *) SvPVbyte(key, k_len);
13 iv = (unsigned char *) SvPVbyte(nonce, iv_len);
12 if (nonce) {
13 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
14 iv = (unsigned char *) SvPVbyte(nonce, iv_len);
15 }
1416
1517 Newz(0, RETVAL, 1, struct chacha20poly1305_struct);
1618 if (!RETVAL) croak("FATAL: Newz failed");
1820 rv = chacha20poly1305_init(&RETVAL->state, k, (unsigned long)k_len);
1921 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv));
2022
21 rv = chacha20poly1305_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
22 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
23 if (iv && iv_len > 0) {
24 rv = chacha20poly1305_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
25 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
26 }
2327 }
2428 OUTPUT:
2529 RETVAL
3539 Newz(0, RETVAL, 1, struct chacha20poly1305_struct);
3640 if (!RETVAL) croak("FATAL: Newz failed");
3741 Copy(&self->state, &RETVAL->state, 1, struct chacha20poly1305_struct);
38 OUTPUT:
39 RETVAL
40
41 int
42 increment_iv(Crypt::AuthEnc::ChaCha20Poly1305 self)
43 CODE:
44 {
45 int rv;
46 rv = chacha20poly1305_inciv(&self->state);
47 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_inciv failed: %s", error_to_string(rv));
48 RETVAL = rv;
49 }
5042 OUTPUT:
5143 RETVAL
5244
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM
11
22 Crypt::AuthEnc::GCM
3 _new(char * cipher_name, SV * key)
3 _new(char * cipher_name, SV * key, SV * nonce = NULL)
44 CODE:
55 {
6 STRLEN k_len=0;
7 unsigned char *k=NULL;
8 int id;
6 STRLEN k_len = 0, iv_len = 0;
7 unsigned char *k = NULL, *iv = NULL;
8 int id, rv;
99
10 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
10 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1111 k = (unsigned char *) SvPVbyte(key, k_len);
12
12 if (nonce) {
13 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
14 iv = (unsigned char *)SvPVbyte(nonce, iv_len);
15 }
1316
1417 id = find_cipher(cipher_name);
15 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1619
1720 Newz(0, RETVAL, 1, struct gcm_struct);
1821 if (!RETVAL) croak("FATAL: Newz failed");
1922
20 if (gcm_init(&RETVAL->state, id, k, (unsigned long)k_len) != CRYPT_OK) {
21 croak("FATAL: gcm setup failed");
23 rv = gcm_init(&RETVAL->state, id, k, (unsigned long)k_len);
24 if (rv != CRYPT_OK) croak("FATAL: gcm_init failed: %s", error_to_string(rv));
25
26 if (iv && iv_len > 0) {
27 rv = gcm_add_iv(&RETVAL->state, iv, (unsigned long)iv_len);
28 if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
2229 }
2330 }
2431 OUTPUT:
144144
145145 my $ae_new = $ae->clone;
146146
147 =head2 increment_iv
148
149 $ae->increment_iv();
150
151147 =head2 set_iv
152148
153149 $ae->set_iv($iv);
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
77 use Carp 'croak';
8 our %EXPORT_TAGS = ( all => [qw(encode_b64 decode_b64 encode_b64u decode_b64u pem_to_der der_to_pem read_rawfile write_rawfile slow_eq is_v4uuid random_v4uuid)] );
8 our %EXPORT_TAGS = ( all => [qw(encode_b64 decode_b64 encode_b64u decode_b64u
9 pem_to_der der_to_pem
10 read_rawfile write_rawfile
11 slow_eq is_v4uuid random_v4uuid
12 increment_octets_be increment_octets_le
13 )] );
914 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
1015 our @EXPORT = qw();
1116
3338
3439 sub decode_b64u {
3540 CryptX::_decode_base64url(@_);
41 }
42
43 sub increment_octets_be {
44 CryptX::_increment_octets_be(@_);
45 #$_[0] = CryptX::_increment_octets_be($_[0]);
46 }
47
48 sub increment_octets_le {
49 CryptX::_increment_octets_le(@_);
50 #$_[0] = CryptX::_increment_octets_le($_[0]);
3651 }
3752
3853 sub pem_to_der {
320335
321336 Checks the given C<$uuid> string whether it matches V4 UUID format and returns C<0> (mismatch) or C<1> (match).
322337
338 =head2 increment_octets_le
339
340 $octects = increment_octets_le($octets);
341
342 =head2 increment_octets_be
343
344 $octects = increment_octets_be($octets);
345
323346 =head1 SEE ALSO
324347
325348 =over
55 ltc/encauth/ccm/ccm_done.o ltc/encauth/ccm/ccm_init.o ltc/encauth/ccm/ccm_memory.o \
66 ltc/encauth/ccm/ccm_process.o ltc/encauth/ccm/ccm_reset.o ltc/encauth/chachapoly/chacha20poly1305_add_aad.o \
77 ltc/encauth/chachapoly/chacha20poly1305_decrypt.o ltc/encauth/chachapoly/chacha20poly1305_done.o \
8 ltc/encauth/chachapoly/chacha20poly1305_encrypt.o ltc/encauth/chachapoly/chacha20poly1305_inciv.o \
9 ltc/encauth/chachapoly/chacha20poly1305_init.o ltc/encauth/chachapoly/chacha20poly1305_memory.o \
10 ltc/encauth/chachapoly/chacha20poly1305_setiv.o ltc/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.o \
11 ltc/encauth/eax/eax_addheader.o ltc/encauth/eax/eax_decrypt.o ltc/encauth/eax/eax_decrypt_verify_memory.o \
12 ltc/encauth/eax/eax_done.o ltc/encauth/eax/eax_encrypt.o ltc/encauth/eax/eax_encrypt_authenticate_memory.o \
13 ltc/encauth/eax/eax_init.o ltc/encauth/gcm/gcm_add_aad.o ltc/encauth/gcm/gcm_add_iv.o \
14 ltc/encauth/gcm/gcm_done.o ltc/encauth/gcm/gcm_gf_mult.o ltc/encauth/gcm/gcm_init.o \
15 ltc/encauth/gcm/gcm_memory.o ltc/encauth/gcm/gcm_mult_h.o ltc/encauth/gcm/gcm_process.o \
16 ltc/encauth/gcm/gcm_reset.o ltc/encauth/ocb3/ocb3_add_aad.o ltc/encauth/ocb3/ocb3_decrypt.o \
17 ltc/encauth/ocb3/ocb3_decrypt_last.o ltc/encauth/ocb3/ocb3_decrypt_verify_memory.o \
18 ltc/encauth/ocb3/ocb3_done.o ltc/encauth/ocb3/ocb3_encrypt.o ltc/encauth/ocb3/ocb3_encrypt_authenticate_memory.o \
19 ltc/encauth/ocb3/ocb3_encrypt_last.o ltc/encauth/ocb3/ocb3_init.o ltc/encauth/ocb3/ocb3_int_aad_add_block.o \
20 ltc/encauth/ocb3/ocb3_int_calc_offset_zero.o ltc/encauth/ocb3/ocb3_int_ntz.o ltc/encauth/ocb3/ocb3_int_xor_blocks.o \
21 ltc/hashes/blake2b.o ltc/hashes/blake2s.o ltc/hashes/md2.o ltc/hashes/md4.o ltc/hashes/md5.o \
22 ltc/hashes/rmd128.o ltc/hashes/rmd160.o ltc/hashes/rmd256.o ltc/hashes/rmd320.o ltc/hashes/sha1.o \
23 ltc/hashes/sha3.o ltc/hashes/sha3_test.o ltc/hashes/tiger.o ltc/hashes/chc/chc.o \
24 ltc/hashes/helper/hash_file.o ltc/hashes/helper/hash_filehandle.o ltc/hashes/helper/hash_memory.o \
25 ltc/hashes/helper/hash_memory_multi.o ltc/hashes/sha2/sha224.o ltc/hashes/sha2/sha256.o \
26 ltc/hashes/sha2/sha384.o ltc/hashes/sha2/sha512.o ltc/hashes/sha2/sha512_224.o ltc/hashes/sha2/sha512_256.o \
27 ltc/hashes/whirl/whirl.o ltc/mac/blake2/blake2bmac.o ltc/mac/blake2/blake2bmac_file.o \
28 ltc/mac/blake2/blake2bmac_memory.o ltc/mac/blake2/blake2bmac_memory_multi.o ltc/mac/blake2/blake2smac.o \
29 ltc/mac/blake2/blake2smac_file.o ltc/mac/blake2/blake2smac_memory.o ltc/mac/blake2/blake2smac_memory_multi.o \
30 ltc/mac/f9/f9_done.o ltc/mac/f9/f9_file.o ltc/mac/f9/f9_init.o ltc/mac/f9/f9_memory.o \
31 ltc/mac/f9/f9_memory_multi.o ltc/mac/f9/f9_process.o ltc/mac/hmac/hmac_done.o ltc/mac/hmac/hmac_file.o \
32 ltc/mac/hmac/hmac_init.o ltc/mac/hmac/hmac_memory.o ltc/mac/hmac/hmac_memory_multi.o \
33 ltc/mac/hmac/hmac_process.o ltc/mac/omac/omac_done.o ltc/mac/omac/omac_file.o ltc/mac/omac/omac_init.o \
34 ltc/mac/omac/omac_memory.o ltc/mac/omac/omac_memory_multi.o ltc/mac/omac/omac_process.o \
35 ltc/mac/pelican/pelican.o ltc/mac/pelican/pelican_memory.o ltc/mac/pmac/pmac_done.o \
36 ltc/mac/pmac/pmac_file.o ltc/mac/pmac/pmac_init.o ltc/mac/pmac/pmac_memory.o ltc/mac/pmac/pmac_memory_multi.o \
8 ltc/encauth/chachapoly/chacha20poly1305_encrypt.o ltc/encauth/chachapoly/chacha20poly1305_init.o \
9 ltc/encauth/chachapoly/chacha20poly1305_memory.o ltc/encauth/chachapoly/chacha20poly1305_setiv.o \
10 ltc/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.o ltc/encauth/eax/eax_addheader.o \
11 ltc/encauth/eax/eax_decrypt.o ltc/encauth/eax/eax_decrypt_verify_memory.o ltc/encauth/eax/eax_done.o \
12 ltc/encauth/eax/eax_encrypt.o ltc/encauth/eax/eax_encrypt_authenticate_memory.o ltc/encauth/eax/eax_init.o \
13 ltc/encauth/gcm/gcm_add_aad.o ltc/encauth/gcm/gcm_add_iv.o ltc/encauth/gcm/gcm_done.o \
14 ltc/encauth/gcm/gcm_gf_mult.o ltc/encauth/gcm/gcm_init.o ltc/encauth/gcm/gcm_memory.o \
15 ltc/encauth/gcm/gcm_mult_h.o ltc/encauth/gcm/gcm_process.o ltc/encauth/gcm/gcm_reset.o \
16 ltc/encauth/ocb3/ocb3_add_aad.o ltc/encauth/ocb3/ocb3_decrypt.o ltc/encauth/ocb3/ocb3_decrypt_last.o \
17 ltc/encauth/ocb3/ocb3_decrypt_verify_memory.o ltc/encauth/ocb3/ocb3_done.o ltc/encauth/ocb3/ocb3_encrypt.o \
18 ltc/encauth/ocb3/ocb3_encrypt_authenticate_memory.o ltc/encauth/ocb3/ocb3_encrypt_last.o \
19 ltc/encauth/ocb3/ocb3_init.o ltc/encauth/ocb3/ocb3_int_aad_add_block.o ltc/encauth/ocb3/ocb3_int_calc_offset_zero.o \
20 ltc/encauth/ocb3/ocb3_int_ntz.o ltc/encauth/ocb3/ocb3_int_xor_blocks.o ltc/hashes/blake2b.o \
21 ltc/hashes/blake2s.o ltc/hashes/md2.o ltc/hashes/md4.o ltc/hashes/md5.o ltc/hashes/rmd128.o \
22 ltc/hashes/rmd160.o ltc/hashes/rmd256.o ltc/hashes/rmd320.o ltc/hashes/sha1.o ltc/hashes/sha3.o \
23 ltc/hashes/sha3_test.o ltc/hashes/tiger.o ltc/hashes/chc/chc.o ltc/hashes/helper/hash_file.o \
24 ltc/hashes/helper/hash_filehandle.o ltc/hashes/helper/hash_memory.o ltc/hashes/helper/hash_memory_multi.o \
25 ltc/hashes/sha2/sha224.o ltc/hashes/sha2/sha256.o ltc/hashes/sha2/sha384.o ltc/hashes/sha2/sha512.o \
26 ltc/hashes/sha2/sha512_224.o ltc/hashes/sha2/sha512_256.o ltc/hashes/whirl/whirl.o \
27 ltc/mac/blake2/blake2bmac.o ltc/mac/blake2/blake2bmac_file.o ltc/mac/blake2/blake2bmac_memory.o \
28 ltc/mac/blake2/blake2bmac_memory_multi.o ltc/mac/blake2/blake2smac.o ltc/mac/blake2/blake2smac_file.o \
29 ltc/mac/blake2/blake2smac_memory.o ltc/mac/blake2/blake2smac_memory_multi.o ltc/mac/f9/f9_done.o \
30 ltc/mac/f9/f9_file.o ltc/mac/f9/f9_init.o ltc/mac/f9/f9_memory.o ltc/mac/f9/f9_memory_multi.o \
31 ltc/mac/f9/f9_process.o ltc/mac/hmac/hmac_done.o ltc/mac/hmac/hmac_file.o ltc/mac/hmac/hmac_init.o \
32 ltc/mac/hmac/hmac_memory.o ltc/mac/hmac/hmac_memory_multi.o ltc/mac/hmac/hmac_process.o \
33 ltc/mac/omac/omac_done.o ltc/mac/omac/omac_file.o ltc/mac/omac/omac_init.o ltc/mac/omac/omac_memory.o \
34 ltc/mac/omac/omac_memory_multi.o ltc/mac/omac/omac_process.o ltc/mac/pelican/pelican.o \
35 ltc/mac/pelican/pelican_memory.o ltc/mac/pmac/pmac_done.o ltc/mac/pmac/pmac_file.o \
36 ltc/mac/pmac/pmac_init.o ltc/mac/pmac/pmac_memory.o ltc/mac/pmac/pmac_memory_multi.o \
3737 ltc/mac/pmac/pmac_ntz.o ltc/mac/pmac/pmac_process.o ltc/mac/pmac/pmac_shift_xor.o \
3838 ltc/mac/poly1305/poly1305.o ltc/mac/poly1305/poly1305_file.o ltc/mac/poly1305/poly1305_memory.o \
3939 ltc/mac/poly1305/poly1305_memory_multi.o ltc/mac/xcbc/xcbc_done.o ltc/mac/xcbc/xcbc_file.o \
66 ltc/encauth/ccm/ccm_init.obj ltc/encauth/ccm/ccm_memory.obj ltc/encauth/ccm/ccm_process.obj \
77 ltc/encauth/ccm/ccm_reset.obj ltc/encauth/chachapoly/chacha20poly1305_add_aad.obj \
88 ltc/encauth/chachapoly/chacha20poly1305_decrypt.obj ltc/encauth/chachapoly/chacha20poly1305_done.obj \
9 ltc/encauth/chachapoly/chacha20poly1305_encrypt.obj ltc/encauth/chachapoly/chacha20poly1305_inciv.obj \
10 ltc/encauth/chachapoly/chacha20poly1305_init.obj ltc/encauth/chachapoly/chacha20poly1305_memory.obj \
11 ltc/encauth/chachapoly/chacha20poly1305_setiv.obj ltc/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.obj \
12 ltc/encauth/eax/eax_addheader.obj ltc/encauth/eax/eax_decrypt.obj ltc/encauth/eax/eax_decrypt_verify_memory.obj \
13 ltc/encauth/eax/eax_done.obj ltc/encauth/eax/eax_encrypt.obj ltc/encauth/eax/eax_encrypt_authenticate_memory.obj \
9 ltc/encauth/chachapoly/chacha20poly1305_encrypt.obj ltc/encauth/chachapoly/chacha20poly1305_init.obj \
10 ltc/encauth/chachapoly/chacha20poly1305_memory.obj ltc/encauth/chachapoly/chacha20poly1305_setiv.obj \
11 ltc/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.obj ltc/encauth/eax/eax_addheader.obj \
12 ltc/encauth/eax/eax_decrypt.obj ltc/encauth/eax/eax_decrypt_verify_memory.obj ltc/encauth/eax/eax_done.obj \
13 ltc/encauth/eax/eax_encrypt.obj ltc/encauth/eax/eax_encrypt_authenticate_memory.obj \
1414 ltc/encauth/eax/eax_init.obj ltc/encauth/gcm/gcm_add_aad.obj ltc/encauth/gcm/gcm_add_iv.obj \
1515 ltc/encauth/gcm/gcm_done.obj ltc/encauth/gcm/gcm_gf_mult.obj ltc/encauth/gcm/gcm_init.obj \
1616 ltc/encauth/gcm/gcm_memory.obj ltc/encauth/gcm/gcm_mult_h.obj ltc/encauth/gcm/gcm_process.obj \
+0
-65
src/ltc/encauth/chachapoly/chacha20poly1305_inciv.c less more
0 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
1 *
2 * LibTomCrypt is a library that provides various cryptographic
3 * algorithms in a highly modular and flexible manner.
4 *
5 * The library is free for all purposes without any express
6 * guarantee it works.
7 */
8
9 #include "tomcrypt.h"
10
11 #ifdef LTC_CHACHA20POLY1305_MODE
12
13 /**
14 Reset ChaCha20Poly1305 state with incremented IV - used by https://shadowsocks.org/en/spec/AEAD-Ciphers.html
15 @param st The ChaCha20Poly1305 state
16 @param iv The IV data to add
17 @param inlen The length of the IV (must be 12 or 8)
18 @return CRYPT_OK on success
19 */
20 int chacha20poly1305_inciv(chacha20poly1305_state *st)
21 {
22 int err;
23 unsigned char tmp_iv[12];
24 unsigned long ivlen;
25
26 LTC_ARGCHK(st != NULL);
27
28 ivlen = st->chacha.ivlen;
29 if (ivlen == 12) {
30 STORE32L(st->chacha.input[13], tmp_iv + 0);
31 STORE32L(st->chacha.input[14], tmp_iv + 4);
32 STORE32L(st->chacha.input[15], tmp_iv + 8);
33 /* increment IV 96bit / 12 bytes */
34 if (!++tmp_iv[0] && !++tmp_iv[1] && !++tmp_iv[2] && !++tmp_iv[3] &&
35 !++tmp_iv[4] && !++tmp_iv[5] && !++tmp_iv[6] && !++tmp_iv[7] &&
36 !++tmp_iv[8] && !++tmp_iv[9] && !++tmp_iv[10] && !++tmp_iv[11])
37 {
38 err = CRYPT_ERROR; /* IV overflow */
39 }
40 else {
41 err = chacha20poly1305_setiv(st, tmp_iv, 12);
42 }
43 }
44 else if (ivlen == 8) {
45 STORE32L(st->chacha.input[14], tmp_iv + 0);
46 STORE32L(st->chacha.input[15], tmp_iv + 4);
47 /* increment IV 64bit / 8 bytes */
48 if (!++tmp_iv[0] && !++tmp_iv[1] && !++tmp_iv[2] && !++tmp_iv[3] &&
49 !++tmp_iv[4] && !++tmp_iv[5] && !++tmp_iv[6] && !++tmp_iv[7])
50 {
51 err = CRYPT_ERROR; /* IV overflow */
52 }
53 else {
54 err = chacha20poly1305_setiv(st, tmp_iv, 8);
55 }
56 }
57 else {
58 err = CRYPT_ERROR; /* invalid IV length */
59 }
60
61 return err;
62 }
63
64 #endif