]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/pkcs1/pkcs_1_oaep_decode.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / pkcs1 / pkcs_1_oaep_decode.c
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 #include "tomcrypt.h"
10
11 /**
12   @file pkcs_1_oaep_decode.c
13   OAEP Padding for PKCS #1, Tom St Denis
14 */
15
16 #ifdef LTC_PKCS_1
17
18 /**
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
30 */
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,
35                              int           *res)
36 {
37    unsigned char *DB, *seed, *mask;
38    unsigned long hLen, x, y, modulus_len;
39    int           err, ret;
40
41    LTC_ARGCHK(msg    != NULL);
42    LTC_ARGCHK(out    != NULL);
43    LTC_ARGCHK(outlen != NULL);
44    LTC_ARGCHK(res    != NULL);
45
46    /* default to invalid packet */
47    *res = 0;
48
49    /* test valid hash */
50    if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
51       return err;
52    }
53    hLen        = hash_descriptor[hash_idx].hashsize;
54    modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
55
56    /* test hash/message size */
57    if ((2*hLen >= (modulus_len - 2)) || (msglen != modulus_len)) {
58       return CRYPT_PK_INVALID_SIZE;
59    }
60
61    /* allocate ram for DB/mask/salt of size modulus_len */
62    DB   = XMALLOC(modulus_len);
63    mask = XMALLOC(modulus_len);
64    seed = XMALLOC(hLen);
65    if (DB == NULL || mask == NULL || seed == NULL) {
66       if (DB != NULL) {
67          XFREE(DB);
68       }
69       if (mask != NULL) {
70          XFREE(mask);
71       }
72       if (seed != NULL) {
73          XFREE(seed);
74       }
75       return CRYPT_MEM;
76    }
77
78    /* ok so it's now in the form
79
80       0x00  || maskedseed || maskedDB
81
82        1    ||   hLen     ||  modulus_len - hLen - 1
83
84     */
85
86    ret = CRYPT_OK;
87
88    /* must have leading 0x00 byte */
89    if (msg[0] != 0x00) {
90       ret = CRYPT_INVALID_PACKET;
91    }
92
93    /* now read the masked seed */
94    x = 1;
95    XMEMCPY(seed, msg + x, hLen);
96    x += hLen;
97
98    /* now read the masked DB */
99    XMEMCPY(DB, msg + x, modulus_len - hLen - 1);
100    x += modulus_len - hLen - 1;
101
102    /* compute MGF1 of maskedDB (hLen) */
103    if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) {
104       goto LBL_ERR;
105    }
106
107    /* XOR against seed */
108    for (y = 0; y < hLen; y++) {
109       seed[y] ^= mask[y];
110    }
111
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) {
114       goto LBL_ERR;
115    }
116
117    /* xor against DB */
118    for (y = 0; y < (modulus_len - hLen - 1); y++) {
119        DB[y] ^= mask[y];
120    }
121
122    /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
123
124    /* compute lhash and store it in seed [reuse temps!] */
125    x = modulus_len;
126    if (lparam != NULL) {
127       if ((err = hash_memory(hash_idx, lparam, lparamlen, seed, &x)) != CRYPT_OK) {
128          goto LBL_ERR;
129       }
130    } else {
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) {
133          goto LBL_ERR;
134       }
135    }
136
137    /* compare the lhash'es */
138    if (XMEM_NEQ(seed, DB, hLen) != 0) {
139       ret = CRYPT_INVALID_PACKET;
140    }
141
142    /* now zeroes before a 0x01 */
143    for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) {
144       /* step... */
145    }
146
147    /* error if wasn't 0x01 */
148    if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) {
149       ret = CRYPT_INVALID_PACKET;
150    }
151
152    /* rest is the message (and skip 0x01) */
153    if ((modulus_len - hLen - 1 - ++x) > *outlen) {
154       ret = CRYPT_INVALID_PACKET;
155    }
156
157    if (ret == CRYPT_OK) {
158       /* copy message */
159       *outlen = modulus_len - hLen - 1 - x;
160       XMEMCPY(out, DB + x, modulus_len - hLen - 1 - x);
161
162       /* valid packet */
163       *res = 1;
164    }
165    err = ret;
166
167 LBL_ERR:
168 #ifdef LTC_CLEAN_STACK
169    zeromem(DB,   modulus_len);
170    zeromem(seed, hLen);
171    zeromem(mask, modulus_len);
172 #endif
173
174    XFREE(seed);
175    XFREE(mask);
176    XFREE(DB);
177
178    return err;
179 }
180
181 #endif /* LTC_PKCS_1 */
182
183 /* ref:         $Format:%D$ */
184 /* git commit:  $Format:%H$ */
185 /* commit time: $Format:%ai$ */