2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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;
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.
47 /* OAEP message digest */
49 /* message digest for MGF1 */
52 unsigned char *oaep_label;
56 static void *rsa_newctx(void *provctx)
58 PROV_RSA_CTX *prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
62 prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
67 static int rsa_init(void *vprsactx, void *vrsa)
69 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
71 if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
73 RSA_free(prsactx->rsa);
75 prsactx->pad_mode = RSA_PKCS1_PADDING;
79 static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
80 size_t outsize, const unsigned char *in, size_t inlen)
82 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
86 size_t len = RSA_size(prsactx->rsa);
89 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
96 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
97 int rsasize = RSA_size(prsactx->rsa);
100 if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
101 PROVerr(0, ERR_R_MALLOC_FAILURE);
104 ret = RSA_padding_add_PKCS1_OAEP_mgf1(tbuf, rsasize, in, inlen,
106 prsactx->oaep_labellen,
114 ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
118 ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
121 /* A ret value of 0 is not an error */
128 static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
129 size_t outsize, const unsigned char *in, size_t inlen)
131 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
135 size_t len = RSA_size(prsactx->rsa);
138 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
145 if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
146 int rsasize = RSA_size(prsactx->rsa);
149 if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
150 PROVerr(0, ERR_R_MALLOC_FAILURE);
153 ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
159 ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, ret, tbuf,
162 prsactx->oaep_labellen,
167 ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
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);
175 static void rsa_freectx(void *vprsactx)
177 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
179 RSA_free(prsactx->rsa);
181 EVP_MD_free(prsactx->oaep_md);
182 EVP_MD_free(prsactx->mgf1_md);
184 OPENSSL_free(prsactx);
187 static void *rsa_dupctx(void *vprsactx)
189 PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
190 PROV_RSA_CTX *dstctx;
192 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
197 if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
198 OPENSSL_free(dstctx);
202 if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
203 RSA_free(dstctx->rsa);
204 OPENSSL_free(dstctx);
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);
218 static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
220 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
223 if (prsactx == NULL || params == NULL)
226 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
227 if (p != NULL && !OSSL_PARAM_set_int(p, prsactx->pad_mode))
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
233 : EVP_MD_name(prsactx->oaep_md)))
236 p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
238 EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
241 if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
243 : EVP_MD_name(mgf1_md)))
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))
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))
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,
264 OSSL_PARAM_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, NULL),
268 static const OSSL_PARAM *rsa_gettable_ctx_params(void)
270 return known_gettable_ctx_params;
273 static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
275 PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
277 /* Should be big enough */
278 char mdname[80], mdprops[80] = { '\0' };
282 if (prsactx == NULL || params == NULL)
285 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
287 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
291 p = OSSL_PARAM_locate_const(params,
292 OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
294 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
298 EVP_MD_free(prsactx->oaep_md);
299 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
301 if (prsactx->oaep_md == NULL)
305 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
307 if (!OSSL_PARAM_get_int(p, &pad_mode))
310 * PSS padding is for signatures only so is not compatible with
311 * asymmetric cipher use.
313 if (pad_mode == RSA_PKCS1_PSS_PADDING)
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)
320 prsactx->pad_mode = pad_mode;
323 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
325 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
329 p = OSSL_PARAM_locate_const(params,
330 OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
332 if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
338 EVP_MD_free(prsactx->mgf1_md);
339 prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
341 if (prsactx->mgf1_md == NULL)
345 p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
347 void *tmp_label = NULL;
350 if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
352 OPENSSL_free(prsactx->oaep_label);
353 prsactx->oaep_label = (unsigned char *)tmp_label;
354 prsactx->oaep_labellen = tmp_labellen;
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),
369 static const OSSL_PARAM *rsa_settable_ctx_params(void)
371 return known_settable_ctx_params;
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 },