2 * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/ffc.h"
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]
17 * ret contains 0 on success, or error flags (see FFC_ERROR_PUBKEY_TOO_SMALL)
19 int ossl_ffc_validate_public_key_partial(const FFC_PARAMS
*params
,
20 const BIGNUM
*pub_key
, int *ret
)
27 if (params
== NULL
|| pub_key
== NULL
|| params
->p
== NULL
) {
28 *ret
= FFC_ERROR_PASSED_NULL_PARAM
;
32 ctx
= BN_CTX_new_ex(NULL
);
37 tmp
= BN_CTX_get(ctx
);
38 /* Step(1): Verify pub_key >= 2 */
40 || !BN_set_word(tmp
, 1))
42 if (BN_cmp(pub_key
, tmp
) <= 0) {
43 *ret
|= FFC_ERROR_PUBKEY_TOO_SMALL
;
46 /* Step(1): Verify pub_key <= p-2 */
47 if (BN_copy(tmp
, params
->p
) == NULL
48 || !BN_sub_word(tmp
, 1))
50 if (BN_cmp(pub_key
, tmp
) >= 0) {
51 *ret
|= FFC_ERROR_PUBKEY_TOO_LARGE
;
64 * See SP800-56Ar3 Section 5.6.2.3.1 : FFC Full public key validation.
66 int ossl_ffc_validate_public_key(const FFC_PARAMS
*params
,
67 const BIGNUM
*pub_key
, int *ret
)
73 if (!ossl_ffc_validate_public_key_partial(params
, pub_key
, ret
))
76 if (params
->q
!= NULL
) {
77 ctx
= BN_CTX_new_ex(NULL
);
81 tmp
= BN_CTX_get(ctx
);
83 /* Check pub_key^q == 1 mod p */
85 || !BN_mod_exp(tmp
, pub_key
, params
->q
, params
->p
, ctx
))
87 if (!BN_is_one(tmp
)) {
88 *ret
|= FFC_ERROR_PUBKEY_INVALID
;
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.
108 int ossl_ffc_validate_private_key(const BIGNUM
*upper
, const BIGNUM
*priv
,
115 if (priv
== NULL
|| upper
== NULL
) {
116 *ret
= FFC_ERROR_PASSED_NULL_PARAM
;
119 if (BN_cmp(priv
, BN_value_one()) < 0) {
120 *ret
|= FFC_ERROR_PRIVKEY_TOO_SMALL
;
123 if (BN_cmp(priv
, upper
) >= 0) {
124 *ret
|= FFC_ERROR_PRIVKEY_TOO_LARGE
;