Botan 1.10.17

bn_wrap.cpp
Go to the documentation of this file.
1 /*
2 * OpenSSL BN Wrapper
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/bn_wrap.h>
9
10 namespace Botan {
11
12 /*
13 * OSSL_BN Constructor
14 */
15 OSSL_BN::OSSL_BN(const BigInt& in)
16 {
17 value = BN_new();
18 SecureVector<byte> encoding = BigInt::encode(in);
19 if(in != 0)
20 BN_bin2bn(encoding, encoding.size(), value);
21 }
22
23 /*
24 * OSSL_BN Constructor
25 */
26 OSSL_BN::OSSL_BN(const byte in[], size_t length)
27 {
28 value = BN_new();
29 BN_bin2bn(in, length, value);
30 }
31
32 /*
33 * OSSL_BN Copy Constructor
34 */
35 OSSL_BN::OSSL_BN(const OSSL_BN& other)
36 {
37 value = BN_dup(other.value);
38 }
39
40 /*
41 * OSSL_BN Destructor
42 */
43 OSSL_BN::~OSSL_BN()
44 {
45 BN_clear_free(value);
46 }
47
48 /*
49 * OSSL_BN Assignment Operator
50 */
51 OSSL_BN& OSSL_BN::operator=(const OSSL_BN& other)
52 {
53 BN_copy(value, other.value);
54 return (*this);
55 }
56
57 /*
58 * Export the BIGNUM as a bytestring
59 */
60 void OSSL_BN::encode(byte out[], size_t length) const
61 {
62 BN_bn2bin(value, out + (length - bytes()));
63 }
64
65 /*
66 * Return the number of significant bytes
67 */
68 size_t OSSL_BN::bytes() const
69 {
70 return BN_num_bytes(value);
71 }
72
73 /*
74 * OpenSSL to BigInt Conversions
75 */
76 BigInt OSSL_BN::to_bigint() const
77 {
78 SecureVector<byte> out(bytes());
79 BN_bn2bin(value, out);
80 return BigInt::decode(out);
81 }
82
83 /*
84 * OSSL_BN_CTX Constructor
85 */
86 OSSL_BN_CTX::OSSL_BN_CTX()
87 {
88 value = BN_CTX_new();
89 }
90
91 /*
92 * OSSL_BN_CTX Copy Constructor
93 */
94 OSSL_BN_CTX::OSSL_BN_CTX(const OSSL_BN_CTX&)
95 {
96 value = BN_CTX_new();
97 }
98
99 /*
100 * OSSL_BN_CTX Destructor
101 */
102 OSSL_BN_CTX::~OSSL_BN_CTX()
103 {
104 BN_CTX_free(value);
105 }
106
107 /*
108 * OSSL_BN_CTX Assignment Operator
109 */
110 OSSL_BN_CTX& OSSL_BN_CTX::operator=(const OSSL_BN_CTX&)
111 {
112 value = BN_CTX_new();
113 return (*this);
114 }
115
116 }
Botan::OSSL_BN::encode
void encode(byte[], size_t) const
Definition: bn_wrap.cpp:60
Botan::BigInt
Definition: bigint.h:22
Botan::OSSL_BN_CTX::OSSL_BN_CTX
OSSL_BN_CTX()
Definition: bn_wrap.cpp:86
Botan::OSSL_BN::OSSL_BN
OSSL_BN(const OSSL_BN &)
Definition: bn_wrap.cpp:35
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::OSSL_BN_CTX::operator=
OSSL_BN_CTX & operator=(const OSSL_BN_CTX &)
Definition: bn_wrap.cpp:110
Botan::OSSL_BN_CTX
Definition: bn_wrap.h:42
Botan
Definition: algo_base.h:14
Botan::OSSL_BN::~OSSL_BN
~OSSL_BN()
Definition: bn_wrap.cpp:43
Botan::OSSL_BN::operator=
OSSL_BN & operator=(const OSSL_BN &)
Definition: bn_wrap.cpp:51
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::OSSL_BN::to_bigint
BigInt to_bigint() const
Definition: bn_wrap.cpp:76
Botan::BigInt::decode
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
Botan::OSSL_BN::bytes
size_t bytes() const
Definition: bn_wrap.cpp:68
Botan::OSSL_BN
Definition: bn_wrap.h:19
Botan::SecureVector< byte >
Botan::OSSL_BN_CTX::~OSSL_BN_CTX
~OSSL_BN_CTX()
Definition: bn_wrap.cpp:102
Botan::OSSL_BN_CTX::value
BN_CTX * value
Definition: bn_wrap.h:45
Botan::OSSL_BN::value
BIGNUM * value
Definition: bn_wrap.h:22