Latest update.

This commit is contained in:
2020-04-15 18:45:34 +09:00
parent be29e7bcc6
commit 2015c00c43
573 changed files with 22943 additions and 6714 deletions
+3 -2
View File
@@ -1,8 +1,9 @@
LIBS=../../libcrypto
$COMMON=rsa_ossl.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_aid.c rsa_pk1.c \
$COMMON=rsa_ossl.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_pk1.c \
rsa_none.c rsa_oaep.c rsa_chk.c rsa_pss.c rsa_x931.c rsa_crpt.c \
rsa_x931g.c rsa_sp800_56b_gen.c rsa_sp800_56b_check.c
rsa_x931g.c rsa_sp800_56b_gen.c rsa_sp800_56b_check.c rsa_backend.c \
rsa_mp_names.c
SOURCE[../../libcrypto]=$COMMON\
rsa_saos.c rsa_err.c rsa_asn1.c rsa_depr.c rsa_ameth.c rsa_prn.c \
-98
View File
@@ -1,98 +0,0 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdlib.h>
#include <openssl/objects.h>
#include "crypto/rsa.h"
#define ASN1_SEQUENCE 0x30
#define ASN1_OID 0x06
/*
* -- RFC 2313
* pkcs-1 OBJECT IDENTIFIER ::= {
* iso(1) member-body(2) US(840) rsadsi(113549) pkcs(1) 1
* }
*/
/*
* -- RFC 3279
* md2WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 2 }
* md5WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 4 }
* sha1WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 5 }
*/
#define ENCODE_ALGORITHMIDENTIFIER_PKCS1(name, n) \
static const unsigned char algorithmidentifier_##name##_der[] = { \
ASN1_SEQUENCE, 0x0b, \
ASN1_OID, 0x09, 1 * 40 + 2, 134, 72, 134, 247, 13, 1, 1, n \
}
#ifndef FIPS_MODE
ENCODE_ALGORITHMIDENTIFIER_PKCS1(md2, 2);
ENCODE_ALGORITHMIDENTIFIER_PKCS1(md5, 4);
#endif
ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha1, 5);
/*
* -- RFC 4055
* sha224WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 14 }
* sha256WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 11 }
* sha384WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 12 }
* sha512WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 13 }
*/
ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha224, 14);
ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha256, 11);
ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha384, 12);
ENCODE_ALGORITHMIDENTIFIER_PKCS1(sha512, 13);
/*
* -- https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration
*
* sigAlgs OBJECT IDENTIFIER ::= { 2 16 840 1 101 3 4 3 }
*
* id-rsassa-pkcs1-v1_5-with-sha3-224 ::= { sigAlgs 13 }
* id-rsassa-pkcs1-v1_5-with-sha3-256 ::= { sigAlgs 14 }
* id-rsassa-pkcs1-v1_5-with-sha3-384 ::= { sigAlgs 15 }
* id-rsassa-pkcs1-v1_5-with-sha3-512 ::= { sigAlgs 16 }
*/
#define ENCODE_ALGORITHMIDENTIFIER_SIGALGS(name, n) \
static const unsigned char algorithmidentifier_##name##_der[] = { \
ASN1_SEQUENCE, 0x0c, \
ASN1_OID, 0x0a, 1 * 40 + 2, 16, 134, 72, 1, 101, 3, 4, 3, n \
}
ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_224, 13);
ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_256, 14);
ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_384, 15);
ENCODE_ALGORITHMIDENTIFIER_SIGALGS(sha3_512, 16);
#define MD_CASE(name) \
case NID_##name: \
*len = sizeof(algorithmidentifier_##name##_der); \
return algorithmidentifier_##name##_der
const unsigned char *rsa_algorithmidentifier_encoding(int md_nid, size_t *len)
{
switch (md_nid) {
#ifndef FIPS_MODE
MD_CASE(md2);
MD_CASE(md5);
#endif
MD_CASE(sha1);
MD_CASE(sha224);
MD_CASE(sha256);
MD_CASE(sha384);
MD_CASE(sha512);
MD_CASE(sha3_224);
MD_CASE(sha3_256);
MD_CASE(sha3_384);
MD_CASE(sha3_512);
default:
return NULL;
}
}
+51 -29
View File
@@ -14,7 +14,7 @@
#include <openssl/bn.h>
#include <openssl/cms.h>
#include <openssl/core_names.h>
#include "internal/param_build.h"
#include <openssl/param_build.h>
#include "crypto/asn1.h"
#include "crypto/evp.h"
#include "crypto/rsa.h"
@@ -1075,28 +1075,38 @@ static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
EVP_KEYMGMT *to_keymgmt)
EVP_KEYMGMT *to_keymgmt, OPENSSL_CTX *libctx,
const char *propq)
{
RSA *rsa = from->pkey.rsa;
OSSL_PARAM_BLD tmpl;
OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
const BIGNUM *n = RSA_get0_n(rsa), *e = RSA_get0_e(rsa);
const BIGNUM *d = RSA_get0_d(rsa);
STACK_OF(BIGNUM_const) *primes = NULL, *exps = NULL, *coeffs = NULL;
int numprimes = 0, numexps = 0, numcoeffs = 0;
OSSL_PARAM *params = NULL;
int selection = 0;
int rv = 0;
if (tmpl == NULL)
return 0;
/*
* If the RSA method is foreign, then we can't be sure of anything, and
* can therefore not export or pretend to export.
*/
if (RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
goto err;
/* Public parameters must always be present */
if (n == NULL || e == NULL)
goto err;
ossl_param_bld_init(&tmpl);
/* |e| and |n| are always present */
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_E, e))
if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, e))
goto err;
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_N, n))
if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, n))
goto err;
selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
if (d != NULL) {
int i;
@@ -1123,55 +1133,65 @@ static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
&& (numprimes < 2 || numexps < 2 || numcoeffs < 1))
goto err;
/* assert that an OSSL_PARAM_BLD has enough space. */
if (!ossl_assert(/* n, e */ 2 + /* d */ 1 + /* numprimes */ 1
+ numprimes + numexps + numcoeffs
<= OSSL_PARAM_BLD_MAX))
if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, d))
goto err;
selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_D, d))
goto err;
for (i = 0; i < numprimes; i++) {
for (i = 0; i < numprimes && rsa_mp_factor_names[i] != NULL; i++) {
const BIGNUM *num = sk_BIGNUM_const_value(primes, i);
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_FACTOR,
num))
if (!OSSL_PARAM_BLD_push_BN(tmpl, rsa_mp_factor_names[i], num))
goto err;
}
for (i = 0; i < numexps; i++) {
for (i = 0; i < numexps && rsa_mp_exp_names[i] != NULL; i++) {
const BIGNUM *num = sk_BIGNUM_const_value(exps, i);
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT,
num))
if (!OSSL_PARAM_BLD_push_BN(tmpl, rsa_mp_exp_names[i], num))
goto err;
}
for (i = 0; i < numcoeffs; i++) {
for (i = 0; i < numcoeffs && rsa_mp_coeff_names[i] != NULL; i++) {
const BIGNUM *num = sk_BIGNUM_const_value(coeffs, i);
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT,
num))
if (!OSSL_PARAM_BLD_push_BN(tmpl, rsa_mp_coeff_names[i], num))
goto err;
}
}
if ((params = ossl_param_bld_to_param(&tmpl)) == NULL)
if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
goto err;
/* We export, the provider imports */
rv = evp_keymgmt_import(to_keymgmt, to_keydata, OSSL_KEYMGMT_SELECT_ALL,
params);
rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
err:
sk_BIGNUM_const_free(primes);
sk_BIGNUM_const_free(exps);
sk_BIGNUM_const_free(coeffs);
ossl_param_bld_free(params);
OSSL_PARAM_BLD_free_params(params);
OSSL_PARAM_BLD_free(tmpl);
return rv;
}
static int rsa_pkey_import_from(const OSSL_PARAM params[], void *key)
{
EVP_PKEY *pkey = key;
RSA *rsa = RSA_new();
if (rsa == NULL) {
ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
return 0;
}
if (!rsa_fromdata(rsa, params)
|| !EVP_PKEY_assign_RSA(pkey, rsa)) {
RSA_free(rsa);
return 0;
}
return 1;
}
const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
{
EVP_PKEY_RSA,
@@ -1210,7 +1230,8 @@ const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
0, 0, 0, 0,
rsa_pkey_dirty_cnt,
rsa_pkey_export_to
rsa_pkey_export_to,
rsa_pkey_import_from
},
{
@@ -1255,5 +1276,6 @@ const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
0, 0, 0, 0,
rsa_pkey_dirty_cnt,
rsa_pkey_export_to
rsa_pkey_export_to,
rsa_pkey_import_from
};
+99
View File
@@ -0,0 +1,99 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <openssl/core_names.h>
#include <openssl/params.h>
#include "crypto/rsa.h"
/*
* The intention with the "backend" source file is to offer backend support
* for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
* implementations alike.
*/
DEFINE_STACK_OF(BIGNUM)
static int collect_numbers(STACK_OF(BIGNUM) *numbers,
const OSSL_PARAM params[], const char *names[])
{
const OSSL_PARAM *p = NULL;
int i;
if (numbers == NULL)
return 0;
for (i = 0; names[i] != NULL; i++){
p = OSSL_PARAM_locate_const(params, names[i]);
if (p != NULL) {
BIGNUM *tmp = NULL;
if (!OSSL_PARAM_get_BN(p, &tmp)
|| sk_BIGNUM_push(numbers, tmp) == 0)
return 0;
}
}
return 1;
}
int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
{
const OSSL_PARAM *param_n, *param_e, *param_d;
BIGNUM *n = NULL, *e = NULL, *d = NULL;
STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
int is_private = 0;
if (rsa == NULL)
return 0;
param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
|| (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
|| (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
goto err;
is_private = (d != NULL);
if (!RSA_set0_key(rsa, n, e, d))
goto err;
n = e = d = NULL;
if (is_private) {
if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
rsa_mp_factor_names)
|| !collect_numbers(exps = sk_BIGNUM_new_null(), params,
rsa_mp_exp_names)
|| !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
rsa_mp_coeff_names))
goto err;
/* It's ok if this private key just has n, e and d */
if (sk_BIGNUM_num(factors) != 0
&& !rsa_set0_all_params(rsa, factors, exps, coeffs))
goto err;
}
sk_BIGNUM_free(factors);
sk_BIGNUM_free(exps);
sk_BIGNUM_free(coeffs);
return 1;
err:
BN_free(n);
BN_free(e);
BN_free(d);
sk_BIGNUM_pop_free(factors, BN_free);
sk_BIGNUM_pop_free(exps, BN_free);
sk_BIGNUM_pop_free(coeffs, BN_free);
return 0;
}
+95 -3
View File
@@ -14,6 +14,7 @@
#include <openssl/evp.h>
#include "internal/cryptlib.h"
#include "internal/refcount.h"
#include "openssl/param_build.h"
#include "crypto/bn.h"
#include "crypto/evp.h"
#include "crypto/rsa.h"
@@ -628,13 +629,10 @@ const BIGNUM *RSA_get0_iqmp(const RSA *r)
return r->iqmp;
}
/* TODO(3.0): Temporary until we move PSS support into the FIPS module */
#ifndef FIPS_MODE
const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
{
return r->pss;
}
#endif
void RSA_clear_flags(RSA *r, int flags)
{
@@ -1260,4 +1258,98 @@ int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
return 1;
}
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
{
OSSL_PARAM params[2], *p = params;
size_t bits2 = bits;
if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.keymgmt.genctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_RSA_KEYGEN_BITS, bits, NULL);
*p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_set_params(ctx, params))
return 0;
return 1;
}
int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
{
OSSL_PARAM_BLD *tmpl;
OSSL_PARAM *params;
int ret;
if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.keymgmt.genctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
if ((tmpl = OSSL_PARAM_BLD_new()) == NULL)
return 0;
if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, pubexp)
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
OSSL_PARAM_BLD_free(tmpl);
return 0;
}
OSSL_PARAM_BLD_free(tmpl);
ret = EVP_PKEY_CTX_set_params(ctx, params);
OSSL_PARAM_BLD_free_params(params);
return ret;
}
int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
{
OSSL_PARAM params[2], *p = params;
size_t primes2 = primes;
if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.keymgmt.genctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, primes,
NULL);
*p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_set_params(ctx, params))
return 0;
return 1;
}
#endif
+25 -9
View File
@@ -29,13 +29,15 @@ DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
DEFINE_STACK_OF(RSA_PRIME_INFO)
struct rsa_st {
OPENSSL_CTX *libctx;
/*
* The first parameter is used to pickup errors where this is passed
* instead of an EVP_PKEY, it is set to 0
* #legacy
* The first field is used to pickup errors where this is passed
* instead of an EVP_PKEY. It is always zero.
* THIS MUST REMAIN THE FIRST FIELD.
*/
int pad;
int dummy_zero;
OPENSSL_CTX *libctx;
int32_t version;
const RSA_METHOD *meth;
/* functional reference if 'meth' is ENGINE-provided */
@@ -48,13 +50,12 @@ struct rsa_st {
BIGNUM *dmp1;
BIGNUM *dmq1;
BIGNUM *iqmp;
/* TODO(3.0): Support PSS in FIPS_MODE */
/* If a PSS only key this contains the parameter restrictions */
RSA_PSS_PARAMS *pss;
#ifndef FIPS_MODE
/* for multi-prime RSA, defined in RFC 8017 */
STACK_OF(RSA_PRIME_INFO) *prime_infos;
/* If a PSS only key this contains the parameter restrictions */
RSA_PSS_PARAMS *pss;
/* be careful using this if the RSA structure is shared */
/* Be careful using this if the RSA structure is shared */
CRYPTO_EX_DATA ex_data;
#endif
CRYPTO_REF_COUNT references;
@@ -168,4 +169,19 @@ int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,
const BIGNUM *Xq2, int nbits,
const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
int rsa_padding_add_SSLv23_with_libctx(OPENSSL_CTX *libctx, unsigned char *to,
int tlen, const unsigned char *from,
int flen);
int rsa_padding_add_PKCS1_type_2_with_libctx(OPENSSL_CTX *libctx,
unsigned char *to, int tlen,
const unsigned char *from,
int flen);
int rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(OPENSSL_CTX *libctx,
unsigned char *to, int tlen,
const unsigned char *from,
int flen,
const unsigned char *param,
int plen, const EVP_MD *md,
const EVP_MD *mgf1md);
#endif /* OSSL_CRYPTO_RSA_LOCAL_H */
+76
View File
@@ -0,0 +1,76 @@
/*
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <openssl/core_names.h>
#include "crypto/rsa.h"
/*
* The following tables are constants used during RSA parameter building
* operations. It is easier to point to one of these fixed strings than have
* to dynamically add and generate the names on the fly.
*/
/*
* A fixed table of names for the RSA prime factors starting with
* P,Q and up to 8 additional primes.
*/
const char *rsa_mp_factor_names[] = {
OSSL_PKEY_PARAM_RSA_FACTOR1,
OSSL_PKEY_PARAM_RSA_FACTOR2,
#ifndef FIPS_MODE
OSSL_PKEY_PARAM_RSA_FACTOR3,
OSSL_PKEY_PARAM_RSA_FACTOR4,
OSSL_PKEY_PARAM_RSA_FACTOR5,
OSSL_PKEY_PARAM_RSA_FACTOR6,
OSSL_PKEY_PARAM_RSA_FACTOR7,
OSSL_PKEY_PARAM_RSA_FACTOR8,
OSSL_PKEY_PARAM_RSA_FACTOR9,
OSSL_PKEY_PARAM_RSA_FACTOR10,
#endif
NULL
};
/*
* A fixed table of names for the RSA exponents starting with
* DP,DQ and up to 8 additional exponents.
*/
const char *rsa_mp_exp_names[] = {
OSSL_PKEY_PARAM_RSA_EXPONENT1,
OSSL_PKEY_PARAM_RSA_EXPONENT2,
#ifndef FIPS_MODE
OSSL_PKEY_PARAM_RSA_EXPONENT3,
OSSL_PKEY_PARAM_RSA_EXPONENT4,
OSSL_PKEY_PARAM_RSA_EXPONENT5,
OSSL_PKEY_PARAM_RSA_EXPONENT6,
OSSL_PKEY_PARAM_RSA_EXPONENT7,
OSSL_PKEY_PARAM_RSA_EXPONENT8,
OSSL_PKEY_PARAM_RSA_EXPONENT9,
OSSL_PKEY_PARAM_RSA_EXPONENT10,
#endif
NULL
};
/*
* A fixed table of names for the RSA coefficients starting with
* QINV and up to 8 additional exponents.
*/
const char *rsa_mp_coeff_names[] = {
OSSL_PKEY_PARAM_RSA_COEFFICIENT1,
OSSL_PKEY_PARAM_RSA_COEFFICIENT2,
#ifndef FIPS_MODE
OSSL_PKEY_PARAM_RSA_COEFFICIENT3,
OSSL_PKEY_PARAM_RSA_COEFFICIENT4,
OSSL_PKEY_PARAM_RSA_COEFFICIENT5,
OSSL_PKEY_PARAM_RSA_COEFFICIENT6,
OSSL_PKEY_PARAM_RSA_COEFFICIENT7,
OSSL_PKEY_PARAM_RSA_COEFFICIENT8,
OSSL_PKEY_PARAM_RSA_COEFFICIENT9,
#endif
NULL
};
+25 -14
View File
@@ -34,8 +34,9 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
const unsigned char *from, int flen,
const unsigned char *param, int plen)
{
return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
param, plen, NULL, NULL);
return rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(NULL, to, tlen, from,
flen, param, plen, NULL,
NULL);
}
/*
@@ -45,10 +46,13 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
* Step numbers are included here but not in the constant time inverse below
* to avoid complicating an already difficult enough function.
*/
int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
const unsigned char *from, int flen,
const unsigned char *param, int plen,
const EVP_MD *md, const EVP_MD *mgf1md)
int rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(OPENSSL_CTX *libctx,
unsigned char *to, int tlen,
const unsigned char *from,
int flen,
const unsigned char *param,
int plen, const EVP_MD *md,
const EVP_MD *mgf1md)
{
int rv = 0;
int i, emlen = tlen - 1;
@@ -61,8 +65,7 @@ int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
if (md == NULL)
md = EVP_sha1();
#else
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
ERR_R_PASSED_NULL_PARAMETER);
RSAerr(0, ERR_R_PASSED_NULL_PARAMETER);
return 0;
#endif
if (mgf1md == NULL)
@@ -72,14 +75,12 @@ int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
/* step 2b: check KLen > nLen - 2 HLen - 2 */
if (flen > emlen - 2 * mdlen - 1) {
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
RSAerr(0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
return 0;
}
if (emlen < 2 * mdlen + 1) {
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
RSA_R_KEY_SIZE_TOO_SMALL);
RSAerr(0, RSA_R_KEY_SIZE_TOO_SMALL);
return 0;
}
@@ -97,13 +98,13 @@ int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
db[emlen - flen - mdlen - 1] = 0x01;
memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
/* step 3d: generate random byte string */
if (RAND_bytes(seed, mdlen) <= 0)
if (RAND_bytes_ex(libctx, seed, mdlen) <= 0)
goto err;
dbmask_len = emlen - mdlen;
dbmask = OPENSSL_malloc(dbmask_len);
if (dbmask == NULL) {
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
RSAerr(0, ERR_R_MALLOC_FAILURE);
goto err;
}
@@ -128,6 +129,16 @@ int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
return rv;
}
int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
const unsigned char *from, int flen,
const unsigned char *param, int plen,
const EVP_MD *md, const EVP_MD *mgf1md)
{
return rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(NULL, to, tlen, from,
flen, param, plen, md,
mgf1md);
}
int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
const unsigned char *from, int flen, int num,
const unsigned char *param, int plen)
+7 -3
View File
@@ -105,14 +105,18 @@ static int rsa_ossl_public_encrypt(int flen, const unsigned char *from,
switch (padding) {
case RSA_PKCS1_PADDING:
i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
i = rsa_padding_add_PKCS1_type_2_with_libctx(rsa->libctx, buf, num,
from, flen);
break;
case RSA_PKCS1_OAEP_PADDING:
i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
i = rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx(rsa->libctx, buf, num,
from, flen, NULL, 0,
NULL, NULL);
break;
#ifndef FIPS_MODE
case RSA_SSLV23_PADDING:
i = RSA_padding_add_SSLv23(buf, num, from, flen);
i = rsa_padding_add_SSLv23_with_libctx(rsa->libctx, buf, num, from,
flen);
break;
#endif
case RSA_NO_PADDING:
+20 -11
View File
@@ -17,6 +17,7 @@
#include <openssl/ssl.h>
#include "internal/cryptlib.h"
#include "crypto/rsa.h"
#include "rsa_local.h"
int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
const unsigned char *from, int flen)
@@ -117,15 +118,16 @@ int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
return j;
}
int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
const unsigned char *from, int flen)
int rsa_padding_add_PKCS1_type_2_with_libctx(OPENSSL_CTX *libctx,
unsigned char *to, int tlen,
const unsigned char *from,
int flen)
{
int i, j;
unsigned char *p;
if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
RSAerr(0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
return 0;
}
@@ -137,12 +139,12 @@ int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
/* pad out with non-zero random data */
j = tlen - 3 - flen;
if (RAND_bytes(p, j) <= 0)
if (RAND_bytes_ex(libctx, p, j) <= 0)
return 0;
for (i = 0; i < j; i++) {
if (*p == '\0')
do {
if (RAND_bytes(p, 1) <= 0)
if (RAND_bytes_ex(libctx, p, 1) <= 0)
return 0;
} while (*p == '\0');
p++;
@@ -154,6 +156,12 @@ int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
return 1;
}
int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
const unsigned char *from, int flen)
{
return rsa_padding_add_PKCS1_type_2_with_libctx(NULL, to, tlen, from, flen);
}
int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
const unsigned char *from, int flen,
int num)
@@ -285,9 +293,10 @@ int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
* decrypted data will be randomly generated (as per
* https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
*/
int rsa_padding_check_PKCS1_type_2_TLS(unsigned char *to, size_t tlen,
const unsigned char *from, size_t flen,
int client_version, int alt_version)
int rsa_padding_check_PKCS1_type_2_TLS(OPENSSL_CTX *libctx, unsigned char *to,
size_t tlen, const unsigned char *from,
size_t flen, int client_version,
int alt_version)
{
unsigned int i, good, version_good;
unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
@@ -306,8 +315,8 @@ int rsa_padding_check_PKCS1_type_2_TLS(unsigned char *to, size_t tlen,
* Generate a random premaster secret to use in the event that we fail
* to decrypt.
*/
if (RAND_priv_bytes(rand_premaster_secret,
sizeof(rand_premaster_secret)) <= 0) {
if (RAND_priv_bytes_ex(libctx, rand_premaster_secret,
sizeof(rand_premaster_secret)) <= 0) {
ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
return -1;
}
+1 -1
View File
@@ -200,7 +200,7 @@ int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
ERR_R_MALLOC_FAILURE);
goto err;
}
if (RAND_bytes(salt, sLen) <= 0)
if (RAND_bytes_ex(rsa->libctx, salt, sLen) <= 0)
goto err;
}
maskedDBLen = emLen - hLen - 1;
+48 -7
View File
@@ -180,6 +180,47 @@ const unsigned char *rsa_digestinfo_encoding(int md_nid, size_t *len)
}
}
#define MD_NID_CASE(name, sz) \
case NID_##name: \
return sz;
static int digest_sz_from_nid(int nid)
{
switch (nid) {
#ifndef FIPS_MODE
# ifndef OPENSSL_NO_MDC2
MD_NID_CASE(mdc2, MDC2_DIGEST_LENGTH)
# endif
# ifndef OPENSSL_NO_MD2
MD_NID_CASE(md2, MD2_DIGEST_LENGTH)
# endif
# ifndef OPENSSL_NO_MD4
MD_NID_CASE(md4, MD4_DIGEST_LENGTH)
# endif
# ifndef OPENSSL_NO_MD5
MD_NID_CASE(md5, MD5_DIGEST_LENGTH)
# endif
# ifndef OPENSSL_NO_RMD160
MD_NID_CASE(ripemd160, RIPEMD160_DIGEST_LENGTH)
# endif
#endif /* FIPS_MODE */
MD_NID_CASE(sha1, SHA_DIGEST_LENGTH)
MD_NID_CASE(sha224, SHA224_DIGEST_LENGTH)
MD_NID_CASE(sha256, SHA256_DIGEST_LENGTH)
MD_NID_CASE(sha384, SHA384_DIGEST_LENGTH)
MD_NID_CASE(sha512, SHA512_DIGEST_LENGTH)
MD_NID_CASE(sha512_224, SHA224_DIGEST_LENGTH)
MD_NID_CASE(sha512_256, SHA256_DIGEST_LENGTH)
MD_NID_CASE(sha3_224, SHA224_DIGEST_LENGTH)
MD_NID_CASE(sha3_256, SHA256_DIGEST_LENGTH)
MD_NID_CASE(sha3_384, SHA384_DIGEST_LENGTH)
MD_NID_CASE(sha3_512, SHA512_DIGEST_LENGTH)
default:
return 0;
}
}
/* Size of an SSL signature: MD5+SHA1 */
#define SSL_SIG_LENGTH 36
@@ -231,8 +272,10 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
unsigned char *tmps = NULL;
const unsigned char *encoded = NULL;
#ifndef FIPS_MODE
if (rsa->meth->rsa_sign != NULL)
return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
#endif /* FIPS_MODE */
/* Compute the encoded digest. */
if (type == NID_md5_sha1) {
@@ -305,6 +348,7 @@ int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
goto err;
decrypt_len = len;
#ifndef FIPS_MODE
if (type == NID_md5_sha1) {
/*
* NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
@@ -350,20 +394,17 @@ int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
goto err;
}
}
} else {
} else
#endif /* FIPS_MODE */
{
/*
* If recovering the digest, extract a digest-sized output from the end
* of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
* output as in a standard verification.
*/
if (rm != NULL) {
const EVP_MD *md = EVP_get_digestbynid(type);
if (md == NULL) {
RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_UNKNOWN_ALGORITHM_TYPE);
goto err;
}
len = digest_sz_from_nid(type);
len = EVP_MD_size(md);
if (len <= 0)
goto err;
m_len = (unsigned int)len;
+13 -6
View File
@@ -13,16 +13,17 @@
#include <openssl/rsa.h>
#include <openssl/rand.h>
#include "internal/constant_time.h"
#include "rsa_local.h"
int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
const unsigned char *from, int flen)
int rsa_padding_add_SSLv23_with_libctx(OPENSSL_CTX *libctx, unsigned char *to,
int tlen, const unsigned char *from,
int flen)
{
int i, j;
unsigned char *p;
if (flen > (tlen - RSA_PKCS1_PADDING_SIZE)) {
RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
RSAerr(0, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
return 0;
}
@@ -34,12 +35,12 @@ int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
/* pad out with non-zero random data */
j = tlen - 3 - 8 - flen;
if (RAND_bytes(p, j) <= 0)
if (RAND_bytes_ex(libctx, p, j) <= 0)
return 0;
for (i = 0; i < j; i++) {
if (*p == '\0')
do {
if (RAND_bytes(p, 1) <= 0)
if (RAND_bytes_ex(libctx, p, 1) <= 0)
return 0;
} while (*p == '\0');
p++;
@@ -53,6 +54,12 @@ int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
return 1;
}
int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
const unsigned char *from, int flen)
{
return rsa_padding_add_SSLv23_with_libctx(NULL, to, tlen, from, flen);
}
/*
* Copy of RSA_padding_check_PKCS1_type_2 with a twist that rejects padding
* if nul delimiter is not preceded by 8 consecutive 0x03 bytes. It also