| 1 | /* | 
|---|
| 2 | * Copyright 2017 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 <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 |  | 
|---|
| 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 | dh->dirty_cnt++; | 
|---|
| 26 | return dh; | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | DH *DH_new_by_nid(int nid) | 
|---|
| 30 | { | 
|---|
| 31 | switch (nid) { | 
|---|
| 32 | case NID_ffdhe2048: | 
|---|
| 33 | return dh_param_init(&_bignum_ffdhe2048_p, 225); | 
|---|
| 34 | case NID_ffdhe3072: | 
|---|
| 35 | return dh_param_init(&_bignum_ffdhe3072_p, 275); | 
|---|
| 36 | case NID_ffdhe4096: | 
|---|
| 37 | return dh_param_init(&_bignum_ffdhe4096_p, 325); | 
|---|
| 38 | case NID_ffdhe6144: | 
|---|
| 39 | return dh_param_init(&_bignum_ffdhe6144_p, 375); | 
|---|
| 40 | case NID_ffdhe8192: | 
|---|
| 41 | return dh_param_init(&_bignum_ffdhe8192_p, 400); | 
|---|
| 42 | default: | 
|---|
| 43 | DHerr(DH_F_DH_NEW_BY_NID, DH_R_INVALID_PARAMETER_NID); | 
|---|
| 44 | return NULL; | 
|---|
| 45 | } | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | int DH_get_nid(const DH *dh) | 
|---|
| 49 | { | 
|---|
| 50 | int nid; | 
|---|
| 51 |  | 
|---|
| 52 | if (BN_get_word(dh->g) != 2) | 
|---|
| 53 | return NID_undef; | 
|---|
| 54 | if (!BN_cmp(dh->p, &_bignum_ffdhe2048_p)) | 
|---|
| 55 | nid = NID_ffdhe2048; | 
|---|
| 56 | else if (!BN_cmp(dh->p, &_bignum_ffdhe3072_p)) | 
|---|
| 57 | nid = NID_ffdhe3072; | 
|---|
| 58 | else if (!BN_cmp(dh->p, &_bignum_ffdhe4096_p)) | 
|---|
| 59 | nid = NID_ffdhe4096; | 
|---|
| 60 | else if (!BN_cmp(dh->p, &_bignum_ffdhe6144_p)) | 
|---|
| 61 | nid = NID_ffdhe6144; | 
|---|
| 62 | else if (!BN_cmp(dh->p, &_bignum_ffdhe8192_p)) | 
|---|
| 63 | nid = NID_ffdhe8192; | 
|---|
| 64 | else | 
|---|
| 65 | return NID_undef; | 
|---|
| 66 | if (dh->q != NULL) { | 
|---|
| 67 | BIGNUM *q = BN_dup(dh->p); | 
|---|
| 68 |  | 
|---|
| 69 | /* Check q = p * 2 + 1 we already know q is odd, so just shift right */ | 
|---|
| 70 | if (q == NULL || !BN_rshift1(q, q) || !BN_cmp(dh->q, q)) | 
|---|
| 71 | nid = NID_undef; | 
|---|
| 72 | BN_free(q); | 
|---|
| 73 | } | 
|---|
| 74 | return nid; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|