coverity 1462548 Resource leak
[openssl.git] / crypto / ffc / ffc_key_validate.c
1 /*
2  * Copyright 2019-2020 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 ffc_validate_public_key_partial(const FFC_PARAMS *params,
20                                     const BIGNUM *pub_key, int *ret)
21 {
22     int ok = 0;
23     BIGNUM *tmp = NULL;
24     BN_CTX *ctx = NULL;
25
26     *ret = 0;
27     ctx = BN_CTX_new_ex(NULL);
28     if (ctx == NULL)
29         goto err;
30
31     BN_CTX_start(ctx);
32     tmp = BN_CTX_get(ctx);
33     /* Step(1): Verify pub_key >= 2 */
34     if (tmp == NULL
35         || !BN_set_word(tmp, 1))
36         goto err;
37     if (BN_cmp(pub_key, tmp) <= 0) {
38         *ret |= FFC_ERROR_PUBKEY_TOO_SMALL;
39         goto err;
40     }
41     /* Step(1): Verify pub_key <=  p-2 */
42     if (BN_copy(tmp, params->p) == NULL
43         || !BN_sub_word(tmp, 1))
44         goto err;
45     if (BN_cmp(pub_key, tmp) >= 0) {
46         *ret |= FFC_ERROR_PUBKEY_TOO_LARGE;
47         goto err;
48     }
49     ok = 1;
50  err:
51     if (ctx != NULL) {
52         BN_CTX_end(ctx);
53         BN_CTX_free(ctx);
54     }
55     return ok;
56 }
57
58 /*
59  * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
60  */
61 int ffc_validate_public_key(const FFC_PARAMS *params, const BIGNUM *pub_key,
62                             int *ret)
63 {
64     int ok = 0;
65     BIGNUM *tmp = NULL;
66     BN_CTX *ctx = NULL;
67
68     if (!ffc_validate_public_key_partial(params, pub_key, ret))
69         return 0;
70
71     if (params->q != NULL) {
72         ctx = BN_CTX_new_ex(NULL);
73         if (ctx == NULL)
74             goto err;
75         BN_CTX_start(ctx);
76         tmp = BN_CTX_get(ctx);
77
78         /* Check pub_key^q == 1 mod p */
79         if (tmp == NULL
80             || !BN_mod_exp(tmp, pub_key, params->q, params->p, ctx))
81             goto err;
82         if (!BN_is_one(tmp)) {
83             *ret |= FFC_ERROR_PUBKEY_INVALID;
84             goto err;
85         }
86     }
87
88     ok = 1;
89  err:
90     if (ctx != NULL) {
91         BN_CTX_end(ctx);
92         BN_CTX_free(ctx);
93     }
94     return ok;
95 }
96
97 /*
98  * See SP800-56Ar3 Section 5.6.2.1.2: Owner assurance of Private key validity.
99  * Verifies priv_key is in the range [1..upper-1]. The passed in value of upper
100  * is normally params->q but can be 2^N for approved safe prime groups.
101  * Note: This assumes that the domain parameters are valid.
102  */
103 int ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv, int *ret)
104 {
105     int ok = 0;
106
107     *ret = 0;
108
109     if (BN_cmp(priv, BN_value_one()) < 0) {
110         *ret |= FFC_ERROR_PRIVKEY_TOO_SMALL;
111         goto err;
112     }
113     if (BN_cmp(priv, upper) >= 0) {
114         *ret |= FFC_ERROR_PRIVKEY_TOO_LARGE;
115         goto err;
116     }
117     ok = 1;
118 err:
119     return ok;
120 }