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