]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/eck_prn.c
rand: remove unimplemented librandom stub code
[thirdparty/openssl.git] / crypto / ec / eck_prn.c
CommitLineData
a4e75b3d 1/*
4333b89f 2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
a4e75b3d 4 *
a7f182b7 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
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
1b593194 9 */
aa6bb135 10
5b5eea4b
SL
11#include "internal/deprecated.h"
12
1b593194 13#include <stdio.h>
b39fc560 14#include "internal/cryptlib.h"
1b593194
DSH
15#include <openssl/evp.h>
16#include <openssl/ec.h>
1e26a8ba 17#include <openssl/bn.h>
1b593194 18
5b5eea4b
SL
19#ifndef OPENSSL_NO_DEPRECATED_3_0
20# ifndef OPENSSL_NO_STDIO
1b593194 21int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
0f113f3e
MC
22{
23 BIO *b;
24 int ret;
25
26 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 27 ERR_raise(ERR_LIB_EC, ERR_R_BUF_LIB);
26a7d938 28 return 0;
0f113f3e
MC
29 }
30 BIO_set_fp(b, fp, BIO_NOCLOSE);
31 ret = ECPKParameters_print(b, x, off);
32 BIO_free(b);
26a7d938 33 return ret;
0f113f3e 34}
1b593194
DSH
35
36int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
0f113f3e
MC
37{
38 BIO *b;
39 int ret;
40
41 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 42 ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
26a7d938 43 return 0;
0f113f3e
MC
44 }
45 BIO_set_fp(b, fp, BIO_NOCLOSE);
46 ret = EC_KEY_print(b, x, off);
47 BIO_free(b);
26a7d938 48 return ret;
0f113f3e 49}
1b593194
DSH
50
51int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
0f113f3e
MC
52{
53 BIO *b;
54 int ret;
55
56 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 57 ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
26a7d938 58 return 0;
0f113f3e
MC
59 }
60 BIO_set_fp(b, fp, BIO_NOCLOSE);
61 ret = ECParameters_print(b, x);
62 BIO_free(b);
26a7d938 63 return ret;
0f113f3e 64}
5b5eea4b 65#endif /* OPENSSL_NO_STDIO */
1b593194 66
1b593194 67static int print_bin(BIO *fp, const char *str, const unsigned char *num,
0f113f3e 68 size_t len, int off);
1b593194
DSH
69
70int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
0f113f3e 71{
0f113f3e
MC
72 int ret = 0, reason = ERR_R_BIO_LIB;
73 BN_CTX *ctx = NULL;
74 const EC_POINT *point = NULL;
c1131e6a
SL
75 BIGNUM *p = NULL, *a = NULL, *b = NULL;
76 unsigned char *gen_buf = NULL;
be2e334f 77 const BIGNUM *order = NULL, *cofactor = NULL;
0f113f3e 78 const unsigned char *seed;
c1131e6a 79 size_t seed_len = 0, gen_buf_len = 0;
0f113f3e
MC
80
81 static const char *gen_compressed = "Generator (compressed):";
82 static const char *gen_uncompressed = "Generator (uncompressed):";
83 static const char *gen_hybrid = "Generator (hybrid):";
84
85 if (!x) {
86 reason = ERR_R_PASSED_NULL_PARAMETER;
87 goto err;
88 }
89
90 ctx = BN_CTX_new();
91 if (ctx == NULL) {
e077455e 92 reason = ERR_R_BN_LIB;
0f113f3e
MC
93 goto err;
94 }
95
96 if (EC_GROUP_get_asn1_flag(x)) {
97 /* the curve parameter are given by an asn1 OID */
98 int nid;
99 const char *nname;
100
101 if (!BIO_indent(bp, off, 128))
102 goto err;
103
104 nid = EC_GROUP_get_curve_name(x);
105 if (nid == 0)
106 goto err;
107 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
108 goto err;
109 if (BIO_printf(bp, "\n") <= 0)
110 goto err;
111 nname = EC_curve_nid2nist(nid);
112 if (nname) {
113 if (!BIO_indent(bp, off, 128))
114 goto err;
115 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
116 goto err;
117 }
118 } else {
c1131e6a 119 const char *form_str;
0f113f3e
MC
120 /* explicit parameters */
121 int is_char_two = 0;
122 point_conversion_form_t form;
23ccae80 123 int tmp_nid = EC_GROUP_get_field_type(x);
0f113f3e
MC
124
125 if (tmp_nid == NID_X9_62_characteristic_two_field)
126 is_char_two = 1;
127
128 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
be2e334f 129 (b = BN_new()) == NULL) {
e077455e 130 reason = ERR_R_BN_LIB;
0f113f3e
MC
131 goto err;
132 }
9cc570d4
MC
133
134 if (!EC_GROUP_get_curve(x, p, a, b, ctx)) {
135 reason = ERR_R_EC_LIB;
136 goto err;
0f113f3e
MC
137 }
138
139 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
140 reason = ERR_R_EC_LIB;
141 goto err;
142 }
be2e334f
DSH
143 order = EC_GROUP_get0_order(x);
144 cofactor = EC_GROUP_get0_cofactor(x);
145 if (order == NULL) {
0f113f3e
MC
146 reason = ERR_R_EC_LIB;
147 goto err;
148 }
149
150 form = EC_GROUP_get_point_conversion_form(x);
151
c1131e6a
SL
152 gen_buf_len = EC_POINT_point2buf(x, point, form, &gen_buf, ctx);
153 if (gen_buf_len == 0) {
0f113f3e
MC
154 reason = ERR_R_EC_LIB;
155 goto err;
156 }
157
0f113f3e
MC
158 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
159 seed_len = EC_GROUP_get_seed_len(x);
160
0f113f3e
MC
161 if (!BIO_indent(bp, off, 128))
162 goto err;
163
164 /* print the 'short name' of the field type */
165 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
166 <= 0)
167 goto err;
168
169 if (is_char_two) {
170 /* print the 'short name' of the base type OID */
171 int basis_type = EC_GROUP_get_basis_type(x);
172 if (basis_type == 0)
173 goto err;
174
175 if (!BIO_indent(bp, off, 128))
176 goto err;
177
178 if (BIO_printf(bp, "Basis Type: %s\n",
179 OBJ_nid2sn(basis_type)) <= 0)
180 goto err;
181
182 /* print the polynomial */
a773b52a 183 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
0f113f3e
MC
184 off))
185 goto err;
186 } else {
a773b52a 187 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
0f113f3e
MC
188 goto err;
189 }
a773b52a 190 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
0f113f3e 191 goto err;
a773b52a 192 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
0f113f3e 193 goto err;
0f113f3e 194
c1131e6a
SL
195 if (form == POINT_CONVERSION_COMPRESSED)
196 form_str = gen_compressed;
197 else if (form == POINT_CONVERSION_UNCOMPRESSED)
198 form_str = gen_uncompressed;
199 else
200 form_str = gen_hybrid;
201 if (gen_buf != NULL
202 && !print_bin(bp, form_str, gen_buf, gen_buf_len, off))
203 goto err;
204
205 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, NULL, off))
0f113f3e
MC
206 goto err;
207 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
a773b52a 208 NULL, off))
0f113f3e
MC
209 goto err;
210 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
211 goto err;
212 }
213 ret = 1;
214 err:
215 if (!ret)
9311d0c4 216 ERR_raise(ERR_LIB_EC, reason);
23a1d5e9
RS
217 BN_free(p);
218 BN_free(a);
219 BN_free(b);
c1131e6a 220 OPENSSL_clear_free(gen_buf, gen_buf_len);
23a1d5e9 221 BN_CTX_free(ctx);
26a7d938 222 return ret;
0f113f3e 223}
1b593194
DSH
224
225static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
0f113f3e
MC
226 size_t len, int off)
227{
228 size_t i;
8fce04ee 229 char str[128 + 1 + 4];
0f113f3e
MC
230
231 if (buf == NULL)
232 return 1;
3deeeeb6 233 if (off > 0) {
0f113f3e
MC
234 if (off > 128)
235 off = 128;
236 memset(str, ' ', off);
237 if (BIO_write(fp, str, off) <= 0)
238 return 0;
3deeeeb6
MC
239 } else {
240 off = 0;
0f113f3e
MC
241 }
242
243 if (BIO_printf(fp, "%s", name) <= 0)
244 return 0;
245
246 for (i = 0; i < len; i++) {
247 if ((i % 15) == 0) {
248 str[0] = '\n';
249 memset(&(str[1]), ' ', off + 4);
250 if (BIO_write(fp, str, off + 1 + 4) <= 0)
251 return 0;
252 }
253 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
254 0)
255 return 0;
256 }
257 if (BIO_write(fp, "\n", 1) <= 0)
258 return 0;
259
260 return 1;
261}
5b5eea4b 262#endif /* OPENSSL_NO_DEPRECATED_3_0 */