increment_octets_be, increment_octets_le
Karel Miko
7 years ago
31 | 31 | - NEW: Crypt::Stream::ChaCha |
32 | 32 | - NEW: Crypt::Stream::RC4 |
33 | 33 | - NEW: Crypt::Stream::Sober128 |
34 | - NEW: functions in Crypt::Misc - increment_octets_be, increment_octets_le | |
34 | 35 | |
35 | 36 | 0.047 2017/04/05 |
36 | 37 | - fix #32 Compile "ar" step fails when Perl built with -flto (better version) |
475 | 475 | OUTPUT: |
476 | 476 | RETVAL |
477 | 477 | |
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 | ||
478 | 532 | ############################################################################### |
479 | 533 | |
480 | 534 | INCLUDE: inc/CryptX_Digest.xs.inc |
0 | 0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::ChaCha20Poly1305 |
1 | 1 | |
2 | 2 | Crypt::AuthEnc::ChaCha20Poly1305 |
3 | _new(SV * key, SV * nonce) | |
3 | _new(SV * key, SV * nonce = NULL) | |
4 | 4 | CODE: |
5 | 5 | { |
6 | 6 | int rv; |
8 | 8 | unsigned char *iv=NULL, *k=NULL; |
9 | 9 | |
10 | 10 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); |
11 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
12 | 11 | 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 | } | |
14 | 16 | |
15 | 17 | Newz(0, RETVAL, 1, struct chacha20poly1305_struct); |
16 | 18 | if (!RETVAL) croak("FATAL: Newz failed"); |
18 | 20 | rv = chacha20poly1305_init(&RETVAL->state, k, (unsigned long)k_len); |
19 | 21 | if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv)); |
20 | 22 | |
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 | } | |
23 | 27 | } |
24 | 28 | OUTPUT: |
25 | 29 | RETVAL |
35 | 39 | Newz(0, RETVAL, 1, struct chacha20poly1305_struct); |
36 | 40 | if (!RETVAL) croak("FATAL: Newz failed"); |
37 | 41 | 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 | } | |
50 | 42 | OUTPUT: |
51 | 43 | RETVAL |
52 | 44 |
0 | 0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM |
1 | 1 | |
2 | 2 | Crypt::AuthEnc::GCM |
3 | _new(char * cipher_name, SV * key) | |
3 | _new(char * cipher_name, SV * key, SV * nonce = NULL) | |
4 | 4 | CODE: |
5 | 5 | { |
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; | |
9 | 9 | |
10 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
10 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
11 | 11 | 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 | } | |
13 | 16 | |
14 | 17 | 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); | |
16 | 19 | |
17 | 20 | Newz(0, RETVAL, 1, struct gcm_struct); |
18 | 21 | if (!RETVAL) croak("FATAL: Newz failed"); |
19 | 22 | |
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)); | |
22 | 29 | } |
23 | 30 | } |
24 | 31 | OUTPUT: |
144 | 144 | |
145 | 145 | my $ae_new = $ae->clone; |
146 | 146 | |
147 | =head2 increment_iv | |
148 | ||
149 | $ae->increment_iv(); | |
150 | ||
151 | 147 | =head2 set_iv |
152 | 148 | |
153 | 149 | $ae->set_iv($iv); |
5 | 5 | |
6 | 6 | require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import'; |
7 | 7 | 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 | )] ); | |
9 | 14 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
10 | 15 | our @EXPORT = qw(); |
11 | 16 | |
33 | 38 | |
34 | 39 | sub decode_b64u { |
35 | 40 | 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]); | |
36 | 51 | } |
37 | 52 | |
38 | 53 | sub pem_to_der { |
320 | 335 | |
321 | 336 | Checks the given C<$uuid> string whether it matches V4 UUID format and returns C<0> (mismatch) or C<1> (match). |
322 | 337 | |
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 | ||
323 | 346 | =head1 SEE ALSO |
324 | 347 | |
325 | 348 | =over |
5 | 5 | ltc/encauth/ccm/ccm_done.o ltc/encauth/ccm/ccm_init.o ltc/encauth/ccm/ccm_memory.o \ |
6 | 6 | ltc/encauth/ccm/ccm_process.o ltc/encauth/ccm/ccm_reset.o ltc/encauth/chachapoly/chacha20poly1305_add_aad.o \ |
7 | 7 | 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 \ | |
37 | 37 | ltc/mac/pmac/pmac_ntz.o ltc/mac/pmac/pmac_process.o ltc/mac/pmac/pmac_shift_xor.o \ |
38 | 38 | ltc/mac/poly1305/poly1305.o ltc/mac/poly1305/poly1305_file.o ltc/mac/poly1305/poly1305_memory.o \ |
39 | 39 | ltc/mac/poly1305/poly1305_memory_multi.o ltc/mac/xcbc/xcbc_done.o ltc/mac/xcbc/xcbc_file.o \ |
6 | 6 | ltc/encauth/ccm/ccm_init.obj ltc/encauth/ccm/ccm_memory.obj ltc/encauth/ccm/ccm_process.obj \ |
7 | 7 | ltc/encauth/ccm/ccm_reset.obj ltc/encauth/chachapoly/chacha20poly1305_add_aad.obj \ |
8 | 8 | 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 \ | |
14 | 14 | ltc/encauth/eax/eax_init.obj ltc/encauth/gcm/gcm_add_aad.obj ltc/encauth/gcm/gcm_add_iv.obj \ |
15 | 15 | ltc/encauth/gcm/gcm_done.obj ltc/encauth/gcm/gcm_gf_mult.obj ltc/encauth/gcm/gcm_init.obj \ |
16 | 16 | ltc/encauth/gcm/gcm_memory.obj ltc/encauth/gcm/gcm_mult_h.obj ltc/encauth/gcm/gcm_process.obj \ |
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 |