Coverage Report

Created: 2018-08-29 13:53

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