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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*************************************************
* AEP Interface Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/eng_aep.h>
#include <botan/parsing.h>
#include <botan/util.h>
#include <botan/mutex.h>
#include <botan/aep_conn.h>
#include <botan/hw_aep.h>
#include <botan/es_aep.h>
namespace Botan {
namespace {
/*************************************************
* AEP Exception *
*************************************************/
class AEP_Exception : public Exception
{
public:
AEP_Exception(const std::string func, u32bit retval) :
Exception(func + " failed; returned " + to_string(retval)) {}
};
/*************************************************
* Return the size in bytes of this BigInt *
*************************************************/
u32bit get_bigint_size(void* bigint_ptr, u32bit* bytes)
{
const BigInt* bigint = static_cast<BigInt*>(bigint_ptr);
const u32bit actual_bytes = bigint->bytes();
*bytes = round_up(actual_bytes, 4);
return 0;
}
/*************************************************
* Store a BigInt into AEP format *
*************************************************/
u32bit store_bigint(void* bigint_ptr, u32bit output_size, byte* output)
{
const BigInt* bigint = static_cast<BigInt*>(bigint_ptr);
const u32bit leading_zeros = round_up(bigint->bytes(), 4) - bigint->bytes();
clear_mem(output, output_size);
bigint->binary_encode(output + leading_zeros);
for(u32bit j = 0; j != output_size / 2; j++)
std::swap(output[j], output[output_size-j-1]);
return 0;
}
/*************************************************
* Read a BigInt from the AEP format *
*************************************************/
u32bit create_bigint(void* bigint_ptr, u32bit input_size, byte* input)
{
BigInt* bigint = static_cast<BigInt*>(bigint_ptr);
for(u32bit j = 0; j != input_size / 2; j++)
std::swap(input[j], input[input_size-j-1]);
bigint->binary_decode(input, input_size);
return 0;
}
}
/*************************************************
* AEP Modular Exponentiation Operation *
*************************************************/
BigInt AEP_Engine::pow_mod(const BigInt& i, const BigInt& x, const BigInt& m)
{
BigInt output;
AEP_Connection conn;
u32bit retval = AEP::AEP_ModExp(conn, &i, &x, &m, &output, 0);
if(retval != 0)
throw AEP_Exception("AEP_ModExp", retval);
return output;
}
/*************************************************
* AEP Modular Exponentiation with CRT Operation *
*************************************************/
BigInt AEP_Engine::pow_mod_crt(const BigInt& i, const BigInt&,
const BigInt& p, const BigInt& q,
const BigInt& d1, const BigInt& d2,
const BigInt& c)
{
BigInt output;
AEP_Connection conn;
u32bit retval = AEP::AEP_ModExpCrt(conn, &i, &p, &q, &d1, &d2, &c,
&output, 0);
if(retval != 0)
throw AEP_Exception("AEP_ModExpCrt", retval);
return output;
}
/*************************************************
* AEP RNG Operation *
*************************************************/
u32bit AEP_Engine::get_entropy(byte output[], u32bit length) throw()
{
if(length > 256)
length = 256;
try {
AEP_Connection conn;
u32bit retval = AEP::AEP_GenRandom(conn, length, 1, output, 0);
if(retval != 0)
return 0;
return length;
}
catch(...)
{
return 0;
}
}
/*************************************************
* AEP usability check *
*************************************************/
bool AEP_Engine::ok_to_use(const BigInt& x) throw()
{
if(daemon_is_up && (x.bits() <= AEP::MAX_MODULO_BITS))
return true;
return false;
}
/*************************************************
* AEP daemon status flag *
*************************************************/
bool AEP_Engine::daemon_is_up = false;
/*************************************************
* AEP_Engine Constructor *
*************************************************/
AEP_Engine::AEP_Engine()
{
daemon_is_up = false;
try {
u32bit retval = AEP::AEP_Initialize(0);
if(retval != 0 && retval != AEP::ALREADY_INIT)
throw AEP_Exception("AEP_Initialize", retval);
if(retval == 0)
{
retval = AEP::AEP_SetBNCallBacks(get_bigint_size, store_bigint,
create_bigint);
if(retval != 0)
throw AEP_Exception("AEP_SetBNCallBacks", retval);
AEP_Connection conn;
daemon_is_up = true;
}
}
catch(AEP_Exception&) {}
}
/*************************************************
* AEP_Engine Destructor *
*************************************************/
AEP_Engine::~AEP_Engine()
{
AEP_Connection::close_all_connections();
u32bit retval = AEP::AEP_Finalize();
if(retval != 0)
throw AEP_Exception("AEP_Finalize", retval);
}
/*************************************************
* Gather Entropy from AEP Hardware RNG *
*************************************************/
u32bit AEP_EntropySource::slow_poll(byte output[], u32bit length)
{
return AEP_Engine::get_entropy(output, length);
}
}
|