Add KEM (Key encapsulation mechanism) support to providers
[openssl.git] / providers / implementations / kem / rsa_kem.c
1 /*
2  * Copyright 2020 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 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include "e_os.h"  /* strcasecmp */
17 #include <openssl/crypto.h>
18 #include <openssl/evp.h>
19 #include <openssl/core_dispatch.h>
20 #include <openssl/core_names.h>
21 #include <openssl/rsa.h>
22 #include <openssl/params.h>
23 #include <openssl/err.h>
24 #include <crypto/rsa.h>
25 #include "prov/providercommonerr.h"
26 #include "prov/provider_ctx.h"
27 #include "prov/implementations.h"
28
29 static OSSL_FUNC_kem_newctx_fn rsakem_newctx;
30 static OSSL_FUNC_kem_encapsulate_init_fn rsakem_init;
31 static OSSL_FUNC_kem_encapsulate_fn rsakem_generate;
32 static OSSL_FUNC_kem_decapsulate_init_fn rsakem_init;
33 static OSSL_FUNC_kem_decapsulate_fn rsakem_recover;
34 static OSSL_FUNC_kem_freectx_fn rsakem_freectx;
35 static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx;
36 static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params;
37 static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params;
38 static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params;
39 static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params;
40
41 /*
42  * Only the KEM for RSASVE as defined in SP800-56b r2 is implemented
43  * currently.
44  */
45 #define KEM_OP_UNDEFINED   -1
46 #define KEM_OP_RSASVE       0
47
48 /*
49  * What's passed as an actual key is defined by the KEYMGMT interface.
50  * We happen to know that our KEYMGMT simply passes RSA structures, so
51  * we use that here too.
52  */
53 typedef struct {
54     OPENSSL_CTX *libctx;
55     RSA *rsa;
56     int op;
57 } PROV_RSA_CTX;
58
59 static const OSSL_ITEM rsakem_opname_id_map[] = {
60     { KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE },
61 };
62
63 static int name2id(const char *name, const OSSL_ITEM *map, size_t sz)
64 {
65     size_t i;
66
67     if (name == NULL)
68         return -1;
69
70     for (i = 0; i < sz; ++i) {
71         if (strcasecmp(map[i].ptr, name) == 0)
72             return map[i].id;
73     }
74     return -1;
75 }
76
77 static int rsakem_opname2id(const char *name)
78 {
79     return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map));
80 }
81
82 static void *rsakem_newctx(void *provctx)
83 {
84     PROV_RSA_CTX *prsactx =  OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
85
86     if (prsactx == NULL)
87         return NULL;
88     prsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
89     prsactx->op = KEM_OP_UNDEFINED;
90
91     return prsactx;
92 }
93
94 static void rsakem_freectx(void *vprsactx)
95 {
96     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
97
98     RSA_free(prsactx->rsa);
99     OPENSSL_free(prsactx);
100 }
101
102 static void *rsakem_dupctx(void *vprsactx)
103 {
104     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
105     PROV_RSA_CTX *dstctx;
106
107     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
108     if (dstctx == NULL)
109         return NULL;
110
111     *dstctx = *srcctx;
112     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
113         OPENSSL_free(dstctx);
114         return NULL;
115     }
116     return dstctx;
117 }
118
119 static int rsakem_init(void *vprsactx, void *vrsa)
120 {
121     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
122
123     if (prsactx == NULL || vrsa == NULL || !RSA_up_ref(vrsa))
124         return 0;
125     RSA_free(prsactx->rsa);
126     prsactx->rsa = vrsa;
127     /* TODO(3.0) Add a RSA keylength check here for fips */
128     return 1;
129 }
130
131 static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
132 {
133     PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx;
134
135     if (ctx == NULL || params == NULL)
136         return 0;
137     return 1;
138 }
139
140 static const OSSL_PARAM known_gettable_rsakem_ctx_params[] = {
141     OSSL_PARAM_END
142 };
143
144 static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *provctx)
145 {
146     return known_gettable_rsakem_ctx_params;
147 }
148
149 static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
150 {
151     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
152     const OSSL_PARAM *p;
153     int op;
154
155     if (prsactx == NULL || params == NULL)
156         return 0;
157
158     p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_OPERATION);
159     if (p != NULL) {
160         if (p->data_type != OSSL_PARAM_UTF8_STRING)
161             return 0;
162         op = rsakem_opname2id(p->data);
163         if (op < 0)
164             return 0;
165         prsactx->op = op;
166     }
167     return 1;
168 }
169
170 static const OSSL_PARAM known_settable_rsakem_ctx_params[] = {
171     OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0),
172     OSSL_PARAM_END
173 };
174
175 static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *provctx)
176 {
177     return known_settable_rsakem_ctx_params;
178 }
179
180 /*
181  * NIST.SP.800-56Br2
182  * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
183  *
184  * Generate a random in the range 1 < z < (n – 1)
185  */
186 static int rsasve_gen_rand_bytes(RSA *rsa_pub,
187                                  unsigned char *out, int outlen)
188 {
189     int ret = 0;
190     BN_CTX *bnctx;
191     BIGNUM *z, *nminus3;
192
193     bnctx = BN_CTX_secure_new_ex(rsa_get0_libctx(rsa_pub));
194     if (bnctx == NULL)
195         return 0;
196
197     /*
198      * Generate a random in the range 1 < z < (n – 1).
199      * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max
200      * We can achieve this by adding 2.. but then we need to subtract 3 from
201      * the upper bound i.e: 2 + (0 <= r < (n - 3))
202      */
203     BN_CTX_start(bnctx);
204     nminus3 = BN_CTX_get(bnctx);
205     z = BN_CTX_get(bnctx);
206     ret = (z != NULL
207            && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL)
208            && BN_sub_word(nminus3, 3)
209            && BN_priv_rand_range_ex(z, nminus3, bnctx)
210            && BN_add_word(z, 2)
211            && (BN_bn2binpad(z, out, outlen) == outlen));
212     BN_CTX_end(bnctx);
213     BN_CTX_free(bnctx);
214     return ret;
215 }
216
217 /*
218  * NIST.SP.800-56Br2
219  * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE).
220  */
221 static int rsasve_generate(PROV_RSA_CTX *prsactx,
222                            unsigned char *out, size_t *outlen,
223                            unsigned char *secret, size_t *secretlen)
224 {
225     int ret;
226     size_t nlen;
227
228     /* Step (1): nlen = Ceil(len(n)/8) */
229     nlen = RSA_size(prsactx->rsa);
230
231     if (out == NULL) {
232         if (nlen == 0) {
233             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
234             return 0;
235         }
236         if (outlen == NULL && secretlen == NULL)
237             return 0;
238         if (outlen != NULL)
239             *outlen = nlen;
240         if (secretlen != NULL)
241             *secretlen = nlen;
242         return 1;
243     }
244     /*
245      * Step (2): Generate a random byte string z of nlen bytes where
246      *            1 < z < n - 1
247      */
248     if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, nlen))
249         return 0;
250
251     /* Step(3): out = RSAEP((n,e), z) */
252     ret = RSA_public_encrypt(nlen, secret, out, prsactx->rsa, RSA_NO_PADDING);
253     if (ret) {
254         ret = 1;
255         if (outlen != NULL)
256             *outlen = nlen;
257         if (secretlen != NULL)
258             *secretlen = nlen;
259     } else {
260         OPENSSL_cleanse(secret, nlen);
261     }
262     return ret;
263 }
264
265 /*
266  * NIST.SP.800-56Br2
267  * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER).
268  */
269 static int rsasve_recover(PROV_RSA_CTX *prsactx,
270                           unsigned char *out, size_t *outlen,
271                           const unsigned char *in, size_t inlen)
272 {
273     size_t nlen;
274
275     /* Step (1): get the byte length of n */
276     nlen = RSA_size(prsactx->rsa);
277
278     if (out == NULL) {
279         if (nlen == 0) {
280             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
281             return 0;
282         }
283         *outlen = nlen;
284         return 1;
285     }
286
287     /* Step (2): check the input ciphertext 'inlen' matches the nlen */
288     if (inlen != nlen) {
289         ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
290         return 0;
291     }
292     /* Step (3): out = RSADP((n,d), in) */
293     return (RSA_private_decrypt(inlen, in, out, prsactx->rsa, RSA_NO_PADDING) > 0);
294 }
295
296 static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen,
297                            unsigned char *secret, size_t *secretlen)
298 {
299     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
300
301     switch (prsactx->op) {
302         case KEM_OP_RSASVE:
303             return rsasve_generate(prsactx, out, outlen, secret, secretlen);
304         default:
305             return -2;
306     }
307 }
308
309 static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen,
310                           const unsigned char *in, size_t inlen)
311 {
312     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
313
314     switch (prsactx->op) {
315         case KEM_OP_RSASVE:
316             return rsasve_recover(prsactx, out, outlen, in, inlen);
317         default:
318             return -2;
319     }
320 }
321
322 const OSSL_DISPATCH rsa_asym_kem_functions[] = {
323     { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx },
324     { OSSL_FUNC_KEM_ENCAPSULATE_INIT,
325       (void (*)(void))rsakem_init },
326     { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate },
327     { OSSL_FUNC_KEM_DECAPSULATE_INIT,
328       (void (*)(void))rsakem_init },
329     { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover },
330     { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx },
331     { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx },
332     { OSSL_FUNC_KEM_GET_CTX_PARAMS,
333       (void (*)(void))rsakem_get_ctx_params },
334     { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS,
335       (void (*)(void))rsakem_gettable_ctx_params },
336     { OSSL_FUNC_KEM_SET_CTX_PARAMS,
337       (void (*)(void))rsakem_set_ctx_params },
338     { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS,
339       (void (*)(void))rsakem_settable_ctx_params },
340     { 0, NULL }
341 };