comparison libtomcrypt/src/pk/katja/katja_encrypt_key.c @ 1511:5916af64acd4 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Sat, 17 Feb 2018 19:29:51 +0800
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1457:32f990cc96b1 1511:5916af64acd4
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file katja_encrypt_key.c 12 @file katja_encrypt_key.c
15 Katja LTC_PKCS-style OAEP encryption, Tom St Denis 13 Katja PKCS-style OAEP encryption, Tom St Denis
16 */ 14 */
17 15
18 #ifdef MKAT 16 #ifdef LTC_MKAT
19 17
20 /** 18 /**
21 (LTC_PKCS #1 v2.0) OAEP pad then encrypt 19 (PKCS #1 v2.0) OAEP pad then encrypt
22 @param in The plaintext 20 @param in The plaintext
23 @param inlen The length of the plaintext (octets) 21 @param inlen The length of the plaintext (octets)
24 @param out [out] The ciphertext 22 @param out [out] The ciphertext
25 @param outlen [in/out] The max size and resulting size of the ciphertext 23 @param outlen [in/out] The max size and resulting size of the ciphertext
26 @param lparam The system "lparam" for the encryption 24 @param lparam The system "lparam" for the encryption
28 @param prng An active PRNG 26 @param prng An active PRNG
29 @param prng_idx The index of the desired prng 27 @param prng_idx The index of the desired prng
30 @param hash_idx The index of the desired hash 28 @param hash_idx The index of the desired hash
31 @param key The Katja key to encrypt to 29 @param key The Katja key to encrypt to
32 @return CRYPT_OK if successful 30 @return CRYPT_OK if successful
33 */ 31 */
34 int katja_encrypt_key(const unsigned char *in, unsigned long inlen, 32 int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
35 unsigned char *out, unsigned long *outlen, 33 unsigned char *out, unsigned long *outlen,
36 const unsigned char *lparam, unsigned long lparamlen, 34 const unsigned char *lparam, unsigned long lparamlen,
37 prng_state *prng, int prng_idx, int hash_idx, katja_key *key) 35 prng_state *prng, int prng_idx, int hash_idx, katja_key *key)
38 { 36 {
39 unsigned long modulus_bitlen, modulus_bytelen, x; 37 unsigned long modulus_bitlen, modulus_bytelen, x;
40 int err; 38 int err;
41 39
42 LTC_ARGCHK(in != NULL); 40 LTC_ARGCHK(in != NULL);
43 LTC_ARGCHK(out != NULL); 41 LTC_ARGCHK(out != NULL);
44 LTC_ARGCHK(outlen != NULL); 42 LTC_ARGCHK(outlen != NULL);
45 LTC_ARGCHK(key != NULL); 43 LTC_ARGCHK(key != NULL);
46 44
47 /* valid prng and hash ? */ 45 /* valid prng and hash ? */
48 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { 46 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
49 return err; 47 return err;
50 } 48 }
51 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { 49 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
52 return err; 50 return err;
53 } 51 }
54 52
55 /* get modulus len in bits */ 53 /* get modulus len in bits */
56 modulus_bitlen = mp_count_bits((key->N)); 54 modulus_bitlen = mp_count_bits((key->N));
57 55
58 /* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */ 56 /* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */
59 modulus_bitlen = ((modulus_bitlen << 1) / 3); 57 modulus_bitlen = ((modulus_bitlen << 1) / 3);
68 return CRYPT_BUFFER_OVERFLOW; 66 return CRYPT_BUFFER_OVERFLOW;
69 } 67 }
70 68
71 /* OAEP pad the key */ 69 /* OAEP pad the key */
72 x = *outlen; 70 x = *outlen;
73 if ((err = pkcs_1_oaep_encode(in, inlen, lparam, 71 if ((err = pkcs_1_oaep_encode(in, inlen, lparam,
74 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx, 72 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
75 out, &x)) != CRYPT_OK) { 73 out, &x)) != CRYPT_OK) {
76 return err; 74 return err;
77 } 75 }
78 76
79 /* Katja exptmod the OAEP pad */ 77 /* Katja exptmod the OAEP pad */
80 return katja_exptmod(out, x, out, outlen, PK_PUBLIC, key); 78 return katja_exptmod(out, x, out, outlen, PK_PUBLIC, key);
81 } 79 }
82 80
83 #endif /* LTC_MRSA */ 81 #endif /* LTC_MRSA */
84 82
85 /* $Source$ */ 83 /* ref: $Format:%D$ */
86 /* $Revision$ */ 84 /* git commit: $Format:%H$ */
87 /* $Date$ */ 85 /* commit time: $Format:%ai$ */