Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/dh/check.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/dh.h>
5
6 #include <openssl/bn.h>
7 #include <openssl/err.h>
8
9 #include "internal.h"
10
11 int dh_check_params_fast(const DH *dh) {
12 // Most operations scale with p and q.
13 if (BN_is_negative(dh->p) || !BN_is_odd(dh->p) ||
14 BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
15 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PARAMETERS);
16 return 0;
17 }
18
19 // q must be bounded by p.
20 if (dh->q != NULL && (BN_is_negative(dh->q) || BN_ucmp(dh->q, dh->p) > 0)) {
21 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PARAMETERS);
22 return 0;
23 }
24
25 // g must be an element of p's multiplicative group.
26 if (BN_is_negative(dh->g) || BN_is_zero(dh->g) ||
27 BN_ucmp(dh->g, dh->p) >= 0) {
28 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PARAMETERS);
29 return 0;
30 }
31
32 return 1;
33 }
34
35 int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *out_flags) {
36 *out_flags = 0;
37 if (!dh_check_params_fast(dh)) {
38 return 0;
39 }
40
41 BN_CTX *ctx = BN_CTX_new();
42 if (ctx == NULL) {
43 return 0;
44 }
45 BN_CTX_start(ctx);
46
47 int ok = 0;
48
49 // Check |pub_key| is greater than 1.
50 if (BN_cmp(pub_key, BN_value_one()) <= 0) {
51 *out_flags |= DH_CHECK_PUBKEY_TOO_SMALL;
52 }
53
54 // Check |pub_key| is less than |dh->p| - 1.
55 BIGNUM *tmp = BN_CTX_get(ctx);
56 if (tmp == NULL ||
57 !BN_copy(tmp, dh->p) ||
58 !BN_sub_word(tmp, 1)) {
59 goto err;
60 }
61 if (BN_cmp(pub_key, tmp) >= 0) {
62 *out_flags |= DH_CHECK_PUBKEY_TOO_LARGE;
63 }
64
65 if (dh->q != NULL) {
66 // Check |pub_key|^|dh->q| is 1 mod |dh->p|. This is necessary for RFC 5114
67 // groups which are not safe primes but pick a generator on a prime-order
68 // subgroup of size |dh->q|.
69 if (!BN_mod_exp_mont(tmp, pub_key, dh->q, dh->p, ctx, NULL)) {
70 goto err;
71 }
72 if (!BN_is_one(tmp)) {
73 *out_flags |= DH_CHECK_PUBKEY_INVALID;
74 }
75 }
76
77 ok = 1;
78
79 err:
80 BN_CTX_end(ctx);
81 BN_CTX_free(ctx);
82 return ok;
83 }
84
85
86 // DH_check confirms that the Diffie-Hellman parameters dh are valid.
87 int DH_check(const DH *dh, int *out_flags) {
88 *out_flags = 0;
89 if (!dh_check_params_fast(dh)) {
90 return 0;
91 }
92
93 // Check that p is a safe prime.
94 int ok = 0, r, q_good = 0;
95 BN_CTX *ctx = NULL;
96 BIGNUM *t1 = NULL, *t2 = NULL;
97
98 ctx = BN_CTX_new();
99 if (ctx == NULL) {
100 goto err;
101 }
102 BN_CTX_start(ctx);
103 t1 = BN_CTX_get(ctx);
104 if (t1 == NULL) {
105 goto err;
106 }
107 t2 = BN_CTX_get(ctx);
108 if (t2 == NULL) {
109 goto err;
110 }
111
112 if (dh->q) {
113 if (BN_ucmp(dh->p, dh->q) > 0) {
114 q_good = 1;
115 } else {
116 *out_flags |= DH_CHECK_INVALID_Q_VALUE;
117 }
118 }
119
120 if (q_good) {
121 if (BN_cmp(dh->g, BN_value_one()) <= 0) {
122 *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR;
123 } else if (BN_cmp(dh->g, dh->p) >= 0) {
124 *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR;
125 } else {
126 // Check g^q == 1 mod p
127 if (!BN_mod_exp_mont(t1, dh->g, dh->q, dh->p, ctx, NULL)) {
128 goto err;
129 }
130 if (!BN_is_one(t1)) {
131 *out_flags |= DH_CHECK_NOT_SUITABLE_GENERATOR;
132 }
133 }
134 r = BN_is_prime_ex(dh->q, BN_prime_checks_for_validation, ctx, NULL);
135 if (r < 0) {
136 goto err;
137 }
138 if (!r) {
139 *out_flags |= DH_CHECK_Q_NOT_PRIME;
140 }
141 // Check p == 1 mod q i.e. q divides p - 1
142 if (!BN_div(t1, t2, dh->p, dh->q, ctx)) {
143 goto err;
144 }
145 if (!BN_is_one(t2)) {
146 *out_flags |= DH_CHECK_INVALID_Q_VALUE;
147 }
148 }
149
150 r = BN_is_prime_ex(dh->p, BN_prime_checks_for_validation, ctx, NULL);
151 if (r < 0) {
152 goto err;
153 }
154 if (!r) {
155 *out_flags |= DH_CHECK_P_NOT_PRIME;
156 } else if (!dh->q) {
157 if (!BN_rshift1(t1, dh->p)) {
158 goto err;
159 }
160 r = BN_is_prime_ex(t1, BN_prime_checks_for_validation, ctx, NULL);
161 if (r < 0) {
162 goto err;
163 }
164 if (!r) {
165 *out_flags |= DH_CHECK_P_NOT_SAFE_PRIME;
166 }
167 }
168 ok = 1;
169
170 err:
171 if (ctx != NULL) {
172 BN_CTX_end(ctx);
173 BN_CTX_free(ctx);
174 }
175 return ok;
176 }