2016-05-17 18:51:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:56:39 +00:00
|
|
|
*
|
2016-05-17 18:51:04 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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 10:56:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2019-09-27 21:58:06 +00:00
|
|
|
#include "bn_local.h"
|
1998-12-21 10:56:39 +00:00
|
|
|
|
1999-06-20 17:36:11 +00:00
|
|
|
int BN_bn2mpi(const BIGNUM *a, unsigned char *d)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int bits;
|
|
|
|
int num = 0;
|
|
|
|
int ext = 0;
|
|
|
|
long l;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00: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 10:56:39 +00:00
|
|
|
|
2016-04-28 18:53:08 +00:00
|
|
|
BIGNUM *BN_mpi2bn(const unsigned char *d, int n, BIGNUM *ain)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
long len;
|
|
|
|
int neg = 0;
|
2016-04-28 18:53:08 +00:00
|
|
|
BIGNUM *a = NULL;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (n < 4) {
|
|
|
|
BNerr(BN_F_BN_MPI2BN, BN_R_INVALID_LENGTH);
|
2016-04-27 11:55:44 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00: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 11:55:44 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2016-04-28 18:53:08 +00:00
|
|
|
if (ain == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
a = BN_new();
|
2016-04-28 18:53:08 +00:00
|
|
|
else
|
|
|
|
a = ain;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (a == NULL)
|
2016-04-27 11:55:44 +00:00
|
|
|
return NULL;
|
1998-12-21 10:56:39 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (len == 0) {
|
|
|
|
a->neg = 0;
|
|
|
|
a->top = 0;
|
2016-04-27 11:55:44 +00:00
|
|
|
return a;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
d += 4;
|
|
|
|
if ((*d) & 0x80)
|
|
|
|
neg = 1;
|
2016-04-27 11:55:44 +00:00
|
|
|
if (BN_bin2bn(d, (int)len, a) == NULL) {
|
2016-04-28 18:53:08 +00:00
|
|
|
if (ain == NULL)
|
|
|
|
BN_free(a);
|
2016-04-27 11:55:44 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
a->neg = neg;
|
|
|
|
if (neg) {
|
|
|
|
BN_clear_bit(a, BN_num_bits(a) - 1);
|
|
|
|
}
|
|
|
|
bn_check_top(a);
|
2016-04-27 11:55:44 +00:00
|
|
|
return a;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|