]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_oct.c
Update copyright year
[thirdparty/openssl.git] / crypto / ec / ec_oct.c
CommitLineData
84b08eee 1/*
28428130 2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
84b08eee 4 *
4f22f405
RS
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
84b08eee 9 */
4f22f405 10
84b08eee
DSH
11#include <string.h>
12
13#include <openssl/err.h>
14#include <openssl/opensslv.h>
15
16#include "ec_lcl.h"
17
0f113f3e
MC
18int 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
1acc24a8 38#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
39 {
40 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP,
41 EC_R_GF2M_NOT_SUPPORTED);
42 return 0;
43 }
1acc24a8 44#else
0f113f3e
MC
45 return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
46 y_bit, ctx);
1acc24a8 47#endif
0f113f3e
MC
48 }
49 return group->meth->point_set_compressed_coordinates(group, point, x,
50 y_bit, ctx);
51}
84b08eee
DSH
52
53#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
54int 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}
84b08eee
DSH
80#endif
81
0f113f3e
MC
82size_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
1acc24a8 99#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
100 {
101 ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
102 return 0;
103 }
1acc24a8 104#else
0f113f3e
MC
105 return ec_GF2m_simple_point2oct(group, point,
106 form, buf, len, ctx);
1acc24a8 107#endif
0f113f3e 108 }
84b08eee 109
0f113f3e
MC
110 return group->meth->point2oct(group, point, form, buf, len, ctx);
111}
84b08eee
DSH
112
113int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
0f113f3e
MC
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
1acc24a8 129#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
130 {
131 ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
132 return 0;
133 }
1acc24a8 134#else
0f113f3e 135 return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
1acc24a8 136#endif
0f113f3e
MC
137 }
138 return group->meth->oct2point(group, point, buf, len, ctx);
139}
981bd8a2
DSH
140
141size_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;
cdb10bae 147
981bd8a2
DSH
148 len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
149 if (len == 0)
150 return 0;
cdb10bae
RS
151 if ((buf = OPENSSL_malloc(len)) == NULL) {
152 ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
981bd8a2 153 return 0;
cdb10bae 154 }
981bd8a2
DSH
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}