Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ec/ec_check.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2016 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 "ec_lcl.h"
11
#include <openssl/err.h>
12
13
int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx)
14
0
{
15
0
    int ret = 0;
16
0
    const BIGNUM *order;
17
0
    BN_CTX *new_ctx = NULL;
18
0
    EC_POINT *point = NULL;
19
0
20
0
    /* Custom curves assumed to be correct */
21
0
    if ((group->meth->flags & EC_FLAGS_CUSTOM_CURVE) != 0)
22
0
        return 1;
23
0
24
0
    if (ctx == NULL) {
25
0
        ctx = new_ctx = BN_CTX_new();
26
0
        if (ctx == NULL) {
27
0
            ECerr(EC_F_EC_GROUP_CHECK, ERR_R_MALLOC_FAILURE);
28
0
            goto err;
29
0
        }
30
0
    }
31
0
32
0
    /* check the discriminant */
33
0
    if (!EC_GROUP_check_discriminant(group, ctx)) {
34
0
        ECerr(EC_F_EC_GROUP_CHECK, EC_R_DISCRIMINANT_IS_ZERO);
35
0
        goto err;
36
0
    }
37
0
38
0
    /* check the generator */
39
0
    if (group->generator == NULL) {
40
0
        ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_GENERATOR);
41
0
        goto err;
42
0
    }
43
0
    if (EC_POINT_is_on_curve(group, group->generator, ctx) <= 0) {
44
0
        ECerr(EC_F_EC_GROUP_CHECK, EC_R_POINT_IS_NOT_ON_CURVE);
45
0
        goto err;
46
0
    }
47
0
48
0
    /* check the order of the generator */
49
0
    if ((point = EC_POINT_new(group)) == NULL)
50
0
        goto err;
51
0
    order = EC_GROUP_get0_order(group);
52
0
    if (order == NULL)
53
0
        goto err;
54
0
    if (BN_is_zero(order)) {
55
0
        ECerr(EC_F_EC_GROUP_CHECK, EC_R_UNDEFINED_ORDER);
56
0
        goto err;
57
0
    }
58
0
59
0
    if (!EC_POINT_mul(group, point, order, NULL, NULL, ctx))
60
0
        goto err;
61
0
    if (!EC_POINT_is_at_infinity(group, point)) {
62
0
        ECerr(EC_F_EC_GROUP_CHECK, EC_R_INVALID_GROUP_ORDER);
63
0
        goto err;
64
0
    }
65
0
66
0
    ret = 1;
67
0
68
0
 err:
69
0
    BN_CTX_free(new_ctx);
70
0
    EC_POINT_free(point);
71
0
    return ret;
72
0
}