-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenssl_utils.h
173 lines (147 loc) · 4.68 KB
/
openssl_utils.h
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
/******************************************************************************
* BIKE -- Bit Flipping Key Encapsulation
*
* Copyright (c) 2021 Nir Drucker, Shay Gueron, Rafael Misoczki, Tobias Oder,
* Tim Gueneysu, Jan Richter-Brockmann.
* Contact: [email protected], [email protected],
*
* Permission to use this code BIKE is granted.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ""AS IS"" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS CORPORATION OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#ifndef _OSSL_UTILITIES_H_
#define _OSSL_UTILITIES_H_
#include "string.h"
#include "types.h"
#include "utilities.h"
#include "openssl/bn.h"
//Perform a cyclic product by using OpenSSL.
_INLINE_ status_t cyclic_product(OUT BIGNUM *r,
IN const BIGNUM *a,
IN const BIGNUM *b)
{
BN_CTX *bn_ctx = BN_CTX_new();
BIGNUM *m = BN_new();
status_t res = SUCCESS;
// m = x^PARAM_R - 1
if((BN_set_bit(m, R_BITS) == 0) ||
(BN_set_bit(m, 0) == 0))
{
ERR(E_OSSL_FAILURE);
}
// r = a*b mod m
if(BN_GF2m_mod_mul(r, a, b, m, bn_ctx) == 0)
{
ERR(E_OSSL_FAILURE);
}
EXIT:
//Cleanup
BN_free(m);
BN_CTX_free(bn_ctx);
return res;
}
_INLINE_ status_t invert_poly(OUT BIGNUM *r,
IN const BIGNUM *a)
{
BN_CTX *bn_ctx = BN_CTX_new();
BIGNUM *m = BN_new();
status_t res = SUCCESS;
// m = x^PARAM_R - 1
if((BN_set_bit(m, R_BITS) == 0) ||
(BN_set_bit(m, 0) == 0))
{
ERR(E_OSSL_FAILURE);
}
// r = a*b mod m
if(BN_GF2m_mod_inv (r, a, m, bn_ctx) == 0)
{
ERR(E_OSSL_FAILURE);
}
EXIT:
//Cleanup
BN_free(m);
BN_CTX_free(bn_ctx);
return res;
}
//Loading numbers into OpenSSL should be done in Big Endian representation.
//Therefore the byte order of a number should be reversed.
_INLINE_ void reverse_endian(OUT uint8_t *res,
IN const uint8_t *in,
IN const uint32_t n)
{
uint32_t i;
uint64_t tmp;
for(i = 0 ; i < (n/2) ; i++)
{
tmp = in[i];
res[i] = in[n-1-i];
res[n-1-i] = tmp;
}
//If the number of blocks is odd swap also the middle block.
if (n%2)
{
res[i] = in[i];
}
}
_INLINE_ status_t ossl_bn2bin(OUT uint8_t* out,
IN const BIGNUM* in,
IN const uint32_t size)
{
uint8_t be_tmp[size];
memset(out, 0, size);
if (BN_bn2bin(in, be_tmp) == 0)
{
return E_OSSL_FAILURE;
}
reverse_endian(out, be_tmp, BN_num_bytes(in));
return SUCCESS;
}
_INLINE_ status_t ossl_bin2bn(IN BIGNUM* out,
OUT const uint8_t* in,
IN const uint32_t size)
{
uint8_t be_tmp[size];
memset(be_tmp, 0, size);
reverse_endian(be_tmp, in, size);
if(BN_bin2bn(be_tmp, size, out) == 0)
{
return E_OSSL_FAILURE;
}
return SUCCESS;
}
_INLINE_ status_t print_ossl_bn(IN const BIGNUM* bn, IN const uint64_t size)
{
uint8_t tmp[size];
if(ossl_bn2bin(tmp, bn, size))
{
return E_OSSL_FAILURE;
}
print((uint64_t*)tmp, size*8);
return SUCCESS;
}
#endif //_OSSL_UTILITIES_H_