]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dh/dh_group_params.c
Fix external symbols for bn
[thirdparty/openssl.git] / crypto / dh / dh_group_params.c
CommitLineData
ca2bf555
SL
1/*
2 * Copyright 2017-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/* DH parameters from RFC7919 and RFC3526 */
11
ada66e78
P
12/*
13 * DH low level APIs are deprecated for public use, but still ok for
14 * internal use.
15 */
16#include "internal/deprecated.h"
17
ca2bf555
SL
18#include <stdio.h>
19#include "internal/cryptlib.h"
7165593c 20#include "internal/ffc.h"
ca2bf555
SL
21#include "dh_local.h"
22#include <openssl/bn.h>
23#include <openssl/objects.h>
c829c23b 24#include "internal/nelem.h"
8083fd3a 25#include "crypto/dh.h"
b03ec3b5 26#include "e_os.h" /* strcasecmp */
55f02cb6 27
c829c23b 28static DH *dh_param_init(OSSL_LIB_CTX *libctx, const DH_NAMED_GROUP *group)
ca2bf555 29{
d8652be0 30 DH *dh = dh_new_ex(libctx);
ca2bf555
SL
31
32 if (dh == NULL)
33 return NULL;
34
c829c23b
RL
35 ossl_ffc_named_group_set_pqg(&dh->params, group);
36 dh->params.nid = ossl_ffc_named_group_get_uid(group);
37 dh->length = BN_num_bits(dh->params.q);
ca2bf555
SL
38 dh->dirty_cnt++;
39 return dh;
40}
41
c829c23b 42DH *dh_new_by_nid_ex(OSSL_LIB_CTX *libctx, int nid)
ca2bf555 43{
c829c23b 44 const DH_NAMED_GROUP *group;
55f02cb6 45
c829c23b
RL
46 if ((group = ossl_ffc_uid_to_dh_named_group(nid)) != NULL)
47 return dh_param_init(libctx, group);
7165593c 48
9311d0c4 49 ERR_raise(ERR_LIB_DH, DH_R_INVALID_PARAMETER_NID);
55f02cb6 50 return NULL;
ca2bf555
SL
51}
52
8083fd3a
SL
53DH *DH_new_by_nid(int nid)
54{
d8652be0 55 return dh_new_by_nid_ex(NULL, nid);
7165593c
SL
56}
57
738ee181 58void dh_cache_named_group(DH *dh)
ca2bf555 59{
c829c23b 60 const DH_NAMED_GROUP *group;
55f02cb6
SL
61
62 if (dh == NULL)
738ee181
SL
63 return;
64
65 dh->params.nid = NID_undef; /* flush cached value */
ca2bf555 66
738ee181
SL
67 /* Exit if p or g is not set */
68 if (dh->params.p == NULL
69 || dh->params.g == NULL)
70 return;
ca2bf555 71
c829c23b
RL
72 if ((group = ossl_ffc_numbers_to_dh_named_group(dh->params.p,
73 dh->params.q,
74 dh->params.g)) != NULL) {
75 if (dh->params.q == NULL)
76 dh->params.q = (BIGNUM *)ossl_ffc_named_group_get_q(group);
77 /* cache the nid */
78 dh->params.nid = ossl_ffc_named_group_get_uid(group);
79 dh->length = BN_num_bits(dh->params.q);
80 dh->dirty_cnt++;
ca2bf555 81 }
738ee181
SL
82}
83
4718326a
SL
84int ossl_dh_is_named_safe_prime_group(const DH *dh)
85{
86 int id = DH_get_nid(dh);
87
88 /*
89 * Exclude RFC5114 groups (id = 1..3) since they do not have
90 * q = (p - 1) / 2
91 */
92 return (id > 3);
93}
94
738ee181
SL
95int DH_get_nid(const DH *dh)
96{
97 if (dh == NULL)
98 return NID_undef;
99
100 return dh->params.nid;
ca2bf555 101}