Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ec/ec_print.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2018 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 <openssl/crypto.h>
11
#include <openssl/err.h>
12
#include "ec_lcl.h"
13
14
BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
15
                          const EC_POINT *point,
16
                          point_conversion_form_t form,
17
                          BIGNUM *ret, BN_CTX *ctx)
18
0
{
19
0
    size_t buf_len = 0;
20
0
    unsigned char *buf;
21
0
22
0
    buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
23
0
24
0
    if (buf_len == 0)
25
0
        return NULL;
26
0
27
0
    ret = BN_bin2bn(buf, buf_len, ret);
28
0
29
0
    OPENSSL_free(buf);
30
0
31
0
    return ret;
32
0
}
33
34
EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
35
                            const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
36
0
{
37
0
    size_t buf_len = 0;
38
0
    unsigned char *buf;
39
0
    EC_POINT *ret;
40
0
41
0
    if ((buf_len = BN_num_bytes(bn)) == 0)
42
0
        return NULL;
43
0
    if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
44
0
        ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
45
0
        return NULL;
46
0
    }
47
0
48
0
    if (!BN_bn2bin(bn, buf)) {
49
0
        OPENSSL_free(buf);
50
0
        return NULL;
51
0
    }
52
0
53
0
    if (point == NULL) {
54
0
        if ((ret = EC_POINT_new(group)) == NULL) {
55
0
            OPENSSL_free(buf);
56
0
            return NULL;
57
0
        }
58
0
    } else
59
0
        ret = point;
60
0
61
0
    if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
62
0
        if (ret != point)
63
0
            EC_POINT_clear_free(ret);
64
0
        OPENSSL_free(buf);
65
0
        return NULL;
66
0
    }
67
0
68
0
    OPENSSL_free(buf);
69
0
    return ret;
70
0
}
71
72
static const char *HEX_DIGITS = "0123456789ABCDEF";
73
74
/* the return value must be freed (using OPENSSL_free()) */
75
char *EC_POINT_point2hex(const EC_GROUP *group,
76
                         const EC_POINT *point,
77
                         point_conversion_form_t form, BN_CTX *ctx)
78
0
{
79
0
    char *ret, *p;
80
0
    size_t buf_len = 0, i;
81
0
    unsigned char *buf = NULL, *pbuf;
82
0
83
0
    buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
84
0
85
0
    if (buf_len == 0)
86
0
        return NULL;
87
0
88
0
    ret = OPENSSL_malloc(buf_len * 2 + 2);
89
0
    if (ret == NULL) {
90
0
        OPENSSL_free(buf);
91
0
        return NULL;
92
0
    }
93
0
    p = ret;
94
0
    pbuf = buf;
95
0
    for (i = buf_len; i > 0; i--) {
96
0
        int v = (int)*(pbuf++);
97
0
        *(p++) = HEX_DIGITS[v >> 4];
98
0
        *(p++) = HEX_DIGITS[v & 0x0F];
99
0
    }
100
0
    *p = '\0';
101
0
102
0
    OPENSSL_free(buf);
103
0
104
0
    return ret;
105
0
}
106
107
EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
108
                             const char *buf, EC_POINT *point, BN_CTX *ctx)
109
0
{
110
0
    EC_POINT *ret = NULL;
111
0
    BIGNUM *tmp_bn = NULL;
112
0
113
0
    if (!BN_hex2bn(&tmp_bn, buf))
114
0
        return NULL;
115
0
116
0
    ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
117
0
118
0
    BN_clear_free(tmp_bn);
119
0
120
0
    return ret;
121
0
}