Coverage Report

Created: 2025-04-22 06:18

/src/openssl/crypto/ec/ec_print.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2002-2020 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 <string.h> /* strlen */
11
#include <openssl/crypto.h>
12
#include "ec_local.h"
13
14
static const char *HEX_DIGITS = "0123456789ABCDEF";
15
16
/* the return value must be freed (using OPENSSL_free()) */
17
char *EC_POINT_point2hex(const EC_GROUP *group,
18
                         const EC_POINT *point,
19
                         point_conversion_form_t form, BN_CTX *ctx)
20
0
{
21
0
    char *ret, *p;
22
0
    size_t buf_len = 0, i;
23
0
    unsigned char *buf = NULL, *pbuf;
24
25
0
    buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
26
27
0
    if (buf_len == 0)
28
0
        return NULL;
29
30
0
    ret = OPENSSL_malloc(buf_len * 2 + 2);
31
0
    if (ret == NULL) {
32
0
        OPENSSL_free(buf);
33
0
        return NULL;
34
0
    }
35
0
    p = ret;
36
0
    pbuf = buf;
37
0
    for (i = buf_len; i > 0; i--) {
38
0
        int v = (int)*(pbuf++);
39
0
        *(p++) = HEX_DIGITS[v >> 4];
40
0
        *(p++) = HEX_DIGITS[v & 0x0F];
41
0
    }
42
0
    *p = '\0';
43
44
0
    OPENSSL_free(buf);
45
46
0
    return ret;
47
0
}
48
49
EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
50
                             const char *hex, EC_POINT *point, BN_CTX *ctx)
51
0
{
52
0
    int ok = 0;
53
0
    unsigned char *oct_buf = NULL;
54
0
    size_t len, oct_buf_len = 0;
55
0
    EC_POINT *pt = NULL;
56
57
0
    if (group == NULL || hex == NULL)
58
0
        return NULL;
59
60
0
    if (point == NULL) {
61
0
        pt = EC_POINT_new(group);
62
0
        if (pt == NULL)
63
0
            goto err;
64
0
    } else {
65
0
        pt = point;
66
0
    }
67
68
0
    len = strlen(hex) / 2;
69
0
    oct_buf = OPENSSL_malloc(len);
70
0
    if (oct_buf == NULL)
71
0
        goto err;
72
73
0
    if (!OPENSSL_hexstr2buf_ex(oct_buf, len, &oct_buf_len, hex, '\0')
74
0
        || !EC_POINT_oct2point(group, pt, oct_buf, oct_buf_len, ctx))
75
0
        goto err;
76
0
    ok = 1;
77
0
err:
78
0
    OPENSSL_clear_free(oct_buf, oct_buf_len);
79
0
    if (!ok) {
80
0
        if (pt != point)
81
0
            EC_POINT_clear_free(pt);
82
0
        pt = NULL;
83
0
    }
84
0
    return pt;
85
0
}