Codebase list libcryptx-perl / 760b5b5
New upstream version 0.061 gregor herrmann 5 years ago
643 changed file(s) with 1929 addition(s) and 1858 deletion(s). Raw diff Collapse all Expand all
00 Changes for CryptX
1
2 0.061 2018-06-07
3 - bundled libtommath update (+stdint.h workaround)
4 - bundled libtomcrypt update (+necessary adaptation)
5 - fix #43 HP-UX 11.11 C-ANSI-C has no stdint.h
6 - fix #44 Anonymous unions are GNU and C11 (troubles with HP C/aC++, HP-UX 11.31/IA64)
17
28 0.060 2018-05-01
39 - bundled libtomcrypt update
254254 src/ltc/headers/tomcrypt_misc.h
255255 src/ltc/headers/tomcrypt_pk.h
256256 src/ltc/headers/tomcrypt_pkcs.h
257 src/ltc/headers/tomcrypt_private.h
257258 src/ltc/headers/tomcrypt_prng.h
258259 src/ltc/mac/blake2/blake2bmac.c
259260 src/ltc/mac/blake2/blake2bmac_file.c
482483 src/ltc/pk/ecc/ecc_free.c
483484 src/ltc/pk/ecc/ecc_get_curve.c
484485 src/ltc/pk/ecc/ecc_get_key.c
486 src/ltc/pk/ecc/ecc_get_oid_str.c
485487 src/ltc/pk/ecc/ecc_get_size.c
486488 src/ltc/pk/ecc/ecc_import.c
487489 src/ltc/pk/ecc/ecc_import_openssl.c
551553 src/ltc/stream/salsa20/salsa20_ivctr64.c
552554 src/ltc/stream/salsa20/salsa20_keystream.c
553555 src/ltc/stream/salsa20/salsa20_setup.c
556 src/ltc/stream/salsa20/xsalsa20_setup.c
554557 src/ltc/stream/sober128/sober128_stream.c
555558 src/ltc/stream/sober128/sober128tab.c
556559 src/ltc/stream/sosemanuk/sosemanuk.c
595598 src/ltm/bn_mp_gcd.c
596599 src/ltm/bn_mp_get_int.c
597600 src/ltm/bn_mp_get_long.c
598 src/ltm/bn_mp_get_long_long.c
599601 src/ltm/bn_mp_grow.c
600602 src/ltm/bn_mp_import.c
601603 src/ltm/bn_mp_init.c
652654 src/ltm/bn_mp_set.c
653655 src/ltm/bn_mp_set_int.c
654656 src/ltm/bn_mp_set_long.c
655 src/ltm/bn_mp_set_long_long.c
656657 src/ltm/bn_mp_shrink.c
657658 src/ltm/bn_mp_signed_bin_size.c
658659 src/ltm/bn_mp_sqr.c
4444 "url" : "https://github.com/DCIT/perl-CryptX"
4545 }
4646 },
47 "version" : "0.060",
47 "version" : "0.061",
4848 "x_serialization_backend" : "JSON::PP version 2.94"
4949 }
2121 resources:
2222 bugtracker: https://github.com/DCIT/perl-CryptX/issues
2323 repository: https://github.com/DCIT/perl-CryptX
24 version: '0.060'
24 version: '0.061'
2525 x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
4949 if (self->key.type != -1) { ecc_free(&self->key); self->key.type = -1; }
5050 rv = ecc_import_openssl(data, (unsigned long)data_len, &self->key);
5151 if (rv != CRYPT_OK) croak("FATAL: ecc_import_openssl failed: %s", error_to_string(rv));
52 XPUSHs(ST(0)); /* return self */
53 }
54
55 void
56 _import_old(Crypt::PK::ECC self, SV * key_data)
57 PPCODE:
58 {
59 int rv;
60 unsigned char *data=NULL;
61 STRLEN data_len=0;
62
63 data = (unsigned char *)SvPVbyte(key_data, data_len);
64 if (self->key.type != -1) { ecc_free(&self->key); self->key.type = -1; }
65 rv = ecc_import(data, (unsigned long)data_len, &self->key);
66 if (rv != CRYPT_OK) croak("FATAL: ecc_import failed: %s", error_to_string(rv));
5267 XPUSHs(ST(0)); /* return self */
5368 }
5469
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( ccm_encrypt_authenticate ccm_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( chacha20poly1305_encrypt_authenticate chacha20poly1305_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( eax_encrypt_authenticate eax_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( gcm_encrypt_authenticate gcm_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( ocb_encrypt_authenticate ocb_decrypt_verify )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 ### not used
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::Checksum Exporter);
77 our %EXPORT_TAGS = ( all => [qw( adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::Checksum Exporter);
77 our %EXPORT_TAGS = ( all => [qw( crc32_data crc32_data_hex crc32_data_int crc32_file crc32_file_hex crc32_file_int )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw/ adler32_data adler32_data_hex adler32_data_int adler32_file adler32_file_hex adler32_file_int
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Cipher);
99
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use Carp;
77 $Carp::Internal{(__PACKAGE__)}++;
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_160 blake2b_160_hex blake2b_160_b64 blake2b_160_b64u blake2b_160_file blake2b_160_file_hex blake2b_160_file_b64 blake2b_160_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_256 blake2b_256_hex blake2b_256_b64 blake2b_256_b64u blake2b_256_file blake2b_256_file_hex blake2b_256_file_b64 blake2b_256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_384 blake2b_384_hex blake2b_384_b64 blake2b_384_b64u blake2b_384_file blake2b_384_file_hex blake2b_384_file_b64 blake2b_384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b_512 blake2b_512_hex blake2b_512_b64 blake2b_512_b64u blake2b_512_file blake2b_512_file_hex blake2b_512_file_b64 blake2b_512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_128 blake2s_128_hex blake2s_128_b64 blake2s_128_b64u blake2s_128_file blake2s_128_file_hex blake2s_128_file_b64 blake2s_128_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_160 blake2s_160_hex blake2s_160_b64 blake2s_160_b64u blake2s_160_file blake2s_160_file_hex blake2s_160_file_b64 blake2s_160_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_224 blake2s_224_hex blake2s_224_b64 blake2s_224_b64u blake2s_224_file blake2s_224_file_hex blake2s_224_file_b64 blake2s_224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s_256 blake2s_256_hex blake2s_256_b64 blake2s_256_b64u blake2s_256_file blake2s_256_file_hex blake2s_256_file_b64 blake2s_256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( chaes chaes_hex chaes_b64 chaes_b64u chaes_file chaes_file_hex chaes_file_b64 chaes_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak224 keccak224_hex keccak224_b64 keccak224_b64u keccak224_file keccak224_file_hex keccak224_file_b64 keccak224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak256 keccak256_hex keccak256_b64 keccak256_b64u keccak256_file keccak256_file_hex keccak256_file_b64 keccak256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak384 keccak384_hex keccak384_b64 keccak384_b64u keccak384_file keccak384_file_hex keccak384_file_b64 keccak384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( keccak512 keccak512_hex keccak512_b64 keccak512_b64u keccak512_file keccak512_file_hex keccak512_file_b64 keccak512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( md2 md2_hex md2_b64 md2_b64u md2_file md2_file_hex md2_file_b64 md2_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( md4 md4_hex md4_b64 md4_b64u md4_file md4_file_hex md4_file_b64 md4_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( md5 md5_hex md5_b64 md5_b64u md5_file md5_file_hex md5_file_b64 md5_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd128 ripemd128_hex ripemd128_b64 ripemd128_b64u ripemd128_file ripemd128_file_hex ripemd128_file_b64 ripemd128_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd160 ripemd160_hex ripemd160_b64 ripemd160_b64u ripemd160_file ripemd160_file_hex ripemd160_file_b64 ripemd160_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd256 ripemd256_hex ripemd256_b64 ripemd256_b64u ripemd256_file ripemd256_file_hex ripemd256_file_b64 ripemd256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( ripemd320 ripemd320_hex ripemd320_b64 ripemd320_b64u ripemd320_file ripemd320_file_hex ripemd320_file_b64 ripemd320_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha1 sha1_hex sha1_b64 sha1_b64u sha1_file sha1_file_hex sha1_file_b64 sha1_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha224 sha224_hex sha224_b64 sha224_b64u sha224_file sha224_file_hex sha224_file_b64 sha224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha256 sha256_hex sha256_b64 sha256_b64u sha256_file sha256_file_hex sha256_file_b64 sha256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha384 sha384_hex sha384_b64 sha384_b64u sha384_file sha384_file_hex sha384_file_b64 sha384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_224 sha3_224_hex sha3_224_b64 sha3_224_b64u sha3_224_file sha3_224_file_hex sha3_224_file_b64 sha3_224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_256 sha3_256_hex sha3_256_b64 sha3_256_b64u sha3_256_file sha3_256_file_hex sha3_256_file_b64 sha3_256_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_384 sha3_384_hex sha3_384_b64 sha3_384_b64u sha3_384_file sha3_384_file_hex sha3_384_file_b64 sha3_384_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha3_512 sha3_512_hex sha3_512_b64 sha3_512_b64u sha3_512_file sha3_512_file_hex sha3_512_file_b64 sha3_512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha512 sha512_hex sha512_b64 sha512_b64u sha512_file sha512_file_hex sha512_file_b64 sha512_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha512_224 sha512_224_hex sha512_224_b64 sha512_224_b64u sha512_224_file sha512_224_file_hex sha512_224_file_b64 sha512_224_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( sha512_256 sha512_256_hex sha512_256_b64 sha512_256_b64u sha512_256_file sha512_256_file_hex sha512_256_file_b64 sha512_256_file_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use Carp;
77 $Carp::Internal{(__PACKAGE__)}++;
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( tiger192 tiger192_hex tiger192_b64 tiger192_b64u tiger192_file tiger192_file_hex tiger192_file_b64 tiger192_file_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Digest Exporter);
99 our %EXPORT_TAGS = ( all => [qw( whirlpool whirlpool_hex whirlpool_b64 whirlpool_b64u whirlpool_file whirlpool_file_hex whirlpool_file_b64 whirlpool_file_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( digest_data digest_data_hex digest_data_b64 digest_data_b64u digest_file digest_file_hex digest_file_b64 digest_file_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw(pbkdf1 pbkdf2 hkdf hkdf_expand hkdf_extract)] );
2121 Crypt::KeyDerivation - PBKDF1, PBKDF2 and HKDF key derivation functions
2222
2323 =head1 SYNOPSIS
24
25 use Crypt::KeyDerivation ':all';
2426
2527 ### PBKDF1/2
2628 $derived_key1 = pbkdf1($password, $salt, $iteration_count, $hash_name, $len);
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2b blake2b_hex blake2b_b64 blake2b_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( blake2s blake2s_hex blake2s_b64 blake2s_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( f9 f9_hex f9_b64 f9_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( hmac hmac_hex hmac_b64 hmac_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( omac omac_hex omac_b64 omac_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( pmac pmac_hex pmac_b64 pmac_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( pelican pelican_hex pelican_b64 pelican_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( poly1305 poly1305_hex poly1305_b64 poly1305_b64u )] );
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use base qw(Crypt::Mac Exporter);
99 our %EXPORT_TAGS = ( all => [qw( xcbc xcbc_hex xcbc_b64 xcbc_b64u )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use Carp;
77 $Carp::Internal{(__PACKAGE__)}++;
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 5.57 'import';
77 use Carp 'croak';
215215 my ($cipher, undef, $klen, $mode) = $cipher_name =~ /^(AES|CAMELLIA|DES|DES-EDE3|SEED)(-(\d+))?-(CBC|CFB|ECB|OFB)$/i;
216216 croak "FATAL: unsupported cipher '$cipher_name'" unless $cipher && $mode;
217217 $cipher = $trans{$cipher} || $cipher;
218 $klen = 192 if $cipher eq 'DES_EDE';
219 $klen = 64 if $cipher eq 'DES';
220 $klen = 128 if $cipher eq 'SEED';
218221 $klen = $klen ? int($klen/8) : Crypt::Cipher::min_keysize($cipher);
219222 my $ilen = Crypt::Cipher::blocksize($cipher);
220223 croak "FATAL: unsupported cipher '$cipher_name'" unless $klen && $ilen;
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use Crypt::Cipher;
99
33
44 use strict;
55 use warnings;
6 our $VERSION = '0.060';
6 our $VERSION = '0.061';
77
88 use Crypt::Cipher;
99
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 ### not used
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( dh_shared_secret )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( dsa_encrypt dsa_decrypt dsa_sign_message dsa_verify_message dsa_sign_hash dsa_verify_hash )] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw( ecc_encrypt ecc_decrypt ecc_sign_message ecc_verify_message ecc_sign_hash ecc_verify_hash ecc_shared_secret )] );
215215
216216 if ($data =~ /-----BEGIN (EC PRIVATE|EC PUBLIC|PUBLIC) KEY-----(.*?)-----END/sg) {
217217 $data = pem_to_der($data, $password);
218 return $self->_import($data);
218 my $rv = eval { $self->_import($data) } || eval { $self->_import_old($data) };
219 return $rv if $rv;
219220 }
220221 elsif ($data =~ /-----BEGIN PRIVATE KEY-----(.*?)-----END/sg) {
221222 $data = pem_to_der($data, $password);
252253 return $self->import_key_raw($pubkey, "$2") if $pubkey && $typ =~ /^ecdsa-(.+?)-(.*)$/;
253254 }
254255 else {
255 my $rv = eval { $self->_import($data) } || eval { $self->_import_pkcs8($data, $password) } || eval { $self->_import_x509($data) };
256 my $rv = eval { $self->_import($data) } ||
257 eval { $self->_import_old($data) } ||
258 eval { $self->_import_pkcs8($data, $password) } ||
259 eval { $self->_import_x509($data) };
256260 return $rv if $rv;
257261 }
258262 croak "FATAL: invalid or unsupported EC key format";
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw(rsa_encrypt rsa_decrypt rsa_sign_message rsa_verify_message rsa_sign_hash rsa_verify_hash)] );
99 our @EXPORT = qw();
1010
1111 use Carp;
12 $Carp::Internal{(__PACKAGE__)}++;
1213 use CryptX;
1314 use Crypt::Digest qw(digest_data digest_data_b64u);
1415 use Crypt::Misc qw(read_rawfile encode_b64u decode_b64u encode_b64 decode_b64 pem_to_der der_to_pem);
2122
2223 sub export_key_pem {
2324 my ($self, $type, $password, $cipher) = @_;
25 local $SIG{__DIE__} = \&CryptX::_croak;
2426 my $key = $self->export_key_der($type||'');
2527 return unless $key;
2628
3739
3840 sub export_key_jwk {
3941 my ($self, $type, $wanthash) = @_;
42 local $SIG{__DIE__} = \&CryptX::_croak;
4043 my $kh = $self->key2hash;
4144 if ($type eq 'private') {
4245 return unless $kh->{N} && $kh->{e} && $kh->{d} && $kh->{p} && $kh->{q} && $kh->{dP} && $kh->{dQ} && $kh->{qP};
7275
7376 sub export_key_jwk_thumbprint {
7477 my ($self, $hash_name) = @_;
78 local $SIG{__DIE__} = \&CryptX::_croak;
7579 $hash_name ||= 'SHA256';
7680 my $h = $self->export_key_jwk('public', 1);
7781 my $json = CryptX::_encode_json({kty=>$h->{kty}, n=>$h->{n}, e=>$h->{e}});
8084
8185 sub import_key {
8286 my ($self, $key, $password) = @_;
87 local $SIG{__DIE__} = \&CryptX::_croak;
8388 croak "FATAL: undefined key" unless $key;
8489
8590 # special case
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use Carp;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use base qw(Crypt::PRNG Exporter);
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require Exporter; our @ISA = qw(Exporter); ### use Exporter 'import';
77 our %EXPORT_TAGS = ( all => [qw(random_bytes random_bytes_hex random_bytes_b64 random_bytes_b64u random_string random_string_from rand irand)] );
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
11
22 use strict;
33 use warnings ;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 require XSLoader;
77 XSLoader::load('CryptX', $VERSION);
11
22 use strict;
33 use warnings;
4 our $VERSION = '0.060';
4 our $VERSION = '0.061';
55
66 use CryptX;
77
102102 ltc/pk/dsa/dsa_verify_hash.o ltc/pk/dsa/dsa_verify_key.o ltc/pk/ecc/ecc.o ltc/pk/ecc/ecc_ansi_x963_export.o \
103103 ltc/pk/ecc/ecc_ansi_x963_import.o ltc/pk/ecc/ecc_decrypt_key.o ltc/pk/ecc/ecc_encrypt_key.o \
104104 ltc/pk/ecc/ecc_export.o ltc/pk/ecc/ecc_export_openssl.o ltc/pk/ecc/ecc_free.o ltc/pk/ecc/ecc_get_curve.o \
105 ltc/pk/ecc/ecc_get_key.o ltc/pk/ecc/ecc_get_size.o ltc/pk/ecc/ecc_import.o ltc/pk/ecc/ecc_import_openssl.o \
106 ltc/pk/ecc/ecc_import_pkcs8.o ltc/pk/ecc/ecc_import_x509.o ltc/pk/ecc/ecc_make_key.o \
107 ltc/pk/ecc/ecc_set_dp.o ltc/pk/ecc/ecc_set_dp_internal.o ltc/pk/ecc/ecc_set_key.o \
108 ltc/pk/ecc/ecc_shared_secret.o ltc/pk/ecc/ecc_sign_hash.o ltc/pk/ecc/ecc_sizes.o \
109 ltc/pk/ecc/ecc_verify_hash.o ltc/pk/ecc/ltc_ecc_export_point.o ltc/pk/ecc/ltc_ecc_import_point.o \
110 ltc/pk/ecc/ltc_ecc_is_point.o ltc/pk/ecc/ltc_ecc_is_point_at_infinity.o ltc/pk/ecc/ltc_ecc_map.o \
111 ltc/pk/ecc/ltc_ecc_mul2add.o ltc/pk/ecc/ltc_ecc_mulmod.o ltc/pk/ecc/ltc_ecc_mulmod_timing.o \
112 ltc/pk/ecc/ltc_ecc_points.o ltc/pk/ecc/ltc_ecc_projective_add_point.o ltc/pk/ecc/ltc_ecc_projective_dbl_point.o \
113 ltc/pk/ecc/ltc_ecc_verify_key.o ltc/pk/pkcs1/pkcs_1_i2osp.o ltc/pk/pkcs1/pkcs_1_mgf1.o \
114 ltc/pk/pkcs1/pkcs_1_oaep_decode.o ltc/pk/pkcs1/pkcs_1_oaep_encode.o ltc/pk/pkcs1/pkcs_1_os2ip.o \
115 ltc/pk/pkcs1/pkcs_1_pss_decode.o ltc/pk/pkcs1/pkcs_1_pss_encode.o ltc/pk/pkcs1/pkcs_1_v1_5_decode.o \
116 ltc/pk/pkcs1/pkcs_1_v1_5_encode.o ltc/pk/rsa/rsa_decrypt_key.o ltc/pk/rsa/rsa_encrypt_key.o \
117 ltc/pk/rsa/rsa_export.o ltc/pk/rsa/rsa_exptmod.o ltc/pk/rsa/rsa_free.o ltc/pk/rsa/rsa_get_size.o \
118 ltc/pk/rsa/rsa_import.o ltc/pk/rsa/rsa_import_pkcs8.o ltc/pk/rsa/rsa_import_x509.o \
105 ltc/pk/ecc/ecc_get_key.o ltc/pk/ecc/ecc_get_oid_str.o ltc/pk/ecc/ecc_get_size.o ltc/pk/ecc/ecc_import.o \
106 ltc/pk/ecc/ecc_import_openssl.o ltc/pk/ecc/ecc_import_pkcs8.o ltc/pk/ecc/ecc_import_x509.o \
107 ltc/pk/ecc/ecc_make_key.o ltc/pk/ecc/ecc_set_dp.o ltc/pk/ecc/ecc_set_dp_internal.o \
108 ltc/pk/ecc/ecc_set_key.o ltc/pk/ecc/ecc_shared_secret.o ltc/pk/ecc/ecc_sign_hash.o \
109 ltc/pk/ecc/ecc_sizes.o ltc/pk/ecc/ecc_verify_hash.o ltc/pk/ecc/ltc_ecc_export_point.o \
110 ltc/pk/ecc/ltc_ecc_import_point.o ltc/pk/ecc/ltc_ecc_is_point.o ltc/pk/ecc/ltc_ecc_is_point_at_infinity.o \
111 ltc/pk/ecc/ltc_ecc_map.o ltc/pk/ecc/ltc_ecc_mul2add.o ltc/pk/ecc/ltc_ecc_mulmod.o \
112 ltc/pk/ecc/ltc_ecc_mulmod_timing.o ltc/pk/ecc/ltc_ecc_points.o ltc/pk/ecc/ltc_ecc_projective_add_point.o \
113 ltc/pk/ecc/ltc_ecc_projective_dbl_point.o ltc/pk/ecc/ltc_ecc_verify_key.o ltc/pk/pkcs1/pkcs_1_i2osp.o \
114 ltc/pk/pkcs1/pkcs_1_mgf1.o ltc/pk/pkcs1/pkcs_1_oaep_decode.o ltc/pk/pkcs1/pkcs_1_oaep_encode.o \
115 ltc/pk/pkcs1/pkcs_1_os2ip.o ltc/pk/pkcs1/pkcs_1_pss_decode.o ltc/pk/pkcs1/pkcs_1_pss_encode.o \
116 ltc/pk/pkcs1/pkcs_1_v1_5_decode.o ltc/pk/pkcs1/pkcs_1_v1_5_encode.o ltc/pk/rsa/rsa_decrypt_key.o \
117 ltc/pk/rsa/rsa_encrypt_key.o ltc/pk/rsa/rsa_export.o ltc/pk/rsa/rsa_exptmod.o ltc/pk/rsa/rsa_free.o \
118 ltc/pk/rsa/rsa_get_size.o ltc/pk/rsa/rsa_import.o ltc/pk/rsa/rsa_import_pkcs8.o ltc/pk/rsa/rsa_import_x509.o \
119119 ltc/pk/rsa/rsa_make_key.o ltc/pk/rsa/rsa_set.o ltc/pk/rsa/rsa_sign_hash.o ltc/pk/rsa/rsa_sign_saltlen_get.o \
120120 ltc/pk/rsa/rsa_verify_hash.o ltc/prngs/chacha20.o ltc/prngs/fortuna.o ltc/prngs/rc4.o \
121121 ltc/prngs/rng_get_bytes.o ltc/prngs/rng_make_prng.o ltc/prngs/sober128.o ltc/prngs/sprng.o \
123123 ltc/stream/chacha/chacha_ivctr32.o ltc/stream/chacha/chacha_ivctr64.o ltc/stream/chacha/chacha_keystream.o \
124124 ltc/stream/chacha/chacha_setup.o ltc/stream/rabbit/rabbit.o ltc/stream/rc4/rc4_stream.o \
125125 ltc/stream/salsa20/salsa20_crypt.o ltc/stream/salsa20/salsa20_done.o ltc/stream/salsa20/salsa20_ivctr64.o \
126 ltc/stream/salsa20/salsa20_keystream.o ltc/stream/salsa20/salsa20_setup.o ltc/stream/sober128/sober128_stream.o \
127 ltc/stream/sosemanuk/sosemanuk.o ltm/bncore.o ltm/bn_error.o ltm/bn_fast_mp_invmod.o \
128 ltm/bn_fast_mp_montgomery_reduce.o ltm/bn_fast_s_mp_mul_digs.o ltm/bn_fast_s_mp_mul_high_digs.o \
129 ltm/bn_fast_s_mp_sqr.o ltm/bn_mp_2expt.o ltm/bn_mp_abs.o ltm/bn_mp_add.o ltm/bn_mp_addmod.o \
130 ltm/bn_mp_add_d.o ltm/bn_mp_and.o ltm/bn_mp_clamp.o ltm/bn_mp_clear.o ltm/bn_mp_clear_multi.o \
131 ltm/bn_mp_cmp.o ltm/bn_mp_cmp_d.o ltm/bn_mp_cmp_mag.o ltm/bn_mp_cnt_lsb.o ltm/bn_mp_copy.o \
132 ltm/bn_mp_count_bits.o ltm/bn_mp_div.o ltm/bn_mp_div_2.o ltm/bn_mp_div_2d.o ltm/bn_mp_div_3.o \
133 ltm/bn_mp_div_d.o ltm/bn_mp_dr_is_modulus.o ltm/bn_mp_dr_reduce.o ltm/bn_mp_dr_setup.o \
134 ltm/bn_mp_exch.o ltm/bn_mp_export.o ltm/bn_mp_exptmod.o ltm/bn_mp_exptmod_fast.o \
135 ltm/bn_mp_expt_d.o ltm/bn_mp_expt_d_ex.o ltm/bn_mp_exteuclid.o ltm/bn_mp_fread.o \
136 ltm/bn_mp_fwrite.o ltm/bn_mp_gcd.o ltm/bn_mp_get_int.o ltm/bn_mp_get_long.o ltm/bn_mp_get_long_long.o \
126 ltc/stream/salsa20/salsa20_keystream.o ltc/stream/salsa20/salsa20_setup.o ltc/stream/salsa20/xsalsa20_setup.o \
127 ltc/stream/sober128/sober128_stream.o ltc/stream/sosemanuk/sosemanuk.o ltm/bncore.o \
128 ltm/bn_error.o ltm/bn_fast_mp_invmod.o ltm/bn_fast_mp_montgomery_reduce.o ltm/bn_fast_s_mp_mul_digs.o \
129 ltm/bn_fast_s_mp_mul_high_digs.o ltm/bn_fast_s_mp_sqr.o ltm/bn_mp_2expt.o ltm/bn_mp_abs.o \
130 ltm/bn_mp_add.o ltm/bn_mp_addmod.o ltm/bn_mp_add_d.o ltm/bn_mp_and.o ltm/bn_mp_clamp.o \
131 ltm/bn_mp_clear.o ltm/bn_mp_clear_multi.o ltm/bn_mp_cmp.o ltm/bn_mp_cmp_d.o ltm/bn_mp_cmp_mag.o \
132 ltm/bn_mp_cnt_lsb.o ltm/bn_mp_copy.o ltm/bn_mp_count_bits.o ltm/bn_mp_div.o ltm/bn_mp_div_2.o \
133 ltm/bn_mp_div_2d.o ltm/bn_mp_div_3.o ltm/bn_mp_div_d.o ltm/bn_mp_dr_is_modulus.o \
134 ltm/bn_mp_dr_reduce.o ltm/bn_mp_dr_setup.o ltm/bn_mp_exch.o ltm/bn_mp_export.o ltm/bn_mp_exptmod.o \
135 ltm/bn_mp_exptmod_fast.o ltm/bn_mp_expt_d.o ltm/bn_mp_expt_d_ex.o ltm/bn_mp_exteuclid.o \
136 ltm/bn_mp_fread.o ltm/bn_mp_fwrite.o ltm/bn_mp_gcd.o ltm/bn_mp_get_int.o ltm/bn_mp_get_long.o \
137137 ltm/bn_mp_grow.o ltm/bn_mp_import.o ltm/bn_mp_init.o ltm/bn_mp_init_copy.o ltm/bn_mp_init_multi.o \
138138 ltm/bn_mp_init_set.o ltm/bn_mp_init_set_int.o ltm/bn_mp_init_size.o ltm/bn_mp_invmod.o \
139139 ltm/bn_mp_invmod_slow.o ltm/bn_mp_is_square.o ltm/bn_mp_jacobi.o ltm/bn_mp_karatsuba_mul.o \
148148 ltm/bn_mp_reduce.o ltm/bn_mp_reduce_2k.o ltm/bn_mp_reduce_2k_l.o ltm/bn_mp_reduce_2k_setup.o \
149149 ltm/bn_mp_reduce_2k_setup_l.o ltm/bn_mp_reduce_is_2k.o ltm/bn_mp_reduce_is_2k_l.o \
150150 ltm/bn_mp_reduce_setup.o ltm/bn_mp_rshd.o ltm/bn_mp_set.o ltm/bn_mp_set_int.o ltm/bn_mp_set_long.o \
151 ltm/bn_mp_set_long_long.o ltm/bn_mp_shrink.o ltm/bn_mp_signed_bin_size.o ltm/bn_mp_sqr.o \
152 ltm/bn_mp_sqrmod.o ltm/bn_mp_sqrt.o ltm/bn_mp_sqrtmod_prime.o ltm/bn_mp_sub.o ltm/bn_mp_submod.o \
153 ltm/bn_mp_sub_d.o ltm/bn_mp_toom_mul.o ltm/bn_mp_toom_sqr.o ltm/bn_mp_toradix.o ltm/bn_mp_toradix_n.o \
151 ltm/bn_mp_shrink.o ltm/bn_mp_signed_bin_size.o ltm/bn_mp_sqr.o ltm/bn_mp_sqrmod.o \
152 ltm/bn_mp_sqrt.o ltm/bn_mp_sqrtmod_prime.o ltm/bn_mp_sub.o ltm/bn_mp_submod.o ltm/bn_mp_sub_d.o \
153 ltm/bn_mp_toom_mul.o ltm/bn_mp_toom_sqr.o ltm/bn_mp_toradix.o ltm/bn_mp_toradix_n.o \
154154 ltm/bn_mp_to_signed_bin.o ltm/bn_mp_to_signed_bin_n.o ltm/bn_mp_to_unsigned_bin.o \
155155 ltm/bn_mp_to_unsigned_bin_n.o ltm/bn_mp_unsigned_bin_size.o ltm/bn_mp_xor.o ltm/bn_mp_zero.o \
156156 ltm/bn_prime_tab.o ltm/bn_reverse.o ltm/bn_s_mp_add.o ltm/bn_s_mp_exptmod.o ltm/bn_s_mp_mul_digs.o \
109109 ltc/pk/dsa/dsa_verify_key.obj ltc/pk/ecc/ecc.obj ltc/pk/ecc/ecc_ansi_x963_export.obj \
110110 ltc/pk/ecc/ecc_ansi_x963_import.obj ltc/pk/ecc/ecc_decrypt_key.obj ltc/pk/ecc/ecc_encrypt_key.obj \
111111 ltc/pk/ecc/ecc_export.obj ltc/pk/ecc/ecc_export_openssl.obj ltc/pk/ecc/ecc_free.obj \
112 ltc/pk/ecc/ecc_get_curve.obj ltc/pk/ecc/ecc_get_key.obj ltc/pk/ecc/ecc_get_size.obj \
113 ltc/pk/ecc/ecc_import.obj ltc/pk/ecc/ecc_import_openssl.obj ltc/pk/ecc/ecc_import_pkcs8.obj \
114 ltc/pk/ecc/ecc_import_x509.obj ltc/pk/ecc/ecc_make_key.obj ltc/pk/ecc/ecc_set_dp.obj \
115 ltc/pk/ecc/ecc_set_dp_internal.obj ltc/pk/ecc/ecc_set_key.obj ltc/pk/ecc/ecc_shared_secret.obj \
116 ltc/pk/ecc/ecc_sign_hash.obj ltc/pk/ecc/ecc_sizes.obj ltc/pk/ecc/ecc_verify_hash.obj \
117 ltc/pk/ecc/ltc_ecc_export_point.obj ltc/pk/ecc/ltc_ecc_import_point.obj ltc/pk/ecc/ltc_ecc_is_point.obj \
118 ltc/pk/ecc/ltc_ecc_is_point_at_infinity.obj ltc/pk/ecc/ltc_ecc_map.obj ltc/pk/ecc/ltc_ecc_mul2add.obj \
119 ltc/pk/ecc/ltc_ecc_mulmod.obj ltc/pk/ecc/ltc_ecc_mulmod_timing.obj ltc/pk/ecc/ltc_ecc_points.obj \
120 ltc/pk/ecc/ltc_ecc_projective_add_point.obj ltc/pk/ecc/ltc_ecc_projective_dbl_point.obj \
112 ltc/pk/ecc/ecc_get_curve.obj ltc/pk/ecc/ecc_get_key.obj ltc/pk/ecc/ecc_get_oid_str.obj \
113 ltc/pk/ecc/ecc_get_size.obj ltc/pk/ecc/ecc_import.obj ltc/pk/ecc/ecc_import_openssl.obj \
114 ltc/pk/ecc/ecc_import_pkcs8.obj ltc/pk/ecc/ecc_import_x509.obj ltc/pk/ecc/ecc_make_key.obj \
115 ltc/pk/ecc/ecc_set_dp.obj ltc/pk/ecc/ecc_set_dp_internal.obj ltc/pk/ecc/ecc_set_key.obj \
116 ltc/pk/ecc/ecc_shared_secret.obj ltc/pk/ecc/ecc_sign_hash.obj ltc/pk/ecc/ecc_sizes.obj \
117 ltc/pk/ecc/ecc_verify_hash.obj ltc/pk/ecc/ltc_ecc_export_point.obj ltc/pk/ecc/ltc_ecc_import_point.obj \
118 ltc/pk/ecc/ltc_ecc_is_point.obj ltc/pk/ecc/ltc_ecc_is_point_at_infinity.obj ltc/pk/ecc/ltc_ecc_map.obj \
119 ltc/pk/ecc/ltc_ecc_mul2add.obj ltc/pk/ecc/ltc_ecc_mulmod.obj ltc/pk/ecc/ltc_ecc_mulmod_timing.obj \
120 ltc/pk/ecc/ltc_ecc_points.obj ltc/pk/ecc/ltc_ecc_projective_add_point.obj ltc/pk/ecc/ltc_ecc_projective_dbl_point.obj \
121121 ltc/pk/ecc/ltc_ecc_verify_key.obj ltc/pk/pkcs1/pkcs_1_i2osp.obj ltc/pk/pkcs1/pkcs_1_mgf1.obj \
122122 ltc/pk/pkcs1/pkcs_1_oaep_decode.obj ltc/pk/pkcs1/pkcs_1_oaep_encode.obj ltc/pk/pkcs1/pkcs_1_os2ip.obj \
123123 ltc/pk/pkcs1/pkcs_1_pss_decode.obj ltc/pk/pkcs1/pkcs_1_pss_encode.obj ltc/pk/pkcs1/pkcs_1_v1_5_decode.obj \
131131 ltc/stream/chacha/chacha_ivctr32.obj ltc/stream/chacha/chacha_ivctr64.obj ltc/stream/chacha/chacha_keystream.obj \
132132 ltc/stream/chacha/chacha_setup.obj ltc/stream/rabbit/rabbit.obj ltc/stream/rc4/rc4_stream.obj \
133133 ltc/stream/salsa20/salsa20_crypt.obj ltc/stream/salsa20/salsa20_done.obj ltc/stream/salsa20/salsa20_ivctr64.obj \
134 ltc/stream/salsa20/salsa20_keystream.obj ltc/stream/salsa20/salsa20_setup.obj ltc/stream/sober128/sober128_stream.obj \
135 ltc/stream/sosemanuk/sosemanuk.obj ltm/bncore.obj ltm/bn_error.obj ltm/bn_fast_mp_invmod.obj \
136 ltm/bn_fast_mp_montgomery_reduce.obj ltm/bn_fast_s_mp_mul_digs.obj ltm/bn_fast_s_mp_mul_high_digs.obj \
137 ltm/bn_fast_s_mp_sqr.obj ltm/bn_mp_2expt.obj ltm/bn_mp_abs.obj ltm/bn_mp_add.obj \
138 ltm/bn_mp_addmod.obj ltm/bn_mp_add_d.obj ltm/bn_mp_and.obj ltm/bn_mp_clamp.obj ltm/bn_mp_clear.obj \
139 ltm/bn_mp_clear_multi.obj ltm/bn_mp_cmp.obj ltm/bn_mp_cmp_d.obj ltm/bn_mp_cmp_mag.obj \
140 ltm/bn_mp_cnt_lsb.obj ltm/bn_mp_copy.obj ltm/bn_mp_count_bits.obj ltm/bn_mp_div.obj \
141 ltm/bn_mp_div_2.obj ltm/bn_mp_div_2d.obj ltm/bn_mp_div_3.obj ltm/bn_mp_div_d.obj \
134 ltc/stream/salsa20/salsa20_keystream.obj ltc/stream/salsa20/salsa20_setup.obj ltc/stream/salsa20/xsalsa20_setup.obj \
135 ltc/stream/sober128/sober128_stream.obj ltc/stream/sosemanuk/sosemanuk.obj ltm/bncore.obj \
136 ltm/bn_error.obj ltm/bn_fast_mp_invmod.obj ltm/bn_fast_mp_montgomery_reduce.obj ltm/bn_fast_s_mp_mul_digs.obj \
137 ltm/bn_fast_s_mp_mul_high_digs.obj ltm/bn_fast_s_mp_sqr.obj ltm/bn_mp_2expt.obj ltm/bn_mp_abs.obj \
138 ltm/bn_mp_add.obj ltm/bn_mp_addmod.obj ltm/bn_mp_add_d.obj ltm/bn_mp_and.obj ltm/bn_mp_clamp.obj \
139 ltm/bn_mp_clear.obj ltm/bn_mp_clear_multi.obj ltm/bn_mp_cmp.obj ltm/bn_mp_cmp_d.obj \
140 ltm/bn_mp_cmp_mag.obj ltm/bn_mp_cnt_lsb.obj ltm/bn_mp_copy.obj ltm/bn_mp_count_bits.obj \
141 ltm/bn_mp_div.obj ltm/bn_mp_div_2.obj ltm/bn_mp_div_2d.obj ltm/bn_mp_div_3.obj ltm/bn_mp_div_d.obj \
142142 ltm/bn_mp_dr_is_modulus.obj ltm/bn_mp_dr_reduce.obj ltm/bn_mp_dr_setup.obj ltm/bn_mp_exch.obj \
143143 ltm/bn_mp_export.obj ltm/bn_mp_exptmod.obj ltm/bn_mp_exptmod_fast.obj ltm/bn_mp_expt_d.obj \
144144 ltm/bn_mp_expt_d_ex.obj ltm/bn_mp_exteuclid.obj ltm/bn_mp_fread.obj ltm/bn_mp_fwrite.obj \
145 ltm/bn_mp_gcd.obj ltm/bn_mp_get_int.obj ltm/bn_mp_get_long.obj ltm/bn_mp_get_long_long.obj \
146 ltm/bn_mp_grow.obj ltm/bn_mp_import.obj ltm/bn_mp_init.obj ltm/bn_mp_init_copy.obj \
147 ltm/bn_mp_init_multi.obj ltm/bn_mp_init_set.obj ltm/bn_mp_init_set_int.obj ltm/bn_mp_init_size.obj \
148 ltm/bn_mp_invmod.obj ltm/bn_mp_invmod_slow.obj ltm/bn_mp_is_square.obj ltm/bn_mp_jacobi.obj \
149 ltm/bn_mp_karatsuba_mul.obj ltm/bn_mp_karatsuba_sqr.obj ltm/bn_mp_lcm.obj ltm/bn_mp_lshd.obj \
150 ltm/bn_mp_mod.obj ltm/bn_mp_mod_2d.obj ltm/bn_mp_mod_d.obj ltm/bn_mp_montgomery_calc_normalization.obj \
145 ltm/bn_mp_gcd.obj ltm/bn_mp_get_int.obj ltm/bn_mp_get_long.obj ltm/bn_mp_grow.obj \
146 ltm/bn_mp_import.obj ltm/bn_mp_init.obj ltm/bn_mp_init_copy.obj ltm/bn_mp_init_multi.obj \
147 ltm/bn_mp_init_set.obj ltm/bn_mp_init_set_int.obj ltm/bn_mp_init_size.obj ltm/bn_mp_invmod.obj \
148 ltm/bn_mp_invmod_slow.obj ltm/bn_mp_is_square.obj ltm/bn_mp_jacobi.obj ltm/bn_mp_karatsuba_mul.obj \
149 ltm/bn_mp_karatsuba_sqr.obj ltm/bn_mp_lcm.obj ltm/bn_mp_lshd.obj ltm/bn_mp_mod.obj \
150 ltm/bn_mp_mod_2d.obj ltm/bn_mp_mod_d.obj ltm/bn_mp_montgomery_calc_normalization.obj \
151151 ltm/bn_mp_montgomery_reduce.obj ltm/bn_mp_montgomery_setup.obj ltm/bn_mp_mul.obj \
152152 ltm/bn_mp_mulmod.obj ltm/bn_mp_mul_2.obj ltm/bn_mp_mul_2d.obj ltm/bn_mp_mul_d.obj \
153153 ltm/bn_mp_neg.obj ltm/bn_mp_n_root.obj ltm/bn_mp_n_root_ex.obj ltm/bn_mp_or.obj ltm/bn_mp_prime_fermat.obj \
158158 ltm/bn_mp_reduce_2k.obj ltm/bn_mp_reduce_2k_l.obj ltm/bn_mp_reduce_2k_setup.obj ltm/bn_mp_reduce_2k_setup_l.obj \
159159 ltm/bn_mp_reduce_is_2k.obj ltm/bn_mp_reduce_is_2k_l.obj ltm/bn_mp_reduce_setup.obj \
160160 ltm/bn_mp_rshd.obj ltm/bn_mp_set.obj ltm/bn_mp_set_int.obj ltm/bn_mp_set_long.obj \
161 ltm/bn_mp_set_long_long.obj ltm/bn_mp_shrink.obj ltm/bn_mp_signed_bin_size.obj ltm/bn_mp_sqr.obj \
162 ltm/bn_mp_sqrmod.obj ltm/bn_mp_sqrt.obj ltm/bn_mp_sqrtmod_prime.obj ltm/bn_mp_sub.obj \
163 ltm/bn_mp_submod.obj ltm/bn_mp_sub_d.obj ltm/bn_mp_toom_mul.obj ltm/bn_mp_toom_sqr.obj \
164 ltm/bn_mp_toradix.obj ltm/bn_mp_toradix_n.obj ltm/bn_mp_to_signed_bin.obj ltm/bn_mp_to_signed_bin_n.obj \
161 ltm/bn_mp_shrink.obj ltm/bn_mp_signed_bin_size.obj ltm/bn_mp_sqr.obj ltm/bn_mp_sqrmod.obj \
162 ltm/bn_mp_sqrt.obj ltm/bn_mp_sqrtmod_prime.obj ltm/bn_mp_sub.obj ltm/bn_mp_submod.obj \
163 ltm/bn_mp_sub_d.obj ltm/bn_mp_toom_mul.obj ltm/bn_mp_toom_sqr.obj ltm/bn_mp_toradix.obj \
164 ltm/bn_mp_toradix_n.obj ltm/bn_mp_to_signed_bin.obj ltm/bn_mp_to_signed_bin_n.obj \
165165 ltm/bn_mp_to_unsigned_bin.obj ltm/bn_mp_to_unsigned_bin_n.obj ltm/bn_mp_unsigned_bin_size.obj \
166166 ltm/bn_mp_xor.obj ltm/bn_mp_zero.obj ltm/bn_prime_tab.obj ltm/bn_reverse.obj ltm/bn_s_mp_add.obj \
167167 ltm/bn_s_mp_exptmod.obj ltm/bn_s_mp_mul_digs.obj ltm/bn_s_mp_mul_high_digs.obj ltm/bn_s_mp_sqr.obj \
2727 Implementation of AES
2828 */
2929
30 #include "tomcrypt.h"
30 #include "tomcrypt_private.h"
3131
3232 #ifdef LTC_RIJNDAEL
3333
1212 Authors: Paulo S.L.M. Barreto and Vincent Rijmen.
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_ANUBIS
1818
99 @file blowfish.c
1010 Implementation of the Blowfish block cipher, Tom St Denis
1111 */
12 #include "tomcrypt.h"
12 #include "tomcrypt_private.h"
1313
1414 #ifdef LTC_BLOWFISH
1515
393393 ulong32 L, R;
394394 int r;
395395 #ifndef __GNUC__
396 ulong32 *S1, *S2, *S3, *S4;
396 const ulong32 *S1, *S2, *S3, *S4;
397397 #endif
398398
399399 LTC_ARGCHK(pt != NULL);
455455 ulong32 L, R;
456456 int r;
457457 #ifndef __GNUC__
458 ulong32 *S1, *S2, *S3, *S4;
458 const ulong32 *S1, *S2, *S3, *S4;
459459 #endif
460460
461461 LTC_ARGCHK(pt != NULL);
1111 Implementation by Tom St Denis of Elliptic Semiconductor
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_CAMELLIA
1717
1010 @file cast5.c
1111 Implementation of LTC_CAST5 (RFC 2144) by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_CAST5
1616
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file des.c
3535 {
3636 "3des",
3737 14,
38 24, 24, 8, 16,
38 16, 24, 8, 16,
3939 &des3_setup,
4040 &des3_ecb_encrypt,
4141 &des3_ecb_decrypt,
20672067 int des3_keysize(int *keysize)
20682068 {
20692069 LTC_ARGCHK(keysize != NULL);
2070 if(*keysize < 24) {
2071 return CRYPT_INVALID_KEYSIZE;
2070 if (*keysize < 16)
2071 return CRYPT_INVALID_KEYSIZE;
2072 if (*keysize < 24) {
2073 *keysize = 16;
2074 return CRYPT_OK;
20722075 }
20732076 *keysize = 24;
20742077 return CRYPT_OK;
2121 and Japan (JP 3225440) (expired May 16, 2011).
2222 */
2323
24 #include "tomcrypt.h"
24 #include "tomcrypt_private.h"
2525
2626 #ifdef LTC_IDEA
2727
1212 Derived from the 3GPP standard source code
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_KASUMI
1818
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file khazad.c
1212 Tom St Denis
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_KSEED
1818
1010 @file multi2.c
1111 Multi-2 implementation (not public domain, hence the default disable)
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_MULTI2
1616
99 @file noekeon.c
1010 Implementation of the Noekeon block cipher by Tom St Denis
1111 */
12 #include "tomcrypt.h"
12 #include "tomcrypt_private.h"
1313
1414 #ifdef LTC_NOEKEON
1515
1515 * Thanks to CodeView, SoftIce, and D86 for helping bring this code to *
1616 * the public. *
1717 \**********************************************************************/
18 #include "tomcrypt.h"
18 #include "tomcrypt_private.h"
1919
2020 /**
2121 @file rc2.c
1111 LTC_RC5 code by Tom St Denis
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_RC5
1717
1010 @file rc6.c
1111 LTC_RC6 code by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_RC6
1616
2525 *
2626 *******************************************************************************/
2727
28 #include "tomcrypt.h"
28 #include "tomcrypt_private.h"
2929
3030 #ifdef LTC_SAFER
3131
1010 @file saferp.c
1111 LTC_SAFER+ Implementation by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_SAFERP
1616
1414 https://en.wikipedia.org/wiki/Serpent_(cipher)
1515 */
1616
17 #include "tomcrypt.h"
17 #include "tomcrypt_private.h"
1818
1919 #ifdef LTC_SERPENT
2020
1010 @file skipjack.c
1111 Skipjack Implementation by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_SKIPJACK
1616
1010 @file twofish.c
1111 Implementation of Twofish by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_TWOFISH
1616
472472 const ulong32 *k;
473473 int r;
474474 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
475 ulong32 *S1, *S2, *S3, *S4;
475 const ulong32 *S1, *S2, *S3, *S4;
476476 #endif
477477
478478 LTC_ARGCHK(pt != NULL);
546546 const ulong32 *k;
547547 int r;
548548 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
549 ulong32 *S1, *S2, *S3, *S4;
549 const ulong32 *S1, *S2, *S3, *S4;
550550 #endif
551551
552552 LTC_ARGCHK(pt != NULL);
1010 @file xtea.c
1111 Implementation of LTC_XTEA, Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_XTEA
1616
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_CCM_MODE
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_CCM_MODE
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_CCM_MODE
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_CCM_MODE
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ccm_memory.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_CCM_MODE
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_CCM_MODE
1111
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA20POLY1305_MODE
1212
99 @file eax_addheader.c
1010 EAX implementation, add meta-data, by Tom St Denis
1111 */
12 #include "tomcrypt.h"
12 #include "tomcrypt_private.h"
1313
1414 #ifdef LTC_EAX_MODE
1515
1010 @file eax_decrypt.c
1111 EAX implementation, decrypt block, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_EAX_MODE
1616
1010 @file eax_decrypt_verify_memory.c
1111 EAX implementation, decrypt block of memory, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_EAX_MODE
1616
1010 @file eax_done.c
1111 EAX implementation, terminate session, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_EAX_MODE
1616
1010 @file eax_encrypt.c
1111 EAX implementation, encrypt block by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_EAX_MODE
1616
1010 @file eax_encrypt_authenticate_memory.c
1111 EAX implementation, encrypt a block of memory, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_EAX_MODE
1616
1010 @file eax_init.c
1111 EAX implementation, initialized EAX state, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_EAX_MODE
1616
1010 @file gcm_add_aad.c
1111 GCM implementation, Add AAD data to the stream, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file gcm_add_iv.c
1111 GCM implementation, add IV data to the state, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file gcm_done.c
1111 GCM implementation, Terminate the stream, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file gcm_gf_mult.c
1111 GCM implementation, do the GF mult, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #if defined(LTC_GCM_TABLES) || defined(LTC_LRW_TABLES) || ((defined(LTC_GCM_MODE) || defined(LTC_GCM_MODE)) && defined(LTC_FAST))
1616
1010 @file gcm_init.c
1111 GCM implementation, initialize state, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file gcm_memory.c
1111 GCM implementation, process a packet, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file gcm_mult_h.c
1111 GCM implementation, do the GF mult, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #if defined(LTC_GCM_MODE)
1616 /**
1010 @file gcm_process.c
1111 GCM implementation, process message data, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file gcm_reset.c
1111 GCM implementation, reset a used state so it can accept IV data, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_GCM_MODE
1616
1010 @file ocb3_add_aad.c
1111 OCB implementation, add AAD data, by Karel Miko
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_decrypt.c
1111 OCB implementation, decrypt data, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_decrypt_last.c
1111 OCB implementation, internal helper, by Karel Miko
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_decrypt_verify_memory.c
1111 OCB implementation, helper to decrypt block of memory, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_done.c
1111 OCB implementation, INTERNAL ONLY helper, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_encrypt.c
1111 OCB implementation, encrypt data, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_encrypt_authenticate_memory.c
1111 OCB implementation, encrypt block of memory, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_encrypt_last.c
1111 OCB implementation, internal helper, by Karel Miko
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_init.c
1111 OCB implementation, initialize state, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_int_ntz.c
1111 OCB implementation, INTERNAL ONLY helper, by Tom St Denis
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
1010 @file ocb3_int_xor_blocks.c
1111 OCB implementation, INTERNAL ONLY helper, by Karel Miko
1212 */
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_OCB3_MODE
1616
2222 */
2323 /* see also https://www.ietf.org/rfc/rfc7693.txt */
2424
25 #include "tomcrypt.h"
25 #include "tomcrypt_private.h"
2626
2727 #ifdef LTC_BLAKE2B
2828
198198 return CRYPT_OK;
199199 }
200200
201 /**
202 Initialize the hash/MAC state
203
204 Use this function to init for arbitrary sizes.
205
206 Give a key and keylen to init for MAC mode.
207
208 @param md The hash state you wish to initialize
209 @param outlen The desired output-length
210 @param key The key of the MAC
211 @param keylen The length of the key
212 @return CRYPT_OK if successful
213 */
201214 int blake2b_init(hash_state *md, unsigned long outlen, const unsigned char *key, unsigned long keylen)
202215 {
203216 unsigned char P[BLAKE2B_PARAM_SIZE];
236249 return CRYPT_OK;
237250 }
238251
252 /**
253 Initialize the hash state
254 @param md The hash state you wish to initialize
255 @return CRYPT_OK if successful
256 */
239257 int blake2b_160_init(hash_state *md) { return blake2b_init(md, 20, NULL, 0); }
240258
259 /**
260 Initialize the hash state
261 @param md The hash state you wish to initialize
262 @return CRYPT_OK if successful
263 */
241264 int blake2b_256_init(hash_state *md) { return blake2b_init(md, 32, NULL, 0); }
242265
266 /**
267 Initialize the hash state
268 @param md The hash state you wish to initialize
269 @return CRYPT_OK if successful
270 */
243271 int blake2b_384_init(hash_state *md) { return blake2b_init(md, 48, NULL, 0); }
244272
273 /**
274 Initialize the hash state
275 @param md The hash state you wish to initialize
276 @return CRYPT_OK if successful
277 */
245278 int blake2b_512_init(hash_state *md) { return blake2b_init(md, 64, NULL, 0); }
246279
247280 #define G(r, i, a, b, c, d) \
327360 }
328361 #endif
329362
363 /**
364 Process a block of memory through the hash
365 @param md The hash state
366 @param in The data to hash
367 @param inlen The length of the data (octets)
368 @return CRYPT_OK if successful
369 */
330370 int blake2b_process(hash_state *md, const unsigned char *in, unsigned long inlen)
331371 {
332372 LTC_ARGCHK(md != NULL);
359399 return CRYPT_OK;
360400 }
361401
402 /**
403 Terminate the hash to get the digest
404 @param md The hash state
405 @param out [out] The destination of the hash (size depending on the length used on init)
406 @return CRYPT_OK if successful
407 */
362408 int blake2b_done(hash_state *md, unsigned char *out)
363409 {
364410 unsigned char buffer[BLAKE2B_OUTBYTES] = { 0 };
2222 */
2323 /* see also https://www.ietf.org/rfc/rfc7693.txt */
2424
25 #include "tomcrypt.h"
25 #include "tomcrypt_private.h"
2626
2727 #ifdef LTC_BLAKE2S
2828
192192 return CRYPT_OK;
193193 }
194194
195 /**
196 Initialize the hash/MAC state
197
198 Use this function to init for arbitrary sizes.
199
200 Give a key and keylen to init for MAC mode.
201
202 @param md The hash state you wish to initialize
203 @param outlen The desired output-length
204 @param key The key of the MAC
205 @param keylen The length of the key
206 @return CRYPT_OK if successful
207 */
195208 int blake2s_init(hash_state *md, unsigned long outlen, const unsigned char *key, unsigned long keylen)
196209 {
197210 unsigned char P[BLAKE2S_PARAM_SIZE];
229242 return CRYPT_OK;
230243 }
231244
245 /**
246 Initialize the hash state
247 @param md The hash state you wish to initialize
248 @return CRYPT_OK if successful
249 */
232250 int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16, NULL, 0); }
233251
252 /**
253 Initialize the hash state
254 @param md The hash state you wish to initialize
255 @return CRYPT_OK if successful
256 */
234257 int blake2s_160_init(hash_state *md) { return blake2s_init(md, 20, NULL, 0); }
235258
259 /**
260 Initialize the hash state
261 @param md The hash state you wish to initialize
262 @return CRYPT_OK if successful
263 */
236264 int blake2s_224_init(hash_state *md) { return blake2s_init(md, 28, NULL, 0); }
237265
266 /**
267 Initialize the hash state
268 @param md The hash state you wish to initialize
269 @return CRYPT_OK if successful
270 */
238271 int blake2s_256_init(hash_state *md) { return blake2s_init(md, 32, NULL, 0); }
239272
240273 #define G(r, i, a, b, c, d) \
315348 }
316349 #endif
317350
351 /**
352 Process a block of memory through the hash
353 @param md The hash state
354 @param in The data to hash
355 @param inlen The length of the data (octets)
356 @return CRYPT_OK if successful
357 */
318358 int blake2s_process(hash_state *md, const unsigned char *in, unsigned long inlen)
319359 {
320360 LTC_ARGCHK(md != NULL);
347387 return CRYPT_OK;
348388 }
349389
390 /**
391 Terminate the hash to get the digest
392 @param md The hash state
393 @param out [out] The destination of the hash (size depending on the length used on init)
394 @return CRYPT_OK if successful
395 */
350396 int blake2s_done(hash_state *md, unsigned char *out)
351397 {
352398 unsigned char buffer[BLAKE2S_OUTBYTES] = { 0 };
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file chc.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifndef LTC_NO_FILE
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifndef LTC_NO_FILE
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_HASH_HELPERS
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 #ifdef LTC_HASH_HELPERS
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @param md2.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @param md4.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @param rmd128.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rmd160.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @param rmd256.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rmd320.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file sha1.c
1010 LTC_SHA-224 new NIST standard based off of LTC_SHA-256 truncated to 224 bits (Tom St Denis)
1111 */
1212
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #if defined(LTC_SHA224) && defined(LTC_SHA256)
1616
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file sha256.c
1010 LTC_SHA384 hash included in sha512.c, Tom St Denis
1111 */
1212
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #if defined(LTC_SHA384) && defined(LTC_SHA512)
1616
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @param sha512.c
1010 SHA512/224 hash included in sha512.c
1111 */
1212
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #if defined(LTC_SHA512_224) && defined(LTC_SHA512)
1616
1010 SHA512/256 hash included in sha512.c
1111 */
1212
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #if defined(LTC_SHA512_256) && defined(LTC_SHA512)
1616
88
99 /* based on https://github.com/brainhub/SHA3IUF (public domain) */
1010
11 #include "tomcrypt.h"
11 #include "tomcrypt_private.h"
1212
1313 #ifdef LTC_SHA3
1414
88
99 /* based on https://github.com/brainhub/SHA3IUF (public domain) */
1010
11 #include "tomcrypt.h"
11 #include "tomcrypt_private.h"
1212
1313 #ifdef LTC_SHA3
1414
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file tiger.c
1111 LTC_WHIRLPOOL (using their new sbox) hash function by Tom St Denis
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_WHIRLPOOL
1717
10301030
10311031 #endif /* LTC_SALSA20 */
10321032
1033 #ifdef LTC_XSALSA20
1034
1035 int xsalsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long keylen,
1036 const unsigned char *nonce, unsigned long noncelen,
1037 int rounds);
1038 int xsalsa20_test(void);
1039
1040 #endif /* LTC_XSALSA20 */
1041
10331042 #ifdef LTC_SOSEMANUK
10341043
10351044 typedef struct {
207207 /* stream ciphers */
208208 #define LTC_CHACHA
209209 #define LTC_SALSA20
210 #define LTC_XSALSA20
210211 #define LTC_SOSEMANUK
211212 #define LTC_RABBIT
212213 #define LTC_RC4_STREAM
363364 /* time-based rate limit of the reseeding */
364365 #define LTC_FORTUNA_RESEED_RATELIMIT_TIMED
365366
367 /* with non-glibc or glibc 2.17+ prefer clock_gettime over gettimeofday */
368 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
369 #if __GLIBC_PREREQ(2, 17)
370 #define LTC_CLOCK_GETTIME
371 #endif
372 #elif defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L
373 #define LTC_CLOCK_GETTIME
374 #endif
375
366376 #else
367377
368378 #ifndef LTC_FORTUNA_WD
413423 #define LTC_DH6144
414424 #define LTC_DH8192
415425 #endif
416
417 /* Include Katja (a Rabin variant like RSA) */
418 /* #define LTC_MKAT */
419426
420427 /* Digital Signature Algorithm */
421428 #define LTC_MDSA
537544 #endif
538545 #endif
539546
540 #if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA) || defined(LTC_MKAT)
547 #if defined(LTC_MECC) || defined(LTC_MRSA) || defined(LTC_MDSA)
541548 /* Include the MPI functionality? (required by the PK algorithms) */
542549 #define LTC_MPI
543550
567574 #error ASN.1 DER requires MPI functionality
568575 #endif
569576
570 #if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC) || defined(LTC_MKAT)) && !defined(LTC_DER)
577 #if (defined(LTC_MDSA) || defined(LTC_MRSA) || defined(LTC_MECC)) && !defined(LTC_DER)
571578 #error PK requires ASN.1 DER functionality, make sure LTC_DER is enabled
572579 #endif
573580
577584
578585 #if defined(LTC_CHACHA20_PRNG) && !defined(LTC_CHACHA)
579586 #error LTC_CHACHA20_PRNG requires LTC_CHACHA
587 #endif
588
589 #if defined(LTC_XSALSA20) && !defined(LTC_SALSA20)
590 #error LTC_XSALSA20 requires LTC_SALSA20
580591 #endif
581592
582593 #if defined(LTC_RC4) && !defined(LTC_RC4_STREAM)
506506 int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
507507 #endif
508508
509 /* a simple macro for making hash "process" functions */
510 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
511 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
512 { \
513 unsigned long n; \
514 int err; \
515 LTC_ARGCHK(md != NULL); \
516 LTC_ARGCHK(in != NULL); \
517 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
518 return CRYPT_INVALID_ARG; \
519 } \
520 if ((md-> state_var .length + inlen) < md-> state_var .length) { \
521 return CRYPT_HASH_OVERFLOW; \
522 } \
523 while (inlen > 0) { \
524 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
525 if ((err = compress_name (md, in)) != CRYPT_OK) { \
526 return err; \
527 } \
528 md-> state_var .length += block_size * 8; \
529 in += block_size; \
530 inlen -= block_size; \
531 } else { \
532 n = MIN(inlen, (block_size - md-> state_var .curlen)); \
533 XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
534 md-> state_var .curlen += n; \
535 in += n; \
536 inlen -= n; \
537 if (md-> state_var .curlen == block_size) { \
538 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
539 return err; \
540 } \
541 md-> state_var .length += 8*block_size; \
542 md-> state_var .curlen = 0; \
543 } \
544 } \
545 } \
546 return CRYPT_OK; \
547 }
548
549509 /* ref: $Format:%D$ */
550510 /* git commit: $Format:%H$ */
551511 /* commit time: $Format:%ai$ */
145145 int blake2bmac_test(void);
146146 #endif /* LTC_BLAKE2BMAC */
147147
148
148149 #ifdef LTC_PELICAN
149150
150151 typedef struct pelican_state
390391
391392 int ocb3_test(void);
392393
393 #ifdef LTC_SOURCE
394 /* internal helper functions */
395 int ocb3_int_ntz(unsigned long x);
396 void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
397 #endif /* LTC_SOURCE */
398
399394 #endif /* LTC_OCB3_MODE */
400395
401396 #ifdef LTC_CCM_MODE
523523 extern const ltc_math_descriptor gmp_desc;
524524 #endif
525525
526 #if !defined(DESC_DEF_ONLY) && defined(LTC_SOURCE)
527
528 #define MP_DIGIT_BIT ltc_mp.bits_per_digit
529
530 /* some handy macros */
531 #define mp_init(a) ltc_mp.init(a)
532 #define mp_init_multi ltc_init_multi
533 #define mp_clear(a) ltc_mp.deinit(a)
534 #define mp_clear_multi ltc_deinit_multi
535 #define mp_cleanup_multi ltc_cleanup_multi
536 #define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
537
538 #define mp_neg(a, b) ltc_mp.neg(a, b)
539 #define mp_copy(a, b) ltc_mp.copy(a, b)
540
541 #define mp_set(a, b) ltc_mp.set_int(a, b)
542 #define mp_set_int(a, b) ltc_mp.set_int(a, b)
543 #define mp_get_int(a) ltc_mp.get_int(a)
544 #define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
545 #define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
546 #define mp_cmp(a, b) ltc_mp.compare(a, b)
547 #define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
548 #define mp_count_bits(a) ltc_mp.count_bits(a)
549 #define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
550 #define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
551
552 #define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
553 #define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
554 #define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
555 #define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
556 #define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
557
558 #define mp_add(a, b, c) ltc_mp.add(a, b, c)
559 #define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
560 #define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
561 #define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
562 #define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
563 #define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
564 #define mp_sqr(a, b) ltc_mp.sqr(a, b)
565 #define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
566 #define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
567 #define mp_div_2(a, b) ltc_mp.div_2(a, b)
568 #define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
569 #define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
570 #define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
571 #define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
572
573 #define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
574 #define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
575 #define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
576 #define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
577 #define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
578
579 #define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
580 #define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
581 #define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
582 #define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
583
584 #define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
585 #define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
586
587 #define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
588 #define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
589 #define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
590
591 #define mp_tohex(a, b) mp_toradix(a, b, 16)
592
593 #define mp_rand(a, b) ltc_mp.rand(a, b)
594
595 #endif
596
597526 /* ref: $Format:%D$ */
598527 /* git commit: $Format:%H$ */
599528 /* commit time: $Format:%ai$ */
5353 #ifdef LTC_BASE16
5454 int base16_encode(const unsigned char *in, unsigned long inlen,
5555 char *out, unsigned long *outlen,
56 int caps);
56 unsigned int options);
5757 int base16_decode(const char *in, unsigned long inlen,
5858 unsigned char *out, unsigned long *outlen);
5959 #endif
8484 /* ---- MEM routines ---- */
8585 int mem_neq(const void *a, const void *b, size_t len);
8686 void zeromem(volatile void *dst, size_t len);
87 #ifdef LTC_SOURCE
88 void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
89 #endif
9087 void burn_stack(unsigned long len);
9188
9289 const char *error_to_string(int err);
154151
155152 int padding_pad(unsigned char *data, unsigned long length, unsigned long* padded_length, unsigned long mode);
156153 int padding_depad(const unsigned char *data, unsigned long *length, unsigned long mode);
157
158 #ifdef LTC_SOURCE
159 /* internal helper functions */
160 #define LTC_PAD_MASK (0xF000U)
161 #endif
162154 #endif /* LTC_PADDING */
163155
164156 int compare_testvector(const void* is, const unsigned long is_len, const void* should, const unsigned long should_len, const char* what, int which);
2323 };
2424
2525 int rand_prime(void *N, long len, prng_state *prng, int wprng);
26
27 #ifdef LTC_SOURCE
28 /* internal helper functions */
29 int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
30 int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
31
32 enum public_key_algorithms {
33 PKA_RSA,
34 PKA_DSA,
35 PKA_EC,
36 PKA_EC_PRIMEF
37 };
38
39 typedef struct Oid {
40 unsigned long OID[16];
41 /** Number of OID digits in use */
42 unsigned long OIDlen;
43 } oid_st;
44
45 int pk_get_oid(int pk, oid_st *st);
46 int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
47 int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
48 #endif /* LTC_SOURCE */
4926
5027 /* ---- RSA ---- */
5128 #ifdef LTC_MRSA
148125 rsa_key *key);
149126 #endif
150127
151 /* ---- Katja ---- */
152 #ifdef LTC_MKAT
153
154 /* Min and Max KAT key sizes (in bits) */
155 #define MIN_KAT_SIZE 1024
156 #define MAX_KAT_SIZE 4096
157
158 /** Katja PKCS style key */
159 typedef struct KAT_key {
160 /** Type of key, PK_PRIVATE or PK_PUBLIC */
161 int type;
162 /** The private exponent */
163 void *d;
164 /** The modulus */
165 void *N;
166 /** The p factor of N */
167 void *p;
168 /** The q factor of N */
169 void *q;
170 /** The 1/q mod p CRT param */
171 void *qP;
172 /** The d mod (p - 1) CRT param */
173 void *dP;
174 /** The d mod (q - 1) CRT param */
175 void *dQ;
176 /** The pq param */
177 void *pq;
178 } katja_key;
179
180 int katja_make_key(prng_state *prng, int wprng, int size, katja_key *key);
181
182 int katja_exptmod(const unsigned char *in, unsigned long inlen,
183 unsigned char *out, unsigned long *outlen, int which,
184 katja_key *key);
185
186 void katja_free(katja_key *key);
187
188 /* These use PKCS #1 v2.0 padding */
189 int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
190 unsigned char *out, unsigned long *outlen,
191 const unsigned char *lparam, unsigned long lparamlen,
192 prng_state *prng, int prng_idx, int hash_idx, katja_key *key);
193
194 int katja_decrypt_key(const unsigned char *in, unsigned long inlen,
195 unsigned char *out, unsigned long *outlen,
196 const unsigned char *lparam, unsigned long lparamlen,
197 int hash_idx, int *stat,
198 katja_key *key);
199
200 /* PKCS #1 import/export */
201 int katja_export(unsigned char *out, unsigned long *outlen, int type, katja_key *key);
202 int katja_import(const unsigned char *in, unsigned long inlen, katja_key *key);
203
204 #endif
205
206128 /* ---- DH Routines ---- */
207129 #ifdef LTC_MDH
208130
234156 void dh_free(dh_key *key);
235157
236158 int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key);
237
238 #ifdef LTC_SOURCE
239 typedef struct {
240 int size;
241 const char *name, *base, *prime;
242 } ltc_dh_set_type;
243
244 extern const ltc_dh_set_type ltc_dh_sets[];
245
246 /* internal helper functions */
247 int dh_check_pubkey(const dh_key *key);
248 #endif
249
250159 #endif /* LTC_MDH */
251160
252161
346255 int ecc_generate_key(prng_state *prng, int wprng, ecc_key *key);
347256 int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key *key);
348257 int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_key *key);
258 int ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key);
349259
350260 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key);
351261 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_curve *cu);
391301 int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
392302 const unsigned char *hash, unsigned long hashlen,
393303 int *stat, const ecc_key *key);
394
395
396 #ifdef LTC_SOURCE
397 /* INTERNAL ONLY - it should be later moved to src/headers/tomcrypt_internal.h */
398
399 int ecc_set_dp_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
400 int ecc_copy_dp(const ecc_key *srckey, ecc_key *key);
401 int ecc_set_dp_by_size(int size, ecc_key *key);
402
403 /* low level functions */
404 ecc_point *ltc_ecc_new_point(void);
405 void ltc_ecc_del_point(ecc_point *p);
406 int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p);
407 int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst);
408 int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
409 int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval);
410 int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
411 int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
412 int ltc_ecc_verify_key(const ecc_key *key);
413
414 /* point ops (mp == montgomery digit) */
415 #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
416 /* R = 2P */
417 int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, void *ma, void *modulus, void *mp);
418
419 /* R = P + Q */
420 int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R, void *ma, void *modulus, void *mp);
421 #endif
422
423 #if defined(LTC_MECC_FP)
424 /* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
425 int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
426
427 /* functions for saving/loading/freeing/adding to fixed point cache */
428 int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
429 int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
430 void ltc_ecc_fp_free(void);
431 int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
432
433 /* lock/unlock all points currently in fixed point cache */
434 void ltc_ecc_fp_tablelock(int lock);
435 #endif
436
437 /* R = kG */
438 int ltc_ecc_mulmod(void *k, const ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
439
440 #ifdef LTC_ECC_SHAMIR
441 /* kA*A + kB*B = C */
442 int ltc_ecc_mul2add(const ecc_point *A, void *kA,
443 const ecc_point *B, void *kB,
444 ecc_point *C,
445 void *ma,
446 void *modulus);
447
448 #ifdef LTC_MECC_FP
449 /* Shamir's trick with optimized point multiplication using fixed point cache */
450 int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
451 const ecc_point *B, void *kB,
452 ecc_point *C,
453 void *ma,
454 void *modulus);
455 #endif
456
457 #endif
458
459
460 /* map P to affine from projective */
461 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
462
463 #endif /* LTC_SOURCE */
464304
465305 #endif
466306
538378 int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key);
539379 int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_key *key);
540380 int dsa_verify_key(const dsa_key *key, int *stat);
541 #ifdef LTC_SOURCE
542 /* internal helper functions */
543 int dsa_int_validate_xy(const dsa_key *key, int *stat);
544 int dsa_int_validate_pqg(const dsa_key *key, int *stat);
545 int dsa_int_validate_primes(const dsa_key *key, int *stat);
546 #endif
547381 int dsa_shared_secret(void *private_key, void *base,
548382 const dsa_key *public_key,
549383 unsigned char *out, unsigned long *outlen);
550 #endif
384 #endif /* LTC_MDSA */
551385
552386 #ifdef LTC_DER
553387 /* DER handling */
710544 unsigned long *outlen,
711545 unsigned long *payloadlen);
712546
713 #ifdef LTC_SOURCE
714 /* internal helper functions */
715 int der_decode_custom_type_ex(const unsigned char *in, unsigned long inlen,
716 ltc_asn1_list *root,
717 ltc_asn1_list *list, unsigned long outlen, unsigned int flags);
718
719 int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen);
720 int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id);
721 int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen);
722
723 int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen);
724 int der_decode_asn1_length(const unsigned char* len, unsigned long* lenlen, unsigned long* outlen);
725 int der_length_asn1_length(unsigned long len, unsigned long *outlen);
726
727 int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
728 unsigned long *outlen, unsigned long *payloadlen);
729
730 extern const ltc_asn1_type der_asn1_tag_to_type_map[];
731 extern const unsigned long der_asn1_tag_to_type_map_sz;
732
733 extern const int der_asn1_type_to_identifier_map[];
734 extern const unsigned long der_asn1_type_to_identifier_map_sz;
735 #endif /* LTC_SOURCE */
736
737547 /* SET */
738548 #define der_decode_set(in, inlen, list, outlen) der_decode_sequence_ex(in, inlen, list, outlen, LTC_DER_SEQ_SET)
739549 #define der_length_set der_length_sequence
746556 /* VA list handy helpers with triplets of <type, size, data> */
747557 int der_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...);
748558 int der_decode_sequence_multi(const unsigned char *in, unsigned long inlen, ...);
749 #ifdef LTC_SOURCE
750 /* internal helper functions */
751 int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...);
752 #endif /* LTC_SOURCE */
753559
754560 /* FLEXI DECODER handle unknown list decoder */
755561 int der_decode_sequence_flexi(const unsigned char *in, unsigned long *inlen, ltc_asn1_list **out);
814620 unsigned char *out, unsigned long *outlen);
815621 int der_length_teletex_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
816622
817 #ifdef LTC_SOURCE
818 /* internal helper functions */
819 int der_teletex_char_encode(int c);
820 int der_teletex_value_decode(int v);
821 #endif /* LTC_SOURCE */
822
823
824623 /* PRINTABLE STRING */
825624 int der_encode_printable_string(const unsigned char *in, unsigned long inlen,
826625 unsigned char *out, unsigned long *outlen);
833632
834633 /* UTF-8 */
835634 #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
836 #include <wchar.h>
837 #if defined(__WCHAR_MAX__)
838 #define LTC_WCHAR_MAX __WCHAR_MAX__
839 #elif defined(WCHAR_MAX)
840 #define LTC_WCHAR_MAX WCHAR_MAX
841 #endif
635 #if defined(__WCHAR_MAX__)
636 #define LTC_WCHAR_MAX __WCHAR_MAX__
637 #else
638 #include <wchar.h>
639 #define LTC_WCHAR_MAX WCHAR_MAX
640 #endif
842641 /* please note that it might happen that LTC_WCHAR_MAX is undefined */
843642 #else
844 typedef ulong32 wchar_t;
845 #define LTC_WCHAR_MAX 0xFFFFFFFF
643 typedef ulong32 wchar_t;
644 #define LTC_WCHAR_MAX 0xFFFFFFFF
846645 #endif
847646
848647 int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
851650 int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
852651 wchar_t *out, unsigned long *outlen);
853652 unsigned long der_utf8_charsize(const wchar_t c);
854 #ifdef LTC_SOURCE
855 /* internal helper functions */
856 int der_utf8_valid_char(const wchar_t c);
857 #endif /* LTC_SOURCE */
858653 int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);
859654
860655
905700
906701 int der_length_generalizedtime(const ltc_generalizedtime *gtime, unsigned long *outlen);
907702
908 #ifdef LTC_SOURCE
909 /* internal helper functions */
910 /* SUBJECT PUBLIC KEY INFO */
911 int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
912 unsigned int algorithm, const void* public_key, unsigned long public_key_len,
913 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
914
915 int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
916 unsigned int algorithm, void* public_key, unsigned long* public_key_len,
917 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
918 #endif /* LTC_SOURCE */
919
920703 #endif
921704
922705 /* ref: $Format:%D$ */
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 /*
12 * Internal Macros
13 */
14
15 #define LTC_PAD_MASK (0xF000U)
16
17 /*
18 * Internal Enums
19 */
20
21 enum public_key_algorithms {
22 PKA_RSA,
23 PKA_DSA,
24 PKA_EC,
25 PKA_EC_PRIMEF
26 };
27
28 /*
29 * Internal Types
30 */
31
32 typedef struct Oid {
33 unsigned long OID[16];
34 /** Number of OID digits in use */
35 unsigned long OIDlen;
36 } oid_st;
37
38 typedef struct {
39 int size;
40 const char *name, *base, *prime;
41 } ltc_dh_set_type;
42
43
44 /*
45 * Internal functions
46 */
47
48 /* tomcrypt_hash.h */
49
50 /* a simple macro for making hash "process" functions */
51 #define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
52 int func_name (hash_state * md, const unsigned char *in, unsigned long inlen) \
53 { \
54 unsigned long n; \
55 int err; \
56 LTC_ARGCHK(md != NULL); \
57 LTC_ARGCHK(in != NULL); \
58 if (md-> state_var .curlen > sizeof(md-> state_var .buf)) { \
59 return CRYPT_INVALID_ARG; \
60 } \
61 if ((md-> state_var .length + inlen) < md-> state_var .length) { \
62 return CRYPT_HASH_OVERFLOW; \
63 } \
64 while (inlen > 0) { \
65 if (md-> state_var .curlen == 0 && inlen >= block_size) { \
66 if ((err = compress_name (md, in)) != CRYPT_OK) { \
67 return err; \
68 } \
69 md-> state_var .length += block_size * 8; \
70 in += block_size; \
71 inlen -= block_size; \
72 } else { \
73 n = MIN(inlen, (block_size - md-> state_var .curlen)); \
74 XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n); \
75 md-> state_var .curlen += n; \
76 in += n; \
77 inlen -= n; \
78 if (md-> state_var .curlen == block_size) { \
79 if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) { \
80 return err; \
81 } \
82 md-> state_var .length += 8*block_size; \
83 md-> state_var .curlen = 0; \
84 } \
85 } \
86 } \
87 return CRYPT_OK; \
88 }
89
90
91 /* tomcrypt_mac.h */
92
93 int ocb3_int_ntz(unsigned long x);
94 void ocb3_int_xor_blocks(unsigned char *out, const unsigned char *block_a, const unsigned char *block_b, unsigned long block_len);
95
96
97 /* tomcrypt_math.h */
98
99 #if !defined(DESC_DEF_ONLY)
100
101 #define MP_DIGIT_BIT ltc_mp.bits_per_digit
102
103 /* some handy macros */
104 #define mp_init(a) ltc_mp.init(a)
105 #define mp_init_multi ltc_init_multi
106 #define mp_clear(a) ltc_mp.deinit(a)
107 #define mp_clear_multi ltc_deinit_multi
108 #define mp_cleanup_multi ltc_cleanup_multi
109 #define mp_init_copy(a, b) ltc_mp.init_copy(a, b)
110
111 #define mp_neg(a, b) ltc_mp.neg(a, b)
112 #define mp_copy(a, b) ltc_mp.copy(a, b)
113
114 #define mp_set(a, b) ltc_mp.set_int(a, b)
115 #define mp_set_int(a, b) ltc_mp.set_int(a, b)
116 #define mp_get_int(a) ltc_mp.get_int(a)
117 #define mp_get_digit(a, n) ltc_mp.get_digit(a, n)
118 #define mp_get_digit_count(a) ltc_mp.get_digit_count(a)
119 #define mp_cmp(a, b) ltc_mp.compare(a, b)
120 #define mp_cmp_d(a, b) ltc_mp.compare_d(a, b)
121 #define mp_count_bits(a) ltc_mp.count_bits(a)
122 #define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a)
123 #define mp_2expt(a, b) ltc_mp.twoexpt(a, b)
124
125 #define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c)
126 #define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c)
127 #define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a)
128 #define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b)
129 #define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c)
130
131 #define mp_add(a, b, c) ltc_mp.add(a, b, c)
132 #define mp_add_d(a, b, c) ltc_mp.addi(a, b, c)
133 #define mp_sub(a, b, c) ltc_mp.sub(a, b, c)
134 #define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c)
135 #define mp_mul(a, b, c) ltc_mp.mul(a, b, c)
136 #define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c)
137 #define mp_sqr(a, b) ltc_mp.sqr(a, b)
138 #define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c)
139 #define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d)
140 #define mp_div_2(a, b) ltc_mp.div_2(a, b)
141 #define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c)
142 #define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c)
143 #define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c)
144 #define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c)
145
146 #define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d)
147 #define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d)
148 #define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d)
149 #define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c)
150 #define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c)
151
152 #define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b)
153 #define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b)
154 #define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c)
155 #define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a)
156
157 #define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d)
158 #define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c)
159
160 #define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO)
161 #define mp_isodd(a) (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO)
162 #define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0)
163
164 #define mp_tohex(a, b) mp_toradix(a, b, 16)
165
166 #define mp_rand(a, b) ltc_mp.rand(a, b)
167
168 #endif
169
170
171 /* tomcrypt_misc.h */
172
173 void copy_or_zeromem(const unsigned char* src, unsigned char* dest, unsigned long len, int coz);
174
175
176 /* tomcrypt_pk.h */
177
178 int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng);
179 int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng);
180
181 int pk_get_oid(int pk, oid_st *st);
182 int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen);
183 int pk_oid_num_to_str(const unsigned long *oid, unsigned long oidlen, char *OID, unsigned long *outlen);
184
185 /* ---- DH Routines ---- */
186 #ifdef LTC_MDH
187 extern const ltc_dh_set_type ltc_dh_sets[];
188
189 int dh_check_pubkey(const dh_key *key);
190 #endif /* LTC_MDH */
191
192 /* ---- ECC Routines ---- */
193 #ifdef LTC_MECC
194 int ecc_set_dp_from_mpis(void *a, void *b, void *prime, void *order, void *gx, void *gy, unsigned long cofactor, ecc_key *key);
195 int ecc_copy_dp(const ecc_key *srckey, ecc_key *key);
196 int ecc_set_dp_by_size(int size, ecc_key *key);
197
198 /* low level functions */
199 ecc_point *ltc_ecc_new_point(void);
200 void ltc_ecc_del_point(ecc_point *p);
201 int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p);
202 int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst);
203 int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y);
204 int ltc_ecc_is_point_at_infinity(const ecc_point *P, void *modulus, int *retval);
205 int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *prime, void *a, void *b, void *x, void *y);
206 int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed);
207 int ltc_ecc_verify_key(const ecc_key *key);
208
209 /* point ops (mp == montgomery digit) */
210 #if !defined(LTC_MECC_ACCEL) || defined(LTM_DESC) || defined(GMP_DESC)
211 /* R = 2P */
212 int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, void *ma, void *modulus, void *mp);
213
214 /* R = P + Q */
215 int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_point *R, void *ma, void *modulus, void *mp);
216 #endif
217
218 #if defined(LTC_MECC_FP)
219 /* optimized point multiplication using fixed point cache (HAC algorithm 14.117) */
220 int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
221
222 /* functions for saving/loading/freeing/adding to fixed point cache */
223 int ltc_ecc_fp_save_state(unsigned char **out, unsigned long *outlen);
224 int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen);
225 void ltc_ecc_fp_free(void);
226 int ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock);
227
228 /* lock/unlock all points currently in fixed point cache */
229 void ltc_ecc_fp_tablelock(int lock);
230 #endif
231
232 /* R = kG */
233 int ltc_ecc_mulmod(void *k, const ecc_point *G, ecc_point *R, void *a, void *modulus, int map);
234
235 #ifdef LTC_ECC_SHAMIR
236 /* kA*A + kB*B = C */
237 int ltc_ecc_mul2add(const ecc_point *A, void *kA,
238 const ecc_point *B, void *kB,
239 ecc_point *C,
240 void *ma,
241 void *modulus);
242
243 #ifdef LTC_MECC_FP
244 /* Shamir's trick with optimized point multiplication using fixed point cache */
245 int ltc_ecc_fp_mul2add(const ecc_point *A, void *kA,
246 const ecc_point *B, void *kB,
247 ecc_point *C,
248 void *ma,
249 void *modulus);
250 #endif
251
252 #endif
253
254
255 /* map P to affine from projective */
256 int ltc_ecc_map(ecc_point *P, void *modulus, void *mp);
257 #endif /* LTC_MECC */
258
259 #ifdef LTC_MDSA
260 int dsa_int_validate_xy(const dsa_key *key, int *stat);
261 int dsa_int_validate_pqg(const dsa_key *key, int *stat);
262 int dsa_int_validate_primes(const dsa_key *key, int *stat);
263 #endif /* LTC_MDSA */
264
265 #ifdef LTC_DER
266 /* DER handling */
267 int der_decode_custom_type_ex(const unsigned char *in, unsigned long inlen,
268 ltc_asn1_list *root,
269 ltc_asn1_list *list, unsigned long outlen, unsigned int flags);
270
271 int der_encode_asn1_identifier(const ltc_asn1_list *id, unsigned char *out, unsigned long *outlen);
272 int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, ltc_asn1_list *id);
273 int der_length_asn1_identifier(const ltc_asn1_list *id, unsigned long *idlen);
274
275 int der_encode_asn1_length(unsigned long len, unsigned char* out, unsigned long* outlen);
276 int der_decode_asn1_length(const unsigned char* len, unsigned long* lenlen, unsigned long* outlen);
277 int der_length_asn1_length(unsigned long len, unsigned long *outlen);
278
279 int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
280 unsigned long *outlen, unsigned long *payloadlen);
281
282 extern const ltc_asn1_type der_asn1_tag_to_type_map[];
283 extern const unsigned long der_asn1_tag_to_type_map_sz;
284
285 extern const int der_asn1_type_to_identifier_map[];
286 extern const unsigned long der_asn1_type_to_identifier_map_sz;
287
288 int der_decode_sequence_multi_ex(const unsigned char *in, unsigned long inlen, unsigned int flags, ...);
289
290 int der_teletex_char_encode(int c);
291 int der_teletex_value_decode(int v);
292
293 int der_utf8_valid_char(const wchar_t c);
294
295 /* SUBJECT PUBLIC KEY INFO */
296 int x509_encode_subject_public_key_info(unsigned char *out, unsigned long *outlen,
297 unsigned int algorithm, const void* public_key, unsigned long public_key_len,
298 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long parameters_len);
299
300 int x509_decode_subject_public_key_info(const unsigned char *in, unsigned long inlen,
301 unsigned int algorithm, void* public_key, unsigned long* public_key_len,
302 ltc_asn1_type parameters_type, ltc_asn1_list* parameters, unsigned long *parameters_len);
303
304 #endif /* LTC_DER */
305
306 /* tomcrypt_prng.h */
307
308 #define _LTC_PRNG_EXPORT(which) \
309 int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
310 { \
311 unsigned long len = which ## _desc.export_size; \
312 \
313 LTC_ARGCHK(prng != NULL); \
314 LTC_ARGCHK(out != NULL); \
315 LTC_ARGCHK(outlen != NULL); \
316 \
317 if (*outlen < len) { \
318 *outlen = len; \
319 return CRYPT_BUFFER_OVERFLOW; \
320 } \
321 \
322 if (which ## _read(out, len, prng) != len) { \
323 return CRYPT_ERROR_READPRNG; \
324 } \
325 \
326 *outlen = len; \
327 return CRYPT_OK; \
328 }
329
330
331 /* ref: $Format:%D$ */
332 /* git commit: $Format:%H$ */
333 /* commit time: $Format:%ai$ */
3939 IV[16]; /* IV for CTR mode */
4040
4141 unsigned long pool_idx, /* current pool we will add to */
42 pool0_len, /* length of 0'th pool */
43 wd;
44
42 pool0_len; /* length of 0'th pool */
43 ulong64 wd;
4544 ulong64 reset_cnt; /* number of times we have reseeded */
4645 };
4746 #endif
7271 #ifdef LTC_SOBER128
7372 struct sober128_prng sober128;
7473 #endif
75 };
74 } u;
7675 short ready; /* ready flag 0-1 */
7776 LTC_MUTEX_TYPE(lock) /* lock */
7877 } prng_state;
213212 int prng_is_valid(int idx);
214213 LTC_MUTEX_PROTO(ltc_prng_mutex)
215214
216 #ifdef LTC_SOURCE
217 /* internal helper functions */
218 #define _LTC_PRNG_EXPORT(which) \
219 int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng) \
220 { \
221 unsigned long len = which ## _desc.export_size; \
222 \
223 LTC_ARGCHK(prng != NULL); \
224 LTC_ARGCHK(out != NULL); \
225 LTC_ARGCHK(outlen != NULL); \
226 \
227 if (*outlen < len) { \
228 *outlen = len; \
229 return CRYPT_BUFFER_OVERFLOW; \
230 } \
231 \
232 if (which ## _read(out, len, prng) != len) { \
233 return CRYPT_ERROR_READPRNG; \
234 } \
235 \
236 *outlen = len; \
237 return CRYPT_OK; \
238 }
239 #endif
240
241215 /* Slow RNG you **might** be able to use to seed a PRNG with. Be careful as this
242216 * might not work on all platforms as planned
243217 */
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BLAKE2BMAC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BLAKE2BMAC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BLAKE2BMAC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010 #include <stdarg.h>
1111
1212 #ifdef LTC_BLAKE2BMAC
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BLAKE2SMAC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BLAKE2SMAC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BLAKE2SMAC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010 #include <stdarg.h>
1111
1212 #ifdef LTC_BLAKE2SMAC
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file f9_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file f9_file.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file f9_init.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file f9_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file f9_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file hmac_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file hmac_file.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file hmac_init.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file hmac_memory.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file hmac_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file omac_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file omac_file.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file omac_init.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file omac_memory.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file omac_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pelican.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pelican_memory.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_file.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_init.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_memory.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_ntz.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pmac_shift_xor.c
1111 * https://github.com/floodyberry/poly1305-donna
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_POLY1305
1717
1111 * https://github.com/floodyberry/poly1305-donna
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_POLY1305
1717
1111 * https://github.com/floodyberry/poly1305-donna
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_POLY1305
1717
1111 * https://github.com/floodyberry/poly1305-donna
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515 #include <stdarg.h>
1616
1717 #ifdef LTC_POLY1305
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file xcbc_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file xcbc_file.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file xcbc_init.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file xcbc_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file xcbc_process.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ltc_ecc_fp_mulmod.c
77 */
88
99 #define DESC_DEF_ONLY
10 #include "tomcrypt.h"
10 #include "tomcrypt_private.h"
1111
1212 #ifdef LTM_DESC
1313
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_MPI
1111 #include <stdarg.h>
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file radix_to_bin.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #if defined(LTC_MDSA) || defined(LTC_MECC)
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #if defined(LTC_MRSA) || (!defined(LTC_NO_MATH) && !defined(LTC_NO_PRNGS))
1111
77 */
88
99 #define DESC_DEF_ONLY
10 #include "tomcrypt.h"
10 #include "tomcrypt_private.h"
1111
1212 #ifdef TFM_DESC
1313
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file adler32.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file base16_decode.c
2020 /**
2121 Base16 decode a string
2222 @param in The Base16 string to decode
23 @param inlen The length of the Base16 data
2324 @param out [out] The destination of the binary decoded data
2425 @param outlen [in/out] The max size and resulting size of the decoded data
2526 @return CRYPT_OK if successful
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file base16_encode.c
2121 @param inlen The length of the input buffer
2222 @param out [out] The destination of the Base16 encoded data
2323 @param outlen [in/out] The max size and resulting size of the encoded data
24 @param caps Output 'a-f' on 0 and 'A-F' otherwise.
24 @param options Output 'a-f' on 0 and 'A-F' otherwise.
2525 @return CRYPT_OK if successful
2626 */
2727 int base16_encode(const unsigned char *in, unsigned long inlen,
2828 char *out, unsigned long *outlen,
29 int caps)
29 unsigned int options)
3030 {
3131 unsigned long i, x;
3232 const char *alphabet;
5151 x--;
5252 *outlen = x; /* returning the length without terminating NUL */
5353
54 if (caps == 0) alphabet = alphabets[0];
54 if (options == 0) alphabet = alphabets[0];
5555 else alphabet = alphabets[1];
5656
5757 for (i = 0; i < x; i += 2) {
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BASE32
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_BASE32
1212
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file base64_decode.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file base64_encode.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file burn_stack.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file compare_testvector.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file copy_or_zeromem.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crc32.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt.c
134134 #if defined(LTC_SALSA20)
135135 " Salsa20\n"
136136 #endif
137 #if defined(LTC_XSALSA20)
138 " XSalsa20\n"
139 #endif
137140 #if defined(LTC_SOSEMANUK)
138141 " Sosemanuk\n"
139142 #endif
349352 #if defined(LTC_MDSA)
350353 " DSA\n"
351354 #endif
352 #if defined(LTC_MKAT)
353 " Katja\n"
354 #endif
355355 #if defined(LTC_PK_MAX_RETRIES)
356356 " "NAME_VALUE(LTC_PK_MAX_RETRIES)"\n"
357357 #endif
514514 " LTC_MECC_ACCEL "
515515 #endif
516516 #if defined(LTC_MECC_FP)
517 " LTC_MECC_FP "
517 " LTC_MECC_FP "
518518 #endif
519519 #if defined(LTC_ECC_SHAMIR)
520 " LTC_ECC_SHAMIR "
520 " LTC_ECC_SHAMIR "
521 #endif
522 #if defined(LTC_CLOCK_GETTIME)
523 " LTC_CLOCK_GETTIME "
521524 #endif
522525 "\n"
523526 ;
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_argchk.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_cipher_descriptor.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_cipher_is_valid.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_constants.c
9595 {"LTC_MRSA", 0},
9696 #endif
9797
98 #ifdef LTC_MKAT
99 {"LTC_MKAT", 1},
100 _C_STRINGIFY(MIN_KAT_SIZE),
101 _C_STRINGIFY(MAX_KAT_SIZE),
102 #else
103 {"LTC_MKAT", 0},
104 #endif
105
10698 #ifdef LTC_MECC
10799 {"LTC_MECC", 1},
108100 _C_STRINGIFY(ECC_BUF_SIZE),
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_cipher.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_cipher_any.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_cipher_id.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_hash.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_hash_any.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_hash_id.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_hash_oid.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_find_prng.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_hash_descriptor.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_hash_is_valid.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_inits.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /* Initialize ltc_mp to nulls, to force allocation on all platforms, including macOS. */
1111 ltc_math_descriptor ltc_mp = { 0 };
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_prng_descriptor.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_prng_is_valid.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_PRNG_ENABLE_LTC_RNG
1111 unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file crypt_register_all_ciphers.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file crypt_register_all_hashes.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file crypt_register_all_prngs.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_register_cipher.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_register_hash.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_register_prng.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_sizes.c
247247 _SZ_STRINGIFY_T(ltc_ecc_curve),
248248 _SZ_STRINGIFY_T(ecc_point),
249249 _SZ_STRINGIFY_T(ecc_key),
250 #endif
251 #ifdef LTC_MKAT
252 _SZ_STRINGIFY_T(katja_key),
253250 #endif
254251
255252 /* DER handling */
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_unregister_cipher.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_unregister_hash.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file crypt_unregister_prng.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file error_to_string.c
1010 #include <stdio.h>
1111 #include <stdlib.h>
1212
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_HKDF
1616
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file mem_neq.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_PADDING
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_PADDING
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_DER
1111 static const oid_st rsa_oid = {
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 int pk_oid_str_to_num(const char *OID, unsigned long *oid, unsigned long *oidlen)
1212 {
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_5_1.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_5_2.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file zeromem.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cbc_decrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cbc_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cbc_encrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cbc_getiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cbc_setiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cbc_start.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cfb_decrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cfb_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cfb_encrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cfb_getiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cfb_setiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file cfb_start.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ctr_decrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ctr_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ctr_encrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ctr_getiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ctr_setiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ctr_start.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ecb_decrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ecb_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ecb_encrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ecb_start.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ofb_decrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ofb_done.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ofb_encrypt.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ofb_getiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ofb_setiv.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file ofb_start.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_bit_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_bit_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_bit_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_bit_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_bit_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_boolean.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_boolean.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_boolean.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_choice.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_custom_type.c
189189 }
190190 } else {
191191 /* calc length of length */
192 if ((err = der_length_asn1_length(y, &x)) != CRYPT_OK) {
192 if ((err = der_length_asn1_length(y - id_len, &x)) != CRYPT_OK) {
193193 goto LBL_ERR;
194194 }
195195 if (payloadlen != NULL) {
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_asn1_maps.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_asn1_identifier.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_asn1_length.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_asn1_identifier.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_asn1_length.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_asn1_identifier.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_asn1_length.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_generalizedtime.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_utctime.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_utctime.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_ia5_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_ia5_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_ia5_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_integer.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_integer.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_integer.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_object_identifier.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_object_identifier.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_object_identifier.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_octet_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_octet_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_octet_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_printable_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_printable_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_printable_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_sequence_flexi.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99 #include <stdarg.h>
1010
1111
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_sequence.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_sequence_free.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_sequence_shrink.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_set.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_setof.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_short_integer.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_short_integer.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_short_integer.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_teletex_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_teletex_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_utctime.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_utctime.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_utctime.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_decode_utf8_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_encode_utf8_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file der_length_utf8_string.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file x509_decode_subject_public_key_info.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file x509_encode_subject_public_key_info.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MDH
1212
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_decrypt_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_encrypt_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_export.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_free.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_make_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_generate_pqg.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_import.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_make_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 #ifdef LTC_MDSA
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 #ifdef LTC_MDSA
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_shared_secret.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_sign_hash.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_verify_hash.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file dsa_verify_key.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_ansi_x963_export.c
2323 */
2424 int ecc_ansi_x963_export(const ecc_key *key, unsigned char *out, unsigned long *outlen)
2525 {
26 unsigned char buf[ECC_BUF_SIZE];
27 unsigned long numlen, xlen, ylen;
28
29 LTC_ARGCHK(key != NULL);
30 LTC_ARGCHK(out != NULL);
31 LTC_ARGCHK(outlen != NULL);
32
33 numlen = key->dp.size;
34 xlen = mp_unsigned_bin_size(key->pubkey.x);
35 ylen = mp_unsigned_bin_size(key->pubkey.y);
36
37 if (xlen > numlen || ylen > numlen || sizeof(buf) < numlen) {
38 return CRYPT_BUFFER_OVERFLOW;
39 }
40
41 if (*outlen < (1 + 2*numlen)) {
42 *outlen = 1 + 2*numlen;
43 return CRYPT_BUFFER_OVERFLOW;
44 }
45
46 /* store byte 0x04 */
47 out[0] = 0x04;
48
49 /* pad and store x */
50 zeromem(buf, sizeof(buf));
51 mp_to_unsigned_bin(key->pubkey.x, buf + (numlen - xlen));
52 XMEMCPY(out+1, buf, numlen);
53
54 /* pad and store y */
55 zeromem(buf, sizeof(buf));
56 mp_to_unsigned_bin(key->pubkey.y, buf + (numlen - ylen));
57 XMEMCPY(out+1+numlen, buf, numlen);
58
59 *outlen = 1 + 2*numlen;
60 return CRYPT_OK;
26 return ecc_get_key(out, outlen, PK_PUBLIC, key);
6127 }
6228
6329 #endif
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_ansi_x963_import.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_decrypt_key.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_encrypt_key.c
4141 LTC_ARGCHK(out != NULL);
4242 LTC_ARGCHK(outlen != NULL);
4343 LTC_ARGCHK(key != NULL);
44
45 /* check that wprng/cipher/hash are not invalid */
46 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
47 return err;
48 }
4944
5045 if ((err = hash_is_valid(hash)) != CRYPT_OK) {
5146 return err;
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_export.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_free.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
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_private.h"
10
11 #ifdef LTC_MECC
12
13 /** Extract OID as a string from ECC key
14 @param out [out] destination buffer
15 @param outlen [in/out] Length of destination buffer and final output size (without terminating NUL byte)
16 @param key The ECC key
17 Return CRYPT_OK on success
18 */
19
20 int ecc_get_oid_str(char *out, unsigned long *outlen, const ecc_key *key)
21 {
22 LTC_ARGCHK(key != NULL);
23
24 return pk_oid_num_to_str(key->dp.oid, key->dp.oidlen, out, outlen);
25 }
26
27 #endif
28
29 /* ref: $Format:%D$ */
30 /* git commit: $Format:%H$ */
31 /* commit time: $Format:%ai$ */
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_get_size.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_import.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
148148 goto success;
149149 }
150150
151 /* ### 5. backward compatibility - try to load old-DER format */
152
153 if ((err = ecc_import(in, inlen, key)) != CRYPT_OK) { goto error; }
151 /* ### 5. all attempts failed */
152 goto error;
154153
155154 success:
156155 err = CRYPT_OK;
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
114114 key = XMALLOC(u * c);
115115 I = XMALLOC(Plen + Slen);
116116 if (key == NULL || I == NULL) goto DONE;
117 zeromem(key, u * c);
117118
118119 for (i = 0; i < v; i++) D[i] = purpose; /* D - diversifier */
119120 for (i = 0; i < Slen; i++) I[i] = salt[i % saltlen];
228229 pad = dec_data[enc_size-1];
229230 if (pad < 1 || pad > blklen) goto LBL_ERROR;
230231 *dec_size = enc_size - pad;
231 return CRYPT_OK;
232 err = CRYPT_OK;
233 goto LBL_DONE;
232234 }
233235
234236 LBL_ERROR:
237 err = CRYPT_INVALID_ARG;
238 LBL_DONE:
235239 zeromem(key_iv, sizeof(key_iv));
236240 if (pw) { zeromem(pw, pwlen); XFREE(pw); }
237 return CRYPT_INVALID_ARG;
241 return err;
238242 }
239243
240244 static int _pbes2_pbkdf2_decrypt(const unsigned char *enc_data, unsigned long enc_size,
569573 /* load private key value 'k' */
570574 len = lpri->size;
571575 if ((err = der_decode_sequence_flexi(lpri->data, &len, &p)) == CRYPT_OK) {
572 err = CRYPT_INVALID_PACKET;
573576 if (p->type == LTC_ASN1_SEQUENCE &&
574577 p->child && p->child->type == LTC_ASN1_INTEGER &&
575578 p->child->next && p->child->next->type == LTC_ASN1_OCTET_STRING) {
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_MECC
1111
2727
2828 len = inlen;
2929 if ((err = der_decode_sequence_flexi(in, &len, &decoded_list)) == CRYPT_OK) {
30 err = CRYPT_ERROR;
3031 l = decoded_list;
3132 if (l->type == LTC_ASN1_SEQUENCE &&
3233 l->child && l->child->type == LTC_ASN1_SEQUENCE) {
33 err = CRYPT_ERROR;
3434 l = l->child->child;
3535 while (l) {
3636 if (l->type == LTC_ASN1_SEQUENCE && l->data &&
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_make_key.c
4848 LTC_ARGCHK(key != NULL);
4949 LTC_ARGCHK(key->dp.size > 0);
5050
51 /* good prng? */
52 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
53 return err;
54 }
55
5651 /* ECC key pair generation according to FIPS-186-4 (B.4.2 Key Pair Generation by Testing Candidates):
5752 * the generated private key k should be the range [1, order-1]
5853 * a/ N = bitlen(order)
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
2323 a = key->dp.A;
2424 b = key->dp.B;
2525
26 if (type == PK_PRIVATE && inlen <= (unsigned long)key->dp.size) {
26 if (type == PK_PRIVATE) {
2727 /* load private key */
2828 if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)in, inlen)) != CRYPT_OK) {
2929 goto error;
3030 }
31 if (mp_iszero(key->k)) {
31 if (mp_iszero(key->k) || (mp_cmp(key->k, key->dp.order) != LTC_MP_LT)) {
3232 err = CRYPT_INVALID_PACKET;
3333 goto error;
3434 }
3535 /* compute public key */
3636 if ((err = ltc_mp.ecc_ptmul(key->k, &key->dp.base, &key->pubkey, a, prime, 1)) != CRYPT_OK) { goto error; }
37 key->type = type;
3837 }
3938 else if (type == PK_PUBLIC) {
4039 /* load public key */
4140 if ((err = ltc_ecc_import_point(in, inlen, prime, a, b, key->pubkey.x, key->pubkey.y)) != CRYPT_OK) { goto error; }
4241 if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; }
43 key->type = type;
4442 }
4543 else {
4644 err = CRYPT_INVALID_PACKET;
5250 goto error;
5351 }
5452
53 key->type = type;
5554 return CRYPT_OK;
5655
5756 error:
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_shared_secret.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
3333 /* is this a private key? */
3434 if (key->type != PK_PRIVATE) {
3535 return CRYPT_PK_NOT_PRIVATE;
36 }
37
38 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
39 return err;
4036 }
4137
4238 /* init the bignums */
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ecc_sizes.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_MECC
1212
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ltc_ecc_map.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ltc_ecc_mul2add.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ltc_ecc_mulmod.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ltc_ecc_mulmod_timing.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ltc_ecc_points.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file ltc_ecc_projective_add_point.c
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /* ### Point doubling in Jacobian coordinate system ###
1212 *
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /* origin of this code - OLPC */
1212
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_i2osp.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_mgf1.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_oaep_decode.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_oaep_encode.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_os2ip.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_pss_decode.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file pkcs_1_pss_encode.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /** @file pkcs_1_v1_5_decode.c
1111 *
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /*! \file pkcs_1_v1_5_encode.c
1111 *
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_decrypt_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_encrypt_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_export.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_exptmod.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_free.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_get_size.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_import.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_import_pkcs8.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_import.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_make_key.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010
1111 #ifdef LTC_MRSA
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_sign_hash.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_sign_saltlen_get.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file rsa_verify_hash.c
1010 * http://bxr.su/OpenBSD/lib/libc/crypt/arc4random.c
1111 */
1212
13 #include "tomcrypt.h"
13 #include "tomcrypt_private.h"
1414
1515 #ifdef LTC_CHACHA20_PRNG
1616
3737 {
3838 LTC_ARGCHK(prng != NULL);
3939 prng->ready = 0;
40 XMEMSET(&prng->chacha.ent, 0, sizeof(prng->chacha.ent));
41 prng->chacha.idx = 0;
40 XMEMSET(&prng->u.chacha.ent, 0, sizeof(prng->u.chacha.ent));
41 prng->u.chacha.idx = 0;
4242 LTC_MUTEX_INIT(&prng->lock)
4343 return CRYPT_OK;
4444 }
6363 LTC_MUTEX_LOCK(&prng->lock);
6464 if (prng->ready) {
6565 /* chacha20_prng_ready() was already called, do "rekey" operation */
66 if ((err = chacha_keystream(&prng->chacha.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
66 if ((err = chacha_keystream(&prng->u.chacha.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
6767 for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i];
6868 /* key 32 bytes, 20 rounds */
69 if ((err = chacha_setup(&prng->chacha.s, buf, 32, 20)) != CRYPT_OK) goto LBL_UNLOCK;
69 if ((err = chacha_setup(&prng->u.chacha.s, buf, 32, 20)) != CRYPT_OK) goto LBL_UNLOCK;
7070 /* iv 8 bytes */
71 if ((err = chacha_ivctr64(&prng->chacha.s, buf + 32, 8, 0)) != CRYPT_OK) goto LBL_UNLOCK;
71 if ((err = chacha_ivctr64(&prng->u.chacha.s, buf + 32, 8, 0)) != CRYPT_OK) goto LBL_UNLOCK;
7272 /* clear KEY + IV */
7373 zeromem(buf, sizeof(buf));
7474 }
7575 else {
7676 /* chacha20_prng_ready() was not called yet, add entropy to ent buffer */
77 while (inlen--) prng->chacha.ent[prng->chacha.idx++ % sizeof(prng->chacha.ent)] ^= *in++;
77 while (inlen--) prng->u.chacha.ent[prng->u.chacha.idx++ % sizeof(prng->u.chacha.ent)] ^= *in++;
7878 }
7979 err = CRYPT_OK;
8080 LBL_UNLOCK:
9696 LTC_MUTEX_LOCK(&prng->lock);
9797 if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; }
9898 /* key 32 bytes, 20 rounds */
99 if ((err = chacha_setup(&prng->chacha.s, prng->chacha.ent, 32, 20)) != CRYPT_OK) goto LBL_UNLOCK;
99 if ((err = chacha_setup(&prng->u.chacha.s, prng->u.chacha.ent, 32, 20)) != CRYPT_OK) goto LBL_UNLOCK;
100100 /* iv 8 bytes */
101 if ((err = chacha_ivctr64(&prng->chacha.s, prng->chacha.ent + 32, 8, 0)) != CRYPT_OK) goto LBL_UNLOCK;
102 XMEMSET(&prng->chacha.ent, 0, sizeof(prng->chacha.ent));
103 prng->chacha.idx = 0;
101 if ((err = chacha_ivctr64(&prng->u.chacha.s, prng->u.chacha.ent + 32, 8, 0)) != CRYPT_OK) goto LBL_UNLOCK;
102 XMEMSET(&prng->u.chacha.ent, 0, sizeof(prng->u.chacha.ent));
103 prng->u.chacha.idx = 0;
104104 prng->ready = 1;
105105 LBL_UNLOCK:
106106 LTC_MUTEX_UNLOCK(&prng->lock);
119119 if (outlen == 0 || prng == NULL || out == NULL) return 0;
120120 LTC_MUTEX_LOCK(&prng->lock);
121121 if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; }
122 if (chacha_keystream(&prng->chacha.s, out, outlen) != CRYPT_OK) outlen = 0;
122 if (chacha_keystream(&prng->u.chacha.s, out, outlen) != CRYPT_OK) outlen = 0;
123123 LBL_UNLOCK:
124124 LTC_MUTEX_UNLOCK(&prng->lock);
125125 return outlen;
136136 LTC_ARGCHK(prng != NULL);
137137 LTC_MUTEX_LOCK(&prng->lock);
138138 prng->ready = 0;
139 err = chacha_done(&prng->chacha.s);
139 err = chacha_done(&prng->u.chacha.s);
140140 LTC_MUTEX_UNLOCK(&prng->lock);
141141 LTC_MUTEX_DESTROY(&prng->lock);
142142 return err;
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
9
8 #include "tomcrypt_private.h"
9
10 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
1011 #if defined(_WIN32)
1112 #include <windows.h>
13 #elif defined(LTC_CLOCK_GETTIME)
14 #include <time.h> /* struct timespec + clock_gettime */
1215 #else
13 #include <sys/time.h>
16 #include <sys/time.h> /* struct timeval + gettimeofday */
17 #endif
1418 #endif
1519
1620 /**
5963 int x;
6064 unsigned char *IV;
6165 /* update IV */
62 IV = prng->fortuna.IV;
66 IV = prng->u.fortuna.IV;
6367 for (x = 0; x < 16; x++) {
6468 IV[x] = (IV[x] + 1) & 255;
6569 if (IV[x] != 0) break;
102106 int err, x;
103107
104108 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
105 unsigned long now = _fortuna_current_time();
106 if (now == prng->fortuna.wd)
109 ulong64 now = _fortuna_current_time();
110 if (now == prng->u.fortuna.wd)
107111 return CRYPT_OK;
108112 #else
109 if (++prng->fortuna.wd < LTC_FORTUNA_WD)
113 if (++prng->u.fortuna.wd < LTC_FORTUNA_WD)
110114 return CRYPT_OK;
111115 #endif
112116
113117 /* new K == LTC_SHA256(K || s) where s == LTC_SHA256(P0) || LTC_SHA256(P1) ... */
114118 sha256_init(&md);
115 if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) {
119 if ((err = sha256_process(&md, prng->u.fortuna.K, 32)) != CRYPT_OK) {
116120 sha256_done(&md, tmp);
117121 return err;
118122 }
119123
120 reset_cnt = prng->fortuna.reset_cnt + 1;
124 reset_cnt = prng->u.fortuna.reset_cnt + 1;
121125
122126 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
123127 if (x == 0 || ((reset_cnt >> (x-1)) & 1) == 0) {
124128 /* terminate this hash */
125 if ((err = sha256_done(&prng->fortuna.pool[x], tmp)) != CRYPT_OK) {
129 if ((err = sha256_done(&prng->u.fortuna.pool[x], tmp)) != CRYPT_OK) {
126130 sha256_done(&md, tmp);
127131 return err;
128132 }
132136 return err;
133137 }
134138 /* reset this pool */
135 if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
139 if ((err = sha256_init(&prng->u.fortuna.pool[x])) != CRYPT_OK) {
136140 sha256_done(&md, tmp);
137141 return err;
138142 }
142146 }
143147
144148 /* finish key */
145 if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
146 return err;
147 }
148 if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
149 if ((err = sha256_done(&md, prng->u.fortuna.K)) != CRYPT_OK) {
150 return err;
151 }
152 if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
149153 return err;
150154 }
151155 _fortuna_update_iv(prng);
152156
153157 /* reset/update internals */
154 prng->fortuna.pool0_len = 0;
158 prng->u.fortuna.pool0_len = 0;
155159 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
156 prng->fortuna.wd = now;
160 prng->u.fortuna.wd = now;
157161 #else
158 prng->fortuna.wd = 0;
159 #endif
160 prng->fortuna.reset_cnt = reset_cnt;
162 prng->u.fortuna.wd = 0;
163 #endif
164 prng->u.fortuna.reset_cnt = reset_cnt;
161165
162166
163167 #ifdef LTC_CLEAN_STACK
185189 LTC_MUTEX_LOCK(&prng->lock);
186190 /* new K = LTC_SHA256(K || in) */
187191 sha256_init(&md);
188 if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) {
192 if ((err = sha256_process(&md, prng->u.fortuna.K, 32)) != CRYPT_OK) {
189193 sha256_done(&md, tmp);
190194 goto LBL_UNLOCK;
191195 }
194198 goto LBL_UNLOCK;
195199 }
196200 /* finish key */
197 if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
201 if ((err = sha256_done(&md, prng->u.fortuna.K)) != CRYPT_OK) {
198202 goto LBL_UNLOCK;
199203 }
200204 _fortuna_update_iv(prng);
223227
224228 /* initialize the pools */
225229 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
226 if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
230 if ((err = sha256_init(&prng->u.fortuna.pool[x])) != CRYPT_OK) {
227231 for (y = 0; y < x; y++) {
228 sha256_done(&prng->fortuna.pool[y], tmp);
232 sha256_done(&prng->u.fortuna.pool[y], tmp);
229233 }
230234 return err;
231235 }
232236 }
233 prng->fortuna.pool_idx = prng->fortuna.pool0_len = prng->fortuna.wd = 0;
234 prng->fortuna.reset_cnt = 0;
237 prng->u.fortuna.pool_idx = prng->u.fortuna.pool0_len = prng->u.fortuna.wd = 0;
238 prng->u.fortuna.reset_cnt = 0;
235239
236240 /* reset bufs */
237 zeromem(prng->fortuna.K, 32);
238 if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
241 zeromem(prng->u.fortuna.K, 32);
242 if ((err = rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey)) != CRYPT_OK) {
239243 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
240 sha256_done(&prng->fortuna.pool[x], tmp);
244 sha256_done(&prng->u.fortuna.pool[x], tmp);
241245 }
242246 return err;
243247 }
244 zeromem(prng->fortuna.IV, 16);
248 zeromem(prng->u.fortuna.IV, 16);
245249
246250 LTC_MUTEX_INIT(&prng->lock)
247251
262266 tmp[0] = (unsigned char)source;
263267 tmp[1] = (unsigned char)inlen;
264268
265 if ((err = sha256_process(&prng->fortuna.pool[pool], tmp, 2)) != CRYPT_OK) {
266 return err;
267 }
268 if ((err = sha256_process(&prng->fortuna.pool[pool], in, inlen)) != CRYPT_OK) {
269 if ((err = sha256_process(&prng->u.fortuna.pool[pool], tmp, 2)) != CRYPT_OK) {
270 return err;
271 }
272 if ((err = sha256_process(&prng->u.fortuna.pool[pool], in, inlen)) != CRYPT_OK) {
269273 return err;
270274 }
271275 if (pool == 0) {
272 prng->fortuna.pool0_len += inlen;
276 prng->u.fortuna.pool0_len += inlen;
273277 }
274278 return CRYPT_OK; /* success */
275279 }
319323
320324 LTC_MUTEX_LOCK(&prng->lock);
321325
322 err = _fortuna_add(0, prng->fortuna.pool_idx, in, inlen, prng);
326 err = _fortuna_add(0, prng->u.fortuna.pool_idx, in, inlen, prng);
323327
324328 if (err == CRYPT_OK) {
325 ++(prng->fortuna.pool_idx);
326 prng->fortuna.pool_idx %= LTC_FORTUNA_POOLS;
329 ++(prng->u.fortuna.pool_idx);
330 prng->u.fortuna.pool_idx %= LTC_FORTUNA_POOLS;
327331 }
328332
329333 LTC_MUTEX_UNLOCK(&prng->lock);
345349 /* make sure the reseed doesn't fail because
346350 * of the chosen rate limit */
347351 #ifdef LTC_FORTUNA_RESEED_RATELIMIT_TIMED
348 prng->fortuna.wd = _fortuna_current_time() - 1;
352 prng->u.fortuna.wd = _fortuna_current_time() - 1;
349353 #else
350 prng->fortuna.wd = LTC_FORTUNA_WD;
354 prng->u.fortuna.wd = LTC_FORTUNA_WD;
351355 #endif
352356 err = _fortuna_reseed(prng);
353357 prng->ready = (err == CRYPT_OK) ? 1 : 0;
377381 }
378382
379383 /* do we have to reseed? */
380 if (prng->fortuna.pool0_len >= 64) {
384 if (prng->u.fortuna.pool0_len >= 64) {
381385 if (_fortuna_reseed(prng) != CRYPT_OK) {
382386 goto LBL_UNLOCK;
383387 }
384388 }
385389
386390 /* ensure that one reseed happened before allowing to read */
387 if (prng->fortuna.reset_cnt == 0) {
391 if (prng->u.fortuna.reset_cnt == 0) {
388392 goto LBL_UNLOCK;
389393 }
390394
394398 /* handle whole blocks without the extra XMEMCPY */
395399 while (outlen >= 16) {
396400 /* encrypt the IV and store it */
397 rijndael_ecb_encrypt(prng->fortuna.IV, out, &prng->fortuna.skey);
401 rijndael_ecb_encrypt(prng->u.fortuna.IV, out, &prng->u.fortuna.skey);
398402 out += 16;
399403 outlen -= 16;
400404 _fortuna_update_iv(prng);
402406
403407 /* left over bytes? */
404408 if (outlen > 0) {
405 rijndael_ecb_encrypt(prng->fortuna.IV, tmp, &prng->fortuna.skey);
409 rijndael_ecb_encrypt(prng->u.fortuna.IV, tmp, &prng->u.fortuna.skey);
406410 XMEMCPY(out, tmp, outlen);
407411 _fortuna_update_iv(prng);
408412 }
409413
410414 /* generate new key */
411 rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K , &prng->fortuna.skey);
415 rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K , &prng->u.fortuna.skey);
412416 _fortuna_update_iv(prng);
413417
414 rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K+16, &prng->fortuna.skey);
418 rijndael_ecb_encrypt(prng->u.fortuna.IV, prng->u.fortuna.K+16, &prng->u.fortuna.skey);
415419 _fortuna_update_iv(prng);
416420
417 if (rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey) != CRYPT_OK) {
421 if (rijndael_setup(prng->u.fortuna.K, 32, 0, &prng->u.fortuna.skey) != CRYPT_OK) {
418422 tlen = 0;
419423 }
420424
443447
444448 /* terminate all the hashes */
445449 for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
446 if ((err = sha256_done(&(prng->fortuna.pool[x]), tmp)) != CRYPT_OK) {
450 if ((err = sha256_done(&(prng->u.fortuna.pool[x]), tmp)) != CRYPT_OK) {
447451 goto LBL_UNLOCK;
448452 }
449453 }
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file prngs/rc4.c
3838 LTC_ARGCHK(prng != NULL);
3939 prng->ready = 0;
4040 /* set entropy (key) size to zero */
41 prng->rc4.s.x = 0;
41 prng->u.rc4.s.x = 0;
4242 /* clear entropy (key) buffer */
43 XMEMSET(&prng->rc4.s.buf, 0, sizeof(prng->rc4.s.buf));
43 XMEMSET(&prng->u.rc4.s.buf, 0, sizeof(prng->u.rc4.s.buf));
4444 LTC_MUTEX_INIT(&prng->lock)
4545 return CRYPT_OK;
4646 }
6565 LTC_MUTEX_LOCK(&prng->lock);
6666 if (prng->ready) {
6767 /* rc4_ready() was already called, do "rekey" operation */
68 if ((err = rc4_stream_keystream(&prng->rc4.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
68 if ((err = rc4_stream_keystream(&prng->u.rc4.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
6969 for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i];
7070 /* initialize RC4 */
71 if ((err = rc4_stream_setup(&prng->rc4.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
71 if ((err = rc4_stream_setup(&prng->u.rc4.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
7272 /* drop first 3072 bytes - https://en.wikipedia.org/wiki/RC4#Fluhrer.2C_Mantin_and_Shamir_attack */
73 for (i = 0; i < 12; i++) rc4_stream_keystream(&prng->rc4.s, buf, sizeof(buf));
73 for (i = 0; i < 12; i++) rc4_stream_keystream(&prng->u.rc4.s, buf, sizeof(buf));
7474 zeromem(buf, sizeof(buf));
7575 }
7676 else {
7777 /* rc4_ready() was not called yet, add entropy to the buffer */
78 while (inlen--) prng->rc4.s.buf[prng->rc4.s.x++ % sizeof(prng->rc4.s.buf)] ^= *in++;
78 while (inlen--) prng->u.rc4.s.buf[prng->u.rc4.s.x++ % sizeof(prng->u.rc4.s.buf)] ^= *in++;
7979 }
8080 err = CRYPT_OK;
8181 LBL_UNLOCK:
9898
9999 LTC_MUTEX_LOCK(&prng->lock);
100100 if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; }
101 XMEMCPY(buf, prng->rc4.s.buf, sizeof(buf));
101 XMEMCPY(buf, prng->u.rc4.s.buf, sizeof(buf));
102102 /* initialize RC4 */
103 len = MIN(prng->rc4.s.x, 256); /* TODO: we can perhaps always use all 256 bytes */
104 if ((err = rc4_stream_setup(&prng->rc4.s, buf, len)) != CRYPT_OK) goto LBL_UNLOCK;
103 len = MIN(prng->u.rc4.s.x, 256); /* TODO: we can perhaps always use all 256 bytes */
104 if ((err = rc4_stream_setup(&prng->u.rc4.s, buf, len)) != CRYPT_OK) goto LBL_UNLOCK;
105105 /* drop first 3072 bytes - https://en.wikipedia.org/wiki/RC4#Fluhrer.2C_Mantin_and_Shamir_attack */
106 for (i = 0; i < 12; i++) rc4_stream_keystream(&prng->rc4.s, buf, sizeof(buf));
106 for (i = 0; i < 12; i++) rc4_stream_keystream(&prng->u.rc4.s, buf, sizeof(buf));
107107 prng->ready = 1;
108108 LBL_UNLOCK:
109109 LTC_MUTEX_UNLOCK(&prng->lock);
122122 if (outlen == 0 || prng == NULL || out == NULL) return 0;
123123 LTC_MUTEX_LOCK(&prng->lock);
124124 if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; }
125 if (rc4_stream_keystream(&prng->rc4.s, out, outlen) != CRYPT_OK) outlen = 0;
125 if (rc4_stream_keystream(&prng->u.rc4.s, out, outlen) != CRYPT_OK) outlen = 0;
126126 LBL_UNLOCK:
127127 LTC_MUTEX_UNLOCK(&prng->lock);
128128 return outlen;
139139 LTC_ARGCHK(prng != NULL);
140140 LTC_MUTEX_LOCK(&prng->lock);
141141 prng->ready = 0;
142 err = rc4_stream_done(&prng->rc4.s);
142 err = rc4_stream_done(&prng->u.rc4.s);
143143 LTC_MUTEX_UNLOCK(&prng->lock);
144144 LTC_MUTEX_DESTROY(&prng->lock);
145145 return err;
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_RNG_GET_BYTES
1111 /**
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 #ifdef LTC_RNG_MAKE_PRNG
1111 /**
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 /**
1212 @file prngs/sober128.c
3939 {
4040 LTC_ARGCHK(prng != NULL);
4141 prng->ready = 0;
42 XMEMSET(&prng->sober128.ent, 0, sizeof(prng->sober128.ent));
43 prng->sober128.idx = 0;
42 XMEMSET(&prng->u.sober128.ent, 0, sizeof(prng->u.sober128.ent));
43 prng->u.sober128.idx = 0;
4444 LTC_MUTEX_INIT(&prng->lock)
4545 return CRYPT_OK;
4646 }
6565 LTC_MUTEX_LOCK(&prng->lock);
6666 if (prng->ready) {
6767 /* sober128_ready() was already called, do "rekey" operation */
68 if ((err = sober128_stream_keystream(&prng->sober128.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
68 if ((err = sober128_stream_keystream(&prng->u.sober128.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
6969 for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i];
7070 /* key 32 bytes, 20 rounds */
71 if ((err = sober128_stream_setup(&prng->sober128.s, buf, 32)) != CRYPT_OK) goto LBL_UNLOCK;
71 if ((err = sober128_stream_setup(&prng->u.sober128.s, buf, 32)) != CRYPT_OK) goto LBL_UNLOCK;
7272 /* iv 8 bytes */
73 if ((err = sober128_stream_setiv(&prng->sober128.s, buf + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK;
73 if ((err = sober128_stream_setiv(&prng->u.sober128.s, buf + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK;
7474 /* clear KEY + IV */
7575 zeromem(buf, sizeof(buf));
7676 }
7777 else {
7878 /* sober128_ready() was not called yet, add entropy to ent buffer */
79 while (inlen--) prng->sober128.ent[prng->sober128.idx++ % sizeof(prng->sober128.ent)] ^= *in++;
79 while (inlen--) prng->u.sober128.ent[prng->u.sober128.idx++ % sizeof(prng->u.sober128.ent)] ^= *in++;
8080 }
8181 err = CRYPT_OK;
8282 LBL_UNLOCK:
9898 LTC_MUTEX_LOCK(&prng->lock);
9999 if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; }
100100 /* key 32 bytes, 20 rounds */
101 if ((err = sober128_stream_setup(&prng->sober128.s, prng->sober128.ent, 32)) != CRYPT_OK) goto LBL_UNLOCK;
101 if ((err = sober128_stream_setup(&prng->u.sober128.s, prng->u.sober128.ent, 32)) != CRYPT_OK) goto LBL_UNLOCK;
102102 /* iv 8 bytes */
103 if ((err = sober128_stream_setiv(&prng->sober128.s, prng->sober128.ent + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK;
104 XMEMSET(&prng->sober128.ent, 0, sizeof(prng->sober128.ent));
105 prng->sober128.idx = 0;
103 if ((err = sober128_stream_setiv(&prng->u.sober128.s, prng->u.sober128.ent + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK;
104 XMEMSET(&prng->u.sober128.ent, 0, sizeof(prng->u.sober128.ent));
105 prng->u.sober128.idx = 0;
106106 prng->ready = 1;
107107 LBL_UNLOCK:
108108 LTC_MUTEX_UNLOCK(&prng->lock);
121121 if (outlen == 0 || prng == NULL || out == NULL) return 0;
122122 LTC_MUTEX_LOCK(&prng->lock);
123123 if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; }
124 if (sober128_stream_keystream(&prng->sober128.s, out, outlen) != CRYPT_OK) outlen = 0;
124 if (sober128_stream_keystream(&prng->u.sober128.s, out, outlen) != CRYPT_OK) outlen = 0;
125125 LBL_UNLOCK:
126126 LTC_MUTEX_UNLOCK(&prng->lock);
127127 return outlen;
138138 LTC_ARGCHK(prng != NULL);
139139 LTC_MUTEX_LOCK(&prng->lock);
140140 prng->ready = 0;
141 err = sober128_stream_done(&prng->sober128.s);
141 err = sober128_stream_done(&prng->u.sober128.s);
142142 LTC_MUTEX_UNLOCK(&prng->lock);
143143 LTC_MUTEX_DESTROY(&prng->lock);
144144 return err;
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file sprng.c
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file yarrow.c
4242 /* these are the default hash/cipher combo used */
4343 #ifdef LTC_RIJNDAEL
4444 #if LTC_YARROW_AES==0
45 prng->yarrow.cipher = register_cipher(&rijndael_enc_desc);
45 prng->u.yarrow.cipher = register_cipher(&rijndael_enc_desc);
4646 #elif LTC_YARROW_AES==1
47 prng->yarrow.cipher = register_cipher(&aes_enc_desc);
47 prng->u.yarrow.cipher = register_cipher(&aes_enc_desc);
4848 #elif LTC_YARROW_AES==2
49 prng->yarrow.cipher = register_cipher(&rijndael_desc);
49 prng->u.yarrow.cipher = register_cipher(&rijndael_desc);
5050 #elif LTC_YARROW_AES==3
51 prng->yarrow.cipher = register_cipher(&aes_desc);
51 prng->u.yarrow.cipher = register_cipher(&aes_desc);
5252 #endif
5353 #elif defined(LTC_BLOWFISH)
54 prng->yarrow.cipher = register_cipher(&blowfish_desc);
54 prng->u.yarrow.cipher = register_cipher(&blowfish_desc);
5555 #elif defined(LTC_TWOFISH)
56 prng->yarrow.cipher = register_cipher(&twofish_desc);
56 prng->u.yarrow.cipher = register_cipher(&twofish_desc);
5757 #elif defined(LTC_RC6)
58 prng->yarrow.cipher = register_cipher(&rc6_desc);
58 prng->u.yarrow.cipher = register_cipher(&rc6_desc);
5959 #elif defined(LTC_RC5)
60 prng->yarrow.cipher = register_cipher(&rc5_desc);
60 prng->u.yarrow.cipher = register_cipher(&rc5_desc);
6161 #elif defined(LTC_SAFERP)
62 prng->yarrow.cipher = register_cipher(&saferp_desc);
62 prng->u.yarrow.cipher = register_cipher(&saferp_desc);
6363 #elif defined(LTC_RC2)
64 prng->yarrow.cipher = register_cipher(&rc2_desc);
64 prng->u.yarrow.cipher = register_cipher(&rc2_desc);
6565 #elif defined(LTC_NOEKEON)
66 prng->yarrow.cipher = register_cipher(&noekeon_desc);
66 prng->u.yarrow.cipher = register_cipher(&noekeon_desc);
6767 #elif defined(LTC_ANUBIS)
68 prng->yarrow.cipher = register_cipher(&anubis_desc);
68 prng->u.yarrow.cipher = register_cipher(&anubis_desc);
6969 #elif defined(LTC_KSEED)
70 prng->yarrow.cipher = register_cipher(&kseed_desc);
70 prng->u.yarrow.cipher = register_cipher(&kseed_desc);
7171 #elif defined(LTC_KHAZAD)
72 prng->yarrow.cipher = register_cipher(&khazad_desc);
72 prng->u.yarrow.cipher = register_cipher(&khazad_desc);
7373 #elif defined(LTC_CAST5)
74 prng->yarrow.cipher = register_cipher(&cast5_desc);
74 prng->u.yarrow.cipher = register_cipher(&cast5_desc);
7575 #elif defined(LTC_XTEA)
76 prng->yarrow.cipher = register_cipher(&xtea_desc);
76 prng->u.yarrow.cipher = register_cipher(&xtea_desc);
7777 #elif defined(LTC_SAFER)
78 prng->yarrow.cipher = register_cipher(&safer_sk128_desc);
78 prng->u.yarrow.cipher = register_cipher(&safer_sk128_desc);
7979 #elif defined(LTC_DES)
80 prng->yarrow.cipher = register_cipher(&des3_desc);
80 prng->u.yarrow.cipher = register_cipher(&des3_desc);
8181 #else
8282 #error LTC_YARROW needs at least one CIPHER
8383 #endif
84 if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
84 if ((err = cipher_is_valid(prng->u.yarrow.cipher)) != CRYPT_OK) {
8585 return err;
8686 }
8787
8888 #ifdef LTC_SHA256
89 prng->yarrow.hash = register_hash(&sha256_desc);
89 prng->u.yarrow.hash = register_hash(&sha256_desc);
9090 #elif defined(LTC_SHA512)
91 prng->yarrow.hash = register_hash(&sha512_desc);
91 prng->u.yarrow.hash = register_hash(&sha512_desc);
9292 #elif defined(LTC_TIGER)
93 prng->yarrow.hash = register_hash(&tiger_desc);
93 prng->u.yarrow.hash = register_hash(&tiger_desc);
9494 #elif defined(LTC_SHA1)
95 prng->yarrow.hash = register_hash(&sha1_desc);
95 prng->u.yarrow.hash = register_hash(&sha1_desc);
9696 #elif defined(LTC_RIPEMD320)
97 prng->yarrow.hash = register_hash(&rmd320_desc);
97 prng->u.yarrow.hash = register_hash(&rmd320_desc);
9898 #elif defined(LTC_RIPEMD256)
99 prng->yarrow.hash = register_hash(&rmd256_desc);
99 prng->u.yarrow.hash = register_hash(&rmd256_desc);
100100 #elif defined(LTC_RIPEMD160)
101 prng->yarrow.hash = register_hash(&rmd160_desc);
101 prng->u.yarrow.hash = register_hash(&rmd160_desc);
102102 #elif defined(LTC_RIPEMD128)
103 prng->yarrow.hash = register_hash(&rmd128_desc);
103 prng->u.yarrow.hash = register_hash(&rmd128_desc);
104104 #elif defined(LTC_MD5)
105 prng->yarrow.hash = register_hash(&md5_desc);
105 prng->u.yarrow.hash = register_hash(&md5_desc);
106106 #elif defined(LTC_MD4)
107 prng->yarrow.hash = register_hash(&md4_desc);
107 prng->u.yarrow.hash = register_hash(&md4_desc);
108108 #elif defined(LTC_MD2)
109 prng->yarrow.hash = register_hash(&md2_desc);
109 prng->u.yarrow.hash = register_hash(&md2_desc);
110110 #elif defined(LTC_WHIRLPOOL)
111 prng->yarrow.hash = register_hash(&whirlpool_desc);
111 prng->u.yarrow.hash = register_hash(&whirlpool_desc);
112112 #else
113113 #error LTC_YARROW needs at least one HASH
114114 #endif
115 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
115 if ((err = hash_is_valid(prng->u.yarrow.hash)) != CRYPT_OK) {
116116 return err;
117117 }
118118
119119 /* zero the memory used */
120 zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool));
120 zeromem(prng->u.yarrow.pool, sizeof(prng->u.yarrow.pool));
121121 LTC_MUTEX_INIT(&prng->lock)
122122
123123 return CRYPT_OK;
141141
142142 LTC_MUTEX_LOCK(&prng->lock);
143143
144 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
144 if ((err = hash_is_valid(prng->u.yarrow.hash)) != CRYPT_OK) {
145145 goto LBL_UNLOCK;
146146 }
147147
148148 /* start the hash */
149 if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) {
149 if ((err = hash_descriptor[prng->u.yarrow.hash].init(&md)) != CRYPT_OK) {
150150 goto LBL_UNLOCK;
151151 }
152152
153153 /* hash the current pool */
154 if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool,
155 hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) {
154 if ((err = hash_descriptor[prng->u.yarrow.hash].process(&md, prng->u.yarrow.pool,
155 hash_descriptor[prng->u.yarrow.hash].hashsize)) != CRYPT_OK) {
156156 goto LBL_UNLOCK;
157157 }
158158
159159 /* add the new entropy */
160 if ((err = hash_descriptor[prng->yarrow.hash].process(&md, in, inlen)) != CRYPT_OK) {
160 if ((err = hash_descriptor[prng->u.yarrow.hash].process(&md, in, inlen)) != CRYPT_OK) {
161161 goto LBL_UNLOCK;
162162 }
163163
164164 /* store result */
165 err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool);
165 err = hash_descriptor[prng->u.yarrow.hash].done(&md, prng->u.yarrow.pool);
166166
167167 LBL_UNLOCK:
168168 LTC_MUTEX_UNLOCK(&prng->lock);
182182
183183 LTC_MUTEX_LOCK(&prng->lock);
184184
185 if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
186 goto LBL_UNLOCK;
187 }
188
189 if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
185 if ((err = hash_is_valid(prng->u.yarrow.hash)) != CRYPT_OK) {
186 goto LBL_UNLOCK;
187 }
188
189 if ((err = cipher_is_valid(prng->u.yarrow.cipher)) != CRYPT_OK) {
190190 goto LBL_UNLOCK;
191191 }
192192
193193 /* setup CTR mode using the "pool" as the key */
194 ks = (int)hash_descriptor[prng->yarrow.hash].hashsize;
195 if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
196 goto LBL_UNLOCK;
197 }
198
199 if ((err = ctr_start(prng->yarrow.cipher, /* what cipher to use */
200 prng->yarrow.pool, /* IV */
201 prng->yarrow.pool, ks, /* KEY and key size */
194 ks = (int)hash_descriptor[prng->u.yarrow.hash].hashsize;
195 if ((err = cipher_descriptor[prng->u.yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
196 goto LBL_UNLOCK;
197 }
198
199 if ((err = ctr_start(prng->u.yarrow.cipher, /* what cipher to use */
200 prng->u.yarrow.pool, /* IV */
201 prng->u.yarrow.pool, ks, /* KEY and key size */
202202 0, /* number of rounds */
203203 CTR_COUNTER_LITTLE_ENDIAN, /* little endian counter */
204 &prng->yarrow.ctr)) != CRYPT_OK) {
204 &prng->u.yarrow.ctr)) != CRYPT_OK) {
205205 goto LBL_UNLOCK;
206206 }
207207 prng->ready = 1;
233233 zeromem(out, outlen);
234234
235235 /* now randomize it */
236 if (ctr_encrypt(out, out, outlen, &prng->yarrow.ctr) != CRYPT_OK) {
236 if (ctr_encrypt(out, out, outlen, &prng->u.yarrow.ctr) != CRYPT_OK) {
237237 outlen = 0;
238238 }
239239
258258 /* call cipher done when we invent one ;-) */
259259
260260 /* we invented one */
261 err = ctr_done(&prng->yarrow.ctr);
261 err = ctr_done(&prng->u.yarrow.ctr);
262262
263263 LTC_MUTEX_UNLOCK(&prng->lock);
264264 LTC_MUTEX_DESTROY(&prng->lock);
311311 }
312312
313313 /* now let's test the hash/cipher that was chosen */
314 if (cipher_descriptor[prng.yarrow.cipher].test &&
315 ((err = cipher_descriptor[prng.yarrow.cipher].test()) != CRYPT_OK)) {
316 return err;
317 }
318 if (hash_descriptor[prng.yarrow.hash].test &&
319 ((err = hash_descriptor[prng.yarrow.hash].test()) != CRYPT_OK)) {
314 if (cipher_descriptor[prng.u.yarrow.cipher].test &&
315 ((err = cipher_descriptor[prng.u.yarrow.cipher].test()) != CRYPT_OK)) {
316 return err;
317 }
318 if (hash_descriptor[prng.u.yarrow.hash].test &&
319 ((err = hash_descriptor[prng.u.yarrow.hash].test()) != CRYPT_OK)) {
320320 return err;
321321 }
322322
1111 * Public domain from D. J. Bernstein
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_CHACHA
1717
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_CHACHA
1212
1111 * Public domain from D. J. Bernstein
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_CHACHA
1717
1111 * Public domain from D. J. Bernstein
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_CHACHA
1717
1111 * Public domain from D. J. Bernstein
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_CHACHA
1717
1111 * Public domain from D. J. Bernstein
1212 */
1313
14 #include "tomcrypt.h"
14 #include "tomcrypt_private.h"
1515
1616 #ifdef LTC_CHACHA
1717
6161 ******************************************************************************/
6262
6363
64 #include "tomcrypt.h"
64 #include "tomcrypt_private.h"
6565
6666 #ifdef LTC_RABBIT
6767
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_RC4_STREAM
1212
1212 * Public domain from D. J. Bernstein
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_SALSA20
1818
6161 LTC_ARGCHK(st != NULL);
6262 LTC_ARGCHK(in != NULL);
6363 LTC_ARGCHK(out != NULL);
64 LTC_ARGCHK(st->ivlen == 8);
64 LTC_ARGCHK(st->ivlen == 8 || st->ivlen == 24);
6565
6666 if (st->ksleft > 0) {
6767 j = MIN(st->ksleft, inlen);
66 * guarantee it works.
77 */
88
9 #include "tomcrypt.h"
9 #include "tomcrypt_private.h"
1010
1111 #ifdef LTC_SALSA20
1212
1212 * Public domain from D. J. Bernstein
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_SALSA20
1818
1212 * Public domain from D. J. Bernstein
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_SALSA20
1818
1212 * Public domain from D. J. Bernstein
1313 */
1414
15 #include "tomcrypt.h"
15 #include "tomcrypt_private.h"
1616
1717 #ifdef LTC_SALSA20
1818
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 /* The implementation is based on:
10 * "Extending the Salsa20 nonce", https://cr.yp.to/snuffle/xsalsa-20081128.pdf
11 * "Salsa20 specification", http://cr.yp.to/snuffle/spec.pdf
12 * and salsa20-ref.c version 20051118
13 * Public domain from D. J. Bernstein
14 */
15
16 #include "tomcrypt.h"
17
18 #ifdef LTC_XSALSA20
19
20 static const char * const constants = "expand 32-byte k";
21
22 #define QUARTERROUND(a,b,c,d) \
23 x[b] ^= (ROL((x[a] + x[d]), 7)); \
24 x[c] ^= (ROL((x[b] + x[a]), 9)); \
25 x[d] ^= (ROL((x[c] + x[b]), 13)); \
26 x[a] ^= (ROL((x[d] + x[c]), 18));
27
28 /* use modified salsa20 doubleround (no final addition as in salsa20) */
29 static void _xsalsa20_doubleround(ulong32 *x, int rounds)
30 {
31 int i;
32
33 for (i = rounds; i > 0; i -= 2) {
34 /* columnround */
35 QUARTERROUND( 0, 4, 8,12)
36 QUARTERROUND( 5, 9,13, 1)
37 QUARTERROUND(10,14, 2, 6)
38 QUARTERROUND(15, 3, 7,11)
39 /* rowround */
40 QUARTERROUND( 0, 1, 2, 3)
41 QUARTERROUND( 5, 6, 7, 4)
42 QUARTERROUND(10,11, 8, 9)
43 QUARTERROUND(15,12,13,14)
44 }
45 }
46
47 #undef QUARTERROUND
48
49 /**
50 Initialize an XSalsa20 context
51 @param st [out] The destination of the XSalsa20 state
52 @param key The secret key
53 @param keylen The length of the secret key, must be 32 (octets)
54 @param nonce The nonce
55 @param noncelen The length of the nonce, must be 24 (octets)
56 @param rounds Number of rounds (must be evenly divisible by 2, default is 20)
57 @return CRYPT_OK if successful
58 */
59 int xsalsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long keylen,
60 const unsigned char *nonce, unsigned long noncelen,
61 int rounds)
62 {
63 const int sti[] = {0, 5, 10, 15, 6, 7, 8, 9}; /* indices used to build subkey fm x */
64 ulong32 x[64]; /* input to & output fm doubleround */
65 unsigned char subkey[32];
66 int i;
67
68 LTC_ARGCHK(st != NULL);
69 LTC_ARGCHK(key != NULL);
70 LTC_ARGCHK(keylen == 32);
71 LTC_ARGCHK(nonce != NULL);
72 LTC_ARGCHK(noncelen == 24);
73 if (rounds == 0) rounds = 20;
74 LTC_ARGCHK(rounds % 2 == 0); /* number of rounds must be evenly divisible by 2 */
75
76 /* load the state to "hash" the key */
77 LOAD32L(x[ 0], constants + 0);
78 LOAD32L(x[ 5], constants + 4);
79 LOAD32L(x[10], constants + 8);
80 LOAD32L(x[15], constants + 12);
81 LOAD32L(x[ 1], key + 0);
82 LOAD32L(x[ 2], key + 4);
83 LOAD32L(x[ 3], key + 8);
84 LOAD32L(x[ 4], key + 12);
85 LOAD32L(x[11], key + 16);
86 LOAD32L(x[12], key + 20);
87 LOAD32L(x[13], key + 24);
88 LOAD32L(x[14], key + 28);
89 LOAD32L(x[ 6], nonce + 0);
90 LOAD32L(x[ 7], nonce + 4);
91 LOAD32L(x[ 8], nonce + 8);
92 LOAD32L(x[ 9], nonce + 12);
93
94 /* use modified salsa20 doubleround (no final addition) */
95 _xsalsa20_doubleround(x, rounds);
96
97 /* extract the subkey */
98 for (i = 0; i < 8; ++i) {
99 STORE32L(x[sti[i]], subkey + 4 * i);
100 }
101
102 /* load the final initial state */
103 LOAD32L(st->input[ 0], constants + 0);
104 LOAD32L(st->input[ 5], constants + 4);
105 LOAD32L(st->input[10], constants + 8);
106 LOAD32L(st->input[15], constants + 12);
107 LOAD32L(st->input[ 1], subkey + 0);
108 LOAD32L(st->input[ 2], subkey + 4);
109 LOAD32L(st->input[ 3], subkey + 8);
110 LOAD32L(st->input[ 4], subkey + 12);
111 LOAD32L(st->input[11], subkey + 16);
112 LOAD32L(st->input[12], subkey + 20);
113 LOAD32L(st->input[13], subkey + 24);
114 LOAD32L(st->input[14], subkey + 28);
115 LOAD32L(st->input[ 6], &(nonce[16]) + 0);
116 LOAD32L(st->input[ 7], &(nonce[16]) + 4);
117 st->input[ 8] = 0;
118 st->input[ 9] = 0;
119 st->rounds = rounds;
120 st->ksleft = 0;
121 st->ivlen = 24; /* set switch to say nonce/IV has been loaded */
122
123 #ifdef LTC_CLEAN_STACK
124 zeromem(x, sizeof(x));
125 zeromem(subkey, sizeof(subkey));
126 #endif
127
128 return CRYPT_OK;
129 }
130
131
132 #endif
133
134 /* ref: $Format:%D$ */
135 /* git commit: $Format:%H$ */
136 /* commit time: $Format:%ai$ */
55 * The library is free for all purposes without any express
66 * guarantee it works.
77 */
8 #include "tomcrypt.h"
8 #include "tomcrypt_private.h"
99
1010 /**
1111 @file sober128_stream.c
2929 * <thomas.pornin@cryptolog.com>
3030 */
3131
32 #include "tomcrypt.h"
32 #include "tomcrypt_private.h"
3333
3434 #ifdef LTC_SOSEMANUK
3535
256256
257257 LTC_ARGCHK(ss != NULL);
258258 LTC_ARGCHK(key != NULL);
259 LTC_ARGCHK(keylen > 0 && keylen <= 32);
259260
260261 /*
261262 * The key is copied into the wbuf[] buffer and padded to 256 bits
262263 * as described in the Serpent specification.
263264 */
264 if (keylen == 0 || keylen > 32) {
265 fprintf(stderr, "invalid key size: %lu\n",
266 (unsigned long)keylen);
267 exit(EXIT_FAILURE);
268 }
269265 XMEMCPY(wbuf, key, keylen);
270266 if (keylen < 32) {
271267 wbuf[keylen] = 0x01;
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_ERROR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 static const struct {
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_FAST_MP_INVMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes the modular inverse via binary extended euclidean algorithm,
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes xR**-1 == x (mod N) via Montgomery Reduction
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_FAST_S_MP_MUL_DIGS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Fast (comba) multiplier
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* this is a modified version of fast_s_mul_digs that only produces
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_FAST_S_MP_SQR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* the jist of squaring...
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_2EXPT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes a = 2**b
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_ABS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* b = |a|
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_ADD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* high level addition (handles signs) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_ADD_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* single digit addition */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_ADDMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* d = a + b (mod c) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_AND_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* AND two ints together */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CLAMP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* trim unused digits
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CLEAR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* clear one (frees) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CLEAR_MULTI_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
14
1615 #include <stdarg.h>
1716
1817 void mp_clear_multi(mp_int *mp, ...)
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CMP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* compare two ints (signed)*/
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CMP_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* compare a digit */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CMP_MAG_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* compare maginitude of two ints (unsigned) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_CNT_LSB_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 static const int lnz[16] = {
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_COPY_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* copy, b = a */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_COUNT_BITS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* returns the number of bits in an int */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DIV_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 #ifdef BN_MP_DIV_SMALL
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DIV_2_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* b = a/2 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DIV_2D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DIV_3_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* divide by three (based on routine from MPI and the GMP manual) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DIV_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 static int s_is_power_of_two(mp_digit b, int *p)
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DR_IS_MODULUS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines if a number is a valid DR modulus */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DR_REDUCE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* reduce "x" in place modulo "n" using the Diminished Radix algorithm.
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_DR_SETUP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines the setup value */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXCH_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* swap the elements of two integers, for cases where you can't simply swap the
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXPORT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* based on gmp's mpz_export.
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXPT_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* wrapper function for mp_expt_d_ex() */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXPT_D_EX_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* calculate c = a**b using a square-multiply algorithm */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXPTMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXPTMOD_FAST_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_EXTEUCLID_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Extended euclidean algorithm of (a, b) produces
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_FREAD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 #ifndef LTM_NO_FILE
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_FWRITE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 #ifndef LTM_NO_FILE
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_GCD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Greatest Common Divisor using the binary method */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_GET_INT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* get the lower 32-bits of an mp_int */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_GET_LONG_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* get the lower unsigned long of an mp_int, platform dependent */
+0
-41
src/ltm/bn_mp_get_long_long.c less more
0 #include <tommath_private.h>
1 #ifdef BN_MP_GET_LONG_LONG_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
15 */
16
17 /* get the lower unsigned long long of an mp_int, platform dependent */
18 uint64_t mp_get_long_long(const mp_int *a)
19 {
20 int i;
21 uint64_t res;
22
23 if (a->used == 0) {
24 return 0;
25 }
26
27 /* get number of digits of the lsb we have to read */
28 i = MIN(a->used, ((((int)sizeof(uint64_t) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
29
30 /* get most significant digit of result */
31 res = DIGIT(a, i);
32
33 #if DIGIT_BIT < 64
34 while (--i >= 0) {
35 res = (res << DIGIT_BIT) | DIGIT(a, i);
36 }
37 #endif
38 return res;
39 }
40 #endif
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_GROW_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* grow as required */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_IMPORT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* based on gmp's mpz_import.
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INIT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* init a new mp_int */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INIT_COPY_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* creates "a" then copies b into it */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INIT_MULTI_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
14
1615 #include <stdarg.h>
1716
1817 int mp_init_multi(mp_int *mp, ...)
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INIT_SET_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* initialize and set a digit */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INIT_SET_INT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* initialize and set a digit */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INIT_SIZE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* init an mp_init for a given size */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INVMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* hac 14.61, pp608 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_INVMOD_SLOW_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* hac 14.61, pp608 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_IS_SQUARE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Check if remainders are possible squares - fast exclude non-squares */
7472 return res;
7573 }
7674 if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
77 goto ERR;
75 goto LBL_ERR;
7876 }
7977 r = mp_get_int(&t);
8078 /* Check for other prime modules, note it's not an ERROR but we must
81 * free "t" so the easiest way is to goto ERR. We know that res
79 * free "t" so the easiest way is to goto LBL_ERR. We know that res
8280 * is already equal to MP_OKAY from the mp_mod call
8381 */
84 if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto ERR;
85 if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto ERR;
86 if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto ERR;
87 if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto ERR;
88 if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto ERR;
89 if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto ERR;
90 if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto ERR;
82 if (((1uL<<(r%11uL)) & 0x5C4uL) != 0uL) goto LBL_ERR;
83 if (((1uL<<(r%13uL)) & 0x9E4uL) != 0uL) goto LBL_ERR;
84 if (((1uL<<(r%17uL)) & 0x5CE8uL) != 0uL) goto LBL_ERR;
85 if (((1uL<<(r%19uL)) & 0x4F50CuL) != 0uL) goto LBL_ERR;
86 if (((1uL<<(r%23uL)) & 0x7ACCA0uL) != 0uL) goto LBL_ERR;
87 if (((1uL<<(r%29uL)) & 0xC2EDD0CuL) != 0uL) goto LBL_ERR;
88 if (((1uL<<(r%31uL)) & 0x6DE2B848uL) != 0uL) goto LBL_ERR;
9189
9290 /* Final check - is sqr(sqrt(arg)) == arg ? */
9391 if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
94 goto ERR;
92 goto LBL_ERR;
9593 }
9694 if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
97 goto ERR;
95 goto LBL_ERR;
9896 }
9997
10098 *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
101 ERR:
99 LBL_ERR:
102100 mp_clear(&t);
103101 return res;
104102 }
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_JACOBI_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes the jacobi c = (a | n) (or Legendre if n is prime)
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_KARATSUBA_MUL_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* c = |a| * |b| using Karatsuba Multiplication using
5957
6058 /* init copy all the temps */
6159 if (mp_init_size(&x0, B) != MP_OKAY)
62 goto ERR;
60 goto LBL_ERR;
6361 if (mp_init_size(&x1, a->used - B) != MP_OKAY)
6462 goto X0;
6563 if (mp_init_size(&y0, B) != MP_OKAY)
163161 mp_clear(&x1);
164162 X0:
165163 mp_clear(&x0);
166 ERR:
164 LBL_ERR:
167165 return err;
168166 }
169167 #endif
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_KARATSUBA_SQR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Karatsuba squaring, computes b = a*a using three
3634
3735 /* init copy all the temps */
3836 if (mp_init_size(&x0, B) != MP_OKAY)
39 goto ERR;
37 goto LBL_ERR;
4038 if (mp_init_size(&x1, a->used - B) != MP_OKAY)
4139 goto X0;
4240
116114 mp_clear(&x1);
117115 X0:
118116 mp_clear(&x0);
119 ERR:
117 LBL_ERR:
120118 return err;
121119 }
122120 #endif
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_LCM_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes least common multiple as |a*b|/(a, b) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_LSHD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* shift left a certain amount of digits */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MOD_2D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* calc a value mod 2**b */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MOD_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 int mp_mod_d(const mp_int *a, mp_digit b, mp_digit *c)
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /*
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MONTGOMERY_REDUCE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes xR**-1 == x (mod N) via Montgomery Reduction */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MONTGOMERY_SETUP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* setups the montgomery reduction stuff */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MUL_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* high level multiplication (handles sign) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MUL_2_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* b = a*2 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MUL_2D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* shift left by a certain bit count */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MUL_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* multiply by a digit */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_MULMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* d = a * b (mod c) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_N_ROOT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* wrapper function for mp_n_root_ex()
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_N_ROOT_EX_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* find the n'th root of an integer
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_NEG_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* b = -a */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_OR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* OR two ints together */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_FERMAT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* performs one Fermat test.
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_IS_DIVISIBLE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines if an integers is divisible by one
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_IS_PRIME_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* performs a variable number of rounds of Miller-Rabin
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_MILLER_RABIN_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Miller-Rabin test of "a" to the base of "b" as described in
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_NEXT_PRIME_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* finds the next prime after the number "a" using "t" trials
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_RABIN_MILLER_TRIALS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_PRIME_RANDOM_EX_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* makes a truly random prime of a given size (bits),
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_RADIX_SIZE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* returns size of ASCII reprensentation */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_RADIX_SMAP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* chars used in radix conversions */
1816 const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
19 const uint8_t mp_s_rmap_reverse[] = {
20 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */
21 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
22 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */
23 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */
24 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */
25 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */
26 0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */
27 0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */
28 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */
29 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */
30 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */
17 const unsigned char mp_s_rmap_reverse[] = {
18 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, /* ()*+,-./ */
19 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* 01234567 */
20 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 89:;<=>? */
21 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, /* @ABCDEFG */
22 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, /* HIJKLMNO */
23 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, /* PQRSTUVW */
24 0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0xff, /* XYZ[\]^_ */
25 0xff, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, /* `abcdefg */
26 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, /* hijklmno */
27 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, /* pqrstuvw */
28 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, /* xyz{|}~. */
3129 };
3230 const size_t mp_s_rmap_reverse_sz = sizeof(mp_s_rmap_reverse);
3331 #endif
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_RAND_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
17 #if defined(MP_8BIT) || defined(MP_16BIT)
18 #define MP_GEN_RANDOM_SHIFT DIGIT_BIT
19 #else
20 #if MP_GEN_RANDOM_MAX == 0xffffffffu
21 #define MP_GEN_RANDOM_SHIFT 32
22 #elif MP_GEN_RANDOM_MAX == 32767
23 /* SHRT_MAX */
24 #define MP_GEN_RANDOM_SHIFT 15
25 #elif MP_GEN_RANDOM_MAX == 2147483647
26 /* INT_MAX */
27 #define MP_GEN_RANDOM_SHIFT 31
28 #elif !defined(MP_GEN_RANDOM_SHIFT)
29 #error Thou shalt define their own valid MP_GEN_RANDOM_SHIFT
30 #endif
31 #endif
32
33 /* makes a pseudo-random int of a given size */
34 static mp_digit s_gen_random(void)
15 /* First the OS-specific special cases
16 * - *BSD
17 * - Windows
18 */
19 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
20 #define MP_ARC4RANDOM
21 #define MP_GEN_RANDOM_MAX 0xffffffffu
22 #define MP_GEN_RANDOM_SHIFT 32
23
24 static int s_read_arc4random(mp_digit* p)
3525 {
3626 mp_digit d = 0, msk = 0;
3727 do {
3828 d <<= MP_GEN_RANDOM_SHIFT;
39 d |= ((mp_digit) MP_GEN_RANDOM());
29 d |= ((mp_digit) arc4random());
4030 msk <<= MP_GEN_RANDOM_SHIFT;
4131 msk |= (MP_MASK & MP_GEN_RANDOM_MAX);
4232 } while ((MP_MASK & msk) != MP_MASK);
43 d &= MP_MASK;
44 return d;
33 *p = d;
34 return MP_OKAY;
35 }
36 #endif
37
38 #if defined(_WIN32) || defined(_WIN32_WCE)
39 #define MP_WIN_CSP
40
41 #ifndef _WIN32_WINNT
42 #define _WIN32_WINNT 0x0400
43 #endif
44 #ifdef _WIN32_WCE
45 #define UNDER_CE
46 #define ARM
47 #endif
48
49 #define WIN32_LEAN_AND_MEAN
50 #include <windows.h>
51 #include <wincrypt.h>
52
53 static HCRYPTPROV hProv = 0;
54
55 static void s_cleanup_win_csp(void)
56 {
57 CryptReleaseContext(hProv, 0);
58 hProv = 0;
59 }
60
61 static int s_read_win_csp(mp_digit* p)
62 {
63 int ret = -1;
64 if (hProv == 0) {
65 if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
66 (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
67 !CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
68 CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
69 hProv = 0;
70 return ret;
71 }
72 atexit(s_cleanup_win_csp);
73 }
74 if (CryptGenRandom(hProv, sizeof(*p), (void*)p) == TRUE) {
75 ret = MP_OKAY;
76 }
77 return ret;
78 }
79 #endif /* WIN32 */
80
81 #if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
82 #if __GLIBC_PREREQ(2, 25)
83 #define MP_GETRANDOM
84 #include <sys/random.h>
85 #include <errno.h>
86
87 static int s_read_getrandom(mp_digit* p)
88 {
89 int ret;
90 do {
91 ret = getrandom(p, sizeof(*p), 0);
92 } while((ret == -1) && (errno == EINTR));
93 if (ret == sizeof(*p)) return MP_OKAY;
94 return -1;
95 }
96 #endif
97 #endif
98
99 /* We assume all platforms besides windows provide "/dev/urandom".
100 * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
101 */
102 #if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
103 #ifndef MP_DEV_URANDOM
104 #define MP_DEV_URANDOM "/dev/urandom"
105 #endif
106 #include <fcntl.h>
107 #include <errno.h>
108 #include <unistd.h>
109
110 static int s_read_dev_urandom(mp_digit* p)
111 {
112 ssize_t r;
113 int fd;
114 do {
115 fd = open(MP_DEV_URANDOM, O_RDONLY);
116 } while((fd == -1) && (errno == EINTR));
117 if (fd == -1) return -1;
118 do {
119 r = read(fd, p, sizeof(*p));
120 } while((r == -1) && (errno == EINTR));
121 close(fd);
122 if (r != sizeof(*p)) return -1;
123 return MP_OKAY;
124 }
125 #endif
126
127 #if defined(MP_PRNG_ENABLE_LTM_RNG)
128 unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
129 void (*ltm_rng_callback)(void);
130
131 static int s_read_ltm_rng(mp_digit* p)
132 {
133 unsigned long ret;
134 if (ltm_rng == NULL) return -1;
135 ret = ltm_rng((void*)p, sizeof(*p), ltm_rng_callback);
136 if (ret != sizeof(*p)) return -1;
137 return MP_OKAY;
138 }
139 #endif
140
141 static int s_rand_digit(mp_digit* p)
142 {
143 int ret = -1;
144
145 #if defined(MP_ARC4RANDOM)
146 ret = s_read_arc4random(p);
147 if (ret == MP_OKAY) return ret;
148 #endif
149
150 #if defined(MP_WIN_CSP)
151 ret = s_read_win_csp(p);
152 if (ret == MP_OKAY) return ret;
153 #else
154
155 #if defined(MP_GETRANDOM)
156 ret = s_read_getrandom(p);
157 if (ret == MP_OKAY) return ret;
158 #endif
159 #if defined(MP_DEV_URANDOM)
160 ret = s_read_dev_urandom(p);
161 if (ret == MP_OKAY) return ret;
162 #endif
163
164 #endif /* MP_WIN_CSP */
165
166 #if defined(MP_PRNG_ENABLE_LTM_RNG)
167 ret = s_read_ltm_rng(p);
168 if (ret == MP_OKAY) return ret;
169 #endif
170
171 return ret;
172 }
173
174 /* makes a pseudo-random int of a given size */
175 static int s_gen_random(mp_digit *r)
176 {
177 int ret = s_rand_digit(r);
178 *r &= MP_MASK;
179 return ret;
45180 }
46181
47182 int mp_rand(mp_int *a, int digits)
56191
57192 /* first place a random non-zero digit */
58193 do {
59 d = s_gen_random();
194 if (s_gen_random(&d) != MP_OKAY) {
195 return MP_VAL;
196 }
60197 } while (d == 0u);
61198
62199 if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
68205 return res;
69206 }
70207
71 if ((res = mp_add_d(a, s_gen_random(), a)) != MP_OKAY) {
208 if (s_gen_random(&d) != MP_OKAY) {
209 return MP_VAL;
210 }
211 if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
72212 return res;
73213 }
74214 }
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_READ_RADIX_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* read a string [ASCII] in a given radix */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_READ_SIGNED_BIN_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* read signed bin, big endian, first byte is 0==positive or 1==negative */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_READ_UNSIGNED_BIN_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* reads a unsigned char array, assumes the msb is stored first [big endian] */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* reduces x mod m, assumes 0 < x < m**2, mu is
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_2K_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* reduces a modulo n where n is of the form 2**p - d */
2826 top:
2927 /* q = a/2**p, a = a mod 2**p */
3028 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
31 goto ERR;
29 goto LBL_ERR;
3230 }
3331
3432 if (d != 1u) {
3533 /* q = q * d */
3634 if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
37 goto ERR;
35 goto LBL_ERR;
3836 }
3937 }
4038
4139 /* a = a + q */
4240 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
43 goto ERR;
41 goto LBL_ERR;
4442 }
4543
4644 if (mp_cmp_mag(a, n) != MP_LT) {
4745 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
48 goto ERR;
46 goto LBL_ERR;
4947 }
5048 goto top;
5149 }
5250
53 ERR:
51 LBL_ERR:
5452 mp_clear(&q);
5553 return res;
5654 }
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_2K_L_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* reduces a modulo n where n is of the form 2**p - d
3129 top:
3230 /* q = a/2**p, a = a mod 2**p */
3331 if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
34 goto ERR;
32 goto LBL_ERR;
3533 }
3634
3735 /* q = q * d */
3836 if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
39 goto ERR;
37 goto LBL_ERR;
4038 }
4139
4240 /* a = a + q */
4341 if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
44 goto ERR;
42 goto LBL_ERR;
4543 }
4644
4745 if (mp_cmp_mag(a, n) != MP_LT) {
4846 if ((res = s_mp_sub(a, n, a)) != MP_OKAY) {
49 goto ERR;
47 goto LBL_ERR;
5048 }
5149 goto top;
5250 }
5351
54 ERR:
52 LBL_ERR:
5553 mp_clear(&q);
5654 return res;
5755 }
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_2K_SETUP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines the setup value */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_2K_SETUP_L_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines the setup value */
2523 }
2624
2725 if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
28 goto ERR;
26 goto LBL_ERR;
2927 }
3028
3129 if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
32 goto ERR;
30 goto LBL_ERR;
3331 }
3432
35 ERR:
33 LBL_ERR:
3634 mp_clear(&tmp);
3735 return res;
3836 }
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_IS_2K_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines if mp_reduce_2k can be used */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_IS_2K_L_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* determines if reduce_2k_l can be used */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_REDUCE_SETUP_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* pre-calculate the value required for Barrett reduction
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_RSHD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* shift right a certain amount of digits */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SET_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* set to a digit */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SET_INT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* set a 32-bit const */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SET_LONG_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* set a platform dependent unsigned long int */
+0
-24
src/ltm/bn_mp_set_long_long.c less more
0 #include <tommath_private.h>
1 #ifdef BN_MP_SET_LONG_LONG_C
2 /* LibTomMath, multiple-precision integer library -- Tom St Denis
3 *
4 * LibTomMath is a library that provides multiple-precision
5 * integer arithmetic as well as number theoretic functionality.
6 *
7 * The library was designed directly after the MPI library by
8 * Michael Fromberger but has been written from scratch with
9 * additional optimizations in place.
10 *
11 * The library is free for all purposes without any express
12 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
15 */
16
17 /* set a platform dependent unsigned long long int */
18 MP_SET_XLONG(mp_set_long_long, uint64_t)
19 #endif
20
21 /* ref: $Format:%D$ */
22 /* git commit: $Format:%H$ */
23 /* commit time: $Format:%ai$ */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SHRINK_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* shrink a bignum */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SIGNED_BIN_SIZE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* get the size for an signed equivalent */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SQR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* computes b = a*a */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SQRMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* c = a * a (mod b) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SQRT_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* this function is less generic than mp_n_root, simpler and faster */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SQRTMOD_PRIME_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SUB_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* high level subtraction (handles signs) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SUB_D_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* single digit subtraction */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_SUBMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* d = a - b (mod c) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TO_SIGNED_BIN_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* store in signed [big endian] format */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TO_SIGNED_BIN_N_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* store in signed [big endian] format */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TO_UNSIGNED_BIN_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* store in unsigned [big endian] format */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TO_UNSIGNED_BIN_N_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* store in unsigned [big endian] format */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TOOM_MUL_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* multiplication using the Toom-Cook 3-way algorithm
3836
3937 /* a = a2 * B**2 + a1 * B + a0 */
4038 if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
41 goto ERR;
39 goto LBL_ERR;
4240 }
4341
4442 if ((res = mp_copy(a, &a1)) != MP_OKAY) {
45 goto ERR;
43 goto LBL_ERR;
4644 }
4745 mp_rshd(&a1, B);
4846 if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) {
49 goto ERR;
47 goto LBL_ERR;
5048 }
5149
5250 if ((res = mp_copy(a, &a2)) != MP_OKAY) {
53 goto ERR;
51 goto LBL_ERR;
5452 }
5553 mp_rshd(&a2, B*2);
5654
5755 /* b = b2 * B**2 + b1 * B + b0 */
5856 if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
59 goto ERR;
57 goto LBL_ERR;
6058 }
6159
6260 if ((res = mp_copy(b, &b1)) != MP_OKAY) {
63 goto ERR;
61 goto LBL_ERR;
6462 }
6563 mp_rshd(&b1, B);
6664 (void)mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
6765
6866 if ((res = mp_copy(b, &b2)) != MP_OKAY) {
69 goto ERR;
67 goto LBL_ERR;
7068 }
7169 mp_rshd(&b2, B*2);
7270
7371 /* w0 = a0*b0 */
7472 if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
75 goto ERR;
73 goto LBL_ERR;
7674 }
7775
7876 /* w4 = a2 * b2 */
7977 if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
80 goto ERR;
78 goto LBL_ERR;
8179 }
8280
8381 /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
8482 if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
85 goto ERR;
83 goto LBL_ERR;
8684 }
8785 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
88 goto ERR;
86 goto LBL_ERR;
8987 }
9088 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
91 goto ERR;
89 goto LBL_ERR;
9290 }
9391 if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
94 goto ERR;
92 goto LBL_ERR;
9593 }
9694
9795 if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
98 goto ERR;
96 goto LBL_ERR;
9997 }
10098 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
101 goto ERR;
99 goto LBL_ERR;
102100 }
103101 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
104 goto ERR;
102 goto LBL_ERR;
105103 }
106104 if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
107 goto ERR;
105 goto LBL_ERR;
108106 }
109107
110108 if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
111 goto ERR;
109 goto LBL_ERR;
112110 }
113111
114112 /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
115113 if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
116 goto ERR;
114 goto LBL_ERR;
117115 }
118116 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
119 goto ERR;
117 goto LBL_ERR;
120118 }
121119 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
122 goto ERR;
120 goto LBL_ERR;
123121 }
124122 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
125 goto ERR;
123 goto LBL_ERR;
126124 }
127125
128126 if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
129 goto ERR;
127 goto LBL_ERR;
130128 }
131129 if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
132 goto ERR;
130 goto LBL_ERR;
133131 }
134132 if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
135 goto ERR;
133 goto LBL_ERR;
136134 }
137135 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
138 goto ERR;
136 goto LBL_ERR;
139137 }
140138
141139 if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
142 goto ERR;
140 goto LBL_ERR;
143141 }
144142
145143
146144 /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
147145 if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
148 goto ERR;
146 goto LBL_ERR;
149147 }
150148 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
151 goto ERR;
149 goto LBL_ERR;
152150 }
153151 if ((res = mp_add(&b2, &b1, &tmp2)) != MP_OKAY) {
154 goto ERR;
152 goto LBL_ERR;
155153 }
156154 if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
157 goto ERR;
155 goto LBL_ERR;
158156 }
159157 if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
160 goto ERR;
158 goto LBL_ERR;
161159 }
162160
163161 /* now solve the matrix
174172
175173 /* r1 - r4 */
176174 if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
177 goto ERR;
175 goto LBL_ERR;
178176 }
179177 /* r3 - r0 */
180178 if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
181 goto ERR;
179 goto LBL_ERR;
182180 }
183181 /* r1/2 */
184182 if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
185 goto ERR;
183 goto LBL_ERR;
186184 }
187185 /* r3/2 */
188186 if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
189 goto ERR;
187 goto LBL_ERR;
190188 }
191189 /* r2 - r0 - r4 */
192190 if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
193 goto ERR;
191 goto LBL_ERR;
194192 }
195193 if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
196 goto ERR;
194 goto LBL_ERR;
197195 }
198196 /* r1 - r2 */
199197 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
200 goto ERR;
198 goto LBL_ERR;
201199 }
202200 /* r3 - r2 */
203201 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
204 goto ERR;
202 goto LBL_ERR;
205203 }
206204 /* r1 - 8r0 */
207205 if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
208 goto ERR;
206 goto LBL_ERR;
209207 }
210208 if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
211 goto ERR;
209 goto LBL_ERR;
212210 }
213211 /* r3 - 8r4 */
214212 if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
215 goto ERR;
213 goto LBL_ERR;
216214 }
217215 if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
218 goto ERR;
216 goto LBL_ERR;
219217 }
220218 /* 3r2 - r1 - r3 */
221219 if ((res = mp_mul_d(&w2, 3uL, &w2)) != MP_OKAY) {
222 goto ERR;
220 goto LBL_ERR;
223221 }
224222 if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
225 goto ERR;
223 goto LBL_ERR;
226224 }
227225 if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
228 goto ERR;
226 goto LBL_ERR;
229227 }
230228 /* r1 - r2 */
231229 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
232 goto ERR;
230 goto LBL_ERR;
233231 }
234232 /* r3 - r2 */
235233 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
236 goto ERR;
234 goto LBL_ERR;
237235 }
238236 /* r1/3 */
239237 if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
240 goto ERR;
238 goto LBL_ERR;
241239 }
242240 /* r3/3 */
243241 if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
244 goto ERR;
242 goto LBL_ERR;
245243 }
246244
247245 /* at this point shift W[n] by B*n */
248246 if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
249 goto ERR;
247 goto LBL_ERR;
250248 }
251249 if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
252 goto ERR;
250 goto LBL_ERR;
253251 }
254252 if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
255 goto ERR;
253 goto LBL_ERR;
256254 }
257255 if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
258 goto ERR;
256 goto LBL_ERR;
259257 }
260258
261259 if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
262 goto ERR;
260 goto LBL_ERR;
263261 }
264262 if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
265 goto ERR;
263 goto LBL_ERR;
266264 }
267265 if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
268 goto ERR;
266 goto LBL_ERR;
269267 }
270268 if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
271 goto ERR;
272 }
273
274 ERR:
269 goto LBL_ERR;
270 }
271
272 LBL_ERR:
275273 mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
276274 &a0, &a1, &a2, &b0, &b1,
277275 &b2, &tmp1, &tmp2, NULL);
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TOOM_SQR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* squaring using Toom-Cook 3-way algorithm */
3028
3129 /* a = a2 * B**2 + a1 * B + a0 */
3230 if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
33 goto ERR;
31 goto LBL_ERR;
3432 }
3533
3634 if ((res = mp_copy(a, &a1)) != MP_OKAY) {
37 goto ERR;
35 goto LBL_ERR;
3836 }
3937 mp_rshd(&a1, B);
4038 if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) {
41 goto ERR;
39 goto LBL_ERR;
4240 }
4341
4442 if ((res = mp_copy(a, &a2)) != MP_OKAY) {
45 goto ERR;
43 goto LBL_ERR;
4644 }
4745 mp_rshd(&a2, B*2);
4846
4947 /* w0 = a0*a0 */
5048 if ((res = mp_sqr(&a0, &w0)) != MP_OKAY) {
51 goto ERR;
49 goto LBL_ERR;
5250 }
5351
5452 /* w4 = a2 * a2 */
5553 if ((res = mp_sqr(&a2, &w4)) != MP_OKAY) {
56 goto ERR;
54 goto LBL_ERR;
5755 }
5856
5957 /* w1 = (a2 + 2(a1 + 2a0))**2 */
6058 if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
61 goto ERR;
59 goto LBL_ERR;
6260 }
6361 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
64 goto ERR;
62 goto LBL_ERR;
6563 }
6664 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
67 goto ERR;
65 goto LBL_ERR;
6866 }
6967 if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
70 goto ERR;
68 goto LBL_ERR;
7169 }
7270
7371 if ((res = mp_sqr(&tmp1, &w1)) != MP_OKAY) {
74 goto ERR;
72 goto LBL_ERR;
7573 }
7674
7775 /* w3 = (a0 + 2(a1 + 2a2))**2 */
7876 if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
79 goto ERR;
77 goto LBL_ERR;
8078 }
8179 if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
82 goto ERR;
80 goto LBL_ERR;
8381 }
8482 if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
85 goto ERR;
83 goto LBL_ERR;
8684 }
8785 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
88 goto ERR;
86 goto LBL_ERR;
8987 }
9088
9189 if ((res = mp_sqr(&tmp1, &w3)) != MP_OKAY) {
92 goto ERR;
90 goto LBL_ERR;
9391 }
9492
9593
9694 /* w2 = (a2 + a1 + a0)**2 */
9795 if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
98 goto ERR;
96 goto LBL_ERR;
9997 }
10098 if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
101 goto ERR;
99 goto LBL_ERR;
102100 }
103101 if ((res = mp_sqr(&tmp1, &w2)) != MP_OKAY) {
104 goto ERR;
102 goto LBL_ERR;
105103 }
106104
107105 /* now solve the matrix
117115
118116 /* r1 - r4 */
119117 if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
120 goto ERR;
118 goto LBL_ERR;
121119 }
122120 /* r3 - r0 */
123121 if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
124 goto ERR;
122 goto LBL_ERR;
125123 }
126124 /* r1/2 */
127125 if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
128 goto ERR;
126 goto LBL_ERR;
129127 }
130128 /* r3/2 */
131129 if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
132 goto ERR;
130 goto LBL_ERR;
133131 }
134132 /* r2 - r0 - r4 */
135133 if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
136 goto ERR;
134 goto LBL_ERR;
137135 }
138136 if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
139 goto ERR;
137 goto LBL_ERR;
140138 }
141139 /* r1 - r2 */
142140 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
143 goto ERR;
141 goto LBL_ERR;
144142 }
145143 /* r3 - r2 */
146144 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
147 goto ERR;
145 goto LBL_ERR;
148146 }
149147 /* r1 - 8r0 */
150148 if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
151 goto ERR;
149 goto LBL_ERR;
152150 }
153151 if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
154 goto ERR;
152 goto LBL_ERR;
155153 }
156154 /* r3 - 8r4 */
157155 if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
158 goto ERR;
156 goto LBL_ERR;
159157 }
160158 if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
161 goto ERR;
159 goto LBL_ERR;
162160 }
163161 /* 3r2 - r1 - r3 */
164162 if ((res = mp_mul_d(&w2, 3uL, &w2)) != MP_OKAY) {
165 goto ERR;
163 goto LBL_ERR;
166164 }
167165 if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
168 goto ERR;
166 goto LBL_ERR;
169167 }
170168 if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
171 goto ERR;
169 goto LBL_ERR;
172170 }
173171 /* r1 - r2 */
174172 if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
175 goto ERR;
173 goto LBL_ERR;
176174 }
177175 /* r3 - r2 */
178176 if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
179 goto ERR;
177 goto LBL_ERR;
180178 }
181179 /* r1/3 */
182180 if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
183 goto ERR;
181 goto LBL_ERR;
184182 }
185183 /* r3/3 */
186184 if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
187 goto ERR;
185 goto LBL_ERR;
188186 }
189187
190188 /* at this point shift W[n] by B*n */
191189 if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
192 goto ERR;
190 goto LBL_ERR;
193191 }
194192 if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
195 goto ERR;
193 goto LBL_ERR;
196194 }
197195 if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
198 goto ERR;
196 goto LBL_ERR;
199197 }
200198 if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
201 goto ERR;
199 goto LBL_ERR;
202200 }
203201
204202 if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
205 goto ERR;
203 goto LBL_ERR;
206204 }
207205 if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
208 goto ERR;
206 goto LBL_ERR;
209207 }
210208 if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
211 goto ERR;
209 goto LBL_ERR;
212210 }
213211 if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
214 goto ERR;
215 }
216
217 ERR:
212 goto LBL_ERR;
213 }
214
215 LBL_ERR:
218216 mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
219217 return res;
220218 }
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TORADIX_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* stores a bignum as a ASCII string in a given radix (2..64) */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_TORADIX_N_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* stores a bignum as a ASCII string in a given radix (2..64)
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_UNSIGNED_BIN_SIZE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* get the size for an unsigned equivalent */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_XOR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* XOR two ints together */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_MP_ZERO_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* set to zero */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_PRIME_TAB_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
14
1615 const mp_digit ltm_prime_tab[] = {
1716 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013,
1817 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035,
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_REVERSE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* reverse an array, used for radix code */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_S_MP_ADD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* low level addition, based on HAC pp.594, Algorithm 14.7 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_S_MP_EXPTMOD_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
14
1615 #ifdef MP_LOW_MEM
1716 # define TAB_SIZE 32
1817 #else
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_S_MP_MUL_DIGS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* multiplies |a| * |b| and only computes upto digs digits of result
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_S_MP_MUL_HIGH_DIGS_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* multiplies |a| * |b| and does not compute the lower digs digits
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_S_MP_SQR_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* low level squaring, b = a*a, HAC pp.596-597, Algorithm 14.16 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BN_S_MP_SUB_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
0 #include <tommath_private.h>
0 #include "tommath_private.h"
11 #ifdef BNCORE_C
22 /* LibTomMath, multiple-precision integer library -- Tom St Denis
33 *
1010 *
1111 * The library is free for all purposes without any express
1212 * guarantee it works.
13 *
14 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org
1513 */
1614
1715 /* Known optimal configurations
88 *
99 * The library is free for all purposes without any express
1010 * guarantee it works.
11 *
12 * Tom St Denis, tstdenis82@gmail.com, http://math.libtomcrypt.com
1311 */
1412 #ifndef BN_H_
1513 #define BN_H_
1816 #include <stdlib.h>
1917 #include <limits.h>
2018
21 #if !defined(_MSC_VER) || _MSC_VER >= 1600
22 /* supported since Microsoft Visual Studio 2010 */
23 #include <stdint.h>
24 #else
25 typedef signed char int8_t;
26 typedef unsigned char uint8_t;
27 typedef signed short int16_t;
28 typedef unsigned short uint16_t;
29 typedef signed int int32_t;
30 typedef unsigned int uint32_t;
31 # ifdef _MSC_VER
32 /* long long does not work before MS Visual C++ 7.0 */
33 typedef signed __int64 int64_t;
34 typedef unsigned __int64 uint64_t;
35 # else
36 typedef long long int64_t;
37 typedef unsigned long long uint64_t;
38 # endif
39 #endif
40
4119 #include <tommath_class.h>
4220
4321 #ifdef __cplusplus
4523 #endif
4624
4725 /* MS Visual C++ doesn't have a 128bit type for words, so fall back to 32bit MPI's (where words are 64bit) */
48 #if defined(_MSC_VER) || defined(__LLP64__)
26 #if defined(_MSC_VER) || defined(__LLP64__) || defined(__e2k__) || defined(__LCC__)
4927 # define MP_32BIT
5028 #endif
5129
5735 defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
5836 defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
5937 # if !(defined(MP_32BIT) || defined(MP_16BIT) || defined(MP_8BIT))
60 # define MP_64BIT
38 # if defined(__GNUC__)
39 /* we support 128bit integers only via: __attribute__((mode(TI))) */
40 # define MP_64BIT
41 # else
42 /* otherwise we fall back to MP_32BIT even on 64bit platforms */
43 # define MP_32BIT
44 # endif
6145 # endif
6246 #endif
6347
7054 * [any size beyond that is ok provided it doesn't overflow the data type]
7155 */
7256 #ifdef MP_8BIT
73 typedef uint8_t mp_digit;
74 typedef uint16_t mp_word;
57 typedef unsigned char mp_digit;
58 typedef unsigned short mp_word;
7559 # define MP_SIZEOF_MP_DIGIT 1
7660 # ifdef DIGIT_BIT
7761 # error You must not define DIGIT_BIT when using MP_8BIT
7862 # endif
7963 #elif defined(MP_16BIT)
80 typedef uint16_t mp_digit;
81 typedef uint32_t mp_word;
64 typedef unsigned short mp_digit;
65 typedef unsigned int mp_word;
8266 # define MP_SIZEOF_MP_DIGIT 2
8367 # ifdef DIGIT_BIT
8468 # error You must not define DIGIT_BIT when using MP_16BIT
8569 # endif
8670 #elif defined(MP_64BIT)
8771 /* for GCC only on supported platforms */
88 typedef uint64_t mp_digit;
89 # if defined(__GNUC__)
72 typedef unsigned long long mp_digit;
9073 typedef unsigned long mp_word __attribute__((mode(TI)));
91 # else
92 /* it seems you have a problem
93 * but we assume you can somewhere define your own uint128_t */
94 typedef uint128_t mp_word;
95 # endif
96
9774 # define DIGIT_BIT 60
9875 #else
9976 /* this is the default case, 28-bit digits */
10077
10178 /* this is to make porting into LibTomCrypt easier :-) */
102 typedef uint32_t mp_digit;
103 typedef uint64_t mp_word;
79 typedef unsigned int mp_digit;
80 #ifdef _MSC_VER
81 typedef unsigned __int64 mp_word;
82 #else
83 typedef unsigned long long mp_word; /* PATCHED */
84 #endif
10485
10586 # ifdef MP_31BIT
10687 /* this is an extension that uses 31-bit digits */
11596 /* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
11697 #ifndef DIGIT_BIT
11798 # define DIGIT_BIT (((CHAR_BIT * MP_SIZEOF_MP_DIGIT) - 1)) /* bits per digit */
118 typedef uint_least32_t mp_min_u32;
99 typedef unsigned long mp_min_u32;
119100 #else
120101 typedef mp_digit mp_min_u32;
121 #endif
122
123 /* use arc4random on platforms that support it */
124 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
125 # define MP_GEN_RANDOM() arc4random()
126 # define MP_GEN_RANDOM_MAX 0xffffffffu
127 #endif
128
129 /* use rand() as fall-back if there's no better rand function */
130 #ifndef MP_GEN_RANDOM
131 # define MP_GEN_RANDOM() rand()
132 # define MP_GEN_RANDOM_MAX RAND_MAX
133102 #endif
134103
135104 #define MP_DIGIT_BIT DIGIT_BIT
241210 int mp_set_long(mp_int *a, unsigned long b);
242211
243212 /* set a platform dependent unsigned long long value */
244 int mp_set_long_long(mp_int *a, uint64_t b);
213 /* int mp_set_long_long(mp_int *a, unsigned long long b); */
245214
246215 /* get a 32-bit value */
247216 unsigned long mp_get_int(const mp_int *a);
250219 unsigned long mp_get_long(const mp_int *a);
251220
252221 /* get a platform dependent unsigned long long value */
253 uint64_t mp_get_long_long(const mp_int *a);
222 /* unsigned long long mp_get_long_long(const mp_int *a); */
254223
255224 /* initialize and set a digit */
256225 int mp_init_set(mp_int *a, mp_digit b);
306275
307276 /* makes a pseudo-random int of a given size */
308277 int mp_rand(mp_int *a, int digits);
278
279 #ifdef MP_PRNG_ENABLE_LTM_RNG
280 /* as last resort we will fall back to libtomcrypt's rng_get_bytes()
281 * in case you don't use libtomcrypt or use it w/o rng_get_bytes()
282 * you have to implement it somewhere else, as it's required */
283 extern unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
284 extern void (*ltm_rng_callback)(void);
285 #endif
309286
310287 /* ---> binary operations <--- */
311288 /* c = a XOR b */
88 *
99 * The library is free for all purposes without any express
1010 * guarantee it works.
11 *
12 * Tom St Denis, tstdenis82@gmail.com, http://math.libtomcrypt.com
1311 */
1412 #ifndef TOMMATH_PRIV_H_
1513 #define TOMMATH_PRIV_H_
1614
17 #include <tommath.h>
15 #include "tommath.h"
1816 #include <ctype.h>
1917
2018 #ifndef MIN
7573 void bn_reverse(unsigned char *s, int len);
7674
7775 extern const char *mp_s_rmap;
78 extern const uint8_t mp_s_rmap_reverse[];
76 extern const unsigned char mp_s_rmap_reverse[];
7977 extern const size_t mp_s_rmap_reverse_sz;
8078
8179 /* Fancy macro to set an MPI from another type.
1010 is( Crypt::Cipher::DES_EDE::blocksize, 8, '::blocksize');
1111 is( Crypt::Cipher::DES_EDE::keysize, 24, '::keysize');
1212 is( Crypt::Cipher::DES_EDE::max_keysize, 24, '::max_keysize');
13 is( Crypt::Cipher::DES_EDE::min_keysize, 24, '::min_keysize');
13 is( Crypt::Cipher::DES_EDE::min_keysize, 16, '::min_keysize');
1414 is( Crypt::Cipher::DES_EDE::default_rounds, 16, '::default_rounds');
1515
1616 is( Crypt::Cipher::DES_EDE->blocksize, 8, '->blocksize');
1717 is( Crypt::Cipher::DES_EDE->keysize, 24, '->keysize');
1818 is( Crypt::Cipher::DES_EDE->max_keysize, 24, '->max_keysize');
19 is( Crypt::Cipher::DES_EDE->min_keysize, 24, '->min_keysize');
19 is( Crypt::Cipher::DES_EDE->min_keysize, 16, '->min_keysize');
2020 is( Crypt::Cipher::DES_EDE->default_rounds, 16, '->default_rounds');
2121
2222 my $min_key = 'kkkkkkkkkkkkkkkkkkkkkkkk';
2525 is( Crypt::Cipher::blocksize('DES_EDE'), 8, 'Cipher->blocksize');
2626 is( Crypt::Cipher::keysize('DES_EDE'), 24, 'Cipher->keysize');
2727 is( Crypt::Cipher::max_keysize('DES_EDE'), 24, 'Cipher->max_keysize');
28 is( Crypt::Cipher::min_keysize('DES_EDE'), 24, 'Cipher->min_keysize');
28 is( Crypt::Cipher::min_keysize('DES_EDE'), 16, 'Cipher->min_keysize');
2929 is( Crypt::Cipher::default_rounds('DES_EDE'), 16, 'Cipher->default_rounds');
3030
3131 is( Crypt::Cipher->blocksize('DES_EDE'), 8, 'Cipher->blocksize');
3232 is( Crypt::Cipher->keysize('DES_EDE'), 24, 'Cipher->keysize');
3333 is( Crypt::Cipher->max_keysize('DES_EDE'), 24, 'Cipher->max_keysize');
34 is( Crypt::Cipher->min_keysize('DES_EDE'), 24, 'Cipher->min_keysize');
34 is( Crypt::Cipher->min_keysize('DES_EDE'), 16, 'Cipher->min_keysize');
3535 is( Crypt::Cipher->default_rounds('DES_EDE'), 16, 'Cipher->default_rounds');
3636
3737 is( Crypt::Cipher::DES_EDE->new($min_key)->blocksize, 8, 'DES_EDE->new()->blocksize');
3838 is( Crypt::Cipher::DES_EDE->new($min_key)->keysize, 24, 'DES_EDE->new()->keysize');
3939 is( Crypt::Cipher::DES_EDE->new($min_key)->max_keysize, 24, 'DES_EDE->new()->max_keysize');
40 is( Crypt::Cipher::DES_EDE->new($min_key)->min_keysize, 24, 'DES_EDE->new()->min_keysize');
40 is( Crypt::Cipher::DES_EDE->new($min_key)->min_keysize, 16, 'DES_EDE->new()->min_keysize');
4141 is( Crypt::Cipher::DES_EDE->new($min_key)->default_rounds, 16, 'DES_EDE->new()->default_rounds');
4242
4343 is( Crypt::Cipher->new('DES_EDE', $min_key)->blocksize, 8, 'Cipher->new()->blocksize');
4444 is( Crypt::Cipher->new('DES_EDE', $min_key)->keysize, 24, 'Cipher->new()->keysize');
4545 is( Crypt::Cipher->new('DES_EDE', $min_key)->max_keysize, 24, 'Cipher->new()->max_keysize');
46 is( Crypt::Cipher->new('DES_EDE', $min_key)->min_keysize, 24, 'Cipher->new()->min_keysize');
46 is( Crypt::Cipher->new('DES_EDE', $min_key)->min_keysize, 16, 'Cipher->new()->min_keysize');
4747 is( Crypt::Cipher->new('DES_EDE', $min_key)->default_rounds, 16, 'Cipher->new()->default_rounds');
4848
4949 my $block_plain = 'BBBBBBBB';
00 use strict;
11 use warnings;
22
3 use Test::More tests => 660;
3 use Test::More tests => 663;
44 use Crypt::PK::ECC;
55
66 my $data = [
8787 ok( $ec_pub->verify_message(pack("H*", $h->{ECDSA_SHA1}), 'test-data', 'SHA1'), "$h->{PRI_FILE}/ECDSA_SHA1");
8888 ok( $ec_pub->verify_message(pack("H*", $h->{ECDSA_SHA256}), 'test-data', 'SHA256'), "$h->{PRI_FILE}/ECDSA_SHA256");
8989 }
90
91 ### ecc_set_key bug
92 {
93 my $der = pack("H*", "3081be020101041500c78b055db0706fd86b5a15e14b9e51f0043d18f9a074307202010130200607".
94 "2a8648ce3d0101021500fffffffffffffffffffffffffffffffeffffac7330060401000401070429".
95 "043b4c382ce37aa192a4019e763036f4f5dd4d7ebb938cf935318fdced6bc28286531733c3f03c4f".
96 "ee02150100000000000000000001b8fa16dfab9aca16b6b3020101a12c032a00040ca9f03d79907c".
97 "97538177a3027970abefa351073a73120e1b5d2dab02dde37c118a44f8cb267b56");
98 my $pk = Crypt::PK::ECC->new(\$der);
99 ok($pk->is_private, "ecc_set_key bug");
100 }
101
102 ### der_length_custom_type bug:
103 {
104 my $der = pack("H*", "3081a8020101040e20df177a6f7e4bb9fecbd2d75b57a07f307d020101301a06072a8648ce3d0101".
105 "020f00db7c2abf62e35e668076bead208b3037040edb7c2abf62e35e668076bead2088040e659ef8".
106 "ba043916eede8911702b2203150000f50b028e4d696e676875615175290472783fb1040f02094872".
107 "39995a5ee76b55f9c2f098020f00db7c2abf62e35e7628dfac6561c5020101a11203100003a63510".
108 "5c5717812580408a3fd5ac");
109 my $pk = Crypt::PK::ECC->new(\$der);
110 ok($pk->is_private, "der_length_custom_type bug");
111 }
112
113 ### ecc_import_pkcs8 bug
114 {
115 my $der = pack("H*", "308193020100301306072a8648ce3d020106082a8648ce3d030107047930770201010420052f2b26".
116 "1d38522126f6a49cbdb958d5af85ed2c9e8cfebff85c44f20fe89a0fa00a06082a8648ce3d030107".
117 "a144034200043998011b129539269423f531808ed0854bef5b93d00aad8438742c1d272c24ca8649".
118 "e07f17da47c07453183fe35b68d069b700c38cc61dd5d98eb92dc474b573");
119 my $pk = Crypt::PK::ECC->new(\$der);
120 ok($pk->is_private, "ecc_import_pkcs8 bug");
121 }