1 /*
2 * Copyright 2011-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11 /*
12 * ECDSA low level APIs are deprecated for public use, but still ok for
13 * internal use.
14 */
15 #include "internal/deprecated.h"
16
17 #include <string.h>
18
19 #include <openssl/err.h>
20 #include <openssl/opensslv.h>
21
22 #include "ec_local.h"
23
EC_POINT_set_compressed_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)24 int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
25 const BIGNUM *x, int y_bit, BN_CTX *ctx)
26 {
27 if (group->meth->point_set_compressed_coordinates == NULL
28 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
29 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
30 return 0;
31 }
32 if (!ec_point_is_compat(point, group)) {
33 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
34 return 0;
35 }
36 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
37 if (group->meth->field_type == NID_X9_62_prime_field)
38 return ossl_ec_GFp_simple_set_compressed_coordinates(group, point, x,
39 y_bit, ctx);
40 else
41 #ifdef OPENSSL_NO_EC2M
42 {
43 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
44 return 0;
45 }
46 #else
47 return ossl_ec_GF2m_simple_set_compressed_coordinates(group, point,
48 x, y_bit, ctx);
49 #endif
50 }
51 return group->meth->point_set_compressed_coordinates(group, point, x,
52 y_bit, ctx);
53 }
54
55 #ifndef OPENSSL_NO_DEPRECATED_3_0
EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)56 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
57 EC_POINT *point, const BIGNUM *x,
58 int y_bit, BN_CTX *ctx)
59 {
60 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
61 }
62
63 # ifndef OPENSSL_NO_EC2M
EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)64 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
65 EC_POINT *point, const BIGNUM *x,
66 int y_bit, BN_CTX *ctx)
67 {
68 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
69 }
70 # endif
71 #endif
72
EC_POINT_point2oct(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,unsigned char * buf,size_t len,BN_CTX * ctx)73 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
74 point_conversion_form_t form, unsigned char *buf,
75 size_t len, BN_CTX *ctx)
76 {
77 if (point == NULL) {
78 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
79 return 0;
80 }
81 if (group->meth->point2oct == 0
82 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
83 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
84 return 0;
85 }
86 if (!ec_point_is_compat(point, group)) {
87 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
88 return 0;
89 }
90 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
91 if (group->meth->field_type == NID_X9_62_prime_field)
92 return ossl_ec_GFp_simple_point2oct(group, point, form, buf, len,
93 ctx);
94 else
95 #ifdef OPENSSL_NO_EC2M
96 {
97 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
98 return 0;
99 }
100 #else
101 return ossl_ec_GF2m_simple_point2oct(group, point,
102 form, buf, len, ctx);
103 #endif
104 }
105
106 return group->meth->point2oct(group, point, form, buf, len, ctx);
107 }
108
EC_POINT_oct2point(const EC_GROUP * group,EC_POINT * point,const unsigned char * buf,size_t len,BN_CTX * ctx)109 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
110 const unsigned char *buf, size_t len, BN_CTX *ctx)
111 {
112 if (group->meth->oct2point == 0
113 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
114 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
115 return 0;
116 }
117 if (!ec_point_is_compat(point, group)) {
118 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
119 return 0;
120 }
121 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
122 if (group->meth->field_type == NID_X9_62_prime_field)
123 return ossl_ec_GFp_simple_oct2point(group, point, buf, len, ctx);
124 else
125 #ifdef OPENSSL_NO_EC2M
126 {
127 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
128 return 0;
129 }
130 #else
131 return ossl_ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
132 #endif
133 }
134 return group->meth->oct2point(group, point, buf, len, ctx);
135 }
136
EC_POINT_point2buf(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,unsigned char ** pbuf,BN_CTX * ctx)137 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
138 point_conversion_form_t form,
139 unsigned char **pbuf, BN_CTX *ctx)
140 {
141 size_t len;
142 unsigned char *buf;
143
144 len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
145 if (len == 0)
146 return 0;
147 if ((buf = OPENSSL_malloc(len)) == NULL)
148 return 0;
149 len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
150 if (len == 0) {
151 OPENSSL_free(buf);
152 return 0;
153 }
154 *pbuf = buf;
155 return len;
156 }
157