2016-05-18 02:51:04 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 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)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int bits;
|
|
|
|
int num = 0;
|
|
|
|
int ext = 0;
|
|
|
|
long l;
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
bits = BN_num_bits(a);
|
|
|
|
num = (bits + 7) / 8;
|
|
|
|
if (bits > 0) {
|
|
|
|
ext = ((bits & 0x07) == 0);
|
|
|
|
}
|
|
|
|
if (d == NULL)
|
|
|
|
return (num + 4 + ext);
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
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);
|
|
|
|
}
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2016-04-29 02:53:08 +08:00
|
|
|
BIGNUM *BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain)
|
2015-01-22 11:40:55 +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
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (n < 4) {
|
|
|
|
BNerr(BN_F_BN_MPI2BN, BN_R_INVALID_LENGTH);
|
2016-04-27 19:55:44 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
len = ((long)d[0] << 24) | ((long)d[1] << 16) | ((int)d[2] << 8) | (int)
|
|
|
|
d[3];
|
|
|
|
if ((len + 4) != n) {
|
|
|
|
BNerr(BN_F_BN_MPI2BN, 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)
|
2015-01-22 11:40:55 +08:00
|
|
|
a = BN_new();
|
2016-04-29 02:53:08 +08:00
|
|
|
else
|
|
|
|
a = ain;
|
|
|
|
|
2015-01-22 11:40:55 +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;
|
|
|
|
if ((*d) & 0x80)
|
|
|
|
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;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
a->neg = neg;
|
|
|
|
if (neg) {
|
|
|
|
BN_clear_bit(a, BN_num_bits(a) - 1);
|
|
|
|
}
|
|
|
|
bn_check_top(a);
|
2016-04-27 19:55:44 +08:00
|
|
|
return a;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|