Mercurial > dropbear
comparison src/pk/pkcs1/pkcs_1_oaep_decode.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_decode.c | |
15 OAEP Padding for PKCS #1, Tom St Denis | |
16 */ | |
17 | |
18 #ifdef PKCS_1 | |
19 | |
20 /** | |
21 PKCS #1 v2.00 OAEP decode | |
22 @param msg The encoded data to decode | |
23 @param msglen The length of the encoded data (octets) | |
24 @param lparam The session or system data (can be NULL) | |
25 @param lparamlen The length of the lparam | |
26 @param modulus_bitlen The bit length of the RSA modulus | |
27 @param hash_idx The index of the hash desired | |
28 @param out [out] Destination of decoding | |
29 @param outlen [in/out] The max size and resulting size of the decoding | |
30 @param res [out] Result of decoding, 1==valid, 0==invalid | |
31 @return CRYPT_OK if successful (even if invalid) | |
32 */ | |
33 int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, | |
34 const unsigned char *lparam, unsigned long lparamlen, | |
35 unsigned long modulus_bitlen, int hash_idx, | |
36 unsigned char *out, unsigned long *outlen, | |
37 int *res) | |
38 { | |
39 unsigned char *DB, *seed, *mask; | |
40 unsigned long hLen, x, y, modulus_len; | |
41 int err; | |
42 | |
43 LTC_ARGCHK(msg != NULL); | |
44 LTC_ARGCHK(out != NULL); | |
45 LTC_ARGCHK(outlen != NULL); | |
46 LTC_ARGCHK(res != NULL); | |
47 | |
48 /* default to invalid packet */ | |
49 *res = 0; | |
50 | |
51 /* test valid hash */ | |
52 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { | |
53 return err; | |
54 } | |
55 hLen = hash_descriptor[hash_idx].hashsize; | |
56 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); | |
57 | |
58 /* test hash/message size */ | |
59 if ((2*hLen >= (modulus_len - 2)) || (msglen != modulus_len)) { | |
60 return CRYPT_PK_INVALID_SIZE; | |
61 } | |
62 | |
63 /* allocate ram for DB/mask/salt of size modulus_len */ | |
64 DB = XMALLOC(modulus_len); | |
65 mask = XMALLOC(modulus_len); | |
66 seed = XMALLOC(modulus_len); | |
67 if (DB == NULL || mask == NULL || seed == NULL) { | |
68 if (DB != NULL) { | |
69 XFREE(DB); | |
70 } | |
71 if (mask != NULL) { | |
72 XFREE(mask); | |
73 } | |
74 if (seed != NULL) { | |
75 XFREE(seed); | |
76 } | |
77 return CRYPT_MEM; | |
78 } | |
79 | |
80 /* ok so it's now in the form | |
81 | |
82 0x00 || maskedseed || maskedDB | |
83 | |
84 1 || hLen || modulus_len - hLen - 1 | |
85 | |
86 */ | |
87 | |
88 /* must have leading 0x00 byte */ | |
89 if (msg[0] != 0x00) { | |
90 err = CRYPT_OK; | |
91 goto LBL_ERR; | |
92 } | |
93 | |
94 /* now read the masked seed */ | |
95 for (x = 1, y = 0; y < hLen; y++) { | |
96 seed[y] = msg[x++]; | |
97 } | |
98 | |
99 /* now read the masked DB */ | |
100 for (y = 0; y < modulus_len - hLen - 1; y++) { | |
101 DB[y] = msg[x++]; | |
102 } | |
103 | |
104 /* compute MGF1 of maskedDB (hLen) */ | |
105 if ((err = pkcs_1_mgf1(DB, modulus_len - hLen - 1, hash_idx, mask, hLen)) != CRYPT_OK) { | |
106 goto LBL_ERR; | |
107 } | |
108 | |
109 /* XOR against seed */ | |
110 for (y = 0; y < hLen; y++) { | |
111 seed[y] ^= mask[y]; | |
112 } | |
113 | |
114 /* compute MGF1 of seed (k - hlen - 1) */ | |
115 if ((err = pkcs_1_mgf1(seed, hLen, hash_idx, mask, modulus_len - hLen - 1)) != CRYPT_OK) { | |
116 goto LBL_ERR; | |
117 } | |
118 | |
119 /* xor against DB */ | |
120 for (y = 0; y < (modulus_len - hLen - 1); y++) { | |
121 DB[y] ^= mask[y]; | |
122 } | |
123 | |
124 /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */ | |
125 | |
126 /* compute lhash and store it in seed [reuse temps!] */ | |
127 x = modulus_len; | |
128 if (lparam != NULL) { | |
129 if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) { | |
130 goto LBL_ERR; | |
131 } | |
132 } else { | |
133 /* can't pass hash_memory a NULL so use DB with zero length */ | |
134 if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) { | |
135 goto LBL_ERR; | |
136 } | |
137 } | |
138 | |
139 /* compare the lhash'es */ | |
140 if (memcmp(seed, DB, hLen) != 0) { | |
141 err = CRYPT_OK; | |
142 goto LBL_ERR; | |
143 } | |
144 | |
145 /* now zeroes before a 0x01 */ | |
146 for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) { | |
147 /* step... */ | |
148 } | |
149 | |
150 /* error out if wasn't 0x01 */ | |
151 if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) { | |
152 err = CRYPT_OK; | |
153 goto LBL_ERR; | |
154 } | |
155 | |
156 /* rest is the message (and skip 0x01) */ | |
157 if ((modulus_len - hLen - 1) - ++x > *outlen) { | |
158 err = CRYPT_BUFFER_OVERFLOW; | |
159 goto LBL_ERR; | |
160 } | |
161 | |
162 /* copy message */ | |
163 *outlen = (modulus_len - hLen - 1) - x; | |
164 for (y = 0; x != (modulus_len - hLen - 1); ) { | |
165 out[y++] = DB[x++]; | |
166 } | |
167 | |
168 /* valid packet */ | |
169 *res = 1; | |
170 | |
171 err = CRYPT_OK; | |
172 LBL_ERR: | |
173 #ifdef LTC_CLEAN_STACK | |
174 zeromem(DB, modulus_len); | |
175 zeromem(seed, modulus_len); | |
176 zeromem(mask, modulus_len); | |
177 #endif | |
178 | |
179 XFREE(seed); | |
180 XFREE(mask); | |
181 XFREE(DB); | |
182 | |
183 return err; | |
184 } | |
185 | |
186 #endif /* PKCS_1 */ |