3
* Licensed Materials - Property of IBM
5
* trousers - An open source TCG Software Stack
7
* (C) Copyright International Business Machines Corp. 2006
15
// needed for OPENSSL_free
16
#include <openssl/crypto.h>
19
typedef mpz_ptr bi_ptr;
21
extern gmp_randstate_t state;
23
/* initialized the given big integer */
24
INLINE_DECL bi_ptr bi_new(bi_t i) {
29
/* create a big integer pointer */
30
INLINE_DECL bi_ptr bi_new_ptr(void) {
33
res = (mpz_ptr)malloc( sizeof( mpz_t));
34
if( res == NULL) return NULL;
39
/* free resources allocated to the big integer <i> */
40
INLINE_DECL void bi_free(const bi_ptr i) {
44
/* free resources allocated to the big integer pointer <i> */
45
INLINE_DECL void bi_free_ptr(const bi_ptr i) {
50
/* return the current number of bits of the number */
51
INLINE_DECL long bi_length( const bi_ptr res) {
52
return mpz_sizeinbase( res, 2);
55
/***********************************************************************************
57
*************************************************************************************/
60
/* return an hex dump of the given big integer */
61
INLINE_DECL char *bi_2_hex_char(const bi_ptr i) {
64
gmp_asprintf( &ret, "%ZX", i);
65
list_add( allocs, ret);
69
/* return an hex dump of the given big integer */
70
INLINE_DECL char *bi_2_dec_char(const bi_ptr i) {
73
gmp_asprintf( &ret, "%Zd", i);
74
list_add( allocs, ret);
78
/* set <i> to the same value as the big integer <value> */
79
INLINE_DECL bi_ptr bi_set( bi_ptr result, const bi_ptr value) {
80
mpz_set( result, value);
84
/* set the initialized variable to the value represented by the given hex format stirng */
85
INLINE_DECL bi_ptr bi_set_as_hex( bi_ptr result, const char *value) {
86
mpz_set_str( result, value, 16);
90
/* set the initialized variable to the value represented by the given hex format stirng */
91
INLINE_DECL bi_ptr bi_set_as_dec( bi_ptr result, const char *value) {
92
mpz_set_str( result, value, 10);
96
/* set <i> with the value represented by unsigned int <value> */
98
INLINE_DECL bi_ptr bi_set_as_si( bi_ptr result, const int value) {
99
mpz_set_si( result, value);
103
/* return (long)bi_t */
104
INLINE_DECL long bi_get_si(const bi_ptr i) {
105
return mpz_get_si( i);
108
/* convert a bi type to a openssl BIGNUM struct */
109
INLINE_DECL BIGNUM *bi_2_BIGNUM( const bi_ptr i) {
111
char *value = bi_2_hex_char( i);
113
BN_hex2bn( &result, value);
117
/* set <i> with the value represented by the given openssl BIGNUM struct */
118
INLINE_DECL bi_ptr bi_set_as_BIGNUM( bi_ptr i, BIGNUM *bn) {
119
char *value = BN_bn2hex( bn);
121
if( value == NULL) return NULL;
122
bi_set_as_hex( i, value);
123
OPENSSL_free( value);
127
/***********************************************************************************
129
*************************************************************************************/
131
/* <result> := result + 1 */
132
INLINE_DECL bi_ptr bi_inc(bi_ptr result) {
133
mpz_add_ui( result, result, 1);
137
/* <result> := result - 1 */
138
INLINE_DECL bi_ptr bi_dec(bi_ptr result) {
139
mpz_sub_ui( result, result, 1);
143
/* set <result> by the division of <i> by the long <n> */
144
/* <result> := <i> / <n> */
145
INLINE_DECL bi_ptr bi_div_si( bi_ptr result, const bi_ptr i, const long n) {
146
mpz_div_ui( result, i, n);
150
/* <result> := <i> / <n> */
151
INLINE_DECL bi_ptr bi_div( bi_ptr result, const bi_ptr i, const bi_ptr n) {
152
mpz_div( result, i, n);
156
/* <result> := - <result> */
157
INLINE_DECL bi_ptr bi_negate( bi_ptr result) {
158
mpz_neg( result, result);
162
/* multiply the given big integer <i> by the give long <n> and return the result in <result> */
163
INLINE_DECL bi_ptr bi_mul_si( bi_ptr result, const bi_ptr i, const long n) {
164
mpz_mul_si( result, i, n);
168
/* <result> := <i> * <n> */
169
INLINE_DECL bi_ptr bi_mul( bi_ptr result, const bi_ptr i, const bi_ptr n) {
170
mpz_mul( result, i, n);
174
/* <result> := <i> + <n> */
175
INLINE_DECL bi_ptr bi_add_si( bi_ptr result, const bi_ptr i, const long n) {
176
mpz_add_ui( result, i, n);
180
/* <result> := <i> + <n> */
181
INLINE_DECL bi_ptr bi_add( bi_ptr result, const bi_ptr i, const bi_ptr n) {
182
mpz_add( result, i, n);
186
/* <result> := <i> - <n> */
187
INLINE_DECL bi_ptr bi_sub_si( bi_ptr result, const bi_ptr i, const long n) {
188
// n should be unsigned
189
mpz_sub_ui( result, i, n);
193
/* <result> := <i> - <n> */
194
INLINE_DECL bi_ptr bi_sub( bi_ptr result, const bi_ptr i, const bi_ptr n) {
195
mpz_sub( result, result, n);
199
/* <result> := ( <g> ^ <e> ) mod <m> */
200
INLINE_DECL bi_ptr bi_mod_exp( bi_ptr result, const bi_ptr g, const bi_ptr e, const bi_ptr m) {
201
mpz_powm( result, g, e, m);
205
/* <result> := ( <g> ^ <e> ) mod <m> */
206
INLINE_DECL bi_ptr bi_mod_exp_si( bi_ptr result, const bi_ptr g, const bi_ptr e, const long m) {
210
mpz_set_si( bi_m, m);
211
mpz_powm( result, g, e, bi_m);
216
/***********************************************************************************
218
*************************************************************************************/
219
/* set the bit to 1 */
220
INLINE_DECL bi_ptr bi_setbit(bi_ptr result, const int bit) {
221
mpz_setbit( result, bit);
225
/* <result> := <i> << <n> */
226
INLINE_DECL bi_ptr bi_shift_left( bi_ptr result, const bi_ptr i, const int n) {
227
mpz_mul_2exp( result, i, n);
231
/* <result> := <i> >> <n> */
232
INLINE_DECL bi_ptr bi_shift_right( bi_ptr result, const bi_ptr i, const int n) {
233
mpz_div_2exp( result, i, n);
237
/***********************************************************************************
239
*************************************************************************************/
240
/* n1<n2 return negative value
242
* n1>n2 return positive value
244
INLINE_DECL int bi_cmp( const bi_ptr n1, const bi_ptr n2) {
245
return mpz_cmp( n1, n2);
248
/* n1<n2 return negative value
250
* n1>n2 return positive value
252
INLINE_DECL int bi_cmp_si( const bi_ptr n1, const int n2) {
253
return mpz_cmp_ui( n1, n2);
256
/* n1 == n2 return 1 (true)
259
INLINE_DECL int bi_equals( const bi_ptr n1, const bi_ptr n2) {
260
return mpz_cmp( n1, n2) == 0 ? 1 : 0;
263
/* n1 == n2 return 1 (true)
266
INLINE_DECL int bi_equals_si( const bi_ptr n1, const int n2) {
267
return mpz_cmp_ui( n1, n2) == 0 ? 1 : 0;
270
/* create a random of length <length> bits */
271
/* res := random( length) */
272
INLINE_DECL bi_ptr bi_urandom( bi_ptr result, const long length) {
273
mpz_urandomb( result, state, length);
277
/* res := <n> mod <m> */
278
INLINE_DECL bi_ptr bi_mod_si( bi_ptr result, const bi_ptr n, const long m) {
279
mpz_mod_ui( result, n, m);
283
/* res := <n> mod <m> */
284
INLINE_DECL bi_ptr bi_mod( bi_ptr result, const bi_ptr n, const bi_ptr m) {
285
mpz_mod( result, n, m);
289
/* result := (inverse of <i>) mod <m> */
290
/* if the inverse exist, return >0, otherwise 0 */
291
INLINE_DECL int bi_invert_mod( bi_ptr result, const bi_ptr i, const bi_ptr m) {
292
return mpz_invert( result, i, m);