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 /*
11 * Suppress deprecation warnings for EC low level implementations that are
12 * kept until removal.
13 */
14 #define OPENSSL_SUPPRESS_DEPRECATED
15
16 #include <openssl/crypto.h>
17 #include <openssl/err.h>
18 #include <openssl/ec.h>
19
20 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_point2bn(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,BIGNUM * ret,BN_CTX * ctx)21 BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
22 const EC_POINT *point,
23 point_conversion_form_t form,
24 BIGNUM *ret, BN_CTX *ctx)
25 {
26 size_t buf_len = 0;
27 unsigned char *buf;
28
29 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
30
31 if (buf_len == 0)
32 return NULL;
33
34 ret = BN_bin2bn(buf, buf_len, ret);
35
36 OPENSSL_free(buf);
37
38 return ret;
39 }
40
EC_POINT_bn2point(const EC_GROUP * group,const BIGNUM * bn,EC_POINT * point,BN_CTX * ctx)41 EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
42 const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
43 {
44 size_t buf_len = 0;
45 unsigned char *buf;
46 EC_POINT *ret;
47
48 if ((buf_len = BN_num_bytes(bn)) == 0)
49 buf_len = 1;
50 if ((buf = OPENSSL_malloc(buf_len)) == NULL)
51 return NULL;
52
53 if (BN_bn2binpad(bn, buf, buf_len) < 0) {
54 OPENSSL_free(buf);
55 return NULL;
56 }
57
58 if (point == NULL) {
59 if ((ret = EC_POINT_new(group)) == NULL) {
60 OPENSSL_free(buf);
61 return NULL;
62 }
63 } else
64 ret = point;
65
66 if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
67 if (ret != point)
68 EC_POINT_clear_free(ret);
69 OPENSSL_free(buf);
70 return NULL;
71 }
72
73 OPENSSL_free(buf);
74 return ret;
75 }
76 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
77