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
12 @file pkcs_1_oaep_decode.c
13 OAEP Padding for PKCS #1, Tom St Denis
19 PKCS #1 v2.00 OAEP decode
20 @param msg The encoded data to decode
21 @param msglen The length of the encoded data (octets)
22 @param lparam The session or system data (can be NULL)
23 @param lparamlen The length of the lparam
24 @param modulus_bitlen The bit length of the RSA modulus
25 @param hash_idx The index of the hash desired
26 @param out [out] Destination of decoding
27 @param outlen [in/out] The max size and resulting size of the decoding
28 @param res [out] Result of decoding, 1==valid, 0==invalid
29 @return CRYPT_OK if successful
31 int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
32 const unsigned char *lparam, unsigned long lparamlen,
33 unsigned long modulus_bitlen, int hash_idx,
34 unsigned char *out, unsigned long *outlen,
37 unsigned char *DB, *seed, *mask;
38 unsigned long hLen, x, y, modulus_len;
41 LTC_ARGCHK(msg != NULL);
42 LTC_ARGCHK(out != NULL);
43 LTC_ARGCHK(outlen != NULL);
44 LTC_ARGCHK(res != NULL);
46 /* default to invalid packet */
50 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
53 hLen = hash_descriptor[hash_idx].hashsize;
54 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
56 /* test hash/message size */
57 if ((2*hLen >= (modulus_len - 2)) || (msglen != modulus_len)) {
58 return CRYPT_PK_INVALID_SIZE;
61 /* allocate ram for DB/mask/salt of size modulus_len */
62 DB = XMALLOC(modulus_len);
63 mask = XMALLOC(modulus_len);
65 if (DB == NULL || mask == NULL || seed == NULL) {
78 /* ok so it's now in the form
80 0x00 || maskedseed || maskedDB
82 1 || hLen || modulus_len - hLen - 1
88 /* must have leading 0x00 byte */
90 ret = CRYPT_INVALID_PACKET;
93 /* now read the masked seed */
95 XMEMCPY(seed, msg + x, hLen);
98 /* now read the masked DB */
99 XMEMCPY(DB, msg + x, modulus_len - hLen - 1);
100 x += modulus_len - hLen - 1;
102 /* compute MGF1 of maskedDB (hLen) */
103 if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) {
107 /* XOR against seed */
108 for (y = 0; y < hLen; y++) {
112 /* compute MGF1 of seed (k - hlen - 1) */
113 if ((err = pkcs_1_mgf1(hash_idx, seed, hLen, mask, modulus_len - hLen - 1)) != CRYPT_OK) {
118 for (y = 0; y < (modulus_len - hLen - 1); y++) {
122 /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
124 /* compute lhash and store it in seed [reuse temps!] */
126 if (lparam != NULL) {
127 if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) {
131 /* can't pass hash_memory a NULL so use DB with zero length */
132 if ((err = hash_memory(hash_idx, DB, 0, seed, &x)) != CRYPT_OK) {
137 /* compare the lhash'es */
138 if (XMEM_NEQ(seed, DB, hLen) != 0) {
139 ret = CRYPT_INVALID_PACKET;
142 /* now zeroes before a 0x01 */
143 for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) {
147 /* error if wasn't 0x01 */
148 if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) {
149 ret = CRYPT_INVALID_PACKET;
152 /* rest is the message (and skip 0x01) */
153 if ((modulus_len - hLen - 1 - ++x) > *outlen) {
154 ret = CRYPT_INVALID_PACKET;
157 if (ret == CRYPT_OK) {
159 *outlen = modulus_len - hLen - 1 - x;
160 XMEMCPY(out, DB + x, modulus_len - hLen - 1 - x);
168 #ifdef LTC_CLEAN_STACK
169 zeromem(DB, modulus_len);
171 zeromem(mask, modulus_len);
181 #endif /* LTC_PKCS_1 */
183 /* ref: $Format:%D$ */
184 /* git commit: $Format:%H$ */
185 /* commit time: $Format:%ai$ */