1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * Suppress deprecation warnings for EC low level implementations that are
12e1051a39Sopenharmony_ci * kept until removal.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <openssl/crypto.h>
17e1051a39Sopenharmony_ci#include <openssl/err.h>
18e1051a39Sopenharmony_ci#include <openssl/ec.h>
19e1051a39Sopenharmony_ci
20e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
21e1051a39Sopenharmony_ciBIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
22e1051a39Sopenharmony_ci                          const EC_POINT *point,
23e1051a39Sopenharmony_ci                          point_conversion_form_t form,
24e1051a39Sopenharmony_ci                          BIGNUM *ret, BN_CTX *ctx)
25e1051a39Sopenharmony_ci{
26e1051a39Sopenharmony_ci    size_t buf_len = 0;
27e1051a39Sopenharmony_ci    unsigned char *buf;
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ci    buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ci    if (buf_len == 0)
32e1051a39Sopenharmony_ci        return NULL;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci    ret = BN_bin2bn(buf, buf_len, ret);
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    OPENSSL_free(buf);
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ci    return ret;
39e1051a39Sopenharmony_ci}
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ciEC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
42e1051a39Sopenharmony_ci                            const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
43e1051a39Sopenharmony_ci{
44e1051a39Sopenharmony_ci    size_t buf_len = 0;
45e1051a39Sopenharmony_ci    unsigned char *buf;
46e1051a39Sopenharmony_ci    EC_POINT *ret;
47e1051a39Sopenharmony_ci
48e1051a39Sopenharmony_ci    if ((buf_len = BN_num_bytes(bn)) == 0)
49e1051a39Sopenharmony_ci        buf_len = 1;
50e1051a39Sopenharmony_ci    if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
51e1051a39Sopenharmony_ci        ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
52e1051a39Sopenharmony_ci        return NULL;
53e1051a39Sopenharmony_ci    }
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_ci    if (BN_bn2binpad(bn, buf, buf_len) < 0) {
56e1051a39Sopenharmony_ci        OPENSSL_free(buf);
57e1051a39Sopenharmony_ci        return NULL;
58e1051a39Sopenharmony_ci    }
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    if (point == NULL) {
61e1051a39Sopenharmony_ci        if ((ret = EC_POINT_new(group)) == NULL) {
62e1051a39Sopenharmony_ci            OPENSSL_free(buf);
63e1051a39Sopenharmony_ci            return NULL;
64e1051a39Sopenharmony_ci        }
65e1051a39Sopenharmony_ci    } else
66e1051a39Sopenharmony_ci        ret = point;
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_ci    if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
69e1051a39Sopenharmony_ci        if (ret != point)
70e1051a39Sopenharmony_ci            EC_POINT_clear_free(ret);
71e1051a39Sopenharmony_ci        OPENSSL_free(buf);
72e1051a39Sopenharmony_ci        return NULL;
73e1051a39Sopenharmony_ci    }
74e1051a39Sopenharmony_ci
75e1051a39Sopenharmony_ci    OPENSSL_free(buf);
76e1051a39Sopenharmony_ci    return ret;
77e1051a39Sopenharmony_ci}
78e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DEPRECATED_3_0 */
79