diff options
Diffstat (limited to 'crypto/ffc')
| -rw-r--r-- | crypto/ffc/ffc_backend.c | 16 | ||||
| -rw-r--r-- | crypto/ffc/ffc_dh.c | 64 | ||||
| -rw-r--r-- | crypto/ffc/ffc_key_generate.c | 2 | ||||
| -rw-r--r-- | crypto/ffc/ffc_key_validate.c | 10 | ||||
| -rw-r--r-- | crypto/ffc/ffc_params.c | 62 | ||||
| -rw-r--r-- | crypto/ffc/ffc_params_generate.c | 186 | ||||
| -rw-r--r-- | crypto/ffc/ffc_params_validate.c | 46 |
7 files changed, 194 insertions, 192 deletions
diff --git a/crypto/ffc/ffc_backend.c b/crypto/ffc/ffc_backend.c index fe0a82eac8a6..bfd6d0883941 100644 --- a/crypto/ffc/ffc_backend.c +++ b/crypto/ffc/ffc_backend.c @@ -27,7 +27,7 @@ int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]) if (ffc == NULL) return 0; - prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); + prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME); if (prm != NULL) { /* * In a no-dh build we just go straight to err because we have no @@ -57,7 +57,7 @@ int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]) if (prm != NULL) { if (!OSSL_PARAM_get_int(prm, &i)) goto err; - ffc->gindex = i; + ffc->gindex = i; } prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER); if (prm != NULL) { @@ -72,28 +72,28 @@ int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]) if (prm != NULL) { if (!OSSL_PARAM_get_int(prm, &i)) goto err; - ffc->h = i; + ffc->h = i; } - prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED); + prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED); if (prm != NULL) { if (prm->data_type != OSSL_PARAM_OCTET_STRING) goto err; if (!ossl_ffc_params_set_seed(ffc, prm->data, prm->data_size)) goto err; } - prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ); + prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ); if (prm != NULL) { if (!OSSL_PARAM_get_int(prm, &i)) goto err; ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_PQ, i); } - prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G); + prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_G); if (prm != NULL) { if (!OSSL_PARAM_get_int(prm, &i)) goto err; ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_G, i); } - prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY); + prm = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY); if (prm != NULL) { if (!OSSL_PARAM_get_int(prm, &i)) goto err; @@ -121,7 +121,7 @@ int ossl_ffc_params_fromdata(FFC_PARAMS *ffc, const OSSL_PARAM params[]) ossl_ffc_params_set0_j(ffc, j); return 1; - err: +err: BN_free(j); BN_free(p); BN_free(q); diff --git a/crypto/ffc/ffc_dh.c b/crypto/ffc/ffc_dh.c index df07e173bcb8..e4077e5e5246 100644 --- a/crypto/ffc/ffc_dh.c +++ b/crypto/ffc/ffc_dh.c @@ -13,35 +13,37 @@ #ifndef OPENSSL_NO_DH -# define FFDHE(sz, keylength) { \ - SN_ffdhe##sz, NID_ffdhe##sz, \ - sz, \ - keylength, \ - &ossl_bignum_ffdhe##sz##_p, &ossl_bignum_ffdhe##sz##_q, \ - &ossl_bignum_const_2, \ - } +#define FFDHE(sz, keylength) { \ + SN_ffdhe##sz, \ + NID_ffdhe##sz, \ + sz, \ + keylength, \ + &ossl_bignum_ffdhe##sz##_p, \ + &ossl_bignum_ffdhe##sz##_q, \ + &ossl_bignum_const_2, \ +} -# define MODP(sz, keylength) { \ - SN_modp_##sz, NID_modp_##sz, \ - sz, \ - keylength, \ - &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \ - &ossl_bignum_const_2 \ - } +#define MODP(sz, keylength) { \ + SN_modp_##sz, NID_modp_##sz, \ + sz, \ + keylength, \ + &ossl_bignum_modp_##sz##_p, &ossl_bignum_modp_##sz##_q, \ + &ossl_bignum_const_2 \ +} -# define RFC5114(name, uid, sz, tag) { \ - name, uid, \ - sz, \ - 0, \ - &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \ - &ossl_bignum_dh##tag##_g \ - } +#define RFC5114(name, uid, sz, tag) { \ + name, uid, \ + sz, \ + 0, \ + &ossl_bignum_dh##tag##_p, &ossl_bignum_dh##tag##_q, \ + &ossl_bignum_dh##tag##_g \ +} #else -# define FFDHE(sz, keylength) { SN_ffdhe##sz, NID_ffdhe##sz } -# define MODP(sz, keylength) { SN_modp_##sz, NID_modp_##sz } -# define RFC5114(name, uid, sz, tag) { name, uid } +#define FFDHE(sz, keylength) { SN_ffdhe##sz, NID_ffdhe##sz } +#define MODP(sz, keylength) { SN_modp_##sz, NID_modp_##sz } +#define RFC5114(name, uid, sz, tag) { name, uid } #endif @@ -76,10 +78,10 @@ static const DH_NAMED_GROUP dh_named_groups[] = { MODP(4096, 325), MODP(6144, 375), MODP(8192, 400), - /* - * Additional dh named groups from RFC 5114 that have a different g. - * The uid can be any unique identifier. - */ +/* + * Additional dh named groups from RFC 5114 that have a different g. + * The uid can be any unique identifier. + */ #ifndef FIPS_MODULE RFC5114("dh_1024_160", 1, 1024, 1024_160), RFC5114("dh_2048_224", 2, 2048, 2048_224), @@ -111,8 +113,8 @@ const DH_NAMED_GROUP *ossl_ffc_uid_to_dh_named_group(int uid) #ifndef OPENSSL_NO_DH const DH_NAMED_GROUP *ossl_ffc_numbers_to_dh_named_group(const BIGNUM *p, - const BIGNUM *q, - const BIGNUM *g) + const BIGNUM *q, + const BIGNUM *g) { size_t i; @@ -163,7 +165,7 @@ int ossl_ffc_named_group_set(FFC_PARAMS *ffc, const DH_NAMED_GROUP *group) return 0; ossl_ffc_params_set0_pqg(ffc, (BIGNUM *)group->p, (BIGNUM *)group->q, - (BIGNUM *)group->g); + (BIGNUM *)group->g); ffc->keylength = group->keylength; /* flush the cached nid, The DH layer is responsible for caching */ diff --git a/crypto/ffc/ffc_key_generate.c b/crypto/ffc/ffc_key_generate.c index d02c6575274d..1e7f6417ef3b 100644 --- a/crypto/ffc/ffc_key_generate.c +++ b/crypto/ffc/ffc_key_generate.c @@ -20,7 +20,7 @@ * priv_key is the returned private key, */ int ossl_ffc_generate_private_key(BN_CTX *ctx, const FFC_PARAMS *params, - int N, int s, BIGNUM *priv) + int N, int s, BIGNUM *priv) { int ret = 0, qbits = BN_num_bits(params->q); BIGNUM *m, *two_powN = NULL; diff --git a/crypto/ffc/ffc_key_validate.c b/crypto/ffc/ffc_key_validate.c index a4a2a58e9a7f..9b48c74ffce9 100644 --- a/crypto/ffc/ffc_key_validate.c +++ b/crypto/ffc/ffc_key_validate.c @@ -17,7 +17,7 @@ * ret contains 0 on success, or error flags (see FFC_ERROR_PUBKEY_TOO_SMALL) */ int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params, - const BIGNUM *pub_key, int *ret) + const BIGNUM *pub_key, int *ret) { int ok = 0; BIGNUM *tmp = NULL; @@ -48,7 +48,7 @@ int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params, if (BN_cmp(pub_key, tmp) >= 0) *ret |= FFC_ERROR_PUBKEY_TOO_LARGE; ok = 1; - err: +err: if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); @@ -60,7 +60,7 @@ int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params, * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation. */ int ossl_ffc_validate_public_key(const FFC_PARAMS *params, - const BIGNUM *pub_key, int *ret) + const BIGNUM *pub_key, int *ret) { int ok = 0; BIGNUM *tmp = NULL; @@ -85,7 +85,7 @@ int ossl_ffc_validate_public_key(const FFC_PARAMS *params, } ok = 1; - err: +err: if (ctx != NULL) { BN_CTX_end(ctx); BN_CTX_free(ctx); @@ -100,7 +100,7 @@ int ossl_ffc_validate_public_key(const FFC_PARAMS *params, * Note: This assumes that the domain parameters are valid. */ int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv, - int *ret) + int *ret) { int ok = 0; diff --git a/crypto/ffc/ffc_params.c b/crypto/ffc/ffc_params.c index 3536efd1ad85..88b0612d4182 100644 --- a/crypto/ffc/ffc_params.c +++ b/crypto/ffc/ffc_params.c @@ -14,7 +14,7 @@ #include "internal/nelem.h" #ifndef FIPS_MODULE -# include <openssl/asn1.h> /* ossl_ffc_params_print */ +#include <openssl/asn1.h> /* ossl_ffc_params_print */ #endif void ossl_ffc_params_init(FFC_PARAMS *params) @@ -52,7 +52,7 @@ void ossl_ffc_params_set0_pqg(FFC_PARAMS *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) } void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p, - const BIGNUM **q, const BIGNUM **g) + const BIGNUM **q, const BIGNUM **g) { if (p != NULL) *p = d->p; @@ -62,7 +62,6 @@ void ossl_ffc_params_get0_pqg(const FFC_PARAMS *d, const BIGNUM **p, *g = d->g; } - /* j is the 'cofactor' that is optionally output for ASN1. */ void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j) { @@ -73,7 +72,7 @@ void ossl_ffc_params_set0_j(FFC_PARAMS *d, BIGNUM *j) } int ossl_ffc_params_set_seed(FFC_PARAMS *params, - const unsigned char *seed, size_t seedlen) + const unsigned char *seed, size_t seedlen) { if (params == NULL) return 0; @@ -117,7 +116,7 @@ void ossl_ffc_params_set_flags(FFC_PARAMS *params, unsigned int flags) } void ossl_ffc_params_enable_flags(FFC_PARAMS *params, unsigned int flags, - int enable) + int enable) { if (enable) params->flags |= flags; @@ -133,8 +132,8 @@ int ossl_ffc_set_digest(FFC_PARAMS *params, const char *alg, const char *props) } int ossl_ffc_params_set_validate_params(FFC_PARAMS *params, - const unsigned char *seed, - size_t seedlen, int counter) + const unsigned char *seed, + size_t seedlen, int counter) { if (!ossl_ffc_params_set_seed(params, seed, seedlen)) return 0; @@ -143,8 +142,8 @@ int ossl_ffc_params_set_validate_params(FFC_PARAMS *params, } void ossl_ffc_params_get_validate_params(const FFC_PARAMS *params, - unsigned char **seed, size_t *seedlen, - int *pcounter) + unsigned char **seed, size_t *seedlen, + int *pcounter) { if (seed != NULL) *seed = params->seed; @@ -165,7 +164,7 @@ static int ffc_bn_cpy(BIGNUM **dst, const BIGNUM *src) if (src == NULL) a = NULL; else if (BN_get_flags(src, BN_FLG_STATIC_DATA) - && !BN_get_flags(src, BN_FLG_MALLOCED)) + && !BN_get_flags(src, BN_FLG_MALLOCED)) a = (BIGNUM *)src; else if ((a = BN_dup(src)) == NULL) return 0; @@ -188,7 +187,7 @@ int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src) dst->seedlen = src->seedlen; if (src->seed != NULL) { dst->seed = OPENSSL_memdup(src->seed, src->seedlen); - if (dst->seed == NULL) + if (dst->seed == NULL) return 0; } else { dst->seed = NULL; @@ -205,12 +204,12 @@ int ossl_ffc_params_copy(FFC_PARAMS *dst, const FFC_PARAMS *src) int ossl_ffc_params_cmp(const FFC_PARAMS *a, const FFC_PARAMS *b, int ignore_q) { return BN_cmp(a->p, b->p) == 0 - && BN_cmp(a->g, b->g) == 0 - && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */ + && BN_cmp(a->g, b->g) == 0 + && (ignore_q || BN_cmp(a->q, b->q) == 0); /* Note: q may be NULL */ } int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld, - OSSL_PARAM params[]) + OSSL_PARAM params[]) { int test_flags; @@ -228,20 +227,20 @@ int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld, return 0; if (ffc->j != NULL && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_FFC_COFACTOR, - ffc->j)) + ffc->j)) return 0; if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_GINDEX, - ffc->gindex)) + ffc->gindex)) return 0; if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_PCOUNTER, - ffc->pcounter)) + ffc->pcounter)) return 0; if (!ossl_param_build_set_int(bld, params, OSSL_PKEY_PARAM_FFC_H, ffc->h)) return 0; if (ffc->seed != NULL && !ossl_param_build_set_octet_string(bld, params, - OSSL_PKEY_PARAM_FFC_SEED, - ffc->seed, ffc->seedlen)) + OSSL_PKEY_PARAM_FFC_SEED, + ffc->seed, ffc->seedlen)) return 0; if (ffc->nid != NID_undef) { const DH_NAMED_GROUP *group = ossl_ffc_uid_to_dh_named_group(ffc->nid); @@ -249,33 +248,33 @@ int ossl_ffc_params_todata(const FFC_PARAMS *ffc, OSSL_PARAM_BLD *bld, if (name == NULL || !ossl_param_build_set_utf8_string(bld, params, - OSSL_PKEY_PARAM_GROUP_NAME, - name)) + OSSL_PKEY_PARAM_GROUP_NAME, + name)) return 0; } test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_PQ) != 0); if (!ossl_param_build_set_int(bld, params, - OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags)) + OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, test_flags)) return 0; test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_G) != 0); if (!ossl_param_build_set_int(bld, params, - OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags)) + OSSL_PKEY_PARAM_FFC_VALIDATE_G, test_flags)) return 0; test_flags = ((ffc->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) != 0); if (!ossl_param_build_set_int(bld, params, - OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, - test_flags)) + OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, + test_flags)) return 0; if (ffc->mdname != NULL && !ossl_param_build_set_utf8_string(bld, params, - OSSL_PKEY_PARAM_FFC_DIGEST, - ffc->mdname)) - return 0; + OSSL_PKEY_PARAM_FFC_DIGEST, + ffc->mdname)) + return 0; if (ffc->mdprops != NULL && !ossl_param_build_set_utf8_string(bld, params, - OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, - ffc->mdprops)) + OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, + ffc->mdprops)) return 0; return 1; } @@ -306,7 +305,8 @@ int ossl_ffc_params_print(BIO *bp, const FFC_PARAMS *ffc, int indent) goto err; } if (BIO_printf(bp, "%02x%s", ffc->seed[i], - ((i + 1) == ffc->seedlen) ? "" : ":") <= 0) + ((i + 1) == ffc->seedlen) ? "" : ":") + <= 0) goto err; } if (BIO_write(bp, "\n", 1) <= 0) diff --git a/crypto/ffc/ffc_params_generate.c b/crypto/ffc/ffc_params_generate.c index 6b018edfffce..bae5a6afbdca 100644 --- a/crypto/ffc/ffc_params_generate.c +++ b/crypto/ffc/ffc_params_generate.c @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2019-2026 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -45,9 +45,9 @@ static int ffc_validate_LN(size_t L, size_t N, int type, int verify) /* Valid DH L,N parameters from SP800-56Ar3 5.5.1 Table 1 */ if (L == 2048 && (N == 224 || N == 256)) return 112; -# ifndef OPENSSL_NO_DH +#ifndef OPENSSL_NO_DH ERR_raise(ERR_LIB_DH, DH_R_BAD_FFC_PARAMETERS); -# endif +#endif } else if (type == FFC_PARAM_TYPE_DSA) { /* Valid DSA L,N parameters from FIPS 186-4 Section 4.2 */ /* In fips mode 1024/160 can only be used for verification */ @@ -57,9 +57,9 @@ static int ffc_validate_LN(size_t L, size_t N, int type, int verify) return 112; if (L == 3072 && N == 256) return 128; -# ifndef OPENSSL_NO_DSA +#ifndef OPENSSL_NO_DSA ERR_raise(ERR_LIB_DSA, DSA_R_BAD_FFC_PARAMETERS); -# endif +#endif } return 0; } @@ -73,9 +73,9 @@ static int ffc_validate_LN(size_t L, size_t N, int type, int verify) /* Valid DH L,N parameters from SP800-56Ar3 5.5.1 Table 1 */ if (L == 2048 && (N == 224 || N == 256)) return 112; -# ifndef OPENSSL_NO_DH +#ifndef OPENSSL_NO_DH ERR_raise(ERR_LIB_DH, DH_R_BAD_FFC_PARAMETERS); -# endif +#endif } else if (type == FFC_PARAM_TYPE_DSA) { if (L >= 3072 && N >= 256) return 128; @@ -83,9 +83,9 @@ static int ffc_validate_LN(size_t L, size_t N, int type, int verify) return 112; if (L >= 1024 && N >= 160) return 80; -# ifndef OPENSSL_NO_DSA +#ifndef OPENSSL_NO_DSA ERR_raise(ERR_LIB_DSA, DSA_R_BAD_FFC_PARAMETERS); -# endif +#endif } return 0; } @@ -93,9 +93,9 @@ static int ffc_validate_LN(size_t L, size_t N, int type, int verify) /* FIPS186-4 A.2.1 Unverifiable Generation of Generator g */ static int generate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, BIGNUM *g, - BIGNUM *hbn, const BIGNUM *p, - const BIGNUM *e,const BIGNUM *pm1, - int *hret) + BIGNUM *hbn, const BIGNUM *p, + const BIGNUM *e, const BIGNUM *pm1, + int *hret) { int h = 2; @@ -130,9 +130,9 @@ static int generate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, BIGNUM *g, * Returns a value in g. */ static int generate_canonical_g(BN_CTX *ctx, BN_MONT_CTX *mont, - const EVP_MD *evpmd, BIGNUM *g, BIGNUM *tmp, - const BIGNUM *p, const BIGNUM *e, - int gindex, unsigned char *seed, size_t seedlen) + const EVP_MD *evpmd, BIGNUM *g, BIGNUM *tmp, + const BIGNUM *p, const BIGNUM *e, + int gindex, unsigned char *seed, size_t seedlen) { int ret = 0; int counter = 1; @@ -148,11 +148,11 @@ static int generate_canonical_g(BN_CTX *ctx, BN_MONT_CTX *mont, if (mctx == NULL) return 0; - /* - * A.2.3 Step (4) & (5) - * A.2.4 Step (6) & (7) - * counter = 0; counter += 1 - */ + /* + * A.2.3 Step (4) & (5) + * A.2.4 Step (6) & (7) + * counter = 0; counter += 1 + */ for (counter = 1; counter <= 0xFFFF; ++counter) { /* * A.2.3 Step (7) & (8) & (9) @@ -166,13 +166,13 @@ static int generate_canonical_g(BN_CTX *ctx, BN_MONT_CTX *mont, md[1] = (unsigned char)((counter >> 8) & 0xff); md[2] = (unsigned char)(counter & 0xff); if (!EVP_DigestInit_ex(mctx, evpmd, NULL) - || !EVP_DigestUpdate(mctx, seed, seedlen) - || !EVP_DigestUpdate(mctx, ggen, sizeof(ggen)) - || !EVP_DigestUpdate(mctx, md, 3) - || !EVP_DigestFinal_ex(mctx, md, NULL) - || (BN_bin2bn(md, mdsize, tmp) == NULL) - || !BN_mod_exp_mont(g, tmp, e, p, ctx, mont)) - break; /* exit on failure */ + || !EVP_DigestUpdate(mctx, seed, seedlen) + || !EVP_DigestUpdate(mctx, ggen, sizeof(ggen)) + || !EVP_DigestUpdate(mctx, md, 3) + || !EVP_DigestFinal_ex(mctx, md, NULL) + || (BN_bin2bn(md, mdsize, tmp) == NULL) + || !BN_mod_exp_mont(g, tmp, e, p, ctx, mont)) + break; /* exit on failure */ /* * A.2.3 Step (10) * A.2.4 Step (12) @@ -189,9 +189,9 @@ static int generate_canonical_g(BN_CTX *ctx, BN_MONT_CTX *mont, /* Generation of p is the same for FIPS 186-4 & FIPS 186-2 */ static int generate_p(BN_CTX *ctx, const EVP_MD *evpmd, int max_counter, int n, - unsigned char *buf, size_t buf_len, const BIGNUM *q, - BIGNUM *p, int L, BN_GENCB *cb, int *counter, - int *res) + unsigned char *buf, size_t buf_len, const BIGNUM *q, + BIGNUM *p, int L, BN_GENCB *cb, int *counter, + int *res) { int ret = -1; int i, j, k, r; @@ -242,14 +242,14 @@ static int generate_p(BN_CTX *ctx, const EVP_MD *evpmd, int max_counter, int n, * tmp = V(j) = Hash((seed + offset + j) % 2^seedlen) */ if (!EVP_Digest(buf, buf_len, md, NULL, evpmd, NULL) - || (BN_bin2bn(md, mdsize, tmp) == NULL) - /* - * A.1.1.2 Step (11.2) - * A.1.1.3 Step (13.2) - * W += V(j) * 2^(outlen * j) - */ - || !BN_lshift(tmp, tmp, (mdsize << 3) * j) - || !BN_add(W, W, tmp)) + || (BN_bin2bn(md, mdsize, tmp) == NULL) + /* + * A.1.1.2 Step (11.2) + * A.1.1.3 Step (13.2) + * W += V(j) * 2^(outlen * j) + */ + || !BN_lshift(tmp, tmp, (mdsize << 3) * j) + || !BN_add(W, W, tmp)) goto err; } @@ -259,22 +259,22 @@ static int generate_p(BN_CTX *ctx, const EVP_MD *evpmd, int max_counter, int n, * X = W + 2^(L-1) where W < 2^(L-1) */ if (!BN_mask_bits(W, L - 1) - || !BN_copy(X, W) - || !BN_add(X, X, test) - /* - * A.1.1.2 Step (11.4) AND - * A.1.1.3 Step (13.4) - * c = X mod 2q - */ - || !BN_lshift1(tmp, q) - || !BN_mod(c, X, tmp, ctx) - /* - * A.1.1.2 Step (11.5) AND - * A.1.1.3 Step (13.5) - * p = X - (c - 1) - */ - || !BN_sub(tmp, c, BN_value_one()) - || !BN_sub(p, X, tmp)) + || !BN_copy(X, W) + || !BN_add(X, X, test) + /* + * A.1.1.2 Step (11.4) AND + * A.1.1.3 Step (13.4) + * c = X mod 2q + */ + || !BN_lshift1(tmp, q) + || !BN_mod(c, X, tmp, ctx) + /* + * A.1.1.2 Step (11.5) AND + * A.1.1.3 Step (13.5) + * p = X - (c - 1) + */ + || !BN_sub(tmp, c, BN_value_one()) + || !BN_sub(p, X, tmp)) goto err; /* @@ -294,7 +294,7 @@ static int generate_p(BN_CTX *ctx, const EVP_MD *evpmd, int max_counter, int n, /* A.1.1.2 Step (11.8) : Return if p is prime */ if (r > 0) { *counter = i; - ret = 1; /* return success */ + ret = 1; /* return success */ goto err; } if (r != 0) @@ -311,9 +311,9 @@ err: } static int generate_q_fips186_4(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd, - int qsize, unsigned char *seed, size_t seedlen, - int generate_seed, int *retm, int *res, - BN_GENCB *cb) + int qsize, unsigned char *seed, size_t seedlen, + int generate_seed, int *retm, int *res, + BN_GENCB *cb) { int ret = 0, r; int m = *retm; @@ -324,12 +324,12 @@ static int generate_q_fips186_4(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd, /* find q */ for (;;) { - if(!BN_GENCB_call(cb, 0, m++)) + if (!BN_GENCB_call(cb, 0, m++)) goto err; /* A.1.1.2 Step (5) : generate seed with size seed_len */ if (generate_seed - && RAND_bytes_ex(libctx, seed, seedlen, 0) <= 0) + && RAND_bytes_ex(libctx, seed, seedlen, 0) <= 0) goto err; /* * A.1.1.2 Step (6) AND @@ -352,7 +352,7 @@ static int generate_q_fips186_4(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd, * q = U + 2^(N-1) + (1 - U %2) (This sets top and bottom bits) */ pmd[0] |= 0x80; - pmd[qsize-1] |= 0x01; + pmd[qsize - 1] |= 0x01; if (!BN_bin2bn(pmd, qsize, q)) goto err; @@ -384,9 +384,9 @@ err: } static int generate_q_fips186_2(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd, - unsigned char *buf, unsigned char *seed, - size_t qsize, int generate_seed, int *retm, - int *res, BN_GENCB *cb) + unsigned char *buf, unsigned char *seed, + size_t qsize, int generate_seed, int *retm, + int *res, BN_GENCB *cb) { unsigned char buf2[EVP_MAX_MD_SIZE]; unsigned char md[EVP_MAX_MD_SIZE]; @@ -435,7 +435,7 @@ static int generate_q_fips186_2(BN_CTX *ctx, BIGNUM *q, const EVP_MD *evpmd, } if (r != 0) goto err; /* Exit if error */ - /* Try another iteration if it wasnt prime - was in old code.. */ + /* Try another iteration if it wasn't prime - was in old code.. */ generate_seed = 1; } err: @@ -511,9 +511,9 @@ static const char *default_mdname(size_t N) * but G is unverifiable. */ int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx, - FFC_PARAMS *params, int mode, int type, - size_t L, size_t N, int *res, - BN_GENCB *cb) + FFC_PARAMS *params, int mode, int type, + size_t L, size_t N, int *res, + BN_GENCB *cb) { int ok = FFC_PARAM_RET_STATUS_FAILED; unsigned char *seed = NULL, *seed_tmp = NULL; @@ -673,7 +673,7 @@ int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx, * A.1.1.3 Step (10) * n = floor(L / hash_outlen) - 1 */ - n = (L - 1 ) / (mdsize << 3); + n = (L - 1) / (mdsize << 3); /* Calculate 2^(L-1): Used in step A.1.1.2 Step (11.3) */ if (!BN_lshift(test, BN_value_one(), L - 1)) @@ -681,21 +681,21 @@ int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx, for (;;) { if (!generate_q_fips186_4(ctx, q, md, qsize, seed, seedlen, - seed != params->seed, &m, res, cb)) + seed != params->seed, &m, res, cb)) goto err; /* A.1.1.3 Step (9): Verify that q matches the expected value */ if (verify && (BN_cmp(q, params->q) != 0)) { *res = FFC_CHECK_Q_MISMATCH; goto err; } - if(!BN_GENCB_call(cb, 2, 0)) + if (!BN_GENCB_call(cb, 2, 0)) goto err; - if(!BN_GENCB_call(cb, 3, 0)) + if (!BN_GENCB_call(cb, 3, 0)) goto err; memcpy(seed_tmp, seed, seedlen); r = generate_p(ctx, md, counter, n, seed_tmp, seedlen, q, p, L, - cb, &pcounter, res); + cb, &pcounter, res); if (r > 0) break; /* found p */ if (r < 0) @@ -710,7 +710,7 @@ int ossl_ffc_params_FIPS186_4_gen_verify(OSSL_LIB_CTX *libctx, goto err; } } - if(!BN_GENCB_call(cb, 2, 1)) + if (!BN_GENCB_call(cb, 2, 1)) goto err; /* * Gets here if we found p. @@ -730,7 +730,7 @@ g_only: if (((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0) && !ossl_ffc_params_validate_unverifiable_g(ctx, mont, p, q, params->g, - tmp, res)) + tmp, res)) goto err; /* @@ -746,7 +746,7 @@ g_only: if ((seed != NULL) && (params->gindex != FFC_UNVERIFIABLE_GINDEX)) { canonical_g = 1; if (!generate_canonical_g(ctx, mont, md, g, tmp, p, e, - params->gindex, seed, seedlen)) { + params->gindex, seed, seedlen)) { *res = FFC_CHECK_INVALID_G; goto err; } @@ -779,7 +779,7 @@ g_only: if (params->p == NULL || params->q == NULL || params->g == NULL) goto err; if (!ossl_ffc_params_set_validate_params(params, seed, seedlen, - pcounter)) + pcounter)) goto err; params->h = hret; } @@ -804,9 +804,9 @@ err: /* Note this function is only used for verification in fips mode */ int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx, - FFC_PARAMS *params, int mode, int type, - size_t L, size_t N, int *res, - BN_GENCB *cb) + FFC_PARAMS *params, int mode, int type, + size_t L, size_t N, int *res, + BN_GENCB *cb) { int ok = FFC_PARAM_RET_STATUS_FAILED; unsigned char seed[SHA256_DIGEST_LENGTH]; @@ -928,7 +928,7 @@ int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx, use_random_seed = (seed_in == NULL); for (;;) { if (!generate_q_fips186_2(ctx, q, md, buf, seed, qsize, - use_random_seed, &m, res, cb)) + use_random_seed, &m, res, cb)) goto err; if (!BN_GENCB_call(cb, 2, 0)) @@ -949,7 +949,7 @@ int ossl_ffc_params_FIPS186_2_gen_verify(OSSL_LIB_CTX *libctx, } rv = generate_p(ctx, md, counter, n, buf, qsize, q, p, L, cb, - &pcounter, res); + &pcounter, res); if (rv > 0) break; /* found it */ if (rv == -1) @@ -991,9 +991,9 @@ g_only: if (!generate_unverifiable_g(ctx, mont, g, tmp, p, r0, test, &hret)) goto err; } else if (((flags & FFC_PARAM_FLAG_VALIDATE_G) != 0) - && !ossl_ffc_params_validate_unverifiable_g(ctx, mont, p, q, - params->g, tmp, - res)) { + && !ossl_ffc_params_validate_unverifiable_g(ctx, mont, p, q, + params->g, tmp, + res)) { goto err; } @@ -1034,22 +1034,22 @@ err: } int ossl_ffc_params_FIPS186_4_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params, - int type, size_t L, size_t N, - int *res, BN_GENCB *cb) + int type, size_t L, size_t N, + int *res, BN_GENCB *cb) { return ossl_ffc_params_FIPS186_4_gen_verify(libctx, params, - FFC_PARAM_MODE_GENERATE, - type, L, N, res, cb); + FFC_PARAM_MODE_GENERATE, + type, L, N, res, cb); } /* This should no longer be used in FIPS mode */ int ossl_ffc_params_FIPS186_2_generate(OSSL_LIB_CTX *libctx, FFC_PARAMS *params, - int type, size_t L, size_t N, - int *res, BN_GENCB *cb) + int type, size_t L, size_t N, + int *res, BN_GENCB *cb) { if (!ossl_ffc_params_FIPS186_2_gen_verify(libctx, params, - FFC_PARAM_MODE_GENERATE, - type, L, N, res, cb)) + FFC_PARAM_MODE_GENERATE, + type, L, N, res, cb)) return 0; ossl_ffc_params_enable_flags(params, FFC_PARAM_FLAG_VALIDATE_LEGACY, 1); diff --git a/crypto/ffc/ffc_params_validate.c b/crypto/ffc/ffc_params_validate.c index c1b4cf05d200..36ea9449b6ae 100644 --- a/crypto/ffc/ffc_params_validate.c +++ b/crypto/ffc/ffc_params_validate.c @@ -21,9 +21,9 @@ /* FIPS186-4 A.2.2 Unverifiable partial validation of Generator g */ int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, - const BIGNUM *p, const BIGNUM *q, - const BIGNUM *g, BIGNUM *tmp, - int *ret) + const BIGNUM *p, const BIGNUM *q, + const BIGNUM *g, BIGNUM *tmp, + int *ret) { /* * A.2.2 Step (1) AND @@ -50,8 +50,8 @@ int ossl_ffc_params_validate_unverifiable_g(BN_CTX *ctx, BN_MONT_CTX *mont, } int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx, - const FFC_PARAMS *params, int type, - int *res, BN_GENCB *cb) + const FFC_PARAMS *params, int type, + int *res, BN_GENCB *cb) { size_t L, N; @@ -62,14 +62,14 @@ int ossl_ffc_params_FIPS186_4_validate(OSSL_LIB_CTX *libctx, L = BN_num_bits(params->p); N = BN_num_bits(params->q); return ossl_ffc_params_FIPS186_4_gen_verify(libctx, (FFC_PARAMS *)params, - FFC_PARAM_MODE_VERIFY, type, - L, N, res, cb); + FFC_PARAM_MODE_VERIFY, type, + L, N, res, cb); } /* This may be used in FIPS mode to validate deprecated FIPS-186-2 Params */ int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx, - const FFC_PARAMS *params, int type, - int *res, BN_GENCB *cb) + const FFC_PARAMS *params, int type, + int *res, BN_GENCB *cb) { size_t L, N; @@ -82,8 +82,8 @@ int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx, L = BN_num_bits(params->p); N = BN_num_bits(params->q); return ossl_ffc_params_FIPS186_2_gen_verify(libctx, (FFC_PARAMS *)params, - FFC_PARAM_MODE_VERIFY, type, - L, N, res, cb); + FFC_PARAM_MODE_VERIFY, type, + L, N, res, cb); } /* @@ -93,11 +93,11 @@ int ossl_ffc_params_FIPS186_2_validate(OSSL_LIB_CTX *libctx, * this test. */ int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params, - int paramstype, int *res) + int paramstype, int *res) { int ret; int tmpres = 0; - FFC_PARAMS tmpparams = {0}; + FFC_PARAMS tmpparams = { 0 }; if (params == NULL) return 0; @@ -114,11 +114,11 @@ int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *para #ifndef FIPS_MODULE if (params->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) ret = ossl_ffc_params_FIPS186_2_validate(libctx, &tmpparams, paramstype, - res, NULL); + res, NULL); else #endif ret = ossl_ffc_params_FIPS186_4_validate(libctx, &tmpparams, paramstype, - res, NULL); + res, NULL); #ifndef OPENSSL_NO_DH if (ret == FFC_PARAM_RET_STATUS_FAILED && (*res & FFC_ERROR_NOT_SUITABLE_GENERATOR) != 0) { @@ -137,7 +137,7 @@ int ossl_ffc_params_simple_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *para * p and q. */ int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params, - int paramstype, int *res) + int paramstype, int *res) { int tmpres = 0; @@ -149,15 +149,15 @@ int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params #ifdef FIPS_MODULE return ossl_ffc_params_FIPS186_4_validate(libctx, params, paramstype, - res, NULL); + res, NULL); #else if (params->seed != NULL) { if (params->flags & FFC_PARAM_FLAG_VALIDATE_LEGACY) return ossl_ffc_params_FIPS186_2_validate(libctx, params, paramstype, - res, NULL); + res, NULL); else return ossl_ffc_params_FIPS186_4_validate(libctx, params, paramstype, - res, NULL); + res, NULL); } else { int ret = 0; @@ -168,15 +168,15 @@ int ossl_ffc_params_full_validate(OSSL_LIB_CTX *libctx, const FFC_PARAMS *params if ((ctx = BN_CTX_new_ex(libctx)) == NULL) return 0; if (BN_check_prime(params->q, ctx, NULL) != 1) { -# ifndef OPENSSL_NO_DSA +#ifndef OPENSSL_NO_DSA ERR_raise(ERR_LIB_DSA, DSA_R_Q_NOT_PRIME); -# endif +#endif ret = 0; } if (ret && BN_check_prime(params->p, ctx, NULL) != 1) { -# ifndef OPENSSL_NO_DSA +#ifndef OPENSSL_NO_DSA ERR_raise(ERR_LIB_DSA, DSA_R_P_NOT_PRIME); -# endif +#endif ret = 0; } BN_CTX_free(ctx); |
