diff options
author | Miloslav Trmač <mitr@redhat.com> | 2010-07-24 11:54:02 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2010-07-24 11:54:02 +0200 |
commit | 951eda087f418c2a5ced189fa9d64c8616634dd0 (patch) | |
tree | 72050e803bfce0e4c8454011078f10435bb66f04 /libtomcrypt | |
parent | c13723d4a2a9627f4cd85d47954ab1fd3a115dbd (diff) | |
download | cryptodev-linux-951eda087f418c2a5ced189fa9d64c8616634dd0.tar.gz cryptodev-linux-951eda087f418c2a5ced189fa9d64c8616634dd0.tar.xz cryptodev-linux-951eda087f418c2a5ced189fa9d64c8616634dd0.zip |
Use of algo_properties to avoid linear search on a table for each property.
Diffstat (limited to 'libtomcrypt')
-rw-r--r-- | libtomcrypt/hashes/crypt_hash_is_valid.c | 4 | ||||
-rw-r--r-- | libtomcrypt/hashes/hash_get_oid.c | 4 | ||||
-rw-r--r-- | libtomcrypt/hashes/hash_memory.c | 14 | ||||
-rw-r--r-- | libtomcrypt/hashes/hash_memory_multi.c | 14 | ||||
-rw-r--r-- | libtomcrypt/headers/tomcrypt_hash.h | 10 | ||||
-rw-r--r-- | libtomcrypt/headers/tomcrypt_pk.h | 26 | ||||
-rw-r--r-- | libtomcrypt/headers/tomcrypt_pkcs.h | 12 | ||||
-rw-r--r-- | libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c | 10 | ||||
-rw-r--r-- | libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c | 16 | ||||
-rw-r--r-- | libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c | 16 | ||||
-rw-r--r-- | libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c | 12 | ||||
-rw-r--r-- | libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c | 12 | ||||
-rw-r--r-- | libtomcrypt/pk/rsa/rsa_decrypt_key.c | 9 | ||||
-rw-r--r-- | libtomcrypt/pk/rsa/rsa_encrypt_key.c | 11 | ||||
-rw-r--r-- | libtomcrypt/pk/rsa/rsa_sign_hash.c | 11 | ||||
-rw-r--r-- | libtomcrypt/pk/rsa/rsa_verify_hash.c | 11 |
16 files changed, 99 insertions, 93 deletions
diff --git a/libtomcrypt/hashes/crypt_hash_is_valid.c b/libtomcrypt/hashes/crypt_hash_is_valid.c index 32e8699..d01d418 100644 --- a/libtomcrypt/hashes/crypt_hash_is_valid.c +++ b/libtomcrypt/hashes/crypt_hash_is_valid.c @@ -17,10 +17,10 @@ /* Test if a hash index is valid - @param idx The index of the hash to search for + @param idx The hash to search for @return CRYPT_OK if valid */ -int hash_is_valid(int idx) +int hash_is_valid(const struct algo_properties_st *hash) { return CRYPT_OK; } diff --git a/libtomcrypt/hashes/hash_get_oid.c b/libtomcrypt/hashes/hash_get_oid.c index 32e4390..c6469ba 100644 --- a/libtomcrypt/hashes/hash_get_oid.c +++ b/libtomcrypt/hashes/hash_get_oid.c @@ -46,9 +46,9 @@ static const oid_st sha512_oid = { .OID = { 2, 16, 840, 1, 101, 3, 4, 2, 3, }, }; -int hash_get_oid(int hash, oid_st *st) +int hash_get_oid(const struct algo_properties_st *hash, oid_st *st) { - switch (hash) { + switch (hash->algo) { case NCR_ALG_SHA1: memcpy(st, &sha1_oid, sizeof(*st)); break; diff --git a/libtomcrypt/hashes/hash_memory.c b/libtomcrypt/hashes/hash_memory.c index 274c208..5ba3bc6 100644 --- a/libtomcrypt/hashes/hash_memory.c +++ b/libtomcrypt/hashes/hash_memory.c @@ -19,18 +19,17 @@ /** Hash a block of memory and store the digest. - @param hash The index of the hash you wish to use + @param hash The hash you wish to use @param in The data you wish to hash @param inlen The length of the data to hash (octets) @param out [out] Where to store the digest @param outlen [in/out] Max size and resulting size of the digest @return CRYPT_OK if successful */ -int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) +int hash_memory(const struct algo_properties_st *hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen) { int err; struct hash_data hdata; - int digest_size; LTC_ARGCHK(in != NULL); LTC_ARGCHK(out != NULL); @@ -40,13 +39,12 @@ int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned return err; } - digest_size = _ncr_algo_digest_size(hash); - if (*outlen < digest_size) { - *outlen = digest_size; + if (*outlen < hash->digest_size) { + *outlen = hash->digest_size; return CRYPT_BUFFER_OVERFLOW; } - err = cryptodev_hash_init( &hdata, _ncr_algo_to_str(hash), 0, NULL, 0); + err = cryptodev_hash_init( &hdata, hash->kstr, 0, NULL, 0); if (err < 0) { err = CRYPT_INVALID_HASH; goto LBL_ERR; @@ -59,7 +57,7 @@ int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned err = cryptodev_hash_final(&hdata, out); - *outlen = digest_size; + *outlen = hash->digest_size; LBL_ERR: cryptodev_hash_deinit(&hdata); diff --git a/libtomcrypt/hashes/hash_memory_multi.c b/libtomcrypt/hashes/hash_memory_multi.c index 6a85f65..d772492 100644 --- a/libtomcrypt/hashes/hash_memory_multi.c +++ b/libtomcrypt/hashes/hash_memory_multi.c @@ -20,7 +20,7 @@ /** Hash multiple (non-adjacent) blocks of memory at once. - @param hash The index of the hash you wish to use + @param hash The hash you wish to use @param out [out] Where to store the digest @param outlen [in/out] Max size and resulting size of the digest @param in The data you wish to hash @@ -28,11 +28,10 @@ @param ... tuples of (data,len) pairs to hash, terminated with a (NULL,x) (x=don't care) @return CRYPT_OK if successful */ -int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, +int hash_memory_multi(const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen, const unsigned char *in, unsigned long inlen, ...) { struct hash_data hdata; - int digest_size; int err; va_list args; const unsigned char *curptr; @@ -46,13 +45,12 @@ int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, return err; } - digest_size = _ncr_algo_digest_size(hash); - if (*outlen < digest_size) { - *outlen = digest_size; + if (*outlen < hash->digest_size) { + *outlen = hash->digest_size; return CRYPT_BUFFER_OVERFLOW; } - err = cryptodev_hash_init( &hdata, _ncr_algo_to_str(hash), 0, NULL, 0); + err = cryptodev_hash_init( &hdata, hash->kstr, 0, NULL, 0); if (err < 0) { err = CRYPT_INVALID_HASH; goto LBL_ERR; @@ -77,7 +75,7 @@ int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, err = cryptodev_hash_final(&hdata, out); - *outlen = digest_size; + *outlen = hash->digest_size; LBL_ERR: cryptodev_hash_deinit(&hdata); va_end(args); diff --git a/libtomcrypt/headers/tomcrypt_hash.h b/libtomcrypt/headers/tomcrypt_hash.h index 417e481..e4e84e4 100644 --- a/libtomcrypt/headers/tomcrypt_hash.h +++ b/libtomcrypt/headers/tomcrypt_hash.h @@ -1,12 +1,14 @@ /* ---- HASH FUNCTIONS ---- */ -int hash_is_valid(int idx); +struct algo_properties_st; -int hash_memory(int hash, +int hash_is_valid(const struct algo_properties_st *hash); + +int hash_memory(const struct algo_properties_st *hash, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen); -int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen, +int hash_memory_multi(const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen, const unsigned char *in, unsigned long inlen, ...); -int hash_get_oid(int hash, oid_st* st); +int hash_get_oid(const struct algo_properties_st *hash, oid_st* st); diff --git a/libtomcrypt/headers/tomcrypt_pk.h b/libtomcrypt/headers/tomcrypt_pk.h index fa6030e..145165e 100644 --- a/libtomcrypt/headers/tomcrypt_pk.h +++ b/libtomcrypt/headers/tomcrypt_pk.h @@ -1,5 +1,7 @@ /* ---- NUMBER THEORY ---- */ +struct algo_properties_st; + enum { PK_PUBLIC=0, PK_PRIVATE=1 @@ -57,40 +59,40 @@ int rsa_exptmod(const unsigned char *in, unsigned long inlen, void rsa_free(rsa_key *key); /* These use LTC_PKCS #1 v2.0 padding */ -#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _key) \ - rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_LTC_PKCS_1_OAEP, _key) +#define rsa_encrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash, _key) \ + rsa_encrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash, LTC_LTC_PKCS_1_OAEP, _key) -#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, _stat, _key) \ - rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash_idx, LTC_LTC_PKCS_1_OAEP, _stat, _key) +#define rsa_decrypt_key(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash, _stat, _key) \ + rsa_decrypt_key_ex(_in, _inlen, _out, _outlen, _lparam, _lparamlen, _hash, LTC_LTC_PKCS_1_OAEP, _stat, _key) -#define rsa_sign_hash(_in, _inlen, _out, _outlen, _hash_idx, _saltlen, _key) \ - rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_LTC_PKCS_1_PSS, _hash_idx, _saltlen, _key) +#define rsa_sign_hash(_in, _inlen, _out, _outlen, _hash, _saltlen, _key) \ + rsa_sign_hash_ex(_in, _inlen, _out, _outlen, LTC_LTC_PKCS_1_PSS, _hash, _saltlen, _key) -#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_idx, _saltlen, _stat, _key) \ - rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_LTC_PKCS_1_PSS, _hash_idx, _saltlen, _stat, _key) +#define rsa_verify_hash(_sig, _siglen, _hash, _hashlen, _hash_algo, _saltlen, _stat, _key) \ + rsa_verify_hash_ex(_sig, _siglen, _hash, _hashlen, LTC_LTC_PKCS_1_PSS, _hash_algo, _saltlen, _stat, _key) /* These can be switched between LTC_PKCS #1 v2.x and LTC_PKCS #1 v1.5 paddings */ int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *lparam, unsigned long lparamlen, - int hash_idx, int padding, rsa_key *key); + const struct algo_properties_st *hash, int padding, rsa_key *key); int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *lparam, unsigned long lparamlen, - int hash_idx, int padding, + const struct algo_properties_st *hash, int padding, int *stat, rsa_key *key); int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, int padding, - int hash_idx, unsigned long saltlen, + const struct algo_properties_st *hash, unsigned long saltlen, rsa_key *key); int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int padding, - int hash_idx, unsigned long saltlen, + const struct algo_properties_st *hash_algo, unsigned long saltlen, int *stat, rsa_key *key); /* LTC_PKCS #1 import/export */ diff --git a/libtomcrypt/headers/tomcrypt_pkcs.h b/libtomcrypt/headers/tomcrypt_pkcs.h index 8e43942..be0d7f6 100644 --- a/libtomcrypt/headers/tomcrypt_pkcs.h +++ b/libtomcrypt/headers/tomcrypt_pkcs.h @@ -3,6 +3,8 @@ /* ===> LTC_PKCS #1 -- RSA Cryptography <=== */ #ifdef LTC_PKCS_1 +struct algo_properties_st; + enum ltc_pkcs_1_v1_5_blocks { LTC_LTC_PKCS_1_EMSA = 1, /* Block type 1 (LTC_PKCS #1 v1.5 signature padding) */ @@ -16,7 +18,7 @@ enum ltc_pkcs_1_paddings LTC_LTC_PKCS_1_PSS = 3 /* LTC_PKCS #1 v2.1 signature padding */ }; -int pkcs_1_mgf1( int hash_idx, +int pkcs_1_mgf1(const struct algo_properties_st *hash, const unsigned char *seed, unsigned long seedlen, unsigned char *mask, unsigned long masklen); @@ -42,23 +44,23 @@ int pkcs_1_v1_5_decode(const unsigned char *msg, /* *** v2.1 padding */ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, const unsigned char *lparam, unsigned long lparamlen, - unsigned long modulus_bitlen, int hash_idx, + unsigned long modulus_bitlen, const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen); int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, const unsigned char *lparam, unsigned long lparamlen, - unsigned long modulus_bitlen, int hash_idx, + unsigned long modulus_bitlen, const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen, int *res); int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, - unsigned long saltlen, int hash_idx, + unsigned long saltlen, const struct algo_properties_st *hash, unsigned long modulus_bitlen, unsigned char *out, unsigned long *outlen); int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, const unsigned char *sig, unsigned long siglen, - unsigned long saltlen, int hash_idx, + unsigned long saltlen, const struct algo_properties_st *hash, unsigned long modulus_bitlen, int *res); #endif /* LTC_PKCS_1 */ diff --git a/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c b/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c index 01fe231..b09dd11 100644 --- a/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c +++ b/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c @@ -22,12 +22,12 @@ Perform LTC_PKCS #1 MGF1 (internal) @param seed The seed for MGF1 @param seedlen The length of the seed - @param hash_idx The index of the hash desired + @param hash The desired hash @param mask [out] The destination @param masklen The length of the mask desired @return CRYPT_OK if successful */ -int pkcs_1_mgf1(int hash_idx, +int pkcs_1_mgf1(const struct algo_properties_st *hash, const unsigned char *seed, unsigned long seedlen, unsigned char *mask, unsigned long masklen) { @@ -40,12 +40,12 @@ int pkcs_1_mgf1(int hash_idx, LTC_ARGCHK(mask != NULL); /* ensure valid hash */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } /* get hash output size */ - hLen = _ncr_algo_digest_size(hash_idx); + hLen = hash->digest_size; /* allocate memory */ buf = XMALLOC(hLen); @@ -61,7 +61,7 @@ int pkcs_1_mgf1(int hash_idx, STORE32H(counter, buf); ++counter; - err = hash_memory_multi(hash_idx, buf, &hLen, seed, seedlen, buf, (unsigned long) 4, NULL, 0); + err = hash_memory_multi(hash, buf, &hLen, seed, seedlen, buf, (unsigned long) 4, NULL, 0); if (err != CRYPT_OK) { goto LBL_ERR; } diff --git a/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c b/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c index 4114c56..60f76a0 100644 --- a/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c +++ b/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c @@ -25,7 +25,7 @@ @param lparam The session or system data (can be NULL) @param lparamlen The length of the lparam @param modulus_bitlen The bit length of the RSA modulus - @param hash_idx The index of the hash desired + @param hash The desired hash @param out [out] Destination of decoding @param outlen [in/out] The max size and resulting size of the decoding @param res [out] Result of decoding, 1==valid, 0==invalid @@ -33,7 +33,7 @@ */ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, const unsigned char *lparam, unsigned long lparamlen, - unsigned long modulus_bitlen, int hash_idx, + unsigned long modulus_bitlen, const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen, int *res) { @@ -50,11 +50,11 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, *res = 0; /* test valid hash */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } - hLen = _ncr_algo_digest_size(hash_idx); + hLen = hash->digest_size; modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); /* test hash/message size */ @@ -103,7 +103,7 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, x += modulus_len - hLen - 1; /* compute MGF1 of maskedDB (hLen) */ - if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) { + if ((err = pkcs_1_mgf1(hash, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) { goto LBL_ERR; } @@ -113,7 +113,7 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, } /* compute MGF1 of seed (k - hlen - 1) */ - if ((err = pkcs_1_mgf1(hash_idx, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { + if ((err = pkcs_1_mgf1(hash, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { goto LBL_ERR; } @@ -127,12 +127,12 @@ int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, /* compute lhash and store it in seed [reuse temps!] */ x = modulus_len; if (lparam != NULL) { - if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) { + if ((err = hash_memory(hash, lparam, lparamlen, seed, &x)) != CRYPT_OK) { goto LBL_ERR; } } else { /* can't pass hash_memory a NULL so use DB with zero length */ - if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) { + if ((err = hash_memory(hash, DB, 0, seed, &x)) != CRYPT_OK) { goto LBL_ERR; } } diff --git a/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c b/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c index ccee5cf..c56e3b1 100644 --- a/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c +++ b/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c @@ -25,14 +25,14 @@ @param lparam A session or system parameter (can be NULL) @param lparamlen The length of the lparam data @param modulus_bitlen The bit length of the RSA modulus - @param hash_idx The index of the hash desired + @param hash The desired hash @param out [out] The destination for the encoded data @param outlen [in/out] The max size and resulting size of the encoded data @return CRYPT_OK if successful */ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, const unsigned char *lparam, unsigned long lparamlen, - unsigned long modulus_bitlen, int hash_idx, + unsigned long modulus_bitlen, const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen) { unsigned char *DB, *seed, *mask; @@ -44,11 +44,11 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, LTC_ARGCHK(outlen != NULL); /* test valid hash */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } - hLen = _ncr_algo_digest_size(hash_idx); + hLen = hash->digest_size; modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); /* test message size */ @@ -77,12 +77,12 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, /* DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */ x = modulus_len; if (lparam != NULL) { - if ((err = hash_memory(hash_idx, lparam, lparamlen, DB, &x)) != CRYPT_OK) { + if ((err = hash_memory(hash, lparam, lparamlen, DB, &x)) != CRYPT_OK) { goto LBL_ERR; } } else { /* can't pass hash_memory a NULL so use DB with zero length */ - if ((err = hash_memory(hash_idx, DB, 0, DB, &x)) != CRYPT_OK) { + if ((err = hash_memory(hash, DB, 0, DB, &x)) != CRYPT_OK) { goto LBL_ERR; } } @@ -104,7 +104,7 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, get_random_bytes(seed, hLen); /* compute MGF1 of seed (k - hlen - 1) */ - if ((err = pkcs_1_mgf1(hash_idx, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { + if ((err = pkcs_1_mgf1(hash, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { goto LBL_ERR; } @@ -114,7 +114,7 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, } /* compute MGF1 of maskedDB (hLen) */ - if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) { + if ((err = pkcs_1_mgf1(hash, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) { goto LBL_ERR; } diff --git a/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c b/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c index 5a26654..293d84f 100644 --- a/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c +++ b/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c @@ -25,14 +25,14 @@ @param sig The signature data (encoded data) @param siglen The length of the signature data (octets) @param saltlen The length of the salt used (octets) - @param hash_idx The index of the hash desired + @param hash_algo The desired hash @param modulus_bitlen The bit length of the RSA modulus @param res [out] The result of the comparison, 1==valid, 0==invalid @return CRYPT_OK if successful (even if the comparison failed) */ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, const unsigned char *sig, unsigned long siglen, - unsigned long saltlen, int hash_idx, + unsigned long saltlen, const struct algo_properties_st *hash_algo, unsigned long modulus_bitlen, int *res) { unsigned char *DB, *mask, *salt, *hash; @@ -46,11 +46,11 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, *res = 0; /* ensure hash is valid */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash_algo)) != CRYPT_OK) { return err; } - hLen = _ncr_algo_digest_size(hash_idx); + hLen = hash_algo->digest_size; modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); /* check sizes */ @@ -102,7 +102,7 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, } /* generate mask of length modulus_len - hLen - 1 from hash */ - if ((err = pkcs_1_mgf1(hash_idx, hash, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { + if ((err = pkcs_1_mgf1(hash_algo, hash, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { goto LBL_ERR; } @@ -131,7 +131,7 @@ int pkcs_1_pss_decode(const unsigned char *msghash, unsigned long msghashlen, } /* M = (eight) 0x00 || msghash || salt, mask = H(M) */ - err = hash_memory_multi(hash_idx, mask, &hLen, mask, 8, msghash, (unsigned long)msghashlen, DB+x, (unsigned long)saltlen, NULL, 0); + err = hash_memory_multi(hash_algo, mask, &hLen, mask, 8, msghash, (unsigned long)msghashlen, DB+x, (unsigned long)saltlen, NULL, 0); if (err != CRYPT_OK) { goto LBL_ERR; } diff --git a/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c b/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c index 382820d..d747b49 100644 --- a/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c +++ b/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c @@ -23,14 +23,14 @@ @param msghash The hash to encode @param msghashlen The length of the hash (octets) @param saltlen The length of the salt desired (octets) - @param hash_idx The index of the hash desired + @param hash_algo The desired hash @param modulus_bitlen The bit length of the RSA modulus @param out [out] The destination of the encoding @param outlen [in/out] The max size and resulting size of the encoded data @return CRYPT_OK if successful */ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, - unsigned long saltlen, int hash_idx, + unsigned long saltlen, const struct algo_properties_st *hash_algo, unsigned long modulus_bitlen, unsigned char *out, unsigned long *outlen) { @@ -43,11 +43,11 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, LTC_ARGCHK(outlen != NULL); /* ensure hash and PRNG are valid */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash_algo)) != CRYPT_OK) { return err; } - hLen = _ncr_algo_digest_size(hash_idx); + hLen = hash_algo->digest_size; modulus_len = (modulus_bitlen>>3) + (modulus_bitlen & 7 ? 1 : 0); /* check sizes */ @@ -83,7 +83,7 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, } /* M = (eight) 0x00 || msghash || salt, hash = H(M) */ - err = hash_memory_multi(hash_idx, hash, &hLen, DB, 8, msghash, (unsigned long)msghashlen, salt, (unsigned long)saltlen, NULL, 0); + err = hash_memory_multi(hash_algo, hash, &hLen, DB, 8, msghash, (unsigned long)msghashlen, salt, (unsigned long)saltlen, NULL, 0); if (err != CRYPT_OK) { goto LBL_ERR; } @@ -97,7 +97,7 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen, x += saltlen; /* generate mask of length modulus_len - hLen - 1 from hash */ - if ((err = pkcs_1_mgf1(hash_idx, hash, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { + if ((err = pkcs_1_mgf1(hash_algo, hash, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) { goto LBL_ERR; } diff --git a/libtomcrypt/pk/rsa/rsa_decrypt_key.c b/libtomcrypt/pk/rsa/rsa_decrypt_key.c index 52885e8..989c935 100644 --- a/libtomcrypt/pk/rsa/rsa_decrypt_key.c +++ b/libtomcrypt/pk/rsa/rsa_decrypt_key.c @@ -9,6 +9,7 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" +#include "ncr_int.h" /** @file rsa_decrypt_key.c @@ -25,7 +26,7 @@ @param outlen [in/out] The max size and resulting size of the plaintext (octets) @param lparam The system "lparam" value @param lparamlen The length of the lparam value (octets) - @param hash_idx The index of the hash desired + @param hash The desired hash @param padding Type of padding (LTC_LTC_PKCS_1_OAEP or LTC_LTC_PKCS_1_V1_5) @param stat [out] Result of the decryption, 1==valid, 0==invalid @param key The corresponding private RSA key @@ -34,7 +35,7 @@ int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *lparam, unsigned long lparamlen, - int hash_idx, int padding, + const struct algo_properties_st *hash, int padding, int *stat, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; @@ -58,7 +59,7 @@ int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen, if (padding == LTC_LTC_PKCS_1_OAEP) { /* valid hash ? */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } } @@ -87,7 +88,7 @@ int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen, if (padding == LTC_LTC_PKCS_1_OAEP) { /* now OAEP decode the packet */ - err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx, + err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash, out, outlen, stat); } else { /* now LTC_PKCS #1 v1.5 depad the packet */ diff --git a/libtomcrypt/pk/rsa/rsa_encrypt_key.c b/libtomcrypt/pk/rsa/rsa_encrypt_key.c index d59699c..4ce51a4 100644 --- a/libtomcrypt/pk/rsa/rsa_encrypt_key.c +++ b/libtomcrypt/pk/rsa/rsa_encrypt_key.c @@ -9,6 +9,7 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" +#include "ncr_int.h" /** @file rsa_encrypt_key.c @@ -25,7 +26,7 @@ @param outlen [in/out] The max size and resulting size of the ciphertext @param lparam The system "lparam" for the encryption @param lparamlen The length of lparam (octets) - @param hash_idx The index of the desired hash + @param hash The desired hash @param padding Type of padding (LTC_LTC_PKCS_1_OAEP or LTC_LTC_PKCS_1_V1_5) @param key The RSA key to encrypt to @return CRYPT_OK if successful @@ -33,7 +34,7 @@ int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, const unsigned char *lparam, unsigned long lparamlen, - int hash_idx, int padding, rsa_key *key) + const struct algo_properties_st *hash, int padding, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; int err; @@ -51,7 +52,7 @@ int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, if (padding == LTC_LTC_PKCS_1_OAEP) { /* valid hash? */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } } @@ -70,8 +71,8 @@ int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, /* OAEP pad the key */ x = *outlen; if ((err = pkcs_1_oaep_encode(in, inlen, lparam, - lparamlen, modulus_bitlen, hash_idx, - out, &x)) != CRYPT_OK) { + lparamlen, modulus_bitlen, hash, + out, &x)) != CRYPT_OK) { return err; } } else { diff --git a/libtomcrypt/pk/rsa/rsa_sign_hash.c b/libtomcrypt/pk/rsa/rsa_sign_hash.c index 1298d46..5a32d33 100644 --- a/libtomcrypt/pk/rsa/rsa_sign_hash.c +++ b/libtomcrypt/pk/rsa/rsa_sign_hash.c @@ -9,6 +9,7 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" +#include "ncr_int.h" /** @file rsa_sign_hash.c @@ -24,7 +25,7 @@ @param out [out] The signature @param outlen [in/out] The max size and resulting size of the signature @param padding Type of padding (LTC_LTC_PKCS_1_PSS or LTC_LTC_PKCS_1_V1_5) - @param hash_idx The index of the hash desired + @param hash The desired hash @param saltlen The length of the salt desired (octets) @param key The private RSA key to use @return CRYPT_OK if successful @@ -32,7 +33,7 @@ int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen, int padding, - int hash_idx, unsigned long saltlen, + const struct algo_properties_st *hash, unsigned long saltlen, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x, y; @@ -49,7 +50,7 @@ int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, } if (padding == LTC_LTC_PKCS_1_PSS) { - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash)) != CRYPT_OK) { return err; } } @@ -68,7 +69,7 @@ int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, /* PSS pad the key */ x = *outlen; if ((err = pkcs_1_pss_encode(in, inlen, saltlen, - hash_idx, modulus_bitlen, out, &x)) != CRYPT_OK) { + hash, modulus_bitlen, out, &x)) != CRYPT_OK) { return err; } } else { @@ -78,7 +79,7 @@ int rsa_sign_hash_ex(const unsigned char *in, unsigned long inlen, oid_st st; /* not all hashes have OIDs... so sad */ - if (hash_get_oid(hash_idx, &st) != CRYPT_OK) { + if (hash_get_oid(hash, &st) != CRYPT_OK) { return CRYPT_INVALID_ARG; } diff --git a/libtomcrypt/pk/rsa/rsa_verify_hash.c b/libtomcrypt/pk/rsa/rsa_verify_hash.c index 773ea7d..20f852e 100644 --- a/libtomcrypt/pk/rsa/rsa_verify_hash.c +++ b/libtomcrypt/pk/rsa/rsa_verify_hash.c @@ -9,6 +9,7 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ #include "tomcrypt.h" +#include "ncr_int.h" /** @file rsa_verify_hash.c @@ -24,7 +25,7 @@ @param hash The hash of the message that was signed @param hashlen The length of the hash of the message that was signed (octets) @param padding Type of padding (LTC_LTC_PKCS_1_PSS or LTC_LTC_PKCS_1_V1_5) - @param hash_idx The index of the desired hash + @param hash_algo The desired hash @param saltlen The length of the salt used during signature @param stat [out] The result of the signature comparison, 1==valid, 0==invalid @param key The public RSA key corresponding to the key that performed the signature @@ -33,7 +34,7 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, const unsigned char *hash, unsigned long hashlen, int padding, - int hash_idx, unsigned long saltlen, + const struct algo_properties_st *hash_algo, unsigned long saltlen, int *stat, rsa_key *key) { unsigned long modulus_bitlen, modulus_bytelen, x; @@ -57,7 +58,7 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, if (padding == LTC_LTC_PKCS_1_PSS) { /* valid hash ? */ - if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { + if ((err = hash_is_valid(hash_algo)) != CRYPT_OK) { return err; } } @@ -92,7 +93,7 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, if (padding == LTC_LTC_PKCS_1_PSS) { /* PSS decode and verify it */ - err = pkcs_1_pss_decode(hash, hashlen, tmpbuf, x, saltlen, hash_idx, modulus_bitlen, stat); + err = pkcs_1_pss_decode(hash, hashlen, tmpbuf, x, saltlen, hash_algo, modulus_bitlen, stat); } else { /* LTC_PKCS #1 v1.5 decode it */ unsigned char *out; @@ -102,7 +103,7 @@ int rsa_verify_hash_ex(const unsigned char *sig, unsigned long siglen, oid_st st; /* not all hashes have OIDs... so sad */ - if (hash_get_oid(hash_idx, &st) != CRYPT_OK) { + if (hash_get_oid(hash_algo, &st) != CRYPT_OK) { err = CRYPT_INVALID_ARG; goto bail_2; } |