]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_oct.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / ec / ec_oct.c
CommitLineData
84b08eee 1/*
33388b44 2 * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
84b08eee 4 *
a7f182b7 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
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
579422c8
P
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
84b08eee
DSH
17#include <string.h>
18
19#include <openssl/err.h>
20#include <openssl/opensslv.h>
21
706457b7 22#include "ec_local.h"
84b08eee 23
8e3cced7
MC
24int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
25 const BIGNUM *x, int y_bit, BN_CTX *ctx)
0f113f3e 26{
8e3cced7 27 if (group->meth->point_set_compressed_coordinates == NULL
0f113f3e 28 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
9311d0c4 29 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
30 return 0;
31 }
b14e6015 32 if (!ec_point_is_compat(point, group)) {
9311d0c4 33 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
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 ec_GFp_simple_set_compressed_coordinates(group, point, x,
39 y_bit, ctx);
40 else
1acc24a8 41#ifdef OPENSSL_NO_EC2M
0f113f3e 42 {
9311d0c4 43 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
0f113f3e
MC
44 return 0;
45 }
1acc24a8 46#else
0f113f3e
MC
47 return ec_GF2m_simple_set_compressed_coordinates(group, point, x,
48 y_bit, ctx);
1acc24a8 49#endif
0f113f3e
MC
50 }
51 return group->meth->point_set_compressed_coordinates(group, point, x,
52 y_bit, ctx);
53}
84b08eee 54
936c2b9e 55#ifndef OPENSSL_NO_DEPRECATED_3_0
8e3cced7
MC
56int 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
50db8163 63# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
64int 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{
8e3cced7 68 return EC_POINT_set_compressed_coordinates(group, point, x, y_bit, ctx);
0f113f3e 69}
50db8163 70# endif
84b08eee
DSH
71#endif
72
0f113f3e
MC
73size_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 (group->meth->point2oct == 0
78 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
9311d0c4 79 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
80 return 0;
81 }
b14e6015 82 if (!ec_point_is_compat(point, group)) {
9311d0c4 83 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
84 return 0;
85 }
86 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
87 if (group->meth->field_type == NID_X9_62_prime_field)
88 return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
89 else
1acc24a8 90#ifdef OPENSSL_NO_EC2M
0f113f3e 91 {
9311d0c4 92 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
0f113f3e
MC
93 return 0;
94 }
1acc24a8 95#else
0f113f3e
MC
96 return ec_GF2m_simple_point2oct(group, point,
97 form, buf, len, ctx);
1acc24a8 98#endif
0f113f3e 99 }
84b08eee 100
0f113f3e
MC
101 return group->meth->point2oct(group, point, form, buf, len, ctx);
102}
84b08eee
DSH
103
104int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
0f113f3e
MC
105 const unsigned char *buf, size_t len, BN_CTX *ctx)
106{
107 if (group->meth->oct2point == 0
108 && !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
9311d0c4 109 ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
0f113f3e
MC
110 return 0;
111 }
b14e6015 112 if (!ec_point_is_compat(point, group)) {
9311d0c4 113 ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);
0f113f3e
MC
114 return 0;
115 }
116 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
117 if (group->meth->field_type == NID_X9_62_prime_field)
118 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
119 else
1acc24a8 120#ifdef OPENSSL_NO_EC2M
0f113f3e 121 {
9311d0c4 122 ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);
0f113f3e
MC
123 return 0;
124 }
1acc24a8 125#else
0f113f3e 126 return ec_GF2m_simple_oct2point(group, point, buf, len, ctx);
1acc24a8 127#endif
0f113f3e
MC
128 }
129 return group->meth->oct2point(group, point, buf, len, ctx);
130}
981bd8a2
DSH
131
132size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
133 point_conversion_form_t form,
134 unsigned char **pbuf, BN_CTX *ctx)
135{
136 size_t len;
137 unsigned char *buf;
cdb10bae 138
981bd8a2
DSH
139 len = EC_POINT_point2oct(group, point, form, NULL, 0, NULL);
140 if (len == 0)
141 return 0;
cdb10bae 142 if ((buf = OPENSSL_malloc(len)) == NULL) {
9311d0c4 143 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
981bd8a2 144 return 0;
cdb10bae 145 }
981bd8a2
DSH
146 len = EC_POINT_point2oct(group, point, form, buf, len, ctx);
147 if (len == 0) {
148 OPENSSL_free(buf);
149 return 0;
150 }
151 *pbuf = buf;
152 return len;
153}