2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2020-06-04 21:33:57 +08:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:56:39 +08:00
|
|
|
*
|
2018-12-06 20:22:12 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:56:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "bn_local.h"
|
1998-12-21 18:56:39 +08:00
|
|
|
|
1999-06-21 01:36:11 +08:00
|
|
|
int BN_bn2mpi(const BIGNUM *a, unsigned char *d)
|
1998-12-21 18:56:39 +08:00
|
|
|
{
|
|
|
|
int bits;
|
|
|
|
int num = 0;
|
|
|
|
int ext = 0;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
bits = BN_num_bits(a);
|
|
|
|
num = (bits + 7) / 8;
|
|
|
|
if (bits > 0) {
|
|
|
|
ext = ((bits & 0x07) == 0);
|
|
|
|
}
|
|
|
|
if (d == NULL)
|
|
|
|
return (num + 4 + ext);
|
|
|
|
|
|
|
|
l = num + ext;
|
|
|
|
d[0] = (unsigned char)(l >> 24) & 0xff;
|
|
|
|
d[1] = (unsigned char)(l >> 16) & 0xff;
|
|
|
|
d[2] = (unsigned char)(l >> 8) & 0xff;
|
|
|
|
d[3] = (unsigned char)(l) & 0xff;
|
|
|
|
if (ext)
|
|
|
|
d[4] = 0;
|
|
|
|
num = BN_bn2bin(a, &(d[4 + ext]));
|
|
|
|
if (a->neg)
|
|
|
|
d[4] |= 0x80;
|
|
|
|
return (num + 4 + ext);
|
|
|
|
}
|
|
|
|
|
2016-04-29 02:53:08 +08:00
|
|
|
BIGNUM *BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain)
|
1998-12-21 18:56:39 +08:00
|
|
|
{
|
|
|
|
long len;
|
|
|
|
int neg = 0;
|
2016-04-29 02:53:08 +08:00
|
|
|
BIGNUM *a = NULL;
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2020-05-19 16:51:53 +08:00
|
|
|
if (n < 4 || (d[0] & 0x80) != 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_BN, BN_R_INVALID_LENGTH);
|
2016-04-27 19:55:44 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
len = ((long)d[0] << 24) | ((long)d[1] << 16) | ((int)d[2] << 8) | (int)
|
2015-01-22 11:40:55 +08:00
|
|
|
d[3];
|
1998-12-21 18:56:39 +08:00
|
|
|
if ((len + 4) != n) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_BN, BN_R_ENCODING_ERROR);
|
2016-04-27 19:55:44 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2016-04-29 02:53:08 +08:00
|
|
|
if (ain == NULL)
|
1998-12-21 18:56:39 +08:00
|
|
|
a = BN_new();
|
2016-04-29 02:53:08 +08:00
|
|
|
else
|
|
|
|
a = ain;
|
|
|
|
|
1998-12-21 18:56:39 +08:00
|
|
|
if (a == NULL)
|
2016-04-27 19:55:44 +08:00
|
|
|
return NULL;
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (len == 0) {
|
|
|
|
a->neg = 0;
|
|
|
|
a->top = 0;
|
2016-04-27 19:55:44 +08:00
|
|
|
return a;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
d += 4;
|
1998-12-21 18:56:39 +08:00
|
|
|
if ((*d) & 0x80)
|
2015-01-22 11:40:55 +08:00
|
|
|
neg = 1;
|
2016-04-27 19:55:44 +08:00
|
|
|
if (BN_bin2bn(d, (int)len, a) == NULL) {
|
2016-04-29 02:53:08 +08:00
|
|
|
if (ain == NULL)
|
|
|
|
BN_free(a);
|
2016-04-27 19:55:44 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
a->neg = neg;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (neg) {
|
1998-12-21 18:56:39 +08:00
|
|
|
BN_clear_bit(a, BN_num_bits(a) - 1);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2003-11-05 06:54:49 +08:00
|
|
|
bn_check_top(a);
|
2016-04-27 19:55:44 +08:00
|
|
|
return a;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|