9b173777835ec5b4033df1110bcad83e3cc9b98d
[oweals/openssl.git] / providers / implementations / asymciphers / rsa_enc.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/crypto.h>
11 #include <openssl/evp.h>
12 #include <openssl/core_numbers.h>
13 #include <openssl/core_names.h>
14 #include <openssl/rsa.h>
15 #include <openssl/params.h>
16 #include <openssl/err.h>
17 #include "internal/constant_time.h"
18 #include "prov/providercommonerr.h"
19 #include "prov/provider_ctx.h"
20 #include "prov/implementations.h"
21
22 #include <stdlib.h>
23
24 static OSSL_OP_asym_cipher_newctx_fn rsa_newctx;
25 static OSSL_OP_asym_cipher_encrypt_init_fn rsa_init;
26 static OSSL_OP_asym_cipher_encrypt_fn rsa_encrypt;
27 static OSSL_OP_asym_cipher_decrypt_init_fn rsa_init;
28 static OSSL_OP_asym_cipher_decrypt_fn rsa_decrypt;
29 static OSSL_OP_asym_cipher_freectx_fn rsa_freectx;
30 static OSSL_OP_asym_cipher_dupctx_fn rsa_dupctx;
31 static OSSL_OP_asym_cipher_get_ctx_params_fn rsa_get_ctx_params;
32 static OSSL_OP_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params;
33 static OSSL_OP_asym_cipher_set_ctx_params_fn rsa_set_ctx_params;
34 static OSSL_OP_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params;
35
36
37 /*
38  * What's passed as an actual key is defined by the KEYMGMT interface.
39  * We happen to know that our KEYMGMT simply passes RSA structures, so
40  * we use that here too.
41  */
42
43 typedef struct {
44     OPENSSL_CTX *libctx;
45     RSA *rsa;
46     int pad_mode;
47     /* OAEP message digest */
48     EVP_MD *oaep_md;
49     /* message digest for MGF1 */
50     EVP_MD *mgf1_md;
51     /* OAEP label */
52     unsigned char *oaep_label;
53     size_t oaep_labellen;
54 } PROV_RSA_CTX;
55
56 static void *rsa_newctx(void *provctx)
57 {
58     PROV_RSA_CTX *prsactx =  OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
59
60     if (prsactx == NULL)
61         return NULL;
62     prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
63
64     return prsactx;
65 }
66
67 static int rsa_init(void *vprsactx, void *vrsa)
68 {
69     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
70
71     if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
72         return 0;
73     RSA_free(prsactx->rsa);
74     prsactx->rsa = vrsa;
75     prsactx->pad_mode = RSA_PKCS1_PADDING;
76     return 1;
77 }
78
79 static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
80                        size_t outsize, const unsigned char *in, size_t inlen)
81 {
82     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
83     int ret;
84
85     if (out == NULL) {
86         size_t len = RSA_size(prsactx->rsa);
87
88         if (len == 0) {
89             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
90             return 0;
91         }
92         *outlen = len;
93         return 1;
94     }
95
96     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
97         int rsasize = RSA_size(prsactx->rsa);
98         unsigned char *tbuf;
99
100         if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
101             PROVerr(0, ERR_R_MALLOC_FAILURE);
102             return 0;
103         }
104         ret = RSA_padding_add_PKCS1_OAEP_mgf1(tbuf, rsasize, in, inlen,
105                                               prsactx->oaep_label,
106                                               prsactx->oaep_labellen,
107                                               prsactx->oaep_md,
108                                               prsactx->mgf1_md);
109
110         if (!ret) {
111             OPENSSL_free(tbuf);
112             return 0;
113         }
114         ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
115                                  RSA_NO_PADDING);
116         OPENSSL_free(tbuf);
117     } else {
118         ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
119                                  prsactx->pad_mode);
120     }
121     /* A ret value of 0 is not an error */
122     if (ret < 0)
123         return ret;
124     *outlen = ret;
125     return 1;
126 }
127
128 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
129                        size_t outsize, const unsigned char *in, size_t inlen)
130 {
131     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
132     int ret;
133
134     if (out == NULL) {
135         size_t len = RSA_size(prsactx->rsa);
136
137         if (len == 0) {
138             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
139             return 0;
140         }
141         *outlen = len;
142         return 1;
143     }
144
145     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
146         int rsasize = RSA_size(prsactx->rsa);
147         unsigned char *tbuf;
148
149         if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
150             PROVerr(0, ERR_R_MALLOC_FAILURE);
151             return 0;
152         }
153         ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
154                                   RSA_NO_PADDING);
155         if (ret <= 0) {
156             OPENSSL_free(tbuf);
157             return 0;
158         }
159         ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, tbuf,
160                                                 ret, ret,
161                                                 prsactx->oaep_label,
162                                                 prsactx->oaep_labellen,
163                                                 prsactx->oaep_md,
164                                                 prsactx->mgf1_md);
165         OPENSSL_free(tbuf);
166     } else {
167         ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
168                                   prsactx->pad_mode);
169     }
170     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
171     ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
172     return ret;
173 }
174
175 static void rsa_freectx(void *vprsactx)
176 {
177     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
178
179     RSA_free(prsactx->rsa);
180
181     EVP_MD_free(prsactx->oaep_md);
182     EVP_MD_free(prsactx->mgf1_md);
183
184     OPENSSL_free(prsactx);
185 }
186
187 static void *rsa_dupctx(void *vprsactx)
188 {
189     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
190     PROV_RSA_CTX *dstctx;
191
192     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
193     if (dstctx == NULL)
194         return NULL;
195
196     *dstctx = *srcctx;
197     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
198         OPENSSL_free(dstctx);
199         return NULL;
200     }
201
202     if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
203         RSA_free(dstctx->rsa);
204         OPENSSL_free(dstctx);
205         return NULL;
206     }
207
208     if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
209         RSA_free(dstctx->rsa);
210         EVP_MD_free(dstctx->oaep_md);
211         OPENSSL_free(dstctx);
212         return NULL;
213     }
214
215     return dstctx;
216 }
217
218 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
219 {
220     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
221     OSSL_PARAM *p;
222
223     if (prsactx == NULL || params == NULL)
224         return 0;
225
226     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
227     if (p != NULL && !OSSL_PARAM_set_int(p, prsactx->pad_mode))
228         return 0;
229
230     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
231     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
232                                                     ? ""
233                                                     : EVP_MD_name(prsactx->oaep_md)))
234         return 0;
235
236     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
237     if (p != NULL) {
238         EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
239                                                    : prsactx->mgf1_md;
240
241         if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
242                                            ? ""
243                                            : EVP_MD_name(mgf1_md)))
244         return 0;
245     }
246
247     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
248     if (p != NULL && !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label, 0))
249         return 0;
250
251     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN);
252     if (p != NULL && !OSSL_PARAM_set_size_t(p, prsactx->oaep_labellen))
253         return 0;
254
255     return 1;
256 }
257
258 static const OSSL_PARAM known_gettable_ctx_params[] = {
259     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
260     OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
261     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
262     OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
263                     NULL, 0),
264     OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
265     OSSL_PARAM_END
266 };
267
268 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
269 {
270     return known_gettable_ctx_params;
271 }
272
273 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
274 {
275     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
276     const OSSL_PARAM *p;
277     /* Should be big enough */
278     char mdname[80], mdprops[80] = { '\0' };
279     char *str = mdname;
280     int pad_mode;
281
282     if (prsactx == NULL || params == NULL)
283         return 0;
284
285     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
286     if (p != NULL) {
287         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
288             return 0;
289
290         str = mdprops;
291         p = OSSL_PARAM_locate_const(params,
292                                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
293         if (p != NULL) {
294             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
295                 return 0;
296         }
297
298         EVP_MD_free(prsactx->oaep_md);
299         prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
300
301         if (prsactx->oaep_md == NULL)
302             return 0;
303     }
304
305     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
306     if (p != NULL) {
307         if (!OSSL_PARAM_get_int(p, &pad_mode))
308             return 0;
309         /*
310          * PSS padding is for signatures only so is not compatible with
311          * asymmetric cipher use.
312          */
313         if (pad_mode == RSA_PKCS1_PSS_PADDING)
314             return 0;
315         if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
316             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
317             if (prsactx->oaep_md == NULL)
318                 return 0;
319         }
320         prsactx->pad_mode = pad_mode;
321     }
322
323     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
324     if (p != NULL) {
325         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
326             return 0;
327
328         str = mdprops;
329         p = OSSL_PARAM_locate_const(params,
330                                     OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
331         if (p != NULL) {
332             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
333                 return 0;
334         } else {
335             str = NULL;
336         }
337
338         EVP_MD_free(prsactx->mgf1_md);
339         prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
340
341         if (prsactx->mgf1_md == NULL)
342             return 0;
343     }
344
345     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
346     if (p != NULL) {
347         void *tmp_label = NULL;
348         size_t tmp_labellen;
349
350         if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
351             return 0;
352         OPENSSL_free(prsactx->oaep_label);
353         prsactx->oaep_label = (unsigned char *)tmp_label;
354         prsactx->oaep_labellen = tmp_labellen;
355     }
356
357     return 1;
358 }
359
360 static const OSSL_PARAM known_settable_ctx_params[] = {
361     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
362     OSSL_PARAM_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL),
363     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
364     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
365     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
366     OSSL_PARAM_END
367 };
368
369 static const OSSL_PARAM *rsa_settable_ctx_params(void)
370 {
371     return known_settable_ctx_params;
372 }
373
374 const OSSL_DISPATCH rsa_asym_cipher_functions[] = {
375     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
376     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_init },
377     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
378     { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_init },
379     { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
380     { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
381     { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
382     { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
383       (void (*)(void))rsa_get_ctx_params },
384     { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
385       (void (*)(void))rsa_gettable_ctx_params },
386     { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
387       (void (*)(void))rsa_set_ctx_params },
388     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
389       (void (*)(void))rsa_settable_ctx_params },
390     { 0, NULL }
391 };