Coverage Report

Created: 2023-09-25 06:45

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