]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_mpi.c
b8e92e126391266e942f39c7fbf5010908a6301e
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
14 int BN_bn2mpi(const BIGNUM
*a
, unsigned char *d
)
21 bits
= BN_num_bits(a
);
24 ext
= ((bits
& 0x07) == 0);
27 return (num
+ 4 + 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;
36 num
= BN_bn2bin(a
, &(d
[4 + ext
]));
39 return (num
+ 4 + ext
);
42 BIGNUM
*BN_mpi2bn(const unsigned char *d
, int n
, BIGNUM
*ain
)
48 if (n
< 4 || (d
[0] & 0x80) != 0) {
49 BNerr(BN_F_BN_MPI2BN
, BN_R_INVALID_LENGTH
);
52 len
= ((long)d
[0] << 24) | ((long)d
[1] << 16) | ((int)d
[2] << 8) | (int)
55 BNerr(BN_F_BN_MPI2BN
, BN_R_ENCODING_ERROR
);
75 if (BN_bin2bn(d
, (int)len
, a
) == NULL
) {
82 BN_clear_bit(a
, BN_num_bits(a
) - 1);