| 1 |
/* |
| 2 |
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include "bn_local.h" |
| 13 |
|
| 14 |
int BN_bn2mpi(const BIGNUM *a, unsigned char *d) |
| 15 |
{ |
| 16 |
int bits; |
| 17 |
int num = 0; |
| 18 |
int ext = 0; |
| 19 |
long l; |
| 20 |
|
| 21 |
bits = BN_num_bits(a); |
| 22 |
num = (bits + 7) / 8; |
| 23 |
if (bits > 0) { |
| 24 |
ext = ((bits & 0x07) == 0); |
| 25 |
} |
| 26 |
if (d == NULL) |
| 27 |
return (num + 4 + ext); |
| 28 |
|
| 29 |
l = num + ext; |
| 30 |
d[0] = (unsigned char)(l >> 24) & 0xff; |
| 31 |
d[1] = (unsigned char)(l >> 16) & 0xff; |
| 32 |
d[2] = (unsigned char)(l >> 8) & 0xff; |
| 33 |
d[3] = (unsigned char)(l) & 0xff; |
| 34 |
if (ext) |
| 35 |
d[4] = 0; |
| 36 |
num = BN_bn2bin(a, &(d[4 + ext])); |
| 37 |
if (a->neg) |
| 38 |
d[4] |= 0x80; |
| 39 |
return (num + 4 + ext); |
| 40 |
} |
| 41 |
|
| 42 |
BIGNUM *BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain) |
| 43 |
{ |
| 44 |
long len; |
| 45 |
int neg = 0; |
| 46 |
BIGNUM *a = NULL; |
| 47 |
|
| 48 |
if (n < 4 || (d[0] & 0x80) != 0) { |
| 49 |
ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
len = ((long)d[0] << 24) | ((long)d[1] << 16) | ((int)d[2] << 8) | (int)d[3]; |
| 53 |
if ((len + 4) != n) { |
| 54 |
ERR_raise(ERR_LIB_BN, BN_R_ENCODING_ERROR); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
if (ain == NULL) |
| 59 |
a = BN_new(); |
| 60 |
else |
| 61 |
a = ain; |
| 62 |
|
| 63 |
if (a == NULL) |
| 64 |
return NULL; |
| 65 |
|
| 66 |
if (len == 0) { |
| 67 |
a->neg = 0; |
| 68 |
a->top = 0; |
| 69 |
return a; |
| 70 |
} |
| 71 |
d += 4; |
| 72 |
if ((*d) & 0x80) |
| 73 |
neg = 1; |
| 74 |
if (BN_bin2bn(d, (int)len, a) == NULL) { |
| 75 |
if (ain == NULL) |
| 76 |
BN_free(a); |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
a->neg = neg; |
| 80 |
if (neg) { |
| 81 |
BN_clear_bit(a, BN_num_bits(a) - 1); |
| 82 |
} |
| 83 |
bn_check_top(a); |
| 84 |
return a; |
| 85 |
} |