1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file pkcs_1_oaep_decode.c
15 OAEP Padding for PKCS #1, Tom St Denis
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)
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,
39 unsigned char *DB, *seed, *mask;
40 unsigned long hLen, x, y, modulus_len;
43 LTC_ARGCHK(msg != NULL);
44 LTC_ARGCHK(out != NULL);
45 LTC_ARGCHK(outlen != NULL);
46 LTC_ARGCHK(res != NULL);
48 /* default to invalid packet */
52 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
55 hLen = hash_descriptor[hash_idx].hashsize;
56 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
58 /* test hash/message size */
59 if ((2*hLen >= (modulus_len - 2)) || (msglen != modulus_len)) {
60 return CRYPT_PK_INVALID_SIZE;
63 /* allocate ram for DB/mask/salt of size modulus_len */
64 DB = XMALLOC(modulus_len);
65 mask = XMALLOC(modulus_len);
67 if (DB == NULL || mask == NULL || seed == NULL) {
80 /* ok so it's now in the form
82 0x00 || maskedseed || maskedDB
84 1 || hLen || modulus_len - hLen - 1
88 /* must have leading 0x00 byte */
94 /* now read the masked seed */
96 XMEMCPY(seed, msg + x, hLen);
99 /* now read the masked DB */
100 XMEMCPY(DB, msg + x, modulus_len - hLen - 1);
101 x += modulus_len - hLen - 1;
103 /* compute MGF1 of maskedDB (hLen) */
104 if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) {
108 /* XOR against seed */
109 for (y = 0; y < hLen; y++) {
113 /* compute MGF1 of seed (k - hlen - 1) */
114 if ((err = pkcs_1_mgf1(hash_idx, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
119 for (y = 0; y < (modulus_len - hLen - 1); y++) {
123 /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
125 /* compute lhash and store it in seed [reuse temps!] */
127 if (lparam != NULL) {
128 if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) {
132 /* can't pass hash_memory a NULL so use DB with zero length */
133 if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) {
138 /* compare the lhash'es */
139 if (XMEMCMP(seed, DB, hLen) != 0) {
144 /* now zeroes before a 0x01 */
145 for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) {
149 /* error out if wasn't 0x01 */
150 if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) {
151 err = CRYPT_INVALID_PACKET;
155 /* rest is the message (and skip 0x01) */
156 if ((modulus_len - hLen - 1 - ++x) > *outlen) {
157 *outlen = modulus_len - hLen - 1 - x;
158 err = CRYPT_BUFFER_OVERFLOW;
163 *outlen = modulus_len - hLen - 1 - x;
164 XMEMCPY(out, DB + x, modulus_len - hLen - 1 - x);
165 x += modulus_len - hLen - 1;
172 #ifdef LTC_CLEAN_STACK
173 zeromem(DB, modulus_len);
175 zeromem(mask, modulus_len);
187 /* $Source: /cvs/libtom/libtomcrypt/src/pk/pkcs1/pkcs_1_oaep_decode.c,v $ */
188 /* $Revision: 1.11 $ */
189 /* $Date: 2006/11/01 09:28:17 $ */