diff options
author | Miod Vallat <miod@cvs.openbsd.org> | 2014-07-09 08:20:09 +0000 |
---|---|---|
committer | Miod Vallat <miod@cvs.openbsd.org> | 2014-07-09 08:20:09 +0000 |
commit | a1fc322e45095c3c909044521a123834ff367cc1 (patch) | |
tree | 2661944e25cda628939c2f7a11febe63a606581f /lib | |
parent | aa4b893879492c359baa295e1bc5bdf817d4c50c (diff) |
KNF
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libcrypto/rsa/rsa_ameth.c | 462 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_asn1.c | 27 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_chk.c | 145 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_crpt.c | 151 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_depr.c | 31 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_eay.c | 794 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_gen.c | 204 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_lib.c | 243 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_none.c | 59 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_null.c | 87 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_oaep.c | 155 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_pk1.c | 241 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_pmeth.c | 426 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_prn.c | 33 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_pss.c | 190 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_saos.c | 128 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_sign.c | 291 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_ssl.c | 137 | ||||
-rw-r--r-- | lib/libcrypto/rsa/rsa_x931.c | 122 |
19 files changed, 1959 insertions, 1967 deletions
diff --git a/lib/libcrypto/rsa/rsa_ameth.c b/lib/libcrypto/rsa/rsa_ameth.c index 2e2194e8b36..8e15e3f5358 100644 --- a/lib/libcrypto/rsa/rsa_ameth.c +++ b/lib/libcrypto/rsa/rsa_ameth.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_ameth.c,v 1.6 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_ameth.c,v 1.7 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ @@ -67,207 +67,213 @@ #endif #include "asn1_locl.h" -static int rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) - { +static int +rsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) +{ unsigned char *penc = NULL; int penclen; + penclen = i2d_RSAPublicKey(pkey->pkey.rsa, &penc); if (penclen <= 0) return 0; if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_RSA), - V_ASN1_NULL, NULL, penc, penclen)) + V_ASN1_NULL, NULL, penc, penclen)) return 1; free(penc); return 0; - } +} -static int rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) - { +static int +rsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) +{ const unsigned char *p; int pklen; RSA *rsa = NULL; + if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, NULL, pubkey)) return 0; - if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen))) - { + if (!(rsa = d2i_RSAPublicKey(NULL, &p, pklen))) { RSAerr(RSA_F_RSA_PUB_DECODE, ERR_R_RSA_LIB); return 0; - } + } EVP_PKEY_assign_RSA (pkey, rsa); return 1; - } +} -static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) - { - if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0 - || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0) - return 0; +static int +rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) +{ + if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0 || + BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0) + return 0; return 1; - } +} -static int old_rsa_priv_decode(EVP_PKEY *pkey, - const unsigned char **pder, int derlen) - { +static int +old_rsa_priv_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) +{ RSA *rsa; - if (!(rsa = d2i_RSAPrivateKey (NULL, pder, derlen))) - { + + if (!(rsa = d2i_RSAPrivateKey (NULL, pder, derlen))) { RSAerr(RSA_F_OLD_RSA_PRIV_DECODE, ERR_R_RSA_LIB); return 0; - } + } EVP_PKEY_assign_RSA(pkey, rsa); return 1; - } +} -static int old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) - { +static int +old_rsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) +{ return i2d_RSAPrivateKey(pkey->pkey.rsa, pder); - } +} -static int rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) - { +static int +rsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) +{ unsigned char *rk = NULL; int rklen; + rklen = i2d_RSAPrivateKey(pkey->pkey.rsa, &rk); - if (rklen <= 0) - { - RSAerr(RSA_F_RSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); + if (rklen <= 0) { + RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); return 0; - } + } if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_rsaEncryption), 0, - V_ASN1_NULL, NULL, rk, rklen)) - { - RSAerr(RSA_F_RSA_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); + V_ASN1_NULL, NULL, rk, rklen)) { + RSAerr(RSA_F_RSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); return 0; - } + } return 1; - } +} -static int rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) - { +static int +rsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) +{ const unsigned char *p; int pklen; + if (!PKCS8_pkey_get0(NULL, &p, &pklen, NULL, p8)) return 0; return old_rsa_priv_decode(pkey, &p, pklen); - } +} -static int int_rsa_size(const EVP_PKEY *pkey) - { +static int +int_rsa_size(const EVP_PKEY *pkey) +{ return RSA_size(pkey->pkey.rsa); - } +} -static int rsa_bits(const EVP_PKEY *pkey) - { +static int +rsa_bits(const EVP_PKEY *pkey) +{ return BN_num_bits(pkey->pkey.rsa->n); - } +} -static void int_rsa_free(EVP_PKEY *pkey) - { +static void +int_rsa_free(EVP_PKEY *pkey) +{ RSA_free(pkey->pkey.rsa); - } - +} -static void update_buflen(const BIGNUM *b, size_t *pbuflen) - { +static void +update_buflen(const BIGNUM *b, size_t *pbuflen) +{ size_t i; + if (!b) return; if (*pbuflen < (i = (size_t)BN_num_bytes(b))) - *pbuflen = i; - } + *pbuflen = i; +} -static int do_rsa_print(BIO *bp, const RSA *x, int off, int priv) - { +static int +do_rsa_print(BIO *bp, const RSA *x, int off, int priv) +{ char *str; const char *s; - unsigned char *m=NULL; - int ret=0, mod_len = 0; - size_t buf_len=0; + unsigned char *m = NULL; + int ret = 0, mod_len = 0; + size_t buf_len = 0; update_buflen(x->n, &buf_len); update_buflen(x->e, &buf_len); - if (priv) - { + if (priv) { update_buflen(x->d, &buf_len); update_buflen(x->p, &buf_len); update_buflen(x->q, &buf_len); update_buflen(x->dmp1, &buf_len); update_buflen(x->dmq1, &buf_len); update_buflen(x->iqmp, &buf_len); - } + } - m = malloc(buf_len+10); - if (m == NULL) - { - RSAerr(RSA_F_DO_RSA_PRINT,ERR_R_MALLOC_FAILURE); + m = malloc(buf_len + 10); + if (m == NULL) { + RSAerr(RSA_F_DO_RSA_PRINT, ERR_R_MALLOC_FAILURE); goto err; - } + } if (x->n != NULL) mod_len = BN_num_bits(x->n); - if(!BIO_indent(bp,off,128)) + if (!BIO_indent(bp, off, 128)) goto err; - if (priv && x->d) - { - if (BIO_printf(bp,"Private-Key: (%d bit)\n", mod_len) - <= 0) goto err; + if (priv && x->d) { + if (BIO_printf(bp, "Private-Key: (%d bit)\n", mod_len) <= 0) + goto err; str = "modulus:"; s = "publicExponent:"; - } - else - { - if (BIO_printf(bp,"Public-Key: (%d bit)\n", mod_len) - <= 0) goto err; + } else { + if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0) + goto err; str = "Modulus:"; s= "Exponent:"; - } - if (!ASN1_bn_print(bp,str,x->n,m,off)) goto err; - if (!ASN1_bn_print(bp,s,x->e,m,off)) + } + if (!ASN1_bn_print(bp, str, x->n, m, off)) goto err; - if (priv) - { - if (!ASN1_bn_print(bp,"privateExponent:",x->d,m,off)) + if (!ASN1_bn_print(bp, s, x->e, m, off)) + goto err; + if (priv) { + if (!ASN1_bn_print(bp, "privateExponent:", x->d,m, off)) goto err; - if (!ASN1_bn_print(bp,"prime1:",x->p,m,off)) + if (!ASN1_bn_print(bp, "prime1:", x->p, m, off)) goto err; - if (!ASN1_bn_print(bp,"prime2:",x->q,m,off)) + if (!ASN1_bn_print(bp, "prime2:", x->q, m, off)) goto err; - if (!ASN1_bn_print(bp,"exponent1:",x->dmp1,m,off)) + if (!ASN1_bn_print(bp, "exponent1:", x->dmp1, m, off)) goto err; - if (!ASN1_bn_print(bp,"exponent2:",x->dmq1,m,off)) + if (!ASN1_bn_print(bp, "exponent2:", x->dmq1, m, off)) goto err; - if (!ASN1_bn_print(bp,"coefficient:",x->iqmp,m,off)) + if (!ASN1_bn_print(bp, "coefficient:", x->iqmp, m, off)) goto err; - } - ret=1; + } + ret = 1; err: free(m); return(ret); - } +} -static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *ctx) - { +static int +rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) +{ return do_rsa_print(bp, pkey->pkey.rsa, indent, 0); - } - +} -static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, - ASN1_PCTX *ctx) - { +static int +rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) +{ return do_rsa_print(bp, pkey->pkey.rsa, indent, 1); - } +} -static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg, - X509_ALGOR **pmaskHash) - { +static RSA_PSS_PARAMS * +rsa_pss_decode(const X509_ALGOR *alg, X509_ALGOR **pmaskHash) +{ const unsigned char *p; int plen; RSA_PSS_PARAMS *pss; @@ -283,31 +289,30 @@ static RSA_PSS_PARAMS *rsa_pss_decode(const X509_ALGOR *alg, if (!pss) return NULL; - if (pss->maskGenAlgorithm) - { + if (pss->maskGenAlgorithm) { ASN1_TYPE *param = pss->maskGenAlgorithm->parameter; - if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) == NID_mgf1 - && param->type == V_ASN1_SEQUENCE) - { + if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) == NID_mgf1 && + param->type == V_ASN1_SEQUENCE) { p = param->value.sequence->data; plen = param->value.sequence->length; *pmaskHash = d2i_X509_ALGOR(NULL, &p, plen); - } } + } return pss; - } +} -static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss, - X509_ALGOR *maskHash, int indent) - { +static int +rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss, X509_ALGOR *maskHash, + int indent) +{ int rv = 0; - if (!pss) - { + + if (!pss) { if (BIO_puts(bp, " (INVALID PSS PARAMETERS)\n") <= 0) return 0; return 1; - } + } if (BIO_puts(bp, "\n") <= 0) goto err; if (!BIO_indent(bp, indent, 128)) @@ -315,12 +320,10 @@ static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss, if (BIO_puts(bp, "Hash Algorithm: ") <= 0) goto err; - if (pss->hashAlgorithm) - { + if (pss->hashAlgorithm) { if (i2a_ASN1_OBJECT(bp, pss->hashAlgorithm->algorithm) <= 0) goto err; - } - else if (BIO_puts(bp, "sha1 (default)") <= 0) + } else if (BIO_puts(bp, "sha1 (default)") <= 0) goto err; if (BIO_puts(bp, "\n") <= 0) @@ -330,22 +333,18 @@ static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss, goto err; if (BIO_puts(bp, "Mask Algorithm: ") <= 0) - goto err; - if (pss->maskGenAlgorithm) - { + goto err; + if (pss->maskGenAlgorithm) { if (i2a_ASN1_OBJECT(bp, pss->maskGenAlgorithm->algorithm) <= 0) goto err; if (BIO_puts(bp, " with ") <= 0) goto err; - if (maskHash) - { + if (maskHash) { if (i2a_ASN1_OBJECT(bp, maskHash->algorithm) <= 0) + goto err; + } else if (BIO_puts(bp, "INVALID") <= 0) goto err; - } - else if (BIO_puts(bp, "INVALID") <= 0) - goto err; - } - else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) + } else if (BIO_puts(bp, "mgf1 with sha1 (default)") <= 0) goto err; BIO_puts(bp, "\n"); @@ -353,41 +352,35 @@ static int rsa_pss_param_print(BIO *bp, RSA_PSS_PARAMS *pss, goto err; if (BIO_puts(bp, "Salt Length: 0x") <= 0) goto err; - if (pss->saltLength) - { + if (pss->saltLength) { if (i2a_ASN1_INTEGER(bp, pss->saltLength) <= 0) goto err; - } - else if (BIO_puts(bp, "0x14 (default)") <= 0) + } else if (BIO_puts(bp, "0x14 (default)") <= 0) goto err; BIO_puts(bp, "\n"); if (!BIO_indent(bp, indent, 128)) goto err; if (BIO_puts(bp, "Trailer Field: 0x") <= 0) - goto err; - if (pss->trailerField) - { + goto err; + if (pss->trailerField) { if (i2a_ASN1_INTEGER(bp, pss->trailerField) <= 0) goto err; - } - else if (BIO_puts(bp, "BC (default)") <= 0) + } else if (BIO_puts(bp, "BC (default)") <= 0) goto err; BIO_puts(bp, "\n"); rv = 1; - err: +err: return rv; +} - } - -static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, - const ASN1_STRING *sig, - int indent, ASN1_PCTX *pctx) - { - if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) - { +static int +rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, const ASN1_STRING *sig, + int indent, ASN1_PCTX *pctx) +{ + if (OBJ_obj2nid(sigalg->algorithm) == NID_rsassaPss) { int rv; RSA_PSS_PARAMS *pss; X509_ALGOR *maskHash; @@ -399,147 +392,132 @@ static int rsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, X509_ALGOR_free(maskHash); if (!rv) return 0; - } - else if (!sig && BIO_puts(bp, "\n") <= 0) + } else if (!sig && BIO_puts(bp, "\n") <= 0) return 0; if (sig) return X509_signature_dump(bp, sig, indent); return 1; - } +} -static int rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) - { +static int +rsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) +{ X509_ALGOR *alg = NULL; - switch (op) - { - - case ASN1_PKEY_CTRL_PKCS7_SIGN: + switch (op) { + case ASN1_PKEY_CTRL_PKCS7_SIGN: if (arg1 == 0) PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, NULL, &alg); break; - case ASN1_PKEY_CTRL_PKCS7_ENCRYPT: + case ASN1_PKEY_CTRL_PKCS7_ENCRYPT: if (arg1 == 0) PKCS7_RECIP_INFO_get0_alg(arg2, &alg); break; #ifndef OPENSSL_NO_CMS - case ASN1_PKEY_CTRL_CMS_SIGN: + case ASN1_PKEY_CTRL_CMS_SIGN: if (arg1 == 0) CMS_SignerInfo_get0_algs(arg2, NULL, NULL, NULL, &alg); break; - case ASN1_PKEY_CTRL_CMS_ENVELOPE: + case ASN1_PKEY_CTRL_CMS_ENVELOPE: if (arg1 == 0) CMS_RecipientInfo_ktri_get0_algs(arg2, NULL, NULL, &alg); - break; + break; #endif - case ASN1_PKEY_CTRL_DEFAULT_MD_NID: + case ASN1_PKEY_CTRL_DEFAULT_MD_NID: *(int *)arg2 = NID_sha1; return 1; - default: + default: return -2; - - } + } if (alg) X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), - V_ASN1_NULL, 0); + V_ASN1_NULL, 0); return 1; - - } +} /* Customised RSA item verification routine. This is called * when a signature is encountered requiring special handling. We * currently only handle PSS. */ - - -static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, - X509_ALGOR *sigalg, ASN1_BIT_STRING *sig, - EVP_PKEY *pkey) - { +static int +rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, + X509_ALGOR *sigalg, ASN1_BIT_STRING *sig, EVP_PKEY *pkey) +{ int rv = -1; int saltlen; const EVP_MD *mgf1md = NULL, *md = NULL; RSA_PSS_PARAMS *pss; X509_ALGOR *maskHash; EVP_PKEY_CTX *pkctx; + /* Sanity check: make sure it is PSS */ - if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) - { + if (OBJ_obj2nid(sigalg->algorithm) != NID_rsassaPss) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_SIGNATURE_TYPE); return -1; - } + } /* Decode PSS parameters */ pss = rsa_pss_decode(sigalg, &maskHash); - if (pss == NULL) - { + if (pss == NULL) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_PSS_PARAMETERS); goto err; - } + } /* Check mask and lookup mask hash algorithm */ - if (pss->maskGenAlgorithm) - { - if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) != NID_mgf1) - { - RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_ALGORITHM); + if (pss->maskGenAlgorithm) { + if (OBJ_obj2nid(pss->maskGenAlgorithm->algorithm) != NID_mgf1) { + RSAerr(RSA_F_RSA_ITEM_VERIFY, + RSA_R_UNSUPPORTED_MASK_ALGORITHM); goto err; - } - if (!maskHash) - { - RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNSUPPORTED_MASK_PARAMETER); + } + if (!maskHash) { + RSAerr(RSA_F_RSA_ITEM_VERIFY, + RSA_R_UNSUPPORTED_MASK_PARAMETER); goto err; - } + } mgf1md = EVP_get_digestbyobj(maskHash->algorithm); - if (mgf1md == NULL) - { - RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_MASK_DIGEST); + if (mgf1md == NULL) { + RSAerr(RSA_F_RSA_ITEM_VERIFY, + RSA_R_UNKNOWN_MASK_DIGEST); goto err; - } } - else + } else mgf1md = EVP_sha1(); - if (pss->hashAlgorithm) - { + if (pss->hashAlgorithm) { md = EVP_get_digestbyobj(pss->hashAlgorithm->algorithm); - if (md == NULL) - { + if (md == NULL) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_UNKNOWN_PSS_DIGEST); goto err; - } } - else + } else md = EVP_sha1(); - if (pss->saltLength) - { + if (pss->saltLength) { saltlen = ASN1_INTEGER_get(pss->saltLength); /* Could perform more salt length sanity checks but the main * RSA routines will trap other invalid values anyway. */ - if (saltlen < 0) - { - RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_SALT_LENGTH); + if (saltlen < 0) { + RSAerr(RSA_F_RSA_ITEM_VERIFY, + RSA_R_INVALID_SALT_LENGTH); goto err; - } } - else + } else saltlen = 20; /* low-level routines support only trailer field 0xbc (value 1) * and PKCS#1 says we should reject any other value anyway. */ - if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) - { + if (pss->trailerField && ASN1_INTEGER_get(pss->trailerField) != 1) { RSAerr(RSA_F_RSA_ITEM_VERIFY, RSA_R_INVALID_TRAILER); goto err; - } + } /* We have all parameters now set up context */ @@ -557,31 +535,32 @@ static int rsa_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, /* Carry on */ rv = 2; - err: +err: RSA_PSS_PARAMS_free(pss); if (maskHash) X509_ALGOR_free(maskHash); return rv; - } +} -static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, - X509_ALGOR *alg1, X509_ALGOR *alg2, - ASN1_BIT_STRING *sig) - { +static int +rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, + X509_ALGOR *alg1, X509_ALGOR *alg2, ASN1_BIT_STRING *sig) +{ int pad_mode; EVP_PKEY_CTX *pkctx = ctx->pctx; + if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0) return 0; if (pad_mode == RSA_PKCS1_PADDING) return 2; - if (pad_mode == RSA_PKCS1_PSS_PADDING) - { + if (pad_mode == RSA_PKCS1_PSS_PADDING) { const EVP_MD *sigmd, *mgf1md; RSA_PSS_PARAMS *pss = NULL; X509_ALGOR *mgf1alg = NULL; ASN1_STRING *os1 = NULL, *os2 = NULL; EVP_PKEY *pk = EVP_PKEY_CTX_get0_pkey(pkctx); int saltlen, rv = 0; + sigmd = EVP_MD_CTX_md(ctx); if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0) goto err; @@ -589,62 +568,56 @@ static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, goto err; if (saltlen == -1) saltlen = EVP_MD_size(sigmd); - else if (saltlen == -2) - { + else if (saltlen == -2) { saltlen = EVP_PKEY_size(pk) - EVP_MD_size(sigmd) - 2; if (((EVP_PKEY_bits(pk) - 1) & 0x7) == 0) saltlen--; - } + } pss = RSA_PSS_PARAMS_new(); if (!pss) goto err; - if (saltlen != 20) - { + if (saltlen != 20) { pss->saltLength = ASN1_INTEGER_new(); if (!pss->saltLength) goto err; if (!ASN1_INTEGER_set(pss->saltLength, saltlen)) goto err; - } - if (EVP_MD_type(sigmd) != NID_sha1) - { + } + if (EVP_MD_type(sigmd) != NID_sha1) { pss->hashAlgorithm = X509_ALGOR_new(); if (!pss->hashAlgorithm) goto err; X509_ALGOR_set_md(pss->hashAlgorithm, sigmd); - } - if (EVP_MD_type(mgf1md) != NID_sha1) - { + } + if (EVP_MD_type(mgf1md) != NID_sha1) { ASN1_STRING *stmp = NULL; /* need to embed algorithm ID inside another */ mgf1alg = X509_ALGOR_new(); X509_ALGOR_set_md(mgf1alg, mgf1md); if (!ASN1_item_pack(mgf1alg, ASN1_ITEM_rptr(X509_ALGOR), - &stmp)) - goto err; + &stmp)) + goto err; pss->maskGenAlgorithm = X509_ALGOR_new(); if (!pss->maskGenAlgorithm) goto err; X509_ALGOR_set0(pss->maskGenAlgorithm, - OBJ_nid2obj(NID_mgf1), - V_ASN1_SEQUENCE, stmp); - } + OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp); + } /* Finally create string with pss parameter encoding. */ if (!ASN1_item_pack(pss, ASN1_ITEM_rptr(RSA_PSS_PARAMS), &os1)) goto err; - if (alg2) - { + if (alg2) { os2 = ASN1_STRING_dup(os1); if (!os2) goto err; X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_rsassaPss), - V_ASN1_SEQUENCE, os2); - } + V_ASN1_SEQUENCE, os2); + } X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_rsassaPss), - V_ASN1_SEQUENCE, os1); + V_ASN1_SEQUENCE, os1); os1 = os2 = NULL; rv = 3; - err: +err: if (mgf1alg) X509_ALGOR_free(mgf1alg); if (pss) @@ -652,10 +625,9 @@ static int rsa_item_sign(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn, if (os1) ASN1_STRING_free(os1); return rv; - - } - return 2; } + return 2; +} const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[] = { { diff --git a/lib/libcrypto/rsa/rsa_asn1.c b/lib/libcrypto/rsa/rsa_asn1.c index b535c85141e..e876dbdf493 100644 --- a/lib/libcrypto/rsa/rsa_asn1.c +++ b/lib/libcrypto/rsa/rsa_asn1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_asn1.c,v 1.6 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_asn1.c,v 1.7 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2000. */ @@ -64,14 +64,15 @@ #include <openssl/asn1t.h> /* Override the default free and new methods */ -static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, - void *exarg) +static int +rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg) { - if(operation == ASN1_OP_NEW_PRE) { + if (operation == ASN1_OP_NEW_PRE) { *pval = (ASN1_VALUE *)RSA_new(); - if(*pval) return 2; + if (*pval) + return 2; return 0; - } else if(operation == ASN1_OP_FREE_PRE) { + } else if (operation == ASN1_OP_FREE_PRE) { RSA_free((RSA *)*pval); *pval = NULL; return 2; @@ -110,12 +111,14 @@ IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey) IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey) -RSA *RSAPublicKey_dup(RSA *rsa) - { +RSA * +RSAPublicKey_dup(RSA *rsa) +{ return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa); - } +} -RSA *RSAPrivateKey_dup(RSA *rsa) - { +RSA * +RSAPrivateKey_dup(RSA *rsa) +{ return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa); - } +} diff --git a/lib/libcrypto/rsa/rsa_chk.c b/lib/libcrypto/rsa/rsa_chk.c index d7e496aab2a..54113f89f6a 100644 --- a/lib/libcrypto/rsa/rsa_chk.c +++ b/lib/libcrypto/rsa/rsa_chk.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_chk.c,v 1.6 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_chk.c,v 1.7 2014/07/09 08:20:08 miod Exp $ */ /* ==================================================================== * Copyright (c) 1999 The OpenSSL Project. All rights reserved. * @@ -53,18 +53,18 @@ #include <openssl/rsa.h> -int RSA_check_key(const RSA *key) - { +int +RSA_check_key(const RSA *key) +{ BIGNUM *i, *j, *k, *l, *m; BN_CTX *ctx; int r; - int ret=1; + int ret = 1; - if (!key->p || !key->q || !key->n || !key->e || !key->d) - { + if (!key->p || !key->q || !key->n || !key->e || !key->d) { RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_VALUE_MISSING); return 0; - } + } i = BN_new(); j = BN_new(); @@ -72,119 +72,148 @@ int RSA_check_key(const RSA *key) l = BN_new(); m = BN_new(); ctx = BN_CTX_new(); - if (i == NULL || j == NULL || k == NULL || l == NULL || - m == NULL || ctx == NULL) - { + if (i == NULL || j == NULL || k == NULL || l == NULL || m == NULL || + ctx == NULL) { ret = -1; RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE); goto err; - } + } /* p prime? */ r = BN_is_prime_ex(key->p, BN_prime_checks, NULL, NULL); - if (r != 1) - { + if (r != 1) { ret = r; if (r != 0) goto err; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME); - } + } /* q prime? */ r = BN_is_prime_ex(key->q, BN_prime_checks, NULL, NULL); - if (r != 1) - { + if (r != 1) { ret = r; if (r != 0) goto err; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME); - } + } /* n = p*q? */ r = BN_mul(i, key->p, key->q, ctx); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } - if (BN_cmp(i, key->n) != 0) - { + if (BN_cmp(i, key->n) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q); - } + } /* d*e = 1 mod lcm(p-1,q-1)? */ r = BN_sub(i, key->p, BN_value_one()); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } r = BN_sub(j, key->q, BN_value_one()); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } /* now compute k = lcm(i,j) */ r = BN_mul(l, i, j, ctx); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } r = BN_gcd(m, i, j, ctx); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */ - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } r = BN_mod_mul(i, key->d, key->e, k, ctx); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } - if (!BN_is_one(i)) - { + if (!BN_is_one(i)) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1); - } + } - if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) - { + if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) { /* dmp1 = d mod (p-1)? */ r = BN_sub(i, key->p, BN_value_one()); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } r = BN_mod(j, key->d, i, ctx); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } - if (BN_cmp(j, key->dmp1) != 0) - { + if (BN_cmp(j, key->dmp1) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, - RSA_R_DMP1_NOT_CONGRUENT_TO_D); - } + RSA_R_DMP1_NOT_CONGRUENT_TO_D); + } /* dmq1 = d mod (q-1)? */ r = BN_sub(i, key->q, BN_value_one()); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } r = BN_mod(j, key->d, i, ctx); - if (!r) { ret = -1; goto err; } + if (!r) { + ret = -1; + goto err; + } - if (BN_cmp(j, key->dmq1) != 0) - { + if (BN_cmp(j, key->dmq1) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, - RSA_R_DMQ1_NOT_CONGRUENT_TO_D); - } + RSA_R_DMQ1_NOT_CONGRUENT_TO_D); + } /* iqmp = q^-1 mod p? */ - if(!BN_mod_inverse(i, key->q, key->p, ctx)) - { + if (!BN_mod_inverse(i, key->q, key->p, ctx)) { ret = -1; goto err; - } + } - if (BN_cmp(i, key->iqmp) != 0) - { + if (BN_cmp(i, key->iqmp) != 0) { ret = 0; RSAerr(RSA_F_RSA_CHECK_KEY, - RSA_R_IQMP_NOT_INVERSE_OF_Q); - } + RSA_R_IQMP_NOT_INVERSE_OF_Q); } + } err: - if (i != NULL) BN_free(i); - if (j != NULL) BN_free(j); - if (k != NULL) BN_free(k); - if (l != NULL) BN_free(l); - if (m != NULL) BN_free(m); - if (ctx != NULL) BN_CTX_free(ctx); + if (i != NULL) + BN_free(i); + if (j != NULL) + BN_free(j); + if (k != NULL) + BN_free(k); + if (l != NULL) + BN_free(l); + if (m != NULL) + BN_free(m); + if (ctx != NULL) + BN_CTX_free(ctx); return (ret); - } +} diff --git a/lib/libcrypto/rsa/rsa_crpt.c b/lib/libcrypto/rsa/rsa_crpt.c index 2122e7baa40..16679cfd141 100644 --- a/lib/libcrypto/rsa/rsa_crpt.c +++ b/lib/libcrypto/rsa/rsa_crpt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_crpt.c,v 1.4 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_crpt.c,v 1.5 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -67,54 +67,61 @@ #include <openssl/engine.h> #endif -int RSA_size(const RSA *r) - { - return(BN_num_bytes(r->n)); - } +int +RSA_size(const RSA *r) +{ + return BN_num_bytes(r->n); +} -int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, - RSA *rsa, int padding) - { - return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding)); - } +int +RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + return rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding); +} -int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, - RSA *rsa, int padding) - { - return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding)); - } +int +RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + return rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding); +} -int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, - RSA *rsa, int padding) - { - return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding)); - } +int +RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + return rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding); +} -int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, - RSA *rsa, int padding) - { - return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding)); - } +int +RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + return rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding); +} -int RSA_flags(const RSA *r) - { - return((r == NULL)?0:r->meth->flags); - } +int +RSA_flags(const RSA *r) +{ + return r == NULL ? 0 : r->meth->flags; +} -void RSA_blinding_off(RSA *rsa) - { - if (rsa->blinding != NULL) - { +void +RSA_blinding_off(RSA *rsa) +{ + if (rsa->blinding != NULL) { BN_BLINDING_free(rsa->blinding); - rsa->blinding=NULL; - } + rsa->blinding = NULL; + } rsa->flags &= ~RSA_FLAG_BLINDING; rsa->flags |= RSA_FLAG_NO_BLINDING; - } +} -int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) - { - int ret=0; +int +RSA_blinding_on(RSA *rsa, BN_CTX *ctx) +{ + int ret = 0; if (rsa->blinding != NULL) RSA_blinding_off(rsa); @@ -125,13 +132,14 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) rsa->flags |= RSA_FLAG_BLINDING; rsa->flags &= ~RSA_FLAG_NO_BLINDING; - ret=1; + ret = 1; err: return(ret); - } +} -static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, - const BIGNUM *q, BN_CTX *ctx) +static BIGNUM * +rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, const BIGNUM *q, + BN_CTX *ctx) { BIGNUM *ret = NULL, *r0, *r1, *r2; @@ -145,9 +153,12 @@ static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p, if (r2 == NULL) goto err; - if (!BN_sub(r1, p, BN_value_one())) goto err; - if (!BN_sub(r2, q, BN_value_one())) goto err; - if (!BN_mul(r0, r1, r2, ctx)) goto err; + if (!BN_sub(r1, p, BN_value_one())) + goto err; + if (!BN_sub(r2, q, BN_value_one())) + goto err; + if (!BN_mul(r0, r1, r2, ctx)) + goto err; ret = BN_mod_inverse(NULL, d, r0, ctx); err: @@ -155,62 +166,56 @@ err: return ret; } -BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx) +BN_BLINDING * +RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx) { BIGNUM local_n; - BIGNUM *e,*n; + BIGNUM *e, *n; BN_CTX *ctx; BN_BLINDING *ret = NULL; - if (in_ctx == NULL) - { - if ((ctx = BN_CTX_new()) == NULL) return 0; - } - else + if (in_ctx == NULL) { + if ((ctx = BN_CTX_new()) == NULL) + return 0; + } else ctx = in_ctx; BN_CTX_start(ctx); e = BN_CTX_get(ctx); - if (e == NULL) - { + if (e == NULL) { RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE); goto err; - } + } - if (rsa->e == NULL) - { + if (rsa->e == NULL) { e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx); - if (e == NULL) - { - RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT); + if (e == NULL) { + RSAerr(RSA_F_RSA_SETUP_BLINDING, + RSA_R_NO_PUBLIC_EXPONENT); goto err; - } } - else + } else e = rsa->e; - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { /* Set BN_FLG_CONSTTIME flag */ n = &local_n; BN_with_flags(n, rsa->n, BN_FLG_CONSTTIME); - } - else + } else n = rsa->n; - ret = BN_BLINDING_create_param(NULL, e, n, ctx, - rsa->meth->bn_mod_exp, rsa->_method_mod_n); - if (ret == NULL) - { + ret = BN_BLINDING_create_param(NULL, e, n, ctx, rsa->meth->bn_mod_exp, + rsa->_method_mod_n); + if (ret == NULL) { RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB); goto err; - } + } CRYPTO_THREADID_current(BN_BLINDING_thread_id(ret)); err: BN_CTX_end(ctx); if (in_ctx == NULL) BN_CTX_free(ctx); - if(rsa->e == NULL) + if (rsa->e == NULL) BN_free(e); return ret; diff --git a/lib/libcrypto/rsa/rsa_depr.c b/lib/libcrypto/rsa/rsa_depr.c index 5f607272551..6808b5aecb9 100644 --- a/lib/libcrypto/rsa/rsa_depr.c +++ b/lib/libcrypto/rsa/rsa_depr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_depr.c,v 1.3 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_depr.c,v 1.4 2014/07/09 08:20:08 miod Exp $ */ /* ==================================================================== * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. * @@ -64,34 +64,37 @@ #ifndef OPENSSL_NO_DEPRECATED -RSA *RSA_generate_key(int bits, unsigned long e_value, - void (*callback)(int,int,void *), void *cb_arg) - { +RSA * +RSA_generate_key(int bits, unsigned long e_value, + void (*callback)(int, int, void *), void *cb_arg) +{ BN_GENCB cb; int i; RSA *rsa = RSA_new(); BIGNUM *e = BN_new(); - if(!rsa || !e) goto err; + if (!rsa || !e) + goto err; /* The problem is when building with 8, 16, or 32 BN_ULONG, * unsigned long can be larger */ - for (i=0; i<(int)sizeof(unsigned long)*8; i++) - { - if (e_value & (1UL<<i)) - if (BN_set_bit(e,i) == 0) + for (i = 0; i < (int)sizeof(unsigned long) * 8; i++) { + if (e_value & (1UL << i)) + if (BN_set_bit(e, i) == 0) goto err; - } + } BN_GENCB_set_old(&cb, callback, cb_arg); - if(RSA_generate_key_ex(rsa, bits, e, &cb)) { + if (RSA_generate_key_ex(rsa, bits, e, &cb)) { BN_free(e); return rsa; } err: - if(e) BN_free(e); - if(rsa) RSA_free(rsa); + if (e) + BN_free(e); + if (rsa) + RSA_free(rsa); return 0; - } +} #endif diff --git a/lib/libcrypto/rsa/rsa_eay.c b/lib/libcrypto/rsa/rsa_eay.c index 9e17483e09a..7b294e761e4 100644 --- a/lib/libcrypto/rsa/rsa_eay.c +++ b/lib/libcrypto/rsa/rsa_eay.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_eay.c,v 1.28 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_eay.c,v 1.29 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -140,114 +140,114 @@ static RSA_METHOD rsa_pkcs1_eay_meth = { .finish = RSA_eay_finish, }; -const RSA_METHOD *RSA_PKCS1_SSLeay(void) - { - return(&rsa_pkcs1_eay_meth); - } +const RSA_METHOD * +RSA_PKCS1_SSLeay(void) +{ + return &rsa_pkcs1_eay_meth; +} -static int RSA_eay_public_encrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { - BIGNUM *f,*ret; - int i,j,k,num=0,r= -1; - unsigned char *buf=NULL; - BN_CTX *ctx=NULL; +static int +RSA_eay_public_encrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + BIGNUM *f, *ret; + int i, j, k, num = 0, r = -1; + unsigned char *buf = NULL; + BN_CTX *ctx = NULL; - if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) - { + if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) { RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE); return -1; - } + } - if (BN_ucmp(rsa->n, rsa->e) <= 0) - { + if (BN_ucmp(rsa->n, rsa->e) <= 0) { RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); return -1; - } + } /* for large moduli, enforce exponent limit */ - if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) - { - if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) - { + if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) { + if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) { RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE); return -1; - } } + } - if ((ctx=BN_CTX_new()) == NULL) goto err; + if ((ctx=BN_CTX_new()) == NULL) + goto err; BN_CTX_start(ctx); f = BN_CTX_get(ctx); ret = BN_CTX_get(ctx); - num=BN_num_bytes(rsa->n); + num = BN_num_bytes(rsa->n); buf = malloc(num); - if (!f || !ret || !buf) - { + if (!f || !ret || !buf) { RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE); goto err; - } + } - switch (padding) - { + switch (padding) { case RSA_PKCS1_PADDING: - i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen); + i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen); break; #ifndef OPENSSL_NO_SHA case RSA_PKCS1_OAEP_PADDING: - i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0); + i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0); break; #endif case RSA_SSLV23_PADDING: - i=RSA_padding_add_SSLv23(buf,num,from,flen); + i = RSA_padding_add_SSLv23(buf, num, from, flen); break; case RSA_NO_PADDING: - i=RSA_padding_add_none(buf,num,from,flen); + i = RSA_padding_add_none(buf, num, from, flen); break; default: - RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); + RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, + RSA_R_UNKNOWN_PADDING_TYPE); + goto err; + } + if (i <= 0) goto err; - } - if (i <= 0) goto err; - if (BN_bin2bn(buf,num,f) == NULL) goto err; + if (BN_bin2bn(buf, num, f) == NULL) + goto err; - if (BN_ucmp(f, rsa->n) >= 0) - { + if (BN_ucmp(f, rsa->n) >= 0) { /* usually the padding functions would catch this */ - RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); + RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, + RSA_R_DATA_TOO_LARGE_FOR_MODULUS); goto err; - } + } if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, + CRYPTO_LOCK_RSA, rsa->n, ctx)) goto err; - if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, - rsa->_method_mod_n)) goto err; + if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, rsa->_method_mod_n)) + goto err; /* put in leading 0 bytes if the number is less than the * length of the modulus */ - j=BN_num_bytes(ret); - i=BN_bn2bin(ret,&(to[num-j])); - for (k=0; k<(num-i); k++) - to[k]=0; + j = BN_num_bytes(ret); + i = BN_bn2bin(ret, &(to[num - j])); + for (k = 0; k < num - i; k++) + to[k] = 0; - r=num; + r = num; err: - if (ctx != NULL) - { + if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); - } - if (buf != NULL) - { - OPENSSL_cleanse(buf,num); + } + if (buf != NULL) { + OPENSSL_cleanse(buf, num); free(buf); - } - return(r); } + return r; +} -static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) +static BN_BLINDING * +rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) { BN_BLINDING *ret; int got_write_lock = 0; @@ -255,53 +255,47 @@ static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) CRYPTO_r_lock(CRYPTO_LOCK_RSA); - if (rsa->blinding == NULL) - { + if (rsa->blinding == NULL) { CRYPTO_r_unlock(CRYPTO_LOCK_RSA); CRYPTO_w_lock(CRYPTO_LOCK_RSA); got_write_lock = 1; if (rsa->blinding == NULL) rsa->blinding = RSA_setup_blinding(rsa, ctx); - } + } ret = rsa->blinding; if (ret == NULL) goto err; CRYPTO_THREADID_current(&cur); - if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) - { + if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) { /* rsa->blinding is ours! */ - *local = 1; - } - else - { + } else { /* resort to rsa->mt_blinding instead */ + /* + * Instruct rsa_blinding_convert(), rsa_blinding_invert() + * that the BN_BLINDING is shared, meaning that accesses + * require locks, and that the blinding factor must be + * stored outside the BN_BLINDING + */ + *local = 0; - *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert() - * that the BN_BLINDING is shared, meaning that accesses - * require locks, and that the blinding factor must be - * stored outside the BN_BLINDING - */ - - if (rsa->mt_blinding == NULL) - { - if (!got_write_lock) - { + if (rsa->mt_blinding == NULL) { + if (!got_write_lock) { CRYPTO_r_unlock(CRYPTO_LOCK_RSA); CRYPTO_w_lock(CRYPTO_LOCK_RSA); got_write_lock = 1; - } + } if (rsa->mt_blinding == NULL) rsa->mt_blinding = RSA_setup_blinding(rsa, ctx); - } - ret = rsa->mt_blinding; } + ret = rsa->mt_blinding; + } - err: +err: if (got_write_lock) CRYPTO_w_unlock(CRYPTO_LOCK_RSA); else @@ -309,29 +303,33 @@ static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx) return ret; } -static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, - BN_CTX *ctx) - { +static int +rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, BN_CTX *ctx) +{ if (unblind == NULL) - /* Local blinding: store the unblinding factor - * in BN_BLINDING. */ + /* + * Local blinding: store the unblinding factor + * in BN_BLINDING. + */ return BN_BLINDING_convert_ex(f, NULL, b, ctx); - else - { - /* Shared blinding: store the unblinding factor - * outside BN_BLINDING. */ + else { + /* + * Shared blinding: store the unblinding factor + * outside BN_BLINDING. + */ int ret; CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING); ret = BN_BLINDING_convert_ex(f, unblind, b, ctx); CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING); return ret; - } } +} -static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, - BN_CTX *ctx) - { - /* For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex +static int +rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, BN_CTX *ctx) +{ + /* + * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex * will use the unblinding factor stored in BN_BLINDING. * If BN_BLINDING is shared between threads, unblind must be non-null: * BN_BLINDING_invert_ex will then use the local unblinding factor, @@ -339,402 +337,392 @@ static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind, * In both cases it's safe to access the blinding without a lock. */ return BN_BLINDING_invert_ex(f, unblind, b, ctx); - } +} /* signing */ -static int RSA_eay_private_encrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { +static int +RSA_eay_private_encrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ BIGNUM *f, *ret, *res; - int i,j,k,num=0,r= -1; - unsigned char *buf=NULL; - BN_CTX *ctx=NULL; + int i, j, k, num = 0, r = -1; + unsigned char *buf = NULL; + BN_CTX *ctx = NULL; int local_blinding = 0; - /* Used only if the blinding structure is shared. A non-NULL unblind + /* + * Used only if the blinding structure is shared. A non-NULL unblind * instructs rsa_blinding_convert() and rsa_blinding_invert() to store - * the unblinding factor outside the blinding structure. */ + * the unblinding factor outside the blinding structure. + */ BIGNUM *unblind = NULL; BN_BLINDING *blinding = NULL; - if ((ctx=BN_CTX_new()) == NULL) goto err; + if ((ctx=BN_CTX_new()) == NULL) + goto err; BN_CTX_start(ctx); - f = BN_CTX_get(ctx); + f = BN_CTX_get(ctx); ret = BN_CTX_get(ctx); num = BN_num_bytes(rsa->n); buf = malloc(num); - if(!f || !ret || !buf) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); + if (!f || !ret || !buf) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE); goto err; - } + } - switch (padding) - { + switch (padding) { case RSA_PKCS1_PADDING: - i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen); + i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen); break; case RSA_X931_PADDING: - i=RSA_padding_add_X931(buf,num,from,flen); + i = RSA_padding_add_X931(buf, num, from, flen); break; case RSA_NO_PADDING: - i=RSA_padding_add_none(buf,num,from,flen); + i = RSA_padding_add_none(buf, num, from, flen); break; case RSA_SSLV23_PADDING: default: - RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE); + RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, + RSA_R_UNKNOWN_PADDING_TYPE); + goto err; + } + if (i <= 0) goto err; - } - if (i <= 0) goto err; - if (BN_bin2bn(buf,num,f) == NULL) goto err; + if (BN_bin2bn(buf,num,f) == NULL) + goto err; - if (BN_ucmp(f, rsa->n) >= 0) - { + if (BN_ucmp(f, rsa->n) >= 0) { /* usually the padding functions would catch this */ - RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); + RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, + RSA_R_DATA_TOO_LARGE_FOR_MODULUS); goto err; - } + } - if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) - { + if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) { blinding = rsa_get_blinding(rsa, &local_blinding, ctx); - if (blinding == NULL) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR); + if (blinding == NULL) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, + ERR_R_INTERNAL_ERROR); goto err; - } } + } - if (blinding != NULL) - { - if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE); + if (blinding != NULL) { + if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, + ERR_R_MALLOC_FAILURE); goto err; - } + } if (!rsa_blinding_convert(blinding, f, unblind, ctx)) goto err; - } + } - if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || - ((rsa->p != NULL) && - (rsa->q != NULL) && - (rsa->dmp1 != NULL) && - (rsa->dmq1 != NULL) && - (rsa->iqmp != NULL)) ) - { - if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; - } - else - { + if ((rsa->flags & RSA_FLAG_EXT_PKEY) || + (rsa->p != NULL && rsa->q != NULL && rsa->dmp1 != NULL && + rsa->dmq1 != NULL && rsa->iqmp != NULL)) { + if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) + goto err; + } else { BIGNUM local_d; BIGNUM *d = NULL; - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { BN_init(&local_d); d = &local_d; BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); - } - else - d= rsa->d; + } else + d = rsa->d; if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) - if(!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, + CRYPTO_LOCK_RSA, rsa->n, ctx)) goto err; - if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, - rsa->_method_mod_n)) goto err; - } + if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n,ctx, + rsa->_method_mod_n)) + goto err; + } if (blinding) if (!rsa_blinding_invert(blinding, ret, unblind, ctx)) goto err; - if (padding == RSA_X931_PADDING) - { + if (padding == RSA_X931_PADDING) { BN_sub(f, rsa->n, ret); if (BN_cmp(ret, f)) res = f; else res = ret; - } - else + } else res = ret; /* put in leading 0 bytes if the number is less than the * length of the modulus */ - j=BN_num_bytes(res); - i=BN_bn2bin(res,&(to[num-j])); - for (k=0; k<(num-i); k++) - to[k]=0; + j = BN_num_bytes(res); + i = BN_bn2bin(res, &(to[num - j])); + for (k = 0; k < num - i; k++) + to[k] = 0; - r=num; + r = num; err: - if (ctx != NULL) - { + if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); - } - if (buf != NULL) - { - OPENSSL_cleanse(buf,num); + } + if (buf != NULL) { + OPENSSL_cleanse(buf, num); free(buf); - } - return(r); } + return r; +} -static int RSA_eay_private_decrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { +static int +RSA_eay_private_decrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ BIGNUM *f, *ret; - int j,num=0,r= -1; + int j, num = 0, r = -1; unsigned char *p; - unsigned char *buf=NULL; - BN_CTX *ctx=NULL; + unsigned char *buf = NULL; + BN_CTX *ctx = NULL; int local_blinding = 0; - /* Used only if the blinding structure is shared. A non-NULL unblind + /* + * Used only if the blinding structure is shared. A non-NULL unblind * instructs rsa_blinding_convert() and rsa_blinding_invert() to store - * the unblinding factor outside the blinding structure. */ + * the unblinding factor outside the blinding structure. + */ BIGNUM *unblind = NULL; BN_BLINDING *blinding = NULL; - if((ctx = BN_CTX_new()) == NULL) goto err; + if ((ctx = BN_CTX_new()) == NULL) + goto err; BN_CTX_start(ctx); - f = BN_CTX_get(ctx); + f = BN_CTX_get(ctx); ret = BN_CTX_get(ctx); num = BN_num_bytes(rsa->n); buf = malloc(num); - if(!f || !ret || !buf) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); + if (!f || !ret || !buf) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE); goto err; - } + } /* This check was for equality but PGP does evil things * and chops off the top '0' bytes */ - if (flen > num) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); + if (flen > num) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, + RSA_R_DATA_GREATER_THAN_MOD_LEN); goto err; - } + } /* make data into a big number */ - if (BN_bin2bn(from,(int)flen,f) == NULL) goto err; + if (BN_bin2bn(from, (int)flen, f) == NULL) + goto err; - if (BN_ucmp(f, rsa->n) >= 0) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); + if (BN_ucmp(f, rsa->n) >= 0) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, + RSA_R_DATA_TOO_LARGE_FOR_MODULUS); goto err; - } + } - if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) - { + if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) { blinding = rsa_get_blinding(rsa, &local_blinding, ctx); - if (blinding == NULL) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR); + if (blinding == NULL) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, + ERR_R_INTERNAL_ERROR); goto err; - } } + } - if (blinding != NULL) - { - if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) - { - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE); + if (blinding != NULL) { + if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) { + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, + ERR_R_MALLOC_FAILURE); goto err; - } + } if (!rsa_blinding_convert(blinding, f, unblind, ctx)) goto err; - } + } /* do the decrypt */ - if ( (rsa->flags & RSA_FLAG_EXT_PKEY) || - ((rsa->p != NULL) && - (rsa->q != NULL) && - (rsa->dmp1 != NULL) && - (rsa->dmq1 != NULL) && - (rsa->iqmp != NULL)) ) - { - if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err; - } - else - { + if ((rsa->flags & RSA_FLAG_EXT_PKEY) || + (rsa->p != NULL && rsa->q != NULL && rsa->dmp1 != NULL && + rsa->dmq1 != NULL && rsa->iqmp != NULL)) { + if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) + goto err; + } else { BIGNUM local_d; BIGNUM *d = NULL; - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { d = &local_d; BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); - } - else + } else d = rsa->d; if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, + CRYPTO_LOCK_RSA, rsa->n, ctx)) goto err; - if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx, - rsa->_method_mod_n)) - goto err; - } + if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx, + rsa->_method_mod_n)) + goto err; + } if (blinding) if (!rsa_blinding_invert(blinding, ret, unblind, ctx)) goto err; - p=buf; - j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */ + p = buf; + j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */ - switch (padding) - { + switch (padding) { case RSA_PKCS1_PADDING: - r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num); + r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num); break; #ifndef OPENSSL_NO_SHA case RSA_PKCS1_OAEP_PADDING: - r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0); + r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0); break; #endif case RSA_SSLV23_PADDING: - r=RSA_padding_check_SSLv23(to,num,buf,j,num); + r = RSA_padding_check_SSLv23(to, num, buf, j, num); break; case RSA_NO_PADDING: - r=RSA_padding_check_none(to,num,buf,j,num); + r = RSA_padding_check_none(to, num, buf, j, num); break; default: - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, + RSA_R_UNKNOWN_PADDING_TYPE); goto err; - } + } if (r < 0) - RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED); + RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, + RSA_R_PADDING_CHECK_FAILED); err: - if (ctx != NULL) - { + if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); - } - if (buf != NULL) - { + } + if (buf != NULL) { OPENSSL_cleanse(buf,num); free(buf); - } - return(r); } + return r; +} /* signature verification */ -static int RSA_eay_public_decrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { - BIGNUM *f,*ret; - int i,num=0,r= -1; +static int +RSA_eay_public_decrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + BIGNUM *f, *ret; + int i, num = 0,r = -1; unsigned char *p; - unsigned char *buf=NULL; - BN_CTX *ctx=NULL; + unsigned char *buf = NULL; + BN_CTX *ctx = NULL; - if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) - { + if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) { RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE); return -1; - } + } - if (BN_ucmp(rsa->n, rsa->e) <= 0) - { + if (BN_ucmp(rsa->n, rsa->e) <= 0) { RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); return -1; - } + } /* for large moduli, enforce exponent limit */ - if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) - { - if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) - { + if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) { + if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) { RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE); return -1; - } } + } - if((ctx = BN_CTX_new()) == NULL) goto err; + if ((ctx = BN_CTX_new()) == NULL) + goto err; BN_CTX_start(ctx); f = BN_CTX_get(ctx); ret = BN_CTX_get(ctx); - num=BN_num_bytes(rsa->n); + num = BN_num_bytes(rsa->n); buf = malloc(num); - if(!f || !ret || !buf) - { - RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE); + if (!f || !ret || !buf) { + RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE); goto err; - } + } /* This check was for equality but PGP does evil things * and chops off the top '0' bytes */ - if (flen > num) - { - RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN); + if (flen > num) { + RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, + RSA_R_DATA_GREATER_THAN_MOD_LEN); goto err; - } + } - if (BN_bin2bn(from,flen,f) == NULL) goto err; + if (BN_bin2bn(from,flen,f) == NULL) + goto err; - if (BN_ucmp(f, rsa->n) >= 0) - { - RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS); + if (BN_ucmp(f, rsa->n) >= 0) { + RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, + RSA_R_DATA_TOO_LARGE_FOR_MODULUS); goto err; - } + } if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, + CRYPTO_LOCK_RSA, rsa->n, ctx)) goto err; - if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx, - rsa->_method_mod_n)) goto err; + if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx, + rsa->_method_mod_n)) + goto err; - if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12)) - if (!BN_sub(ret, rsa->n, ret)) goto err; + if (padding == RSA_X931_PADDING && (ret->d[0] & 0xf) != 12) + if (!BN_sub(ret, rsa->n, ret)) + goto err; - p=buf; - i=BN_bn2bin(ret,p); + p = buf; + i = BN_bn2bin(ret, p); - switch (padding) - { + switch (padding) { case RSA_PKCS1_PADDING: - r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num); + r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num); break; case RSA_X931_PADDING: - r=RSA_padding_check_X931(to,num,buf,i,num); + r = RSA_padding_check_X931(to, num, buf, i, num); break; case RSA_NO_PADDING: - r=RSA_padding_check_none(to,num,buf,i,num); + r = RSA_padding_check_none(to, num, buf, i, num); break; default: - RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE); + RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, + RSA_R_UNKNOWN_PADDING_TYPE); goto err; - } + } if (r < 0) - RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED); + RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, + RSA_R_PADDING_CHECK_FAILED); err: - if (ctx != NULL) - { + if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); - } - if (buf != NULL) - { - OPENSSL_cleanse(buf,num); + } + if (buf != NULL) { + OPENSSL_cleanse(buf, num); free(buf); - } - return(r); } + return r; +} -static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) - { - BIGNUM *r1,*m1,*vrfy; - BIGNUM local_dmp1,local_dmq1,local_c,local_r1; - BIGNUM *dmp1,*dmq1,*c,*pr1; - int ret=0; +static int +RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) +{ + BIGNUM *r1, *m1, *vrfy; + BIGNUM local_dmp1, local_dmq1, local_c, local_r1; + BIGNUM *dmp1, *dmq1, *c, *pr1; + int ret = 0; BN_CTX_start(ctx); r1 = BN_CTX_get(ctx); @@ -745,11 +733,11 @@ static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) BIGNUM local_p, local_q; BIGNUM *p = NULL, *q = NULL; - /* Make sure BN_mod_inverse in Montgomery intialization uses the + /* + * Make sure BN_mod_inverse in Montgomery intialization uses the * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { BN_init(&local_p); p = &local_p; BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); @@ -757,91 +745,92 @@ static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) BN_init(&local_q); q = &local_q; BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME); - } - else - { + } else { p = rsa->p; q = rsa->q; - } + } - if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) - { - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)) + if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) { + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, + CRYPTO_LOCK_RSA, p, ctx)) goto err; - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx)) + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, + CRYPTO_LOCK_RSA, q, ctx)) goto err; - } + } } if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) - if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx)) + if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, + CRYPTO_LOCK_RSA, rsa->n, ctx)) goto err; /* compute I mod q */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { c = &local_c; BN_with_flags(c, I, BN_FLG_CONSTTIME); - if (!BN_mod(r1,c,rsa->q,ctx)) goto err; - } - else - { - if (!BN_mod(r1,I,rsa->q,ctx)) goto err; - } + if (!BN_mod(r1, c, rsa->q, ctx)) + goto err; + } else { + if (!BN_mod(r1, I, rsa->q, ctx)) + goto err; + } /* compute r1^dmq1 mod q */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { dmq1 = &local_dmq1; BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME); - } - else + } else dmq1 = rsa->dmq1; - if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx, - rsa->_method_mod_q)) goto err; + if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, + rsa->_method_mod_q)) + goto err; /* compute I mod p */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { c = &local_c; BN_with_flags(c, I, BN_FLG_CONSTTIME); - if (!BN_mod(r1,c,rsa->p,ctx)) goto err; - } - else - { - if (!BN_mod(r1,I,rsa->p,ctx)) goto err; - } + if (!BN_mod(r1, c, rsa->p, ctx)) + goto err; + } else { + if (!BN_mod(r1, I, rsa->p, ctx)) + goto err; + } /* compute r1^dmp1 mod p */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { dmp1 = &local_dmp1; BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME); - } - else + } else dmp1 = rsa->dmp1; - if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx, - rsa->_method_mod_p)) goto err; + if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p,ctx, + rsa->_method_mod_p)) + goto err; - if (!BN_sub(r0,r0,m1)) goto err; - /* This will help stop the size of r0 increasing, which does - * affect the multiply if it optimised for a power of 2 size */ + if (!BN_sub(r0, r0, m1)) + goto err; + /* + * This will help stop the size of r0 increasing, which does + * affect the multiply if it optimised for a power of 2 size + */ if (BN_is_negative(r0)) - if (!BN_add(r0,r0,rsa->p)) goto err; + if (!BN_add(r0, r0, rsa->p)) + goto err; - if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err; + if (!BN_mul(r1, r0, rsa->iqmp, ctx)) + goto err; /* Turn BN_FLG_CONSTTIME flag on before division operation */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { pr1 = &local_r1; BN_with_flags(pr1, r1, BN_FLG_CONSTTIME); - } - else + } else pr1 = r1; - if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err; + if (!BN_mod(r0, pr1, rsa->p,ctx)) + goto err; - /* If p < q it is occasionally possible for the correction of + /* + * If p < q it is occasionally possible for the correction of * adding 'p' if r0 is negative above to leave the result still * negative. This can break the private key operations: the following * second correction should *always* correct this rare occurrence. @@ -849,62 +838,73 @@ static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) * they ensure p > q [steve] */ if (BN_is_negative(r0)) - if (!BN_add(r0,r0,rsa->p)) goto err; - if (!BN_mul(r1,r0,rsa->q,ctx)) goto err; - if (!BN_add(r0,r1,m1)) goto err; - - if (rsa->e && rsa->n) - { - if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err; - /* If 'I' was greater than (or equal to) rsa->n, the operation + if (!BN_add(r0, r0, rsa->p)) + goto err; + if (!BN_mul(r1, r0, rsa->q, ctx)) + goto err; + if (!BN_add(r0, r1, m1)) + goto err; + + if (rsa->e && rsa->n) { + if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx, + rsa->_method_mod_n)) + goto err; + /* + * If 'I' was greater than (or equal to) rsa->n, the operation * will be equivalent to using 'I mod n'. However, the result of * the verify will *always* be less than 'n' so we don't check - * for absolute equality, just congruency. */ - if (!BN_sub(vrfy, vrfy, I)) goto err; - if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err; + * for absolute equality, just congruency. + */ + if (!BN_sub(vrfy, vrfy, I)) + goto err; + if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) + goto err; if (BN_is_negative(vrfy)) - if (!BN_add(vrfy, vrfy, rsa->n)) goto err; - if (!BN_is_zero(vrfy)) - { - /* 'I' and 'vrfy' aren't congruent mod n. Don't leak + if (!BN_add(vrfy, vrfy, rsa->n)) + goto err; + if (!BN_is_zero(vrfy)) { + /* + * 'I' and 'vrfy' aren't congruent mod n. Don't leak * miscalculated CRT output, just do a raw (slower) - * mod_exp and return that instead. */ + * mod_exp and return that instead. + */ BIGNUM local_d; BIGNUM *d = NULL; - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { d = &local_d; BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); - } - else + } else d = rsa->d; - if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx, - rsa->_method_mod_n)) goto err; - } + if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, + rsa->_method_mod_n)) + goto err; } - ret=1; + } + ret = 1; err: BN_CTX_end(ctx); - return(ret); - } + return ret; +} -static int RSA_eay_init(RSA *rsa) - { - rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; - return(1); - } +static int +RSA_eay_init(RSA *rsa) +{ + rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE; + return 1 ; +} -static int RSA_eay_finish(RSA *rsa) - { +static int +RSA_eay_finish(RSA *rsa) +{ if (rsa->_method_mod_n != NULL) BN_MONT_CTX_free(rsa->_method_mod_n); if (rsa->_method_mod_p != NULL) BN_MONT_CTX_free(rsa->_method_mod_p); if (rsa->_method_mod_q != NULL) BN_MONT_CTX_free(rsa->_method_mod_q); - return(1); - } + return 1; +} #endif diff --git a/lib/libcrypto/rsa/rsa_gen.c b/lib/libcrypto/rsa/rsa_gen.c index 9745b6d6ed2..3a6aa1ca7a8 100644 --- a/lib/libcrypto/rsa/rsa_gen.c +++ b/lib/libcrypto/rsa/rsa_gen.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_gen.c,v 1.13 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_gen.c,v 1.14 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -70,150 +70,168 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb); -/* NB: this wrapper would normally be placed in rsa_lib.c and the static +/* + * NB: this wrapper would normally be placed in rsa_lib.c and the static * implementation would probably be in rsa_eay.c. Nonetheless, is kept here so * that we don't introduce a new linker dependency. Eg. any application that * wasn't previously linking object code related to key-generation won't have to - * now just because key-generation is part of RSA_METHOD. */ -int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) - { - if(rsa->meth->rsa_keygen) + * now just because key-generation is part of RSA_METHOD. + */ +int +RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) +{ + if (rsa->meth->rsa_keygen) return rsa->meth->rsa_keygen(rsa, bits, e_value, cb); return rsa_builtin_keygen(rsa, bits, e_value, cb); - } - -static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) - { - BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp; - BIGNUM local_r0,local_d,local_p; - BIGNUM *pr0,*d,*p; - int bitsp,bitsq,ok= -1,n=0; - BN_CTX *ctx=NULL; - - ctx=BN_CTX_new(); - if (ctx == NULL) goto err; +} + +static int +rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) +{ + BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp; + BIGNUM local_r0, local_d, local_p; + BIGNUM *pr0, *d, *p; + int bitsp, bitsq, ok = -1, n = 0; + BN_CTX *ctx = NULL; + + ctx = BN_CTX_new(); + if (ctx == NULL) + goto err; BN_CTX_start(ctx); r0 = BN_CTX_get(ctx); r1 = BN_CTX_get(ctx); r2 = BN_CTX_get(ctx); r3 = BN_CTX_get(ctx); - if (r3 == NULL) goto err; + if (r3 == NULL) + goto err; - bitsp=(bits+1)/2; - bitsq=bits-bitsp; + bitsp = (bits + 1) / 2; + bitsq = bits - bitsp; /* We need the RSA components non-NULL */ - if(!rsa->n && ((rsa->n=BN_new()) == NULL)) goto err; - if(!rsa->d && ((rsa->d=BN_new()) == NULL)) goto err; - if(!rsa->e && ((rsa->e=BN_new()) == NULL)) goto err; - if(!rsa->p && ((rsa->p=BN_new()) == NULL)) goto err; - if(!rsa->q && ((rsa->q=BN_new()) == NULL)) goto err; - if(!rsa->dmp1 && ((rsa->dmp1=BN_new()) == NULL)) goto err; - if(!rsa->dmq1 && ((rsa->dmq1=BN_new()) == NULL)) goto err; - if(!rsa->iqmp && ((rsa->iqmp=BN_new()) == NULL)) goto err; + if (!rsa->n && ((rsa->n = BN_new()) == NULL)) + goto err; + if (!rsa->d && ((rsa->d = BN_new()) == NULL)) + goto err; + if (!rsa->e && ((rsa->e = BN_new()) == NULL)) + goto err; + if (!rsa->p && ((rsa->p = BN_new()) == NULL)) + goto err; + if (!rsa->q && ((rsa->q = BN_new()) == NULL)) + goto err; + if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) + goto err; + if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) + goto err; + if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) + goto err; BN_copy(rsa->e, e_value); /* generate p and q */ - for (;;) - { - if(!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb)) + for (;;) { + if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb)) goto err; - if (!BN_sub(r2,rsa->p,BN_value_one())) goto err; - if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; - if (BN_is_one(r1)) break; - if(!BN_GENCB_call(cb, 2, n++)) + if (!BN_sub(r2, rsa->p, BN_value_one())) goto err; - } - if(!BN_GENCB_call(cb, 3, 0)) + if (!BN_gcd(r1, r2, rsa->e, ctx)) + goto err; + if (BN_is_one(r1)) + break; + if (!BN_GENCB_call(cb, 2, n++)) + goto err; + } + if (!BN_GENCB_call(cb, 3, 0)) goto err; - for (;;) - { - /* When generating ridiculously small keys, we can get stuck + for (;;) { + /* + * When generating ridiculously small keys, we can get stuck * continually regenerating the same prime values. Check for - * this and bail if it happens 3 times. */ + * this and bail if it happens 3 times. + */ unsigned int degenerate = 0; - do - { - if(!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb)) + do { + if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, + cb)) goto err; - } while((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3)); - if(degenerate == 3) - { + } while (BN_cmp(rsa->p, rsa->q) == 0 && + ++degenerate < 3); + if (degenerate == 3) { ok = 0; /* we set our own err */ - RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,RSA_R_KEY_SIZE_TOO_SMALL); + RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, + RSA_R_KEY_SIZE_TOO_SMALL); + goto err; + } + if (!BN_sub(r2, rsa->q, BN_value_one())) + goto err; + if (!BN_gcd(r1, r2, rsa->e, ctx)) goto err; - } - if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; - if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err; if (BN_is_one(r1)) break; - if(!BN_GENCB_call(cb, 2, n++)) + if (!BN_GENCB_call(cb, 2, n++)) goto err; - } - if(!BN_GENCB_call(cb, 3, 1)) + } + if (!BN_GENCB_call(cb, 3, 1)) goto err; - if (BN_cmp(rsa->p,rsa->q) < 0) - { - tmp=rsa->p; - rsa->p=rsa->q; - rsa->q=tmp; - } + if (BN_cmp(rsa->p,rsa->q) < 0) { + tmp = rsa->p; + rsa->p = rsa->q; + rsa->q = tmp; + } /* calculate n */ - if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err; + if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx)) + goto err; /* calculate d */ - if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */ - if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */ - if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!BN_sub(r1, rsa->p, BN_value_one())) /* p-1 */ + goto err; + if (!BN_sub(r2, rsa->q, BN_value_one())) /* q-1 */ + goto err; + if (!BN_mul(r0, r1, r2, ctx)) /* (p-1)(q-1) */ + goto err; + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { pr0 = &local_r0; BN_with_flags(pr0, r0, BN_FLG_CONSTTIME); - } - else - pr0 = r0; - if (!BN_mod_inverse(rsa->d,rsa->e,pr0,ctx)) goto err; /* d */ + } else + pr0 = r0; + if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) /* d */ + goto err; /* set up d for correct BN_FLG_CONSTTIME flag */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { d = &local_d; BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME); - } - else + } else d = rsa->d; /* calculate d mod (p-1) */ - if (!BN_mod(rsa->dmp1,d,r1,ctx)) goto err; + if (!BN_mod(rsa->dmp1, d, r1, ctx)) + goto err; /* calculate d mod (q-1) */ - if (!BN_mod(rsa->dmq1,d,r2,ctx)) goto err; + if (!BN_mod(rsa->dmq1, d, r2, ctx)) + goto err; /* calculate inverse of q mod p */ - if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) - { + if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) { p = &local_p; BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME); - } - else + } else p = rsa->p; - if (!BN_mod_inverse(rsa->iqmp,rsa->q,p,ctx)) goto err; + if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) + goto err; - ok=1; + ok = 1; err: - if (ok == -1) - { - RSAerr(RSA_F_RSA_BUILTIN_KEYGEN,ERR_LIB_BN); - ok=0; - } - if (ctx != NULL) - { + if (ok == -1) { + RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN); + ok = 0; + } + if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); - } - - return ok; } + return ok; +} diff --git a/lib/libcrypto/rsa/rsa_lib.c b/lib/libcrypto/rsa/rsa_lib.c index 05eb53cc83d..054690cee61 100644 --- a/lib/libcrypto/rsa/rsa_lib.c +++ b/lib/libcrypto/rsa/rsa_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_lib.c,v 1.20 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_lib.c,v 1.21 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -67,148 +67,150 @@ #include <openssl/engine.h> #endif -const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT; +const char RSA_version[] = "RSA" OPENSSL_VERSION_PTEXT; -static const RSA_METHOD *default_RSA_meth=NULL; +static const RSA_METHOD *default_RSA_meth = NULL; -RSA *RSA_new(void) - { - RSA *r=RSA_new_method(NULL); +RSA * +RSA_new(void) +{ + RSA *r = RSA_new_method(NULL); return r; - } +} -void RSA_set_default_method(const RSA_METHOD *meth) - { +void +RSA_set_default_method(const RSA_METHOD *meth) +{ default_RSA_meth = meth; - } +} -const RSA_METHOD *RSA_get_default_method(void) - { - if (default_RSA_meth == NULL) - { +const RSA_METHOD * +RSA_get_default_method(void) +{ + if (default_RSA_meth == NULL) { #ifdef RSA_NULL - default_RSA_meth=RSA_null_method(); + default_RSA_meth = RSA_null_method(); #else - default_RSA_meth=RSA_PKCS1_SSLeay(); + default_RSA_meth = RSA_PKCS1_SSLeay(); #endif - } + } return default_RSA_meth; - } +} -const RSA_METHOD *RSA_get_method(const RSA *rsa) - { +const RSA_METHOD * +RSA_get_method(const RSA *rsa) +{ return rsa->meth; - } +} -int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) - { - /* NB: The caller is specifically setting a method, so it's not up to us - * to deal with which ENGINE it comes from. */ +int +RSA_set_method(RSA *rsa, const RSA_METHOD *meth) +{ + /* + * NB: The caller is specifically setting a method, so it's not up to us + * to deal with which ENGINE it comes from. + */ const RSA_METHOD *mtmp; + mtmp = rsa->meth; - if (mtmp->finish) mtmp->finish(rsa); + if (mtmp->finish) + mtmp->finish(rsa); #ifndef OPENSSL_NO_ENGINE - if (rsa->engine) - { + if (rsa->engine) { ENGINE_finish(rsa->engine); rsa->engine = NULL; - } + } #endif rsa->meth = meth; - if (meth->init) meth->init(rsa); + if (meth->init) + meth->init(rsa); return 1; - } +} -RSA *RSA_new_method(ENGINE *engine) - { +RSA * +RSA_new_method(ENGINE *engine) +{ RSA *ret; ret = malloc(sizeof(RSA)); - if (ret == NULL) - { - RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE); + if (ret == NULL) { + RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); return NULL; - } + } ret->meth = RSA_get_default_method(); #ifndef OPENSSL_NO_ENGINE - if (engine) - { - if (!ENGINE_init(engine)) - { + if (engine) { + if (!ENGINE_init(engine)) { RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); free(ret); return NULL; - } - ret->engine = engine; } - else + ret->engine = engine; + } else ret->engine = ENGINE_get_default_RSA(); - if(ret->engine) - { + if(ret->engine) { ret->meth = ENGINE_get_RSA(ret->engine); - if(!ret->meth) - { - RSAerr(RSA_F_RSA_NEW_METHOD, - ERR_R_ENGINE_LIB); + if (!ret->meth) { + RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); ENGINE_finish(ret->engine); free(ret); return NULL; - } } + } #endif - ret->pad=0; - ret->version=0; - ret->n=NULL; - ret->e=NULL; - ret->d=NULL; - ret->p=NULL; - ret->q=NULL; - ret->dmp1=NULL; - ret->dmq1=NULL; - ret->iqmp=NULL; - ret->references=1; - ret->_method_mod_n=NULL; - ret->_method_mod_p=NULL; - ret->_method_mod_q=NULL; - ret->blinding=NULL; - ret->mt_blinding=NULL; - ret->bignum_data=NULL; - ret->flags=ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; - if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) - { + ret->pad = 0; + ret->version = 0; + ret->n = NULL; + ret->e = NULL; + ret->d = NULL; + ret->p = NULL; + ret->q = NULL; + ret->dmp1 = NULL; + ret->dmq1 = NULL; + ret->iqmp = NULL; + ret->references = 1; + ret->_method_mod_n = NULL; + ret->_method_mod_p = NULL; + ret->_method_mod_q = NULL; + ret->blinding = NULL; + ret->mt_blinding = NULL; + ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; + if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { #ifndef OPENSSL_NO_ENGINE - if (ret->engine) - ENGINE_finish(ret->engine); + if (ret->engine) + ENGINE_finish(ret->engine); #endif free(ret); - return(NULL); - } + return NULL; + } - if ((ret->meth->init != NULL) && !ret->meth->init(ret)) - { + if (ret->meth->init != NULL && !ret->meth->init(ret)) { #ifndef OPENSSL_NO_ENGINE if (ret->engine) ENGINE_finish(ret->engine); #endif CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); free(ret); - ret=NULL; - } - return(ret); + ret = NULL; } + return ret; +} -void RSA_free(RSA *r) - { +void +RSA_free(RSA *r) +{ int i; - if (r == NULL) return; + if (r == NULL) + return; - i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA); - if (i > 0) return; + i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA); + if (i > 0) + return; if (r->meth->finish) r->meth->finish(r); @@ -219,42 +221,55 @@ void RSA_free(RSA *r) CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); - if (r->n != NULL) BN_clear_free(r->n); - if (r->e != NULL) BN_clear_free(r->e); - if (r->d != NULL) BN_clear_free(r->d); - if (r->p != NULL) BN_clear_free(r->p); - if (r->q != NULL) BN_clear_free(r->q); - if (r->dmp1 != NULL) BN_clear_free(r->dmp1); - if (r->dmq1 != NULL) BN_clear_free(r->dmq1); - if (r->iqmp != NULL) BN_clear_free(r->iqmp); - if (r->blinding != NULL) BN_BLINDING_free(r->blinding); - if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding); - free(r->bignum_data); + if (r->n != NULL) + BN_clear_free(r->n); + if (r->e != NULL) + BN_clear_free(r->e); + if (r->d != NULL) + BN_clear_free(r->d); + if (r->p != NULL) + BN_clear_free(r->p); + if (r->q != NULL) + BN_clear_free(r->q); + if (r->dmp1 != NULL) + BN_clear_free(r->dmp1); + if (r->dmq1 != NULL) + BN_clear_free(r->dmq1); + if (r->iqmp != NULL) + BN_clear_free(r->iqmp); + if (r->blinding != NULL) + BN_BLINDING_free(r->blinding); + if (r->mt_blinding != NULL) + BN_BLINDING_free(r->mt_blinding); free(r); - } +} -int RSA_up_ref(RSA *r) - { +int +RSA_up_ref(RSA *r) +{ int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA); - return ((i > 1) ? 1 : 0); - } + return i > 1 ? 1 : 0; +} -int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, - CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) - { +int +RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, + CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) +{ return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, - new_func, dup_func, free_func); - } + new_func, dup_func, free_func); +} -int RSA_set_ex_data(RSA *r, int idx, void *arg) - { - return(CRYPTO_set_ex_data(&r->ex_data,idx,arg)); - } +int +RSA_set_ex_data(RSA *r, int idx, void *arg) +{ + return CRYPTO_set_ex_data(&r->ex_data, idx, arg); +} -void *RSA_get_ex_data(const RSA *r, int idx) - { - return(CRYPTO_get_ex_data(&r->ex_data,idx)); - } +void * +RSA_get_ex_data(const RSA *r, int idx) +{ + return CRYPTO_get_ex_data(&r->ex_data, idx); +} int RSA_memory_lock(RSA *r) { diff --git a/lib/libcrypto/rsa/rsa_none.c b/lib/libcrypto/rsa/rsa_none.c index 1904c7c0987..fde5eb6ef66 100644 --- a/lib/libcrypto/rsa/rsa_none.c +++ b/lib/libcrypto/rsa/rsa_none.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_none.c,v 1.4 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_none.c,v 1.5 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -62,37 +62,36 @@ #include <openssl/rsa.h> #include <openssl/rand.h> -int RSA_padding_add_none(unsigned char *to, int tlen, - const unsigned char *from, int flen) - { - if (flen > tlen) - { - RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); - return(0); - } - - if (flen < tlen) - { - RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); - return(0); - } - - memcpy(to,from,(unsigned int)flen); - return(1); +int +RSA_padding_add_none(unsigned char *to, int tlen, const unsigned char *from, + int flen) +{ + if (flen > tlen) { + RSAerr(RSA_F_RSA_PADDING_ADD_NONE, + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + return 0; } -int RSA_padding_check_none(unsigned char *to, int tlen, - const unsigned char *from, int flen, int num) - { - - if (flen > tlen) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_DATA_TOO_LARGE); - return(-1); - } + if (flen < tlen) { + RSAerr(RSA_F_RSA_PADDING_ADD_NONE, + RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE); + return 0; + } + + memcpy(to, from, (unsigned int)flen); + return 1; +} - memset(to,0,tlen-flen); - memcpy(to+tlen-flen,from,flen); - return(tlen); +int +RSA_padding_check_none(unsigned char *to, int tlen, const unsigned char *from, + int flen, int num) +{ + if (flen > tlen) { + RSAerr(RSA_F_RSA_PADDING_CHECK_NONE, RSA_R_DATA_TOO_LARGE); + return -1; } + memset(to, 0, tlen - flen); + memcpy(to + tlen - flen, from, flen); + return tlen; +} diff --git a/lib/libcrypto/rsa/rsa_null.c b/lib/libcrypto/rsa/rsa_null.c index c4404fbe442..54f1cabc743 100644 --- a/lib/libcrypto/rsa/rsa_null.c +++ b/lib/libcrypto/rsa/rsa_null.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_null.c,v 1.6 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_null.c,v 1.7 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 1999. */ @@ -62,7 +62,8 @@ #include <openssl/rsa.h> #include <openssl/rand.h> -/* This is a dummy RSA implementation that just returns errors when called. +/* + * This is a dummy RSA implementation that just returns errors when called. * It is designed to allow some RSA functions to work while stopping those * covered by the RSA patent. That is RSA, encryption, decryption, signing * and verify is not allowed but RSA key generation, key checking and other @@ -92,53 +93,65 @@ static RSA_METHOD rsa_null_meth = { .finish = RSA_null_finish, }; -const RSA_METHOD *RSA_null_method(void) - { - return(&rsa_null_meth); - } +const RSA_METHOD * +RSA_null_method(void) +{ + return &rsa_null_meth; +} -static int RSA_null_public_encrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { - RSAerr(RSA_F_RSA_NULL_PUBLIC_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); +static int +RSA_null_public_encrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + RSAerr(RSA_F_RSA_NULL_PUBLIC_ENCRYPT, + RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); return -1; - } +} -static int RSA_null_private_encrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { - RSAerr(RSA_F_RSA_NULL_PRIVATE_ENCRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); +static int +RSA_null_private_encrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + RSAerr(RSA_F_RSA_NULL_PRIVATE_ENCRYPT, + RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); return -1; - } +} -static int RSA_null_private_decrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { - RSAerr(RSA_F_RSA_NULL_PRIVATE_DECRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); +static int +RSA_null_private_decrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + RSAerr(RSA_F_RSA_NULL_PRIVATE_DECRYPT, + RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); return -1; - } +} -static int RSA_null_public_decrypt(int flen, const unsigned char *from, - unsigned char *to, RSA *rsa, int padding) - { - RSAerr(RSA_F_RSA_NULL_PUBLIC_DECRYPT, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); +static int +RSA_null_public_decrypt(int flen, const unsigned char *from, unsigned char *to, + RSA *rsa, int padding) +{ + RSAerr(RSA_F_RSA_NULL_PUBLIC_DECRYPT, + RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); return -1; - } +} #if 0 /* not currently used */ -static int RSA_null_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) - { +static int +RSA_null_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa) +{ ...err(RSA_F_RSA_NULL_MOD_EXP, RSA_R_RSA_OPERATIONS_NOT_SUPPORTED); return -1; - } +} #endif -static int RSA_null_init(RSA *rsa) - { - return(1); - } +static int +RSA_null_init(RSA *rsa) +{ + return 1; +} -static int RSA_null_finish(RSA *rsa) - { - return(1); - } +static int +RSA_null_finish(RSA *rsa) +{ + return 1; +} diff --git a/lib/libcrypto/rsa/rsa_oaep.c b/lib/libcrypto/rsa/rsa_oaep.c index 695df4e0a01..f8f9935fac5 100644 --- a/lib/libcrypto/rsa/rsa_oaep.c +++ b/lib/libcrypto/rsa/rsa_oaep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_oaep.c,v 1.16 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_oaep.c,v 1.17 2014/07/09 08:20:08 miod Exp $ */ /* Written by Ulf Moeller. This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */ @@ -28,29 +28,28 @@ #include <openssl/rand.h> #include <openssl/sha.h> -static int MGF1(unsigned char *mask, long len, - const unsigned char *seed, long seedlen); +static int MGF1(unsigned char *mask, long len, const unsigned char *seed, + long seedlen); -int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, - const unsigned char *from, int flen, - const unsigned char *param, int plen) - { +int +RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, + const unsigned char *from, int flen, const unsigned char *param, int plen) +{ int i, emlen = tlen - 1; unsigned char *db, *seed; unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH]; - if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) - { + if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) { RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, - RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); return 0; - } + } - if (emlen < 2 * SHA_DIGEST_LENGTH + 1) - { - RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL); + if (emlen < 2 * SHA_DIGEST_LENGTH + 1) { + RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, + RSA_R_KEY_SIZE_TOO_SMALL); return 0; - } + } to[0] = 0; seed = to + 1; @@ -59,9 +58,9 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL)) return 0; memset(db + SHA_DIGEST_LENGTH, 0, - emlen - flen - 2 * SHA_DIGEST_LENGTH - 1); + emlen - flen - 2 * SHA_DIGEST_LENGTH - 1); db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01; - memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen); + memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int)flen); if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0) return 0; #ifdef PKCS_TESTVECT @@ -71,65 +70,73 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, #endif dbmask = malloc(emlen - SHA_DIGEST_LENGTH); - if (dbmask == NULL) - { + if (dbmask == NULL) { RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); return 0; - } + } - if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH) < 0) + if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, + SHA_DIGEST_LENGTH) < 0) return 0; for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++) db[i] ^= dbmask[i]; - if (MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH) < 0) + if (MGF1(seedmask, SHA_DIGEST_LENGTH, db, + emlen - SHA_DIGEST_LENGTH) < 0) return 0; for (i = 0; i < SHA_DIGEST_LENGTH; i++) seed[i] ^= seedmask[i]; free(dbmask); return 1; - } +} -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) - { +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) +{ int i, dblen, mlen = -1; const unsigned char *maskeddb; int lzero; - unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH]; + unsigned char *db = NULL; + unsigned char seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH]; unsigned char *padded_from; int bad = 0; if (--num < 2 * SHA_DIGEST_LENGTH + 1) - /* 'num' is the length of the modulus, i.e. does not depend on the - * particular ciphertext. */ + /* + * 'num' is the length of the modulus, i.e. does not depend + * on the particular ciphertext. + */ goto decoding_err; lzero = num - flen; - if (lzero < 0) - { - /* signalling this error immediately after detection might allow + if (lzero < 0) { + /* + * signalling this error immediately after detection might allow * for side-channel attacks (e.g. timing if 'plen' is huge - * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal - * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001), - * so we use a 'bad' flag */ + * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA + * Optimal Asymmetric Encryption Padding (OAEP) [...]", + * CRYPTO 2001), so we use a 'bad' flag + */ bad = 1; lzero = 0; flen = num; /* don't overflow the memcpy to padded_from */ - } + } dblen = num - SHA_DIGEST_LENGTH; db = malloc(dblen + num); - if (db == NULL) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE); + if (db == NULL) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, + ERR_R_MALLOC_FAILURE); return -1; - } + } - /* Always do this zero-padding copy (even when lzero == 0) - * to avoid leaking timing info about the value of lzero. */ + /* + * Always do this zero-padding copy (even when lzero == 0) + * to avoid leaking timing info about the value of lzero. + */ padded_from = db + dblen; memset(padded_from, 0, lzero); memcpy(padded_from + lzero, from, flen); @@ -151,41 +158,41 @@ int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, if (CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad) goto decoding_err; - else - { + else { for (i = SHA_DIGEST_LENGTH; i < dblen; i++) if (db[i] != 0x00) break; if (i == dblen || db[i] != 0x01) goto decoding_err; - else - { + else { /* everything looks OK */ mlen = dblen - ++i; - if (tlen < mlen) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE); + if (tlen < mlen) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, + RSA_R_DATA_TOO_LARGE); mlen = -1; - } - else + } else memcpy(to, db + i, mlen); - } } + } free(db); return mlen; decoding_err: - /* to avoid chosen ciphertext attacks, the error message should not reveal - * which kind of decoding error happened */ + /* + * To avoid chosen ciphertext attacks, the error message should not + * reveal which kind of decoding error happened + */ RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR); free(db); return -1; - } +} -int PKCS1_MGF1(unsigned char *mask, long len, - const unsigned char *seed, long seedlen, const EVP_MD *dgst) - { +int +PKCS1_MGF1(unsigned char *mask, long len, const unsigned char *seed, + long seedlen, const EVP_MD *dgst) +{ long i, outlen = 0; unsigned char cnt[4]; EVP_MD_CTX c; @@ -197,39 +204,35 @@ int PKCS1_MGF1(unsigned char *mask, long len, mdlen = EVP_MD_size(dgst); if (mdlen < 0) goto err; - for (i = 0; outlen < len; i++) - { + for (i = 0; outlen < len; i++) { cnt[0] = (unsigned char)((i >> 24) & 255); cnt[1] = (unsigned char)((i >> 16) & 255); cnt[2] = (unsigned char)((i >> 8)) & 255; cnt[3] = (unsigned char)(i & 255); - if (!EVP_DigestInit_ex(&c,dgst, NULL) - || !EVP_DigestUpdate(&c, seed, seedlen) - || !EVP_DigestUpdate(&c, cnt, 4)) + if (!EVP_DigestInit_ex(&c,dgst, NULL) || + !EVP_DigestUpdate(&c, seed, seedlen) || + !EVP_DigestUpdate(&c, cnt, 4)) goto err; - if (outlen + mdlen <= len) - { + if (outlen + mdlen <= len) { if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL)) goto err; outlen += mdlen; - } - else - { + } else { if (!EVP_DigestFinal_ex(&c, md, NULL)) goto err; memcpy(mask + outlen, md, len - outlen); outlen = len; - } } + } rv = 0; - err: +err: EVP_MD_CTX_cleanup(&c); return rv; - } +} -static int MGF1(unsigned char *mask, long len, const unsigned char *seed, - long seedlen) - { +static int +MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen) +{ return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1()); - } +} #endif diff --git a/lib/libcrypto/rsa/rsa_pk1.c b/lib/libcrypto/rsa/rsa_pk1.c index 36133b6d125..f5492315cce 100644 --- a/lib/libcrypto/rsa/rsa_pk1.c +++ b/lib/libcrypto/rsa/rsa_pk1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_pk1.c,v 1.7 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_pk1.c,v 1.8 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -62,160 +62,163 @@ #include <openssl/rsa.h> #include <openssl/rand.h> -int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, - const unsigned char *from, int flen) - { +int +RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen, + const unsigned char *from, int flen) +{ int j; unsigned char *p; - if (flen > (tlen-RSA_PKCS1_PADDING_SIZE)) - { - RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); - return(0); - } + if (flen > (tlen-RSA_PKCS1_PADDING_SIZE)) { + RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1, + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + return 0; + } - p=(unsigned char *)to; + p = (unsigned char *)to; - *(p++)=0; - *(p++)=1; /* Private Key BT (Block Type) */ + *(p++) = 0; + *(p++) = 1; /* Private Key BT (Block Type) */ /* pad out with 0xff data */ - j=tlen-3-flen; - memset(p,0xff,j); - p+=j; - *(p++)='\0'; - memcpy(p,from,(unsigned int)flen); - return(1); - } - -int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, - const unsigned char *from, int flen, int num) - { - int i,j; + j = tlen - 3 - flen; + memset(p, 0xff, j); + p += j; + *(p++) = '\0'; + memcpy(p, from, (unsigned int)flen); + return 1; +} + +int +RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen, + const unsigned char *from, int flen, int num) +{ + int i, j; const unsigned char *p; - p=from; - if ((num != (flen+1)) || (*(p++) != 01)) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01); - return(-1); - } + p = from; + if (num != flen + 1 || *(p++) != 01) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, + RSA_R_BLOCK_TYPE_IS_NOT_01); + return -1; + } /* scan over padding data */ - j=flen-1; /* one for type. */ - for (i=0; i<j; i++) - { - if (*p != 0xff) /* should decrypt to 0xff */ - { - if (*p == 0) - { p++; break; } - else { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT); - return(-1); - } + j = flen - 1; /* one for type. */ + for (i = 0; i < j; i++) { + if (*p != 0xff) { /* should decrypt to 0xff */ + if (*p == 0) { + p++; + break; + } else { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, + RSA_R_BAD_FIXED_HEADER_DECRYPT); + return -1; } - p++; } + p++; + } - if (i == j) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING); - return(-1); - } + if (i == j) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, + RSA_R_NULL_BEFORE_BLOCK_MISSING); + return -1; + } - if (i < 8) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT); - return(-1); - } + if (i < 8) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, + RSA_R_BAD_PAD_BYTE_COUNT); + return -1; + } i++; /* Skip over the '\0' */ - j-=i; - if (j > tlen) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE); - return(-1); - } - memcpy(to,p,(unsigned int)j); - - return(j); + j -= i; + if (j > tlen) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, + RSA_R_DATA_TOO_LARGE); + return -1; } + memcpy(to, p, (unsigned int)j); -int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, - const unsigned char *from, int flen) - { + return j; +} + +int +RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen, + const unsigned char *from, int flen) +{ int i,j; unsigned char *p; - if (flen > (tlen-11)) - { - RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); - return(0); - } + if (flen > tlen - 11) { + RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2, + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + return 0; + } - p=(unsigned char *)to; + p = (unsigned char *)to; - *(p++)=0; - *(p++)=2; /* Public Key BT (Block Type) */ + *(p++) = 0; + *(p++) = 2; /* Public Key BT (Block Type) */ /* pad out with non-zero random data */ - j=tlen-3-flen; + j = tlen - 3 - flen; - if (RAND_bytes(p,j) <= 0) - return(0); - for (i=0; i<j; i++) - { + if (RAND_bytes(p, j) <= 0) + return 0; + for (i = 0; i < j; i++) { if (*p == '\0') - do { - if (RAND_bytes(p,1) <= 0) - return(0); - } while (*p == '\0'); + do { + if (RAND_bytes(p, 1) <= 0) + return 0; + } while (*p == '\0'); p++; - } + } - *(p++)='\0'; + *(p++) = '\0'; - memcpy(p,from,(unsigned int)flen); - return(1); - } + memcpy(p, from, (unsigned int)flen); + return 1; +} -int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, - const unsigned char *from, int flen, int num) - { - int i,j; +int +RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen, + const unsigned char *from, int flen, int num) +{ + int i, j; const unsigned char *p; - p=from; - if ((num != (flen+1)) || (*(p++) != 02)) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BLOCK_TYPE_IS_NOT_02); - return(-1); - } + p = from; + if (num != flen + 1 || *(p++) != 02) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, + RSA_R_BLOCK_TYPE_IS_NOT_02); + return -1; + } /* scan over padding data */ - j=flen-1; /* one for type. */ - for (i=0; i<j; i++) - if (*(p++) == 0) break; - - if (i == j) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_NULL_BEFORE_BLOCK_MISSING); - return(-1); - } + j = flen - 1; /* one for type. */ + for (i = 0; i < j; i++) + if (*(p++) == 0) + break; + + if (i == j) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, + RSA_R_NULL_BEFORE_BLOCK_MISSING); + return -1; + } - if (i < 8) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BAD_PAD_BYTE_COUNT); - return(-1); - } + if (i < 8) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, + RSA_R_BAD_PAD_BYTE_COUNT); + return -1; + } i++; /* Skip over the '\0' */ - j-=i; - if (j > tlen) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE); - return(-1); - } - memcpy(to,p,(unsigned int)j); - - return(j); + j -= i; + if (j > tlen) { + RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, + RSA_R_DATA_TOO_LARGE); + return -1; } + memcpy(to, p, (unsigned int)j); + return j; +} diff --git a/lib/libcrypto/rsa/rsa_pmeth.c b/lib/libcrypto/rsa/rsa_pmeth.c index 1f9d826014d..4a662c2894e 100644 --- a/lib/libcrypto/rsa/rsa_pmeth.c +++ b/lib/libcrypto/rsa/rsa_pmeth.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_pmeth.c,v 1.8 2014/06/12 20:40:57 deraadt Exp $ */ +/* $OpenBSD: rsa_pmeth.c,v 1.9 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ @@ -72,8 +72,7 @@ /* RSA pkey context structure */ -typedef struct - { +typedef struct { /* Key gen parameters */ int nbits; BIGNUM *pub_exp; @@ -89,11 +88,13 @@ typedef struct int saltlen; /* Temp buffer */ unsigned char *tbuf; - } RSA_PKEY_CTX; +} RSA_PKEY_CTX; -static int pkey_rsa_init(EVP_PKEY_CTX *ctx) - { +static int +pkey_rsa_init(EVP_PKEY_CTX *ctx) +{ RSA_PKEY_CTX *rctx; + rctx = malloc(sizeof(RSA_PKEY_CTX)); if (!rctx) return 0; @@ -111,424 +112,390 @@ static int pkey_rsa_init(EVP_PKEY_CTX *ctx) ctx->keygen_info_count = 2; return 1; - } +} -static int pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) - { +static int +pkey_rsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) +{ RSA_PKEY_CTX *dctx, *sctx; + if (!pkey_rsa_init(dst)) return 0; sctx = src->data; dctx = dst->data; dctx->nbits = sctx->nbits; - if (sctx->pub_exp) - { + if (sctx->pub_exp) { dctx->pub_exp = BN_dup(sctx->pub_exp); if (!dctx->pub_exp) return 0; - } + } dctx->pad_mode = sctx->pad_mode; dctx->md = sctx->md; return 1; - } +} -static int setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) - { +static int +setup_tbuf(RSA_PKEY_CTX *ctx, EVP_PKEY_CTX *pk) +{ if (ctx->tbuf) return 1; ctx->tbuf = malloc(EVP_PKEY_size(pk->pkey)); if (!ctx->tbuf) return 0; return 1; - } +} -static void pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) - { +static void +pkey_rsa_cleanup(EVP_PKEY_CTX *ctx) +{ RSA_PKEY_CTX *rctx = ctx->data; - if (rctx) - { + + if (rctx) { if (rctx->pub_exp) BN_free(rctx->pub_exp); free(rctx->tbuf); free(rctx); - } } -static int pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, - const unsigned char *tbs, size_t tbslen) - { +} + +static int +pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, + const unsigned char *tbs, size_t tbslen) +{ int ret; RSA_PKEY_CTX *rctx = ctx->data; RSA *rsa = ctx->pkey->pkey.rsa; - if (rctx->md) - { - if (tbslen != (size_t)EVP_MD_size(rctx->md)) - { + if (rctx->md) { + if (tbslen != (size_t)EVP_MD_size(rctx->md)) { RSAerr(RSA_F_PKEY_RSA_SIGN, - RSA_R_INVALID_DIGEST_LENGTH); + RSA_R_INVALID_DIGEST_LENGTH); return -1; - } + } - if (EVP_MD_type(rctx->md) == NID_mdc2) - { + if (EVP_MD_type(rctx->md) == NID_mdc2) { unsigned int sltmp; + if (rctx->pad_mode != RSA_PKCS1_PADDING) return -1; - ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, - tbs, tbslen, sig, &sltmp, rsa); + ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, tbs, tbslen, + sig, &sltmp, rsa); if (ret <= 0) return ret; ret = sltmp; - } - else if (rctx->pad_mode == RSA_X931_PADDING) - { + } else if (rctx->pad_mode == RSA_X931_PADDING) { if (!setup_tbuf(rctx, ctx)) return -1; memcpy(rctx->tbuf, tbs, tbslen); rctx->tbuf[tbslen] = - RSA_X931_hash_id(EVP_MD_type(rctx->md)); - ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, - sig, rsa, RSA_X931_PADDING); - } - else if (rctx->pad_mode == RSA_PKCS1_PADDING) - { + RSA_X931_hash_id(EVP_MD_type(rctx->md)); + ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, sig, + rsa, RSA_X931_PADDING); + } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { unsigned int sltmp; - ret = RSA_sign(EVP_MD_type(rctx->md), - tbs, tbslen, sig, &sltmp, rsa); + + ret = RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig, + &sltmp, rsa); if (ret <= 0) return ret; ret = sltmp; - } - else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) - { + } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { if (!setup_tbuf(rctx, ctx)) return -1; - if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, - rctx->tbuf, tbs, - rctx->md, rctx->mgf1md, - rctx->saltlen)) + if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf, + tbs, rctx->md, rctx->mgf1md, rctx->saltlen)) return -1; ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf, - sig, rsa, RSA_NO_PADDING); - } - else + sig, rsa, RSA_NO_PADDING); + } else return -1; - } - else + } else ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa, - rctx->pad_mode); + rctx->pad_mode); if (ret < 0) return ret; *siglen = ret; return 1; - } - +} -static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx, - unsigned char *rout, size_t *routlen, - const unsigned char *sig, size_t siglen) - { +static int +pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx, unsigned char *rout, size_t *routlen, + const unsigned char *sig, size_t siglen) +{ int ret; RSA_PKEY_CTX *rctx = ctx->data; - if (rctx->md) - { - if (rctx->pad_mode == RSA_X931_PADDING) - { + if (rctx->md) { + if (rctx->pad_mode == RSA_X931_PADDING) { if (!setup_tbuf(rctx, ctx)) return -1; - ret = RSA_public_decrypt(siglen, sig, - rctx->tbuf, ctx->pkey->pkey.rsa, - RSA_X931_PADDING); + ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, + ctx->pkey->pkey.rsa, RSA_X931_PADDING); if (ret < 1) return 0; ret--; if (rctx->tbuf[ret] != - RSA_X931_hash_id(EVP_MD_type(rctx->md))) - { + RSA_X931_hash_id(EVP_MD_type(rctx->md))) { RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, - RSA_R_ALGORITHM_MISMATCH); + RSA_R_ALGORITHM_MISMATCH); return 0; - } - if (ret != EVP_MD_size(rctx->md)) - { + } + if (ret != EVP_MD_size(rctx->md)) { RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER, - RSA_R_INVALID_DIGEST_LENGTH); + RSA_R_INVALID_DIGEST_LENGTH); return 0; - } + } if (rout) memcpy(rout, rctx->tbuf, ret); - } - else if (rctx->pad_mode == RSA_PKCS1_PADDING) - { + } else if (rctx->pad_mode == RSA_PKCS1_PADDING) { size_t sltmp; - ret = int_rsa_verify(EVP_MD_type(rctx->md), - NULL, 0, rout, &sltmp, - sig, siglen, ctx->pkey->pkey.rsa); + + ret = int_rsa_verify(EVP_MD_type(rctx->md), NULL, 0, + rout, &sltmp, sig, siglen, ctx->pkey->pkey.rsa); if (ret <= 0) return 0; ret = sltmp; - } - else + } else return -1; - } - else + } else ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa, - rctx->pad_mode); + rctx->pad_mode); if (ret < 0) return ret; *routlen = ret; return 1; - } +} -static int pkey_rsa_verify(EVP_PKEY_CTX *ctx, - const unsigned char *sig, size_t siglen, - const unsigned char *tbs, size_t tbslen) - { +static int +pkey_rsa_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen, + const unsigned char *tbs, size_t tbslen) +{ RSA_PKEY_CTX *rctx = ctx->data; RSA *rsa = ctx->pkey->pkey.rsa; size_t rslen; - if (rctx->md) - { + + if (rctx->md) { if (rctx->pad_mode == RSA_PKCS1_PADDING) return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen, - sig, siglen, rsa); - if (rctx->pad_mode == RSA_X931_PADDING) - { - if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, - sig, siglen) <= 0) + sig, siglen, rsa); + if (rctx->pad_mode == RSA_X931_PADDING) { + if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig, + siglen) <= 0) return 0; - } - else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) - { + } else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) { int ret; + if (!setup_tbuf(rctx, ctx)) return -1; ret = RSA_public_decrypt(siglen, sig, rctx->tbuf, - rsa, RSA_NO_PADDING); + rsa, RSA_NO_PADDING); if (ret <= 0) return 0; - ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, - rctx->md, rctx->mgf1md, - rctx->tbuf, rctx->saltlen); + ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md, + rctx->mgf1md, rctx->tbuf, rctx->saltlen); if (ret <= 0) return 0; return 1; - } - else + } else return -1; - } - else - { + } else { if (!setup_tbuf(rctx, ctx)) return -1; - rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, - rsa, rctx->pad_mode); + rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa, + rctx->pad_mode); if (rslen == 0) return 0; - } + } - if ((rslen != tbslen) || memcmp(tbs, rctx->tbuf, rslen)) + if (rslen != tbslen || memcmp(tbs, rctx->tbuf, rslen)) return 0; return 1; - - } - +} -static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, - unsigned char *out, size_t *outlen, - const unsigned char *in, size_t inlen) - { +static int +pkey_rsa_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, + const unsigned char *in, size_t inlen) +{ int ret; RSA_PKEY_CTX *rctx = ctx->data; + ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa, - rctx->pad_mode); + rctx->pad_mode); if (ret < 0) return ret; *outlen = ret; return 1; - } +} -static int pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, - unsigned char *out, size_t *outlen, - const unsigned char *in, size_t inlen) - { +static int +pkey_rsa_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, + const unsigned char *in, size_t inlen) +{ int ret; RSA_PKEY_CTX *rctx = ctx->data; + ret = RSA_private_decrypt(inlen, in, out, ctx->pkey->pkey.rsa, - rctx->pad_mode); + rctx->pad_mode); if (ret < 0) return ret; *outlen = ret; return 1; - } +} -static int check_padding_md(const EVP_MD *md, int padding) - { +static int +check_padding_md(const EVP_MD *md, int padding) +{ if (!md) return 1; - if (padding == RSA_NO_PADDING) - { + if (padding == RSA_NO_PADDING) { RSAerr(RSA_F_CHECK_PADDING_MD, RSA_R_INVALID_PADDING_MODE); return 0; - } + } - if (padding == RSA_X931_PADDING) - { - if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) - { + if (padding == RSA_X931_PADDING) { + if (RSA_X931_hash_id(EVP_MD_type(md)) == -1) { RSAerr(RSA_F_CHECK_PADDING_MD, - RSA_R_INVALID_X931_DIGEST); + RSA_R_INVALID_X931_DIGEST); return 0; - } - return 1; } + return 1; + } return 1; - } - +} -static int pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) - { +static int +pkey_rsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) +{ RSA_PKEY_CTX *rctx = ctx->data; - switch (type) - { - case EVP_PKEY_CTRL_RSA_PADDING: - if ((p1 >= RSA_PKCS1_PADDING) && (p1 <= RSA_PKCS1_PSS_PADDING)) - { + switch (type) { + case EVP_PKEY_CTRL_RSA_PADDING: + if (p1 >= RSA_PKCS1_PADDING && p1 <= RSA_PKCS1_PSS_PADDING) { if (!check_padding_md(rctx->md, p1)) return 0; - if (p1 == RSA_PKCS1_PSS_PADDING) - { + if (p1 == RSA_PKCS1_PSS_PADDING) { if (!(ctx->operation & - (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) + (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY))) goto bad_pad; if (!rctx->md) rctx->md = EVP_sha1(); - } - if (p1 == RSA_PKCS1_OAEP_PADDING) - { + } + if (p1 == RSA_PKCS1_OAEP_PADDING) { if (!(ctx->operation & EVP_PKEY_OP_TYPE_CRYPT)) goto bad_pad; if (!rctx->md) rctx->md = EVP_sha1(); - } + } rctx->pad_mode = p1; return 1; - } - bad_pad: + } +bad_pad: RSAerr(RSA_F_PKEY_RSA_CTRL, - RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); + RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE); return -2; - case EVP_PKEY_CTRL_GET_RSA_PADDING: + case EVP_PKEY_CTRL_GET_RSA_PADDING: *(int *)p2 = rctx->pad_mode; return 1; - case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: - case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: - if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) - { + case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: + case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: + if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_PSS_SALTLEN); return -2; - } + } if (type == EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN) *(int *)p2 = rctx->saltlen; - else - { + else { if (p1 < -2) return -2; rctx->saltlen = p1; - } + } return 1; - case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: - if (p1 < 256) - { + case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: + if (p1 < 256) { RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_KEYBITS); return -2; - } + } rctx->nbits = p1; return 1; - case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: + case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: if (!p2) return -2; rctx->pub_exp = p2; return 1; - case EVP_PKEY_CTRL_MD: + case EVP_PKEY_CTRL_MD: if (!check_padding_md(p2, rctx->pad_mode)) return 0; rctx->md = p2; return 1; - case EVP_PKEY_CTRL_RSA_MGF1_MD: - case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: - if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) - { + case EVP_PKEY_CTRL_RSA_MGF1_MD: + case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: + if (rctx->pad_mode != RSA_PKCS1_PSS_PADDING) { RSAerr(RSA_F_PKEY_RSA_CTRL, RSA_R_INVALID_MGF1_MD); return -2; - } - if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) - { + } + if (type == EVP_PKEY_CTRL_GET_RSA_MGF1_MD) { if (rctx->mgf1md) *(const EVP_MD **)p2 = rctx->mgf1md; else *(const EVP_MD **)p2 = rctx->md; - } - else + } else rctx->mgf1md = p2; return 1; - case EVP_PKEY_CTRL_DIGESTINIT: - case EVP_PKEY_CTRL_PKCS7_ENCRYPT: - case EVP_PKEY_CTRL_PKCS7_DECRYPT: - case EVP_PKEY_CTRL_PKCS7_SIGN: + case EVP_PKEY_CTRL_DIGESTINIT: + case EVP_PKEY_CTRL_PKCS7_ENCRYPT: + case EVP_PKEY_CTRL_PKCS7_DECRYPT: + case EVP_PKEY_CTRL_PKCS7_SIGN: return 1; #ifndef OPENSSL_NO_CMS - case EVP_PKEY_CTRL_CMS_DECRYPT: - { + case EVP_PKEY_CTRL_CMS_DECRYPT: + { X509_ALGOR *alg = NULL; ASN1_OBJECT *encalg = NULL; + if (p2) CMS_RecipientInfo_ktri_get0_algs(p2, NULL, NULL, &alg); if (alg) X509_ALGOR_get0(&encalg, NULL, NULL, alg); if (encalg && OBJ_obj2nid(encalg) == NID_rsaesOaep) rctx->pad_mode = RSA_PKCS1_OAEP_PADDING; - } - case EVP_PKEY_CTRL_CMS_ENCRYPT: - case EVP_PKEY_CTRL_CMS_SIGN: + } + /* FALLTHROUGH */ + + case EVP_PKEY_CTRL_CMS_ENCRYPT: + case EVP_PKEY_CTRL_CMS_SIGN: return 1; #endif - case EVP_PKEY_CTRL_PEER_KEY: - RSAerr(RSA_F_PKEY_RSA_CTRL, - RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); - return -2; + case EVP_PKEY_CTRL_PEER_KEY: + RSAerr(RSA_F_PKEY_RSA_CTRL, + RSA_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); + return -2; - default: + default: return -2; - - } } +} -static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, - const char *type, const char *value) - { +static int +pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, const char *type, const char *value) +{ long lval; char *ep; - if (!value) - { + if (!value) { RSAerr(RSA_F_PKEY_RSA_CTRL_STR, RSA_R_VALUE_MISSING); return 0; - } - if (!strcmp(type, "rsa_padding_mode")) - { + } + if (!strcmp(type, "rsa_padding_mode")) { int pm; if (!strcmp(value, "pkcs1")) pm = RSA_PKCS1_PADDING; @@ -544,14 +511,13 @@ static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, pm = RSA_X931_PADDING; else if (!strcmp(value, "pss")) pm = RSA_PKCS1_PSS_PADDING; - else - { + else { RSAerr(RSA_F_PKEY_RSA_CTRL_STR, - RSA_R_UNKNOWN_PADDING_TYPE); + RSA_R_UNKNOWN_PADDING_TYPE); return -2; - } - return EVP_PKEY_CTX_set_rsa_padding(ctx, pm); } + return EVP_PKEY_CTX_set_rsa_padding(ctx, pm); + } if (!strcmp(type, "rsa_pss_saltlen")) { int saltlen; @@ -560,7 +526,8 @@ static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, lval = strtol(value, &ep, 10); if (value[0] == '\0' || *ep != '\0') goto not_a_number; - if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || + if ((errno == ERANGE && + (lval == LONG_MAX || lval == LONG_MIN)) || (lval > INT_MAX || lval < INT_MIN)) goto out_of_range; saltlen = lval; @@ -574,7 +541,8 @@ static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, lval = strtol(value, &ep, 10); if (value[0] == '\0' || *ep != '\0') goto not_a_number; - if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || + if ((errno == ERANGE && + (lval == LONG_MAX || lval == LONG_MIN)) || (lval > INT_MAX || lval < INT_MIN)) goto out_of_range; nbits = lval; @@ -584,6 +552,7 @@ static int pkey_rsa_ctrl_str(EVP_PKEY_CTX *ctx, if (!strcmp(type, "rsa_keygen_pubexp")) { int ret; BIGNUM *pubexp = NULL; + if (!BN_asc2bn(&pubexp, value)) return 0; ret = EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, pubexp); @@ -597,27 +566,26 @@ out_of_range: return -2; } -static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) - { +static int +pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) +{ RSA *rsa = NULL; RSA_PKEY_CTX *rctx = ctx->data; BN_GENCB *pcb, cb; int ret; - if (!rctx->pub_exp) - { + + if (!rctx->pub_exp) { rctx->pub_exp = BN_new(); if (!rctx->pub_exp || !BN_set_word(rctx->pub_exp, RSA_F4)) return 0; - } + } rsa = RSA_new(); if (!rsa) return 0; - if (ctx->pkey_gencb) - { + if (ctx->pkey_gencb) { pcb = &cb; evp_pkey_set_cb_translate(pcb, ctx); - } - else + } else pcb = NULL; ret = RSA_generate_key_ex(rsa, rctx->nbits, rctx->pub_exp, pcb); if (ret > 0) @@ -625,7 +593,7 @@ static int pkey_rsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) else RSA_free(rsa); return ret; - } +} const EVP_PKEY_METHOD rsa_pkey_meth = { .pkey_id = EVP_PKEY_RSA, diff --git a/lib/libcrypto/rsa/rsa_prn.c b/lib/libcrypto/rsa/rsa_prn.c index 2d7bcb62b03..89cf2b45b0e 100644 --- a/lib/libcrypto/rsa/rsa_prn.c +++ b/lib/libcrypto/rsa/rsa_prn.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_prn.c,v 1.3 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_prn.c,v 1.4 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2006. */ @@ -61,31 +61,32 @@ #include <openssl/rsa.h> #include <openssl/evp.h> -int RSA_print_fp(FILE *fp, const RSA *x, int off) - { +int +RSA_print_fp(FILE *fp, const RSA *x, int off) +{ BIO *b; int ret; - if ((b=BIO_new(BIO_s_file())) == NULL) - { - RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB); - return(0); - } - BIO_set_fp(b,fp,BIO_NOCLOSE); - ret=RSA_print(b,x,off); - BIO_free(b); - return(ret); + if ((b = BIO_new(BIO_s_file())) == NULL) { + RSAerr(RSA_F_RSA_PRINT_FP, ERR_R_BUF_LIB); + return 0; } + BIO_set_fp(b, fp, BIO_NOCLOSE); + ret = RSA_print(b, x, off); + BIO_free(b); + return ret; +} -int RSA_print(BIO *bp, const RSA *x, int off) - { +int +RSA_print(BIO *bp, const RSA *x, int off) +{ EVP_PKEY *pk; int ret; + pk = EVP_PKEY_new(); if (!pk || !EVP_PKEY_set1_RSA(pk, (RSA *)x)) return 0; ret = EVP_PKEY_print_private(bp, pk, off, NULL); EVP_PKEY_free(pk); return ret; - } - +} diff --git a/lib/libcrypto/rsa/rsa_pss.c b/lib/libcrypto/rsa/rsa_pss.c index d26839a5fb6..4c6a90c1c70 100644 --- a/lib/libcrypto/rsa/rsa_pss.c +++ b/lib/libcrypto/rsa/rsa_pss.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_pss.c,v 1.5 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_pss.c,v 1.6 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2005. */ @@ -64,18 +64,20 @@ #include <openssl/rand.h> #include <openssl/sha.h> -static const unsigned char zeroes[] = {0,0,0,0,0,0,0,0}; +static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; -int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, - const EVP_MD *Hash, const unsigned char *EM, int sLen) - { +int +RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash, const EVP_MD *Hash, + const unsigned char *EM, int sLen) +{ return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen); - } +} -int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash, - const EVP_MD *Hash, const EVP_MD *mgf1Hash, - const unsigned char *EM, int sLen) - { +int +RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash, + const EVP_MD *Hash, const EVP_MD *mgf1Hash, const unsigned char *EM, + int sLen) +{ int i; int ret = 0; int hLen, maskedDBLen, MSBits, emLen; @@ -83,6 +85,7 @@ int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash, unsigned char *DB = NULL; EVP_MD_CTX ctx; unsigned char H_[EVP_MAX_MD_SIZE]; + EVP_MD_CTX_init(&ctx); if (mgf1Hash == NULL) @@ -97,99 +100,96 @@ int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash, * -2 salt length is autorecovered from signature * -N reserved */ - if (sLen == -1) sLen = hLen; - else if (sLen == -2) sLen = -2; - else if (sLen < -2) - { - RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED); + if (sLen == -1) + sLen = hLen; + else if (sLen == -2) + sLen = -2; + else if (sLen < -2) { + RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, + RSA_R_SLEN_CHECK_FAILED); goto err; - } + } MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; emLen = RSA_size(rsa); - if (EM[0] & (0xFF << MSBits)) - { - RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_FIRST_OCTET_INVALID); + if (EM[0] & (0xFF << MSBits)) { + RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, + RSA_R_FIRST_OCTET_INVALID); goto err; - } - if (MSBits == 0) - { + } + if (MSBits == 0) { EM++; emLen--; - } - if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */ - { + } + if (emLen < (hLen + sLen + 2)) { /* sLen can be small negative */ RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_DATA_TOO_LARGE); goto err; - } - if (EM[emLen - 1] != 0xbc) - { - RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_LAST_OCTET_INVALID); + } + if (EM[emLen - 1] != 0xbc) { + RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, + RSA_R_LAST_OCTET_INVALID); goto err; - } + } maskedDBLen = emLen - hLen - 1; H = EM + maskedDBLen; DB = malloc(maskedDBLen); - if (!DB) - { + if (!DB) { RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, ERR_R_MALLOC_FAILURE); goto err; - } + } if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0) goto err; for (i = 0; i < maskedDBLen; i++) DB[i] ^= EM[i]; if (MSBits) DB[0] &= 0xFF >> (8 - MSBits); - for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ; - if (DB[i++] != 0x1) - { - RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_RECOVERY_FAILED); + for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) + ; + if (DB[i++] != 0x1) { + RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, + RSA_R_SLEN_RECOVERY_FAILED); goto err; - } - if (sLen >= 0 && (maskedDBLen - i) != sLen) - { - RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED); + } + if (sLen >= 0 && (maskedDBLen - i) != sLen) { + RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, + RSA_R_SLEN_CHECK_FAILED); goto err; - } - if (!EVP_DigestInit_ex(&ctx, Hash, NULL) - || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) - || !EVP_DigestUpdate(&ctx, mHash, hLen)) + } + if (!EVP_DigestInit_ex(&ctx, Hash, NULL) || + !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) || + !EVP_DigestUpdate(&ctx, mHash, hLen)) goto err; - if (maskedDBLen - i) - { + if (maskedDBLen - i) { if (!EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i)) goto err; - } + } if (!EVP_DigestFinal_ex(&ctx, H_, NULL)) goto err; - if (memcmp(H_, H, hLen)) - { + if (memcmp(H_, H, hLen)) { RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1, RSA_R_BAD_SIGNATURE); ret = 0; - } - else + } else ret = 1; - err: +err: free(DB); EVP_MD_CTX_cleanup(&ctx); return ret; +} - } - -int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, - const unsigned char *mHash, - const EVP_MD *Hash, int sLen) - { +int +RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM, + const unsigned char *mHash, const EVP_MD *Hash, int sLen) +{ return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen); - } +} -int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, - const unsigned char *mHash, - const EVP_MD *Hash, const EVP_MD *mgf1Hash, int sLen) - { +int +RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, + const unsigned char *mHash, const EVP_MD *Hash, const EVP_MD *mgf1Hash, + int sLen) +{ int i; int ret = 0; int hLen, maskedDBLen, MSBits, emLen; @@ -208,47 +208,45 @@ int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, * -2 salt length is maximized * -N reserved */ - if (sLen == -1) sLen = hLen; - else if (sLen == -2) sLen = -2; - else if (sLen < -2) - { - RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, RSA_R_SLEN_CHECK_FAILED); + if (sLen == -1) + sLen = hLen; + else if (sLen == -2) + sLen = -2; + else if (sLen < -2) { + RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, + RSA_R_SLEN_CHECK_FAILED); goto err; - } + } MSBits = (BN_num_bits(rsa->n) - 1) & 0x7; emLen = RSA_size(rsa); - if (MSBits == 0) - { + if (MSBits == 0) { *EM++ = 0; emLen--; - } + } if (sLen == -2) - { sLen = emLen - hLen - 2; - } - else if (emLen < (hLen + sLen + 2)) - { - RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + else if (emLen < (hLen + sLen + 2)) { + RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); goto err; - } - if (sLen > 0) - { + } + if (sLen > 0) { salt = malloc(sLen); - if (!salt) - { - RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1,ERR_R_MALLOC_FAILURE); + if (!salt) { + RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1, + ERR_R_MALLOC_FAILURE); goto err; - } + } if (RAND_bytes(salt, sLen) <= 0) goto err; - } + } maskedDBLen = emLen - hLen - 1; H = EM + maskedDBLen; EVP_MD_CTX_init(&ctx); - if (!EVP_DigestInit_ex(&ctx, Hash, NULL) - || !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) - || !EVP_DigestUpdate(&ctx, mHash, hLen)) + if (!EVP_DigestInit_ex(&ctx, Hash, NULL) || + !EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes) || + !EVP_DigestUpdate(&ctx, mHash, hLen)) goto err; if (sLen && !EVP_DigestUpdate(&ctx, salt, sLen)) goto err; @@ -262,29 +260,27 @@ int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM, p = EM; - /* Initial PS XORs with all zeroes which is a NOP so just update + /* + * Initial PS XORs with all zeroes which is a NOP so just update * pointer. Note from a test above this value is guaranteed to * be non-negative. */ p += emLen - sLen - hLen - 2; *p++ ^= 0x1; - if (sLen > 0) - { + if (sLen > 0) { for (i = 0; i < sLen; i++) *p++ ^= salt[i]; - } + } if (MSBits) EM[0] &= 0xFF >> (8 - MSBits); /* H is already in place so just set final 0xbc */ - EM[emLen - 1] = 0xbc; ret = 1; - err: +err: free(salt); return ret; - - } +} diff --git a/lib/libcrypto/rsa/rsa_saos.c b/lib/libcrypto/rsa/rsa_saos.c index cca503e0268..50dfef7e71d 100644 --- a/lib/libcrypto/rsa/rsa_saos.c +++ b/lib/libcrypto/rsa/rsa_saos.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_saos.c,v 1.11 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_saos.c,v 1.12 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -63,88 +63,86 @@ #include <openssl/objects.h> #include <openssl/x509.h> -int RSA_sign_ASN1_OCTET_STRING(int type, - const unsigned char *m, unsigned int m_len, - unsigned char *sigret, unsigned int *siglen, RSA *rsa) - { +int +RSA_sign_ASN1_OCTET_STRING(int type, const unsigned char *m, unsigned int m_len, + unsigned char *sigret, unsigned int *siglen, RSA *rsa) +{ ASN1_OCTET_STRING sig; - int i,j,ret=1; + int i, j, ret = 1; unsigned char *p,*s; - sig.type=V_ASN1_OCTET_STRING; - sig.length=m_len; - sig.data=(unsigned char *)m; + sig.type = V_ASN1_OCTET_STRING; + sig.length = m_len; + sig.data = (unsigned char *)m; - i=i2d_ASN1_OCTET_STRING(&sig,NULL); - j=RSA_size(rsa); - if (i > (j-RSA_PKCS1_PADDING_SIZE)) - { - RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); - return(0); - } - s = malloc((unsigned int)j+1); - if (s == NULL) - { - RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); - return(0); - } - p=s; - i2d_ASN1_OCTET_STRING(&sig,&p); - i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); + i = i2d_ASN1_OCTET_STRING(&sig, NULL); + j = RSA_size(rsa); + if (i > (j - RSA_PKCS1_PADDING_SIZE)) { + RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, + RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); + return 0; + } + s = malloc((unsigned int)j + 1); + if (s == NULL) { + RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, ERR_R_MALLOC_FAILURE); + return 0; + } + p = s; + i2d_ASN1_OCTET_STRING(&sig, &p); + i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING); if (i <= 0) - ret=0; + ret = 0; else - *siglen=i; + *siglen = i; - OPENSSL_cleanse(s,(unsigned int)j+1); + OPENSSL_cleanse(s, (unsigned int)j + 1); free(s); - return(ret); - } + return ret; +} -int RSA_verify_ASN1_OCTET_STRING(int dtype, - const unsigned char *m, - unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, - RSA *rsa) - { - int i,ret=0; +int +RSA_verify_ASN1_OCTET_STRING(int dtype, const unsigned char *m, + unsigned int m_len, unsigned char *sigbuf, unsigned int siglen, RSA *rsa) +{ + int i, ret = 0; unsigned char *s; const unsigned char *p; - ASN1_OCTET_STRING *sig=NULL; + ASN1_OCTET_STRING *sig = NULL; - if (siglen != (unsigned int)RSA_size(rsa)) - { - RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH); - return(0); - } + if (siglen != (unsigned int)RSA_size(rsa)) { + RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, + RSA_R_WRONG_SIGNATURE_LENGTH); + return 0; + } s = malloc((unsigned int)siglen); - if (s == NULL) - { - RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE); + if (s == NULL) { + RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, + ERR_R_MALLOC_FAILURE); goto err; - } - i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); + } + i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING); - if (i <= 0) goto err; + if (i <= 0) + goto err; - p=s; - sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i); - if (sig == NULL) goto err; + p = s; + sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i); + if (sig == NULL) + goto err; - if ( ((unsigned int)sig->length != m_len) || - (memcmp(m,sig->data,m_len) != 0)) - { - RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE); - } - else - ret=1; + if ((unsigned int)sig->length != m_len || + memcmp(m,sig->data, m_len) != 0) { + RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING, + RSA_R_BAD_SIGNATURE); + } else + ret = 1; err: - if (sig != NULL) M_ASN1_OCTET_STRING_free(sig); - if (s != NULL) - { - OPENSSL_cleanse(s,(unsigned int)siglen); + if (sig != NULL) + M_ASN1_OCTET_STRING_free(sig); + if (s != NULL) { + OPENSSL_cleanse(s, (unsigned int)siglen); free(s); - } - return(ret); } - + return ret; +} diff --git a/lib/libcrypto/rsa/rsa_sign.c b/lib/libcrypto/rsa/rsa_sign.c index 4642775964e..239435fe919 100644 --- a/lib/libcrypto/rsa/rsa_sign.c +++ b/lib/libcrypto/rsa/rsa_sign.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_sign.c,v 1.17 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_sign.c,v 1.18 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -67,233 +67,212 @@ /* Size of an SSL signature: MD5+SHA1 */ #define SSL_SIG_LENGTH 36 -int RSA_sign(int type, const unsigned char *m, unsigned int m_len, - unsigned char *sigret, unsigned int *siglen, RSA *rsa) - { +int +RSA_sign(int type, const unsigned char *m, unsigned int m_len, + unsigned char *sigret, unsigned int *siglen, RSA *rsa) +{ X509_SIG sig; ASN1_TYPE parameter; - int i,j,ret=1; + int i, j, ret = 1; unsigned char *p, *tmps = NULL; const unsigned char *s = NULL; X509_ALGOR algor; ASN1_OCTET_STRING digest; - if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign) - { - return rsa->meth->rsa_sign(type, m, m_len, - sigret, siglen, rsa); - } + + if ((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign) + return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa); + /* Special case: SSL signature, just check the length */ - if(type == NID_md5_sha1) { - if(m_len != SSL_SIG_LENGTH) { - RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH); - return(0); + if (type == NID_md5_sha1) { + if (m_len != SSL_SIG_LENGTH) { + RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH); + return 0; } i = SSL_SIG_LENGTH; s = m; } else { - sig.algor= &algor; - sig.algor->algorithm=OBJ_nid2obj(type); - if (sig.algor->algorithm == NULL) - { - RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE); - return(0); - } - if (sig.algor->algorithm->length == 0) - { - RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); - return(0); - } - parameter.type=V_ASN1_NULL; - parameter.value.ptr=NULL; - sig.algor->parameter= ¶meter; + sig.algor = &algor; + sig.algor->algorithm = OBJ_nid2obj(type); + if (sig.algor->algorithm == NULL) { + RSAerr(RSA_F_RSA_SIGN, RSA_R_UNKNOWN_ALGORITHM_TYPE); + return 0; + } + if (sig.algor->algorithm->length == 0) { + RSAerr(RSA_F_RSA_SIGN, + RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD); + return 0; + } + parameter.type = V_ASN1_NULL; + parameter.value.ptr = NULL; + sig.algor->parameter = ¶meter; - sig.digest= &digest; - sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */ - sig.digest->length=m_len; + sig.digest = &digest; + sig.digest->data = (unsigned char *)m; /* TMP UGLY CAST */ + sig.digest->length = m_len; - i=i2d_X509_SIG(&sig,NULL); + i = i2d_X509_SIG(&sig, NULL); + } + j = RSA_size(rsa); + if (i > j - RSA_PKCS1_PADDING_SIZE) { + RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); + return 0; } - j=RSA_size(rsa); - if (i > (j-RSA_PKCS1_PADDING_SIZE)) - { - RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY); - return(0); + if (type != NID_md5_sha1) { + tmps = malloc((unsigned int)j + 1); + if (tmps == NULL) { + RSAerr(RSA_F_RSA_SIGN, ERR_R_MALLOC_FAILURE); + return 0; } - if(type != NID_md5_sha1) { - tmps = malloc((unsigned int)j+1); - if (tmps == NULL) - { - RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE); - return(0); - } - p=tmps; - i2d_X509_SIG(&sig,&p); - s=tmps; + p = tmps; + i2d_X509_SIG(&sig, &p); + s = tmps; } - i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING); + i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING); if (i <= 0) - ret=0; + ret = 0; else - *siglen=i; + *siglen = i; - if(type != NID_md5_sha1) { - OPENSSL_cleanse(tmps,(unsigned int)j+1); + if (type != NID_md5_sha1) { + OPENSSL_cleanse(tmps, (unsigned int)j + 1); free(tmps); } return(ret); - } +} -int int_rsa_verify(int dtype, const unsigned char *m, - unsigned int m_len, - unsigned char *rm, size_t *prm_len, - const unsigned char *sigbuf, size_t siglen, - RSA *rsa) - { - int i,ret=0,sigtype; +int +int_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len, + unsigned char *rm, size_t *prm_len, const unsigned char *sigbuf, + size_t siglen, RSA *rsa) +{ + int i, ret = 0, sigtype; unsigned char *s; - X509_SIG *sig=NULL; + X509_SIG *sig = NULL; - if (siglen != (unsigned int)RSA_size(rsa)) - { - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH); - return(0); - } + if (siglen != (unsigned int)RSA_size(rsa)) { + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_WRONG_SIGNATURE_LENGTH); + return 0; + } - if((dtype == NID_md5_sha1) && rm) - { - i = RSA_public_decrypt((int)siglen, - sigbuf,rm,rsa,RSA_PKCS1_PADDING); + if ((dtype == NID_md5_sha1) && rm) { + i = RSA_public_decrypt((int)siglen, sigbuf, rm, rsa, + RSA_PKCS1_PADDING); if (i <= 0) return 0; *prm_len = i; return 1; - } + } s = malloc((unsigned int)siglen); - if (s == NULL) - { - RSAerr(RSA_F_INT_RSA_VERIFY,ERR_R_MALLOC_FAILURE); + if (s == NULL) { + RSAerr(RSA_F_INT_RSA_VERIFY, ERR_R_MALLOC_FAILURE); + goto err; + } + if (dtype == NID_md5_sha1 && m_len != SSL_SIG_LENGTH) { + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH); goto err; - } - if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) { - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH); - goto err; } - i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING); + i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING); - if (i <= 0) goto err; - /* Oddball MDC2 case: signature can be OCTET STRING. + if (i <= 0) + goto err; + + /* + * Oddball MDC2 case: signature can be OCTET STRING. * check for correct tag and length octets. */ - if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10) - { - if (rm) - { + if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10) { + if (rm) { memcpy(rm, s + 2, 16); *prm_len = 16; ret = 1; - } - else if(memcmp(m, s + 2, 16)) - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE); + } else if (memcmp(m, s + 2, 16)) + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE); else ret = 1; - } + } /* Special case: SSL signature */ - if(dtype == NID_md5_sha1) { - if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH)) - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE); - else ret = 1; + if (dtype == NID_md5_sha1) { + if (i != SSL_SIG_LENGTH || memcmp(s, m, SSL_SIG_LENGTH)) + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE); + else + ret = 1; } else { const unsigned char *p=s; - sig=d2i_X509_SIG(NULL,&p,(long)i); - if (sig == NULL) goto err; + sig = d2i_X509_SIG(NULL, &p, (long)i); + + if (sig == NULL) + goto err; /* Excess data can be used to create forgeries */ - if(p != s+i) - { - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE); + if (p != s + i) { + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE); goto err; - } + } /* Parameters to the signature algorithm can also be used to create forgeries */ - if(sig->algor->parameter - && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) - { - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE); + if (sig->algor->parameter && + ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) { + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE); goto err; - } - - sigtype=OBJ_obj2nid(sig->algor->algorithm); + } + sigtype = OBJ_obj2nid(sig->algor->algorithm); - #ifdef RSA_DEBUG - /* put a backward compatibility flag in EAY */ - fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype), - OBJ_nid2ln(dtype)); - #endif - if (sigtype != dtype) - { - if (((dtype == NID_md5) && - (sigtype == NID_md5WithRSAEncryption)) || - ((dtype == NID_md2) && - (sigtype == NID_md2WithRSAEncryption))) - { + if (sigtype != dtype) { + if ((dtype == NID_md5 && + sigtype == NID_md5WithRSAEncryption) || + (dtype == NID_md2 && + sigtype == NID_md2WithRSAEncryption)) { /* ok, we will let it through */ - fprintf(stderr,"signature has problems, re-make with post SSLeay045\n"); - } - else - { + fprintf(stderr, + "signature has problems, " + "re-make with post SSLeay045\n"); + } else { RSAerr(RSA_F_INT_RSA_VERIFY, - RSA_R_ALGORITHM_MISMATCH); + RSA_R_ALGORITHM_MISMATCH); goto err; - } } - if (rm) - { + } + if (rm) { const EVP_MD *md; + md = EVP_get_digestbynid(dtype); if (md && (EVP_MD_size(md) != sig->digest->length)) RSAerr(RSA_F_INT_RSA_VERIFY, - RSA_R_INVALID_DIGEST_LENGTH); - else - { + RSA_R_INVALID_DIGEST_LENGTH); + else { memcpy(rm, sig->digest->data, - sig->digest->length); + sig->digest->length); *prm_len = sig->digest->length; ret = 1; - } } - else if (((unsigned int)sig->digest->length != m_len) || - (memcmp(m,sig->digest->data,m_len) != 0)) - { - RSAerr(RSA_F_INT_RSA_VERIFY,RSA_R_BAD_SIGNATURE); - } - else - ret=1; + } else if ((unsigned int)sig->digest->length != m_len || + memcmp(m,sig->digest->data,m_len) != 0) { + RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE); + } else + ret = 1; } err: - if (sig != NULL) X509_SIG_free(sig); - if (s != NULL) - { - OPENSSL_cleanse(s,(unsigned int)siglen); + if (sig != NULL) + X509_SIG_free(sig); + if (s != NULL) { + OPENSSL_cleanse(s, (unsigned int)siglen); free(s); - } - return(ret); } + return ret; +} -int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len, - const unsigned char *sigbuf, unsigned int siglen, - RSA *rsa) - { - - if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify) - { - return rsa->meth->rsa_verify(dtype, m, m_len, - sigbuf, siglen, rsa); - } +int +RSA_verify(int dtype, const unsigned char *m, unsigned int m_len, + const unsigned char *sigbuf, unsigned int siglen, RSA *rsa) +{ + if ((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify) + return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, + rsa); return int_rsa_verify(dtype, m, m_len, NULL, NULL, sigbuf, siglen, rsa); - } +} diff --git a/lib/libcrypto/rsa/rsa_ssl.c b/lib/libcrypto/rsa/rsa_ssl.c index 3f4ec95a6ef..c6ab71c6741 100644 --- a/lib/libcrypto/rsa/rsa_ssl.c +++ b/lib/libcrypto/rsa/rsa_ssl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_ssl.c,v 1.7 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_ssl.c,v 1.8 2014/07/09 08:20:08 miod Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -62,93 +62,92 @@ #include <openssl/rsa.h> #include <openssl/rand.h> -int RSA_padding_add_SSLv23(unsigned char *to, int tlen, - const unsigned char *from, int flen) - { +int +RSA_padding_add_SSLv23(unsigned char *to, int tlen, const unsigned char *from, + int flen) +{ int i,j; unsigned char *p; - if (flen > (tlen-11)) - { - RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); - return(0); - } + if (flen > tlen - 11) { + RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23, + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + return 0; + } - p=(unsigned char *)to; + p = (unsigned char *)to; - *(p++)=0; - *(p++)=2; /* Public Key BT (Block Type) */ + *(p++) = 0; + *(p++) = 2; /* Public Key BT (Block Type) */ /* pad out with non-zero random data */ - j=tlen-3-8-flen; + j = tlen - 3 - 8 - flen; - if (RAND_bytes(p,j) <= 0) - return(0); - for (i=0; i<j; i++) - { + if (RAND_bytes(p, j) <= 0) + return 0; + for (i = 0; i < j; i++) { if (*p == '\0') - do { - if (RAND_bytes(p,1) <= 0) - return(0); - } while (*p == '\0'); + do { + if (RAND_bytes(p, 1) <= 0) + return 0; + } while (*p == '\0'); p++; - } + } - memset(p,3,8); - p+=8; - *(p++)='\0'; + memset(p, 3, 8); + p += 8; + *(p++) = '\0'; - memcpy(p,from,(unsigned int)flen); - return(1); - } + memcpy(p, from, (unsigned int)flen); + return 1; +} -int RSA_padding_check_SSLv23(unsigned char *to, int tlen, - const unsigned char *from, int flen, int num) - { - int i,j,k; +int +RSA_padding_check_SSLv23(unsigned char *to, int tlen, const unsigned char *from, + int flen, int num) +{ + int i, j, k; const unsigned char *p; - p=from; - if (flen < 10) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_SMALL); - return(-1); - } - if ((num != (flen+1)) || (*(p++) != 02)) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_BLOCK_TYPE_IS_NOT_02); - return(-1); - } + p = from; + if (flen < 10) { + RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_SMALL); + return -1; + } + if (num != flen + 1 || *(p++) != 02) { + RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, + RSA_R_BLOCK_TYPE_IS_NOT_02); + return -1; + } /* scan over padding data */ - j=flen-1; /* one for type */ - for (i=0; i<j; i++) - if (*(p++) == 0) break; + j = flen - 1; /* one for type */ + for (i = 0; i < j; i++) + if (*(p++) == 0) + break; - if ((i == j) || (i < 8)) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING); - return(-1); - } - for (k = -9; k<-1; k++) - { - if (p[k] != 0x03) break; - } - if (k == -1) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_SSLV3_ROLLBACK_ATTACK); - return(-1); - } + if (i == j || i < 8) { + RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, + RSA_R_NULL_BEFORE_BLOCK_MISSING); + return -1; + } + for (k = -9; k < -1; k++) { + if (p[k] != 0x03) + break; + } + if (k == -1) { + RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, + RSA_R_SSLV3_ROLLBACK_ATTACK); + return -1; + } i++; /* Skip over the '\0' */ - j-=i; - if (j > tlen) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_LARGE); - return(-1); - } - memcpy(to,p,(unsigned int)j); - - return(j); + j -= i; + if (j > tlen) { + RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23, RSA_R_DATA_TOO_LARGE); + return -1; } + memcpy(to, p, (unsigned int)j); + return j; +} diff --git a/lib/libcrypto/rsa/rsa_x931.c b/lib/libcrypto/rsa/rsa_x931.c index 6592c22ee56..f22c50ba13f 100644 --- a/lib/libcrypto/rsa/rsa_x931.c +++ b/lib/libcrypto/rsa/rsa_x931.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rsa_x931.c,v 1.2 2014/06/12 15:49:30 deraadt Exp $ */ +/* $OpenBSD: rsa_x931.c,v 1.3 2014/07/09 08:20:08 miod Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project 2005. */ @@ -63,115 +63,103 @@ #include <openssl/rand.h> #include <openssl/objects.h> -int RSA_padding_add_X931(unsigned char *to, int tlen, - const unsigned char *from, int flen) - { +int +RSA_padding_add_X931(unsigned char *to, int tlen, const unsigned char *from, + int flen) +{ int j; unsigned char *p; - /* Absolute minimum amount of padding is 1 header nibble, 1 padding + /* + * Absolute minimum amount of padding is 1 header nibble, 1 padding * nibble and 2 trailer bytes: but 1 hash if is already in 'from'. */ - j = tlen - flen - 2; - if (j < 0) - { - RSAerr(RSA_F_RSA_PADDING_ADD_X931,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); + if (j < 0) { + RSAerr(RSA_F_RSA_PADDING_ADD_X931, + RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); return -1; - } + } - p=(unsigned char *)to; + p = (unsigned char *)to; /* If no padding start and end nibbles are in one byte */ if (j == 0) *p++ = 0x6A; - else - { + else { *p++ = 0x6B; - if (j > 1) - { + if (j > 1) { memset(p, 0xBB, j - 1); p += j - 1; - } - *p++ = 0xBA; } - memcpy(p,from,(unsigned int)flen); + *p++ = 0xBA; + } + memcpy(p, from, (unsigned int)flen); p += flen; *p = 0xCC; - return(1); - } - -int RSA_padding_check_X931(unsigned char *to, int tlen, - const unsigned char *from, int flen, int num) - { - int i = 0,j; - const unsigned char *p; - - p=from; - if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B))) - { - RSAerr(RSA_F_RSA_PADDING_CHECK_X931,RSA_R_INVALID_HEADER); + return 1; +} + +int +RSA_padding_check_X931(unsigned char *to, int tlen, const unsigned char *from, + int flen, int num) +{ + int i = 0, j; + const unsigned char *p = from; + + if (num != flen || (*p != 0x6A && *p != 0x6B)) { + RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_HEADER); return -1; - } + } - if (*p++ == 0x6B) - { - j=flen-3; - for (i = 0; i < j; i++) - { + if (*p++ == 0x6B) { + j = flen - 3; + for (i = 0; i < j; i++) { unsigned char c = *p++; if (c == 0xBA) break; - if (c != 0xBB) - { + if (c != 0xBB) { RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING); return -1; - } } + } - j -= i; - - if (i == 0) - { + if (i == 0) { RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING); return -1; - } - } - else j = flen - 2; - if (p[j] != 0xCC) - { + j -= i; + } else + j = flen - 2; + + if (p[j] != 0xCC) { RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER); return -1; - } + } - memcpy(to,p,(unsigned int)j); + memcpy(to, p, (unsigned int)j); - return(j); - } + return j; +} /* Translate between X931 hash ids and NIDs */ -int RSA_X931_hash_id(int nid) - { - switch (nid) - { - case NID_sha1: +int +RSA_X931_hash_id(int nid) +{ + switch (nid) { + case NID_sha1: return 0x33; - - case NID_sha256: + case NID_sha256: return 0x34; - - case NID_sha384: + case NID_sha384: return 0x36; - - case NID_sha512: + case NID_sha512: return 0x35; - - } - return -1; } + return -1; +} |