Mercurial > dropbear
comparison src/pk/pkcs1/pkcs_1_oaep_encode.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 * | |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | |
10 */ | |
11 #include "tomcrypt.h" | |
12 | |
13 /** | |
14 @file pkcs_1_oaep_encode.c | |
15 OAEP Padding for PKCS #1, Tom St Denis | |
16 */ | |
17 | |
18 #ifdef PKCS_1 | |
19 | |
20 /** | |
21 PKCS #1 v2.00 OAEP encode | |
22 @param msg The data to encode | |
23 @param msglen The length of the data to encode (octets) | |
24 @param lparam A session or system parameter (can be NULL) | |
25 @param lparamlen The length of the lparam data | |
26 @param modulus_bitlen The bit length of the RSA modulus | |
27 @param prng An active PRNG state | |
28 @param prng_idx The index of the PRNG desired | |
29 @param hash_idx The index of the hash desired | |
30 @param out [out] The destination for the encoded data | |
31 @param outlen [in/out] The max size and resulting size of the encoded data | |
32 @return CRYPT_OK if successful | |
33 */ | |
34 int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen, | |
35 const unsigned char *lparam, unsigned long lparamlen, | |
36 unsigned long modulus_bitlen, prng_state *prng, | |
37 int prng_idx, int hash_idx, | |
38 unsigned char *out, unsigned long *outlen) | |
39 { | |
40 unsigned char *DB, *seed, *mask; | |
41 unsigned long hLen, x, y, modulus_len; | |
42 int err; | |
43 | |
44 LTC_ARGCHK(msg != NULL); | |
45 LTC_ARGCHK(out != NULL); | |
46 LTC_ARGCHK(outlen != NULL); | |
47 | |
48 /* test valid hash */ | |
49 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { | |
50 return err; | |
51 } | |
52 | |
53 /* valid prng */ | |
54 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { | |
55 return err; | |
56 } | |
57 | |
58 hLen = hash_descriptor[hash_idx].hashsize; | |
59 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); | |
60 | |
61 /* test message size */ | |
62 if ((2*hLen >= (modulus_len - 2)) || (msglen > (modulus_len - 2*hLen - 2))) { | |
63 return CRYPT_PK_INVALID_SIZE; | |
64 } | |
65 | |
66 /* allocate ram for DB/mask/salt of size modulus_len */ | |
67 DB = XMALLOC(modulus_len); | |
68 mask = XMALLOC(modulus_len); | |
69 seed = XMALLOC(modulus_len); | |
70 if (DB == NULL || mask == NULL || seed == NULL) { | |
71 if (DB != NULL) { | |
72 XFREE(DB); | |
73 } | |
74 if (mask != NULL) { | |
75 XFREE(mask); | |
76 } | |
77 if (seed != NULL) { | |
78 XFREE(seed); | |
79 } | |
80 return CRYPT_MEM; | |
81 } | |
82 | |
83 /* get lhash */ | |
84 /* DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */ | |
85 x = modulus_len; | |
86 if (lparam != NULL) { | |
87 if ((err = hash_memory(hash_idx, lparam, lparamlen, DB, &x)) != CRYPT_OK) { | |
88 goto LBL_ERR; | |
89 } | |
90 } else { | |
91 /* can't pass hash_memory a NULL so use DB with zero length */ | |
92 if ((err = hash_memory(hash_idx, DB, 0, DB, &x)) != CRYPT_OK) { | |
93 goto LBL_ERR; | |
94 } | |
95 } | |
96 | |
97 /* append PS then 0x01 (to lhash) */ | |
98 x = hLen; | |
99 y = modulus_len - msglen - 2*hLen - 2; | |
100 while (y--) { | |
101 DB[x++] = 0x00; | |
102 } | |
103 DB[x++] = 0x01; | |
104 | |
105 /* message */ | |
106 y = msglen; | |
107 while (y--) { | |
108 DB[x++] = *msg++; | |
109 } | |
110 | |
111 /* now choose a random seed */ | |
112 if (prng_descriptor[prng_idx].read(seed, hLen, prng) != hLen) { | |
113 err = CRYPT_ERROR_READPRNG; | |
114 goto LBL_ERR; | |
115 } | |
116 | |
117 /* compute MGF1 of seed (k - hlen - 1) */ | |
118 if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { | |
119 goto LBL_ERR; | |
120 } | |
121 | |
122 /* xor against DB */ | |
123 for (y = 0; y < (modulus_len - hLen - 1); y++) { | |
124 DB[y] ^= mask[y]; | |
125 } | |
126 | |
127 /* compute MGF1 of maskedDB (hLen) */ | |
128 if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) { | |
129 goto LBL_ERR; | |
130 } | |
131 | |
132 /* XOR against seed */ | |
133 for (y = 0; y < hLen; y++) { | |
134 seed[y] ^= mask[y]; | |
135 } | |
136 | |
137 /* create string of length modulus_len */ | |
138 if (*outlen < modulus_len) { | |
139 err = CRYPT_BUFFER_OVERFLOW; | |
140 goto LBL_ERR; | |
141 } | |
142 | |
143 /* start output which is 0x00 || maskedSeed || maskedDB */ | |
144 x = 0; | |
145 out[x++] = 0x00; | |
146 for (y = 0; y < hLen; y++) { | |
147 out[x++] = seed[y]; | |
148 } | |
149 for (y = 0; y < modulus_len - hLen - 1; y++) { | |
150 out[x++] = DB[y]; | |
151 } | |
152 *outlen = x; | |
153 | |
154 err = CRYPT_OK; | |
155 LBL_ERR: | |
156 #ifdef LTC_CLEAN_STACK | |
157 zeromem(DB, modulus_len); | |
158 zeromem(seed, modulus_len); | |
159 zeromem(mask, modulus_len); | |
160 #endif | |
161 | |
162 XFREE(seed); | |
163 XFREE(mask); | |
164 XFREE(DB); | |
165 | |
166 return err; | |
167 } | |
168 | |
169 #endif /* PKCS_1 */ | |
170 |