/src/openssl32/crypto/ffc/ffc_key_validate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/ffc.h" |
11 | | |
12 | | /* |
13 | | * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Partial public key validation. |
14 | | * To only be used with ephemeral FFC public keys generated using the approved |
15 | | * safe-prime groups. (Checks that the public key is in the range [2, p - 1] |
16 | | * |
17 | | * ret contains 0 on success, or error flags (see FFC_ERROR_PUBKEY_TOO_SMALL) |
18 | | */ |
19 | | int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params, |
20 | | const BIGNUM *pub_key, int *ret) |
21 | 10.6k | { |
22 | 10.6k | int ok = 0; |
23 | 10.6k | BIGNUM *tmp = NULL; |
24 | 10.6k | BN_CTX *ctx = NULL; |
25 | | |
26 | 10.6k | *ret = 0; |
27 | 10.6k | if (params == NULL || pub_key == NULL || params->p == NULL) { |
28 | 0 | *ret = FFC_ERROR_PASSED_NULL_PARAM; |
29 | 0 | return 1; |
30 | 0 | } |
31 | | |
32 | 10.6k | ctx = BN_CTX_new_ex(NULL); |
33 | 10.6k | if (ctx == NULL) |
34 | 0 | goto err; |
35 | | |
36 | 10.6k | BN_CTX_start(ctx); |
37 | 10.6k | tmp = BN_CTX_get(ctx); |
38 | | /* Step(1): Verify pub_key >= 2 */ |
39 | 10.6k | if (tmp == NULL |
40 | 10.6k | || !BN_set_word(tmp, 1)) |
41 | 0 | goto err; |
42 | 10.6k | if (BN_cmp(pub_key, tmp) <= 0) |
43 | 557 | *ret |= FFC_ERROR_PUBKEY_TOO_SMALL; |
44 | | /* Step(1): Verify pub_key <= p-2 */ |
45 | 10.6k | if (BN_copy(tmp, params->p) == NULL |
46 | 10.6k | || !BN_sub_word(tmp, 1)) |
47 | 0 | goto err; |
48 | 10.6k | if (BN_cmp(pub_key, tmp) >= 0) |
49 | 189 | *ret |= FFC_ERROR_PUBKEY_TOO_LARGE; |
50 | 10.6k | ok = 1; |
51 | 10.6k | err: |
52 | 10.6k | if (ctx != NULL) { |
53 | 10.6k | BN_CTX_end(ctx); |
54 | 10.6k | BN_CTX_free(ctx); |
55 | 10.6k | } |
56 | 10.6k | return ok; |
57 | 10.6k | } |
58 | | |
59 | | /* |
60 | | * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation. |
61 | | */ |
62 | | int ossl_ffc_validate_public_key(const FFC_PARAMS *params, |
63 | | const BIGNUM *pub_key, int *ret) |
64 | 9.13k | { |
65 | 9.13k | int ok = 0; |
66 | 9.13k | BIGNUM *tmp = NULL; |
67 | 9.13k | BN_CTX *ctx = NULL; |
68 | | |
69 | 9.13k | if (!ossl_ffc_validate_public_key_partial(params, pub_key, ret)) |
70 | 0 | return 0; |
71 | | |
72 | 9.13k | if (*ret == 0 && params->q != NULL) { |
73 | 1.71k | ctx = BN_CTX_new_ex(NULL); |
74 | 1.71k | if (ctx == NULL) |
75 | 0 | goto err; |
76 | 1.71k | BN_CTX_start(ctx); |
77 | 1.71k | tmp = BN_CTX_get(ctx); |
78 | | |
79 | | /* Check pub_key^q == 1 mod p */ |
80 | 1.71k | if (tmp == NULL |
81 | 1.71k | || !BN_mod_exp(tmp, pub_key, params->q, params->p, ctx)) |
82 | 0 | goto err; |
83 | 1.71k | if (!BN_is_one(tmp)) |
84 | 1.05k | *ret |= FFC_ERROR_PUBKEY_INVALID; |
85 | 1.71k | } |
86 | | |
87 | 9.13k | ok = 1; |
88 | 9.13k | err: |
89 | 9.13k | if (ctx != NULL) { |
90 | 1.71k | BN_CTX_end(ctx); |
91 | 1.71k | BN_CTX_free(ctx); |
92 | 1.71k | } |
93 | 9.13k | return ok; |
94 | 9.13k | } |
95 | | |
96 | | /* |
97 | | * See SP800-56Ar3 Section 5.6.2.1.2: Owner assurance of Private key validity. |
98 | | * Verifies priv_key is in the range [1..upper-1]. The passed in value of upper |
99 | | * is normally params->q but can be 2^N for approved safe prime groups. |
100 | | * Note: This assumes that the domain parameters are valid. |
101 | | */ |
102 | | int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv, |
103 | | int *ret) |
104 | 489 | { |
105 | 489 | int ok = 0; |
106 | | |
107 | 489 | *ret = 0; |
108 | | |
109 | 489 | if (priv == NULL || upper == NULL) { |
110 | 0 | *ret = FFC_ERROR_PASSED_NULL_PARAM; |
111 | 0 | goto err; |
112 | 0 | } |
113 | 489 | if (BN_cmp(priv, BN_value_one()) < 0) { |
114 | 182 | *ret |= FFC_ERROR_PRIVKEY_TOO_SMALL; |
115 | 182 | goto err; |
116 | 182 | } |
117 | 307 | if (BN_cmp(priv, upper) >= 0) { |
118 | 163 | *ret |= FFC_ERROR_PRIVKEY_TOO_LARGE; |
119 | 163 | goto err; |
120 | 163 | } |
121 | 144 | ok = 1; |
122 | 489 | err: |
123 | 489 | return ok; |
124 | 144 | } |