From 3cc754610d6e629483d68913a167ca8da5734122 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 5 Aug 2022 16:44:17 +0200 Subject: [PATCH 1/7] `undef` Macros after usage & rename duplicate symbols Signed-off-by: Steffen Jaeckel --- src/ciphers/aes/aes.c | 8 +++- src/ciphers/anubis.c | 17 ++++++++ src/ciphers/blowfish.c | 2 + src/ciphers/camellia.c | 2 + src/ciphers/cast5.c | 4 ++ src/ciphers/des.c | 3 ++ src/ciphers/idea.c | 8 ++++ src/ciphers/kasumi.c | 4 ++ src/ciphers/khazad.c | 2 + src/ciphers/kseed.c | 3 ++ src/ciphers/noekeon.c | 7 ++++ src/ciphers/rc5.c | 3 ++ src/ciphers/rc6.c | 6 ++- src/ciphers/safer/safer.c | 11 +++-- src/ciphers/safer/saferp.c | 13 +++++- src/ciphers/serpent.c | 42 +++++++++++++++++++ src/ciphers/skipjack.c | 5 +++ src/ciphers/tea.c | 3 ++ src/ciphers/twofish/twofish.c | 11 +++++ src/encauth/gcm/gcm_gf_mult.c | 4 ++ src/encauth/ocb/ocb_init.c | 3 ++ src/encauth/ocb3/ocb3_init.c | 3 ++ src/hashes/blake2b.c | 34 +++++++-------- src/hashes/blake2s.c | 32 +++++++------- src/hashes/md4.c | 20 +++++++++ src/hashes/md5.c | 11 ++++- src/hashes/rmd128.c | 14 ++++++- src/hashes/rmd160.c | 17 +++++++- src/hashes/rmd256.c | 13 ++++++ src/hashes/rmd320.c | 16 +++++++ src/hashes/sha1.c | 9 ++++ src/hashes/sha2/sha256.c | 9 ++++ src/hashes/sha2/sha512.c | 12 ++++-- src/hashes/tiger.c | 5 +++ src/hashes/whirl/whirl.c | 2 + src/mac/pelican/pelican.c | 2 + src/mac/pmac/pmac_init.c | 3 ++ src/modes/cfb/cfb_decrypt.c | 3 ++ src/modes/cfb/cfb_encrypt.c | 3 ++ .../der_decode_generalizedtime.c | 8 +++- .../der_encode_generalizedtime.c | 6 ++- src/pk/asn1/der/set/der_encode_set.c | 3 +- src/pk/asn1/der/set/der_encode_setof.c | 3 ++ src/pk/asn1/der/utctime/der_decode_utctime.c | 7 +++- src/pk/asn1/der/utctime/der_encode_utctime.c | 5 ++- src/pk/ec25519/tweetnacl.c | 5 +++ src/stream/chacha/chacha_crypt.c | 2 + src/stream/chacha/chacha_setup.c | 5 +-- src/stream/salsa20/salsa20_crypt.c | 2 + src/stream/salsa20/salsa20_setup.c | 5 +-- src/stream/salsa20/xsalsa20_setup.c | 3 +- src/stream/sober128/sober128_stream.c | 12 ++++++ 52 files changed, 372 insertions(+), 63 deletions(-) diff --git a/src/ciphers/aes/aes.c b/src/ciphers/aes/aes.c index 3a5d0dbb8..889d79375 100644 --- a/src/ciphers/aes/aes.c +++ b/src/ciphers/aes/aes.c @@ -726,5 +726,11 @@ int ECB_KS(int *keysize) return CRYPT_OK; } -#endif +#undef SETUP +#undef ECB_ENC +#undef ECB_DEC +#undef ECB_DONE +#undef ECB_TEST +#undef ECB_KS +#endif diff --git a/src/ciphers/anubis.c b/src/ciphers/anubis.c index f6a1c58db..8338fa730 100644 --- a/src/ciphers/anubis.c +++ b/src/ciphers/anubis.c @@ -26,6 +26,14 @@ const struct ltc_cipher_descriptor anubis_desc = { #define MAX_N 10 + +#define T0 anubis_T0 +#define T1 anubis_T1 +#define T2 anubis_T2 +#define T3 anubis_T3 +#define T4 anubis_T4 +#define T5 anubis_T5 +#define rc anubis_rc /* * Though Anubis is endianness-neutral, the encryption tables are listed * in BIG-ENDIAN format, which is adopted throughout this implementation @@ -1546,5 +1554,14 @@ int anubis_keysize(int *keysize) return CRYPT_OK; } +#undef MAX_N +#undef T0 +#undef T1 +#undef T2 +#undef T3 +#undef T4 +#undef T5 +#undef rc + #endif diff --git a/src/ciphers/blowfish.c b/src/ciphers/blowfish.c index 4187701d6..a2cd392ee 100644 --- a/src/ciphers/blowfish.c +++ b/src/ciphers/blowfish.c @@ -654,5 +654,7 @@ int blowfish_keysize(int *keysize) return CRYPT_OK; } +#undef F + #endif diff --git a/src/ciphers/camellia.c b/src/ciphers/camellia.c index 733e96395..702c602e8 100644 --- a/src/ciphers/camellia.c +++ b/src/ciphers/camellia.c @@ -717,4 +717,6 @@ int camellia_keysize(int *keysize) return CRYPT_OK; } +#undef loc + #endif diff --git a/src/ciphers/cast5.c b/src/ciphers/cast5.c index 3b401a5c8..d8279b595 100644 --- a/src/ciphers/cast5.c +++ b/src/ciphers/cast5.c @@ -491,6 +491,7 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_ } #endif +#define FI cast5_FI LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr) { ulong32 I; @@ -702,4 +703,7 @@ int cast5_keysize(int *keysize) return CRYPT_OK; } +#undef GB +#undef FI + #endif diff --git a/src/ciphers/des.c b/src/ciphers/des.c index eca87e576..c20dbf98d 100644 --- a/src/ciphers/des.c +++ b/src/ciphers/des.c @@ -2238,5 +2238,8 @@ int des3_keysize(int *keysize) return CRYPT_OK; } +#undef EN0 +#undef DE1 + #endif diff --git a/src/ciphers/idea.c b/src/ciphers/idea.c index b195a4c94..40adc5834 100644 --- a/src/ciphers/idea.c +++ b/src/ciphers/idea.c @@ -66,6 +66,7 @@ static ushort16 s_add_inv(ushort16 x) return LOW16(0 - x); } +#define s_setup_key s_idea_setup_key static int s_setup_key(const unsigned char *key, symmetric_key *skey) { int i, j; @@ -247,4 +248,11 @@ int idea_test(void) #endif } +#undef LOW16 +#undef HIGH16 +#undef MUL +#undef STORE16 +#undef LOAD16 +#undef s_setup_key + #endif diff --git a/src/ciphers/kasumi.c b/src/ciphers/kasumi.c index 1ade9d694..de76fc73e 100644 --- a/src/ciphers/kasumi.c +++ b/src/ciphers/kasumi.c @@ -28,6 +28,7 @@ const struct ltc_cipher_descriptor kasumi_desc = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; +#define FI kasumi_FI static u16 FI( u16 in, u16 subkey ) { u16 nine, seven; @@ -304,4 +305,7 @@ int kasumi_test(void) #endif } +#undef ROL16 +#undef FI + #endif diff --git a/src/ciphers/khazad.c b/src/ciphers/khazad.c index 72cbda879..ccd1b7020 100644 --- a/src/ciphers/khazad.c +++ b/src/ciphers/khazad.c @@ -837,4 +837,6 @@ int khazad_keysize(int *keysize) return CRYPT_INVALID_KEYSIZE; } +#undef R + #endif diff --git a/src/ciphers/kseed.c b/src/ciphers/kseed.c index 1e63aec42..014b4e336 100644 --- a/src/ciphers/kseed.c +++ b/src/ciphers/kseed.c @@ -363,4 +363,7 @@ int kseed_keysize(int *keysize) return CRYPT_OK; } +#undef G +#undef F + #endif diff --git a/src/ciphers/noekeon.c b/src/ciphers/noekeon.c index 58d73aec7..3dfe91e6e 100644 --- a/src/ciphers/noekeon.c +++ b/src/ciphers/noekeon.c @@ -313,5 +313,12 @@ int noekeon_keysize(int *keysize) return CRYPT_OK; } +#undef kTHETA +#undef THETA +#undef GAMMA +#undef PI1 +#undef PI2 +#undef ROUND + #endif diff --git a/src/ciphers/rc5.c b/src/ciphers/rc5.c index f21ba4f4d..4f01fc003 100644 --- a/src/ciphers/rc5.c +++ b/src/ciphers/rc5.c @@ -24,6 +24,7 @@ const struct ltc_cipher_descriptor rc5_desc = NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; +#define stab rc5_stab static const ulong32 stab[50] = { 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL, 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL, @@ -317,6 +318,8 @@ int rc5_keysize(int *keysize) return CRYPT_OK; } +#undef stab + #endif diff --git a/src/ciphers/rc6.c b/src/ciphers/rc6.c index d1341d31b..06c52aa89 100644 --- a/src/ciphers/rc6.c +++ b/src/ciphers/rc6.c @@ -23,6 +23,7 @@ const struct ltc_cipher_descriptor rc6_desc = NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; +#define stab rc6_stab static const ulong32 stab[44] = { 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL, 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL, @@ -319,6 +320,7 @@ int rc6_keysize(int *keysize) return CRYPT_OK; } -#endif /*LTC_RC6*/ - +#undef RND +#undef stab +#endif /*LTC_RC6*/ diff --git a/src/ciphers/safer/safer.c b/src/ciphers/safer/safer.c index 84cc2d01a..f0c4a6b19 100644 --- a/src/ciphers/safer/safer.c +++ b/src/ciphers/safer/safer.c @@ -24,8 +24,10 @@ #ifdef LTC_SAFER +#ifndef LTC_SAFER_TAB_C #define LTC_SAFER_TAB_C #include "safer_tab.c" +#endif const struct ltc_cipher_descriptor safer_k64_desc = { "safer-k64", @@ -478,7 +480,10 @@ int safer_sk128_test(void) #endif } -#endif - - +#undef ROL8 +#undef EXP +#undef LOG +#undef PHT +#undef IPHT +#endif diff --git a/src/ciphers/safer/saferp.c b/src/ciphers/safer/saferp.c index 86cc70896..6803d72aa 100644 --- a/src/ciphers/safer/saferp.c +++ b/src/ciphers/safer/saferp.c @@ -9,8 +9,10 @@ #ifdef LTC_SAFERP +#ifndef LTC_SAFER_TAB_C #define LTC_SAFER_TAB_C #include "safer_tab.c" +#endif const struct ltc_cipher_descriptor saferp_desc = { @@ -562,6 +564,13 @@ int saferp_keysize(int *keysize) return CRYPT_OK; } -#endif - +#undef ROUND +#undef iROUND +#undef PHT +#undef iPHT +#undef SHUF +#undef iSHUF +#undef LT +#undef iLT +#endif diff --git a/src/ciphers/serpent.c b/src/ciphers/serpent.c index dbb999eb1..8f5b7858e 100644 --- a/src/ciphers/serpent.c +++ b/src/ciphers/serpent.c @@ -443,6 +443,7 @@ const struct ltc_cipher_descriptor serpent_desc = { k[(8-r)*4 + 7] = d; \ } +#define s_setup_key s_serpent_setup_key static int s_setup_key(const unsigned char *key, int keylen, int rounds, ulong32 *k) { int i; @@ -714,4 +715,45 @@ int serpent_test(void) #endif } +#undef s_lt +#undef s_ilt +#undef s_beforeS0 +#undef s_afterS0 +#undef s_afterS1 +#undef s_afterS2 +#undef s_afterS3 +#undef s_afterS4 +#undef s_afterS5 +#undef s_afterS6 +#undef s_afterS7 +#undef s_beforeI7 +#undef s_afterI7 +#undef s_afterI6 +#undef s_afterI5 +#undef s_afterI4 +#undef s_afterI3 +#undef s_afterI2 +#undef s_afterI1 +#undef s_afterI0 +#undef s_s0 +#undef s_i0 +#undef s_s1 +#undef s_i1 +#undef s_s2 +#undef s_i2 +#undef s_s3 +#undef s_i3 +#undef s_s4 +#undef s_i4 +#undef s_s5 +#undef s_i5 +#undef s_s6 +#undef s_i6 +#undef s_s7 +#undef s_i7 +#undef s_kx +#undef s_lk +#undef s_sk +#undef s_setup_key + #endif diff --git a/src/ciphers/skipjack.c b/src/ciphers/skipjack.c index 0251946a8..cbcfe7033 100644 --- a/src/ciphers/skipjack.c +++ b/src/ciphers/skipjack.c @@ -331,4 +331,9 @@ int skipjack_keysize(int *keysize) return CRYPT_OK; } +#undef RULE_A +#undef RULE_B +#undef RULE_A1 +#undef RULE_B1 + #endif diff --git a/src/ciphers/tea.c b/src/ciphers/tea.c index fc413d423..2e3a040c8 100644 --- a/src/ciphers/tea.c +++ b/src/ciphers/tea.c @@ -205,5 +205,8 @@ int tea_keysize(int *keysize) return CRYPT_OK; } +#undef DELTA +#undef SUM + #endif diff --git a/src/ciphers/twofish/twofish.c b/src/ciphers/twofish/twofish.c index c006361ba..48149a2d2 100644 --- a/src/ciphers/twofish/twofish.c +++ b/src/ciphers/twofish/twofish.c @@ -702,5 +702,16 @@ int twofish_keysize(int *keysize) return CRYPT_OK; } +#undef MDS_POLY +#undef RS_POLY +#undef sbox +#undef mds_column_mult +#undef S1 +#undef S2 +#undef S3 +#undef S4 +#undef g_func +#undef g1_func + #endif diff --git a/src/encauth/gcm/gcm_gf_mult.c b/src/encauth/gcm/gcm_gf_mult.c index dcfe7e3eb..f2669ec84 100644 --- a/src/encauth/gcm/gcm_gf_mult.c +++ b/src/encauth/gcm/gcm_gf_mult.c @@ -203,6 +203,10 @@ void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char * } +#undef M +#undef BPD +#undef WPV + #endif #endif diff --git a/src/encauth/ocb/ocb_init.c b/src/encauth/ocb/ocb_init.c index 1ae58b6c7..eef01ec0e 100644 --- a/src/encauth/ocb/ocb_init.c +++ b/src/encauth/ocb/ocb_init.c @@ -9,6 +9,7 @@ #ifdef LTC_OCB_MODE +#define polys ocb_polys static const struct { int len; unsigned char poly_div[MAXBLOCKSIZE], @@ -126,4 +127,6 @@ int ocb_init(ocb_state *ocb, int cipher, return CRYPT_OK; } +#undef polys + #endif diff --git a/src/encauth/ocb3/ocb3_init.c b/src/encauth/ocb3/ocb3_init.c index 09493acc9..f506c85f6 100644 --- a/src/encauth/ocb3/ocb3_init.c +++ b/src/encauth/ocb3/ocb3_init.c @@ -54,6 +54,7 @@ static void s_ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *no } } +#define polys ocb3_polys static const struct { int len; unsigned char poly_mul[MAXBLOCKSIZE]; @@ -183,4 +184,6 @@ int ocb3_init(ocb3_state *ocb, int cipher, return CRYPT_OK; } +#undef polys + #endif diff --git a/src/hashes/blake2b.c b/src/hashes/blake2b.c index 8cbeac216..73f50f7f2 100644 --- a/src/hashes/blake2b.c +++ b/src/hashes/blake2b.c @@ -32,18 +32,18 @@ enum blake2b_constant { /* param offsets */ enum { - O_DIGEST_LENGTH = 0, - O_KEY_LENGTH = 1, - O_FANOUT = 2, - O_DEPTH = 3, - O_LEAF_LENGTH = 4, - O_NODE_OFFSET = 8, - O_XOF_LENGTH = 12, - O_NODE_DEPTH = 16, - O_INNER_LENGTH = 17, - O_RESERVED = 18, - O_SALT = 32, - O_PERSONAL = 48 + BLAKE2B_O_DIGEST_LENGTH = 0, + BLAKE2B_O_KEY_LENGTH = 1, + BLAKE2B_O_FANOUT = 2, + BLAKE2B_O_DEPTH = 3, + BLAKE2B_O_LEAF_LENGTH = 4, + BLAKE2B_O_NODE_OFFSET = 8, + BLAKE2B_O_XOF_LENGTH = 12, + BLAKE2B_O_NODE_DEPTH = 16, + BLAKE2B_O_INNER_LENGTH = 17, + BLAKE2B_O_RESERVED = 18, + BLAKE2B_O_SALT = 32, + BLAKE2B_O_PERSONAL = 48 }; /* @@ -190,7 +190,7 @@ static int s_blake2b_init_param(hash_state *md, const unsigned char *P) md->blake2b.h[i] ^= tmp; } - md->blake2b.outlen = P[O_DIGEST_LENGTH]; + md->blake2b.outlen = P[BLAKE2B_O_DIGEST_LENGTH]; return CRYPT_OK; } @@ -223,10 +223,10 @@ int blake2b_init(hash_state *md, unsigned long outlen, const unsigned char *key, XMEMSET(P, 0, sizeof(P)); - P[O_DIGEST_LENGTH] = (unsigned char)outlen; - P[O_KEY_LENGTH] = (unsigned char)keylen; - P[O_FANOUT] = 1; - P[O_DEPTH] = 1; + P[BLAKE2B_O_DIGEST_LENGTH] = (unsigned char)outlen; + P[BLAKE2B_O_KEY_LENGTH] = (unsigned char)keylen; + P[BLAKE2B_O_FANOUT] = 1; + P[BLAKE2B_O_DEPTH] = 1; err = s_blake2b_init_param(md, P); if (err != CRYPT_OK) return err; diff --git a/src/hashes/blake2s.c b/src/hashes/blake2s.c index e8cd6eb4f..2add8093f 100644 --- a/src/hashes/blake2s.c +++ b/src/hashes/blake2s.c @@ -32,17 +32,17 @@ enum blake2s_constant { /* param offsets */ enum { - O_DIGEST_LENGTH = 0, - O_KEY_LENGTH = 1, - O_FANOUT = 2, - O_DEPTH = 3, - O_LEAF_LENGTH = 4, - O_NODE_OFFSET = 8, - O_XOF_LENGTH = 12, - O_NODE_DEPTH = 14, - O_INNER_LENGTH = 15, - O_SALT = 16, - O_PERSONAL = 24 + BLAKE2S_O_DIGEST_LENGTH = 0, + BLAKE2S_O_KEY_LENGTH = 1, + BLAKE2S_O_FANOUT = 2, + BLAKE2S_O_DEPTH = 3, + BLAKE2S_O_LEAF_LENGTH = 4, + BLAKE2S_O_NODE_OFFSET = 8, + BLAKE2S_O_XOF_LENGTH = 12, + BLAKE2S_O_NODE_DEPTH = 14, + BLAKE2S_O_INNER_LENGTH = 15, + BLAKE2S_O_SALT = 16, + BLAKE2S_O_PERSONAL = 24 }; /* @@ -184,7 +184,7 @@ static int s_blake2s_init_param(hash_state *md, const unsigned char *P) md->blake2s.h[i] ^= tmp; } - md->blake2s.outlen = P[O_DIGEST_LENGTH]; + md->blake2s.outlen = P[BLAKE2S_O_DIGEST_LENGTH]; return CRYPT_OK; } @@ -217,10 +217,10 @@ int blake2s_init(hash_state *md, unsigned long outlen, const unsigned char *key, XMEMSET(P, 0, sizeof(P)); - P[O_DIGEST_LENGTH] = (unsigned char)outlen; - P[O_KEY_LENGTH] = (unsigned char)keylen; - P[O_FANOUT] = 1; - P[O_DEPTH] = 1; + P[BLAKE2S_O_DIGEST_LENGTH] = (unsigned char)outlen; + P[BLAKE2S_O_KEY_LENGTH] = (unsigned char)keylen; + P[BLAKE2S_O_FANOUT] = 1; + P[BLAKE2S_O_DEPTH] = 1; err = s_blake2s_init_param(md, P); if (err != CRYPT_OK) return err; diff --git a/src/hashes/md4.c b/src/hashes/md4.c index aadad5f14..4fba8ba68 100644 --- a/src/hashes/md4.c +++ b/src/hashes/md4.c @@ -291,6 +291,26 @@ int md4_test(void) #endif } +#undef F +#undef G +#undef H +#undef FF +#undef GG +#undef HH +#undef S11 +#undef S12 +#undef S13 +#undef S14 +#undef S21 +#undef S22 +#undef S23 +#undef S24 +#undef S31 +#undef S32 +#undef S33 +#undef S34 +#undef ROTATE_LEFT + #endif diff --git a/src/hashes/md5.c b/src/hashes/md5.c index ad404e186..912933ec1 100644 --- a/src/hashes/md5.c +++ b/src/hashes/md5.c @@ -351,6 +351,13 @@ int md5_test(void) #endif } -#endif - +#undef F +#undef G +#undef H +#undef I +#undef FF +#undef GG +#undef HH +#undef II +#endif diff --git a/src/hashes/rmd128.c b/src/hashes/rmd128.c index e8f63e1ce..7d57af8c1 100644 --- a/src/hashes/rmd128.c +++ b/src/hashes/rmd128.c @@ -392,5 +392,17 @@ int rmd128_test(void) #endif } -#endif +#undef F +#undef G +#undef H +#undef I +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III +#endif diff --git a/src/hashes/rmd160.c b/src/hashes/rmd160.c index 1eba20a58..6e45a7e50 100644 --- a/src/hashes/rmd160.c +++ b/src/hashes/rmd160.c @@ -451,5 +451,20 @@ int rmd160_test(void) #endif } -#endif +#undef F +#undef G +#undef H +#undef I +#undef J +#undef FF +#undef GG +#undef HH +#undef II +#undef JJ +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef JJJ +#endif diff --git a/src/hashes/rmd256.c b/src/hashes/rmd256.c index 009719819..704c86ee5 100644 --- a/src/hashes/rmd256.c +++ b/src/hashes/rmd256.c @@ -417,4 +417,17 @@ int rmd256_test(void) #endif } +#undef F +#undef G +#undef H +#undef I +#undef FF +#undef GG +#undef HH +#undef II +#undef FFF +#undef GGG +#undef HHH +#undef III + #endif diff --git a/src/hashes/rmd320.c b/src/hashes/rmd320.c index 0021d6733..e25b6d93a 100644 --- a/src/hashes/rmd320.c +++ b/src/hashes/rmd320.c @@ -482,4 +482,20 @@ int rmd320_test(void) #endif } +#undef F +#undef G +#undef H +#undef I +#undef J +#undef FF +#undef GG +#undef HH +#undef II +#undef JJ +#undef FFF +#undef GGG +#undef HHH +#undef III +#undef JJJ + #endif diff --git a/src/hashes/sha1.c b/src/hashes/sha1.c index 40709db8c..a00c7f7be 100644 --- a/src/hashes/sha1.c +++ b/src/hashes/sha1.c @@ -271,6 +271,15 @@ int sha1_test(void) #endif } +#undef F0 +#undef F1 +#undef F2 +#undef F3 +#undef FF0 +#undef FF1 +#undef FF2 +#undef FF3 + #endif diff --git a/src/hashes/sha2/sha256.c b/src/hashes/sha2/sha256.c index ff2006807..3a012d87b 100644 --- a/src/hashes/sha2/sha256.c +++ b/src/hashes/sha2/sha256.c @@ -317,6 +317,15 @@ int sha256_test(void) #endif } +#undef Ch +#undef Maj +#undef S +#undef R +#undef Sigma0 +#undef Sigma1 +#undef Gamma0 +#undef Gamma1 + #endif diff --git a/src/hashes/sha2/sha512.c b/src/hashes/sha2/sha512.c index ef3a8c815..01f5632e3 100644 --- a/src/hashes/sha2/sha512.c +++ b/src/hashes/sha2/sha512.c @@ -297,7 +297,13 @@ int sha512_test(void) #endif } -#endif - - +#undef Ch +#undef Maj +#undef S +#undef R +#undef Sigma0 +#undef Sigma1 +#undef Gamma0 +#undef Gamma1 +#endif diff --git a/src/hashes/tiger.c b/src/hashes/tiger.c index 8d48ded38..e88fb3a3a 100644 --- a/src/hashes/tiger.c +++ b/src/hashes/tiger.c @@ -888,6 +888,11 @@ int tiger2_test(void) return s_tiger_test(1); } +#undef t1 +#undef t2 +#undef t3 +#undef t4 + #endif /* diff --git a/src/hashes/whirl/whirl.c b/src/hashes/whirl/whirl.c index da7db9cf5..16bcc1787 100644 --- a/src/hashes/whirl/whirl.c +++ b/src/hashes/whirl/whirl.c @@ -291,6 +291,8 @@ int whirlpool_test(void) #endif } +#undef GB +#undef theta_pi_gamma #endif diff --git a/src/mac/pelican/pelican.c b/src/mac/pelican/pelican.c index 25592cc5c..7d62e8dde 100644 --- a/src/mac/pelican/pelican.c +++ b/src/mac/pelican/pelican.c @@ -9,10 +9,12 @@ #ifdef LTC_PELICAN +#ifndef LTC_AES_TAB_C #define LTC_AES_TAB_C #define ENCRYPT_ONLY #define PELI_TAB #include "../../ciphers/aes/aes_tab.c" +#endif /** Initialize a Pelican state diff --git a/src/mac/pmac/pmac_init.c b/src/mac/pmac/pmac_init.c index 1af9c2957..616018d97 100644 --- a/src/mac/pmac/pmac_init.c +++ b/src/mac/pmac/pmac_init.c @@ -9,6 +9,7 @@ #ifdef LTC_PMAC +#define polys pmac_polys static const struct { int len; unsigned char poly_div[MAXBLOCKSIZE], @@ -135,4 +136,6 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l return err; } +#undef polys + #endif diff --git a/src/modes/cfb/cfb_decrypt.c b/src/modes/cfb/cfb_decrypt.c index 1c6d3fa44..ee06988d7 100644 --- a/src/modes/cfb/cfb_decrypt.c +++ b/src/modes/cfb/cfb_decrypt.c @@ -9,6 +9,8 @@ #ifdef LTC_CFB_MODE +#ifndef LTC_CFB_SHIFT1LEFT +#define LTC_CFB_SHIFT1LEFT static LTC_INLINE void s_shift1left_64(unsigned char *b, unsigned char v) { ulong64 bval; @@ -30,6 +32,7 @@ static LTC_INLINE void s_shift1left_128(unsigned char *b, unsigned char v) STORE64H(bval[0], b); STORE64H(bval[1], b + 8); } +#endif /* LTC_CFB_SHIFT1LEFT */ /** CFB decrypt diff --git a/src/modes/cfb/cfb_encrypt.c b/src/modes/cfb/cfb_encrypt.c index 22f8dd2f5..e0a364304 100644 --- a/src/modes/cfb/cfb_encrypt.c +++ b/src/modes/cfb/cfb_encrypt.c @@ -9,6 +9,8 @@ #ifdef LTC_CFB_MODE +#ifndef LTC_CFB_SHIFT1LEFT +#define LTC_CFB_SHIFT1LEFT static LTC_INLINE void s_shift1left_64(unsigned char *b, unsigned char v) { ulong64 bval; @@ -30,6 +32,7 @@ static LTC_INLINE void s_shift1left_128(unsigned char *b, unsigned char v) STORE64H(bval[0], b); STORE64H(bval[1], b + 8); } +#endif /* LTC_CFB_SHIFT1LEFT */ /** CFB encrypt diff --git a/src/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c b/src/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c index 7d2f2960d..b9c7d491c 100644 --- a/src/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c +++ b/src/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c @@ -10,7 +10,9 @@ #ifdef LTC_DER -static int s_char_to_int(unsigned char x) +#ifndef LTC_DER_CHAR_TO_INT +#define LTC_DER_CHAR_TO_INT +static LTC_INLINE int s_char_to_int(unsigned char x) { switch (x) { case '0': return 0; @@ -26,6 +28,7 @@ static int s_char_to_int(unsigned char x) default: return 100; } } +#endif #define DECODE_V(y, max) do {\ y = s_char_to_int(buf[x])*10 + s_char_to_int(buf[x+1]); \ @@ -132,4 +135,7 @@ YYYYMMDDhhmmss.fs-hh'mm' return CRYPT_INVALID_PACKET; } +#undef DECODE_V +#undef DECODE_V4 + #endif diff --git a/src/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c b/src/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c index 2378eeba4..ec17a68cb 100644 --- a/src/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c +++ b/src/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c @@ -10,8 +10,6 @@ #ifdef LTC_DER -static const char * const baseten = "0123456789"; - #define STORE_V(y) do {\ out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ out[x++] = der_ia5_char_encode(baseten[y % 10]); \ @@ -34,6 +32,7 @@ static const char * const baseten = "0123456789"; int der_encode_generalizedtime(const ltc_generalizedtime *gtime, unsigned char *out, unsigned long *outlen) { + const char * const baseten = "0123456789"; unsigned long x, tmplen; int err; @@ -95,4 +94,7 @@ int der_encode_generalizedtime(const ltc_generalizedtime *gtime, return CRYPT_OK; } +#undef STORE_V +#undef STORE_V4 + #endif diff --git a/src/pk/asn1/der/set/der_encode_set.c b/src/pk/asn1/der/set/der_encode_set.c index 1e90a1f2e..4f863e20d 100644 --- a/src/pk/asn1/der/set/der_encode_set.c +++ b/src/pk/asn1/der/set/der_encode_set.c @@ -15,7 +15,7 @@ static int s_ltc_to_asn1(ltc_asn1_type v) return der_asn1_type_to_identifier_map[v]; } - +#define s_qsort_helper s_set_qsort_helper static int s_qsort_helper(const void *a, const void *b) { ltc_asn1_list *A = (ltc_asn1_list *)a, *B = (ltc_asn1_list *)b; @@ -70,5 +70,6 @@ int der_encode_set(const ltc_asn1_list *list, unsigned long inlen, return err; } +#undef s_qsort_helper #endif diff --git a/src/pk/asn1/der/set/der_encode_setof.c b/src/pk/asn1/der/set/der_encode_setof.c index 184b6ec31..6cf44b39e 100644 --- a/src/pk/asn1/der/set/der_encode_setof.c +++ b/src/pk/asn1/der/set/der_encode_setof.c @@ -14,6 +14,7 @@ struct edge { unsigned long size; }; +#define s_qsort_helper s_setof_qsort_helper static int s_qsort_helper(const void *a, const void *b) { struct edge *A = (struct edge *)a, *B = (struct edge *)b; @@ -148,4 +149,6 @@ int der_encode_setof(const ltc_asn1_list *list, unsigned long inlen, return CRYPT_OK; } +#undef s_qsort_helper + #endif diff --git a/src/pk/asn1/der/utctime/der_decode_utctime.c b/src/pk/asn1/der/utctime/der_decode_utctime.c index 6149bfed9..4c78c97f6 100644 --- a/src/pk/asn1/der/utctime/der_decode_utctime.c +++ b/src/pk/asn1/der/utctime/der_decode_utctime.c @@ -9,7 +9,9 @@ #ifdef LTC_DER -static int s_char_to_int(unsigned char x) +#ifndef LTC_DER_CHAR_TO_INT +#define LTC_DER_CHAR_TO_INT +static LTC_INLINE int s_char_to_int(unsigned char x) { switch (x) { case '0': return 0; @@ -25,6 +27,7 @@ static int s_char_to_int(unsigned char x) default: return 100; } } +#endif #define DECODE_V(y, max) \ y = s_char_to_int(buf[x])*10 + s_char_to_int(buf[x+1]); \ @@ -113,4 +116,6 @@ YYMMDDhhmmss-hh'mm' return CRYPT_INVALID_PACKET; } +#undef DECODE_V + #endif diff --git a/src/pk/asn1/der/utctime/der_encode_utctime.c b/src/pk/asn1/der/utctime/der_encode_utctime.c index b1c6fbe6b..a48972eae 100644 --- a/src/pk/asn1/der/utctime/der_encode_utctime.c +++ b/src/pk/asn1/der/utctime/der_encode_utctime.c @@ -9,8 +9,6 @@ #ifdef LTC_DER -static const char * const baseten = "0123456789"; - #define STORE_V(y) \ out[x++] = der_ia5_char_encode(baseten[(y/10) % 10]); \ out[x++] = der_ia5_char_encode(baseten[y % 10]); @@ -25,6 +23,7 @@ static const char * const baseten = "0123456789"; int der_encode_utctime(const ltc_utctime *utctime, unsigned char *out, unsigned long *outlen) { + const char * const baseten = "0123456789"; unsigned long x, tmplen; int err; @@ -68,4 +67,6 @@ int der_encode_utctime(const ltc_utctime *utctime, return CRYPT_OK; } +#undef STORE_V + #endif diff --git a/src/pk/ec25519/tweetnacl.c b/src/pk/ec25519/tweetnacl.c index 9db0dcd8e..eb35d2fff 100644 --- a/src/pk/ec25519/tweetnacl.c +++ b/src/pk/ec25519/tweetnacl.c @@ -4,6 +4,7 @@ /* automatically generated file, do not edit */ +#define add tnacl_add #define FOR(i,n) for (i = 0;i < n;++i) #define sv static void @@ -489,3 +490,7 @@ int tweetnacl_crypto_ph(u8 *out,const u8 *msg,u64 msglen) { return tweetnacl_crypto_hash(out, msg, msglen); } + +#undef add +#undef FOR +#undef sv diff --git a/src/stream/chacha/chacha_crypt.c b/src/stream/chacha/chacha_crypt.c index 27988006b..91500f815 100644 --- a/src/stream/chacha/chacha_crypt.c +++ b/src/stream/chacha/chacha_crypt.c @@ -88,4 +88,6 @@ int chacha_crypt(chacha_state *st, const unsigned char *in, unsigned long inlen, } } +#undef QUARTERROUND + #endif diff --git a/src/stream/chacha/chacha_setup.c b/src/stream/chacha/chacha_setup.c index 608960744..b04e0ea40 100644 --- a/src/stream/chacha/chacha_setup.c +++ b/src/stream/chacha/chacha_setup.c @@ -10,9 +10,6 @@ #ifdef LTC_CHACHA -static const char * const sigma = "expand 32-byte k"; -static const char * const tau = "expand 16-byte k"; - /** Initialize an ChaCha context (only the key) @param st [out] The destination of the ChaCha state @@ -23,6 +20,8 @@ static const char * const tau = "expand 16-byte k"; */ int chacha_setup(chacha_state *st, const unsigned char *key, unsigned long keylen, int rounds) { + const char * const sigma = "expand 32-byte k"; + const char * const tau = "expand 16-byte k"; const char *constants; LTC_ARGCHK(st != NULL); diff --git a/src/stream/salsa20/salsa20_crypt.c b/src/stream/salsa20/salsa20_crypt.c index 305837285..e076d69f1 100644 --- a/src/stream/salsa20/salsa20_crypt.c +++ b/src/stream/salsa20/salsa20_crypt.c @@ -83,4 +83,6 @@ int salsa20_crypt(salsa20_state *st, const unsigned char *in, unsigned long inle } } +#undef QUARTERROUND + #endif diff --git a/src/stream/salsa20/salsa20_setup.c b/src/stream/salsa20/salsa20_setup.c index 012dc95b6..e7c9f2d13 100644 --- a/src/stream/salsa20/salsa20_setup.c +++ b/src/stream/salsa20/salsa20_setup.c @@ -11,9 +11,6 @@ #ifdef LTC_SALSA20 -static const char * const sigma = "expand 32-byte k"; -static const char * const tau = "expand 16-byte k"; - /** Initialize an Salsa20 context (only the key) @param st [out] The destination of the Salsa20 state @@ -24,6 +21,8 @@ static const char * const tau = "expand 16-byte k"; */ int salsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long keylen, int rounds) { + const char * const sigma = "expand 32-byte k"; + const char * const tau = "expand 16-byte k"; const char *constants; LTC_ARGCHK(st != NULL); diff --git a/src/stream/salsa20/xsalsa20_setup.c b/src/stream/salsa20/xsalsa20_setup.c index 6bfac0938..c764a896c 100644 --- a/src/stream/salsa20/xsalsa20_setup.c +++ b/src/stream/salsa20/xsalsa20_setup.c @@ -12,8 +12,6 @@ #ifdef LTC_XSALSA20 -static const char * const constants = "expand 32-byte k"; - #define QUARTERROUND(a,b,c,d) \ x[b] ^= (ROL((x[a] + x[d]), 7)); \ x[c] ^= (ROL((x[b] + x[a]), 9)); \ @@ -55,6 +53,7 @@ int xsalsa20_setup(salsa20_state *st, const unsigned char *key, unsigned long ke const unsigned char *nonce, unsigned long noncelen, int rounds) { + const char * const constants = "expand 32-byte k"; const int sti[] = {0, 5, 10, 15, 6, 7, 8, 9}; /* indices used to build subkey fm x */ ulong32 x[64]; /* input to & output fm doubleround */ unsigned char subkey[32]; diff --git a/src/stream/sober128/sober128_stream.c b/src/stream/sober128/sober128_stream.c index 859cafd31..fcabd0616 100644 --- a/src/stream/sober128/sober128_stream.c +++ b/src/stream/sober128/sober128_stream.c @@ -330,4 +330,16 @@ int sober128_stream_done(sober128_state *st) return CRYPT_OK; } +#undef N +#undef INITKONST +#undef KEYP +#undef FOLDP +#undef OFF +#undef STEP +#undef NLFUNC +#undef ADDKEY +#undef XORNL +#undef DROUND +#undef SROUND + #endif From 236477823a867912e9f6d8de755cfb80d431c621 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 5 Aug 2022 16:52:17 +0200 Subject: [PATCH 2/7] prefix the MPI related macros with `ltc_` Signed-off-by: Steffen Jaeckel --- demos/timing.c | 26 +- demos/tv_gen.c | 34 +-- src/headers/tomcrypt_math.h | 6 +- src/headers/tomcrypt_private.h | 125 +++++----- src/math/fp/ltc_ecc_fp_mulmod.c | 228 +++++++++--------- src/math/multi.c | 14 +- src/math/radix_to_bin.c | 10 +- src/math/rand_bn.c | 6 +- src/math/rand_prime.c | 4 +- src/misc/pbes/pbes1.c | 2 +- src/misc/pbes/pbes2.c | 4 +- src/misc/pem/pem_ssh.c | 14 +- src/misc/ssh/ssh_decode_sequence_multi.c | 4 +- src/misc/ssh/ssh_encode_sequence_multi.c | 14 +- src/pk/asn1/der/integer/der_decode_integer.c | 10 +- src/pk/asn1/der/integer/der_encode_integer.c | 32 +-- src/pk/asn1/der/integer/der_length_integer.c | 10 +- .../der/sequence/der_decode_sequence_flexi.c | 2 +- src/pk/asn1/der/sequence/der_sequence_free.c | 2 +- src/pk/asn1/pkcs8/pkcs8_get.c | 2 +- src/pk/dh/dh.c | 4 +- src/pk/dh/dh_check_pubkey.c | 12 +- src/pk/dh/dh_export_key.c | 4 +- src/pk/dh/dh_free.c | 2 +- src/pk/dh/dh_generate_key.c | 6 +- src/pk/dh/dh_import.c | 2 +- src/pk/dh/dh_import_pkcs8.c | 6 +- src/pk/dh/dh_set.c | 14 +- src/pk/dh/dh_shared_secret.c | 14 +- src/pk/dsa/dsa_decrypt_key.c | 14 +- src/pk/dsa/dsa_encrypt_key.c | 14 +- src/pk/dsa/dsa_export.c | 2 +- src/pk/dsa/dsa_free.c | 2 +- src/pk/dsa/dsa_generate_key.c | 2 +- src/pk/dsa/dsa_generate_pqg.c | 62 ++--- src/pk/dsa/dsa_import.c | 2 +- src/pk/dsa/dsa_import_pkcs8.c | 4 +- src/pk/dsa/dsa_init.c | 2 +- src/pk/dsa/dsa_set.c | 14 +- src/pk/dsa/dsa_set_pqg_dsaparam.c | 2 +- src/pk/dsa/dsa_shared_secret.c | 12 +- src/pk/dsa/dsa_sign_hash.c | 40 +-- src/pk/dsa/dsa_verify_hash.c | 28 +-- src/pk/dsa/dsa_verify_key.c | 42 ++-- src/pk/ecc/ecc_export_openssl.c | 12 +- src/pk/ecc/ecc_free.c | 2 +- src/pk/ecc/ecc_get_key.c | 4 +- src/pk/ecc/ecc_import.c | 2 +- src/pk/ecc/ecc_import_openssl.c | 8 +- src/pk/ecc/ecc_import_pkcs8.c | 14 +- src/pk/ecc/ecc_recover_key.c | 100 ++++---- src/pk/ecc/ecc_set_curve.c | 18 +- src/pk/ecc/ecc_set_curve_internal.c | 36 +-- src/pk/ecc/ecc_set_key.c | 6 +- src/pk/ecc/ecc_shared_secret.c | 4 +- src/pk/ecc/ecc_sign_hash.c | 56 ++--- src/pk/ecc/ecc_sizes.c | 8 +- src/pk/ecc/ecc_verify_hash.c | 54 ++--- src/pk/ecc/ltc_ecc_export_point.c | 12 +- src/pk/ecc/ltc_ecc_import_point.c | 30 +-- src/pk/ecc/ltc_ecc_is_point.c | 30 +-- src/pk/ecc/ltc_ecc_is_point_at_infinity.c | 16 +- src/pk/ecc/ltc_ecc_map.c | 28 +-- src/pk/ecc/ltc_ecc_mul2add.c | 30 +-- src/pk/ecc/ltc_ecc_mulmod.c | 38 +-- src/pk/ecc/ltc_ecc_mulmod_timing.c | 44 ++-- src/pk/ecc/ltc_ecc_points.c | 4 +- src/pk/ecc/ltc_ecc_projective_add_point.c | 156 ++++++------ src/pk/ecc/ltc_ecc_projective_dbl_point.c | 132 +++++----- src/pk/ecc/ltc_ecc_verify_key.c | 2 +- src/pk/pkcs1/pkcs_1_i2osp.c | 4 +- src/pk/pkcs1/pkcs_1_os2ip.c | 2 +- src/pk/rsa/rsa_decrypt_key.c | 4 +- src/pk/rsa/rsa_encrypt_key.c | 4 +- src/pk/rsa/rsa_export.c | 2 +- src/pk/rsa/rsa_exptmod.c | 56 ++--- src/pk/rsa/rsa_get_size.c | 2 +- src/pk/rsa/rsa_key.c | 10 +- src/pk/rsa/rsa_make_key.c | 60 ++--- src/pk/rsa/rsa_set.c | 16 +- src/pk/rsa/rsa_sign_hash.c | 6 +- src/pk/rsa/rsa_verify_hash.c | 4 +- tests/der_test.c | 88 +++---- tests/dh_test.c | 34 +-- tests/dsa_test.c | 10 +- tests/ecc_test.c | 98 ++++---- tests/pkcs_1_eme_test.c | 20 +- tests/pkcs_1_emsa_test.c | 20 +- tests/pkcs_1_oaep_test.c | 20 +- tests/pkcs_1_pss_test.c | 20 +- tests/rsa_test.c | 32 +-- tests/ssh_test.c | 22 +- tests/test.c | 2 +- 93 files changed, 1117 insertions(+), 1120 deletions(-) diff --git a/demos/timing.c b/demos/timing.c index 927ad4385..b5bd78c7a 100644 --- a/demos/timing.c +++ b/demos/timing.c @@ -528,12 +528,12 @@ static void time_mult(void) if (ltc_mp.name == NULL) return; fprintf(stderr, "Timing Multiplying:\n"); - mp_init_multi(&a,&b,&c,NULL); - for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) { - mp_rand(a, x); - mp_rand(b, x); + ltc_mp_init_multi(&a,&b,&c,NULL); + for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) { + ltc_mp_rand(a, x); + ltc_mp_rand(b, x); -#define DO1 mp_mul(a, b, c); +#define DO1 ltc_mp_mul(a, b, c); #define DO2 DO1; DO1; t2 = -1; @@ -544,9 +544,9 @@ static void time_mult(void) t1 = (t_read() - t1)>>1; if (t1 < t2) t2 = t1; } - fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2); + fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*LTC_MP_DIGIT_BIT, t2); } - mp_clear_multi(a,b,c,NULL); + ltc_mp_deinit_multi(a,b,c,NULL); #undef DO1 #undef DO2 @@ -561,11 +561,11 @@ static void time_sqr(void) if (ltc_mp.name == NULL) return; fprintf(stderr, "Timing Squaring:\n"); - mp_init_multi(&a,&b,NULL); - for (x = 128/MP_DIGIT_BIT; x <= (unsigned long)1536/MP_DIGIT_BIT; x += 128/MP_DIGIT_BIT) { - mp_rand(a, x); + ltc_mp_init_multi(&a,&b,NULL); + for (x = 128/LTC_MP_DIGIT_BIT; x <= (unsigned long)1536/LTC_MP_DIGIT_BIT; x += 128/LTC_MP_DIGIT_BIT) { + ltc_mp_rand(a, x); -#define DO1 mp_sqr(a, b); +#define DO1 ltc_mp_sqr(a, b); #define DO2 DO1; DO1; t2 = -1; @@ -576,9 +576,9 @@ static void time_sqr(void) t1 = (t_read() - t1)>>1; if (t1 < t2) t2 = t1; } - fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*MP_DIGIT_BIT, t2); + fprintf(stderr, "%4lu bits: %9"PRI64"u cycles\n", x*LTC_MP_DIGIT_BIT, t2); } - mp_clear_multi(a,b,NULL); + ltc_mp_deinit_multi(a,b,NULL); #undef DO1 #undef DO2 diff --git a/demos/tv_gen.c b/demos/tv_gen.c index 46ed287fe..3710fa635 100644 --- a/demos/tv_gen.c +++ b/demos/tv_gen.c @@ -670,31 +670,31 @@ static void ecc_gen(void) fprintf(out, "ecc vectors. These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are triplets\n\n"); G = ltc_ecc_new_point(); R = ltc_ecc_new_point(); - mp_init(&k); - mp_init(&order); - mp_init(&modulus); - mp_init(&a); + ltc_mp_init(&k); + ltc_mp_init(&order); + ltc_mp_init(&modulus); + ltc_mp_init(&a); for (x = 0; ltc_ecc_curves[x].prime != NULL; x++) { fprintf(out, "%s\n", ltc_ecc_curves[x].OID); - mp_set(k, 1); + ltc_mp_set(k, 1); - mp_read_radix(order, ltc_ecc_curves[x].order, 16); - mp_read_radix(modulus, ltc_ecc_curves[x].prime, 16); - mp_read_radix(a, ltc_ecc_curves[x].A, 16); - mp_read_radix(G->x, ltc_ecc_curves[x].Gx, 16); - mp_read_radix(G->y, ltc_ecc_curves[x].Gy, 16); - mp_set(G->z, 1); + ltc_mp_read_radix(order, ltc_ecc_curves[x].order, 16); + ltc_mp_read_radix(modulus, ltc_ecc_curves[x].prime, 16); + ltc_mp_read_radix(a, ltc_ecc_curves[x].A, 16); + ltc_mp_read_radix(G->x, ltc_ecc_curves[x].Gx, 16); + ltc_mp_read_radix(G->y, ltc_ecc_curves[x].Gy, 16); + ltc_mp_set(G->z, 1); - while (mp_cmp(k, order) == LTC_MP_LT) { + while (ltc_mp_cmp(k, order) == LTC_MP_LT) { ltc_mp.ecc_ptmul(k, G, R, a, modulus, 1); - mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str); - mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str); - mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str); - mp_mul_d(k, 3, k); + ltc_mp_tohex(k, (char*)str); fprintf(out, "%s, ", (char*)str); + ltc_mp_tohex(R->x, (char*)str); fprintf(out, "%s, ", (char*)str); + ltc_mp_tohex(R->y, (char*)str); fprintf(out, "%s\n", (char*)str); + ltc_mp_mul_d(k, 3, k); } } - mp_clear_multi(k, order, modulus, a, LTC_NULL); + ltc_mp_deinit_multi(k, order, modulus, a, LTC_NULL); ltc_ecc_del_point(G); ltc_ecc_del_point(R); fclose(out); diff --git a/src/headers/tomcrypt_math.h b/src/headers/tomcrypt_math.h index e5fc56900..3ce45acf2 100644 --- a/src/headers/tomcrypt_math.h +++ b/src/headers/tomcrypt_math.h @@ -502,9 +502,9 @@ typedef struct { extern ltc_math_descriptor ltc_mp; -int ltc_init_multi(void **a, ...) LTC_NULL_TERMINATED; -void ltc_deinit_multi(void *a, ...) LTC_NULL_TERMINATED; -void ltc_cleanup_multi(void **a, ...) LTC_NULL_TERMINATED; +int ltc_mp_init_multi(void **a, ...) LTC_NULL_TERMINATED; +void ltc_mp_deinit_multi(void *a, ...) LTC_NULL_TERMINATED; +void ltc_mp_cleanup_multi(void **a, ...) LTC_NULL_TERMINATED; #ifdef LTM_DESC extern const ltc_math_descriptor ltm_desc; diff --git a/src/headers/tomcrypt_private.h b/src/headers/tomcrypt_private.h index 1751d3b7a..0452117a8 100644 --- a/src/headers/tomcrypt_private.h +++ b/src/headers/tomcrypt_private.h @@ -175,72 +175,69 @@ int omac_vprocess(omac_state *omac, const unsigned char *in, unsigned long inle #if !defined(DESC_DEF_ONLY) -#define MP_DIGIT_BIT ltc_mp.bits_per_digit +#define LTC_MP_DIGIT_BIT ltc_mp.bits_per_digit /* some handy macros */ -#define mp_init(a) ltc_mp.init(a) -#define mp_init_multi ltc_init_multi -#define mp_clear(a) ltc_mp.deinit(a) -#define mp_clear_multi ltc_deinit_multi -#define mp_cleanup_multi ltc_cleanup_multi -#define mp_init_copy(a, b) ltc_mp.init_copy(a, b) - -#define mp_neg(a, b) ltc_mp.neg(a, b) -#define mp_copy(a, b) ltc_mp.copy(a, b) - -#define mp_set(a, b) ltc_mp.set_int(a, b) -#define mp_set_int(a, b) ltc_mp.set_int(a, b) -#define mp_get_int(a) ltc_mp.get_int(a) -#define mp_get_digit(a, n) ltc_mp.get_digit(a, n) -#define mp_get_digit_count(a) ltc_mp.get_digit_count(a) -#define mp_cmp(a, b) ltc_mp.compare(a, b) -#define mp_cmp_d(a, b) ltc_mp.compare_d(a, b) -#define mp_count_bits(a) ltc_mp.count_bits(a) -#define mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a) -#define mp_2expt(a, b) ltc_mp.twoexpt(a, b) - -#define mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c) -#define mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c) -#define mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a) -#define mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b) -#define mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c) - -#define mp_add(a, b, c) ltc_mp.add(a, b, c) -#define mp_add_d(a, b, c) ltc_mp.addi(a, b, c) -#define mp_sub(a, b, c) ltc_mp.sub(a, b, c) -#define mp_sub_d(a, b, c) ltc_mp.subi(a, b, c) -#define mp_mul(a, b, c) ltc_mp.mul(a, b, c) -#define mp_mul_d(a, b, c) ltc_mp.muli(a, b, c) -#define mp_sqr(a, b) ltc_mp.sqr(a, b) -#define mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c) -#define mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d) -#define mp_div_2(a, b) ltc_mp.div_2(a, b) -#define mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c) -#define mp_mod_d(a, b, c) ltc_mp.modi(a, b, c) -#define mp_gcd(a, b, c) ltc_mp.gcd(a, b, c) -#define mp_lcm(a, b, c) ltc_mp.lcm(a, b, c) - -#define mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d) -#define mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d) -#define mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d) -#define mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c) -#define mp_invmod(a, b, c) ltc_mp.invmod(a, b, c) - -#define mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b) -#define mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b) -#define mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c) -#define mp_montgomery_free(a) ltc_mp.montgomery_deinit(a) - -#define mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d) -#define mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c) - -#define mp_iszero(a) (mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO) -#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) -#define mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0) - -#define mp_tohex(a, b) mp_toradix(a, b, 16) - -#define mp_rand(a, b) ltc_mp.rand(a, b) +#define ltc_mp_init(a) ltc_mp.init(a) +#define ltc_mp_clear(a) ltc_mp.deinit(a) +#define ltc_mp_init_copy(a, b) ltc_mp.init_copy(a, b) + +#define ltc_mp_neg(a, b) ltc_mp.neg(a, b) +#define ltc_mp_copy(a, b) ltc_mp.copy(a, b) + +#define ltc_mp_set(a, b) ltc_mp.set_int(a, b) +#define ltc_mp_set_int(a, b) ltc_mp.set_int(a, b) +#define ltc_mp_get_int(a) ltc_mp.get_int(a) +#define ltc_mp_get_digit(a, n) ltc_mp.get_digit(a, n) +#define ltc_mp_get_digit_count(a) ltc_mp.get_digit_count(a) +#define ltc_mp_cmp(a, b) ltc_mp.compare(a, b) +#define ltc_mp_cmp_d(a, b) ltc_mp.compare_d(a, b) +#define ltc_mp_count_bits(a) ltc_mp.count_bits(a) +#define ltc_mp_cnt_lsb(a) ltc_mp.count_lsb_bits(a) +#define ltc_mp_2expt(a, b) ltc_mp.twoexpt(a, b) + +#define ltc_mp_read_radix(a, b, c) ltc_mp.read_radix(a, b, c) +#define ltc_mp_toradix(a, b, c) ltc_mp.write_radix(a, b, c) +#define ltc_mp_unsigned_bin_size(a) ltc_mp.unsigned_size(a) +#define ltc_mp_to_unsigned_bin(a, b) ltc_mp.unsigned_write(a, b) +#define ltc_mp_read_unsigned_bin(a, b, c) ltc_mp.unsigned_read(a, b, c) + +#define ltc_mp_add(a, b, c) ltc_mp.add(a, b, c) +#define ltc_mp_add_d(a, b, c) ltc_mp.addi(a, b, c) +#define ltc_mp_sub(a, b, c) ltc_mp.sub(a, b, c) +#define ltc_mp_sub_d(a, b, c) ltc_mp.subi(a, b, c) +#define ltc_mp_mul(a, b, c) ltc_mp.mul(a, b, c) +#define ltc_mp_mul_d(a, b, c) ltc_mp.muli(a, b, c) +#define ltc_mp_sqr(a, b) ltc_mp.sqr(a, b) +#define ltc_mp_sqrtmod_prime(a, b, c) ltc_mp.sqrtmod_prime(a, b, c) +#define ltc_mp_div(a, b, c, d) ltc_mp.mpdiv(a, b, c, d) +#define ltc_mp_div_2(a, b) ltc_mp.div_2(a, b) +#define ltc_mp_mod(a, b, c) ltc_mp.mpdiv(a, b, NULL, c) +#define ltc_mp_mod_d(a, b, c) ltc_mp.modi(a, b, c) +#define ltc_mp_gcd(a, b, c) ltc_mp.gcd(a, b, c) +#define ltc_mp_lcm(a, b, c) ltc_mp.lcm(a, b, c) + +#define ltc_mp_addmod(a, b, c, d) ltc_mp.addmod(a, b, c, d) +#define ltc_mp_submod(a, b, c, d) ltc_mp.submod(a, b, c, d) +#define ltc_mp_mulmod(a, b, c, d) ltc_mp.mulmod(a, b, c, d) +#define ltc_mp_sqrmod(a, b, c) ltc_mp.sqrmod(a, b, c) +#define ltc_mp_invmod(a, b, c) ltc_mp.invmod(a, b, c) + +#define ltc_mp_montgomery_setup(a, b) ltc_mp.montgomery_setup(a, b) +#define ltc_mp_montgomery_normalization(a, b) ltc_mp.montgomery_normalization(a, b) +#define ltc_mp_montgomery_reduce(a, b, c) ltc_mp.montgomery_reduce(a, b, c) +#define ltc_mp_montgomery_free(a) ltc_mp.montgomery_deinit(a) + +#define ltc_mp_exptmod(a,b,c,d) ltc_mp.exptmod(a,b,c,d) +#define ltc_mp_prime_is_prime(a, b, c) ltc_mp.isprime(a, b, c) + +#define ltc_mp_iszero(a) (ltc_mp_cmp_d(a, 0) == LTC_MP_EQ ? LTC_MP_YES : LTC_MP_NO) +#define ltc_mp_isodd(a) (ltc_mp_get_digit_count(a) > 0 ? (ltc_mp_get_digit(a, 0) & 1 ? LTC_MP_YES : LTC_MP_NO) : LTC_MP_NO) +#define ltc_mp_exch(a, b) do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0) + +#define ltc_mp_tohex(a, b) ltc_mp_toradix(a, b, 16) + +#define ltc_mp_rand(a, b) ltc_mp.rand(a, b) #endif diff --git a/src/math/fp/ltc_ecc_fp_mulmod.c b/src/math/fp/ltc_ecc_fp_mulmod.c index 9be2ebf16..fa9562524 100644 --- a/src/math/fp/ltc_ecc_fp_mulmod.c +++ b/src/math/fp/ltc_ecc_fp_mulmod.c @@ -587,7 +587,7 @@ static int s_find_hole(void) /* free entry z */ if (z >= 0 && fp_cache[z].g) { if (fp_cache[z].mu != NULL) { - mp_clear(fp_cache[z].mu); + ltc_mp_clear(fp_cache[z].mu); fp_cache[z].mu = NULL; } ltc_ecc_del_point(fp_cache[z].g); @@ -607,9 +607,9 @@ static int s_find_base(ecc_point *g) int x; for (x = 0; x < FP_ENTRIES; x++) { if (fp_cache[x].g != NULL && - mp_cmp(fp_cache[x].g->x, g->x) == LTC_MP_EQ && - mp_cmp(fp_cache[x].g->y, g->y) == LTC_MP_EQ && - mp_cmp(fp_cache[x].g->z, g->z) == LTC_MP_EQ) { + ltc_mp_cmp(fp_cache[x].g->x, g->x) == LTC_MP_EQ && + ltc_mp_cmp(fp_cache[x].g->y, g->y) == LTC_MP_EQ && + ltc_mp_cmp(fp_cache[x].g->z, g->z) == LTC_MP_EQ) { break; } } @@ -631,9 +631,9 @@ static int s_add_entry(int idx, ecc_point *g) } /* copy x and y */ - if ((mp_copy(g->x, fp_cache[idx].g->x) != CRYPT_OK) || - (mp_copy(g->y, fp_cache[idx].g->y) != CRYPT_OK) || - (mp_copy(g->z, fp_cache[idx].g->z) != CRYPT_OK)) { + if ((ltc_mp_copy(g->x, fp_cache[idx].g->x) != CRYPT_OK) || + (ltc_mp_copy(g->y, fp_cache[idx].g->y) != CRYPT_OK) || + (ltc_mp_copy(g->z, fp_cache[idx].g->z) != CRYPT_OK)) { ltc_ecc_del_point(fp_cache[idx].g); fp_cache[idx].g = NULL; return CRYPT_MEM; @@ -676,7 +676,7 @@ static int s_build_lut(int idx, void *a, void *modulus, void *mp, void *mu) } /* get bitlen and round up to next multiple of FP_LUT */ - bitlen = mp_unsigned_bin_size(modulus) << 3; + bitlen = ltc_mp_unsigned_bin_size(modulus) << 3; x = bitlen % FP_LUT; if (x) { bitlen += FP_LUT - x; @@ -684,20 +684,20 @@ static int s_build_lut(int idx, void *a, void *modulus, void *mp, void *mu) lut_gap = bitlen / FP_LUT; /* init the mu */ - if ((err = mp_init_copy(&fp_cache[idx].mu, mu)) != CRYPT_OK) { + if ((err = ltc_mp_init_copy(&fp_cache[idx].mu, mu)) != CRYPT_OK) { goto ERR; } /* copy base */ - if ((mp_mulmod(fp_cache[idx].g->x, mu, modulus, fp_cache[idx].LUT[1]->x) != CRYPT_OK) || - (mp_mulmod(fp_cache[idx].g->y, mu, modulus, fp_cache[idx].LUT[1]->y) != CRYPT_OK) || - (mp_mulmod(fp_cache[idx].g->z, mu, modulus, fp_cache[idx].LUT[1]->z) != CRYPT_OK)) { goto ERR; } + if ((ltc_mp_mulmod(fp_cache[idx].g->x, mu, modulus, fp_cache[idx].LUT[1]->x) != CRYPT_OK) || + (ltc_mp_mulmod(fp_cache[idx].g->y, mu, modulus, fp_cache[idx].LUT[1]->y) != CRYPT_OK) || + (ltc_mp_mulmod(fp_cache[idx].g->z, mu, modulus, fp_cache[idx].LUT[1]->z) != CRYPT_OK)) { goto ERR; } /* make all single bit entries */ for (x = 1; x < FP_LUT; x++) { - if ((mp_copy(fp_cache[idx].LUT[1<<(x-1)]->x, fp_cache[idx].LUT[1<x) != CRYPT_OK) || - (mp_copy(fp_cache[idx].LUT[1<<(x-1)]->y, fp_cache[idx].LUT[1<y) != CRYPT_OK) || - (mp_copy(fp_cache[idx].LUT[1<<(x-1)]->z, fp_cache[idx].LUT[1<z) != CRYPT_OK)) { goto ERR; } + if ((ltc_mp_copy(fp_cache[idx].LUT[1<<(x-1)]->x, fp_cache[idx].LUT[1<x) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx].LUT[1<<(x-1)]->y, fp_cache[idx].LUT[1<y) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx].LUT[1<<(x-1)]->z, fp_cache[idx].LUT[1<z) != CRYPT_OK)) { goto ERR; } /* now double it bitlen/FP_LUT times */ for (y = 0; y < lut_gap; y++) { @@ -721,31 +721,31 @@ static int s_build_lut(int idx, void *a, void *modulus, void *mp, void *mu) } /* now map all entries back to affine space to make point addition faster */ - if ((err = mp_init(&tmp)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_init(&tmp)) != CRYPT_OK) { goto ERR; } for (x = 1; x < (1UL<z, modulus, mp)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_montgomery_reduce(fp_cache[idx].LUT[x]->z, modulus, mp)) != CRYPT_OK) { goto ERR; } /* invert it */ - if ((err = mp_invmod(fp_cache[idx].LUT[x]->z, modulus, fp_cache[idx].LUT[x]->z)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_invmod(fp_cache[idx].LUT[x]->z, modulus, fp_cache[idx].LUT[x]->z)) != CRYPT_OK) { goto ERR; } /* now square it */ - if ((err = mp_sqrmod(fp_cache[idx].LUT[x]->z, modulus, tmp)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_sqrmod(fp_cache[idx].LUT[x]->z, modulus, tmp)) != CRYPT_OK) { goto ERR; } /* fix x */ - if ((err = mp_mulmod(fp_cache[idx].LUT[x]->x, tmp, modulus, fp_cache[idx].LUT[x]->x)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_mulmod(fp_cache[idx].LUT[x]->x, tmp, modulus, fp_cache[idx].LUT[x]->x)) != CRYPT_OK) { goto ERR; } /* get 1/z^3 */ - if ((err = mp_mulmod(tmp, fp_cache[idx].LUT[x]->z, modulus, tmp)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_mulmod(tmp, fp_cache[idx].LUT[x]->z, modulus, tmp)) != CRYPT_OK) { goto ERR; } /* fix y */ - if ((err = mp_mulmod(fp_cache[idx].LUT[x]->y, tmp, modulus, fp_cache[idx].LUT[x]->y)) != CRYPT_OK) { goto ERR; } + if ((err = ltc_mp_mulmod(fp_cache[idx].LUT[x]->y, tmp, modulus, fp_cache[idx].LUT[x]->y)) != CRYPT_OK) { goto ERR; } /* free z */ - mp_clear(fp_cache[idx].LUT[x]->z); + ltc_mp_clear(fp_cache[idx].LUT[x]->z); fp_cache[idx].LUT[x]->z = NULL; } - mp_clear(tmp); + ltc_mp_clear(tmp); return CRYPT_OK; ERR: @@ -759,11 +759,11 @@ static int s_build_lut(int idx, void *a, void *modulus, void *mp, void *mu) fp_cache[idx].g = NULL; fp_cache[idx].lru_count = 0; if (fp_cache[idx].mu != NULL) { - mp_clear(fp_cache[idx].mu); + ltc_mp_clear(fp_cache[idx].mu); fp_cache[idx].mu = NULL; } if (tmp != NULL) { - mp_clear(tmp); + ltc_mp_clear(tmp); } return err; } @@ -777,9 +777,9 @@ static int s_accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus void *tk, *order; /* if it's smaller than modulus we fine */ - if (mp_unsigned_bin_size(k) > mp_unsigned_bin_size(modulus)) { + if (ltc_mp_unsigned_bin_size(k) > ltc_mp_unsigned_bin_size(modulus)) { /* find order */ - y = mp_unsigned_bin_size(modulus); + y = ltc_mp_unsigned_bin_size(modulus); for (x = 0; ltc_ecc_sets[x].size; x++) { if (y <= (unsigned)ltc_ecc_sets[x].size) break; } @@ -787,35 +787,35 @@ static int s_accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus /* back off if we are on the 521 bit curve */ if (y == 66) --x; - if ((err = mp_init(&order)) != CRYPT_OK) { + if ((err = ltc_mp_init(&order)) != CRYPT_OK) { return err; } - if ((err = mp_read_radix(order, ltc_ecc_sets[x].order, 16)) != CRYPT_OK) { - mp_clear(&order); + if ((err = ltc_mp_read_radix(order, ltc_ecc_sets[x].order, 16)) != CRYPT_OK) { + ltc_mp_clear(&order); return err; } /* k must be less than modulus */ - if (mp_cmp(k, order) != LTC_MP_LT) { - if ((err = mp_init(&tk)) != CRYPT_OK) { - mp_clear(order); + if (ltc_mp_cmp(k, order) != LTC_MP_LT) { + if ((err = ltc_mp_init(&tk)) != CRYPT_OK) { + ltc_mp_clear(order); return err; } - if ((err = mp_mod(k, order, tk)) != CRYPT_OK) { - mp_clear(tk); - mp_clear(order); + if ((err = ltc_mp_mod(k, order, tk)) != CRYPT_OK) { + ltc_mp_clear(tk); + ltc_mp_clear(order); return err; } } else { tk = k; } - mp_clear(order); + ltc_mp_clear(order); } else { tk = k; } /* get bitlen and round up to next multiple of FP_LUT */ - bitlen = mp_unsigned_bin_size(modulus) << 3; + bitlen = ltc_mp_unsigned_bin_size(modulus) << 3; x = bitlen % FP_LUT; if (x) { bitlen += FP_LUT - x; @@ -823,27 +823,27 @@ static int s_accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus lut_gap = bitlen / FP_LUT; /* get the k value */ - if (mp_unsigned_bin_size(tk) > (sizeof(kb) - 2)) { + if (ltc_mp_unsigned_bin_size(tk) > (sizeof(kb) - 2)) { if (tk != k) { - mp_clear(tk); + ltc_mp_clear(tk); } return CRYPT_BUFFER_OVERFLOW; } /* store k */ zeromem(kb, sizeof(kb)); - if ((err = mp_to_unsigned_bin(tk, kb)) != CRYPT_OK) { + if ((err = ltc_mp_to_unsigned_bin(tk, kb)) != CRYPT_OK) { if (tk != k) { - mp_clear(tk); + ltc_mp_clear(tk); } return err; } /* let's reverse kb so it's little endian */ x = 0; - y = mp_unsigned_bin_size(tk) - 1; + y = ltc_mp_unsigned_bin_size(tk) - 1; if (tk != k) { - mp_clear(tk); + ltc_mp_clear(tk); } while ((unsigned)x < y) { z = kb[x]; kb[x] = kb[y]; kb[y] = z; @@ -873,9 +873,9 @@ static int s_accel_fp_mul(int idx, void *k, ecc_point *R, void *a, void *modulus return err; } } else if (z) { - if ((mp_copy(fp_cache[idx].LUT[z]->x, R->x) != CRYPT_OK) || - (mp_copy(fp_cache[idx].LUT[z]->y, R->y) != CRYPT_OK) || - (mp_copy(fp_cache[idx].mu, R->z) != CRYPT_OK)) { return CRYPT_MEM; } + if ((ltc_mp_copy(fp_cache[idx].LUT[z]->x, R->x) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx].LUT[z]->y, R->y) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx].mu, R->z) != CRYPT_OK)) { return CRYPT_MEM; } first = 0; } } @@ -902,9 +902,9 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, void *tka, *tkb, *order; /* if it's smaller than modulus we fine */ - if (mp_unsigned_bin_size(kA) > mp_unsigned_bin_size(modulus)) { + if (ltc_mp_unsigned_bin_size(kA) > ltc_mp_unsigned_bin_size(modulus)) { /* find order */ - y = mp_unsigned_bin_size(modulus); + y = ltc_mp_unsigned_bin_size(modulus); for (x = 0; ltc_ecc_sets[x].size; x++) { if (y <= (unsigned)ltc_ecc_sets[x].size) break; } @@ -912,37 +912,37 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, /* back off if we are on the 521 bit curve */ if (y == 66) --x; - if ((err = mp_init(&order)) != CRYPT_OK) { + if ((err = ltc_mp_init(&order)) != CRYPT_OK) { return err; } - if ((err = mp_read_radix(order, ltc_ecc_sets[x].order, 16)) != CRYPT_OK) { - mp_clear(&order); + if ((err = ltc_mp_read_radix(order, ltc_ecc_sets[x].order, 16)) != CRYPT_OK) { + ltc_mp_clear(&order); return err; } /* kA must be less than modulus */ - if (mp_cmp(kA, order) != LTC_MP_LT) { - if ((err = mp_init(&tka)) != CRYPT_OK) { - mp_clear(order); + if (ltc_mp_cmp(kA, order) != LTC_MP_LT) { + if ((err = ltc_mp_init(&tka)) != CRYPT_OK) { + ltc_mp_clear(order); return err; } - if ((err = mp_mod(kA, order, tka)) != CRYPT_OK) { - mp_clear(tka); - mp_clear(order); + if ((err = ltc_mp_mod(kA, order, tka)) != CRYPT_OK) { + ltc_mp_clear(tka); + ltc_mp_clear(order); return err; } } else { tka = kA; } - mp_clear(order); + ltc_mp_clear(order); } else { tka = kA; } /* if it's smaller than modulus we fine */ - if (mp_unsigned_bin_size(kB) > mp_unsigned_bin_size(modulus)) { + if (ltc_mp_unsigned_bin_size(kB) > ltc_mp_unsigned_bin_size(modulus)) { /* find order */ - y = mp_unsigned_bin_size(modulus); + y = ltc_mp_unsigned_bin_size(modulus); for (x = 0; ltc_ecc_sets[x].size; x++) { if (y <= (unsigned)ltc_ecc_sets[x].size) break; } @@ -950,35 +950,35 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, /* back off if we are on the 521 bit curve */ if (y == 66) --x; - if ((err = mp_init(&order)) != CRYPT_OK) { + if ((err = ltc_mp_init(&order)) != CRYPT_OK) { return err; } - if ((err = mp_read_radix(order, ltc_ecc_sets[x].order, 16)) != CRYPT_OK) { - mp_clear(&order); + if ((err = ltc_mp_read_radix(order, ltc_ecc_sets[x].order, 16)) != CRYPT_OK) { + ltc_mp_clear(&order); return err; } /* kB must be less than modulus */ - if (mp_cmp(kB, order) != LTC_MP_LT) { - if ((err = mp_init(&tkb)) != CRYPT_OK) { - mp_clear(order); + if (ltc_mp_cmp(kB, order) != LTC_MP_LT) { + if ((err = ltc_mp_init(&tkb)) != CRYPT_OK) { + ltc_mp_clear(order); return err; } - if ((err = mp_mod(kB, order, tkb)) != CRYPT_OK) { - mp_clear(tkb); - mp_clear(order); + if ((err = ltc_mp_mod(kB, order, tkb)) != CRYPT_OK) { + ltc_mp_clear(tkb); + ltc_mp_clear(order); return err; } } else { tkb = kB; } - mp_clear(order); + ltc_mp_clear(order); } else { tkb = kB; } /* get bitlen and round up to next multiple of FP_LUT */ - bitlen = mp_unsigned_bin_size(modulus) << 3; + bitlen = ltc_mp_unsigned_bin_size(modulus) << 3; x = bitlen % FP_LUT; if (x) { bitlen += FP_LUT - x; @@ -986,33 +986,33 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, lut_gap = bitlen / FP_LUT; /* get the k value */ - if ((mp_unsigned_bin_size(tka) > (sizeof(kb[0]) - 2)) || (mp_unsigned_bin_size(tkb) > (sizeof(kb[0]) - 2)) ) { + if ((ltc_mp_unsigned_bin_size(tka) > (sizeof(kb[0]) - 2)) || (ltc_mp_unsigned_bin_size(tkb) > (sizeof(kb[0]) - 2)) ) { if (tka != kA) { - mp_clear(tka); + ltc_mp_clear(tka); } if (tkb != kB) { - mp_clear(tkb); + ltc_mp_clear(tkb); } return CRYPT_BUFFER_OVERFLOW; } /* store k */ zeromem(kb, sizeof(kb)); - if ((err = mp_to_unsigned_bin(tka, kb[0])) != CRYPT_OK) { + if ((err = ltc_mp_to_unsigned_bin(tka, kb[0])) != CRYPT_OK) { if (tka != kA) { - mp_clear(tka); + ltc_mp_clear(tka); } if (tkb != kB) { - mp_clear(tkb); + ltc_mp_clear(tkb); } return err; } /* let's reverse kb so it's little endian */ x = 0; - y = mp_unsigned_bin_size(tka) - 1; + y = ltc_mp_unsigned_bin_size(tka) - 1; if (tka != kA) { - mp_clear(tka); + ltc_mp_clear(tka); } while ((unsigned)x < y) { z = kb[0][x]; kb[0][x] = kb[0][y]; kb[0][y] = z; @@ -1020,17 +1020,17 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, } /* store b */ - if ((err = mp_to_unsigned_bin(tkb, kb[1])) != CRYPT_OK) { + if ((err = ltc_mp_to_unsigned_bin(tkb, kb[1])) != CRYPT_OK) { if (tkb != kB) { - mp_clear(tkb); + ltc_mp_clear(tkb); } return err; } x = 0; - y = mp_unsigned_bin_size(tkb) - 1; + y = ltc_mp_unsigned_bin_size(tkb) - 1; if (tkb != kB) { - mp_clear(tkb); + ltc_mp_clear(tkb); } while ((unsigned)x < y) { z = kb[1][x]; kb[1][x] = kb[1][y]; kb[1][y] = z; @@ -1069,9 +1069,9 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, } } else { if (zA) { - if ((mp_copy(fp_cache[idx1].LUT[zA]->x, R->x) != CRYPT_OK) || - (mp_copy(fp_cache[idx1].LUT[zA]->y, R->y) != CRYPT_OK) || - (mp_copy(fp_cache[idx1].mu, R->z) != CRYPT_OK)) { return CRYPT_MEM; } + if ((ltc_mp_copy(fp_cache[idx1].LUT[zA]->x, R->x) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx1].LUT[zA]->y, R->y) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx1].mu, R->z) != CRYPT_OK)) { return CRYPT_MEM; } first = 0; } if (zB && first == 0) { @@ -1081,9 +1081,9 @@ static int ss_accel_fp_mul2add(int idx1, int idx2, } } } else if (zB && first == 1) { - if ((mp_copy(fp_cache[idx2].LUT[zB]->x, R->x) != CRYPT_OK) || - (mp_copy(fp_cache[idx2].LUT[zB]->y, R->y) != CRYPT_OK) || - (mp_copy(fp_cache[idx2].mu, R->z) != CRYPT_OK)) { return CRYPT_MEM; } + if ((ltc_mp_copy(fp_cache[idx2].LUT[zB]->x, R->x) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx2].LUT[zB]->y, R->y) != CRYPT_OK) || + (ltc_mp_copy(fp_cache[idx2].mu, R->z) != CRYPT_OK)) { return CRYPT_MEM; } first = 0; } } @@ -1151,13 +1151,13 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA, /* if it's 2 build the LUT, if it's higher just use the LUT */ if (idx1 >= 0 && fp_cache[idx1].lru_count == 2) { /* compute mp */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } /* compute mu */ - if ((err = mp_init(&mu)) != CRYPT_OK) { + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto LBL_ERR; } @@ -1171,13 +1171,13 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA, if (idx2 >= 0 && fp_cache[idx2].lru_count == 2) { if (mp == NULL) { /* compute mp */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } /* compute mu */ - if ((err = mp_init(&mu)) != CRYPT_OK) { + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto LBL_ERR; } } @@ -1192,7 +1192,7 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA, if (idx1 >=0 && idx2 >= 0 && fp_cache[idx1].lru_count >= 2 && fp_cache[idx2].lru_count >= 2) { if (mp == NULL) { /* compute mp */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } } err = ss_accel_fp_mul2add(idx1, idx2, kA, kB, C, a, modulus, mp); } else { @@ -1201,10 +1201,10 @@ int ltc_ecc_fp_mul2add(ecc_point *A, void *kA, LBL_ERR: LTC_MUTEX_UNLOCK(<c_ecc_fp_lock); if (mp != NULL) { - mp_montgomery_free(mp); + ltc_mp_montgomery_free(mp); } if (mu != NULL) { - mp_clear(mu); + ltc_mp_clear(mu); } return err; } @@ -1250,13 +1250,13 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu /* if it's 2 build the LUT, if it's higher just use the LUT */ if (idx >= 0 && fp_cache[idx].lru_count == 2) { /* compute mp */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } /* compute mu */ - if ((err = mp_init(&mu)) != CRYPT_OK) { + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto LBL_ERR; } @@ -1269,7 +1269,7 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu if (idx >= 0 && fp_cache[idx].lru_count >= 2) { if (mp == NULL) { /* compute mp */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } } err = s_accel_fp_mul(idx, k, R, a, modulus, mp, map); } else { @@ -1278,10 +1278,10 @@ int ltc_ecc_fp_mulmod(void *k, ecc_point *G, ecc_point *R, void *a, void *modulu LBL_ERR: LTC_MUTEX_UNLOCK(<c_ecc_fp_lock); if (mp != NULL) { - mp_montgomery_free(mp); + ltc_mp_montgomery_free(mp); } if (mu != NULL) { - mp_clear(mu); + ltc_mp_clear(mu); } return err; } @@ -1299,7 +1299,7 @@ static void s_ltc_ecc_fp_free_cache(void) ltc_ecc_del_point(fp_cache[x].g); fp_cache[x].g = NULL; if (fp_cache[x].mu != NULL) { - mp_clear(fp_cache[x].mu); + ltc_mp_clear(fp_cache[x].mu); fp_cache[x].mu = NULL; } fp_cache[x].lru_count = 0; @@ -1347,15 +1347,15 @@ ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock) goto LBL_ERR; } /* compute mp */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto LBL_ERR; } /* compute mu */ - if ((err = mp_init(&mu)) != CRYPT_OK) { + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto LBL_ERR; } @@ -1368,10 +1368,10 @@ ltc_ecc_fp_add_point(ecc_point *g, void *modulus, int lock) LBL_ERR: LTC_MUTEX_UNLOCK(<c_ecc_fp_lock); if (mp != NULL) { - mp_montgomery_free(mp); + ltc_mp_montgomery_free(mp); } if (mu != NULL) { - mp_clear(mu); + ltc_mp_clear(mu); } return err; } @@ -1546,14 +1546,14 @@ int ltc_ecc_fp_restore_state(unsigned char *in, unsigned long inlen) goto ERR_OUT; } fp_cache[i].LUT[x] = p; - if ((err = mp_init_multi(&p->x, &p->y, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&p->x, &p->y, LTC_NULL)) != CRYPT_OK) { goto ERR_OUT; } p->z = NULL; LTC_SET_ASN1(asn1_list, j++, LTC_ASN1_INTEGER, p->x, 1); LTC_SET_ASN1(asn1_list, j++, LTC_ASN1_INTEGER, p->y, 1); } - if((err = mp_init(&fp_cache[i].mu)) != CRYPT_OK) { + if((err = ltc_mp_init(&fp_cache[i].mu)) != CRYPT_OK) { goto ERR_OUT; } LTC_SET_ASN1(asn1_list, j++, LTC_ASN1_INTEGER, fp_cache[i].mu, 1); diff --git a/src/math/multi.c b/src/math/multi.c index 579a97977..1b7058fad 100644 --- a/src/math/multi.c +++ b/src/math/multi.c @@ -3,7 +3,7 @@ #include "tomcrypt_private.h" #ifdef LTC_MPI -int ltc_init_multi(void **a, ...) +int ltc_mp_init_multi(void **a, ...) { void **cur = a; int np = 0; @@ -11,14 +11,14 @@ int ltc_init_multi(void **a, ...) va_start(args, a); while (cur != NULL) { - if (mp_init(cur) != CRYPT_OK) { + if (ltc_mp_init(cur) != CRYPT_OK) { /* failed */ va_list clean_list; va_start(clean_list, a); cur = a; while (np--) { - mp_clear(*cur); + ltc_mp_clear(*cur); cur = va_arg(clean_list, void**); } va_end(clean_list); @@ -32,20 +32,20 @@ int ltc_init_multi(void **a, ...) return CRYPT_OK; } -void ltc_deinit_multi(void *a, ...) +void ltc_mp_deinit_multi(void *a, ...) { void *cur = a; va_list args; va_start(args, a); while (cur != NULL) { - mp_clear(cur); + ltc_mp_clear(cur); cur = va_arg(args, void *); } va_end(args); } -void ltc_cleanup_multi(void **a, ...) +void ltc_mp_cleanup_multi(void **a, ...) { void **cur = a; va_list args; @@ -53,7 +53,7 @@ void ltc_cleanup_multi(void **a, ...) va_start(args, a); while (cur != NULL) { if (*cur != NULL) { - mp_clear(*cur); + ltc_mp_clear(*cur); *cur = NULL; } cur = va_arg(args, void**); diff --git a/src/math/radix_to_bin.c b/src/math/radix_to_bin.c index 5c17f0d2d..9a7d02d9b 100644 --- a/src/math/radix_to_bin.c +++ b/src/math/radix_to_bin.c @@ -34,19 +34,19 @@ int radix_to_bin(const void *in, int radix, void *out, unsigned long *len) LTC_ARGCHK(in != NULL); LTC_ARGCHK(len != NULL); - if ((err = mp_init(&mpi)) != CRYPT_OK) return err; - if ((err = mp_read_radix(mpi, in, radix)) != CRYPT_OK) goto LBL_ERR; + if ((err = ltc_mp_init(&mpi)) != CRYPT_OK) return err; + if ((err = ltc_mp_read_radix(mpi, in, radix)) != CRYPT_OK) goto LBL_ERR; - if ((l = mp_unsigned_bin_size(mpi)) > *len) { + if ((l = ltc_mp_unsigned_bin_size(mpi)) > *len) { *len = l; err = CRYPT_BUFFER_OVERFLOW; goto LBL_ERR; } *len = l; - if ((err = mp_to_unsigned_bin(mpi, out)) != CRYPT_OK) goto LBL_ERR; + if ((err = ltc_mp_to_unsigned_bin(mpi, out)) != CRYPT_OK) goto LBL_ERR; LBL_ERR: - mp_clear(mpi); + ltc_mp_clear(mpi); return err; } diff --git a/src/math/rand_bn.c b/src/math/rand_bn.c index 03a81b7e2..4f9f4f2fe 100644 --- a/src/math/rand_bn.c +++ b/src/math/rand_bn.c @@ -32,7 +32,7 @@ int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng) /* mask bits */ buf[0] &= mask; /* load value */ - if ((res = mp_read_unsigned_bin(N, buf, bytes)) != CRYPT_OK) goto cleanup; + if ((res = ltc_mp_read_unsigned_bin(N, buf, bytes)) != CRYPT_OK) goto cleanup; res = CRYPT_OK; @@ -54,11 +54,11 @@ int rand_bn_upto(void *N, void *limit, prng_state *prng, int wprng) LTC_ARGCHK(N != NULL); LTC_ARGCHK(limit != NULL); - bits = mp_count_bits(limit); + bits = ltc_mp_count_bits(limit); do { res = rand_bn_bits(N, bits, prng, wprng); if (res != CRYPT_OK) return res; - } while (mp_cmp_d(N, 0) != LTC_MP_GT || mp_cmp(N, limit) != LTC_MP_LT); + } while (ltc_mp_cmp_d(N, 0) != LTC_MP_GT || ltc_mp_cmp(N, limit) != LTC_MP_LT); return CRYPT_OK; } diff --git a/src/math/rand_prime.c b/src/math/rand_prime.c index 498912cb3..0cd6c0dce 100644 --- a/src/math/rand_prime.c +++ b/src/math/rand_prime.c @@ -54,13 +54,13 @@ int rand_prime(void *N, long len, prng_state *prng, int wprng) buf[len-1] |= 0x01 | ((type & USE_BBS) ? 0x02 : 0x00); /* load value */ - if ((err = mp_read_unsigned_bin(N, buf, len)) != CRYPT_OK) { + if ((err = ltc_mp_read_unsigned_bin(N, buf, len)) != CRYPT_OK) { XFREE(buf); return err; } /* test */ - if ((err = mp_prime_is_prime(N, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { + if ((err = ltc_mp_prime_is_prime(N, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { XFREE(buf); return err; } diff --git a/src/misc/pbes/pbes1.c b/src/misc/pbes/pbes1.c index 36e09070f..24c50961c 100644 --- a/src/misc/pbes/pbes1.c +++ b/src/misc/pbes/pbes1.c @@ -109,7 +109,7 @@ int pbes1_extract(const ltc_asn1_list *s, pbes_arg *res) * 33:d=1 hl=4 l= 296 prim: OCTET STRING :bytes (== encrypted data) */ res->salt = s->next->child; - res->iterations = mp_get_int(s->next->child->next->data); + res->iterations = ltc_mp_get_int(s->next->child->next->data); return CRYPT_OK; } diff --git a/src/misc/pbes/pbes2.c b/src/misc/pbes/pbes2.c index e3eeb94ac..5e672fcd8 100644 --- a/src/misc/pbes/pbes2.c +++ b/src/misc/pbes/pbes2.c @@ -134,7 +134,7 @@ int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res) liter = lkdf->next->child->next; loptseq = liter->next; res->salt = lkdf->next->child; - res->iterations = mp_get_int(liter->data); + res->iterations = ltc_mp_get_int(liter->data); /* There's an optional INTEGER keyLength after the iterations, skip that if it's there. * c.f. RFC 2898 A.2 PBKDF2 */ @@ -162,7 +162,7 @@ int pbes2_extract(const ltc_asn1_list *s, pbes_arg *res) */ if (LTC_ASN1_IS_TYPE(lenc->next->child, LTC_ASN1_INTEGER) && LTC_ASN1_IS_TYPE(lenc->next->child->next, LTC_ASN1_OCTET_STRING)) { - klen = mp_get_int(lenc->next->child->data); + klen = ltc_mp_get_int(lenc->next->child->data); res->iv = lenc->next->child->next; /* * Effective Key Bits Encoding diff --git a/src/misc/pem/pem_ssh.c b/src/misc/pem/pem_ssh.c index 2ec386bdc..4630d7086 100644 --- a/src/misc/pem/pem_ssh.c +++ b/src/misc/pem/pem_ssh.c @@ -198,7 +198,7 @@ static int s_ssh_decode_dsa(const unsigned char *in, unsigned long *inlen, ltc_p LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { goto cleanup; } - key->u.dsa.qord = mp_unsigned_bin_size(key->u.dsa.q); + key->u.dsa.qord = ltc_mp_unsigned_bin_size(key->u.dsa.q); if ((err = dsa_int_validate_pqg(&key->u.dsa, &stat)) != CRYPT_OK) { goto cleanup; } @@ -281,7 +281,7 @@ static int s_ssh_decode_rsa(const unsigned char *in, unsigned long *inlen, ltc_p goto cleanup; } - if ((err = mp_init_multi(&tmp1, &tmp2, NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&tmp1, &tmp2, LTC_NULL)) != CRYPT_OK) { goto cleanup; } @@ -294,16 +294,16 @@ static int s_ssh_decode_rsa(const unsigned char *in, unsigned long *inlen, ltc_p goto cleanup_tmps; } - if ((err = mp_sub_d(key->u.rsa.p, 1, tmp1)) != CRYPT_OK) { goto cleanup_tmps; } /* tmp1 = q-1 */ - if ((err = mp_sub_d(key->u.rsa.q, 1, tmp2)) != CRYPT_OK) { goto cleanup_tmps; } /* tmp2 = p-1 */ - if ((err = mp_mod(key->u.rsa.d, tmp1, key->u.rsa.dP)) != CRYPT_OK) { goto cleanup_tmps; } /* dP = d mod p-1 */ - if ((err = mp_mod(key->u.rsa.d, tmp2, key->u.rsa.dQ)) != CRYPT_OK) { goto cleanup_tmps; } /* dQ = d mod q-1 */ + if ((err = ltc_mp_sub_d(key->u.rsa.p, 1, tmp1)) != CRYPT_OK) { goto cleanup_tmps; } /* tmp1 = q-1 */ + if ((err = ltc_mp_sub_d(key->u.rsa.q, 1, tmp2)) != CRYPT_OK) { goto cleanup_tmps; } /* tmp2 = p-1 */ + if ((err = ltc_mp_mod(key->u.rsa.d, tmp1, key->u.rsa.dP)) != CRYPT_OK) { goto cleanup_tmps; } /* dP = d mod p-1 */ + if ((err = ltc_mp_mod(key->u.rsa.d, tmp2, key->u.rsa.dQ)) != CRYPT_OK) { goto cleanup_tmps; } /* dQ = d mod q-1 */ key->id = LTC_PKA_RSA; key->u.rsa.type = PK_PRIVATE; cleanup_tmps: - mp_clear_multi(tmp2, tmp1, NULL); + ltc_mp_deinit_multi(tmp2, tmp1, LTC_NULL); cleanup: if (err != CRYPT_OK) { rsa_free(&key->u.rsa); diff --git a/src/misc/ssh/ssh_decode_sequence_multi.c b/src/misc/ssh/ssh_decode_sequence_multi.c index 4d61a5e72..3c4bb44e6 100644 --- a/src/misc/ssh/ssh_decode_sequence_multi.c +++ b/src/misc/ssh/ssh_decode_sequence_multi.c @@ -135,13 +135,13 @@ int ssh_decode_sequence_multi(const unsigned char *in, unsigned long *inlen, ... break; case LTC_SSHDATA_MPINT: if (size == 0) { - if ((err = mp_set(vdata, 0)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(vdata, 0)) != CRYPT_OK) { goto error; } } else if ((in[0] & 0x80) != 0) { /* Negative number - not supported */ err = CRYPT_INVALID_PACKET; goto error; } else { - if ((err = mp_read_unsigned_bin(vdata, in, size)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(vdata, in, size)) != CRYPT_OK) { goto error; } } in += size; break; diff --git a/src/misc/ssh/ssh_encode_sequence_multi.c b/src/misc/ssh/ssh_encode_sequence_multi.c index 48940b7e5..a2ad70433 100644 --- a/src/misc/ssh/ssh_encode_sequence_multi.c +++ b/src/misc/ssh/ssh_encode_sequence_multi.c @@ -59,9 +59,9 @@ int ssh_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) vdata = va_arg(args, void*); /* Calculate size */ size += 4; - if (mp_iszero(vdata) != LTC_MP_YES) { - size += mp_unsigned_bin_size(vdata); - if ((mp_count_bits(vdata) & 7) == 0) size++; /* Zero padding if high bit set */ + if (ltc_mp_iszero(vdata) != LTC_MP_YES) { + size += ltc_mp_unsigned_bin_size(vdata); + if ((ltc_mp_count_bits(vdata) & 7) == 0) size++; /* Zero padding if high bit set */ } break; @@ -119,12 +119,12 @@ int ssh_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) break; case LTC_SSHDATA_MPINT: vdata = va_arg(args, void*); - if (mp_iszero(vdata) == LTC_MP_YES) { + if (ltc_mp_iszero(vdata) == LTC_MP_YES) { STORE32H(0, out); out += 4; } else { - size = mp_unsigned_bin_size(vdata); - if ((mp_count_bits(vdata) & 7) == 0) { + size = ltc_mp_unsigned_bin_size(vdata); + if ((ltc_mp_count_bits(vdata) & 7) == 0) { /* Zero padding if high bit set */ STORE32H(size+1, out); out += 4; @@ -133,7 +133,7 @@ int ssh_encode_sequence_multi(unsigned char *out, unsigned long *outlen, ...) STORE32H(size, out); out += 4; } - if (mp_to_unsigned_bin(vdata, out) != CRYPT_OK) { + if (ltc_mp_to_unsigned_bin(vdata, out) != CRYPT_OK) { err = CRYPT_ERROR; goto error; } diff --git a/src/pk/asn1/der/integer/der_decode_integer.c b/src/pk/asn1/der/integer/der_decode_integer.c index 2c9bf7b74..e2555b92a 100644 --- a/src/pk/asn1/der/integer/der_decode_integer.c +++ b/src/pk/asn1/der/integer/der_decode_integer.c @@ -43,22 +43,22 @@ int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num) } x += inlen; - if ((err = mp_read_unsigned_bin(num, (unsigned char *)in + x, y)) != CRYPT_OK) { + if ((err = ltc_mp_read_unsigned_bin(num, (unsigned char *)in + x, y)) != CRYPT_OK) { return err; } /* see if it's negative */ if (in[x] & 0x80) { void *tmp; - if (mp_init(&tmp) != CRYPT_OK) { + if (ltc_mp_init(&tmp) != CRYPT_OK) { return CRYPT_MEM; } - if (mp_2expt(tmp, mp_count_bits(num)) != CRYPT_OK || mp_sub(num, tmp, num) != CRYPT_OK) { - mp_clear(tmp); + if (ltc_mp_2expt(tmp, ltc_mp_count_bits(num)) != CRYPT_OK || ltc_mp_sub(num, tmp, num) != CRYPT_OK) { + ltc_mp_clear(tmp); return CRYPT_MEM; } - mp_clear(tmp); + ltc_mp_clear(tmp); } return CRYPT_OK; diff --git a/src/pk/asn1/der/integer/der_encode_integer.c b/src/pk/asn1/der/integer/der_encode_integer.c index 726114960..23e2106a7 100644 --- a/src/pk/asn1/der/integer/der_encode_integer.c +++ b/src/pk/asn1/der/integer/der_encode_integer.c @@ -37,22 +37,22 @@ int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen) return CRYPT_BUFFER_OVERFLOW; } - if (mp_cmp_d(num, 0) != LTC_MP_LT) { + if (ltc_mp_cmp_d(num, 0) != LTC_MP_LT) { /* we only need a leading zero if the msb of the first byte is one */ - if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) { + if ((ltc_mp_count_bits(num) & 7) == 0 || ltc_mp_iszero(num) == LTC_MP_YES) { leading_zero = 1; } else { leading_zero = 0; } /* get length of num in bytes (plus 1 since we force the msbyte to zero) */ - y = mp_unsigned_bin_size(num) + leading_zero; + y = ltc_mp_unsigned_bin_size(num) + leading_zero; } else { leading_zero = 0; - y = mp_count_bits(num); + y = ltc_mp_count_bits(num); y = y + (8 - (y & 7)); y = y >> 3; - if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --y; + if (((ltc_mp_cnt_lsb(num)+1)==ltc_mp_count_bits(num)) && ((ltc_mp_count_bits(num)&7)==0)) --y; } /* now store initial data */ @@ -69,32 +69,32 @@ int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen) } /* if it's not zero store it as big endian */ - if (mp_cmp_d(num, 0) == LTC_MP_GT) { + if (ltc_mp_cmp_d(num, 0) == LTC_MP_GT) { /* now store the mpint */ - if ((err = mp_to_unsigned_bin(num, out)) != CRYPT_OK) { + if ((err = ltc_mp_to_unsigned_bin(num, out)) != CRYPT_OK) { return err; } - } else if (mp_iszero(num) != LTC_MP_YES) { + } else if (ltc_mp_iszero(num) != LTC_MP_YES) { void *tmp; /* negative */ - if (mp_init(&tmp) != CRYPT_OK) { + if (ltc_mp_init(&tmp) != CRYPT_OK) { return CRYPT_MEM; } /* 2^roundup and subtract */ - y = mp_count_bits(num); + y = ltc_mp_count_bits(num); y = y + (8 - (y & 7)); - if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) y -= 8; - if (mp_2expt(tmp, y) != CRYPT_OK || mp_add(tmp, num, tmp) != CRYPT_OK) { - mp_clear(tmp); + if (((ltc_mp_cnt_lsb(num)+1)==ltc_mp_count_bits(num)) && ((ltc_mp_count_bits(num)&7)==0)) y -= 8; + if (ltc_mp_2expt(tmp, y) != CRYPT_OK || ltc_mp_add(tmp, num, tmp) != CRYPT_OK) { + ltc_mp_clear(tmp); return CRYPT_MEM; } - if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) { - mp_clear(tmp); + if ((err = ltc_mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) { + ltc_mp_clear(tmp); return err; } - mp_clear(tmp); + ltc_mp_clear(tmp); } /* we good */ diff --git a/src/pk/asn1/der/integer/der_length_integer.c b/src/pk/asn1/der/integer/der_length_integer.c index 90e2de373..466f297c4 100644 --- a/src/pk/asn1/der/integer/der_length_integer.c +++ b/src/pk/asn1/der/integer/der_length_integer.c @@ -23,24 +23,24 @@ int der_length_integer(void *num, unsigned long *outlen) LTC_ARGCHK(num != NULL); LTC_ARGCHK(outlen != NULL); - if (mp_cmp_d(num, 0) != LTC_MP_LT) { + if (ltc_mp_cmp_d(num, 0) != LTC_MP_LT) { /* positive */ /* we only need a leading zero if the msb of the first byte is one */ - if ((mp_count_bits(num) & 7) == 0 || mp_iszero(num) == LTC_MP_YES) { + if ((ltc_mp_count_bits(num) & 7) == 0 || ltc_mp_iszero(num) == LTC_MP_YES) { leading_zero = 1; } else { leading_zero = 0; } /* size for bignum */ - len = leading_zero + mp_unsigned_bin_size(num); + len = leading_zero + ltc_mp_unsigned_bin_size(num); } else { /* it's negative */ /* find power of 2 that is a multiple of eight and greater than count bits */ - z = mp_count_bits(num); + z = ltc_mp_count_bits(num); z = z + (8 - (z & 7)); - if (((mp_cnt_lsb(num)+1)==mp_count_bits(num)) && ((mp_count_bits(num)&7)==0)) --z; + if (((ltc_mp_cnt_lsb(num)+1)==ltc_mp_count_bits(num)) && ((ltc_mp_count_bits(num)&7)==0)) --z; len = z >> 3; } diff --git a/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c b/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c index 35886cf88..b6ff36600 100644 --- a/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c +++ b/src/pk/asn1/der/sequence/der_decode_sequence_flexi.c @@ -149,7 +149,7 @@ static int s_der_decode_sequence_flexi(const unsigned char *in, unsigned long *i /* init field */ l->size = 1; - if ((err = mp_init(&l->data)) != CRYPT_OK) { + if ((err = ltc_mp_init(&l->data)) != CRYPT_OK) { goto error; } diff --git a/src/pk/asn1/der/sequence/der_sequence_free.c b/src/pk/asn1/der/sequence/der_sequence_free.c index b7cbd0700..63908a548 100644 --- a/src/pk/asn1/der/sequence/der_sequence_free.c +++ b/src/pk/asn1/der/sequence/der_sequence_free.c @@ -39,7 +39,7 @@ void der_sequence_free(ltc_asn1_list *in) switch (in->type) { case LTC_ASN1_SETOF: break; - case LTC_ASN1_INTEGER : if (in->data != NULL) { mp_clear(in->data); } break; + case LTC_ASN1_INTEGER : if (in->data != NULL) { ltc_mp_clear(in->data); } break; default : if (in->data != NULL) { XFREE(in->data); } } diff --git a/src/pk/asn1/pkcs8/pkcs8_get.c b/src/pk/asn1/pkcs8/pkcs8_get.c index 3ff74e2d7..015d1e7f1 100644 --- a/src/pk/asn1/pkcs8/pkcs8_get.c +++ b/src/pk/asn1/pkcs8/pkcs8_get.c @@ -35,7 +35,7 @@ int pkcs8_get_children(const ltc_asn1_list *decoded_list, enum ltc_oid_id *pka, * we get an 'input too long' error but the rest is already decoded and can be * handled the same as for version 0 */ - if (mp_cmp_d(version->data, 0) != LTC_MP_EQ && mp_cmp_d(version->data, 1) != LTC_MP_EQ) { + if (ltc_mp_cmp_d(version->data, 0) != LTC_MP_EQ && ltc_mp_cmp_d(version->data, 1) != LTC_MP_EQ) { return CRYPT_INVALID_PACKET; } break; diff --git a/src/pk/dh/dh.c b/src/pk/dh/dh.c index 471d215ce..a04912710 100644 --- a/src/pk/dh/dh.c +++ b/src/pk/dh/dh.c @@ -221,7 +221,7 @@ const ltc_dh_set_type ltc_dh_sets[] = { int dh_get_groupsize(const dh_key *key) { if (key == NULL) return 0; - return mp_unsigned_bin_size(key->prime); + return ltc_mp_unsigned_bin_size(key->prime); } /** @@ -233,7 +233,7 @@ int dh_init(dh_key *key) { int err; LTC_ARGCHK(key != NULL); - if ((err = mp_init_multi(&key->x, &key->y, &key->base, &key->prime, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&key->x, &key->y, &key->base, &key->prime, LTC_NULL)) != CRYPT_OK) { return err; } return CRYPT_OK; diff --git a/src/pk/dh/dh_check_pubkey.c b/src/pk/dh/dh_check_pubkey.c index 7dd128ed7..88c252e0d 100644 --- a/src/pk/dh/dh_check_pubkey.c +++ b/src/pk/dh/dh_check_pubkey.c @@ -18,23 +18,23 @@ int dh_check_pubkey(const dh_key *key) LTC_ARGCHK(key != NULL); - if ((err = mp_init(&p_minus1)) != CRYPT_OK) { + if ((err = ltc_mp_init(&p_minus1)) != CRYPT_OK) { return err; } /* avoid: y <= 1 OR y >= p-1 */ - if ((err = mp_sub_d(key->prime, 1, p_minus1)) != CRYPT_OK) { + if ((err = ltc_mp_sub_d(key->prime, 1, p_minus1)) != CRYPT_OK) { goto error; } - if (mp_cmp(key->y, p_minus1) != LTC_MP_LT || mp_cmp_d(key->y, 1) != LTC_MP_GT) { + if (ltc_mp_cmp(key->y, p_minus1) != LTC_MP_LT || ltc_mp_cmp_d(key->y, 1) != LTC_MP_GT) { err = CRYPT_INVALID_ARG; goto error; } /* public key must have more than one bit set */ - digit_count = mp_get_digit_count(key->y); + digit_count = ltc_mp_get_digit_count(key->y); for (i = 0; i < digit_count && bits_set < 2; i++) { - digit = mp_get_digit(key->y, i); + digit = ltc_mp_get_digit(key->y, i); while (digit > 0) { if (digit & 1) bits_set++; digit >>= 1; @@ -48,7 +48,7 @@ int dh_check_pubkey(const dh_key *key) } error: - mp_clear(p_minus1); + ltc_mp_clear(p_minus1); return err; } diff --git a/src/pk/dh/dh_export_key.c b/src/pk/dh/dh_export_key.c index 374635c74..dd5bcb701 100644 --- a/src/pk/dh/dh_export_key.c +++ b/src/pk/dh/dh_export_key.c @@ -23,7 +23,7 @@ int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key) LTC_ARGCHK(key != NULL); k = (type == PK_PRIVATE) ? key->x : key->y; - len = mp_unsigned_bin_size(k); + len = ltc_mp_unsigned_bin_size(k); if (*outlen < len) { *outlen = len; @@ -31,7 +31,7 @@ int dh_export_key(void *out, unsigned long *outlen, int type, const dh_key *key) } *outlen = len; - return mp_to_unsigned_bin(k, out); + return ltc_mp_to_unsigned_bin(k, out); } #endif /* LTC_MDH */ diff --git a/src/pk/dh/dh_free.c b/src/pk/dh/dh_free.c index ff56b0064..0801114f7 100644 --- a/src/pk/dh/dh_free.c +++ b/src/pk/dh/dh_free.c @@ -12,7 +12,7 @@ void dh_free(dh_key *key) { LTC_ARGCHKVD(key != NULL); - mp_cleanup_multi(&key->prime, &key->base, &key->y, &key->x, LTC_NULL); + ltc_mp_cleanup_multi(&key->prime, &key->base, &key->y, &key->x, LTC_NULL); } #endif /* LTC_MDH */ diff --git a/src/pk/dh/dh_generate_key.c b/src/pk/dh/dh_generate_key.c index 39ef8e14c..f415a4c68 100644 --- a/src/pk/dh/dh_generate_key.c +++ b/src/pk/dh/dh_generate_key.c @@ -48,7 +48,7 @@ int dh_generate_key(prng_state *prng, int wprng, dh_key *key) return err; } - keysize = s_dh_groupsize_to_keysize(mp_unsigned_bin_size(key->prime)); + keysize = s_dh_groupsize_to_keysize(ltc_mp_unsigned_bin_size(key->prime)); if (keysize == 0) { err = CRYPT_INVALID_KEYSIZE; goto freemp; @@ -69,11 +69,11 @@ int dh_generate_key(prng_state *prng, int wprng, dh_key *key) goto freebuf; } /* load the x value - private key */ - if ((err = mp_read_unsigned_bin(key->x, buf, keysize)) != CRYPT_OK) { + if ((err = ltc_mp_read_unsigned_bin(key->x, buf, keysize)) != CRYPT_OK) { goto freebuf; } /* compute the y value - public key */ - if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto freebuf; } err = dh_check_pubkey(key); diff --git a/src/pk/dh/dh_import.c b/src/pk/dh/dh_import.c index 8be75e259..b18e4396e 100644 --- a/src/pk/dh/dh_import.c +++ b/src/pk/dh/dh_import.c @@ -48,7 +48,7 @@ int dh_import(const unsigned char *in, unsigned long inlen, dh_key *key) goto error; } /* compute public key: y = (base ^ x) mod prime */ - if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto error; } } diff --git a/src/pk/dh/dh_import_pkcs8.c b/src/pk/dh/dh_import_pkcs8.c index 431b05eea..d1fd792bb 100644 --- a/src/pk/dh/dh_import_pkcs8.c +++ b/src/pk/dh/dh_import_pkcs8.c @@ -22,10 +22,10 @@ int dh_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, dh_key return err; } - if ((err = mp_copy(alg_id->child->next->child->data, key->prime)) != CRYPT_OK) { + if ((err = ltc_mp_copy(alg_id->child->next->child->data, key->prime)) != CRYPT_OK) { goto error; } - if ((err = mp_copy(alg_id->child->next->child->next->data, key->base)) != CRYPT_OK) { + if ((err = ltc_mp_copy(alg_id->child->next->child->next->data, key->base)) != CRYPT_OK) { goto error; } @@ -33,7 +33,7 @@ int dh_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, dh_key goto error; } /* compute public key: y = (base ^ x) mod prime */ - if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto error; } /* check public key */ diff --git a/src/pk/dh/dh_set.c b/src/pk/dh/dh_set.c index f81bf3810..1cc044f81 100644 --- a/src/pk/dh/dh_set.c +++ b/src/pk/dh/dh_set.c @@ -30,8 +30,8 @@ int dh_set_pg(const unsigned char *p, unsigned long plen, return err; } - if ((err = mp_read_unsigned_bin(key->base, (unsigned char*)g, glen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->prime, (unsigned char*)p, plen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->base, (unsigned char*)g, glen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->prime, (unsigned char*)p, plen)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; @@ -62,8 +62,8 @@ int dh_set_pg_groupsize(int groupsize, dh_key *key) return err; } - if ((err = mp_read_radix(key->base, ltc_dh_sets[i].base, 16)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_radix(key->prime, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_radix(key->base, ltc_dh_sets[i].base, 16)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_radix(key->prime, ltc_dh_sets[i].prime, 16)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; @@ -92,12 +92,12 @@ int dh_set_key(const unsigned char *in, unsigned long inlen, int type, dh_key *k if (type == PK_PRIVATE) { key->type = PK_PRIVATE; - if ((err = mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->x, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_exptmod(key->base, key->x, key->prime, key->y)) != CRYPT_OK) { goto LBL_ERR; } } else { key->type = PK_PUBLIC; - if ((err = mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->y, (unsigned char*)in, inlen)) != CRYPT_OK) { goto LBL_ERR; } } /* check public key */ diff --git a/src/pk/dh/dh_shared_secret.c b/src/pk/dh/dh_shared_secret.c index 0c4dfc9a5..b60a8486c 100644 --- a/src/pk/dh/dh_shared_secret.c +++ b/src/pk/dh/dh_shared_secret.c @@ -31,11 +31,11 @@ int dh_shared_secret(const dh_key *private_key, const dh_key *public_key, } /* same DH group? */ - if (mp_cmp(private_key->prime, public_key->prime) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; } - if (mp_cmp(private_key->base, public_key->base) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; } + if (ltc_mp_cmp(private_key->prime, public_key->prime) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; } + if (ltc_mp_cmp(private_key->base, public_key->base) != LTC_MP_EQ) { return CRYPT_PK_TYPE_MISMATCH; } /* init big numbers */ - if ((err = mp_init(&tmp)) != CRYPT_OK) { + if ((err = ltc_mp_init(&tmp)) != CRYPT_OK) { return err; } @@ -45,25 +45,25 @@ int dh_shared_secret(const dh_key *private_key, const dh_key *public_key, } /* compute tmp = y^x mod p */ - if ((err = mp_exptmod(public_key->y, private_key->x, private_key->prime, tmp)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(public_key->y, private_key->x, private_key->prime, tmp)) != CRYPT_OK) { goto error; } /* enough space for output? */ - x = (unsigned long)mp_unsigned_bin_size(tmp); + x = (unsigned long)ltc_mp_unsigned_bin_size(tmp); if (*outlen < x) { *outlen = x; err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) { + if ((err = ltc_mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) { goto error; } *outlen = x; err = CRYPT_OK; error: - mp_clear(tmp); + ltc_mp_clear(tmp); return err; } diff --git a/src/pk/dsa/dsa_decrypt_key.c b/src/pk/dsa/dsa_decrypt_key.c index c89702398..fa87ef95f 100644 --- a/src/pk/dsa/dsa_decrypt_key.c +++ b/src/pk/dsa/dsa_decrypt_key.c @@ -53,12 +53,12 @@ int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, /* we now have the hash! */ - if ((err = mp_init(&g_pub)) != CRYPT_OK) { + if ((err = ltc_mp_init(&g_pub)) != CRYPT_OK) { return err; } /* allocate memory */ - expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1); + expt = XMALLOC(ltc_mp_unsigned_bin_size(key->p) + 1); skey = XMALLOC(MAXBLOCKSIZE); if (expt == NULL || skey == NULL) { if (expt != NULL) { @@ -67,7 +67,7 @@ int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, if (skey != NULL) { XFREE(skey); } - mp_clear(g_pub); + ltc_mp_clear(g_pub); return CRYPT_MEM; } @@ -80,12 +80,12 @@ int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, } /* make shared key */ - x = mp_unsigned_bin_size(key->p) + 1; + x = ltc_mp_unsigned_bin_size(key->p) + 1; if ((err = dsa_shared_secret(key->x, g_pub, key, expt, &x)) != CRYPT_OK) { goto LBL_ERR; } - y = mp_unsigned_bin_size(key->p) + 1; + y = ltc_mp_unsigned_bin_size(key->p) + 1; y = MIN(y, MAXBLOCKSIZE); if ((err = hash_memory(hash, expt, x, expt, &y)) != CRYPT_OK) { goto LBL_ERR; @@ -113,14 +113,14 @@ int dsa_decrypt_key(const unsigned char *in, unsigned long inlen, err = CRYPT_OK; LBL_ERR: #ifdef LTC_CLEAN_STACK - zeromem(expt, mp_unsigned_bin_size(key->p) + 1); + zeromem(expt, ltc_mp_unsigned_bin_size(key->p) + 1); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(expt); XFREE(skey); - mp_clear(g_pub); + ltc_mp_clear(g_pub); return err; } diff --git a/src/pk/dsa/dsa_encrypt_key.c b/src/pk/dsa/dsa_encrypt_key.c index 2f4609218..30097651c 100644 --- a/src/pk/dsa/dsa_encrypt_key.c +++ b/src/pk/dsa/dsa_encrypt_key.c @@ -50,11 +50,11 @@ int dsa_encrypt_key(const unsigned char *in, unsigned long inlen, } /* make a random key and export the public copy */ - if ((err = mp_init_multi(&g_pub, &g_priv, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&g_pub, &g_priv, LTC_NULL)) != CRYPT_OK) { return err; } - expt = XMALLOC(mp_unsigned_bin_size(key->p) + 1); + expt = XMALLOC(ltc_mp_unsigned_bin_size(key->p) + 1); skey = XMALLOC(MAXBLOCKSIZE); if (expt == NULL || skey == NULL) { if (expt != NULL) { @@ -63,7 +63,7 @@ int dsa_encrypt_key(const unsigned char *in, unsigned long inlen, if (skey != NULL) { XFREE(skey); } - mp_clear_multi(g_pub, g_priv, LTC_NULL); + ltc_mp_deinit_multi(g_pub, g_priv, LTC_NULL); return CRYPT_MEM; } @@ -75,12 +75,12 @@ int dsa_encrypt_key(const unsigned char *in, unsigned long inlen, } /* compute y */ - if ((err = mp_exptmod(key->g, g_priv, key->p, g_pub)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->g, g_priv, key->p, g_pub)) != CRYPT_OK) { goto LBL_ERR; } /* make random key */ - x = mp_unsigned_bin_size(key->p) + 1; + x = ltc_mp_unsigned_bin_size(key->p) + 1; if ((err = dsa_shared_secret(g_priv, key->y, key, expt, &x)) != CRYPT_OK) { goto LBL_ERR; } @@ -104,14 +104,14 @@ int dsa_encrypt_key(const unsigned char *in, unsigned long inlen, LBL_ERR: #ifdef LTC_CLEAN_STACK /* clean up */ - zeromem(expt, mp_unsigned_bin_size(key->p) + 1); + zeromem(expt, ltc_mp_unsigned_bin_size(key->p) + 1); zeromem(skey, MAXBLOCKSIZE); #endif XFREE(skey); XFREE(expt); - mp_clear_multi(g_pub, g_priv, LTC_NULL); + ltc_mp_deinit_multi(g_pub, g_priv, LTC_NULL); return err; } diff --git a/src/pk/dsa/dsa_export.c b/src/pk/dsa/dsa_export.c index 3550cbeca..c26dd46a1 100644 --- a/src/pk/dsa/dsa_export.c +++ b/src/pk/dsa/dsa_export.c @@ -58,7 +58,7 @@ int dsa_export(unsigned char *out, unsigned long *outlen, int type, const dsa_ke if (type == PK_PUBLIC) { if (std) { - unsigned long tmplen = (unsigned long)(mp_count_bits(key->y) / 8) + 8; + unsigned long tmplen = (unsigned long)(ltc_mp_count_bits(key->y) / 8) + 8; unsigned char* tmp = XMALLOC(tmplen); ltc_asn1_list int_list[3]; diff --git a/src/pk/dsa/dsa_free.c b/src/pk/dsa/dsa_free.c index dbe862533..aa9294fd3 100644 --- a/src/pk/dsa/dsa_free.c +++ b/src/pk/dsa/dsa_free.c @@ -16,7 +16,7 @@ void dsa_free(dsa_key *key) { LTC_ARGCHKVD(key != NULL); - mp_cleanup_multi(&key->y, &key->x, &key->q, &key->g, &key->p, LTC_NULL); + ltc_mp_cleanup_multi(&key->y, &key->x, &key->q, &key->g, &key->p, LTC_NULL); key->type = key->qord = 0; } diff --git a/src/pk/dsa/dsa_generate_key.c b/src/pk/dsa/dsa_generate_key.c index bc83c0ed9..651cfb45d 100644 --- a/src/pk/dsa/dsa_generate_key.c +++ b/src/pk/dsa/dsa_generate_key.c @@ -28,7 +28,7 @@ int dsa_generate_key(prng_state *prng, int wprng, dsa_key *key) */ /* private key x should be from range: 1 <= x <= q-1 (see FIPS 186-4 B.1.2) */ if ((err = rand_bn_upto(key->x, key->q, prng, wprng)) != CRYPT_OK) { return err; } - if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; } + if ((err = ltc_mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { return err; } key->type = PK_PRIVATE; return CRYPT_OK; diff --git a/src/pk/dsa/dsa_generate_pqg.c b/src/pk/dsa/dsa_generate_pqg.c index d607782fa..cad6787bb 100644 --- a/src/pk/dsa/dsa_generate_pqg.c +++ b/src/pk/dsa/dsa_generate_pqg.c @@ -108,14 +108,14 @@ static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int mo if ((wbuf = XMALLOC((n+1)*outbytes)) == NULL) { err = CRYPT_MEM; goto cleanup3; } if ((sbuf = XMALLOC(seedbytes)) == NULL) { err = CRYPT_MEM; goto cleanup2; } - err = mp_init_multi(&t2L1, &t2N1, &t2q, &t2seedlen, &U, &W, &X, &c, &h, &e, &seedinc, LTC_NULL); + err = ltc_mp_init_multi(&t2L1, &t2N1, &t2q, &t2seedlen, &U, &W, &X, &c, &h, &e, &seedinc, LTC_NULL); if (err != CRYPT_OK) { goto cleanup1; } - if ((err = mp_2expt(t2L1, L-1)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_2expt(t2L1, L-1)) != CRYPT_OK) { goto cleanup; } /* t2L1 = 2^(L-1) */ - if ((err = mp_2expt(t2N1, N-1)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_2expt(t2N1, N-1)) != CRYPT_OK) { goto cleanup; } /* t2N1 = 2^(N-1) */ - if ((err = mp_2expt(t2seedlen, seedbytes*8)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_2expt(t2seedlen, seedbytes*8)) != CRYPT_OK) { goto cleanup; } /* t2seedlen = 2^seedlen */ for(found_p=0; !found_p;) { @@ -124,38 +124,38 @@ static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int mo if (prng_descriptor[wprng].read(sbuf, seedbytes, prng) != seedbytes) { err = CRYPT_ERROR_READPRNG; goto cleanup; } i = outbytes; if ((err = hash_memory(hash, sbuf, seedbytes, digest, &i)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_read_unsigned_bin(U, digest, outbytes)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_mod(U, t2N1, U)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_add(t2N1, U, q)) != CRYPT_OK) { goto cleanup; } - if (!mp_isodd(q)) mp_add_d(q, 1, q); - if ((err = mp_prime_is_prime(q, mr_tests_q, &res)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_read_unsigned_bin(U, digest, outbytes)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_mod(U, t2N1, U)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_add(t2N1, U, q)) != CRYPT_OK) { goto cleanup; } + if (!ltc_mp_isodd(q)) ltc_mp_add_d(q, 1, q); + if ((err = ltc_mp_prime_is_prime(q, mr_tests_q, &res)) != CRYPT_OK) { goto cleanup; } if (res == LTC_MP_YES) found_q = 1; } /* p */ - if ((err = mp_read_unsigned_bin(seedinc, sbuf, seedbytes)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_add(q, q, t2q)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_read_unsigned_bin(seedinc, sbuf, seedbytes)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_add(q, q, t2q)) != CRYPT_OK) { goto cleanup; } for(counter=0; counter < 4*L && !found_p; counter++) { for(j=0; j<=n; j++) { - if ((err = mp_add_d(seedinc, 1, seedinc)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_mod(seedinc, t2seedlen, seedinc)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_add_d(seedinc, 1, seedinc)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_mod(seedinc, t2seedlen, seedinc)) != CRYPT_OK) { goto cleanup; } /* seedinc = (seedinc+1) % 2^seed_bitlen */ - if ((i = mp_unsigned_bin_size(seedinc)) > seedbytes) { err = CRYPT_INVALID_ARG; goto cleanup; } + if ((i = ltc_mp_unsigned_bin_size(seedinc)) > seedbytes) { err = CRYPT_INVALID_ARG; goto cleanup; } zeromem(sbuf, seedbytes); - if ((err = mp_to_unsigned_bin(seedinc, sbuf + seedbytes-i)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_to_unsigned_bin(seedinc, sbuf + seedbytes-i)) != CRYPT_OK) { goto cleanup; } i = outbytes; err = hash_memory(hash, sbuf, seedbytes, wbuf+(n-j)*outbytes, &i); if (err != CRYPT_OK) { goto cleanup; } } - if ((err = mp_read_unsigned_bin(W, wbuf, (n+1)*outbytes)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_mod(W, t2L1, W)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_add(W, t2L1, X)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_mod(X, t2q, c)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_sub_d(c, 1, p)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_sub(X, p, p)) != CRYPT_OK) { goto cleanup; } - if (mp_cmp(p, t2L1) != LTC_MP_LT) { + if ((err = ltc_mp_read_unsigned_bin(W, wbuf, (n+1)*outbytes)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_mod(W, t2L1, W)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_add(W, t2L1, X)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_mod(X, t2q, c)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_sub_d(c, 1, p)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_sub(X, p, p)) != CRYPT_OK) { goto cleanup; } + if (ltc_mp_cmp(p, t2L1) != LTC_MP_LT) { /* p >= 2^(L-1) */ - if ((err = mp_prime_is_prime(p, mr_tests_p, &res)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_prime_is_prime(p, mr_tests_p, &res)) != CRYPT_OK) { goto cleanup; } if (res == LTC_MP_YES) { found_p = 1; } @@ -172,22 +172,22 @@ static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int mo * */ - if ((err = mp_sub_d(p, 1, e)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_div(e, q, e, c)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_sub_d(p, 1, e)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_div(e, q, e, c)) != CRYPT_OK) { goto cleanup; } /* e = (p - 1)/q */ - i = mp_count_bits(p); + i = ltc_mp_count_bits(p); do { do { if ((err = rand_bn_bits(h, i, prng, wprng)) != CRYPT_OK) { goto cleanup; } - } while (mp_cmp(h, p) != LTC_MP_LT || mp_cmp_d(h, 2) != LTC_MP_GT); - if ((err = mp_sub_d(h, 1, h)) != CRYPT_OK) { goto cleanup; } + } while (ltc_mp_cmp(h, p) != LTC_MP_LT || ltc_mp_cmp_d(h, 2) != LTC_MP_GT); + if ((err = ltc_mp_sub_d(h, 1, h)) != CRYPT_OK) { goto cleanup; } /* h is randon and 1 < h < (p-1) */ - if ((err = mp_exptmod(h, e, p, g)) != CRYPT_OK) { goto cleanup; } - } while (mp_cmp_d(g, 1) == LTC_MP_EQ); + if ((err = ltc_mp_exptmod(h, e, p, g)) != CRYPT_OK) { goto cleanup; } + } while (ltc_mp_cmp_d(g, 1) == LTC_MP_EQ); err = CRYPT_OK; cleanup: - mp_clear_multi(t2L1, t2N1, t2q, t2seedlen, U, W, X, c, h, e, seedinc, LTC_NULL); + ltc_mp_deinit_multi(t2L1, t2N1, t2q, t2seedlen, U, W, X, c, h, e, seedinc, LTC_NULL); cleanup1: XFREE(sbuf); cleanup2: diff --git a/src/pk/dsa/dsa_import.c b/src/pk/dsa/dsa_import.c index 71619a556..d2c2d49ec 100644 --- a/src/pk/dsa/dsa_import.c +++ b/src/pk/dsa/dsa_import.c @@ -118,7 +118,7 @@ int dsa_import(const unsigned char *in, unsigned long inlen, dsa_key *key) } LBL_OK: - key->qord = mp_unsigned_bin_size(key->q); + key->qord = ltc_mp_unsigned_bin_size(key->q); /* quick p, q, g validation, without primality testing * + x, y validation */ diff --git a/src/pk/dsa/dsa_import_pkcs8.c b/src/pk/dsa/dsa_import_pkcs8.c index f8a592a65..b3bc9c953 100644 --- a/src/pk/dsa/dsa_import_pkcs8.c +++ b/src/pk/dsa/dsa_import_pkcs8.c @@ -26,7 +26,7 @@ int dsa_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, dsa_ke if ((err = der_decode_integer(priv_key->data, priv_key->size, key->x)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; } @@ -40,7 +40,7 @@ int dsa_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, dsa_ke goto LBL_ERR; } - key->qord = mp_unsigned_bin_size(key->q); + key->qord = ltc_mp_unsigned_bin_size(key->q); key->type = PK_PRIVATE; return err; diff --git a/src/pk/dsa/dsa_init.c b/src/pk/dsa/dsa_init.c index e7952845e..77625ebbc 100644 --- a/src/pk/dsa/dsa_init.c +++ b/src/pk/dsa/dsa_init.c @@ -16,7 +16,7 @@ int dsa_int_init(dsa_key *key) LTC_ARGCHK(ltc_mp.name != NULL); /* init key */ - return mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, LTC_NULL); + return ltc_mp_init_multi(&key->p, &key->g, &key->q, &key->x, &key->y, LTC_NULL); } #endif diff --git a/src/pk/dsa/dsa_set.c b/src/pk/dsa/dsa_set.c index c04b42a75..6aee4d8f8 100644 --- a/src/pk/dsa/dsa_set.c +++ b/src/pk/dsa/dsa_set.c @@ -30,11 +30,11 @@ int dsa_set_pqg(const unsigned char *p, unsigned long plen, /* init key */ if ((err = dsa_int_init(key)) != CRYPT_OK) return err; - if ((err = mp_read_unsigned_bin(key->p, p , plen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->g, g , glen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->q, q , qlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->p, p , plen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->g, g , glen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->q, q , qlen)) != CRYPT_OK) { goto LBL_ERR; } - key->qord = mp_unsigned_bin_size(key->q); + key->qord = ltc_mp_unsigned_bin_size(key->q); /* do only a quick validation, without primality testing */ if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) { goto LBL_ERR; } @@ -75,12 +75,12 @@ int dsa_set_key(const unsigned char *in, unsigned long inlen, int type, dsa_key if (type == PK_PRIVATE) { key->type = PK_PRIVATE; - if ((err = mp_read_unsigned_bin(key->x, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->x, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_exptmod(key->g, key->x, key->p, key->y)) != CRYPT_OK) { goto LBL_ERR; } } else { key->type = PK_PUBLIC; - if ((err = mp_read_unsigned_bin(key->y, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->y, in, inlen)) != CRYPT_OK) { goto LBL_ERR; } } if ((err = dsa_int_validate_xy(key, &stat)) != CRYPT_OK) { goto LBL_ERR; } diff --git a/src/pk/dsa/dsa_set_pqg_dsaparam.c b/src/pk/dsa/dsa_set_pqg_dsaparam.c index f269c4bb8..3c9bf4072 100644 --- a/src/pk/dsa/dsa_set_pqg_dsaparam.c +++ b/src/pk/dsa/dsa_set_pqg_dsaparam.c @@ -35,7 +35,7 @@ int dsa_set_pqg_dsaparam(const unsigned char *dsaparam, unsigned long dsaparamle goto LBL_ERR; } - key->qord = mp_unsigned_bin_size(key->q); + key->qord = ltc_mp_unsigned_bin_size(key->q); /* quick p, q, g validation, without primality testing */ if ((err = dsa_int_validate_pqg(key, &stat)) != CRYPT_OK) { diff --git a/src/pk/dsa/dsa_shared_secret.c b/src/pk/dsa/dsa_shared_secret.c index df11c178a..866832a72 100644 --- a/src/pk/dsa/dsa_shared_secret.c +++ b/src/pk/dsa/dsa_shared_secret.c @@ -32,28 +32,28 @@ int dsa_shared_secret(void *private_key, void *base, LTC_ARGCHK(outlen != NULL); /* make new point */ - if ((err = mp_init(&res)) != CRYPT_OK) { + if ((err = ltc_mp_init(&res)) != CRYPT_OK) { return err; } - if ((err = mp_exptmod(base, private_key, public_key->p, res)) != CRYPT_OK) { - mp_clear(res); + if ((err = ltc_mp_exptmod(base, private_key, public_key->p, res)) != CRYPT_OK) { + ltc_mp_clear(res); return err; } - x = (unsigned long)mp_unsigned_bin_size(res); + x = (unsigned long)ltc_mp_unsigned_bin_size(res); if (*outlen < x) { *outlen = x; err = CRYPT_BUFFER_OVERFLOW; goto done; } zeromem(out, x); - if ((err = mp_to_unsigned_bin(res, out + (x - mp_unsigned_bin_size(res)))) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_to_unsigned_bin(res, out + (x - ltc_mp_unsigned_bin_size(res)))) != CRYPT_OK) { goto done; } err = CRYPT_OK; *outlen = x; done: - mp_clear(res); + ltc_mp_clear(res); return err; } diff --git a/src/pk/dsa/dsa_sign_hash.c b/src/pk/dsa/dsa_sign_hash.c index e73429b57..ab3e27adb 100644 --- a/src/pk/dsa/dsa_sign_hash.c +++ b/src/pk/dsa/dsa_sign_hash.c @@ -13,8 +13,8 @@ Sign a hash with DSA @param in The hash to sign @param inlen The length of the hash to sign - @param r The "r" integer of the signature (caller must initialize with mp_init() first) - @param s The "s" integer of the signature (caller must initialize with mp_init() first) + @param r The "r" integer of the signature (caller must initialize with ltc_mp_init() first) + @param s The "s" integer of the signature (caller must initialize with ltc_mp_init() first) @param prng An active PRNG state @param wprng The index of the PRNG desired @param key A private DSA key @@ -51,45 +51,45 @@ int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen, } /* Init our temps */ - if ((err = mp_init_multi(&k, &kinv, &tmp, LTC_NULL)) != CRYPT_OK) { goto ERRBUF; } + if ((err = ltc_mp_init_multi(&k, &kinv, &tmp, LTC_NULL)) != CRYPT_OK) { goto ERRBUF; } - qbits = mp_count_bits(key->q); + qbits = ltc_mp_count_bits(key->q); retry: do { /* gen random k */ - if ((err = rand_bn_bits(k, qbits, prng, wprng)) != CRYPT_OK) { goto error; } + if ((err = rand_bn_bits(k, qbits, prng, wprng)) != CRYPT_OK) { goto error; } /* k should be from range: 1 <= k <= q-1 (see FIPS 186-4 B.2.2) */ - if (mp_cmp_d(k, 0) != LTC_MP_GT || mp_cmp(k, key->q) != LTC_MP_LT) { goto retry; } + if (ltc_mp_cmp_d(k, 0) != LTC_MP_GT || ltc_mp_cmp(k, key->q) != LTC_MP_LT) { goto retry; } /* test gcd */ - if ((err = mp_gcd(k, key->q, tmp)) != CRYPT_OK) { goto error; } - } while (mp_cmp_d(tmp, 1) != LTC_MP_EQ); + if ((err = ltc_mp_gcd(k, key->q, tmp)) != CRYPT_OK) { goto error; } + } while (ltc_mp_cmp_d(tmp, 1) != LTC_MP_EQ); /* now find 1/k mod q */ - if ((err = mp_invmod(k, key->q, kinv)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_invmod(k, key->q, kinv)) != CRYPT_OK) { goto error; } /* now find r = g^k mod p mod q */ - if ((err = mp_exptmod(key->g, k, key->p, r)) != CRYPT_OK) { goto error; } - if ((err = mp_mod(r, key->q, r)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(key->g, k, key->p, r)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mod(r, key->q, r)) != CRYPT_OK) { goto error; } - if (mp_iszero(r) == LTC_MP_YES) { goto retry; } + if (ltc_mp_iszero(r) == LTC_MP_YES) { goto retry; } /* FIPS 186-4 4.6: use leftmost min(bitlen(q), bitlen(hash)) bits of 'hash'*/ inlen = MIN(inlen, (unsigned long)(key->qord)); /* now find s = (in + xr)/k mod q */ - if ((err = mp_read_unsigned_bin(tmp, in, inlen)) != CRYPT_OK) { goto error; } - if ((err = mp_mul(key->x, r, s)) != CRYPT_OK) { goto error; } - if ((err = mp_add(s, tmp, s)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(s, kinv, key->q, s)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(tmp, in, inlen)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mul(key->x, r, s)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add(s, tmp, s)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(s, kinv, key->q, s)) != CRYPT_OK) { goto error; } - if (mp_iszero(s) == LTC_MP_YES) { goto retry; } + if (ltc_mp_iszero(s) == LTC_MP_YES) { goto retry; } err = CRYPT_OK; error: - mp_clear_multi(k, kinv, tmp, LTC_NULL); + ltc_mp_deinit_multi(k, kinv, tmp, LTC_NULL); ERRBUF: #ifdef LTC_CLEAN_STACK zeromem(buf, LTC_MDSA_MAX_GROUP); @@ -121,7 +121,7 @@ int dsa_sign_hash(const unsigned char *in, unsigned long inlen, LTC_ARGCHK(outlen != NULL); LTC_ARGCHK(key != NULL); - if (mp_init_multi(&r, &s, LTC_NULL) != CRYPT_OK) { + if (ltc_mp_init_multi(&r, &s, LTC_NULL) != CRYPT_OK) { return CRYPT_MEM; } @@ -135,7 +135,7 @@ int dsa_sign_hash(const unsigned char *in, unsigned long inlen, LTC_ASN1_EOL, 0UL, NULL); error: - mp_clear_multi(r, s, LTC_NULL); + ltc_mp_deinit_multi(r, s, LTC_NULL); return err; } diff --git a/src/pk/dsa/dsa_verify_hash.c b/src/pk/dsa/dsa_verify_hash.c index 8ac3bf74a..230041a5a 100644 --- a/src/pk/dsa/dsa_verify_hash.c +++ b/src/pk/dsa/dsa_verify_hash.c @@ -36,12 +36,12 @@ int dsa_verify_hash_raw( void *r, void *s, *stat = 0; /* init our variables */ - if ((err = mp_init_multi(&w, &v, &u1, &u2, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&w, &v, &u1, &u2, LTC_NULL)) != CRYPT_OK) { return err; } /* neither r or s can be null or >q*/ - if (mp_cmp_d(r, 0) != LTC_MP_GT || mp_cmp_d(s, 0) != LTC_MP_GT || mp_cmp(r, key->q) != LTC_MP_LT || mp_cmp(s, key->q) != LTC_MP_LT) { + if (ltc_mp_cmp_d(r, 0) != LTC_MP_GT || ltc_mp_cmp_d(s, 0) != LTC_MP_GT || ltc_mp_cmp(r, key->q) != LTC_MP_LT || ltc_mp_cmp(s, key->q) != LTC_MP_LT) { err = CRYPT_INVALID_PACKET; goto error; } @@ -50,29 +50,29 @@ int dsa_verify_hash_raw( void *r, void *s, hashlen = MIN(hashlen, (unsigned long)(key->qord)); /* w = 1/s mod q */ - if ((err = mp_invmod(s, key->q, w)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_invmod(s, key->q, w)) != CRYPT_OK) { goto error; } /* u1 = m * w mod q */ - if ((err = mp_read_unsigned_bin(u1, hash, hashlen)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(u1, w, key->q, u1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(u1, hash, hashlen)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(u1, w, key->q, u1)) != CRYPT_OK) { goto error; } /* u2 = r*w mod q */ - if ((err = mp_mulmod(r, w, key->q, u2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(r, w, key->q, u2)) != CRYPT_OK) { goto error; } /* v = g^u1 * y^u2 mod p mod q */ - if ((err = mp_exptmod(key->g, u1, key->p, u1)) != CRYPT_OK) { goto error; } - if ((err = mp_exptmod(key->y, u2, key->p, u2)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(u1, u2, key->p, v)) != CRYPT_OK) { goto error; } - if ((err = mp_mod(v, key->q, v)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(key->g, u1, key->p, u1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(key->y, u2, key->p, u2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(u1, u2, key->p, v)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mod(v, key->q, v)) != CRYPT_OK) { goto error; } /* if r = v then we're set */ - if (mp_cmp(r, v) == LTC_MP_EQ) { + if (ltc_mp_cmp(r, v) == LTC_MP_EQ) { *stat = 1; } err = CRYPT_OK; error: - mp_clear_multi(w, v, u1, u2, LTC_NULL); + ltc_mp_deinit_multi(w, v, u1, u2, LTC_NULL); return err; } @@ -98,7 +98,7 @@ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, LTC_ARGCHK(stat != NULL); *stat = 0; /* must be set before the first return */ - if ((err = mp_init_multi(&r, &s, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&r, &s, LTC_NULL)) != CRYPT_OK) { return err; } @@ -119,7 +119,7 @@ int dsa_verify_hash(const unsigned char *sig, unsigned long siglen, err = dsa_verify_hash_raw(r, s, hash, hashlen, stat, key); LBL_ERR: - mp_clear_multi(r, s, LTC_NULL); + ltc_mp_deinit_multi(r, s, LTC_NULL); return err; } diff --git a/src/pk/dsa/dsa_verify_key.c b/src/pk/dsa/dsa_verify_key.c index ccf7c6a34..9beae6c66 100644 --- a/src/pk/dsa/dsa_verify_key.c +++ b/src/pk/dsa/dsa_verify_key.c @@ -49,22 +49,22 @@ int dsa_int_validate_pqg(const dsa_key *key, int *stat) /* check q-order */ if ( key->qord >= LTC_MDSA_MAX_GROUP || key->qord <= 15 || - (unsigned long)key->qord >= mp_unsigned_bin_size(key->p) || - (mp_unsigned_bin_size(key->p) - key->qord) >= LTC_MDSA_DELTA ) { + (unsigned long)key->qord >= ltc_mp_unsigned_bin_size(key->p) || + (ltc_mp_unsigned_bin_size(key->p) - key->qord) >= LTC_MDSA_DELTA ) { return CRYPT_OK; } /* FIPS 186-4 chapter 4.1: 1 < g < p */ - if (mp_cmp_d(key->g, 1) != LTC_MP_GT || mp_cmp(key->g, key->p) != LTC_MP_LT) { + if (ltc_mp_cmp_d(key->g, 1) != LTC_MP_GT || ltc_mp_cmp(key->g, key->p) != LTC_MP_LT) { return CRYPT_OK; } - if ((err = mp_init_multi(&tmp1, &tmp2, LTC_NULL)) != CRYPT_OK) { return err; } + if ((err = ltc_mp_init_multi(&tmp1, &tmp2, LTC_NULL)) != CRYPT_OK) { return err; } /* FIPS 186-4 chapter 4.1: q is a divisor of (p - 1) */ - if ((err = mp_sub_d(key->p, 1, tmp1)) != CRYPT_OK) { goto error; } - if ((err = mp_div(tmp1, key->q, tmp1, tmp2)) != CRYPT_OK) { goto error; } - if (mp_iszero(tmp2) != LTC_MP_YES) { + if ((err = ltc_mp_sub_d(key->p, 1, tmp1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_div(tmp1, key->q, tmp1, tmp2)) != CRYPT_OK) { goto error; } + if (ltc_mp_iszero(tmp2) != LTC_MP_YES) { err = CRYPT_OK; goto error; } @@ -72,8 +72,8 @@ int dsa_int_validate_pqg(const dsa_key *key, int *stat) /* FIPS 186-4 chapter 4.1: g is a generator of a subgroup of order q in * the multiplicative group of GF(p) - so we make sure that g^q mod p = 1 */ - if ((err = mp_exptmod(key->g, key->q, key->p, tmp1)) != CRYPT_OK) { goto error; } - if (mp_cmp_d(tmp1, 1) != LTC_MP_EQ) { + if ((err = ltc_mp_exptmod(key->g, key->q, key->p, tmp1)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp_d(tmp1, 1) != LTC_MP_EQ) { err = CRYPT_OK; goto error; } @@ -81,7 +81,7 @@ int dsa_int_validate_pqg(const dsa_key *key, int *stat) err = CRYPT_OK; *stat = 1; error: - mp_clear_multi(tmp2, tmp1, LTC_NULL); + ltc_mp_deinit_multi(tmp2, tmp1, LTC_NULL); return err; } @@ -101,7 +101,7 @@ int dsa_int_validate_primes(const dsa_key *key, int *stat) LTC_ARGCHK(stat != NULL); /* key->q prime? */ - if ((err = mp_prime_is_prime(key->q, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { + if ((err = ltc_mp_prime_is_prime(key->q, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { return err; } if (res == LTC_MP_NO) { @@ -109,7 +109,7 @@ int dsa_int_validate_primes(const dsa_key *key, int *stat) } /* key->p prime? */ - if ((err = mp_prime_is_prime(key->p, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { + if ((err = ltc_mp_prime_is_prime(key->p, LTC_MILLER_RABIN_REPS, &res)) != CRYPT_OK) { return err; } if (res == LTC_MP_NO) { @@ -137,28 +137,28 @@ int dsa_int_validate_xy(const dsa_key *key, int *stat) LTC_ARGCHK(stat != NULL); /* 1 < y < p-1 */ - if ((err = mp_init(&tmp)) != CRYPT_OK) { + if ((err = ltc_mp_init(&tmp)) != CRYPT_OK) { return err; } - if ((err = mp_sub_d(key->p, 1, tmp)) != CRYPT_OK) { + if ((err = ltc_mp_sub_d(key->p, 1, tmp)) != CRYPT_OK) { goto error; } - if (mp_cmp_d(key->y, 1) != LTC_MP_GT || mp_cmp(key->y, tmp) != LTC_MP_LT) { + if (ltc_mp_cmp_d(key->y, 1) != LTC_MP_GT || ltc_mp_cmp(key->y, tmp) != LTC_MP_LT) { err = CRYPT_OK; goto error; } if (key->type == PK_PRIVATE) { /* FIPS 186-4 chapter 4.1: 0 < x < q */ - if (mp_cmp_d(key->x, 0) != LTC_MP_GT || mp_cmp(key->x, key->q) != LTC_MP_LT) { + if (ltc_mp_cmp_d(key->x, 0) != LTC_MP_GT || ltc_mp_cmp(key->x, key->q) != LTC_MP_LT) { err = CRYPT_OK; goto error; } /* FIPS 186-4 chapter 4.1: y = g^x mod p */ - if ((err = mp_exptmod(key->g, key->x, key->p, tmp)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->g, key->x, key->p, tmp)) != CRYPT_OK) { goto error; } - if (mp_cmp(tmp, key->y) != LTC_MP_EQ) { + if (ltc_mp_cmp(tmp, key->y) != LTC_MP_EQ) { err = CRYPT_OK; goto error; } @@ -167,10 +167,10 @@ int dsa_int_validate_xy(const dsa_key *key, int *stat) /* with just a public key we cannot test y = g^x mod p therefore we * only test that y^q mod p = 1, which makes sure y is in g^x mod p */ - if ((err = mp_exptmod(key->y, key->q, key->p, tmp)) != CRYPT_OK) { + if ((err = ltc_mp_exptmod(key->y, key->q, key->p, tmp)) != CRYPT_OK) { goto error; } - if (mp_cmp_d(tmp, 1) != LTC_MP_EQ) { + if (ltc_mp_cmp_d(tmp, 1) != LTC_MP_EQ) { err = CRYPT_OK; goto error; } @@ -179,7 +179,7 @@ int dsa_int_validate_xy(const dsa_key *key, int *stat) err = CRYPT_OK; *stat = 1; error: - mp_clear(tmp); + ltc_mp_clear(tmp); return err; } diff --git a/src/pk/ecc/ecc_export_openssl.c b/src/pk/ecc/ecc_export_openssl.c index 2e8999680..35d83b706 100644 --- a/src/pk/ecc/ecc_export_openssl.c +++ b/src/pk/ecc/ecc_export_openssl.c @@ -53,15 +53,15 @@ int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, cons cofactor = key->dp.cofactor; /* curve param a */ - len_a = mp_unsigned_bin_size(a); + len_a = ltc_mp_unsigned_bin_size(a); if (len_a > sizeof(bin_a)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_to_unsigned_bin(a, bin_a)) != CRYPT_OK) { goto error; } if (len_a == 0) { len_a = 1; bin_a[0] = 0; } /* handle case a == 0 */ /* curve param b */ - len_b = mp_unsigned_bin_size(b); + len_b = ltc_mp_unsigned_bin_size(b); if (len_b > sizeof(bin_b)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_to_unsigned_bin(b, bin_b)) != CRYPT_OK) { goto error; } if (len_b == 0) { len_b = 1; bin_b[0] = 0; } /* handle case b == 0 */ /* base point - (un)compressed based on flag_com */ @@ -134,9 +134,9 @@ int ecc_export_openssl(unsigned char *out, unsigned long *outlen, int type, cons */ /* private key */ - len_k = mp_unsigned_bin_size(key->k); + len_k = ltc_mp_unsigned_bin_size(key->k); if (len_k > sizeof(bin_k)) { err = CRYPT_BUFFER_OVERFLOW; goto error; } - if ((err = mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_to_unsigned_bin(key->k, bin_k)) != CRYPT_OK) { goto error; } LTC_SET_ASN1(&pub_xy, 0, LTC_ASN1_RAW_BIT_STRING, bin_xy, 8*len_xy); LTC_SET_ASN1(seq_priv, 0, LTC_ASN1_SHORT_INTEGER, &one, 1); diff --git a/src/pk/ecc/ecc_free.c b/src/pk/ecc/ecc_free.c index 61331090a..fd954d62f 100644 --- a/src/pk/ecc/ecc_free.c +++ b/src/pk/ecc/ecc_free.c @@ -18,7 +18,7 @@ void ecc_free(ecc_key *key) { LTC_ARGCHKVD(key != NULL); - mp_cleanup_multi(&key->dp.prime, &key->dp.order, + ltc_mp_cleanup_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, diff --git a/src/pk/ecc/ecc_get_key.c b/src/pk/ecc/ecc_get_key.c index 891a74136..05586eff9 100644 --- a/src/pk/ecc/ecc_get_key.c +++ b/src/pk/ecc/ecc_get_key.c @@ -38,9 +38,9 @@ int ecc_get_key(unsigned char *out, unsigned long *outlen, int type, const ecc_k return CRYPT_BUFFER_OVERFLOW; } *outlen = size; - if ((ksize = mp_unsigned_bin_size(key->k)) > size) return CRYPT_BUFFER_OVERFLOW; + if ((ksize = ltc_mp_unsigned_bin_size(key->k)) > size) return CRYPT_BUFFER_OVERFLOW; /* pad and store k */ - if ((err = mp_to_unsigned_bin(key->k, out + (size - ksize))) != CRYPT_OK) return err; + if ((err = ltc_mp_to_unsigned_bin(key->k, out + (size - ksize))) != CRYPT_OK) return err; zeromem(out, size - ksize); } else { diff --git a/src/pk/ecc/ecc_import.c b/src/pk/ecc/ecc_import.c index 64bed21c9..48e050639 100644 --- a/src/pk/ecc/ecc_import.c +++ b/src/pk/ecc/ecc_import.c @@ -85,7 +85,7 @@ int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, co } /* set z */ - if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; } /* point on the curve + other checks */ if ((err = ltc_ecc_verify_key(key)) != CRYPT_OK) { goto done; } diff --git a/src/pk/ecc/ecc_import_openssl.c b/src/pk/ecc/ecc_import_openssl.c index 9008c73a7..36a1bac94 100644 --- a/src/pk/ecc/ecc_import_openssl.c +++ b/src/pk/ecc/ecc_import_openssl.c @@ -58,7 +58,7 @@ int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type unsigned long cofactor = 0, ecver = 0, pkver = 0, tmpoid[16]; int err; - if ((err = mp_init_multi(&prime, &order, &a, &b, &gx, &gy, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&prime, &order, &a, &b, &gx, &gy, LTC_NULL)) != CRYPT_OK) { return err; } @@ -101,8 +101,8 @@ int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type len_b = seq_curve[1].size; len_g = seq_ecparams[3].size; /* create bignums */ - if ((err = mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(a, bin_a, len_a)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(b, bin_b, len_b)) != CRYPT_OK) { goto error; } if ((err = ltc_ecc_import_point(bin_g, len_g, prime, a, b, gx, gy)) != CRYPT_OK) { goto error; } /* load curve parameters */ if ((err = ecc_set_curve_from_mpis(a, b, prime, order, gx, gy, cofactor, key)) != CRYPT_OK) { goto error; } @@ -117,7 +117,7 @@ int ecc_import_with_curve(const unsigned char *in, unsigned long inlen, int type } } error: - mp_clear_multi(prime, order, a, b, gx, gy, LTC_NULL); + ltc_mp_deinit_multi(prime, order, a, b, gx, gy, LTC_NULL); return err; } diff --git a/src/pk/ecc/ecc_import_pkcs8.c b/src/pk/ecc/ecc_import_pkcs8.c index f073d9845..f9c473b69 100644 --- a/src/pk/ecc/ecc_import_pkcs8.c +++ b/src/pk/ecc/ecc_import_pkcs8.c @@ -20,7 +20,7 @@ int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_ke LTC_ARGCHK(ltc_mp.name != NULL); /* init key */ - err = mp_init_multi(&a, &b, &gx, &gy, LTC_NULL); + err = ltc_mp_init_multi(&a, &b, &gx, &gy, LTC_NULL); if (err != CRYPT_OK) goto LBL_DER_FREE; /* Setup for CASE 2 */ @@ -68,10 +68,10 @@ int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_ke * 151:d=1 hl=2 l= 77 prim: OCTET STRING :bytes (== *priv_key) */ - if (mp_cmp_d(version->data, 1) != LTC_MP_EQ) { + if (ltc_mp_cmp_d(version->data, 1) != LTC_MP_EQ) { goto LBL_DONE; } - cofactor = mp_get_int(p_cofactor->data); + cofactor = ltc_mp_get_int(p_cofactor->data); if (LTC_ASN1_IS_TYPE(field->child, LTC_ASN1_OBJECT_IDENTIFIER) && LTC_ASN1_IS_TYPE(field->child->next, LTC_ASN1_INTEGER) && @@ -79,10 +79,10 @@ int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_ke LTC_ASN1_IS_TYPE(point->child->next, LTC_ASN1_OCTET_STRING)) { ltc_asn1_list *prime = field->child->next; - if ((err = mp_read_unsigned_bin(a, point->child->data, point->child->size)) != CRYPT_OK) { + if ((err = ltc_mp_read_unsigned_bin(a, point->child->data, point->child->size)) != CRYPT_OK) { goto LBL_DONE; } - if ((err = mp_read_unsigned_bin(b, point->child->next->data, point->child->next->size)) != CRYPT_OK) { + if ((err = ltc_mp_read_unsigned_bin(b, point->child->next->data, point->child->next->size)) != CRYPT_OK) { goto LBL_DONE; } if ((err = ltc_ecc_import_point(point_g->data, point_g->size, prime->data, a, b, gx, gy)) != CRYPT_OK) { @@ -104,7 +104,7 @@ int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_ke LTC_ASN1_IS_TYPE(p->child, LTC_ASN1_INTEGER) && LTC_ASN1_IS_TYPE(p->child->next, LTC_ASN1_OCTET_STRING)) { ltc_asn1_list *lk = p->child->next; - if (mp_cmp_d(p->child->data, 1) != LTC_MP_EQ) { + if (ltc_mp_cmp_d(p->child->data, 1) != LTC_MP_EQ) { err = CRYPT_INVALID_PACKET; goto LBL_ECCFREE; } @@ -120,7 +120,7 @@ int ecc_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key, ecc_ke LBL_ECCFREE: ecc_free(key); LBL_DONE: - mp_clear_multi(a, b, gx, gy, LTC_NULL); + ltc_mp_deinit_multi(a, b, gx, gy, LTC_NULL); LBL_DER_FREE: if (p) der_free_sequence_flexi(p); return err; diff --git a/src/pk/ecc/ecc_recover_key.c b/src/pk/ecc/ecc_recover_key.c index 8c7a8d9d7..a507a7867 100644 --- a/src/pk/ecc/ecc_recover_key.c +++ b/src/pk/ecc/ecc_recover_key.c @@ -46,7 +46,7 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, } /* allocate ints */ - if ((err = mp_init_multi(&r, &s, &v, &w, &t1, &t2, &u1, &u2, &v1, &v2, &e, &x, &y, &a_plus3, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&r, &s, &v, &w, &t1, &t2, &u1, &u2, &v1, &v2, &e, &x, &y, &a_plus3, LTC_NULL)) != CRYPT_OK) { return err; } @@ -54,7 +54,7 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, m = key->dp.prime; a = key->dp.A; b = key->dp.B; - if ((err = mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { + if ((err = ltc_mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { goto error; } @@ -76,13 +76,13 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, } else if (sigformat == LTC_ECCSIG_RFC7518) { /* RFC7518 format - raw (r,s) */ - i = mp_unsigned_bin_size(key->dp.order); + i = ltc_mp_unsigned_bin_size(key->dp.order); if (siglen != (2*i)) { err = CRYPT_INVALID_PACKET; goto error; } - if ((err = mp_read_unsigned_bin(r, sig, i)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(s, sig+i, i)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(r, sig, i)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(s, sig+i, i)) != CRYPT_OK) { goto error; } } else if (sigformat == LTC_ECCSIG_ETH27) { /* Ethereum (v,r,s) format */ @@ -102,8 +102,8 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, goto error; } recid = i; - if ((err = mp_read_unsigned_bin(r, sig, 32)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(s, sig+32, 32)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(r, sig, 32)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(s, sig+32, 32)) != CRYPT_OK) { goto error; } } #ifdef LTC_SSH else if (sigformat == LTC_ECCSIG_RFC5656) { @@ -140,20 +140,20 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, } /* check for zero */ - if (mp_cmp_d(r, 0) != LTC_MP_GT || mp_cmp_d(s, 0) != LTC_MP_GT || - mp_cmp(r, p) != LTC_MP_LT || mp_cmp(s, p) != LTC_MP_LT) { + if (ltc_mp_cmp_d(r, 0) != LTC_MP_GT || ltc_mp_cmp_d(s, 0) != LTC_MP_GT || + ltc_mp_cmp(r, p) != LTC_MP_LT || ltc_mp_cmp(s, p) != LTC_MP_LT) { err = CRYPT_INVALID_PACKET; goto error; } /* read hash - truncate if needed */ - pbits = mp_count_bits(p); + pbits = ltc_mp_count_bits(p); pbytes = (pbits+7) >> 3; if (pbits > hashlen*8) { - if ((err = mp_read_unsigned_bin(e, hash, hashlen)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(e, hash, hashlen)) != CRYPT_OK) { goto error; } } else if (pbits % 8 == 0) { - if ((err = mp_read_unsigned_bin(e, hash, pbytes)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(e, hash, pbytes)) != CRYPT_OK) { goto error; } } else { shift_right = 8 - pbits % 8; @@ -162,79 +162,79 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, ch = (hash[i] << (8-shift_right)); buf[i] = buf[i] ^ (hash[i] >> shift_right); } - if ((err = mp_read_unsigned_bin(e, buf, pbytes)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(e, buf, pbytes)) != CRYPT_OK) { goto error; } } /* decompress point from r=(x mod p) - BEWARE: requires sqrtmod_prime */ /* x = r + p*(recid/2) */ - if ((err = mp_set(x, recid/2)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(p, x, m, x)) != CRYPT_OK) { goto error; } - if ((err = mp_add(x, r, x)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(x, recid/2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(p, x, m, x)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add(x, r, x)) != CRYPT_OK) { goto error; } /* compute x^3 */ - if ((err = mp_sqr(x, t1)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(t1, x, m, t1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_sqr(x, t1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(t1, x, m, t1)) != CRYPT_OK) { goto error; } /* compute x^3 + a*x */ - if ((err = mp_mulmod(a, x, m, t2)) != CRYPT_OK) { goto error; } - if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(a, x, m, t2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add(t1, t2, t1)) != CRYPT_OK) { goto error; } /* compute x^3 + a*x + b */ - if ((err = mp_add(t1, b, t1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add(t1, b, t1)) != CRYPT_OK) { goto error; } /* compute sqrt(x^3 + a*x + b) */ - if ((err = mp_sqrtmod_prime(t1, m, t2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_sqrtmod_prime(t1, m, t2)) != CRYPT_OK) { goto error; } /* fill in mR */ - if ((err = mp_copy(x, mR->x)) != CRYPT_OK) { goto error; } - if ((mp_isodd(t2) && (recid%2)) || (!mp_isodd(t2) && !(recid%2))) { - if ((err = mp_mod(t2, m, mR->y)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(x, mR->x)) != CRYPT_OK) { goto error; } + if ((ltc_mp_isodd(t2) && (recid%2)) || (!ltc_mp_isodd(t2) && !(recid%2))) { + if ((err = ltc_mp_mod(t2, m, mR->y)) != CRYPT_OK) { goto error; } } else { - if ((err = mp_submod(m, t2, m, mR->y)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_submod(m, t2, m, mR->y)) != CRYPT_OK) { goto error; } } - if ((err = mp_set(mR->z, 1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(mR->z, 1)) != CRYPT_OK) { goto error; } /* w = r^-1 mod n */ - if ((err = mp_invmod(r, p, w)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_invmod(r, p, w)) != CRYPT_OK) { goto error; } /* v1 = sw */ - if ((err = mp_mulmod(s, w, p, v1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(s, w, p, v1)) != CRYPT_OK) { goto error; } /* v2 = -ew */ - if ((err = mp_mulmod(e, w, p, v2)) != CRYPT_OK) { goto error; } - if ((err = mp_submod(p, v2, p, v2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(e, w, p, v2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_submod(p, v2, p, v2)) != CRYPT_OK) { goto error; } /* w = s^-1 mod n */ - if ((err = mp_invmod(s, p, w)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_invmod(s, p, w)) != CRYPT_OK) { goto error; } /* u1 = ew */ - if ((err = mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; } /* u2 = rw */ - if ((err = mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; } /* find mG */ - if ((err = ltc_ecc_copy_point(&key->dp.base, mG)) != CRYPT_OK) { goto error; } + if ((err = ltc_ecc_copy_point(&key->dp.base, mG)) != CRYPT_OK) { goto error; } /* find the montgomery mp */ - if ((err = mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; } /* for curves with a == -3 keep ma == NULL */ - if (mp_cmp(a_plus3, m) != LTC_MP_EQ) { - if ((err = mp_init_multi(&mu, &ma, LTC_NULL)) != CRYPT_OK) { goto error; } - if ((err = mp_montgomery_normalization(mu, m)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(a, mu, m, ma)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp(a_plus3, m) != LTC_MP_EQ) { + if ((err = ltc_mp_init_multi(&mu, &ma, LTC_NULL)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_normalization(mu, m)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(a, mu, m, ma)) != CRYPT_OK) { goto error; } } /* recover mQ from mR */ /* compute v1*mR + v2*mG = mQ using Shamir's trick */ - if ((err = ltc_mp.ecc_mul2add(mR, v1, mG, v2, mQ, ma, m)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp.ecc_mul2add(mR, v1, mG, v2, mQ, ma, m)) != CRYPT_OK) { goto error; } /* compute u1*mG + u2*mQ = mG using Shamir's trick */ - if ((err = ltc_mp.ecc_mul2add(mG, u1, mQ, u2, mG, ma, m)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp.ecc_mul2add(mG, u1, mQ, u2, mG, ma, m)) != CRYPT_OK) { goto error; } /* v = X_x1 mod n */ - if ((err = mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; } /* does v == r */ - if (mp_cmp(v, r) == LTC_MP_EQ) { + if (ltc_mp_cmp(v, r) == LTC_MP_EQ) { /* found public key which verifies signature */ - if ((err = ltc_ecc_copy_point(mQ, &key->pubkey)) != CRYPT_OK) { goto error; } + if ((err = ltc_ecc_copy_point(mQ, &key->pubkey)) != CRYPT_OK) { goto error; } /* point on the curve + other checks */ - if ((err = ltc_ecc_verify_key(key)) != CRYPT_OK) { goto error; } + if ((err = ltc_ecc_verify_key(key)) != CRYPT_OK) { goto error; } key->type = PK_PUBLIC; @@ -246,13 +246,13 @@ int ecc_recover_key(const unsigned char *sig, unsigned long siglen, } error: - if (ma != NULL) mp_clear(ma); - if (mu != NULL) mp_clear(mu); - if (mp != NULL) mp_montgomery_free(mp); + if (ma != NULL) ltc_mp_clear(ma); + if (mu != NULL) ltc_mp_clear(mu); + if (mp != NULL) ltc_mp_montgomery_free(mp); if (mR != NULL) ltc_ecc_del_point(mR); if (mQ != NULL) ltc_ecc_del_point(mQ); if (mG != NULL) ltc_ecc_del_point(mG); - mp_clear_multi(a_plus3, y, x, e, v2, v1, u2, u1, t2, t1, w, v, s, r, LTC_NULL); + ltc_mp_deinit_multi(a_plus3, y, x, e, v2, v1, u2, u1, t2, t1, w, v, s, r, LTC_NULL); return err; } diff --git a/src/pk/ecc/ecc_set_curve.c b/src/pk/ecc/ecc_set_curve.c index 427a59371..7947c6ebb 100644 --- a/src/pk/ecc/ecc_set_curve.c +++ b/src/pk/ecc/ecc_set_curve.c @@ -12,7 +12,7 @@ int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key) LTC_ARGCHK(key != NULL); LTC_ARGCHK(cu != NULL); - if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, + if ((err = ltc_mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL)) != CRYPT_OK) { @@ -20,16 +20,16 @@ int ecc_set_curve(const ltc_ecc_curve *cu, ecc_key *key) } /* A, B, order, prime, Gx, Gy */ - if ((err = mp_read_radix(key->dp.prime, cu->prime, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.order, cu->order, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.A, cu->A, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.B, cu->B, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.base.x, cu->Gx, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(key->dp.base.y, cu->Gy, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(key->dp.prime, cu->prime, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(key->dp.order, cu->order, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(key->dp.A, cu->A, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(key->dp.B, cu->B, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(key->dp.base.x, cu->Gx, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(key->dp.base.y, cu->Gy, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } /* cofactor & size */ key->dp.cofactor = cu->cofactor; - key->dp.size = mp_unsigned_bin_size(key->dp.prime); + key->dp.size = ltc_mp_unsigned_bin_size(key->dp.prime); /* OID string >> unsigned long oid[16] + oidlen */ key->dp.oidlen = 16; if ((err = pk_oid_str_to_num(cu->OID, key->dp.oid, &key->dp.oidlen)) != CRYPT_OK) { goto error; } diff --git a/src/pk/ecc/ecc_set_curve_internal.c b/src/pk/ecc/ecc_set_curve_internal.c index 4f43914eb..e1538607a 100644 --- a/src/pk/ecc/ecc_set_curve_internal.c +++ b/src/pk/ecc/ecc_set_curve_internal.c @@ -7,8 +7,8 @@ static int s_ecc_cmp_hex_bn(const char *left_hex, void *right_bn, void *tmp_bn) { - if (mp_read_radix(tmp_bn, left_hex, 16) != CRYPT_OK) return 0; - if (mp_cmp(tmp_bn, right_bn) != LTC_MP_EQ) return 0; + if (ltc_mp_read_radix(tmp_bn, left_hex, 16) != CRYPT_OK) return 0; + if (ltc_mp_cmp(tmp_bn, right_bn) != LTC_MP_EQ) return 0; return 1; } @@ -18,7 +18,7 @@ static void s_ecc_oid_lookup(ecc_key *key) const ltc_ecc_curve *curve; key->dp.oidlen = 0; - if (mp_init(&bn) != CRYPT_OK) return; + if (ltc_mp_init(&bn) != CRYPT_OK) return; for (curve = ltc_ecc_curves; curve->prime != NULL; curve++) { if (s_ecc_cmp_hex_bn(curve->prime, key->dp.prime, bn) != 1) continue; if (s_ecc_cmp_hex_bn(curve->order, key->dp.order, bn) != 1) continue; @@ -29,7 +29,7 @@ static void s_ecc_oid_lookup(ecc_key *key) if (key->dp.cofactor != curve->cofactor) continue; break; /* found */ } - mp_clear(bn); + ltc_mp_clear(bn); if (curve->prime && curve->OID) { key->dp.oidlen = 16; /* size of key->dp.oid */ pk_oid_str_to_num(curve->OID, key->dp.oid, &key->dp.oidlen); @@ -44,7 +44,7 @@ int ecc_copy_curve(const ecc_key *srckey, ecc_key *key) LTC_ARGCHK(key != NULL); LTC_ARGCHK(srckey != NULL); - if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, + if ((err = ltc_mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL)) != CRYPT_OK) { @@ -52,10 +52,10 @@ int ecc_copy_curve(const ecc_key *srckey, ecc_key *key) } /* A, B, order, prime, Gx, Gy */ - if ((err = mp_copy(srckey->dp.prime, key->dp.prime )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(srckey->dp.order, key->dp.order )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(srckey->dp.A, key->dp.A )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(srckey->dp.B, key->dp.B )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(srckey->dp.prime, key->dp.prime )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(srckey->dp.order, key->dp.order )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(srckey->dp.A, key->dp.A )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(srckey->dp.B, key->dp.B )) != CRYPT_OK) { goto error; } if ((err = ltc_ecc_copy_point(&srckey->dp.base, &key->dp.base)) != CRYPT_OK) { goto error; } /* cofactor & size */ key->dp.cofactor = srckey->dp.cofactor; @@ -88,7 +88,7 @@ int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx LTC_ARGCHK(gx != NULL); LTC_ARGCHK(gy != NULL); - if ((err = mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, + if ((err = ltc_mp_init_multi(&key->dp.prime, &key->dp.order, &key->dp.A, &key->dp.B, &key->dp.base.x, &key->dp.base.y, &key->dp.base.z, &key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL)) != CRYPT_OK) { @@ -96,16 +96,16 @@ int ecc_set_curve_from_mpis(void *a, void *b, void *prime, void *order, void *gx } /* A, B, order, prime, Gx, Gy */ - if ((err = mp_copy(prime, key->dp.prime )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(order, key->dp.order )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(a, key->dp.A )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(b, key->dp.B )) != CRYPT_OK) { goto error; } - if ((err = mp_copy(gx, key->dp.base.x)) != CRYPT_OK) { goto error; } - if ((err = mp_copy(gy, key->dp.base.y)) != CRYPT_OK) { goto error; } - if ((err = mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(prime, key->dp.prime )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(order, key->dp.order )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(a, key->dp.A )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(b, key->dp.B )) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(gx, key->dp.base.x)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_copy(gy, key->dp.base.y)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(key->dp.base.z, 1)) != CRYPT_OK) { goto error; } /* cofactor & size */ key->dp.cofactor = cofactor; - key->dp.size = mp_unsigned_bin_size(prime); + key->dp.size = ltc_mp_unsigned_bin_size(prime); /* try to find OID in ltc_ecc_curves */ s_ecc_oid_lookup(key); /* success */ diff --git a/src/pk/ecc/ecc_set_key.c b/src/pk/ecc/ecc_set_key.c index c6e58219e..2b475808c 100644 --- a/src/pk/ecc/ecc_set_key.c +++ b/src/pk/ecc/ecc_set_key.c @@ -20,10 +20,10 @@ int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key if (type == PK_PRIVATE) { /* load private key */ - if ((err = mp_read_unsigned_bin(key->k, in, inlen)) != CRYPT_OK) { + if ((err = ltc_mp_read_unsigned_bin(key->k, in, inlen)) != CRYPT_OK) { goto error; } - if (mp_iszero(key->k) || (mp_cmp(key->k, key->dp.order) != LTC_MP_LT)) { + if (ltc_mp_iszero(key->k) || (ltc_mp_cmp(key->k, key->dp.order) != LTC_MP_LT)) { err = CRYPT_INVALID_PACKET; goto error; } @@ -33,7 +33,7 @@ int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key else if (type == PK_PUBLIC) { /* load public key */ if ((err = ltc_ecc_import_point(in, inlen, prime, a, b, key->pubkey.x, key->pubkey.y)) != CRYPT_OK) { goto error; } - if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto error; } } else { err = CRYPT_INVALID_PACKET; diff --git a/src/pk/ecc/ecc_shared_secret.c b/src/pk/ecc/ecc_shared_secret.c index 21ac91346..df879dc23 100644 --- a/src/pk/ecc/ecc_shared_secret.c +++ b/src/pk/ecc/ecc_shared_secret.c @@ -47,14 +47,14 @@ int ecc_shared_secret(const ecc_key *private_key, const ecc_key *public_key, if ((err = ltc_mp.ecc_ptmul(private_key->k, &public_key->pubkey, result, a, prime, 1)) != CRYPT_OK) { goto done; } - x = (unsigned long)mp_unsigned_bin_size(prime); + x = (unsigned long)ltc_mp_unsigned_bin_size(prime); if (*outlen < x) { *outlen = x; err = CRYPT_BUFFER_OVERFLOW; goto done; } zeromem(out, x); - if ((err = mp_to_unsigned_bin(result->x, out + (x - mp_unsigned_bin_size(result->x)))) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_to_unsigned_bin(result->x, out + (x - ltc_mp_unsigned_bin_size(result->x)))) != CRYPT_OK) { goto done; } err = CRYPT_OK; *outlen = x; diff --git a/src/pk/ecc/ecc_sign_hash.c b/src/pk/ecc/ecc_sign_hash.c index 6673777da..b50fa4f12 100644 --- a/src/pk/ecc/ecc_sign_hash.c +++ b/src/pk/ecc/ecc_sign_hash.c @@ -46,19 +46,19 @@ int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen, } /* init the bignums */ - if ((err = mp_init_multi(&r, &s, &e, &b, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&r, &s, &e, &b, LTC_NULL)) != CRYPT_OK) { return err; } /* get the hash and load it as a bignum into 'e' */ p = key->dp.order; - pbits = mp_count_bits(p); + pbits = ltc_mp_count_bits(p); pbytes = (pbits+7) >> 3; if (pbits > inlen*8) { - if ((err = mp_read_unsigned_bin(e, in, inlen)) != CRYPT_OK) { goto errnokey; } + if ((err = ltc_mp_read_unsigned_bin(e, in, inlen)) != CRYPT_OK) { goto errnokey; } } else if (pbits % 8 == 0) { - if ((err = mp_read_unsigned_bin(e, in, pbytes)) != CRYPT_OK) { goto errnokey; } + if ((err = ltc_mp_read_unsigned_bin(e, in, pbytes)) != CRYPT_OK) { goto errnokey; } } else { shift_right = 8 - pbits % 8; @@ -67,7 +67,7 @@ int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen, ch = (in[i] << (8-shift_right)); buf[i] = buf[i] ^ (in[i] >> shift_right); } - if ((err = mp_read_unsigned_bin(e, buf, pbytes)) != CRYPT_OK) { goto errnokey; } + if ((err = ltc_mp_read_unsigned_bin(e, buf, pbytes)) != CRYPT_OK) { goto errnokey; } } /* make up a key and export the public copy */ @@ -76,34 +76,34 @@ int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen, if ((err = ecc_generate_key(prng, wprng, &pubkey)) != CRYPT_OK) { goto errnokey; } /* find r = x1 mod n */ - if ((err = mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; } if (recid || sigformat==LTC_ECCSIG_ETH27) { /* find recovery ID (if needed) */ v = 0; - if (mp_copy(pubkey.pubkey.x, s) != CRYPT_OK) { goto error; } - while (mp_cmp_d(s, 0) == LTC_MP_GT && mp_cmp(s, p) != LTC_MP_LT) { + if (ltc_mp_copy(pubkey.pubkey.x, s) != CRYPT_OK) { goto error; } + while (ltc_mp_cmp_d(s, 0) == LTC_MP_GT && ltc_mp_cmp(s, p) != LTC_MP_LT) { /* Compute x1 div n... this will almost never be reached for curves with order 1 */ v += 2; - if ((err = mp_sub(s, p, s)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_sub(s, p, s)) != CRYPT_OK) { goto error; } } - if (mp_isodd(pubkey.pubkey.y)) v += 1; + if (ltc_mp_isodd(pubkey.pubkey.y)) v += 1; } - if (mp_iszero(r) == LTC_MP_YES) { + if (ltc_mp_iszero(r) == LTC_MP_YES) { ecc_free(&pubkey); } else { if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */ /* find s = (e + xr)/k */ - if ((err = mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */ - if ((err = mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */ - if ((err = mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */ - if ((err = mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */ - if ((err = mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */ - if ((err = mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */ - if ((err = mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */ + if ((err = ltc_mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */ + if ((err = ltc_mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */ + if ((err = ltc_mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */ + if ((err = ltc_mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */ + if ((err = ltc_mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */ + if ((err = ltc_mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */ + if ((err = ltc_mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */ ecc_free(&pubkey); - if (mp_iszero(s) == LTC_MP_NO) { + if (ltc_mp_iszero(s) == LTC_MP_NO) { break; } } @@ -126,10 +126,10 @@ int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen, /* RFC7518 format - raw (r,s) */ if (*outlen < 2*pbytes) { err = CRYPT_MEM; goto errnokey; } zeromem(out, 2*pbytes); - i = mp_unsigned_bin_size(r); - if ((err = mp_to_unsigned_bin(r, out + (pbytes - i))) != CRYPT_OK) { goto errnokey; } - i = mp_unsigned_bin_size(s); - if ((err = mp_to_unsigned_bin(s, out + (2*pbytes - i))) != CRYPT_OK) { goto errnokey; } + i = ltc_mp_unsigned_bin_size(r); + if ((err = ltc_mp_to_unsigned_bin(r, out + (pbytes - i))) != CRYPT_OK) { goto errnokey; } + i = ltc_mp_unsigned_bin_size(s); + if ((err = ltc_mp_to_unsigned_bin(s, out + (2*pbytes - i))) != CRYPT_OK) { goto errnokey; } *outlen = 2*pbytes; err = CRYPT_OK; } @@ -141,10 +141,10 @@ int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen, } if (*outlen < 65) { err = CRYPT_MEM; goto errnokey; } zeromem(out, 65); - i = mp_unsigned_bin_size(r); - if ((err = mp_to_unsigned_bin(r, out + 32 - i)) != CRYPT_OK) { goto errnokey; } - i = mp_unsigned_bin_size(s); - if ((err = mp_to_unsigned_bin(s, out + 64 - i)) != CRYPT_OK) { goto errnokey; } + i = ltc_mp_unsigned_bin_size(r); + if ((err = ltc_mp_to_unsigned_bin(r, out + 32 - i)) != CRYPT_OK) { goto errnokey; } + i = ltc_mp_unsigned_bin_size(s); + if ((err = ltc_mp_to_unsigned_bin(s, out + 64 - i)) != CRYPT_OK) { goto errnokey; } out[64] = (unsigned char)(v + 27); /* Recovery ID is 27/28 for Ethereum */ *outlen = 65; err = CRYPT_OK; @@ -174,7 +174,7 @@ int ecc_sign_hash_ex(const unsigned char *in, unsigned long inlen, error: ecc_free(&pubkey); errnokey: - mp_clear_multi(r, s, e, b, LTC_NULL); + ltc_mp_deinit_multi(r, s, e, b, LTC_NULL); return err; } diff --git a/src/pk/ecc/ecc_sizes.c b/src/pk/ecc/ecc_sizes.c index 07379b69e..ed8cfc965 100644 --- a/src/pk/ecc/ecc_sizes.c +++ b/src/pk/ecc/ecc_sizes.c @@ -21,15 +21,15 @@ void ecc_sizes(int *low, int *high) *low = INT_MAX; *high = 0; - if (mp_init(&prime) == CRYPT_OK) { + if (ltc_mp_init(&prime) == CRYPT_OK) { for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) { - if (mp_read_radix(prime, ltc_ecc_curves[i].prime, 16) == CRYPT_OK) { - size = mp_unsigned_bin_size(prime); + if (ltc_mp_read_radix(prime, ltc_ecc_curves[i].prime, 16) == CRYPT_OK) { + size = ltc_mp_unsigned_bin_size(prime); if (size < *low) *low = size; if (size > *high) *high = size; } } - mp_clear(prime); + ltc_mp_clear(prime); } } diff --git a/src/pk/ecc/ecc_verify_hash.c b/src/pk/ecc/ecc_verify_hash.c index 7f5730d5c..6c74ca448 100644 --- a/src/pk/ecc/ecc_verify_hash.c +++ b/src/pk/ecc/ecc_verify_hash.c @@ -42,14 +42,14 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, *stat = 0; /* allocate ints */ - if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &e, &a_plus3, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&r, &s, &v, &w, &u1, &u2, &e, &a_plus3, LTC_NULL)) != CRYPT_OK) { return err; } p = key->dp.order; m = key->dp.prime; a = key->dp.A; - if ((err = mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { + if ((err = ltc_mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { goto error; } @@ -70,13 +70,13 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, } else if (sigformat == LTC_ECCSIG_RFC7518) { /* RFC7518 format - raw (r,s) */ - i = mp_unsigned_bin_size(key->dp.order); + i = ltc_mp_unsigned_bin_size(key->dp.order); if (siglen != (2 * i)) { err = CRYPT_INVALID_PACKET; goto error; } - if ((err = mp_read_unsigned_bin(r, sig, i)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(s, sig+i, i)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(r, sig, i)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(s, sig+i, i)) != CRYPT_OK) { goto error; } } else if (sigformat == LTC_ECCSIG_ETH27) { /* Ethereum (v,r,s) format */ @@ -88,8 +88,8 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, err = CRYPT_INVALID_PACKET; goto error; } - if ((err = mp_read_unsigned_bin(r, sig, 32)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(s, sig+32, 32)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(r, sig, 32)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(s, sig+32, 32)) != CRYPT_OK) { goto error; } } #ifdef LTC_SSH else if (sigformat == LTC_ECCSIG_RFC5656) { @@ -120,20 +120,20 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, } /* check for zero */ - if (mp_cmp_d(r, 0) != LTC_MP_GT || mp_cmp_d(s, 0) != LTC_MP_GT || - mp_cmp(r, p) != LTC_MP_LT || mp_cmp(s, p) != LTC_MP_LT) { + if (ltc_mp_cmp_d(r, 0) != LTC_MP_GT || ltc_mp_cmp_d(s, 0) != LTC_MP_GT || + ltc_mp_cmp(r, p) != LTC_MP_LT || ltc_mp_cmp(s, p) != LTC_MP_LT) { err = CRYPT_INVALID_PACKET; goto error; } /* read hash - truncate if needed */ - pbits = mp_count_bits(p); + pbits = ltc_mp_count_bits(p); pbytes = (pbits+7) >> 3; if (pbits > hashlen*8) { - if ((err = mp_read_unsigned_bin(e, hash, hashlen)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(e, hash, hashlen)) != CRYPT_OK) { goto error; } } else if (pbits % 8 == 0) { - if ((err = mp_read_unsigned_bin(e, hash, pbytes)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(e, hash, pbytes)) != CRYPT_OK) { goto error; } } else { shift_right = 8 - pbits % 8; @@ -142,30 +142,30 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, ch = (hash[i] << (8-shift_right)); buf[i] = buf[i] ^ (hash[i] >> shift_right); } - if ((err = mp_read_unsigned_bin(e, buf, pbytes)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(e, buf, pbytes)) != CRYPT_OK) { goto error; } } /* w = s^-1 mod n */ - if ((err = mp_invmod(s, p, w)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_invmod(s, p, w)) != CRYPT_OK) { goto error; } /* u1 = ew */ - if ((err = mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; } /* u2 = rw */ - if ((err = mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; } /* find mG and mQ */ if ((err = ltc_ecc_copy_point(&key->dp.base, mG)) != CRYPT_OK) { goto error; } if ((err = ltc_ecc_copy_point(&key->pubkey, mQ)) != CRYPT_OK) { goto error; } /* find the montgomery mp */ - if ((err = mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; } /* for curves with a == -3 keep ma == NULL */ - if (mp_cmp(a_plus3, m) != LTC_MP_EQ) { - if ((err = mp_init_multi(&mu, &ma, LTC_NULL)) != CRYPT_OK) { goto error; } - if ((err = mp_montgomery_normalization(mu, m)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(a, mu, m, ma)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp(a_plus3, m) != LTC_MP_EQ) { + if ((err = ltc_mp_init_multi(&mu, &ma, LTC_NULL)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_normalization(mu, m)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(a, mu, m, ma)) != CRYPT_OK) { goto error; } } /* compute u1*mG + u2*mQ = mG */ @@ -184,10 +184,10 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, } /* v = X_x1 mod n */ - if ((err = mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; } /* does v == r */ - if (mp_cmp(v, r) == LTC_MP_EQ) { + if (ltc_mp_cmp(v, r) == LTC_MP_EQ) { *stat = 1; } @@ -196,10 +196,10 @@ int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, error: if (mG != NULL) ltc_ecc_del_point(mG); if (mQ != NULL) ltc_ecc_del_point(mQ); - if (mu != NULL) mp_clear(mu); - if (ma != NULL) mp_clear(ma); - mp_clear_multi(r, s, v, w, u1, u2, e, a_plus3, LTC_NULL); - if (mp != NULL) mp_montgomery_free(mp); + if (mu != NULL) ltc_mp_clear(mu); + if (ma != NULL) ltc_mp_clear(ma); + ltc_mp_deinit_multi(r, s, v, w, u1, u2, e, a_plus3, LTC_NULL); + if (mp != NULL) ltc_mp_montgomery_free(mp); return err; } diff --git a/src/pk/ecc/ltc_ecc_export_point.c b/src/pk/ecc/ltc_ecc_export_point.c index 022c6bc9f..1586633b0 100644 --- a/src/pk/ecc/ltc_ecc_export_point.c +++ b/src/pk/ecc/ltc_ecc_export_point.c @@ -12,8 +12,8 @@ int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, voi unsigned long xsize, ysize; if (size > sizeof(buf)) return CRYPT_BUFFER_OVERFLOW; - if ((xsize = mp_unsigned_bin_size(x)) > size) return CRYPT_BUFFER_OVERFLOW; - if ((ysize = mp_unsigned_bin_size(y)) > size) return CRYPT_BUFFER_OVERFLOW; + if ((xsize = ltc_mp_unsigned_bin_size(x)) > size) return CRYPT_BUFFER_OVERFLOW; + if ((ysize = ltc_mp_unsigned_bin_size(y)) > size) return CRYPT_BUFFER_OVERFLOW; if(compressed) { if (*outlen < (1 + size)) { @@ -21,10 +21,10 @@ int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, voi return CRYPT_BUFFER_OVERFLOW; } /* store first byte */ - out[0] = mp_isodd(y) ? 0x03 : 0x02; + out[0] = ltc_mp_isodd(y) ? 0x03 : 0x02; /* pad and store x */ zeromem(buf, sizeof(buf)); - if ((err = mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err; + if ((err = ltc_mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err; XMEMCPY(out+1, buf, size); /* adjust outlen */ *outlen = 1 + size; @@ -38,11 +38,11 @@ int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, voi out[0] = 0x04; /* pad and store x */ zeromem(buf, sizeof(buf)); - if ((err = mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err; + if ((err = ltc_mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err; XMEMCPY(out+1, buf, size); /* pad and store y */ zeromem(buf, sizeof(buf)); - if ((err = mp_to_unsigned_bin(y, buf + (size - ysize))) != CRYPT_OK) return err; + if ((err = ltc_mp_to_unsigned_bin(y, buf + (size - ysize))) != CRYPT_OK) return err; XMEMCPY(out+1+size, buf, size); /* adjust outlen */ *outlen = 1 + 2*size; diff --git a/src/pk/ecc/ltc_ecc_import_point.c b/src/pk/ecc/ltc_ecc_import_point.c index cc7b7fa49..d16301f58 100644 --- a/src/pk/ecc/ltc_ecc_import_point.c +++ b/src/pk/ecc/ltc_ecc_import_point.c @@ -12,39 +12,39 @@ int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *pri void *t1, *t2; /* init key + temporary numbers */ - if (mp_init_multi(&t1, &t2, LTC_NULL) != CRYPT_OK) { + if (ltc_mp_init_multi(&t1, &t2, LTC_NULL) != CRYPT_OK) { return CRYPT_MEM; } - size = mp_unsigned_bin_size(prime); + size = ltc_mp_unsigned_bin_size(prime); if (in[0] == 0x04 && (inlen&1) && ((inlen-1)>>1) == size) { /* read uncompressed point */ /* load x */ - if ((err = mp_read_unsigned_bin(x, in+1, size)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_read_unsigned_bin(x, in+1, size)) != CRYPT_OK) { goto cleanup; } /* load y */ - if ((err = mp_read_unsigned_bin(y, in+1+size, size)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_read_unsigned_bin(y, in+1+size, size)) != CRYPT_OK) { goto cleanup; } } else if ((in[0] == 0x02 || in[0] == 0x03) && (inlen-1) == size && ltc_mp.sqrtmod_prime != NULL) { /* read compressed point - BEWARE: requires sqrtmod_prime */ /* load x */ - if ((err = mp_read_unsigned_bin(x, in+1, size)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_read_unsigned_bin(x, in+1, size)) != CRYPT_OK) { goto cleanup; } /* compute x^3 */ - if ((err = mp_sqr(x, t1)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_mulmod(t1, x, prime, t1)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_sqr(x, t1)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_mulmod(t1, x, prime, t1)) != CRYPT_OK) { goto cleanup; } /* compute x^3 + a*x */ - if ((err = mp_mulmod(a, x, prime, t2)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_mulmod(a, x, prime, t2)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_add(t1, t2, t1)) != CRYPT_OK) { goto cleanup; } /* compute x^3 + a*x + b */ - if ((err = mp_add(t1, b, t1)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_add(t1, b, t1)) != CRYPT_OK) { goto cleanup; } /* compute sqrt(x^3 + a*x + b) */ - if ((err = mp_sqrtmod_prime(t1, prime, t2)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_sqrtmod_prime(t1, prime, t2)) != CRYPT_OK) { goto cleanup; } /* adjust y */ - if ((mp_isodd(t2) && in[0] == 0x03) || (!mp_isodd(t2) && in[0] == 0x02)) { - if ((err = mp_mod(t2, prime, y)) != CRYPT_OK) { goto cleanup; } + if ((ltc_mp_isodd(t2) && in[0] == 0x03) || (!ltc_mp_isodd(t2) && in[0] == 0x02)) { + if ((err = ltc_mp_mod(t2, prime, y)) != CRYPT_OK) { goto cleanup; } } else { - if ((err = mp_submod(prime, t2, prime, y)) != CRYPT_OK) { goto cleanup; } + if ((err = ltc_mp_submod(prime, t2, prime, y)) != CRYPT_OK) { goto cleanup; } } } else { @@ -54,7 +54,7 @@ int ltc_ecc_import_point(const unsigned char *in, unsigned long inlen, void *pri err = CRYPT_OK; cleanup: - mp_clear_multi(t1, t2, LTC_NULL); + ltc_mp_deinit_multi(t1, t2, LTC_NULL); return err; } diff --git a/src/pk/ecc/ltc_ecc_is_point.c b/src/pk/ecc/ltc_ecc_is_point.c index 56a9537dc..9b5581467 100644 --- a/src/pk/ecc/ltc_ecc_is_point.c +++ b/src/pk/ecc/ltc_ecc_is_point.c @@ -21,41 +21,41 @@ int ltc_ecc_is_point(const ltc_ecc_dp *dp, void *x, void *y) b = dp->B; a = dp->A; - if ((err = mp_init_multi(&t1, &t2, LTC_NULL)) != CRYPT_OK) return err; + if ((err = ltc_mp_init_multi(&t1, &t2, LTC_NULL)) != CRYPT_OK) return err; /* compute y^2 */ - if ((err = mp_sqr(y, t1)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_sqr(y, t1)) != CRYPT_OK) goto cleanup; /* compute x^3 */ - if ((err = mp_sqr(x, t2)) != CRYPT_OK) goto cleanup; - if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) goto cleanup; - if ((err = mp_mul(x, t2, t2)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_sqr(x, t2)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_mod(t2, prime, t2)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_mul(x, t2, t2)) != CRYPT_OK) goto cleanup; /* compute y^2 - x^3 */ - if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_sub(t1, t2, t1)) != CRYPT_OK) goto cleanup; /* compute y^2 - x^3 - a*x */ - if ((err = mp_submod(prime, a, prime, t2)) != CRYPT_OK) goto cleanup; - if ((err = mp_mulmod(t2, x, prime, t2)) != CRYPT_OK) goto cleanup; - if ((err = mp_addmod(t1, t2, prime, t1)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_submod(prime, a, prime, t2)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_mulmod(t2, x, prime, t2)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_addmod(t1, t2, prime, t1)) != CRYPT_OK) goto cleanup; /* adjust range (0, prime) */ - while (mp_cmp_d(t1, 0) == LTC_MP_LT) { - if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) goto cleanup; + while (ltc_mp_cmp_d(t1, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(t1, prime, t1)) != CRYPT_OK) goto cleanup; } - while (mp_cmp(t1, prime) != LTC_MP_LT) { - if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) goto cleanup; + while (ltc_mp_cmp(t1, prime) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, prime, t1)) != CRYPT_OK) goto cleanup; } /* compare to b */ - if (mp_cmp(t1, b) != LTC_MP_EQ) { + if (ltc_mp_cmp(t1, b) != LTC_MP_EQ) { err = CRYPT_INVALID_PACKET; } else { err = CRYPT_OK; } cleanup: - mp_clear_multi(t1, t2, LTC_NULL); + ltc_mp_deinit_multi(t1, t2, LTC_NULL); return err; } diff --git a/src/pk/ecc/ltc_ecc_is_point_at_infinity.c b/src/pk/ecc/ltc_ecc_is_point_at_infinity.c index cf0baf970..1a4f79613 100644 --- a/src/pk/ecc/ltc_ecc_is_point_at_infinity.c +++ b/src/pk/ecc/ltc_ecc_is_point_at_infinity.c @@ -15,37 +15,37 @@ int ltc_ecc_is_point_at_infinity(const ecc_point *P, const void *modulus, int *r void *x3, *y2; /* trivial case */ - if (!mp_iszero(P->z)) { + if (!ltc_mp_iszero(P->z)) { *retval = 0; return CRYPT_OK; } /* point (0,0,0) is not at infinity */ - if (mp_iszero(P->x) && mp_iszero(P->y)) { + if (ltc_mp_iszero(P->x) && ltc_mp_iszero(P->y)) { *retval = 0; return CRYPT_OK; } /* initialize */ - if ((err = mp_init_multi(&x3, &y2, LTC_NULL)) != CRYPT_OK) goto done; + if ((err = ltc_mp_init_multi(&x3, &y2, LTC_NULL)) != CRYPT_OK) goto done; /* compute y^2 */ - if ((err = mp_mulmod(P->y, P->y, modulus, y2)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_mulmod(P->y, P->y, modulus, y2)) != CRYPT_OK) goto cleanup; /* compute x^3 */ - if ((err = mp_mulmod(P->x, P->x, modulus, x3)) != CRYPT_OK) goto cleanup; - if ((err = mp_mulmod(P->x, x3, modulus, x3)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_mulmod(P->x, P->x, modulus, x3)) != CRYPT_OK) goto cleanup; + if ((err = ltc_mp_mulmod(P->x, x3, modulus, x3)) != CRYPT_OK) goto cleanup; /* test y^2 == x^3 */ err = CRYPT_OK; - if ((mp_cmp(x3, y2) == LTC_MP_EQ) && !mp_iszero(y2)) { + if ((ltc_mp_cmp(x3, y2) == LTC_MP_EQ) && !ltc_mp_iszero(y2)) { *retval = 1; } else { *retval = 0; } cleanup: - mp_clear_multi(x3, y2, LTC_NULL); + ltc_mp_deinit_multi(x3, y2, LTC_NULL); done: return err; } diff --git a/src/pk/ecc/ltc_ecc_map.c b/src/pk/ecc/ltc_ecc_map.c index 6214739b2..228978d02 100644 --- a/src/pk/ecc/ltc_ecc_map.c +++ b/src/pk/ecc/ltc_ecc_map.c @@ -26,36 +26,36 @@ int ltc_ecc_map(ecc_point *P, const void *modulus, void *mp) LTC_ARGCHK(modulus != NULL); LTC_ARGCHK(mp != NULL); - if (mp_iszero(P->z)) { + if (ltc_mp_iszero(P->z)) { return ltc_ecc_set_point_xyz(0, 0, 1, P); } - if ((err = mp_init_multi(&t1, &t2, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&t1, &t2, LTC_NULL)) != CRYPT_OK) { return err; } /* first map z back to normal */ - if ((err = mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(P->z, modulus, mp)) != CRYPT_OK) { goto done; } /* get 1/z */ - if ((err = mp_invmod(P->z, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_invmod(P->z, modulus, t1)) != CRYPT_OK) { goto done; } /* get 1/z^2 and 1/z^3 */ - if ((err = mp_sqr(t1, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_mod(t2, modulus, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_mul(t1, t2, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_mod(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(t1, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mod(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t1, t2, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mod(t1, modulus, t1)) != CRYPT_OK) { goto done; } /* multiply against x/y */ - if ((err = mp_mul(P->x, t2, P->x)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK) { goto done; } - if ((err = mp_mul(P->y, t1, P->y)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK) { goto done; } - if ((err = mp_set(P->z, 1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(P->x, t2, P->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(P->x, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(P->y, t1, P->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(P->y, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_set(P->z, 1)) != CRYPT_OK) { goto done; } err = CRYPT_OK; done: - mp_clear_multi(t1, t2, LTC_NULL); + ltc_mp_deinit_multi(t1, t2, LTC_NULL); return err; } diff --git a/src/pk/ecc/ltc_ecc_mul2add.c b/src/pk/ecc/ltc_ecc_mul2add.c index ade971aad..d593efab0 100644 --- a/src/pk/ecc/ltc_ecc_mul2add.c +++ b/src/pk/ecc/ltc_ecc_mul2add.c @@ -55,8 +55,8 @@ int ltc_ecc_mul2add(const ecc_point *A, void *kA, } /* get sizes */ - lenA = mp_unsigned_bin_size(kA); - lenB = mp_unsigned_bin_size(kB); + lenA = ltc_mp_unsigned_bin_size(kA); + lenB = ltc_mp_unsigned_bin_size(kB); len = MAX(lenA, lenB); /* sanity check */ @@ -66,10 +66,10 @@ int ltc_ecc_mul2add(const ecc_point *A, void *kA, } /* extract and justify kA */ - mp_to_unsigned_bin(kA, (len - lenA) + tA); + ltc_mp_to_unsigned_bin(kA, (len - lenA) + tA); /* extract and justify kB */ - mp_to_unsigned_bin(kB, (len - lenB) + tB); + ltc_mp_to_unsigned_bin(kB, (len - lenB) + tB); /* allocate the table */ for (x = 0; x < 16; x++) { @@ -84,24 +84,24 @@ int ltc_ecc_mul2add(const ecc_point *A, void *kA, } /* init montgomery reduction */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto ERR_P; } - if ((err = mp_init(&mu)) != CRYPT_OK) { + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto ERR_MP; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto ERR_MU; } /* copy ones ... */ - if ((err = mp_mulmod(A->x, mu, modulus, precomp[1]->x)) != CRYPT_OK) { goto ERR_MU; } - if ((err = mp_mulmod(A->y, mu, modulus, precomp[1]->y)) != CRYPT_OK) { goto ERR_MU; } - if ((err = mp_mulmod(A->z, mu, modulus, precomp[1]->z)) != CRYPT_OK) { goto ERR_MU; } + if ((err = ltc_mp_mulmod(A->x, mu, modulus, precomp[1]->x)) != CRYPT_OK) { goto ERR_MU; } + if ((err = ltc_mp_mulmod(A->y, mu, modulus, precomp[1]->y)) != CRYPT_OK) { goto ERR_MU; } + if ((err = ltc_mp_mulmod(A->z, mu, modulus, precomp[1]->z)) != CRYPT_OK) { goto ERR_MU; } - if ((err = mp_mulmod(B->x, mu, modulus, precomp[1<<2]->x)) != CRYPT_OK) { goto ERR_MU; } - if ((err = mp_mulmod(B->y, mu, modulus, precomp[1<<2]->y)) != CRYPT_OK) { goto ERR_MU; } - if ((err = mp_mulmod(B->z, mu, modulus, precomp[1<<2]->z)) != CRYPT_OK) { goto ERR_MU; } + if ((err = ltc_mp_mulmod(B->x, mu, modulus, precomp[1<<2]->x)) != CRYPT_OK) { goto ERR_MU; } + if ((err = ltc_mp_mulmod(B->y, mu, modulus, precomp[1<<2]->y)) != CRYPT_OK) { goto ERR_MU; } + if ((err = ltc_mp_mulmod(B->z, mu, modulus, precomp[1<<2]->z)) != CRYPT_OK) { goto ERR_MU; } /* precomp [i,0](A + B) table */ if ((err = ltc_mp.ecc_ptdbl(precomp[1], precomp[2], ma, modulus, mp)) != CRYPT_OK) { goto ERR_MU; } @@ -170,9 +170,9 @@ int ltc_ecc_mul2add(const ecc_point *A, void *kA, /* clean up */ ERR_MU: - mp_clear(mu); + ltc_mp_clear(mu); ERR_MP: - mp_montgomery_free(mp); + ltc_mp_montgomery_free(mp); ERR_P: for (x = 0; x < 16; x++) { ltc_ecc_del_point(precomp[x]); diff --git a/src/pk/ecc/ltc_ecc_mulmod.c b/src/pk/ecc/ltc_ecc_mulmod.c index 96e96826e..73e755690 100644 --- a/src/pk/ecc/ltc_ecc_mulmod.c +++ b/src/pk/ecc/ltc_ecc_mulmod.c @@ -44,16 +44,16 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * } /* init montgomery reduction */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto error; } - if ((err = mp_init(&mu)) != CRYPT_OK) { goto error; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto error; } /* for curves with a == -3 keep ma == NULL */ - if ((err = mp_init(&a_plus3)) != CRYPT_OK) { goto error; } - if ((err = mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { goto error; } - if (mp_cmp(a_plus3, modulus) != LTC_MP_EQ) { - if ((err = mp_init(&ma)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(a, mu, modulus, ma)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_init(&a_plus3)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp(a_plus3, modulus) != LTC_MP_EQ) { + if ((err = ltc_mp_init(&ma)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(a, mu, modulus, ma)) != CRYPT_OK) { goto error; } } /* alloc ram for window temps */ @@ -73,14 +73,14 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * if (tG == NULL) { err = CRYPT_MEM; goto done; } /* tG = G and convert to montgomery */ - if (mp_cmp_d(mu, 1) == LTC_MP_EQ) { + if (ltc_mp_cmp_d(mu, 1) == LTC_MP_EQ) { if ((err = ltc_ecc_copy_point(G, tG)) != CRYPT_OK) { goto done; } } else { - if ((err = mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; } - if ((err = mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; } - if ((err = mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; } } - mp_clear(mu); + ltc_mp_clear(mu); mu = NULL; /* calc the M tab, which holds kG for k==8..15 */ @@ -98,7 +98,7 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * mode = 0; bitcnt = 1; buf = 0; - digidx = mp_get_digit_count(k) - 1; + digidx = ltc_mp_get_digit_count(k) - 1; bitcpy = bitbuf = 0; first = 1; @@ -109,7 +109,7 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * if (digidx == -1) { break; } - buf = mp_get_digit(k, digidx); + buf = ltc_mp_get_digit(k, digidx); bitcnt = (int) ltc_mp.bits_per_digit; --digidx; } @@ -191,10 +191,10 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * ltc_ecc_del_point(M[i]); } error: - if (ma != NULL) mp_clear(ma); - if (a_plus3 != NULL) mp_clear(a_plus3); - if (mu != NULL) mp_clear(mu); - if (mp != NULL) mp_montgomery_free(mp); + if (ma != NULL) ltc_mp_clear(ma); + if (a_plus3 != NULL) ltc_mp_clear(a_plus3); + if (mu != NULL) ltc_mp_clear(mu); + if (mp != NULL) ltc_mp_montgomery_free(mp); return err; } diff --git a/src/pk/ecc/ltc_ecc_mulmod_timing.c b/src/pk/ecc/ltc_ecc_mulmod_timing.c index 728c321d5..04456af16 100644 --- a/src/pk/ecc/ltc_ecc_mulmod_timing.c +++ b/src/pk/ecc/ltc_ecc_mulmod_timing.c @@ -42,16 +42,16 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * } /* init montgomery reduction */ - if ((err = mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto error; } - if ((err = mp_init(&mu)) != CRYPT_OK) { goto error; } - if ((err = mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_setup(modulus, &mp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_init(&mu)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_montgomery_normalization(mu, modulus)) != CRYPT_OK) { goto error; } /* for curves with a == -3 keep ma == NULL */ - if ((err = mp_init(&a_plus3)) != CRYPT_OK) { goto error; } - if ((err = mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { goto error; } - if (mp_cmp(a_plus3, modulus) != LTC_MP_EQ) { - if ((err = mp_init(&ma)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(a, mu, modulus, ma)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_init(&a_plus3)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp(a_plus3, modulus) != LTC_MP_EQ) { + if ((err = ltc_mp_init(&ma)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(a, mu, modulus, ma)) != CRYPT_OK) { goto error; } } /* alloc ram for window temps */ @@ -61,8 +61,8 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * for (j = 0; j < i; j++) { ltc_ecc_del_point(M[j]); } - mp_clear(mu); - mp_montgomery_free(mp); + ltc_mp_clear(mu); + ltc_mp_montgomery_free(mp); return CRYPT_MEM; } } @@ -72,10 +72,10 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * if (tG == NULL) { err = CRYPT_MEM; goto done; } /* tG = G and convert to montgomery */ - if ((err = mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; } - if ((err = mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; } - if ((err = mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; } - mp_clear(mu); + if ((err = ltc_mp_mulmod(G->x, mu, modulus, tG->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mulmod(G->y, mu, modulus, tG->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mulmod(G->z, mu, modulus, tG->z)) != CRYPT_OK) { goto done; } + ltc_mp_clear(mu); mu = NULL; /* calc the M tab */ @@ -88,7 +88,7 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * mode = 0; bitcnt = 1; buf = 0; - digidx = mp_get_digit_count(k) - 1; + digidx = ltc_mp_get_digit_count(k) - 1; /* perform ops */ for (;;) { @@ -97,13 +97,13 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * if (digidx == -1) { break; } - buf = mp_get_digit(k, digidx); - bitcnt = (int) MP_DIGIT_BIT; + buf = ltc_mp_get_digit(k, digidx); + bitcnt = (int) LTC_MP_DIGIT_BIT; --digidx; } /* grab the next msb from the ltiplicand */ - i = (int)((buf >> (MP_DIGIT_BIT - 1)) & 1); + i = (int)((buf >> (LTC_MP_DIGIT_BIT - 1)) & 1); buf <<= 1; if (mode == 0 && i == 0) { @@ -140,10 +140,10 @@ int ltc_ecc_mulmod(const void *k, const ecc_point *G, ecc_point *R, const void * ltc_ecc_del_point(M[i]); } error: - if (ma != NULL) mp_clear(ma); - if (a_plus3 != NULL) mp_clear(a_plus3); - if (mu != NULL) mp_clear(mu); - if (mp != NULL) mp_montgomery_free(mp); + if (ma != NULL) ltc_mp_clear(ma); + if (a_plus3 != NULL) ltc_mp_clear(a_plus3); + if (mu != NULL) ltc_mp_clear(mu); + if (mp != NULL) ltc_mp_montgomery_free(mp); return err; } diff --git a/src/pk/ecc/ltc_ecc_points.c b/src/pk/ecc/ltc_ecc_points.c index b762db502..79adb55e4 100644 --- a/src/pk/ecc/ltc_ecc_points.c +++ b/src/pk/ecc/ltc_ecc_points.c @@ -21,7 +21,7 @@ ecc_point *ltc_ecc_new_point(void) if (p == NULL) { return NULL; } - if (mp_init_multi(&p->x, &p->y, &p->z, LTC_NULL) != CRYPT_OK) { + if (ltc_mp_init_multi(&p->x, &p->y, &p->z, LTC_NULL) != CRYPT_OK) { XFREE(p); return NULL; } @@ -35,7 +35,7 @@ void ltc_ecc_del_point(ecc_point *p) { /* prevents free'ing null arguments */ if (p != NULL) { - mp_clear_multi(p->x, p->y, p->z, LTC_NULL); /* note: p->z may be NULL but that's ok with this function anyways */ + ltc_mp_deinit_multi(p->x, p->y, p->z, LTC_NULL); /* note: p->z may be NULL but that's ok with this function anyways */ XFREE(p); } } diff --git a/src/pk/ecc/ltc_ecc_projective_add_point.c b/src/pk/ecc/ltc_ecc_projective_add_point.c index 370f592b7..1e03dd96f 100644 --- a/src/pk/ecc/ltc_ecc_projective_add_point.c +++ b/src/pk/ecc/ltc_ecc_projective_add_point.c @@ -32,7 +32,7 @@ int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_poi LTC_ARGCHK(modulus != NULL); LTC_ARGCHK(mp != NULL); - if ((err = mp_init_multi(&t1, &t2, &x, &y, &z, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&t1, &t2, &x, &y, &z, LTC_NULL)) != CRYPT_OK) { return err; } @@ -50,148 +50,148 @@ int ltc_ecc_projective_add_point(const ecc_point *P, const ecc_point *Q, ecc_poi goto done; } - if ((mp_cmp(P->x, Q->x) == LTC_MP_EQ) && (mp_cmp(P->z, Q->z) == LTC_MP_EQ)) { - if (mp_cmp(P->y, Q->y) == LTC_MP_EQ) { + if ((ltc_mp_cmp(P->x, Q->x) == LTC_MP_EQ) && (ltc_mp_cmp(P->z, Q->z) == LTC_MP_EQ)) { + if (ltc_mp_cmp(P->y, Q->y) == LTC_MP_EQ) { /* here P = Q >> Result = 2 * P (use doubling) */ - mp_clear_multi(t1, t2, x, y, z, LTC_NULL); + ltc_mp_deinit_multi(t1, t2, x, y, z, LTC_NULL); return ltc_ecc_projective_dbl_point(P, R, ma, modulus, mp); } - if ((err = mp_sub(modulus, Q->y, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(P->y, t1) == LTC_MP_EQ) { + if ((err = ltc_mp_sub(modulus, Q->y, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(P->y, t1) == LTC_MP_EQ) { /* here Q = -P >>> Result = the point at infinity */ err = ltc_ecc_set_point_xyz(1, 1, 0, R); goto done; } } - if ((err = mp_copy(P->x, x)) != CRYPT_OK) { goto done; } - if ((err = mp_copy(P->y, y)) != CRYPT_OK) { goto done; } - if ((err = mp_copy(P->z, z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_copy(P->x, x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_copy(P->y, y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_copy(P->z, z)) != CRYPT_OK) { goto done; } /* if Z is one then these are no-operations */ if (Q->z != NULL) { /* T1 = Z' * Z' */ - if ((err = mp_sqr(Q->z, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(Q->z, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* X = X * T1 */ - if ((err = mp_mul(t1, x, x)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t1, x, x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = Z' * T1 */ - if ((err = mp_mul(Q->z, t1, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(Q->z, t1, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* Y = Y * T1 */ - if ((err = mp_mul(t1, y, y)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(y, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t1, y, y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(y, modulus, mp)) != CRYPT_OK) { goto done; } } /* T1 = Z*Z */ - if ((err = mp_sqr(z, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(z, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* T2 = X' * T1 */ - if ((err = mp_mul(Q->x, t1, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(Q->x, t1, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = Z * T1 */ - if ((err = mp_mul(z, t1, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(z, t1, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = Y' * T1 */ - if ((err = mp_mul(Q->y, t1, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(Q->y, t1, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* Y = Y - T1 */ - if ((err = mp_sub(y, t1, y)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(y, 0) == LTC_MP_LT) { - if ((err = mp_add(y, modulus, y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(y, t1, y)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(y, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(y, modulus, y)) != CRYPT_OK) { goto done; } } /* T1 = 2T1 */ - if ((err = mp_add(t1, t1, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, t1, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } /* T1 = Y + T1 */ - if ((err = mp_add(t1, y, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, y, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } /* X = X - T2 */ - if ((err = mp_sub(x, t2, x)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(x, 0) == LTC_MP_LT) { - if ((err = mp_add(x, modulus, x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(x, t2, x)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(x, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(x, modulus, x)) != CRYPT_OK) { goto done; } } /* T2 = 2T2 */ - if ((err = mp_add(t2, t2, t2)) != CRYPT_OK) { goto done; } - if (mp_cmp(t2, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t2, t2, t2)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t2, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t2, modulus, t2)) != CRYPT_OK) { goto done; } } /* T2 = X + T2 */ - if ((err = mp_add(t2, x, t2)) != CRYPT_OK) { goto done; } - if (mp_cmp(t2, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t2, x, t2)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t2, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t2, modulus, t2)) != CRYPT_OK) { goto done; } } /* if Z' != 1 */ if (Q->z != NULL) { /* Z = Z * Z' */ - if ((err = mp_mul(z, Q->z, z)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(z, Q->z, z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK) { goto done; } } /* Z = Z * X */ - if ((err = mp_mul(z, x, z)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(z, x, z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(z, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = T1 * X */ - if ((err = mp_mul(t1, x, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t1, x, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* X = X * X */ - if ((err = mp_sqr(x, x)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(x, x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; } /* T2 = T2 * x */ - if ((err = mp_mul(t2, x, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t2, x, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = T1 * X */ - if ((err = mp_mul(t1, x, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t1, x, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* X = Y*Y */ - if ((err = mp_sqr(y, x)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(y, x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(x, modulus, mp)) != CRYPT_OK) { goto done; } /* X = X - T2 */ - if ((err = mp_sub(x, t2, x)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(x, 0) == LTC_MP_LT) { - if ((err = mp_add(x, modulus, x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(x, t2, x)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(x, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(x, modulus, x)) != CRYPT_OK) { goto done; } } /* T2 = T2 - X */ - if ((err = mp_sub(t2, x, t2)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(t2, 0) == LTC_MP_LT) { - if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(t2, x, t2)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(t2, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } } /* T2 = T2 - X */ - if ((err = mp_sub(t2, x, t2)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(t2, 0) == LTC_MP_LT) { - if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(t2, x, t2)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(t2, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } } /* T2 = T2 * Y */ - if ((err = mp_mul(t2, y, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t2, y, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* Y = T2 - T1 */ - if ((err = mp_sub(t2, t1, y)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(y, 0) == LTC_MP_LT) { - if ((err = mp_add(y, modulus, y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(t2, t1, y)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(y, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(y, modulus, y)) != CRYPT_OK) { goto done; } } /* Y = Y/2 */ - if (mp_isodd(y)) { - if ((err = mp_add(y, modulus, y)) != CRYPT_OK) { goto done; } + if (ltc_mp_isodd(y)) { + if ((err = ltc_mp_add(y, modulus, y)) != CRYPT_OK) { goto done; } } - if ((err = mp_div_2(y, y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_div_2(y, y)) != CRYPT_OK) { goto done; } - if ((err = mp_copy(x, R->x)) != CRYPT_OK) { goto done; } - if ((err = mp_copy(y, R->y)) != CRYPT_OK) { goto done; } - if ((err = mp_copy(z, R->z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_copy(x, R->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_copy(y, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_copy(z, R->z)) != CRYPT_OK) { goto done; } err = CRYPT_OK; done: - mp_clear_multi(t1, t2, x, y, z, LTC_NULL); + ltc_mp_deinit_multi(t1, t2, x, y, z, LTC_NULL); return err; } diff --git a/src/pk/ecc/ltc_ecc_projective_dbl_point.c b/src/pk/ecc/ltc_ecc_projective_dbl_point.c index f9d4d8aa5..764bfd080 100644 --- a/src/pk/ecc/ltc_ecc_projective_dbl_point.c +++ b/src/pk/ecc/ltc_ecc_projective_dbl_point.c @@ -47,7 +47,7 @@ int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, const void *m LTC_ARGCHK(modulus != NULL); LTC_ARGCHK(mp != NULL); - if ((err = mp_init_multi(&t1, &t2, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&t1, &t2, LTC_NULL)) != CRYPT_OK) { return err; } @@ -63,120 +63,120 @@ int ltc_ecc_projective_dbl_point(const ecc_point *P, ecc_point *R, const void *m } /* t1 = Z * Z */ - if ((err = mp_sqr(R->z, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(R->z, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* Z = Y * Z */ - if ((err = mp_mul(R->z, R->y, R->z)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(R->z, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(R->z, R->y, R->z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(R->z, modulus, mp)) != CRYPT_OK) { goto done; } /* Z = 2Z */ - if ((err = mp_add(R->z, R->z, R->z)) != CRYPT_OK) { goto done; } - if (mp_cmp(R->z, modulus) != LTC_MP_LT) { - if ((err = mp_sub(R->z, modulus, R->z)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(R->z, R->z, R->z)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(R->z, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(R->z, modulus, R->z)) != CRYPT_OK) { goto done; } } if (ma == NULL) { /* special case for curves with a == -3 (10% faster than general case) */ /* T2 = X - T1 */ - if ((err = mp_sub(R->x, t1, t2)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(t2, 0) == LTC_MP_LT) { - if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(R->x, t1, t2)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(t2, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } } /* T1 = X + T1 */ - if ((err = mp_add(t1, R->x, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, R->x, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } /* T2 = T1 * T2 */ - if ((err = mp_mul(t1, t2, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t1, t2, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = 2T2 */ - if ((err = mp_add(t2, t2, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t2, t2, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } /* T1 = T1 + T2 */ - if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } } else { /* T2 = T1 * T1 */ - if ((err = mp_sqr(t1, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(t1, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = T2 * a */ - if ((err = mp_mul(t2, ma, t1)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(t2, ma, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t1, modulus, mp)) != CRYPT_OK) { goto done; } /* T2 = X * X */ - if ((err = mp_sqr(R->x, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(R->x, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* T1 = T2 + T1 */ - if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } /* T1 = T2 + T1 */ - if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } /* T1 = T2 + T1 */ - if ((err = mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } - if (mp_cmp(t1, modulus) != LTC_MP_LT) { - if ((err = mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(t1, t2, t1)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(t1, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(t1, modulus, t1)) != CRYPT_OK) { goto done; } } } /* Y = 2Y */ - if ((err = mp_add(R->y, R->y, R->y)) != CRYPT_OK) { goto done; } - if (mp_cmp(R->y, modulus) != LTC_MP_LT) { - if ((err = mp_sub(R->y, modulus, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_add(R->y, R->y, R->y)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp(R->y, modulus) != LTC_MP_LT) { + if ((err = ltc_mp_sub(R->y, modulus, R->y)) != CRYPT_OK) { goto done; } } /* Y = Y * Y */ - if ((err = mp_sqr(R->y, R->y)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(R->y, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; } /* T2 = Y * Y */ - if ((err = mp_sqr(R->y, t2)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(R->y, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(t2, modulus, mp)) != CRYPT_OK) { goto done; } /* T2 = T2/2 */ - if (mp_isodd(t2)) { - if ((err = mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } + if (ltc_mp_isodd(t2)) { + if ((err = ltc_mp_add(t2, modulus, t2)) != CRYPT_OK) { goto done; } } - if ((err = mp_div_2(t2, t2)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_div_2(t2, t2)) != CRYPT_OK) { goto done; } /* Y = Y * X */ - if ((err = mp_mul(R->y, R->x, R->y)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(R->y, R->x, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; } /* X = T1 * T1 */ - if ((err = mp_sqr(t1, R->x)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(R->x, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sqr(t1, R->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(R->x, modulus, mp)) != CRYPT_OK) { goto done; } /* X = X - Y */ - if ((err = mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(R->x, 0) == LTC_MP_LT) { - if ((err = mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(R->x, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; } } /* X = X - Y */ - if ((err = mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(R->x, 0) == LTC_MP_LT) { - if ((err = mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(R->x, R->y, R->x)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(R->x, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(R->x, modulus, R->x)) != CRYPT_OK) { goto done; } } /* Y = Y - X */ - if ((err = mp_sub(R->y, R->x, R->y)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(R->y, 0) == LTC_MP_LT) { - if ((err = mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(R->y, R->x, R->y)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(R->y, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; } } /* Y = Y * T1 */ - if ((err = mp_mul(R->y, t1, R->y)) != CRYPT_OK) { goto done; } - if ((err = mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_mul(R->y, t1, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_montgomery_reduce(R->y, modulus, mp)) != CRYPT_OK) { goto done; } /* Y = Y - T2 */ - if ((err = mp_sub(R->y, t2, R->y)) != CRYPT_OK) { goto done; } - if (mp_cmp_d(R->y, 0) == LTC_MP_LT) { - if ((err = mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_sub(R->y, t2, R->y)) != CRYPT_OK) { goto done; } + if (ltc_mp_cmp_d(R->y, 0) == LTC_MP_LT) { + if ((err = ltc_mp_add(R->y, modulus, R->y)) != CRYPT_OK) { goto done; } } err = CRYPT_OK; done: - mp_clear_multi(t2, t1, LTC_NULL); + ltc_mp_deinit_multi(t2, t1, LTC_NULL); return err; } #endif diff --git a/src/pk/ecc/ltc_ecc_verify_key.c b/src/pk/ecc/ltc_ecc_verify_key.c index 26c8383f7..7c83d6a85 100644 --- a/src/pk/ecc/ltc_ecc_verify_key.c +++ b/src/pk/ecc/ltc_ecc_verify_key.c @@ -27,7 +27,7 @@ int ltc_ecc_verify_key(const ecc_key *key) (ltc_mp.compare(key->pubkey.y, prime) != LTC_MP_LT) || (ltc_mp.compare_d(key->pubkey.x, 0) == LTC_MP_LT) || (ltc_mp.compare_d(key->pubkey.y, 0) == LTC_MP_LT) || - (mp_iszero(key->pubkey.x) && mp_iszero(key->pubkey.y)) + (ltc_mp_iszero(key->pubkey.x) && ltc_mp_iszero(key->pubkey.y)) ) { err = CRYPT_INVALID_PACKET; diff --git a/src/pk/pkcs1/pkcs_1_i2osp.c b/src/pk/pkcs1/pkcs_1_i2osp.c index a62dd860e..513b03f37 100644 --- a/src/pk/pkcs1/pkcs_1_i2osp.c +++ b/src/pk/pkcs1/pkcs_1_i2osp.c @@ -24,7 +24,7 @@ int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out) { unsigned long size; - size = mp_unsigned_bin_size(n); + size = ltc_mp_unsigned_bin_size(n); if (size > modulus_len) { return CRYPT_BUFFER_OVERFLOW; @@ -32,7 +32,7 @@ int pkcs_1_i2osp(void *n, unsigned long modulus_len, unsigned char *out) /* store it */ zeromem(out, modulus_len); - return mp_to_unsigned_bin(n, out+(modulus_len-size)); + return ltc_mp_to_unsigned_bin(n, out+(modulus_len-size)); } #endif /* LTC_PKCS_1 */ diff --git a/src/pk/pkcs1/pkcs_1_os2ip.c b/src/pk/pkcs1/pkcs_1_os2ip.c index 438030e56..e009cc359 100644 --- a/src/pk/pkcs1/pkcs_1_os2ip.c +++ b/src/pk/pkcs1/pkcs_1_os2ip.c @@ -17,7 +17,7 @@ */ int pkcs_1_os2ip(void *n, unsigned char *in, unsigned long inlen) { - return mp_read_unsigned_bin(n, in, inlen); + return ltc_mp_read_unsigned_bin(n, in, inlen); } #endif /* LTC_PKCS_1 */ diff --git a/src/pk/rsa/rsa_decrypt_key.c b/src/pk/rsa/rsa_decrypt_key.c index ea1ec99f5..d8e7a546f 100644 --- a/src/pk/rsa/rsa_decrypt_key.c +++ b/src/pk/rsa/rsa_decrypt_key.c @@ -58,10 +58,10 @@ int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen } /* get modulus len in bits */ - modulus_bitlen = mp_count_bits( (key->N)); + modulus_bitlen = ltc_mp_count_bits( (key->N)); /* outlen must be at least the size of the modulus */ - modulus_bytelen = mp_unsigned_bin_size( (key->N)); + modulus_bytelen = ltc_mp_unsigned_bin_size( (key->N)); if (modulus_bytelen != inlen) { return CRYPT_INVALID_PACKET; } diff --git a/src/pk/rsa/rsa_encrypt_key.c b/src/pk/rsa/rsa_encrypt_key.c index 8739bb277..17dd1af5d 100644 --- a/src/pk/rsa/rsa_encrypt_key.c +++ b/src/pk/rsa/rsa_encrypt_key.c @@ -59,10 +59,10 @@ int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, } /* get modulus len in bits */ - modulus_bitlen = mp_count_bits( (key->N)); + modulus_bitlen = ltc_mp_count_bits( (key->N)); /* outlen must be at least the size of the modulus */ - modulus_bytelen = mp_unsigned_bin_size( (key->N)); + modulus_bytelen = ltc_mp_unsigned_bin_size( (key->N)); if (modulus_bytelen > *outlen) { *outlen = modulus_bytelen; return CRYPT_BUFFER_OVERFLOW; diff --git a/src/pk/rsa/rsa_export.c b/src/pk/rsa/rsa_export.c index 225224d0b..7d99f47c2 100644 --- a/src/pk/rsa/rsa_export.c +++ b/src/pk/rsa/rsa_export.c @@ -56,7 +56,7 @@ int rsa_export(unsigned char *out, unsigned long *outlen, int type, const rsa_ke unsigned char* tmp = NULL; if (std) { - tmplen = (unsigned long)(mp_count_bits(key->N) / 8) * 2 + 8; + tmplen = (unsigned long)(ltc_mp_count_bits(key->N) / 8) * 2 + 8; tmp = XMALLOC(tmplen); ptmplen = &tmplen; if (tmp == NULL) { diff --git a/src/pk/rsa/rsa_exptmod.c b/src/pk/rsa/rsa_exptmod.c index 11146679b..95ddddeb0 100644 --- a/src/pk/rsa/rsa_exptmod.c +++ b/src/pk/rsa/rsa_exptmod.c @@ -47,18 +47,18 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen, } /* init and copy into tmp */ - if ((err = mp_init_multi(&tmp, &tmpa, &tmpb, + if ((err = ltc_mp_init_multi(&tmp, &tmpa, &tmpb, #ifdef LTC_RSA_BLINDING &rnd, &rndi, #endif /* LTC_RSA_BLINDING */ NULL)) != CRYPT_OK) { return err; } - if ((err = mp_read_unsigned_bin(tmp, in, (int)inlen)) != CRYPT_OK) + if ((err = ltc_mp_read_unsigned_bin(tmp, in, (int)inlen)) != CRYPT_OK) { goto error; } /* sanity check on the input */ - if (mp_cmp(key->N, tmp) == LTC_MP_LT) { + if (ltc_mp_cmp(key->N, tmp) == LTC_MP_LT) { err = CRYPT_PK_INVALID_SIZE; goto error; } @@ -67,61 +67,61 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen, if (which == PK_PRIVATE) { #ifdef LTC_RSA_BLINDING /* do blinding */ - err = mp_rand(rnd, mp_get_digit_count(key->N)); + err = ltc_mp_rand(rnd, ltc_mp_get_digit_count(key->N)); if (err != CRYPT_OK) { goto error; } /* rndi = 1/rnd mod N */ - err = mp_invmod(rnd, key->N, rndi); + err = ltc_mp_invmod(rnd, key->N, rndi); if (err != CRYPT_OK) { goto error; } /* rnd = rnd^e */ - err = mp_exptmod( rnd, key->e, key->N, rnd); + err = ltc_mp_exptmod( rnd, key->e, key->N, rnd); if (err != CRYPT_OK) { goto error; } /* tmp = tmp*rnd mod N */ - err = mp_mulmod( tmp, rnd, key->N, tmp); + err = ltc_mp_mulmod( tmp, rnd, key->N, tmp); if (err != CRYPT_OK) { goto error; } #endif /* LTC_RSA_BLINDING */ - has_crt_parameters = (key->p != NULL) && (mp_get_digit_count(key->p) != 0) && - (key->q != NULL) && (mp_get_digit_count(key->q) != 0) && - (key->dP != NULL) && (mp_get_digit_count(key->dP) != 0) && - (key->dQ != NULL) && (mp_get_digit_count(key->dQ) != 0) && - (key->qP != NULL) && (mp_get_digit_count(key->qP) != 0); + has_crt_parameters = (key->p != NULL) && (ltc_mp_get_digit_count(key->p) != 0) && + (key->q != NULL) && (ltc_mp_get_digit_count(key->q) != 0) && + (key->dP != NULL) && (ltc_mp_get_digit_count(key->dP) != 0) && + (key->dQ != NULL) && (ltc_mp_get_digit_count(key->dQ) != 0) && + (key->qP != NULL) && (ltc_mp_get_digit_count(key->qP) != 0); if (!has_crt_parameters) { /* * In case CRT optimization parameters are not provided, * the private key is directly used to exptmod it */ - if ((err = mp_exptmod(tmp, key->d, key->N, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(tmp, key->d, key->N, tmp)) != CRYPT_OK) { goto error; } } else { /* tmpa = tmp^dP mod p */ - if ((err = mp_exptmod(tmp, key->dP, key->p, tmpa)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(tmp, key->dP, key->p, tmpa)) != CRYPT_OK) { goto error; } /* tmpb = tmp^dQ mod q */ - if ((err = mp_exptmod(tmp, key->dQ, key->q, tmpb)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(tmp, key->dQ, key->q, tmpb)) != CRYPT_OK) { goto error; } /* tmp = (tmpa - tmpb) * qInv (mod p) */ - if ((err = mp_sub(tmpa, tmpb, tmp)) != CRYPT_OK) { goto error; } - if ((err = mp_mulmod(tmp, key->qP, key->p, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_sub(tmpa, tmpb, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mulmod(tmp, key->qP, key->p, tmp)) != CRYPT_OK) { goto error; } /* tmp = tmpb + q * tmp */ - if ((err = mp_mul(tmp, key->q, tmp)) != CRYPT_OK) { goto error; } - if ((err = mp_add(tmp, tmpb, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_mul(tmp, key->q, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_add(tmp, tmpb, tmp)) != CRYPT_OK) { goto error; } } #ifdef LTC_RSA_BLINDING /* unblind */ - err = mp_mulmod( tmp, rndi, key->N, tmp); + err = ltc_mp_mulmod( tmp, rndi, key->N, tmp); if (err != CRYPT_OK) { goto error; } @@ -129,18 +129,18 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen, #ifdef LTC_RSA_CRT_HARDENING if (has_crt_parameters) { - if ((err = mp_exptmod(tmp, key->e, key->N, tmpa)) != CRYPT_OK) { goto error; } - if ((err = mp_read_unsigned_bin(tmpb, in, (int)inlen)) != CRYPT_OK) { goto error; } - if (mp_cmp(tmpa, tmpb) != LTC_MP_EQ) { err = CRYPT_ERROR; goto error; } + if ((err = ltc_mp_exptmod(tmp, key->e, key->N, tmpa)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_unsigned_bin(tmpb, in, (int)inlen)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp(tmpa, tmpb) != LTC_MP_EQ) { err = CRYPT_ERROR; goto error; } } #endif } else { /* exptmod it */ - if ((err = mp_exptmod(tmp, key->e, key->N, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_exptmod(tmp, key->e, key->N, tmp)) != CRYPT_OK) { goto error; } } /* read it back */ - x = (unsigned long)mp_unsigned_bin_size(key->N); + x = (unsigned long)ltc_mp_unsigned_bin_size(key->N); if (x > *outlen) { *outlen = x; err = CRYPT_BUFFER_OVERFLOW; @@ -148,7 +148,7 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen, } /* this should never happen ... */ - if (mp_unsigned_bin_size(tmp) > mp_unsigned_bin_size(key->N)) { + if (ltc_mp_unsigned_bin_size(tmp) > ltc_mp_unsigned_bin_size(key->N)) { err = CRYPT_ERROR; goto error; } @@ -156,12 +156,12 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen, /* convert it */ zeromem(out, x); - if ((err = mp_to_unsigned_bin(tmp, out+(x-mp_unsigned_bin_size(tmp)))) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_to_unsigned_bin(tmp, out+(x-ltc_mp_unsigned_bin_size(tmp)))) != CRYPT_OK) { goto error; } /* clean up and return */ err = CRYPT_OK; error: - mp_clear_multi( + ltc_mp_deinit_multi( #ifdef LTC_RSA_BLINDING rndi, rnd, #endif /* LTC_RSA_BLINDING */ diff --git a/src/pk/rsa/rsa_get_size.c b/src/pk/rsa/rsa_get_size.c index 72d3f7a51..efefc5015 100644 --- a/src/pk/rsa/rsa_get_size.c +++ b/src/pk/rsa/rsa_get_size.c @@ -21,7 +21,7 @@ int rsa_get_size(const rsa_key *key) if (key) { - ret = mp_unsigned_bin_size(key->N); + ret = ltc_mp_unsigned_bin_size(key->N); } /* if */ return ret; diff --git a/src/pk/rsa/rsa_key.c b/src/pk/rsa/rsa_key.c index 74130ae6e..12b88ce39 100644 --- a/src/pk/rsa/rsa_key.c +++ b/src/pk/rsa/rsa_key.c @@ -29,7 +29,7 @@ static void s_mpi_shrink_multi(void **a, ...) } if (*cur != NULL) { arg[n] = cur; - if ((err = mp_init_copy(&tmp[n], *arg[n])) != CRYPT_OK) { + if ((err = ltc_mp_init_copy(&tmp[n], *arg[n])) != CRYPT_OK) { goto out; } n++; @@ -40,7 +40,7 @@ static void s_mpi_shrink_multi(void **a, ...) /* but we clear the old values in the reverse order */ while (n != 0 && arg[--n] != NULL) { - mp_clear(*arg[n]); + ltc_mp_clear(*arg[n]); *arg[n] = tmp[n]; } out: @@ -52,7 +52,7 @@ static void s_mpi_shrink_multi(void **a, ...) (n >= sizeof(tmp)/sizeof(tmp[0]))) { for (n = 0; n < sizeof(tmp)/sizeof(tmp[0]); ++n) { if (tmp[n] != NULL) { - mp_clear(tmp[n]); + ltc_mp_clear(tmp[n]); } } } @@ -85,7 +85,7 @@ void rsa_shrink_key(rsa_key *key) int rsa_init(rsa_key *key) { LTC_ARGCHK(key != NULL); - return mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, LTC_NULL); + return ltc_mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, LTC_NULL); } /** @@ -95,7 +95,7 @@ int rsa_init(rsa_key *key) void rsa_free(rsa_key *key) { LTC_ARGCHKVD(key != NULL); - mp_cleanup_multi(&key->q, &key->p, &key->qP, &key->dP, &key->dQ, &key->N, &key->d, &key->e, LTC_NULL); + ltc_mp_cleanup_multi(&key->q, &key->p, &key->qP, &key->dP, &key->dQ, &key->N, &key->d, &key->e, LTC_NULL); } #endif diff --git a/src/pk/rsa/rsa_make_key.c b/src/pk/rsa/rsa_make_key.c index 9b5395f47..7cbecaf46 100644 --- a/src/pk/rsa/rsa_make_key.c +++ b/src/pk/rsa/rsa_make_key.c @@ -22,7 +22,7 @@ static int s_rsa_make_key(prng_state *prng, int wprng, int size, void *e, rsa_ke return err; } - if ((err = mp_init_multi(&p, &q, &tmp1, &tmp2, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&p, &q, &tmp1, &tmp2, LTC_NULL)) != CRYPT_OK) { return err; } @@ -31,41 +31,41 @@ static int s_rsa_make_key(prng_state *prng, int wprng, int size, void *e, rsa_ke /* make prime "p" */ do { if ((err = rand_prime( p, size/2, prng, wprng)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_sub_d( p, 1, tmp1)) != CRYPT_OK) { goto cleanup; } /* tmp1 = p-1 */ - if ((err = mp_gcd( tmp1, e, tmp2)) != CRYPT_OK) { goto cleanup; } /* tmp2 = gcd(p-1, e) */ - } while (mp_cmp_d( tmp2, 1) != 0); /* while e divides p-1 */ + if ((err = ltc_mp_sub_d( p, 1, tmp1)) != CRYPT_OK) { goto cleanup; } /* tmp1 = p-1 */ + if ((err = ltc_mp_gcd( tmp1, e, tmp2)) != CRYPT_OK) { goto cleanup; } /* tmp2 = gcd(p-1, e) */ + } while (ltc_mp_cmp_d( tmp2, 1) != 0); /* while e divides p-1 */ /* make prime "q" */ do { if ((err = rand_prime( q, size/2, prng, wprng)) != CRYPT_OK) { goto cleanup; } - if ((err = mp_sub_d( q, 1, tmp1)) != CRYPT_OK) { goto cleanup; } /* tmp1 = q-1 */ - if ((err = mp_gcd( tmp1, e, tmp2)) != CRYPT_OK) { goto cleanup; } /* tmp2 = gcd(q-1, e) */ - } while (mp_cmp_d( tmp2, 1) != 0); /* while e divides q-1 */ + if ((err = ltc_mp_sub_d( q, 1, tmp1)) != CRYPT_OK) { goto cleanup; } /* tmp1 = q-1 */ + if ((err = ltc_mp_gcd( tmp1, e, tmp2)) != CRYPT_OK) { goto cleanup; } /* tmp2 = gcd(q-1, e) */ + } while (ltc_mp_cmp_d( tmp2, 1) != 0); /* while e divides q-1 */ /* tmp1 = lcm(p-1, q-1) */ - if ((err = mp_sub_d( p, 1, tmp2)) != CRYPT_OK) { goto cleanup; } /* tmp2 = p-1 */ + if ((err = ltc_mp_sub_d( p, 1, tmp2)) != CRYPT_OK) { goto cleanup; } /* tmp2 = p-1 */ /* tmp1 = q-1 (previous do/while loop) */ - if ((err = mp_lcm( tmp1, tmp2, tmp1)) != CRYPT_OK) { goto cleanup; } /* tmp1 = lcm(p-1, q-1) */ + if ((err = ltc_mp_lcm( tmp1, tmp2, tmp1)) != CRYPT_OK) { goto cleanup; } /* tmp1 = lcm(p-1, q-1) */ /* make key */ if ((err = rsa_init(key)) != CRYPT_OK) { goto errkey; } - if ((err = mp_copy( e, key->e)) != CRYPT_OK) { goto errkey; } /* key->e = e */ - if ((err = mp_invmod( key->e, tmp1, key->d)) != CRYPT_OK) { goto errkey; } /* key->d = 1/e mod lcm(p-1,q-1) */ - if ((err = mp_mul( p, q, key->N)) != CRYPT_OK) { goto errkey; } /* key->N = pq */ + if ((err = ltc_mp_copy( e, key->e)) != CRYPT_OK) { goto errkey; } /* key->e = e */ + if ((err = ltc_mp_invmod( key->e, tmp1, key->d)) != CRYPT_OK) { goto errkey; } /* key->d = 1/e mod lcm(p-1,q-1) */ + if ((err = ltc_mp_mul( p, q, key->N)) != CRYPT_OK) { goto errkey; } /* key->N = pq */ /* optimize for CRT now */ /* find d mod q-1 and d mod p-1 */ - if ((err = mp_sub_d( p, 1, tmp1)) != CRYPT_OK) { goto errkey; } /* tmp1 = p-1 */ - if ((err = mp_sub_d( q, 1, tmp2)) != CRYPT_OK) { goto errkey; } /* tmp2 = q-1 */ - if ((err = mp_mod( key->d, tmp1, key->dP)) != CRYPT_OK) { goto errkey; } /* dP = d mod p-1 */ - if ((err = mp_mod( key->d, tmp2, key->dQ)) != CRYPT_OK) { goto errkey; } /* dQ = d mod q-1 */ - if ((err = mp_invmod( q, p, key->qP)) != CRYPT_OK) { goto errkey; } /* qP = 1/q mod p */ + if ((err = ltc_mp_sub_d( p, 1, tmp1)) != CRYPT_OK) { goto errkey; } /* tmp1 = p-1 */ + if ((err = ltc_mp_sub_d( q, 1, tmp2)) != CRYPT_OK) { goto errkey; } /* tmp2 = q-1 */ + if ((err = ltc_mp_mod( key->d, tmp1, key->dP)) != CRYPT_OK) { goto errkey; } /* dP = d mod p-1 */ + if ((err = ltc_mp_mod( key->d, tmp2, key->dQ)) != CRYPT_OK) { goto errkey; } /* dQ = d mod q-1 */ + if ((err = ltc_mp_invmod( q, p, key->qP)) != CRYPT_OK) { goto errkey; } /* qP = 1/q mod p */ - if ((err = mp_copy( p, key->p)) != CRYPT_OK) { goto errkey; } - if ((err = mp_copy( q, key->q)) != CRYPT_OK) { goto errkey; } + if ((err = ltc_mp_copy( p, key->p)) != CRYPT_OK) { goto errkey; } + if ((err = ltc_mp_copy( q, key->q)) != CRYPT_OK) { goto errkey; } /* set key type (in this case it's CRT optimized) */ key->type = PK_PRIVATE; @@ -76,7 +76,7 @@ static int s_rsa_make_key(prng_state *prng, int wprng, int size, void *e, rsa_ke errkey: rsa_free(key); cleanup: - mp_clear_multi(tmp2, tmp1, q, p, LTC_NULL); + ltc_mp_deinit_multi(tmp2, tmp1, q, p, LTC_NULL); return err; } @@ -91,21 +91,21 @@ static int s_rsa_make_key(prng_state *prng, int wprng, int size, void *e, rsa_ke */ int rsa_make_key(prng_state *prng, int wprng, int size, long e, rsa_key *key) { - void *tmp_e; + void *tltc_mp_e; int err; if ((e < 3) || ((e & 1) == 0)) { return CRYPT_INVALID_ARG; } - if ((err = mp_init(&tmp_e)) != CRYPT_OK) { + if ((err = ltc_mp_init(&tltc_mp_e)) != CRYPT_OK) { return err; } - if ((err = mp_set_int(tmp_e, e)) == CRYPT_OK) - err = s_rsa_make_key(prng, wprng, size, tmp_e, key); + if ((err = ltc_mp_set_int(tltc_mp_e, e)) == CRYPT_OK) + err = s_rsa_make_key(prng, wprng, size, tltc_mp_e, key); - mp_clear(tmp_e); + ltc_mp_clear(tltc_mp_e); return err; } @@ -126,14 +126,14 @@ int rsa_make_key_ubin_e(prng_state *prng, int wprng, int size, int err; void *tmp_e; - if ((err = mp_init(&tmp_e)) != CRYPT_OK) { + if ((err = ltc_mp_init(&tmp_e)) != CRYPT_OK) { return err; } - if ((err = mp_read_unsigned_bin(tmp_e, e, elen)) == CRYPT_OK) + if ((err = ltc_mp_read_unsigned_bin(tmp_e, e, elen)) == CRYPT_OK) err = rsa_make_key_bn_e(prng, wprng, size, tmp_e, key); - mp_clear(tmp_e); + ltc_mp_clear(tmp_e); return err; } @@ -152,8 +152,8 @@ int rsa_make_key_bn_e(prng_state *prng, int wprng, int size, void *e, rsa_key *k int err; int e_bits; - e_bits = mp_count_bits(e); - if ((e_bits > 1 && e_bits < 256) && (mp_get_digit(e, 0) & 1)) { + e_bits = ltc_mp_count_bits(e); + if ((e_bits > 1 && e_bits < 256) && (ltc_mp_get_digit(e, 0) & 1)) { err = s_rsa_make_key(prng, wprng, size, e, key); } else { err = CRYPT_INVALID_ARG; diff --git a/src/pk/rsa/rsa_set.c b/src/pk/rsa/rsa_set.c index 3182275f0..55d6f7cb3 100644 --- a/src/pk/rsa/rsa_set.c +++ b/src/pk/rsa/rsa_set.c @@ -31,10 +31,10 @@ int rsa_set_key(const unsigned char *N, unsigned long Nlen, if ((err = rsa_init(key)) != CRYPT_OK) return err; - if ((err = mp_read_unsigned_bin(key->N , N , Nlen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->e , e , elen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->N , N , Nlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->e , e , elen)) != CRYPT_OK) { goto LBL_ERR; } if (d && dlen) { - if ((err = mp_read_unsigned_bin(key->d , d , dlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->d , d , dlen)) != CRYPT_OK) { goto LBL_ERR; } key->type = PK_PRIVATE; } else { @@ -72,8 +72,8 @@ int rsa_set_factors(const unsigned char *p, unsigned long plen, if (key->type != PK_PRIVATE) return CRYPT_PK_TYPE_MISMATCH; - if ((err = mp_read_unsigned_bin(key->p , p , plen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->q , q , qlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->p , p , plen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->q , q , qlen)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; LBL_ERR: @@ -110,9 +110,9 @@ int rsa_set_crt_params(const unsigned char *dP, unsigned long dPlen, if (key->type != PK_PRIVATE) return CRYPT_PK_TYPE_MISMATCH; - if ((err = mp_read_unsigned_bin(key->dP, dP, dPlen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->dQ, dQ, dQlen)) != CRYPT_OK) { goto LBL_ERR; } - if ((err = mp_read_unsigned_bin(key->qP, qP, qPlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->dP, dP, dPlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->dQ, dQ, dQlen)) != CRYPT_OK) { goto LBL_ERR; } + if ((err = ltc_mp_read_unsigned_bin(key->qP, qP, qPlen)) != CRYPT_OK) { goto LBL_ERR; } return CRYPT_OK; LBL_ERR: diff --git a/src/pk/rsa/rsa_sign_hash.c b/src/pk/rsa/rsa_sign_hash.c index 0061a9a8e..bc5e0a89f 100644 --- a/src/pk/rsa/rsa_sign_hash.c +++ b/src/pk/rsa/rsa_sign_hash.c @@ -60,10 +60,10 @@ int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, } /* get modulus len in bits */ - modulus_bitlen = mp_count_bits((key->N)); + modulus_bitlen = ltc_mp_count_bits((key->N)); /* outlen must be at least the size of the modulus */ - modulus_bytelen = mp_unsigned_bin_size((key->N)); + modulus_bytelen = ltc_mp_unsigned_bin_size((key->N)); if (modulus_bytelen > *outlen) { *outlen = modulus_bytelen; return CRYPT_BUFFER_OVERFLOW; @@ -102,7 +102,7 @@ int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, LTC_SET_ASN1(siginfo, 1, LTC_ASN1_OCTET_STRING, in, inlen); /* allocate memory for the encoding */ - y = mp_unsigned_bin_size(key->N); + y = ltc_mp_unsigned_bin_size(key->N); tmpin = XMALLOC(y); if (tmpin == NULL) { return CRYPT_MEM; diff --git a/src/pk/rsa/rsa_verify_hash.c b/src/pk/rsa/rsa_verify_hash.c index d946a2028..9b2577bb5 100644 --- a/src/pk/rsa/rsa_verify_hash.c +++ b/src/pk/rsa/rsa_verify_hash.c @@ -56,10 +56,10 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long sigle } /* get modulus len in bits */ - modulus_bitlen = mp_count_bits( (key->N)); + modulus_bitlen = ltc_mp_count_bits( (key->N)); /* outlen must be at least the size of the modulus */ - modulus_bytelen = mp_unsigned_bin_size( (key->N)); + modulus_bytelen = ltc_mp_unsigned_bin_size( (key->N)); if (modulus_bytelen != siglen) { return CRYPT_INVALID_PACKET; } diff --git a/tests/der_test.c b/tests/der_test.c index 489b608e4..686f8f41d 100644 --- a/tests/der_test.c +++ b/tests/der_test.c @@ -283,8 +283,8 @@ static void s_der_tests_print_flexi_i(ltc_asn1_list* l, unsigned int level) break; case LTC_ASN1_INTEGER: name = "INTEGER"; - buf = s_xmalloc(((mp_get_digit_count(l->data) + 1) * ltc_mp.bits_per_digit) / 3); - mp_toradix(l->data, buf, 10); + buf = s_xmalloc(((ltc_mp_get_digit_count(l->data) + 1) * ltc_mp.bits_per_digit) / 3); + ltc_mp_toradix(l->data, buf, 10); text = buf; break; case LTC_ASN1_SHORT_INTEGER: @@ -886,7 +886,7 @@ static void der_flexi_test(void) exit(EXIT_FAILURE); } - if (mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) { + if (ltc_mp_cmp_d(l->data, 12345678UL) != LTC_MP_EQ) { fprintf(stderr, "(%d), %d, %lu, next=%p, prev=%p, parent=%p, child=%p\n", __LINE__, l->type, l->size, l->next, l->prev, l->parent, l->child); exit(EXIT_FAILURE); } @@ -1098,10 +1098,10 @@ static int der_choice_n_custom_test(void) for (x = 0; x < sizeof(ia5buf); x++) { ia5buf[x] = 'a'; } for (x = 0; x < sizeof(printbuf); x++) { printbuf[x] = 'a'; } for (x = 0; x < sizeof(utf8buf)/sizeof(utf8buf[0]); x++) { utf8buf[x] = L'a'; } - integer = 10000; + integer = 1; boolean[0] = 1; for (x = 0; x < sizeof(oidbuf)/sizeof(oidbuf[0]); x++) { oidbuf[x] = x + 1; } - DO(mp_init(&mpinteger)); + DO(ltc_mp_init(&mpinteger)); n = sizeof(types)/sizeof(types[0]); for (x = 0; x < n * 2; x++) { @@ -1173,7 +1173,7 @@ static int der_choice_n_custom_test(void) DO(der_decode_custom_type(custbuf, custlen, root)); } } - mp_clear(mpinteger); + ltc_mp_clear(mpinteger); return 0; } @@ -1358,13 +1358,13 @@ static void der_Xcode_test(void) DO(der_encode_sequence(ttex_neg_int, 1, buf, &i)); der_sequence_free(list); - DO(mp_init(&mpinteger)); + DO(ltc_mp_init(&mpinteger)); LTC_SET_ASN1(ttex_neg_int, 0, LTC_ASN1_TELETEX_STRING, buf, sizeof(buf)); LTC_SET_ASN1(ttex_neg_int, 1, LTC_ASN1_INTEGER, mpinteger, 1); DO(der_decode_sequence(teletex_neg_int, sizeof(teletex_neg_int), ttex_neg_int, 2)); - mp_clear(mpinteger); + ltc_mp_clear(mpinteger); } #ifdef LTC_TEST_READDIR @@ -1406,18 +1406,18 @@ static void s_der_regression_test(void) void *x, *y; ltc_asn1_list seq[2]; ltc_asn1_list *l; - mp_init_multi(&x, &y, LTC_NULL); + ltc_mp_init_multi(&x, &y, LTC_NULL); LTC_SET_ASN1(seq, 0, LTC_ASN1_INTEGER, x, 1UL); LTC_SET_ASN1(seq, 1, LTC_ASN1_INTEGER, y, 1UL); SHOULD_FAIL(der_decode_sequence(s_broken_sequence, sizeof(s_broken_sequence), seq, 2)); - mp_cleanup_multi(&y, &x, LTC_NULL); + ltc_mp_cleanup_multi(&y, &x, LTC_NULL); len = sizeof(s_broken_sequence); - mp_init_multi(&x, &y, LTC_NULL); + ltc_mp_init_multi(&x, &y, LTC_NULL); LTC_SET_ASN1(seq, 0, LTC_ASN1_INTEGER, x, 1UL); LTC_SET_ASN1(seq, 1, LTC_ASN1_INTEGER, y, 1UL); SHOULD_FAIL_WITH(der_decode_sequence(s_addtl_bytes, sizeof(s_addtl_bytes), seq, 2), CRYPT_INPUT_TOO_LONG); - mp_cleanup_multi(&y, &x, LTC_NULL); + ltc_mp_cleanup_multi(&y, &x, LTC_NULL); len = sizeof(s_addtl_bytes); s_der_decode_print(s_addtl_bytes, &len); @@ -1506,7 +1506,7 @@ static void der_toolong_test(void) der_sequence_free(list); } - mp_init_multi(&int1, &int2, LTC_NULL); + ltc_mp_init_multi(&int1, &int2, LTC_NULL); LTC_SET_ASN1(seqint, 0, LTC_ASN1_INTEGER, int1, 1); LTC_SET_ASN1(seqint, 1, LTC_ASN1_INTEGER, int2, 1); @@ -1542,7 +1542,7 @@ static void der_toolong_test(void) der_sequence_free(list); } - mp_clear_multi(int1, int2, LTC_NULL); + ltc_mp_deinit_multi(int1, int2, LTC_NULL); LTC_SET_ASN1(seqoid, 0, LTC_ASN1_OBJECT_IDENTIFIER, oid, sizeof(oid)/sizeof(oid[0])); LTC_SET_ASN1(seqoid, 1, LTC_ASN1_NULL, NULL, 0); @@ -1627,32 +1627,32 @@ int der_test(void) unsigned char buf[3][2048]; void *a, *b, *c, *d, *e, *f, *g; - const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d }; - const unsigned long rsa_oid[] = { 1, 2, 840, 113549 }; + static const unsigned char rsa_oid_der[] = { 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d }; + static const unsigned long rsa_oid[] = { 1, 2, 840, 113549 }; - const unsigned char rsa_ia5[] = "test1@rsa.com"; - const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31, + static const unsigned char rsa_ia5[] = "test1@rsa.com"; + static const unsigned char rsa_ia5_der[] = { 0x16, 0x0d, 0x74, 0x65, 0x73, 0x74, 0x31, 0x40, 0x72, 0x73, 0x61, 0x2e, 0x63, 0x6f, 0x6d }; unsigned long rsa_ia5_len = XSTRLEN((char*)rsa_ia5); - const unsigned char rsa_printable[] = "Test User 1"; - const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, + static const unsigned char rsa_printable[] = "Test User 1"; + static const unsigned char rsa_printable_der[] = { 0x13, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x65, 0x72, 0x20, 0x31 }; unsigned long rsa_printable_len = XSTRLEN((char*)rsa_printable); - const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 }; - const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 }; + static const ltc_utctime rsa_time1 = { 91, 5, 6, 16, 45, 40, 1, 7, 0 }; + static const ltc_utctime rsa_time2 = { 91, 5, 6, 23, 45, 40, 0, 0, 0 }; ltc_utctime tmp_time; - const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 }; - const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a }; + static const unsigned char rsa_time1_der[] = { 0x17, 0x11, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x31, 0x36, 0x34, 0x35, 0x34, 0x30, 0x2D, 0x30, 0x37, 0x30, 0x30 }; + static const unsigned char rsa_time2_der[] = { 0x17, 0x0d, 0x39, 0x31, 0x30, 0x35, 0x30, 0x36, 0x32, 0x33, 0x34, 0x35, 0x34, 0x30, 0x5a }; - const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E }; - const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E }; - const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 }; - const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 }; - const wchar_t utf8_3[] = { 0x05E9, 0x05DC, 0x05D5, 0x05DD }; - const unsigned char utf8_3_der[] = { 0x0C, 0x08, 0xD7, 0xA9, 0xD7, 0x9C, 0xD7, 0x95, 0xD7, 0x9D }; + static const wchar_t utf8_1[] = { 0x0041, 0x2262, 0x0391, 0x002E }; + static const unsigned char utf8_1_der[] = { 0x0C, 0x07, 0x41, 0xE2, 0x89, 0xA2, 0xCE, 0x91, 0x2E }; + static const wchar_t utf8_2[] = { 0xD55C, 0xAD6D, 0xC5B4 }; + static const unsigned char utf8_2_der[] = { 0x0C, 0x09, 0xED, 0x95, 0x9C, 0xEA, 0xB5, 0xAD, 0xEC, 0x96, 0xB4 }; + static const wchar_t utf8_3[] = { 0x05E9, 0x05DC, 0x05D5, 0x05DD }; + static const unsigned char utf8_3_der[] = { 0x0C, 0x08, 0xD7, 0xA9, 0xD7, 0x9C, 0xD7, 0x95, 0xD7, 0x9D }; unsigned char utf8_buf[32]; wchar_t utf8_out[32]; @@ -1701,7 +1701,7 @@ int der_test(void) y >>= 3; } - DO(mp_init_multi(&a, &b, &c, &d, &e, &f, &g, LTC_NULL)); + DO(ltc_mp_init_multi(&a, &b, &c, &d, &e, &f, &g, LTC_NULL)); for (zz = 0; zz < 16; zz++) { #ifdef USE_TFM for (z = 0; z < 256; z++) { @@ -1709,17 +1709,17 @@ int der_test(void) for (z = 0; z < 1024; z++) { #endif ENSURE(yarrow_read(buf[0], z, &yarrow_prng) == z); - DO(mp_read_unsigned_bin(a, buf[0], z)); -/* if (mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */ + DO(ltc_mp_read_unsigned_bin(a, buf[0], z)); +/* if (ltc_mp_iszero(a) == LTC_MP_NO) { a.sign = buf[0][0] & 1 ? LTC_MP_ZPOS : LTC_MP_NEG; } */ x = sizeof(buf[0]); DO(der_encode_integer(a, buf[0], &x)); DO(der_length_integer(a, &y)); if (y != x) { fprintf(stderr, "DER INTEGER size mismatch %lu != %lu\n", y, x); return 1; } - mp_set_int(b, 0); + ltc_mp_set_int(b, 0); DO(der_decode_integer(buf[0], y, b)); - if (y != x || mp_cmp(a, b) != LTC_MP_EQ) { + if (y != x || ltc_mp_cmp(a, b) != LTC_MP_EQ) { fprintf(stderr, "%lu: %lu vs %lu\n", z, x, y); - mp_clear_multi(a, b, c, d, e, f, g, LTC_NULL); + ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL); return 1; } } @@ -1730,37 +1730,37 @@ int der_test(void) for (z = 1; z < 4; z++) { ENSURE(yarrow_read(buf[2], z, &yarrow_prng) == z); /* encode with normal */ - DO(mp_read_unsigned_bin(a, buf[2], z)); + DO(ltc_mp_read_unsigned_bin(a, buf[2], z)); x = sizeof(buf[0]); DO(der_encode_integer(a, buf[0], &x)); /* encode with short */ y = sizeof(buf[1]); - DO(der_encode_short_integer(mp_get_int(a), buf[1], &y)); + DO(der_encode_short_integer(ltc_mp_get_int(a), buf[1], &y)); if (x != y || memcmp(buf[0], buf[1], x)) { - fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu, 0x%lX\n", x, y, mp_get_int(a)); + fprintf(stderr, "DER INTEGER short encoding failed, %lu, %lu, 0x%lX\n", x, y, ltc_mp_get_int(a)); for (zz = 0; zz < z; zz++) fprintf(stderr, "%02x ", buf[2][zz]); fprintf(stderr, "\n"); for (z = 0; z < x; z++) fprintf(stderr, "%02x ", buf[0][z]); fprintf(stderr, "\n"); for (z = 0; z < y; z++) fprintf(stderr, "%02x ", buf[1][z]); fprintf(stderr, "\n"); - mp_clear_multi(a, b, c, d, e, f, g, LTC_NULL); + ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL); return 1; } /* decode it */ x = 0; DO(der_decode_short_integer(buf[1], y, &x)); - if (x != mp_get_int(a)) { - fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, mp_get_int(a)); - mp_clear_multi(a, b, c, d, e, f, g, LTC_NULL); + if (x != ltc_mp_get_int(a)) { + fprintf(stderr, "DER INTEGER short decoding failed, %lu, %lu\n", x, ltc_mp_get_int(a)); + ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL); return 1; } } } - mp_clear_multi(a, b, c, d, e, f, g, LTC_NULL); + ltc_mp_deinit_multi(a, b, c, d, e, f, g, LTC_NULL); /* Test bit string */ diff --git a/tests/dh_test.c b/tests/dh_test.c index 08f38027c..1900eb030 100644 --- a/tests/dh_test.c +++ b/tests/dh_test.c @@ -9,35 +9,35 @@ static int s_prime_test(void) void *p, *g, *tmp; int x, err, primality; - if ((err = mp_init_multi(&p, &g, &tmp, LTC_NULL)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_init_multi(&p, &g, &tmp, LTC_NULL)) != CRYPT_OK) { goto error; } for (x = 0; ltc_dh_sets[x].size != 0; x++) { /* tfm has a problem with larger sizes */ if ((strcmp(ltc_mp.name, "TomsFastMath") == 0) && (ltc_dh_sets[x].size > 256)) break; - if ((err = mp_read_radix(g, ltc_dh_sets[x].base, 16)) != CRYPT_OK) { goto error; } - if ((err = mp_read_radix(p, ltc_dh_sets[x].prime, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(g, ltc_dh_sets[x].base, 16)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_read_radix(p, ltc_dh_sets[x].prime, 16)) != CRYPT_OK) { goto error; } /* ensure p is prime */ - if ((err = mp_prime_is_prime(p, 8, &primality)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_prime_is_prime(p, 8, &primality)) != CRYPT_OK) { goto done; } if (primality != LTC_MP_YES ) { err = CRYPT_FAIL_TESTVECTOR; goto done; } - if ((err = mp_sub_d(p, 1, tmp)) != CRYPT_OK) { goto error; } - if ((err = mp_div_2(tmp, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_sub_d(p, 1, tmp)) != CRYPT_OK) { goto error; } + if ((err = ltc_mp_div_2(tmp, tmp)) != CRYPT_OK) { goto error; } /* ensure (p-1)/2 is prime */ - if ((err = mp_prime_is_prime(tmp, 8, &primality)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_prime_is_prime(tmp, 8, &primality)) != CRYPT_OK) { goto done; } if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; goto done; } /* now see if g^((p-1)/2) mod p is in fact 1 */ - if ((err = mp_exptmod(g, tmp, p, tmp)) != CRYPT_OK) { goto error; } - if (mp_cmp_d(tmp, 1)) { + if ((err = ltc_mp_exptmod(g, tmp, p, tmp)) != CRYPT_OK) { goto error; } + if (ltc_mp_cmp_d(tmp, 1)) { err = CRYPT_FAIL_TESTVECTOR; goto done; } @@ -45,7 +45,7 @@ static int s_prime_test(void) err = CRYPT_OK; error: done: - mp_clear_multi(tmp, g, p, LTC_NULL); + ltc_mp_deinit_multi(tmp, g, p, LTC_NULL); return err; } @@ -119,14 +119,14 @@ static int s_dhparam_test(void) DO(dh_set_pg_dhparam(dhparam_der, sizeof(dhparam_der), &k)); DO(dh_generate_key(&yarrow_prng, find_prng ("yarrow"), &k)); - if (mp_unsigned_bin_size(k.prime) > sizeof(buf)) { + if (ltc_mp_unsigned_bin_size(k.prime) > sizeof(buf)) { printf("dhparam_test: short buf\n"); dh_free(&k); return CRYPT_ERROR; } - DO(mp_to_unsigned_bin(k.prime, buf)); + DO(ltc_mp_to_unsigned_bin(k.prime, buf)); DO(do_compare_testvector(buf, sizeof(prime), prime, sizeof(prime), "dhparam_test: prime mismatch", 1)); - if (mp_cmp_d(k.base, 2) != LTC_MP_EQ) { + if (ltc_mp_cmp_d(k.base, 2) != LTC_MP_EQ) { printf("dhparam_test: base mismatch\n"); dh_free(&k); return CRYPT_ERROR; @@ -296,11 +296,11 @@ static int s_set_test(void) DO(dh_set_pg(test[i].p, test[i].plen, test[i].g, test[i].glen, &k3)); DO(dh_generate_key(&yarrow_prng, find_prng("yarrow"), &k3)); - len = mp_unsigned_bin_size(k3.prime); - DO(mp_to_unsigned_bin(k3.prime, buf)); + len = ltc_mp_unsigned_bin_size(k3.prime); + DO(ltc_mp_to_unsigned_bin(k3.prime, buf)); DO(do_compare_testvector(buf, len, pbin, sizeof(pbin), "radix_test: dh_make_key_ex prime mismatch", i*10 + 8)); - len = mp_unsigned_bin_size(k3.base); - DO(mp_to_unsigned_bin(k3.base, buf)); + len = ltc_mp_unsigned_bin_size(k3.base); + DO(ltc_mp_to_unsigned_bin(k3.base, buf)); DO(do_compare_testvector(buf, len, gbin, sizeof(gbin), "radix_test: dh_make_key_ex base mismatch", i*10 + 9)); dh_free(&k3); } diff --git a/tests/dsa_test.c b/tests/dsa_test.c index 18ade7e6f..f997af648 100644 --- a/tests/dsa_test.c +++ b/tests/dsa_test.c @@ -128,12 +128,12 @@ int dsa_key_cmp(const int should_type, const dsa_key *should, const dsa_key *is) { if (should_type != is->type) return CRYPT_ERROR; if (should_type == PK_PRIVATE) { - if (mp_cmp(should->x, is->x) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->x, is->x) != LTC_MP_EQ) return CRYPT_ERROR; } - if (mp_cmp(should->y, is->y) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->g, is->g) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->p, is->p) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->q, is->q) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->y, is->y) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->g, is->g) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->p, is->p) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->q, is->q) != LTC_MP_EQ) return CRYPT_ERROR; if (should->qord != is->qord) return CRYPT_ERROR; return CRYPT_OK; } diff --git a/tests/ecc_test.c b/tests/ecc_test.c index 0a7a449e8..74c65fcf8 100644 --- a/tests/ecc_test.c +++ b/tests/ecc_test.c @@ -149,7 +149,7 @@ static int s_ecc_test_shamir(void) int x, y, z; unsigned char buf[ECC_BUF_SIZE]; - DO(mp_init_multi(&kA, &kB, &rA, &rB, &modulus, &a, &mu, &ma, LTC_NULL)); + DO(ltc_mp_init_multi(&kA, &kB, &rA, &rB, &modulus, &a, &mu, &ma, LTC_NULL)); LTC_ARGCHK((G = ltc_ecc_new_point()) != NULL); LTC_ARGCHK((A = ltc_ecc_new_point()) != NULL); LTC_ARGCHK((B = ltc_ecc_new_point()) != NULL); @@ -159,27 +159,27 @@ static int s_ecc_test_shamir(void) for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) { /* get the base point */ for (z = 0; ltc_ecc_curves[z].prime != NULL; z++) { - DO(mp_read_radix(modulus, ltc_ecc_curves[z].prime, 16)); - if (sizes[x] <= mp_unsigned_bin_size(modulus)) break; + DO(ltc_mp_read_radix(modulus, ltc_ecc_curves[z].prime, 16)); + if (sizes[x] <= ltc_mp_unsigned_bin_size(modulus)) break; } LTC_ARGCHK(ltc_ecc_curves[z].prime != NULL); /* load it */ - DO(mp_read_radix(G->x, ltc_ecc_curves[z].Gx, 16)); - DO(mp_read_radix(G->y, ltc_ecc_curves[z].Gy, 16)); - DO(mp_set(G->z, 1)); - DO(mp_read_radix(a, ltc_ecc_curves[z].A, 16)); - DO(mp_montgomery_setup(modulus, &mp)); - DO(mp_montgomery_normalization(mu, modulus)); - DO(mp_mulmod(a, mu, modulus, ma)); + DO(ltc_mp_read_radix(G->x, ltc_ecc_curves[z].Gx, 16)); + DO(ltc_mp_read_radix(G->y, ltc_ecc_curves[z].Gy, 16)); + DO(ltc_mp_set(G->z, 1)); + DO(ltc_mp_read_radix(a, ltc_ecc_curves[z].A, 16)); + DO(ltc_mp_montgomery_setup(modulus, &mp)); + DO(ltc_mp_montgomery_normalization(mu, modulus)); + DO(ltc_mp_mulmod(a, mu, modulus, ma)); /* do 100 random tests */ for (y = 0; y < 100; y++) { /* pick a random r1, r2 */ ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]); - DO(mp_read_unsigned_bin(rA, buf, sizes[x])); + DO(ltc_mp_read_unsigned_bin(rA, buf, sizes[x])); ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]); - DO(mp_read_unsigned_bin(rB, buf, sizes[x])); + DO(ltc_mp_read_unsigned_bin(rB, buf, sizes[x])); /* compute rA * G = A */ DO(ltc_mp.ecc_ptmul(rA, G, A, a, modulus, 1)); @@ -189,9 +189,9 @@ static int s_ecc_test_shamir(void) /* pick a random kA, kB */ ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]); - DO(mp_read_unsigned_bin(kA, buf, sizes[x])); + DO(ltc_mp_read_unsigned_bin(kA, buf, sizes[x])); ENSURE(yarrow_read(buf, sizes[x], &yarrow_prng) == sizes[x]); - DO(mp_read_unsigned_bin(kB, buf, sizes[x])); + DO(ltc_mp_read_unsigned_bin(kB, buf, sizes[x])); /* now, compute kA*A + kB*B = C1 using the older method */ DO(ltc_mp.ecc_ptmul(kA, A, C1, a, modulus, 0)); @@ -203,19 +203,19 @@ static int s_ecc_test_shamir(void) DO(ltc_mp.ecc_mul2add(A, kA, B, kB, C2, ma, modulus)); /* is they the sames? */ - if ((mp_cmp(C1->x, C2->x) != LTC_MP_EQ) || (mp_cmp(C1->y, C2->y) != LTC_MP_EQ) || (mp_cmp(C1->z, C2->z) != LTC_MP_EQ)) { + if ((ltc_mp_cmp(C1->x, C2->x) != LTC_MP_EQ) || (ltc_mp_cmp(C1->y, C2->y) != LTC_MP_EQ) || (ltc_mp_cmp(C1->z, C2->z) != LTC_MP_EQ)) { fprintf(stderr, "ECC failed shamir test: size=%d, testno=%d\n", sizes[x], y); return 1; } } - mp_montgomery_free(mp); + ltc_mp_montgomery_free(mp); } ltc_ecc_del_point(C2); ltc_ecc_del_point(C1); ltc_ecc_del_point(B); ltc_ecc_del_point(A); ltc_ecc_del_point(G); - mp_clear_multi(kA, kB, rA, rB, modulus, a, mu, ma, LTC_NULL); + ltc_mp_deinit_multi(kA, kB, rA, rB, modulus, a, mu, ma, LTC_NULL); return 0; } #endif @@ -254,29 +254,29 @@ static int s_ecc_issue108(void) const ltc_ecc_curve* dp; /* init */ - if ((err = mp_init_multi(&modulus, &order, &a, LTC_NULL)) != CRYPT_OK) { return err; } + if ((err = ltc_mp_init_multi(&modulus, &order, &a, LTC_NULL)) != CRYPT_OK) { return err; } Q = ltc_ecc_new_point(); Result = ltc_ecc_new_point(); /* ECC-224 AKA SECP224R1 */ if ((err = ecc_find_curve("SECP224R1", &dp)) != CRYPT_OK) { goto done; } /* read A */ - if ((err = mp_read_radix(a, dp->A, 16)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_read_radix(a, dp->A, 16)) != CRYPT_OK) { goto done; } /* read modulus */ - if ((err = mp_read_radix(modulus, dp->prime, 16)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_read_radix(modulus, dp->prime, 16)) != CRYPT_OK) { goto done; } /* read order */ - if ((err = mp_read_radix(order, dp->order, 16)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_read_radix(order, dp->order, 16)) != CRYPT_OK) { goto done; } /* read Q */ - if ((err = mp_read_radix(Q->x, "EA3745501BBC6A70BBFDD8AEEDB18CF5073C6DC9AA7CBB5915170D60", 16)) != CRYPT_OK) { goto done; } - if ((err = mp_read_radix(Q->y, "6C9CB8E68AABFEC989CAC5E2326E0448B7E69C3E56039BA21A44FDAC", 16)) != CRYPT_OK) { goto done; } - mp_set(Q->z, 1); + if ((err = ltc_mp_read_radix(Q->x, "EA3745501BBC6A70BBFDD8AEEDB18CF5073C6DC9AA7CBB5915170D60", 16)) != CRYPT_OK) { goto done; } + if ((err = ltc_mp_read_radix(Q->y, "6C9CB8E68AABFEC989CAC5E2326E0448B7E69C3E56039BA21A44FDAC", 16)) != CRYPT_OK) { goto done; } + ltc_mp_set(Q->z, 1); /* calculate nQ */ if ((err = ltc_mp.ecc_ptmul(order, Q, Result, a, modulus, 1)) != CRYPT_OK) { goto done; } done: ltc_ecc_del_point(Result); ltc_ecc_del_point(Q); - mp_clear_multi(modulus, order, a, LTC_NULL); + ltc_mp_deinit_multi(modulus, order, a, LTC_NULL); return err; } @@ -339,12 +339,12 @@ static int s_ecc_test_mp(void) int i, err, primality; char buf[4096]; - DO(mp_init_multi(&modulus, &order, &a, LTC_NULL)); + DO(ltc_mp_init_multi(&modulus, &order, &a, LTC_NULL)); G = ltc_ecc_new_point(); GG = ltc_ecc_new_point(); if (G == NULL || GG == NULL) { - mp_clear_multi(modulus, order, LTC_NULL); + ltc_mp_deinit_multi(modulus, order, LTC_NULL); ltc_ecc_del_point(G); ltc_ecc_del_point(GG); return CRYPT_MEM; @@ -353,34 +353,34 @@ static int s_ecc_test_mp(void) err = CRYPT_OK; for (i = 0; ltc_ecc_curves[i].prime != NULL; i++) { - DO(mp_read_radix(a, ltc_ecc_curves[i].A, 16)); - DO(mp_read_radix(modulus, ltc_ecc_curves[i].prime, 16)); - DO(mp_read_radix(order, ltc_ecc_curves[i].order, 16)); + DO(ltc_mp_read_radix(a, ltc_ecc_curves[i].A, 16)); + DO(ltc_mp_read_radix(modulus, ltc_ecc_curves[i].prime, 16)); + DO(ltc_mp_read_radix(order, ltc_ecc_curves[i].order, 16)); /* is prime actually prime? */ - DO(mp_prime_is_prime(modulus, 8, &primality)); + DO(ltc_mp_prime_is_prime(modulus, 8, &primality)); if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; - mp_tohex(modulus, buf); + ltc_mp_tohex(modulus, buf); printf("Modulus failed prime check: %s\n", buf); } /* is order prime ? */ - DO(mp_prime_is_prime(order, 8, &primality)); + DO(ltc_mp_prime_is_prime(order, 8, &primality)); if (primality == 0) { err = CRYPT_FAIL_TESTVECTOR; - mp_tohex(order, buf); + ltc_mp_tohex(order, buf); printf("Order failed prime check: %s\n", buf); } - DO(mp_read_radix(G->x, ltc_ecc_curves[i].Gx, 16)); - DO(mp_read_radix(G->y, ltc_ecc_curves[i].Gy, 16)); - mp_set(G->z, 1); + DO(ltc_mp_read_radix(G->x, ltc_ecc_curves[i].Gx, 16)); + DO(ltc_mp_read_radix(G->y, ltc_ecc_curves[i].Gy, 16)); + ltc_mp_set(G->z, 1); /* then we should have G == (order + 1)G */ - DO(mp_add_d(order, 1, order)); + DO(ltc_mp_add_d(order, 1, order)); DO(ltc_mp.ecc_ptmul(order, G, GG, a, modulus, 1)); - if (mp_cmp(G->x, GG->x) != LTC_MP_EQ || mp_cmp(G->y, GG->y) != LTC_MP_EQ) { + if (ltc_mp_cmp(G->x, GG->x) != LTC_MP_EQ || ltc_mp_cmp(G->y, GG->y) != LTC_MP_EQ) { err = CRYPT_FAIL_TESTVECTOR; } if (err != CRYPT_OK) { @@ -391,7 +391,7 @@ static int s_ecc_test_mp(void) done: ltc_ecc_del_point(GG); ltc_ecc_del_point(G); - mp_clear_multi(order, modulus, a, LTC_NULL); + ltc_mp_deinit_multi(order, modulus, a, LTC_NULL); return err; } @@ -532,16 +532,16 @@ int ecc_key_cmp(const int should_type, const ecc_key *should, const ecc_key *is) { if (should_type != is->type) return CRYPT_ERROR; if (should_type == PK_PRIVATE) { - if (mp_cmp(should->k, is->k) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->k, is->k) != LTC_MP_EQ) return CRYPT_ERROR; } - if (mp_cmp(should->dp.prime, is->dp.prime) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->dp.A, is->dp.A) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->dp.B, is->dp.B) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->dp.order, is->dp.order) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->dp.base.x, is->dp.base.x) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->dp.base.y, is->dp.base.y) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->pubkey.x, is->pubkey.x) != LTC_MP_EQ) return CRYPT_ERROR; - if (mp_cmp(should->pubkey.y, is->pubkey.y) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->dp.prime, is->dp.prime) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->dp.A, is->dp.A) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->dp.B, is->dp.B) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->dp.order, is->dp.order) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->dp.base.x, is->dp.base.x) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->dp.base.y, is->dp.base.y) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->pubkey.x, is->pubkey.x) != LTC_MP_EQ) return CRYPT_ERROR; + if (ltc_mp_cmp(should->pubkey.y, is->pubkey.y) != LTC_MP_EQ) return CRYPT_ERROR; if (should->dp.size != is->dp.size) return CRYPT_ERROR; if (should->dp.cofactor != is->dp.cofactor) return CRYPT_ERROR; return CRYPT_OK; diff --git a/tests/pkcs_1_eme_test.c b/tests/pkcs_1_eme_test.c index 09d7c6da3..2e0fc61c4 100644 --- a/tests/pkcs_1_eme_test.c +++ b/tests/pkcs_1_eme_test.c @@ -24,17 +24,17 @@ int pkcs_1_eme_test(void) for (i = 0; i < sizeof(testcases_eme)/sizeof(testcases_eme[0]); ++i) { testcase_t* t = &testcases_eme[i]; rsa_key k, *key = &k; - DOX(mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, + DOX(ltc_mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL), t->name); - DOX(mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); - DOX(mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); - DOX(mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); - DOX(mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); - DOX(mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); - DOX(mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); - DOX(mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); - DOX(mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); key->type = PK_PRIVATE; for (j = 0; j < sizeof(t->data)/sizeof(t->data[0]); ++j) { @@ -49,7 +49,7 @@ int pkcs_1_eme_test(void) DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name); } /* for */ - mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); + ltc_mp_deinit_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); } /* for */ unregister_prng(no_prng_desc); diff --git a/tests/pkcs_1_emsa_test.c b/tests/pkcs_1_emsa_test.c index 7f486bb75..405138a69 100644 --- a/tests/pkcs_1_emsa_test.c +++ b/tests/pkcs_1_emsa_test.c @@ -21,17 +21,17 @@ int pkcs_1_emsa_test(void) for (i = 0; i < sizeof(testcases_emsa)/sizeof(testcases_emsa[0]); ++i) { testcase_t* t = &testcases_emsa[i]; rsa_key k, *key = &k; - DOX(mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, + DOX(ltc_mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL), t->name); - DOX(mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); - DOX(mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); - DOX(mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); - DOX(mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); - DOX(mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); - DOX(mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); - DOX(mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); - DOX(mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); key->type = PK_PRIVATE; for (j = 0; j < sizeof(t->data)/sizeof(t->data[0]); ++j) { @@ -46,7 +46,7 @@ int pkcs_1_emsa_test(void) DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name); } /* for */ - mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); + ltc_mp_deinit_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); } /* for */ return 0; diff --git a/tests/pkcs_1_oaep_test.c b/tests/pkcs_1_oaep_test.c index 739105321..b0d484bb0 100644 --- a/tests/pkcs_1_oaep_test.c +++ b/tests/pkcs_1_oaep_test.c @@ -24,17 +24,17 @@ int pkcs_1_oaep_test(void) for (i = 0; i < sizeof(testcases_oaep)/sizeof(testcases_oaep[0]); ++i) { testcase_t* t = &testcases_oaep[i]; rsa_key k, *key = &k; - DOX(mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, + DOX(ltc_mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL), t->name); - DOX(mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); - DOX(mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); - DOX(mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); - DOX(mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); - DOX(mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); - DOX(mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); - DOX(mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); - DOX(mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); key->type = PK_PRIVATE; for (j = 0; j < sizeof(t->data)/sizeof(t->data[0]); ++j) { @@ -49,7 +49,7 @@ int pkcs_1_oaep_test(void) DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name); } /* for */ - mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); + ltc_mp_deinit_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); } /* for */ unregister_prng(no_prng_desc); diff --git a/tests/pkcs_1_pss_test.c b/tests/pkcs_1_pss_test.c index 82bdb0767..cea2a1a6a 100644 --- a/tests/pkcs_1_pss_test.c +++ b/tests/pkcs_1_pss_test.c @@ -24,17 +24,17 @@ int pkcs_1_pss_test(void) for (i = 0; i < sizeof(testcases_pss)/sizeof(testcases_pss[0]); ++i) { testcase_t* t = &testcases_pss[i]; rsa_key k, *key = &k; - DOX(mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, + DOX(ltc_mp_init_multi(&key->e, &key->d, &key->N, &key->dQ, &key->dP, &key->qP, &key->p, &key->q, NULL), t->name); - DOX(mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); - DOX(mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); - DOX(mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); - DOX(mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); - DOX(mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); - DOX(mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); - DOX(mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); - DOX(mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->e, t->rsa.e, t->rsa.e_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->d, t->rsa.d, t->rsa.d_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->N, t->rsa.n, t->rsa.n_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dQ, t->rsa.dQ, t->rsa.dQ_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->dP, t->rsa.dP, t->rsa.dP_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->qP, t->rsa.qInv, t->rsa.qInv_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->q, t->rsa.q, t->rsa.q_l), t->name); + DOX(ltc_mp_read_unsigned_bin(key->p, t->rsa.p, t->rsa.p_l), t->name); key->type = PK_PRIVATE; for (j = 0; j < sizeof(t->data)/sizeof(t->data[0]); ++j) { @@ -50,7 +50,7 @@ int pkcs_1_pss_test(void) DOX(stat == 1?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name); } /* for */ - mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); + ltc_mp_deinit_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, LTC_NULL); } /* for */ unregister_prng(no_prng_desc); diff --git a/tests/rsa_test.c b/tests/rsa_test.c index 266d395d7..b0fe5a459 100644 --- a/tests/rsa_test.c +++ b/tests/rsa_test.c @@ -271,22 +271,22 @@ int rsa_key_cmp(const int should_type, const rsa_key *should, const rsa_key *is) if(should_type != is->type) return CRYPT_ERROR; if(should_type == PK_PRIVATE) { - if(mp_cmp(should->q, is->q) != LTC_MP_EQ) + if(ltc_mp_cmp(should->q, is->q) != LTC_MP_EQ) return CRYPT_ERROR; - if(mp_cmp(should->p, is->p) != LTC_MP_EQ) + if(ltc_mp_cmp(should->p, is->p) != LTC_MP_EQ) return CRYPT_ERROR; - if(mp_cmp(should->qP, is->qP) != LTC_MP_EQ) + if(ltc_mp_cmp(should->qP, is->qP) != LTC_MP_EQ) return CRYPT_ERROR; - if(mp_cmp(should->dP, is->dP) != LTC_MP_EQ) + if(ltc_mp_cmp(should->dP, is->dP) != LTC_MP_EQ) return CRYPT_ERROR; - if(mp_cmp(should->dQ, is->dQ) != LTC_MP_EQ) + if(ltc_mp_cmp(should->dQ, is->dQ) != LTC_MP_EQ) return CRYPT_ERROR; - if(mp_cmp(should->d, is->d) != LTC_MP_EQ) + if(ltc_mp_cmp(should->d, is->d) != LTC_MP_EQ) return CRYPT_ERROR; } - if(mp_cmp(should->N, is->N) != LTC_MP_EQ) + if(ltc_mp_cmp(should->N, is->N) != LTC_MP_EQ) return CRYPT_ERROR; - if(mp_cmp(should->e, is->e) != LTC_MP_EQ) + if(ltc_mp_cmp(should->e, is->e) != LTC_MP_EQ) return CRYPT_ERROR; return CRYPT_OK; } @@ -487,19 +487,19 @@ int rsa_test(void) /* make 10 random key */ for (cnt = 0; cnt < 10; cnt++) { DO(rsa_make_key(&yarrow_prng, prng_idx, 1024/8, 65537, &key)); - if (mp_count_bits(key.N) != 1024) { - fprintf(stderr, "rsa_1024 key modulus has %d bits\n", mp_count_bits(key.N)); + if (ltc_mp_count_bits(key.N) != 1024) { + fprintf(stderr, "rsa_1024 key modulus has %d bits\n", ltc_mp_count_bits(key.N)); -len = mp_unsigned_bin_size(key.N); -mp_to_unsigned_bin(key.N, tmp); +len = ltc_mp_unsigned_bin_size(key.N); +ltc_mp_to_unsigned_bin(key.N, tmp); print_hex("N", tmp, len); -len = mp_unsigned_bin_size(key.p); -mp_to_unsigned_bin(key.p, tmp); +len = ltc_mp_unsigned_bin_size(key.p); +ltc_mp_to_unsigned_bin(key.p, tmp); print_hex("p", tmp, len); -len = mp_unsigned_bin_size(key.q); -mp_to_unsigned_bin(key.q, tmp); +len = ltc_mp_unsigned_bin_size(key.q); +ltc_mp_to_unsigned_bin(key.q, tmp); print_hex("q", tmp, len); return 1; diff --git a/tests/ssh_test.c b/tests/ssh_test.c index 4760c644d..990db726d 100644 --- a/tests/ssh_test.c +++ b/tests/ssh_test.c @@ -127,13 +127,13 @@ static int s_ssh_encoding_test(void) /* mpint */ - if ((err = mp_init_multi(&zero, &v, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&zero, &v, LTC_NULL)) != CRYPT_OK) { return err; } buflen = BUFSIZE; zeromem(buffer, BUFSIZE); - DO(mp_set(zero, 0)); + DO(ltc_mp_set(zero, 0)); DO(ssh_encode_sequence_multi(buffer, &buflen, LTC_SSHDATA_MPINT, zero, LTC_SSHDATA_EOL, NULL)); @@ -141,7 +141,7 @@ static int s_ssh_encoding_test(void) buflen = BUFSIZE; zeromem(buffer, BUFSIZE); - DO(mp_read_radix(v, "9a378f9b2e332a7", 16)); + DO(ltc_mp_read_radix(v, "9a378f9b2e332a7", 16)); DO(ssh_encode_sequence_multi(buffer, &buflen, LTC_SSHDATA_MPINT, v, LTC_SSHDATA_EOL, NULL)); @@ -149,13 +149,13 @@ static int s_ssh_encoding_test(void) buflen = BUFSIZE; zeromem(buffer, BUFSIZE); - DO(mp_set(v, 0x80)); + DO(ltc_mp_set(v, 0x80)); DO(ssh_encode_sequence_multi(buffer, &buflen, LTC_SSHDATA_MPINT, v, LTC_SSHDATA_EOL, NULL)); COMPARE_TESTVECTOR(buffer, buflen, mpint3, sizeof(mpint3), "enc-mpint", 3); - mp_clear_multi(v, zero, LTC_NULL); + ltc_mp_deinit_multi(v, zero, LTC_NULL); /* name-list */ @@ -269,7 +269,7 @@ static int s_ssh_decoding_test(void) ENSURE(strlen("testing") + 4 == len); /* mpint */ - if ((err = mp_init_multi(&u, &v, LTC_NULL)) != CRYPT_OK) { + if ((err = ltc_mp_init_multi(&u, &v, LTC_NULL)) != CRYPT_OK) { return err; } @@ -277,25 +277,25 @@ static int s_ssh_decoding_test(void) DO(ssh_decode_sequence_multi(mpint1, &len, LTC_SSHDATA_MPINT, v, LTC_SSHDATA_EOL, NULL)); - ENSURE(mp_cmp_d(v, 0) == LTC_MP_EQ); + ENSURE(ltc_mp_cmp_d(v, 0) == LTC_MP_EQ); ENSURE(sizeof(mpint1) == len); len = sizeof(mpint2); DO(ssh_decode_sequence_multi(mpint2, &len, LTC_SSHDATA_MPINT, v, LTC_SSHDATA_EOL, NULL)); - DO(mp_read_radix(u, "9a378f9b2e332a7", 16)); - ENSURE(mp_cmp(u, v) == LTC_MP_EQ); + DO(ltc_mp_read_radix(u, "9a378f9b2e332a7", 16)); + ENSURE(ltc_mp_cmp(u, v) == LTC_MP_EQ); ENSURE(sizeof(mpint2) == len); len = sizeof(mpint3); DO(ssh_decode_sequence_multi(mpint3, &len, LTC_SSHDATA_MPINT, v, LTC_SSHDATA_EOL, NULL)); - ENSURE(mp_cmp_d(v, 0x80) == LTC_MP_EQ); + ENSURE(ltc_mp_cmp_d(v, 0x80) == LTC_MP_EQ); ENSURE(sizeof(mpint3) == len); - mp_clear_multi(v, u, LTC_NULL); + ltc_mp_deinit_multi(v, u, LTC_NULL); /* name-list */ zeromem(strbuf, BUFSIZE); diff --git a/tests/test.c b/tests/test.c index 60e8c363c..6fe789853 100644 --- a/tests/test.c +++ b/tests/test.c @@ -349,7 +349,7 @@ int main(int argc, char **argv) if (ltc_mp.name != NULL) { printf("MP_PROVIDER = %s\n", ltc_mp.name); - printf("MP_DIGIT_BIT = %d\n", MP_DIGIT_BIT); + printf("MP_DIGIT_BIT = %d\n", LTC_MP_DIGIT_BIT); printf("sizeof(ltc_mp_digit) = %d\n", (int)sizeof(ltc_mp_digit)); } else { printf("NO math provider selected, all tests requiring MPI will 'nop'\n"); From 08b69be797868cbb97846f3f67071adc4b77e26e Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 5 Aug 2022 16:54:21 +0200 Subject: [PATCH 3/7] minor changes for amalgamation * de-duplicate `struct oid_to_pbes` * add makefile target * add amalgamation to release * fix `small` demo * add header guards for `tomcrypt_private.h` * update docs * add CI job with amalgamated library Signed-off-by: Steffen Jaeckel --- .github/workflows/main.yml | 21 +++++++++++++++++++++ .gitignore | 1 + doc/crypt.tex | 22 ++++++++++++++++++++++ makefile | 13 +++++++++++++ makefile_include.mk | 7 +++++++ src/headers/tomcrypt_private.h | 10 ++++++++++ src/misc/pbes/pbes1.c | 5 ----- src/misc/pbes/pbes2.c | 5 ----- 8 files changed, 74 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 78b27f3a3..1d5bba05e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -98,3 +98,24 @@ jobs: name: build-${{ github.run_id }}.tar.xz path: build-${{ github.run_id }}.tar.xz retention-days: 1 + + Amalgam: + runs-on: ${{ matrix.os }} + strategy: + matrix: + cc: [ gcc, clang ] + os: [ ubuntu-22.04, ubuntu-24.04 ] + steps: + - uses: actions/checkout@v4 + - name: install dependencies + run: | + sudo apt-get update -qq + sudo apt-get remove -y libtommath1 + curl -s https://packagecloud.io/install/repositories/libtom/packages/script.deb.sh | sudo bash + sudo apt-get install libtommath-git-dev + - name: run tests + env: + CC: "${{ matrix.cc }}" + run: | + make pre_gen + make CFLAGS="-DLTM_DESC -DUSE_LTM" EXTRALIBS="-ltommath" AMALGAM=1 -j$(nproc) check diff --git a/.gitignore b/.gitignore index f9db9dc77..76f6737ee 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ # release files /libtomcrypt-* /crypt-* +pre_gen/ # suppress output of build process gcc_[12].txt diff --git a/doc/crypt.tex b/doc/crypt.tex index ea0b250c4..98332af20 100644 --- a/doc/crypt.tex +++ b/doc/crypt.tex @@ -8896,6 +8896,28 @@ \subsection{Installation Directories} endif() \end{verbatim} + +\mysection{Amalgamated library} + +LibTomCrypt can also be built as an amalgamated library, i.e. as a single C source file + the header files. + +A release of the library contains the amalgamation in the path \texttt{pre\_gen/tomcrypt\_amalgam.c}. + +To create the amalgamation one can run: + +\begin{verbatim} +make pre_gen +\end{verbatim} + +The makefiles also support building the amalgamated library via: + +\begin{verbatim} +make CFLAGS="-DLTM_DESC" EXTRALIBS=-ltommath AMALGAM=1 +\end{verbatim} + +This will build the library and link against LibTomMath (which must be installed on your system). + + \mysection{Header Configuration} The file \textit{tomcrypt\_cfg.h} is what lets you control various high level macros which control the behaviour of the library. Build options are also stored in \textit{tomcrypt\_custom.h} which allow the enabling and disabling of various algorithms. diff --git a/makefile b/makefile index ddddb6cc9..a80cbd21e 100644 --- a/makefile +++ b/makefile @@ -143,3 +143,16 @@ coverage: $(call print-help,coverage,Create code-coverage of the library - but b # cleans everything - coverage output and standard 'clean' cleancov: cleancov-clean clean +ifndef AMALGAM +AMALGAM_FILTER_OUT = src/ciphers/aes/aes_tab.c src/ciphers/aes/aes_enc.c src/ciphers/aes/aes_enc_desc.c +SOURCES = $(filter-out $(AMALGAM_FILTER_OUT),$(OBJECTS:.o=.c)) +pre_gen/tomcrypt_amalgam.c: $(SOURCES) + mkdir -p pre_gen + printf "/*\n * This file has been auto-generated, do not edit!\n */\n\n" > $@ + cat $(SOURCES) >> $@ + +pre_gen: pre_gen/tomcrypt_amalgam.c + +.PHONY: pre_gen +endif + diff --git a/makefile_include.mk b/makefile_include.mk index ba01bf453..ebacc4812 100644 --- a/makefile_include.mk +++ b/makefile_include.mk @@ -210,6 +210,7 @@ library: $(call print-help,library,Builds the library) $(LIBNAME) # List of objects to compile (all goes to libtomcrypt.a) +ifndef AMALGAM OBJECTS=src/ciphers/aes/aes.o src/ciphers/aes/aes_desc.o src/ciphers/aes/aes_enc.o \ src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/ciphers/blowfish.o \ src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \ @@ -401,6 +402,11 @@ src/stream/salsa20/xsalsa20_setup.o src/stream/salsa20/xsalsa20_test.o \ src/stream/sober128/sober128_stream.o src/stream/sober128/sober128_stream_memory.o \ src/stream/sober128/sober128_test.o src/stream/sosemanuk/sosemanuk.o \ src/stream/sosemanuk/sosemanuk_memory.o src/stream/sosemanuk/sosemanuk_test.o +else +OBJECTS=pre_gen/tomcrypt_amalgam.o + +LTC_CFLAGS := $(LTC_CFLAGS) -Wno-shadow -Isrc/ciphers/aes -Isrc/ciphers/safer -Isrc/ciphers/twofish -Isrc/hashes/whirl -Isrc/stream/sober128 +endif # List of test objects to compile (all goes to libtomcrypt_prof.a) TOBJECTS=tests/base16_test.o tests/base32_test.o tests/base64_test.o tests/bcrypt_test.o \ @@ -537,6 +543,7 @@ zipup: $(call print-help,zipup,Prepare the archives for a release) doc/crypt.pdf -@(find libtomcrypt-$(VERSION)/ -type f | xargs grep 'FIXM[E]') && echo '############## BEWARE: the "fixme" marker was found !!! ##############' || true mkdir -p libtomcrypt-$(VERSION)/doc cp doc/crypt.pdf libtomcrypt-$(VERSION)/doc/crypt.pdf + $(MAKE) -C libtomcrypt-$(VERSION)/ pre_gen tar -c libtomcrypt-$(VERSION)/ | xz -6e -c - > crypt-$(VERSION).tar.xz zip -9rq crypt-$(VERSION).zip libtomcrypt-$(VERSION) rm -rf libtomcrypt-$(VERSION) diff --git a/src/headers/tomcrypt_private.h b/src/headers/tomcrypt_private.h index 0452117a8..78f3d08f5 100644 --- a/src/headers/tomcrypt_private.h +++ b/src/headers/tomcrypt_private.h @@ -4,6 +4,9 @@ #include "tomcrypt.h" #include +#ifndef TOMCRYPT_PRIVATE_H_ +#define TOMCRYPT_PRIVATE_H_ + /* * Internal Macros */ @@ -105,6 +108,11 @@ typedef struct unsigned long key_bits; } pbes_arg; +typedef struct { + const pbes_properties *data; + const char *oid; +} oid_to_pbes; + /* * Internal functions */ @@ -692,3 +700,5 @@ int which ## _export(unsigned char *out, unsigned long *outlen, prng_state *prng #define LTC_WIN32_BCRYPT #endif #endif + +#endif /* TOMCRYPT_PRIVATE_H_ */ diff --git a/src/misc/pbes/pbes1.c b/src/misc/pbes/pbes1.c index 24c50961c..86daa47c3 100644 --- a/src/misc/pbes/pbes1.c +++ b/src/misc/pbes/pbes1.c @@ -50,11 +50,6 @@ static const pbes_properties s_pbes1_types[] = { { s_pkcs_12_wrap, "sha1", "3des", 24, 8 }, }; -typedef struct { - const pbes_properties *data; - const char *oid; -} oid_to_pbes; - static const oid_to_pbes s_pbes1_list[] = { { &s_pbes1_types[0], "1.2.840.113549.1.5.1" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.1 pbeWithMD2AndDES-CBC */ { &s_pbes1_types[1], "1.2.840.113549.1.5.4" }, /* http://www.oid-info.com/get/1.2.840.113549.1.5.4 pbeWithMD2AndRC2-CBC */ diff --git a/src/misc/pbes/pbes2.c b/src/misc/pbes/pbes2.c index 5e672fcd8..cfb3426e5 100644 --- a/src/misc/pbes/pbes2.c +++ b/src/misc/pbes/pbes2.c @@ -39,11 +39,6 @@ static const pbes_properties s_pbes2_default_types[] = { { s_pkcs_5_alg2_wrap, "sha1", "aes", 32, 0 }, }; -typedef struct { - const pbes_properties *data; - const char* oid; -} oid_to_pbes; - static const oid_to_pbes s_pbes2_list[] = { { &s_pbes2_default_types[0], "1.3.14.3.2.7" }, /* http://www.oid-info.com/get/1.3.14.3.2.7 desCBC */ { &s_pbes2_default_types[1], "1.2.840.113549.3.2" }, /* http://www.oid-info.com/get/1.2.840.113549.3.2 rc2CBC */ From 647b340ec3c016222aed6a9c18768225f5866c30 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Mon, 17 Feb 2025 14:09:10 +0100 Subject: [PATCH 4/7] Use `LTC_NULL` with `ssh_decode_sequence_multi()` Signed-off-by: Steffen Jaeckel --- src/misc/pem/pem_ssh.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/misc/pem/pem_ssh.c b/src/misc/pem/pem_ssh.c index 4630d7086..64655c337 100644 --- a/src/misc/pem/pem_ssh.c +++ b/src/misc/pem/pem_ssh.c @@ -93,7 +93,7 @@ static int s_ssh_decode_ecdsa(const unsigned char *in, unsigned long *inlen, ltc LTC_SSHDATA_STRING, groupname, &groupnamelen, LTC_SSHDATA_STRING, buf0, &buf0len, LTC_SSHDATA_STRING, buf1, &buf1len, - LTC_SSHDATA_EOL, NULL); + LTC_SSHDATA_EOL, LTC_NULL); if (err == CRYPT_OK) { key = buf1; keylen = buf1len; @@ -137,7 +137,7 @@ static int s_ssh_decode_ed25519(const unsigned char *in, unsigned long *inlen, l if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_STRING, pubkey, &pubkeylen, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup; } @@ -155,7 +155,7 @@ static int s_ssh_decode_ed25519(const unsigned char *in, unsigned long *inlen, l if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_STRING, privkey, &privkeylen, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup; } if ((err = ed25519_import_raw(privkey, privkeylen, PK_PRIVATE, &key->u.ed25519)) != CRYPT_OK) { @@ -195,7 +195,7 @@ static int s_ssh_decode_dsa(const unsigned char *in, unsigned long *inlen, ltc_p LTC_SSHDATA_MPINT, key->u.dsa.q, LTC_SSHDATA_MPINT, key->u.dsa.g, LTC_SSHDATA_MPINT, key->u.dsa.y, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup; } key->u.dsa.qord = ltc_mp_unsigned_bin_size(key->u.dsa.q); @@ -219,7 +219,7 @@ static int s_ssh_decode_dsa(const unsigned char *in, unsigned long *inlen, ltc_p if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_MPINT, key->u.dsa.x, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup; } @@ -262,7 +262,7 @@ static int s_ssh_decode_rsa(const unsigned char *in, unsigned long *inlen, ltc_p if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_MPINT, key->u.rsa.N, LTC_SSHDATA_MPINT, key->u.rsa.e, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup; } @@ -290,7 +290,7 @@ static int s_ssh_decode_rsa(const unsigned char *in, unsigned long *inlen, ltc_p LTC_SSHDATA_MPINT, key->u.rsa.qP, LTC_SSHDATA_MPINT, key->u.rsa.p, LTC_SSHDATA_MPINT, key->u.rsa.q, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup_tmps; } @@ -377,7 +377,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_UINT32, &check1, LTC_SSHDATA_UINT32, &check2, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { return err; } if (check1 != check2) { @@ -390,7 +390,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k } if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_STRING, pka, &pkalen, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { return err; } @@ -427,7 +427,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k } if ((err = ssh_decode_sequence_multi(p, &cur_len, LTC_SSHDATA_STRING, c, &commentlen, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { return err; } if (commentlen == 0) { @@ -651,7 +651,7 @@ static int s_decode_header(unsigned char *in, unsigned long *inlen, struct kdf_o LTC_SSHDATA_STRING, kdfoptions, &kdfoptionslen, LTC_SSHDATA_UINT32, &num_keys, LTC_SSHDATA_STRING, pubkey1, &pubkey1len, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { return err; } if (num_keys != 1) { @@ -680,7 +680,7 @@ static int s_decode_header(unsigned char *in, unsigned long *inlen, struct kdf_o if ((err = ssh_decode_sequence_multi(kdfoptions, &len, LTC_SSHDATA_STRING, opts->salt, &opts->saltlen, LTC_SSHDATA_UINT32, &opts->num_rounds, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { return err; } if (len != kdfoptionslen) { @@ -754,7 +754,7 @@ static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_c if ((err = ssh_decode_sequence_multi(p, &w, LTC_SSHDATA_STRING, privkey, &privkey_len, - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { + LTC_SSHDATA_EOL, LTC_NULL)) != CRYPT_OK) { goto cleanup; } From 00f662da8029f2da70b09e7097e4c1d3303fefe7 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Mon, 17 Feb 2025 14:45:02 +0100 Subject: [PATCH 5/7] Fix compiler warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` pre_gen/tomcrypt_amalgam.c: In function ‘pkcs_1_oaep_encode’: pre_gen/tomcrypt_amalgam.c:64485:18: warning: ‘DB’ may be used uninitialized [-Wmaybe-uninitialized] 64485 | if ((err = hash_memory(lparam_hash_used, DB, 0, DB, &x)) != CRYPT_OK) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pre_gen/tomcrypt_amalgam.c:22613:5: note: by argument 2 of type ‘const unsigned char *’ to ‘hash_memory’ declared here 22613 | int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) | ^~~~~~~~~~~ ``` Use `out` instead of `DB`, since `out` is `LTC_ARGCHK`'ed. Signed-off-by: Steffen Jaeckel --- src/pk/pkcs1/pkcs_1_oaep_encode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pk/pkcs1/pkcs_1_oaep_encode.c b/src/pk/pkcs1/pkcs_1_oaep_encode.c index 294583d9c..45aa5ce5c 100644 --- a/src/pk/pkcs1/pkcs_1_oaep_encode.c +++ b/src/pk/pkcs1/pkcs_1_oaep_encode.c @@ -89,8 +89,8 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, goto LBL_ERR; } } else { - /* can't pass hash_memory a NULL so use DB with zero length */ - if ((err = hash_memory(lparam_hash_used, DB, 0, DB, &x)) != CRYPT_OK) { + /* can't pass hash_memory a NULL so use `out` with zero length */ + if ((err = hash_memory(lparam_hash_used, out, 0, DB, &x)) != CRYPT_OK) { goto LBL_ERR; } } From 22e71d755b6f8c4dc895529138d58d708e73a21f Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Mon, 17 Feb 2025 14:48:19 +0100 Subject: [PATCH 6/7] Fix return value. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of wrongfully always returning OK, return the real error code. This was uncovered by the amalgamation via the warning: ``` pre_gen/tomcrypt_amalgam.c: In function ‘der_decode_custom_type_ex’: pre_gen/tomcrypt_amalgam.c:49820:10: warning: ‘*(unsigned int *)((char *)&ident + offsetof(ltc_asn1_list, type))’ may be used uninitialized [-Wmaybe-uninitialized] 49820 | if ((ident.type != root->type) || | ^ pre_gen/tomcrypt_amalgam.c:49780:18: note: ‘*(unsigned int *)((char *)&ident + offsetof(ltc_asn1_list, type))’ was declared here 49780 | ltc_asn1_list ident; | ^~~~~ ``` Signed-off-by: Steffen Jaeckel --- src/pk/asn1/der/general/der_decode_asn1_identifier.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pk/asn1/der/general/der_decode_asn1_identifier.c b/src/pk/asn1/der/general/der_decode_asn1_identifier.c index ad4dc7b42..865e097dc 100644 --- a/src/pk/asn1/der/general/der_decode_asn1_identifier.c +++ b/src/pk/asn1/der/general/der_decode_asn1_identifier.c @@ -117,7 +117,7 @@ int der_decode_asn1_identifier(const unsigned char *in, unsigned long *inlen, lt } } - return CRYPT_OK; + return err; } #endif From 92aef0ade1b157571b5186b53ddab6e18e429291 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 18 Feb 2025 16:51:40 +0100 Subject: [PATCH 7/7] Include the tables into the amalgamation Signed-off-by: Steffen Jaeckel --- makefile | 14 ++++++++++---- src/ciphers/aes/aes.c | 2 ++ src/hashes/whirl/whirl.c | 2 ++ src/stream/sober128/sober128_stream.c | 2 ++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/makefile b/makefile index a80cbd21e..72437220c 100644 --- a/makefile +++ b/makefile @@ -144,15 +144,21 @@ coverage: $(call print-help,coverage,Create code-coverage of the library - but b # cleans everything - coverage output and standard 'clean' cleancov: cleancov-clean clean ifndef AMALGAM -AMALGAM_FILTER_OUT = src/ciphers/aes/aes_tab.c src/ciphers/aes/aes_enc.c src/ciphers/aes/aes_enc_desc.c +AMALGAM_FILTER_OUT = src/ciphers/aes/aes_enc.c src/ciphers/aes/aes_enc_desc.c +TAB_SOURCES = src/ciphers/aes/aes_tab.c src/ciphers/safer/safer_tab.c src/hashes/whirl/whirltab.c src/stream/sober128/sober128tab.c SOURCES = $(filter-out $(AMALGAM_FILTER_OUT),$(OBJECTS:.o=.c)) -pre_gen/tomcrypt_amalgam.c: $(SOURCES) +pre_gen/tomcrypt_amalgam.c: $(TAB_SOURCES) $(SOURCES) mkdir -p pre_gen printf "/*\n * This file has been auto-generated, do not edit!\n */\n\n" > $@ - cat $(SOURCES) >> $@ + printf "#define LTC_AES_TAB_C\n" >> $@ + printf "#define LTC_SAFER_TAB_C\n" >> $@ + printf "#define LTC_SOBER128TAB_C\n" >> $@ + printf "#define LTC_WHIRLTAB_C\n\n" >> $@ + printf "#include \"tomcrypt_private.h\"\n\n" >> $@ + cat $^ >> $@ pre_gen: pre_gen/tomcrypt_amalgam.c -.PHONY: pre_gen +.PHONY: pre_gen/tomcrypt_amalgam.c endif diff --git a/src/ciphers/aes/aes.c b/src/ciphers/aes/aes.c index 889d79375..1a5e269a3 100644 --- a/src/ciphers/aes/aes.c +++ b/src/ciphers/aes/aes.c @@ -62,8 +62,10 @@ const struct ltc_cipher_descriptor rijndael_enc_desc = #endif +#ifndef LTC_AES_TAB_C #define LTC_AES_TAB_C #include "aes_tab.c" +#endif static ulong32 setup_mix(ulong32 temp) { diff --git a/src/hashes/whirl/whirl.c b/src/hashes/whirl/whirl.c index 16bcc1787..c1e46160a 100644 --- a/src/hashes/whirl/whirl.c +++ b/src/hashes/whirl/whirl.c @@ -29,8 +29,10 @@ const struct ltc_hash_descriptor whirlpool_desc = }; /* the sboxes */ +#ifndef LTC_WHIRLTAB_C #define LTC_WHIRLTAB_C #include "whirltab.c" +#endif /* get a_{i,j} */ #define GB(a,i,j) ((a[(i) & 7] >> (8 * (j))) & 255) diff --git a/src/stream/sober128/sober128_stream.c b/src/stream/sober128/sober128_stream.c index fcabd0616..ab1a7aab6 100644 --- a/src/stream/sober128/sober128_stream.c +++ b/src/stream/sober128/sober128_stream.c @@ -10,8 +10,10 @@ #ifdef LTC_SOBER128 +#ifndef LTC_SOBER128TAB_C #define LTC_SOBER128TAB_C #include "sober128tab.c" +#endif /* don't change these... */ #define N 17