Set error code on alloc failures
[openssl.git] / crypto / ec / ec_oct.c
1 /*
2  * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
4  *
5  * Licensed under the OpenSSL license (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 #include <string.h>
12
13 #include <openssl/err.h>
14 #include <openssl/opensslv.h>
15
16 #include "ec_lcl.h"
17
18 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
19                                             EC_POINT *point, const BIGNUM *x,
20                                             int y_bit, BN_CTX *ctx)
21 {
22     if (group->meth->point_set_compressed_coordinates == 0
23         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
24         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
25               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
26         return 0;
27     }
28     if (group->meth != point->meth) {
29         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
30               EC_R_INCOMPATIBLE_OBJECTS);
31         return 0;
32     }
33     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
34         if (group->meth->field_type == NID_X9_62_prime_field)
35             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
36                                                             y_bit, ctx);
37         else
38 #ifdef OPENSSL_NO_EC2M
39         {
40             ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
41                   EC_R_GF2M_NOT_SUPPORTED);
42             return 0;
43         }
44 #else
45             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
46                                                              y_bit, ctx);
47 #endif
48     }
49     return group->meth->point_set_compressed_coordinates(group, point, x,
50                                                          y_bit, ctx);
51 }
52
53 #ifndef OPENSSL_NO_EC2M
54 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
55                                              EC_POINT *point, const BIGNUM *x,
56                                              int y_bit, BN_CTX *ctx)
57 {
58     if (group->meth->point_set_compressed_coordinates == 0
59         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
60         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
61               ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
62         return 0;
63     }
64     if (group->meth != point->meth) {
65         ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M,
66               EC_R_INCOMPATIBLE_OBJECTS);
67         return 0;
68     }
69     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
70         if (group->meth->field_type == NID_X9_62_prime_field)
71             return ec_GFp_simple_set_compressed_coordinates(group, point, x,
72                                                             y_bit, ctx);
73         else
74             return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
75                                                              y_bit, ctx);
76     }
77     return group->meth->point_set_compressed_coordinates(group, point, x,
78                                                          y_bit, ctx);
79 }
80 #endif
81
82 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
83                           point_conversion_form_t form, unsigned char *buf,
84                           size_t len, BN_CTX *ctx)
85 {
86     if (group->meth->point2oct == 0
87         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
88         ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
89         return 0;
90     }
91     if (group->meth != point->meth) {
92         ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
93         return 0;
94     }
95     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
96         if (group->meth->field_type == NID_X9_62_prime_field)
97             return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
98         else
99 #ifdef OPENSSL_NO_EC2M
100         {
101             ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
102             return 0;
103         }
104 #else
105             return ec_GF2m_simple_point2oct(group, point,
106                                             form, buf, len, ctx);
107 #endif
108     }
109
110     return group->meth->point2oct(group, point, form, buf, len, ctx);
111 }
112
113 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
114                        const unsigned char *buf, size_t len, BN_CTX *ctx)
115 {
116     if (group->meth->oct2point == 0
117         && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
118         ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
119         return 0;
120     }
121     if (group->meth != point->meth) {
122         ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
123         return 0;
124     }
125     if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
126         if (group->meth->field_type == NID_X9_62_prime_field)
127             return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
128         else
129 #ifdef OPENSSL_NO_EC2M
130         {
131             ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
132             return 0;
133         }
134 #else
135             return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
136 #endif
137     }
138     return group->meth->oct2point(group, point, buf, len, ctx);
139 }
140
141 size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
142                           point_conversion_form_t form,
143                           unsigned char **pbuf, BN_CTX *ctx)
144 {
145     size_t len;
146     unsigned char *buf;
147
148     len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
149     if (len == 0)
150         return 0;
151     if ((buf = OPENSSL_malloc(len)) == NULL) {
152         ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
153         return 0;
154     }
155     len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
156     if (len == 0) {
157         OPENSSL_free(buf);
158         return 0;
159     }
160     *pbuf = buf;
161     return len;
162 }