]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_oct.c
Provide EC functions that are not curve type specific
[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
8e3cced7
MC
18int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
19 const BIGNUM *x, int y_bit, BN_CTX *ctx)
0f113f3e 20{
8e3cced7 21 if (group->meth->point_set_compressed_coordinates == NULL
0f113f3e 22 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
8e3cced7 23 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
0f113f3e
MC
24 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
25 return 0;
26 }
b14e6015 27 if (!ec_point_is_compat(point, group)) {
8e3cced7 28 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
0f113f3e
MC
29 EC_R_INCOMPATIBLE_OBJECTS);
30 return 0;
31 }
32 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
33 if (group->meth->field_type == NID_X9_62_prime_field)
34 return ec_GFp_simple_set_compressed_coordinates(group, point, x,
35 y_bit, ctx);
36 else
1acc24a8 37#ifdef OPENSSL_NO_EC2M
0f113f3e 38 {
8e3cced7 39 ECerr(EC_F_EC_POINT_SET_COMPRESSED_COORDINATES,
0f113f3e
MC
40 EC_R_GF2M_NOT_SUPPORTED);
41 return 0;
42 }
1acc24a8 43#else
0f113f3e
MC
44 return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
45 y_bit, ctx);
1acc24a8 46#endif
0f113f3e
MC
47 }
48 return group->meth->point_set_compressed_coordinates(group, point, x,
49 y_bit, ctx);
50}
84b08eee 51
8e3cced7
MC
52int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
53 EC_POINT *point, const BIGNUM *x,
54 int y_bit, BN_CTX *ctx)
55{
56 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
57}
58
84b08eee 59#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
60int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
61 EC_POINT *point, const BIGNUM *x,
62 int y_bit, BN_CTX *ctx)
63{
8e3cced7 64 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
0f113f3e 65}
84b08eee
DSH
66#endif
67
0f113f3e
MC
68size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
69 point_conversion_form_t form, unsigned char *buf,
70 size_t len, BN_CTX *ctx)
71{
72 if (group->meth->point2oct == 0
73 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
74 ECerr(EC_F_EC_POINT_POINT2OCT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
75 return 0;
76 }
b14e6015 77 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
78 ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_INCOMPATIBLE_OBJECTS);
79 return 0;
80 }
81 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
82 if (group->meth->field_type == NID_X9_62_prime_field)
83 return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
84 else
1acc24a8 85#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
86 {
87 ECerr(EC_F_EC_POINT_POINT2OCT, EC_R_GF2M_NOT_SUPPORTED);
88 return 0;
89 }
1acc24a8 90#else
0f113f3e
MC
91 return ec_GF2m_simple_point2oct(group, point,
92 form, buf, len, ctx);
1acc24a8 93#endif
0f113f3e 94 }
84b08eee 95
0f113f3e
MC
96 return group->meth->point2oct(group, point, form, buf, len, ctx);
97}
84b08eee
DSH
98
99int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
0f113f3e
MC
100 const unsigned char *buf, size_t len, BN_CTX *ctx)
101{
102 if (group->meth->oct2point == 0
103 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
104 ECerr(EC_F_EC_POINT_OCT2POINT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
105 return 0;
106 }
b14e6015 107 if (!ec_point_is_compat(point, group)) {
0f113f3e
MC
108 ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_INCOMPATIBLE_OBJECTS);
109 return 0;
110 }
111 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
112 if (group->meth->field_type == NID_X9_62_prime_field)
113 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
114 else
1acc24a8 115#ifdef OPENSSL_NO_EC2M
0f113f3e
MC
116 {
117 ECerr(EC_F_EC_POINT_OCT2POINT, EC_R_GF2M_NOT_SUPPORTED);
118 return 0;
119 }
1acc24a8 120#else
0f113f3e 121 return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
1acc24a8 122#endif
0f113f3e
MC
123 }
124 return group->meth->oct2point(group, point, buf, len, ctx);
125}
981bd8a2
DSH
126
127size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
128 point_conversion_form_t form,
129 unsigned char **pbuf, BN_CTX *ctx)
130{
131 size_t len;
132 unsigned char *buf;
cdb10bae 133
981bd8a2
DSH
134 len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
135 if (len == 0)
136 return 0;
cdb10bae
RS
137 if ((buf = OPENSSL_malloc(len)) == NULL) {
138 ECerr(EC_F_EC_POINT_POINT2BUF, ERR_R_MALLOC_FAILURE);
981bd8a2 139 return 0;
cdb10bae 140 }
981bd8a2
DSH
141 len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
142 if (len == 0) {
143 OPENSSL_free(buf);
144 return 0;
145 }
146 *pbuf = buf;
147 return len;
148}