• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 <stdio.h>
11 #include "internal/cryptlib.h"
12 #include "dh_local.h"
13 #include <openssl/bn.h>
14 #include <openssl/objects.h>
15 #include "crypto/bn_dh.h"
16 
dh_param_init(const BIGNUM * p,int32_t nbits)17 static DH *dh_param_init(const BIGNUM *p, int32_t nbits)
18 {
19     DH *dh = DH_new();
20     if (dh == NULL)
21         return NULL;
22     dh->p = (BIGNUM *)p;
23     dh->g = (BIGNUM *)&_bignum_const_2;
24     dh->length = nbits;
25     return dh;
26 }
27 
DH_new_by_nid(int nid)28 DH *DH_new_by_nid(int nid)
29 {
30     switch (nid) {
31     case NID_ffdhe2048:
32         return dh_param_init(&_bignum_ffdhe2048_p, 225);
33     case NID_ffdhe3072:
34         return dh_param_init(&_bignum_ffdhe3072_p, 275);
35     case NID_ffdhe4096:
36         return dh_param_init(&_bignum_ffdhe4096_p, 325);
37     case NID_ffdhe6144:
38         return dh_param_init(&_bignum_ffdhe6144_p, 375);
39     case NID_ffdhe8192:
40         return dh_param_init(&_bignum_ffdhe8192_p, 400);
41     default:
42         DHerr(DH_F_DH_NEW_BY_NID, DH_R_INVALID_PARAMETER_NID);
43         return NULL;
44     }
45 }
46 
DH_get_nid(const DH * dh)47 int DH_get_nid(const DH *dh)
48 {
49     int nid;
50 
51     if (BN_get_word(dh->g) != 2)
52         return NID_undef;
53     if (!BN_cmp(dh->p, &_bignum_ffdhe2048_p))
54         nid = NID_ffdhe2048;
55     else if (!BN_cmp(dh->p, &_bignum_ffdhe3072_p))
56         nid = NID_ffdhe3072;
57     else if (!BN_cmp(dh->p, &_bignum_ffdhe4096_p))
58         nid = NID_ffdhe4096;
59     else if (!BN_cmp(dh->p, &_bignum_ffdhe6144_p))
60         nid = NID_ffdhe6144;
61     else if (!BN_cmp(dh->p, &_bignum_ffdhe8192_p))
62         nid = NID_ffdhe8192;
63     else
64         return NID_undef;
65     if (dh->q != NULL) {
66         BIGNUM *q = BN_dup(dh->p);
67 
68         /* Check q = p * 2 + 1 we already know q is odd, so just shift right */
69         if (q == NULL || !BN_rshift1(q, q) || !BN_cmp(dh->q, q))
70             nid = NID_undef;
71         BN_free(q);
72     }
73     return nid;
74 }
75