1 1.1 christos /* 2 1.1 christos * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos /* 11 1.1 christos * RSA low level APIs are deprecated for public use, but still ok for 12 1.1 christos * internal use. 13 1.1 christos */ 14 1.1 christos #include "internal/deprecated.h" 15 1.1 christos #include "internal/nelem.h" 16 1.1 christos 17 1.1 christos #include <openssl/crypto.h> 18 1.1 christos #include <openssl/evp.h> 19 1.1 christos #include <openssl/core_dispatch.h> 20 1.1 christos #include <openssl/core_names.h> 21 1.1 christos #include <openssl/rsa.h> 22 1.1 christos #include <openssl/params.h> 23 1.1 christos #include <openssl/err.h> 24 1.1 christos #include "crypto/rsa.h" 25 1.1 christos #include <openssl/proverr.h> 26 1.1 christos #include "prov/provider_ctx.h" 27 1.1 christos #include "prov/implementations.h" 28 1.1 christos #include "prov/securitycheck.h" 29 1.1 christos 30 1.1 christos static OSSL_FUNC_kem_newctx_fn rsakem_newctx; 31 1.1 christos static OSSL_FUNC_kem_encapsulate_init_fn rsakem_encapsulate_init; 32 1.1 christos static OSSL_FUNC_kem_encapsulate_fn rsakem_generate; 33 1.1 christos static OSSL_FUNC_kem_decapsulate_init_fn rsakem_decapsulate_init; 34 1.1 christos static OSSL_FUNC_kem_decapsulate_fn rsakem_recover; 35 1.1 christos static OSSL_FUNC_kem_freectx_fn rsakem_freectx; 36 1.1 christos static OSSL_FUNC_kem_dupctx_fn rsakem_dupctx; 37 1.1 christos static OSSL_FUNC_kem_get_ctx_params_fn rsakem_get_ctx_params; 38 1.1 christos static OSSL_FUNC_kem_gettable_ctx_params_fn rsakem_gettable_ctx_params; 39 1.1 christos static OSSL_FUNC_kem_set_ctx_params_fn rsakem_set_ctx_params; 40 1.1 christos static OSSL_FUNC_kem_settable_ctx_params_fn rsakem_settable_ctx_params; 41 1.1 christos 42 1.1 christos /* 43 1.1 christos * Only the KEM for RSASVE as defined in SP800-56b r2 is implemented 44 1.1 christos * currently. 45 1.1 christos */ 46 1.1 christos #define KEM_OP_UNDEFINED -1 47 1.1 christos #define KEM_OP_RSASVE 0 48 1.1 christos 49 1.1 christos /* 50 1.1 christos * What's passed as an actual key is defined by the KEYMGMT interface. 51 1.1 christos * We happen to know that our KEYMGMT simply passes RSA structures, so 52 1.1 christos * we use that here too. 53 1.1 christos */ 54 1.1 christos typedef struct { 55 1.1 christos OSSL_LIB_CTX *libctx; 56 1.1 christos RSA *rsa; 57 1.1 christos int op; 58 1.1 christos } PROV_RSA_CTX; 59 1.1 christos 60 1.1 christos static const OSSL_ITEM rsakem_opname_id_map[] = { 61 1.1 christos { KEM_OP_RSASVE, OSSL_KEM_PARAM_OPERATION_RSASVE }, 62 1.1 christos }; 63 1.1 christos 64 1.1 christos static int name2id(const char *name, const OSSL_ITEM *map, size_t sz) 65 1.1 christos { 66 1.1 christos size_t i; 67 1.1 christos 68 1.1 christos if (name == NULL) 69 1.1 christos return -1; 70 1.1 christos 71 1.1 christos for (i = 0; i < sz; ++i) { 72 1.1 christos if (OPENSSL_strcasecmp(map[i].ptr, name) == 0) 73 1.1 christos return map[i].id; 74 1.1 christos } 75 1.1 christos return -1; 76 1.1 christos } 77 1.1 christos 78 1.1 christos static int rsakem_opname2id(const char *name) 79 1.1 christos { 80 1.1 christos return name2id(name, rsakem_opname_id_map, OSSL_NELEM(rsakem_opname_id_map)); 81 1.1 christos } 82 1.1 christos 83 1.1 christos static void *rsakem_newctx(void *provctx) 84 1.1 christos { 85 1.1 christos PROV_RSA_CTX *prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX)); 86 1.1 christos 87 1.1 christos if (prsactx == NULL) 88 1.1 christos return NULL; 89 1.1 christos prsactx->libctx = PROV_LIBCTX_OF(provctx); 90 1.1 christos prsactx->op = KEM_OP_UNDEFINED; 91 1.1 christos 92 1.1 christos return prsactx; 93 1.1 christos } 94 1.1 christos 95 1.1 christos static void rsakem_freectx(void *vprsactx) 96 1.1 christos { 97 1.1 christos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; 98 1.1 christos 99 1.1 christos RSA_free(prsactx->rsa); 100 1.1 christos OPENSSL_free(prsactx); 101 1.1 christos } 102 1.1 christos 103 1.1 christos static void *rsakem_dupctx(void *vprsactx) 104 1.1 christos { 105 1.1 christos PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx; 106 1.1 christos PROV_RSA_CTX *dstctx; 107 1.1 christos 108 1.1 christos dstctx = OPENSSL_zalloc(sizeof(*srcctx)); 109 1.1 christos if (dstctx == NULL) 110 1.1 christos return NULL; 111 1.1 christos 112 1.1 christos *dstctx = *srcctx; 113 1.1 christos if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) { 114 1.1 christos OPENSSL_free(dstctx); 115 1.1 christos return NULL; 116 1.1 christos } 117 1.1 christos return dstctx; 118 1.1 christos } 119 1.1 christos 120 1.1 christos static int rsakem_init(void *vprsactx, void *vrsa, 121 1.1 christos const OSSL_PARAM params[], int operation) 122 1.1 christos { 123 1.1 christos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; 124 1.1 christos 125 1.1 christos if (prsactx == NULL || vrsa == NULL) 126 1.1 christos return 0; 127 1.1 christos 128 1.1 christos if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation)) 129 1.1 christos return 0; 130 1.1 christos 131 1.1 christos if (!RSA_up_ref(vrsa)) 132 1.1 christos return 0; 133 1.1 christos RSA_free(prsactx->rsa); 134 1.1 christos prsactx->rsa = vrsa; 135 1.1 christos 136 1.1 christos return rsakem_set_ctx_params(prsactx, params); 137 1.1 christos } 138 1.1 christos 139 1.1 christos static int rsakem_encapsulate_init(void *vprsactx, void *vrsa, 140 1.1 christos const OSSL_PARAM params[]) 141 1.1 christos { 142 1.1 christos return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCAPSULATE); 143 1.1 christos } 144 1.1 christos 145 1.1 christos static int rsakem_decapsulate_init(void *vprsactx, void *vrsa, 146 1.1 christos const OSSL_PARAM params[]) 147 1.1 christos { 148 1.1 christos return rsakem_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECAPSULATE); 149 1.1 christos } 150 1.1 christos 151 1.1 christos static int rsakem_get_ctx_params(void *vprsactx, OSSL_PARAM *params) 152 1.1 christos { 153 1.1 christos PROV_RSA_CTX *ctx = (PROV_RSA_CTX *)vprsactx; 154 1.1 christos 155 1.1 christos return ctx != NULL; 156 1.1 christos } 157 1.1 christos 158 1.1 christos static const OSSL_PARAM known_gettable_rsakem_ctx_params[] = { 159 1.1 christos OSSL_PARAM_END 160 1.1 christos }; 161 1.1 christos 162 1.1 christos static const OSSL_PARAM *rsakem_gettable_ctx_params(ossl_unused void *vprsactx, 163 1.1 christos ossl_unused void *provctx) 164 1.1 christos { 165 1.1 christos return known_gettable_rsakem_ctx_params; 166 1.1 christos } 167 1.1 christos 168 1.1 christos static int rsakem_set_ctx_params(void *vprsactx, const OSSL_PARAM params[]) 169 1.1 christos { 170 1.1 christos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; 171 1.1 christos const OSSL_PARAM *p; 172 1.1 christos int op; 173 1.1 christos 174 1.1 christos if (prsactx == NULL) 175 1.1 christos return 0; 176 1.1 christos if (params == NULL) 177 1.1 christos return 1; 178 1.1 christos 179 1.1 christos 180 1.1 christos p = OSSL_PARAM_locate_const(params, OSSL_KEM_PARAM_OPERATION); 181 1.1 christos if (p != NULL) { 182 1.1 christos if (p->data_type != OSSL_PARAM_UTF8_STRING) 183 1.1 christos return 0; 184 1.1 christos op = rsakem_opname2id(p->data); 185 1.1 christos if (op < 0) 186 1.1 christos return 0; 187 1.1 christos prsactx->op = op; 188 1.1 christos } 189 1.1 christos return 1; 190 1.1 christos } 191 1.1 christos 192 1.1 christos static const OSSL_PARAM known_settable_rsakem_ctx_params[] = { 193 1.1 christos OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0), 194 1.1 christos OSSL_PARAM_END 195 1.1 christos }; 196 1.1 christos 197 1.1 christos static const OSSL_PARAM *rsakem_settable_ctx_params(ossl_unused void *vprsactx, 198 1.1 christos ossl_unused void *provctx) 199 1.1 christos { 200 1.1 christos return known_settable_rsakem_ctx_params; 201 1.1 christos } 202 1.1 christos 203 1.1 christos /* 204 1.1 christos * NIST.SP.800-56Br2 205 1.1 christos * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE). 206 1.1 christos * 207 1.1 christos * Generate a random in the range 1 < z < (n 1) 208 1.1 christos */ 209 1.1 christos static int rsasve_gen_rand_bytes(RSA *rsa_pub, 210 1.1 christos unsigned char *out, int outlen) 211 1.1 christos { 212 1.1 christos int ret = 0; 213 1.1 christos BN_CTX *bnctx; 214 1.1 christos BIGNUM *z, *nminus3; 215 1.1 christos 216 1.1 christos bnctx = BN_CTX_secure_new_ex(ossl_rsa_get0_libctx(rsa_pub)); 217 1.1 christos if (bnctx == NULL) 218 1.1 christos return 0; 219 1.1 christos 220 1.1 christos /* 221 1.1 christos * Generate a random in the range 1 < z < (n 1). 222 1.1 christos * Since BN_priv_rand_range_ex() returns a value in range 0 <= r < max 223 1.1 christos * We can achieve this by adding 2.. but then we need to subtract 3 from 224 1.1 christos * the upper bound i.e: 2 + (0 <= r < (n - 3)) 225 1.1 christos */ 226 1.1 christos BN_CTX_start(bnctx); 227 1.1 christos nminus3 = BN_CTX_get(bnctx); 228 1.1 christos z = BN_CTX_get(bnctx); 229 1.1 christos ret = (z != NULL 230 1.1 christos && (BN_copy(nminus3, RSA_get0_n(rsa_pub)) != NULL) 231 1.1 christos && BN_sub_word(nminus3, 3) 232 1.1 christos && BN_priv_rand_range_ex(z, nminus3, 0, bnctx) 233 1.1 christos && BN_add_word(z, 2) 234 1.1 christos && (BN_bn2binpad(z, out, outlen) == outlen)); 235 1.1 christos BN_CTX_end(bnctx); 236 1.1 christos BN_CTX_free(bnctx); 237 1.1 christos return ret; 238 1.1 christos } 239 1.1 christos 240 1.1 christos /* 241 1.1 christos * NIST.SP.800-56Br2 242 1.1 christos * 7.2.1.2 RSASVE Generate Operation (RSASVE.GENERATE). 243 1.1 christos */ 244 1.1 christos static int rsasve_generate(PROV_RSA_CTX *prsactx, 245 1.1 christos unsigned char *out, size_t *outlen, 246 1.1 christos unsigned char *secret, size_t *secretlen) 247 1.1 christos { 248 1.1 christos int ret; 249 1.1 christos size_t nlen; 250 1.1 christos 251 1.1 christos /* Step (1): nlen = Ceil(len(n)/8) */ 252 1.1 christos nlen = RSA_size(prsactx->rsa); 253 1.1 christos 254 1.1 christos if (out == NULL) { 255 1.1 christos if (nlen == 0) { 256 1.1 christos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); 257 1.1 christos return 0; 258 1.1 christos } 259 1.1 christos if (outlen == NULL && secretlen == NULL) 260 1.1 christos return 0; 261 1.1 christos if (outlen != NULL) 262 1.1 christos *outlen = nlen; 263 1.1 christos if (secretlen != NULL) 264 1.1 christos *secretlen = nlen; 265 1.1 christos return 1; 266 1.1 christos } 267 1.3 christos 268 1.3 christos /* 269 1.3 christos * If outlen is specified, then it must report the length 270 1.3 christos * of the out buffer on input so that we can confirm 271 1.3 christos * its size is sufficent for encapsulation 272 1.3 christos */ 273 1.3 christos if (outlen != NULL && *outlen < nlen) { 274 1.3 christos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); 275 1.3 christos return 0; 276 1.3 christos } 277 1.3 christos 278 1.1 christos /* 279 1.1 christos * Step (2): Generate a random byte string z of nlen bytes where 280 1.1 christos * 1 < z < n - 1 281 1.1 christos */ 282 1.1 christos if (!rsasve_gen_rand_bytes(prsactx->rsa, secret, nlen)) 283 1.1 christos return 0; 284 1.1 christos 285 1.1 christos /* Step(3): out = RSAEP((n,e), z) */ 286 1.1 christos ret = RSA_public_encrypt(nlen, secret, out, prsactx->rsa, RSA_NO_PADDING); 287 1.1 christos if (ret) { 288 1.1 christos ret = 1; 289 1.1 christos if (outlen != NULL) 290 1.1 christos *outlen = nlen; 291 1.1 christos if (secretlen != NULL) 292 1.1 christos *secretlen = nlen; 293 1.1 christos } else { 294 1.1 christos OPENSSL_cleanse(secret, nlen); 295 1.1 christos } 296 1.1 christos return ret; 297 1.1 christos } 298 1.1 christos 299 1.3 christos /** 300 1.3 christos * rsasve_recover - Recovers a secret value from ciphertext using an RSA 301 1.3 christos * private key. Once, recovered, the secret value is considered to be a 302 1.3 christos * shared secret. Algorithm is preformed as per 303 1.3 christos * NIST SP 800-56B Rev 2 304 1.1 christos * 7.2.1.3 RSASVE Recovery Operation (RSASVE.RECOVER). 305 1.3 christos * 306 1.3 christos * This function performs RSA decryption using the private key from the 307 1.3 christos * provided RSA context (`prsactx`). It takes the input ciphertext, decrypts 308 1.3 christos * it, and writes the decrypted message to the output buffer. 309 1.3 christos * 310 1.3 christos * @prsactx: The RSA context containing the private key. 311 1.3 christos * @out: The output buffer to store the decrypted message. 312 1.3 christos * @outlen: On input, the size of the output buffer. On successful 313 1.3 christos * completion, the actual length of the decrypted message. 314 1.3 christos * @in: The input buffer containing the ciphertext to be decrypted. 315 1.3 christos * @inlen: The length of the input ciphertext in bytes. 316 1.3 christos * 317 1.3 christos * Returns 1 on success, or 0 on error. In case of error, appropriate 318 1.3 christos * error messages are raised using the ERR_raise function. 319 1.1 christos */ 320 1.1 christos static int rsasve_recover(PROV_RSA_CTX *prsactx, 321 1.1 christos unsigned char *out, size_t *outlen, 322 1.1 christos const unsigned char *in, size_t inlen) 323 1.1 christos { 324 1.1 christos size_t nlen; 325 1.3 christos int ret; 326 1.1 christos 327 1.1 christos /* Step (1): get the byte length of n */ 328 1.1 christos nlen = RSA_size(prsactx->rsa); 329 1.1 christos 330 1.1 christos if (out == NULL) { 331 1.1 christos if (nlen == 0) { 332 1.1 christos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); 333 1.1 christos return 0; 334 1.1 christos } 335 1.1 christos *outlen = nlen; 336 1.1 christos return 1; 337 1.1 christos } 338 1.1 christos 339 1.3 christos /* 340 1.3 christos * Step (2): check the input ciphertext 'inlen' matches the nlen 341 1.3 christos * and that outlen is at least nlen bytes 342 1.3 christos */ 343 1.1 christos if (inlen != nlen) { 344 1.1 christos ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); 345 1.1 christos return 0; 346 1.1 christos } 347 1.3 christos 348 1.3 christos /* 349 1.3 christos * If outlen is specified, then it must report the length 350 1.3 christos * of the out buffer, so that we can confirm that it is of 351 1.3 christos * sufficient size to hold the output of decapsulation 352 1.3 christos */ 353 1.3 christos if (outlen != NULL && *outlen < nlen) { 354 1.3 christos ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); 355 1.3 christos return 0; 356 1.3 christos } 357 1.3 christos 358 1.1 christos /* Step (3): out = RSADP((n,d), in) */ 359 1.3 christos ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa, RSA_NO_PADDING); 360 1.3 christos if (ret > 0 && outlen != NULL) 361 1.3 christos *outlen = ret; 362 1.3 christos return ret > 0; 363 1.1 christos } 364 1.1 christos 365 1.1 christos static int rsakem_generate(void *vprsactx, unsigned char *out, size_t *outlen, 366 1.1 christos unsigned char *secret, size_t *secretlen) 367 1.1 christos { 368 1.1 christos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; 369 1.1 christos 370 1.1 christos switch (prsactx->op) { 371 1.1 christos case KEM_OP_RSASVE: 372 1.1 christos return rsasve_generate(prsactx, out, outlen, secret, secretlen); 373 1.1 christos default: 374 1.1 christos return -2; 375 1.1 christos } 376 1.1 christos } 377 1.1 christos 378 1.1 christos static int rsakem_recover(void *vprsactx, unsigned char *out, size_t *outlen, 379 1.1 christos const unsigned char *in, size_t inlen) 380 1.1 christos { 381 1.1 christos PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx; 382 1.1 christos 383 1.1 christos switch (prsactx->op) { 384 1.1 christos case KEM_OP_RSASVE: 385 1.1 christos return rsasve_recover(prsactx, out, outlen, in, inlen); 386 1.1 christos default: 387 1.1 christos return -2; 388 1.1 christos } 389 1.1 christos } 390 1.1 christos 391 1.1 christos const OSSL_DISPATCH ossl_rsa_asym_kem_functions[] = { 392 1.1 christos { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))rsakem_newctx }, 393 1.1 christos { OSSL_FUNC_KEM_ENCAPSULATE_INIT, 394 1.1 christos (void (*)(void))rsakem_encapsulate_init }, 395 1.1 christos { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))rsakem_generate }, 396 1.1 christos { OSSL_FUNC_KEM_DECAPSULATE_INIT, 397 1.1 christos (void (*)(void))rsakem_decapsulate_init }, 398 1.1 christos { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))rsakem_recover }, 399 1.1 christos { OSSL_FUNC_KEM_FREECTX, (void (*)(void))rsakem_freectx }, 400 1.1 christos { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))rsakem_dupctx }, 401 1.1 christos { OSSL_FUNC_KEM_GET_CTX_PARAMS, 402 1.1 christos (void (*)(void))rsakem_get_ctx_params }, 403 1.1 christos { OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS, 404 1.1 christos (void (*)(void))rsakem_gettable_ctx_params }, 405 1.1 christos { OSSL_FUNC_KEM_SET_CTX_PARAMS, 406 1.1 christos (void (*)(void))rsakem_set_ctx_params }, 407 1.1 christos { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, 408 1.1 christos (void (*)(void))rsakem_settable_ctx_params }, 409 1.1 christos { 0, NULL } 410 1.1 christos }; 411