aboutsummaryrefslogtreecommitdiffstats
path: root/modules/eng_ossl/bn_wrap.cpp
blob: 77c96ec30dc8e553a03ba90872aac5c4cff2ad92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*************************************************
* OpenSSL BN Wrapper Source File                 *
* (C) 1999-2008 The Botan Project                *
*************************************************/

#include <botan/bn_wrap.h>

namespace Botan {

/*************************************************
* OSSL_BN Constructor                            *
*************************************************/
OSSL_BN::OSSL_BN(const BigInt& in)
   {
   value = BN_new();
   SecureVector<byte> encoding = BigInt::encode(in);
   if(in != 0)
      BN_bin2bn(encoding, encoding.size(), value);
   }

/*************************************************
* OSSL_BN Constructor                            *
*************************************************/
OSSL_BN::OSSL_BN(const byte in[], u32bit length)
   {
   value = BN_new();
   BN_bin2bn(in, length, value);
   }

/*************************************************
* OSSL_BN Copy Constructor                       *
*************************************************/
OSSL_BN::OSSL_BN(const OSSL_BN& other)
   {
   value = BN_dup(other.value);
   }

/*************************************************
* OSSL_BN Destructor                             *
*************************************************/
OSSL_BN::~OSSL_BN()
   {
   BN_clear_free(value);
   }

/*************************************************
* OSSL_BN Assignment Operator                    *
*************************************************/
OSSL_BN& OSSL_BN::operator=(const OSSL_BN& other)
   {
   BN_copy(value, other.value);
   return (*this);
   }

/*************************************************
* Export the BIGNUM as a bytestring              *
*************************************************/
void OSSL_BN::encode(byte out[], u32bit length) const
   {
   BN_bn2bin(value, out + (length - bytes()));
   }

/*************************************************
* Return the number of significant bytes         *
*************************************************/
u32bit OSSL_BN::bytes() const
   {
   return BN_num_bytes(value);
   }

/*************************************************
* OpenSSL to BigInt Conversions                  *
*************************************************/
BigInt OSSL_BN::to_bigint() const
   {
   SecureVector<byte> out(bytes());
   BN_bn2bin(value, out);
   return BigInt::decode(out);
   }

/*************************************************
* OSSL_BN_CTX Constructor                        *
*************************************************/
OSSL_BN_CTX::OSSL_BN_CTX()
   {
   value = BN_CTX_new();
   }

/*************************************************
* OSSL_BN_CTX Copy Constructor                   *
*************************************************/
OSSL_BN_CTX::OSSL_BN_CTX(const OSSL_BN_CTX&)
   {
   value = BN_CTX_new();
   }

/*************************************************
* OSSL_BN_CTX Destructor                         *
*************************************************/
OSSL_BN_CTX::~OSSL_BN_CTX()
   {
   BN_CTX_free(value);
   }

/*************************************************
* OSSL_BN_CTX Assignment Operator                *
*************************************************/
OSSL_BN_CTX& OSSL_BN_CTX::operator=(const OSSL_BN_CTX&)
   {
   value = BN_CTX_new();
   return (*this);
   }

}