Add more complete support for libctx/propq in the EC code
[openssl.git] / crypto / ec / ec_oct.c
1 /*
2  * Copyright 2011-2020 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
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         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
30               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
31         return 0;
32     }
33     if (!ec_point_is_compat(point, group)) {
34         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
35               EC_R_INCOMPATIBLE_OBJECTS);
36         return 0;
37     }
38     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
39         if (group->meth->field_type == NID_X9_62_prime_field)
40             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
41                                                             y_bit, ctx);
42         else
43 #ifdef OPENSSL_NO_EC2M
44         {
45             ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
46                   EC_R_GF2M_NOT_SUPPORTED);
47             return 0;
48         }
49 #else
50             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
51                                                              y_bit, ctx);
52 #endif
53     }
54     return group->meth->point_set_compressed_coordinates(group, point, x,
55                                                          y_bit, ctx);
56 }
57
58 #ifndef OPENSSL_NO_DEPRECATED_3_0
59 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
60                                             EC_POINT *point, const BIGNUM *x,
61                                             int y_bit, BN_CTX *ctx)
62 {
63     return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
64 }
65
66 # ifndef OPENSSL_NO_EC2M
67 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
68                                              EC_POINT *point, const BIGNUM *x,
69                                              int y_bit, BN_CTX *ctx)
70 {
71     return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
72 }
73 # endif
74 #endif
75
76 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
77                           point_conversion_form_t form, unsigned char *buf,
78                           size_t len, BN_CTX *ctx)
79 {
80     if (group->meth->point2oct == 0
81         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
82         ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
83         return 0;
84     }
85     if (!ec_point_is_compat(point, group)) {
86         ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
87         return 0;
88     }
89     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
90         if (group->meth->field_type == NID_X9_62_prime_field)
91             return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
92         else
93 #ifdef OPENSSL_NO_EC2M
94         {
95             ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
96             return 0;
97         }
98 #else
99             return ec_GF2m_simple_point2oct(group, point,
100                                             form, buf, len, ctx);
101 #endif
102     }
103
104     return group->meth->point2oct(group, point, form, buf, len, ctx);
105 }
106
107 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
108                        const unsigned char *buf, size_t len, BN_CTX *ctx)
109 {
110     if (group->meth->oct2point == 0
111         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
112         ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
113         return 0;
114     }
115     if (!ec_point_is_compat(point, group)) {
116         ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
117         return 0;
118     }
119     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
120         if (group->meth->field_type == NID_X9_62_prime_field)
121             return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
122         else
123 #ifdef OPENSSL_NO_EC2M
124         {
125             ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
126             return 0;
127         }
128 #else
129             return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
130 #endif
131     }
132     return group->meth->oct2point(group, point, buf, len, ctx);
133 }
134
135 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
136                           point_conversion_form_t form,
137                           unsigned char **pbuf, BN_CTX *ctx)
138 {
139     size_t len;
140     unsigned char *buf;
141
142     len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
143     if (len == 0)
144         return 0;
145     if ((buf = OPENSSL_malloc(len)) == NULL) {
146         ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
147         return 0;
148     }
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 }