Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bn_extra/convert.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/bn.h>
5
6 #include <assert.h>
7 #include <ctype.h>
8 #include <limits.h>
9 #include <stdio.h>
10
11 #include <openssl/bio.h>
12 #include <openssl/bytestring.h>
13 #include <openssl/err.h>
14 #include <openssl/mem.h>
15
16 #include "../fipsmodule/bn/internal.h"
17
18
19 int BN_bn2cbb_padded(CBB *out, size_t len, const BIGNUM *in) {
20 uint8_t *ptr;
21 return CBB_add_space(out, &ptr, len) && BN_bn2bin_padded(ptr, len, in);
22 }
23
24 static const char hextable[] = "0123456789ABCDEF";
25
26 char *BN_bn2hex(const BIGNUM *bn) {
27 int width = bn_minimal_width(bn);
28 char *buf = OPENSSL_zalloc(1 /* leading '-' */ + 1 /* zero is non-empty */ +
29 width * BN_BYTES * 2 + 1 /* trailing NUL */);
30 if (buf == NULL) {
31 return NULL;
32 }
33
34 char *p = buf;
35 if (bn->neg) {
36 *(p++) = '-';
37 }
38
39 if (BN_is_zero(bn)) {
40 *(p++) = '0';
41 }
42
43 int z = 0;
44 for (int i = width - 1; i >= 0; i--) {
45 for (int j = BN_BITS2 - 8; j >= 0; j -= 8) {
46 // strip leading zeros
47 int v = ((int)(bn->d[i] >> (long)j)) & 0xff;
48 if (z || v != 0) {
49 *(p++) = hextable[v >> 4];
50 *(p++) = hextable[v & 0x0f];
51 z = 1;
52 }
53 }
54 }
55 assert(*p == '\0');
56
57 return buf;
58 }
59
60 // decode_hex decodes |in_len| bytes of hex data from |in| and updates |bn|.
61 static int decode_hex(BIGNUM *bn, const char *in, int in_len) {
62 if (in_len > INT_MAX/4) {
63 OPENSSL_PUT_ERROR(BN, BN_R_BIGNUM_TOO_LONG);
64 return 0;
65 }
66 // |in_len| is the number of hex digits.
67 if (!bn_expand(bn, in_len * 4)) {
68 return 0;
69 }
70
71 int i = 0;
72 while (in_len > 0) {
73 // Decode one |BN_ULONG| at a time.
74 int todo = BN_BYTES * 2;
75 if (todo > in_len) {
76 todo = in_len;
77 }
78
79 BN_ULONG word = 0;
80 int j;
81 for (j = todo; j > 0; j--) {
82 uint8_t hex = 0;
83 if (!OPENSSL_fromxdigit(&hex, in[in_len - j])) {
84 // This shouldn't happen. The caller checks |OPENSSL_isxdigit|.
85 assert(0);
86 }
87 word = (word << 4) | hex;
88 }
89
90 bn->d[i++] = word;
91 in_len -= todo;
92 }
93 assert(i <= bn->dmax);
94 bn->width = i;
95 return 1;
96 }
97
98 // decode_dec decodes |in_len| bytes of decimal data from |in| and updates |bn|.
99 static int decode_dec(BIGNUM *bn, const char *in, int in_len) {
100 int i, j;
101 BN_ULONG l = 0;
102
103 // Decode |BN_DEC_NUM| digits at a time.
104 j = BN_DEC_NUM - (in_len % BN_DEC_NUM);
105 if (j == BN_DEC_NUM) {
106 j = 0;
107 }
108 l = 0;
109 for (i = 0; i < in_len; i++) {
110 l *= 10;
111 l += in[i] - '0';
112 if (++j == BN_DEC_NUM) {
113 if (!BN_mul_word(bn, BN_DEC_CONV) ||
114 !BN_add_word(bn, l)) {
115 return 0;
116 }
117 l = 0;
118 j = 0;
119 }
120 }
121 return 1;
122 }
123
124 typedef int (*decode_func) (BIGNUM *bn, const char *in, int in_len);
125 typedef int (*char_test_func) (int c);
126
127 static int bn_x2bn(BIGNUM **outp, const char *in, decode_func decode, char_test_func want_char) {
128 BIGNUM *ret = NULL;
129 int neg = 0, i;
130 int num;
131
132 if (in == NULL || *in == 0) {
133 return 0;
134 }
135
136 if (*in == '-') {
137 neg = 1;
138 in++;
139 }
140
141 for (i = 0; want_char((unsigned char)in[i]) && i + neg < INT_MAX; i++) {}
142
143 if(i == 0) {
144 OPENSSL_PUT_ERROR(BN, BN_R_INVALID_INPUT);
145 return 0;
146 }
147
148 num = i + neg;
149 if (outp == NULL) {
150 return num;
151 }
152
153 // in is the start of the hex digits, and it is 'i' long
154 if (*outp == NULL) {
155 ret = BN_new();
156 if (ret == NULL) {
157 return 0;
158 }
159 } else {
160 ret = *outp;
161 BN_zero(ret);
162 }
163
164 if (!decode(ret, in, i)) {
165 goto err;
166 }
167
168 bn_set_minimal_width(ret);
169 if (!BN_is_zero(ret)) {
170 ret->neg = neg;
171 }
172
173 *outp = ret;
174 return num;
175
176 err:
177 if (*outp == NULL) {
178 BN_free(ret);
179 }
180
181 return 0;
182 }
183
184 int BN_hex2bn(BIGNUM **outp, const char *in) {
185 return bn_x2bn(outp, in, decode_hex, OPENSSL_isxdigit);
186 }
187
188 char *BN_bn2dec(const BIGNUM *a) {
189 // It is easier to print strings little-endian, so we assemble it in reverse
190 // and fix at the end.
191 BIGNUM *copy = NULL;
192 CBB cbb;
193 if (!CBB_init(&cbb, 16) ||
194 !CBB_add_u8(&cbb, 0 /* trailing NUL */)) {
195 goto err;
196 }
197
198 if (BN_is_zero(a)) {
199 if (!CBB_add_u8(&cbb, '0')) {
200 goto err;
201 }
202 } else {
203 copy = BN_dup(a);
204 if (copy == NULL) {
205 goto err;
206 }
207
208 while (!BN_is_zero(copy)) {
209 BN_ULONG word = BN_div_word(copy, BN_DEC_CONV);
210 if (word == (BN_ULONG)-1) {
211 goto err;
212 }
213
214 const int add_leading_zeros = !BN_is_zero(copy);
215 for (int i = 0; i < BN_DEC_NUM && (add_leading_zeros || word != 0); i++) {
216 if (!CBB_add_u8(&cbb, '0' + word % 10)) {
217 goto err;
218 }
219 word /= 10;
220 }
221 assert(word == 0);
222 }
223 }
224
225 if (BN_is_negative(a) &&
226 !CBB_add_u8(&cbb, '-')) {
227 goto err;
228 }
229
230 uint8_t *data;
231 size_t len;
232 if (!CBB_finish(&cbb, &data, &len)) {
233 goto err;
234 }
235
236 // Reverse the buffer.
237 for (size_t i = 0; i < len/2; i++) {
238 uint8_t tmp = data[i];
239 data[i] = data[len - 1 - i];
240 data[len - 1 - i] = tmp;
241 }
242
243 BN_free(copy);
244 return (char *)data;
245
246 err:
247 BN_free(copy);
248 CBB_cleanup(&cbb);
249 return NULL;
250 }
251
252 int BN_dec2bn(BIGNUM **outp, const char *in) {
253 return bn_x2bn(outp, in, decode_dec, OPENSSL_isdigit);
254 }
255
256 int BN_asc2bn(BIGNUM **outp, const char *in) {
257 const char *const orig_in = in;
258 if (*in == '-') {
259 in++;
260 }
261
262 if (in[0] == '0' && (in[1] == 'X' || in[1] == 'x')) {
263 if (!BN_hex2bn(outp, in+2)) {
264 return 0;
265 }
266 } else {
267 if (!BN_dec2bn(outp, in)) {
268 return 0;
269 }
270 }
271
272 if (*orig_in == '-' && !BN_is_zero(*outp)) {
273 (*outp)->neg = 1;
274 }
275
276 return 1;
277 }
278
279 int BN_print(BIO *bp, const BIGNUM *a) {
280 int i, j, v, z = 0;
281 int ret = 0;
282
283 if (a->neg && BIO_write(bp, "-", 1) != 1) {
284 goto end;
285 }
286
287 if (BN_is_zero(a) && BIO_write(bp, "0", 1) != 1) {
288 goto end;
289 }
290
291 for (i = bn_minimal_width(a) - 1; i >= 0; i--) {
292 for (j = BN_BITS2 - 4; j >= 0; j -= 4) {
293 // strip leading zeros
294 v = ((int)(a->d[i] >> (long)j)) & 0x0f;
295 if (z || v != 0) {
296 if (BIO_write(bp, &hextable[v], 1) != 1) {
297 goto end;
298 }
299 z = 1;
300 }
301 }
302 }
303 ret = 1;
304
305 end:
306 return ret;
307 }
308
309 int BN_print_fp(FILE *fp, const BIGNUM *a) {
310 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
311 if (b == NULL) {
312 return 0;
313 }
314
315 int ret = BN_print(b, a);
316 BIO_free(b);
317 return ret;
318 }
319
320
321 size_t BN_bn2mpi(const BIGNUM *in, uint8_t *out) {
322 const size_t bits = BN_num_bits(in);
323 const size_t bytes = (bits + 7) / 8;
324 // If the number of bits is a multiple of 8, i.e. if the MSB is set,
325 // prefix with a zero byte.
326 int extend = 0;
327 if (bytes != 0 && (bits & 0x07) == 0) {
328 extend = 1;
329 }
330
331 const size_t len = bytes + extend;
332 if (len < bytes ||
333 4 + len < len ||
334 (len & 0xffffffff) != len) {
335 // If we cannot represent the number then we emit zero as the interface
336 // doesn't allow an error to be signalled.
337 if (out) {
338 OPENSSL_memset(out, 0, 4);
339 }
340 return 4;
341 }
342
343 if (out == NULL) {
344 return 4 + len;
345 }
346
347 out[0] = len >> 24;
348 out[1] = len >> 16;
349 out[2] = len >> 8;
350 out[3] = len;
351 if (extend) {
352 out[4] = 0;
353 }
354 BN_bn2bin(in, out + 4 + extend);
355 if (in->neg && len > 0) {
356 out[4] |= 0x80;
357 }
358 return len + 4;
359 }
360
361 BIGNUM *BN_mpi2bn(const uint8_t *in, size_t len, BIGNUM *out) {
362 if (len < 4) {
363 OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
364 return NULL;
365 }
366 const size_t in_len = ((size_t)in[0] << 24) |
367 ((size_t)in[1] << 16) |
368 ((size_t)in[2] << 8) |
369 ((size_t)in[3]);
370 if (in_len != len - 4) {
371 OPENSSL_PUT_ERROR(BN, BN_R_BAD_ENCODING);
372 return NULL;
373 }
374
375 int out_is_alloced = 0;
376 if (out == NULL) {
377 out = BN_new();
378 if (out == NULL) {
379 return NULL;
380 }
381 out_is_alloced = 1;
382 }
383
384 if (in_len == 0) {
385 BN_zero(out);
386 return out;
387 }
388
389 in += 4;
390 if (BN_bin2bn(in, in_len, out) == NULL) {
391 if (out_is_alloced) {
392 BN_free(out);
393 }
394 return NULL;
395 }
396 out->neg = ((*in) & 0x80) != 0;
397 if (out->neg) {
398 unsigned num_bits = BN_num_bits(out);
399 if (num_bits >= INT_MAX) {
400 if (out_is_alloced) {
401 BN_free(out);
402 }
403 return NULL;
404 }
405 BN_clear_bit(out, (int)num_bits - 1);
406 }
407 return out;
408 }
409
410 int BN_bn2binpad(const BIGNUM *in, uint8_t *out, int len) {
411 if (len < 0 ||
412 !BN_bn2bin_padded(out, (size_t)len, in)) {
413 return -1;
414 }
415 return len;
416 }