/[smecontribs]/rpms/openssl3/contribs10/0044-FIPS-140-3-keychecks.patch
ViewVC logotype

Contents of /rpms/openssl3/contribs10/0044-FIPS-140-3-keychecks.patch

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Wed Jan 31 17:24:38 2024 UTC (9 months, 4 weeks ago) by jpp
Branch: MAIN
CVS Tags: openssl3-3_0_7-5_el7_sme_1, HEAD
Initial import

1 diff -up openssl-3.0.1/crypto/dh/dh_key.c.fips3 openssl-3.0.1/crypto/dh/dh_key.c
2 --- openssl-3.0.1/crypto/dh/dh_key.c.fips3 2022-07-18 16:01:41.159543735 +0200
3 +++ openssl-3.0.1/crypto/dh/dh_key.c 2022-07-18 16:24:30.251388248 +0200
4 @@ -43,6 +43,9 @@ int ossl_dh_compute_key(unsigned char *k
5 BN_MONT_CTX *mont = NULL;
6 BIGNUM *z = NULL, *pminus1;
7 int ret = -1;
8 +#ifdef FIPS_MODULE
9 + int validate = 0;
10 +#endif
11
12 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
13 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
14 @@ -54,6 +57,13 @@ int ossl_dh_compute_key(unsigned char *k
15 return 0;
16 }
17
18 +#ifdef FIPS_MODULE
19 + if (DH_check_pub_key(dh, pub_key, &validate) <= 0) {
20 + ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
21 + return 0;
22 + }
23 +#endif
24 +
25 ctx = BN_CTX_new_ex(dh->libctx);
26 if (ctx == NULL)
27 goto err;
28 @@ -262,6 +272,9 @@ static int generate_key(DH *dh)
29 #endif
30 BN_CTX *ctx = NULL;
31 BIGNUM *pub_key = NULL, *priv_key = NULL;
32 +#ifdef FIPS_MODULE
33 + int validate = 0;
34 +#endif
35
36 if (BN_num_bits(dh->params.p) > OPENSSL_DH_MAX_MODULUS_BITS) {
37 ERR_raise(ERR_LIB_DH, DH_R_MODULUS_TOO_LARGE);
38 @@ -354,8 +367,23 @@ static int generate_key(DH *dh)
39 if (!ossl_dh_generate_public_key(ctx, dh, priv_key, pub_key))
40 goto err;
41
42 +#ifdef FIPS_MODULE
43 + if (DH_check_pub_key(dh, pub_key, &validate) <= 0) {
44 + ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
45 + goto err;
46 + }
47 +#endif
48 +
49 dh->pub_key = pub_key;
50 dh->priv_key = priv_key;
51 +#ifdef FIPS_MODULE
52 + if (ossl_dh_check_pairwise(dh) <= 0) {
53 + dh->pub_key = dh->priv_key = NULL;
54 + ERR_raise(ERR_LIB_DH, DH_R_CHECK_PUBKEY_INVALID);
55 + goto err;
56 + }
57 +#endif
58 +
59 dh->dirty_cnt++;
60 ok = 1;
61 err:
62 diff -up openssl-3.0.1/crypto/ec/ec_key.c.fips3 openssl-3.0.1/crypto/ec/ec_key.c
63 diff -up openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c.fips3 openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c
64 --- openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c.fips3 2022-07-25 13:42:46.814952053 +0200
65 +++ openssl-3.0.1/providers/implementations/exchange/ecdh_exch.c 2022-07-25 13:52:12.292065706 +0200
66 @@ -488,6 +488,25 @@ int ecdh_plain_derive(void *vpecdhctx, u
67 }
68
69 ppubkey = EC_KEY_get0_public_key(pecdhctx->peerk);
70 +#ifdef FIPS_MODULE
71 + {
72 + BN_CTX *bn_ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(privk));
73 + int check = 0;
74 +
75 + if (bn_ctx == NULL) {
76 + ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
77 + goto end;
78 + }
79 +
80 + check = ossl_ec_key_public_check(pecdhctx->peerk, bn_ctx);
81 + BN_CTX_free(bn_ctx);
82 +
83 + if (check <= 0) {
84 + ERR_raise(ERR_LIB_PROV, EC_R_INVALID_PEER_KEY);
85 + goto end;
86 + }
87 + }
88 +#endif
89
90 retlen = ECDH_compute_key(secret, size, ppubkey, privk, NULL);
91
92 diff -up openssl-3.0.1/crypto/ec/ec_key.c.fips3 openssl-3.0.1/crypto/ec/ec_key.c
93 --- openssl-3.0.1/crypto/ec/ec_key.c.fips3 2022-07-25 14:03:34.420222507 +0200
94 +++ openssl-3.0.1/crypto/ec/ec_key.c 2022-07-25 14:09:00.728164294 +0200
95 @@ -336,6 +336,11 @@ static int ec_generate_key(EC_KEY *eckey
96
97 OSSL_SELF_TEST_get_callback(eckey->libctx, &cb, &cbarg);
98 ok = ecdsa_keygen_pairwise_test(eckey, cb, cbarg);
99 +
100 +#ifdef FIPS_MODULE
101 + ok &= ossl_ec_key_public_check(eckey, ctx);
102 + ok &= ossl_ec_key_pairwise_check(eckey, ctx);
103 +#endif /* FIPS_MODULE */
104 }
105 err:
106 /* Step (9): If there is an error return an invalid keypair. */
107 diff -up openssl-3.0.1/crypto/rsa/rsa_gen.c.fips3 openssl-3.0.1/crypto/rsa/rsa_gen.c
108 --- openssl-3.0.1/crypto/rsa/rsa_gen.c.fips3 2022-07-25 17:02:17.807271297 +0200
109 +++ openssl-3.0.1/crypto/rsa/rsa_gen.c 2022-07-25 17:18:24.931959649 +0200
110 @@ -23,6 +23,7 @@
111 #include <time.h>
112 #include "internal/cryptlib.h"
113 #include <openssl/bn.h>
114 +#include <openssl/obj_mac.h>
115 #include <openssl/self_test.h>
116 #include "prov/providercommon.h"
117 #include "rsa_local.h"
118 @@ -476,52 +476,43 @@ static int rsa_keygen(OSSL_LIB_CTX *libc
119 static int rsa_keygen_pairwise_test(RSA *rsa, OSSL_CALLBACK *cb, void *cbarg)
120 {
121 int ret = 0;
122 - unsigned int ciphertxt_len;
123 - unsigned char *ciphertxt = NULL;
124 - const unsigned char plaintxt[16] = {0};
125 - unsigned char *decoded = NULL;
126 - unsigned int decoded_len;
127 - unsigned int plaintxt_len = (unsigned int)sizeof(plaintxt_len);
128 - int padding = RSA_PKCS1_PADDING;
129 + unsigned int signature_len;
130 + unsigned char *signature = NULL;
131 OSSL_SELF_TEST *st = NULL;
132 + static const unsigned char dgst[] = {
133 + 0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81,
134 + 0x48, 0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28,
135 + 0x4a, 0xdd, 0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69
136 + };
137
138 st = OSSL_SELF_TEST_new(cb, cbarg);
139 if (st == NULL)
140 goto err;
141 OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_PCT,
142 + /* No special name for RSA signature PCT*/
143 OSSL_SELF_TEST_DESC_PCT_RSA_PKCS1);
144
145 - ciphertxt_len = RSA_size(rsa);
146 + signature_len = RSA_size(rsa);
147 - /*
148 - * RSA_private_encrypt() and RSA_private_decrypt() requires the 'to'
149 - * parameter to be a maximum of RSA_size() - allocate space for both.
150 - */
151 - ciphertxt = OPENSSL_zalloc(ciphertxt_len * 2);
152 - if (ciphertxt == NULL)
153 + signature = OPENSSL_zalloc(signature_len);
154 + if (signature == NULL)
155 goto err;
156 - decoded = ciphertxt + ciphertxt_len;
157
158 - ciphertxt_len = RSA_public_encrypt(plaintxt_len, plaintxt, ciphertxt, rsa,
159 - padding);
160 - if (ciphertxt_len <= 0)
161 + if (RSA_sign(NID_sha256, dgst, sizeof(dgst), signature, &signature_len, rsa) <= 0)
162 goto err;
163 - if (ciphertxt_len == plaintxt_len
164 - && memcmp(ciphertxt, plaintxt, plaintxt_len) == 0)
165 +
166 + if (signature_len <= 0)
167 goto err;
168
169 - OSSL_SELF_TEST_oncorrupt_byte(st, ciphertxt);
170 + OSSL_SELF_TEST_oncorrupt_byte(st, signature);
171
172 - decoded_len = RSA_private_decrypt(ciphertxt_len, ciphertxt, decoded, rsa,
173 - padding);
174 - if (decoded_len != plaintxt_len
175 - || memcmp(decoded, plaintxt, decoded_len) != 0)
176 + if (RSA_verify(NID_sha256, dgst, sizeof(dgst), signature, signature_len, rsa) <= 0)
177 goto err;
178
179 ret = 1;
180 err:
181 OSSL_SELF_TEST_onend(st, ret);
182 OSSL_SELF_TEST_free(st);
183 - OPENSSL_free(ciphertxt);
184 + OPENSSL_free(signature);
185
186 return ret;
187 }

admin@koozali.org
ViewVC Help
Powered by ViewVC 1.2.1 RSS 2.0 feed