1
2/*
3 * Licensed Materials - Property of IBM
4 *
5 * trousers - An open source TCG Software Stack
6 *
7 * (C) Copyright International Business Machines Corp. 2006
8 *
9 */
10
11#ifndef BI_H_
12#define BI_H_
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17// for the BIGNUM definition
18#include <openssl/bn.h>
19
20#include "list.h"
21
22#define INLINE
23#undef INLINE_DECL
24#define INLINE_DECL static inline
25
26void * (*bi_alloc)(size_t size);
27
28// keep the list of allocated memory, usually used for the format functions
29extern list_ptr allocs;
30
31/************************************************************************************
32	TYPE DEF
33*************************************************************************************/
34
35#ifdef BI_GMP
36#include "bi_gmp.h"
37#endif
38
39#ifdef BI_OPENSSL
40#include "bi_openssl.h"
41#endif
42
43/************************************************************************************
44	TYPE DEF
45*************************************************************************************/
46
47struct _bi_array{
48	bi_ptr *array;
49	int length;
50};
51
52typedef struct _bi_array bi_array[1];
53typedef struct _bi_array *bi_array_ptr;
54
55/***********************************************************************************
56	CONSTANT
57*************************************************************************************/
58
59extern bi_t bi_0;
60extern bi_t bi_1;
61extern bi_t bi_2;
62
63/***********************************************************************************
64	TEMPORARY (WORK)
65*************************************************************************************/
66
67/*
68extern bi_t bi_tmp;
69extern bi_t bi_tmp1;
70extern bi_t bi_tmp2;
71extern bi_t bi_tmp3;
72extern bi_t bi_tmp4;
73extern bi_t bi_tmp5;
74extern bi_t bi_tmp6;
75extern bi_t bi_tmp7;
76extern bi_t bi_tmp8;
77extern bi_t bi_tmp9;
78*/
79
80/***********************************************************************************
81	MACROS
82*************************************************************************************/
83#define ALLOC_BI_ARRAY()  (bi_array_ptr)malloc( sizeof( bi_array))
84
85#if 0
86#define BI_SAVE( a, b)  do { bi_save( a, #a, b); } while(0);
87#define BI_SAVE_ARRAY( a, b)  do { bi_save_array( a, #a, b); } while(0);
88#define BI_LOAD( a, b)  do { bi_load( a, b); } while(0);
89#define BI_LOAD_ARRAY( a, b)  do { bi_load_array( a, b); } while(0);
90#endif
91
92#ifdef BI_DEBUG
93#define DUMP_BI(field)  do { \
94	fprintf(stderr, "%s=%s [%ld]\n", #field, bi_2_hex_char( field), bi_nbin_size(field));\
95	} while(0);
96
97#define DUMP_BI_ARRAY(field)  do { dump_bi_array( #field, field); } while(0);
98
99#else
100#define DUMP_BI(field)
101
102#define DUMP_BI_ARRAY(field)
103#endif
104
105/* to free only defines bi_ptr */
106#define FREE_BI(a) do { if( (a) != NULL) bi_free_ptr( a); } while(0);
107
108/***********************************************************************************
109	DUMP LIB
110*************************************************************************************/
111
112char *dump_byte_array(int len, unsigned char *array);
113
114/* convert <strings> and return it into a byte array <result> of length <length> */
115unsigned char *retrieve_byte_array( int *len, const char *strings);
116
117/***********************************************************************************
118	LIBRARY MANAGEMENT
119*************************************************************************************/
120/*
121 initialize the library
122 bi_alloc_p allocation function used only for exporting a bi struct, so for bi_2_nbin
123 if define as NULL, a stdlib malloc() will be used
124*/
125void bi_init( void * (*bi_alloc_p)(size_t size));
126
127/* release resources used by the library */
128void bi_release(void);
129
130/* return >0 if the library was initialized */
131int bi_is_initialized(void);
132
133/* free the list of internally allocated memory, usually used for the format functions */
134void bi_flush_memory(void);
135
136/***********************************************************************************
137	ALLOCATION & BASIC SETTINGS
138*************************************************************************************/
139
140/* create a big integer */
141bi_ptr bi_new( bi_ptr result);
142
143/* create a big integer pointer */
144bi_ptr bi_new_ptr(void);
145
146/* free resources allocated to the big integer <i> */
147void bi_free(const bi_ptr i);
148
149/* free resources allocated to the big integer pointer <i> */
150void bi_free_ptr(const bi_ptr i);
151
152/* return the current number of bits of the number */
153long bi_length( const bi_ptr res);
154
155/* create a <big integer> array */
156void bi_new_array( bi_array_ptr array, const int length);
157
158/* create a <big integer> array */
159void bi_new_array2( bi_array_ptr array, const int length);
160
161/* free resources allocated to the big integer <i> */
162void bi_free_array(bi_array_ptr array);
163
164/* copy length pointers from the array <src, offset_src> to array <dest, offset_dest> */
165void bi_copy_array(bi_array_ptr src,
166		int offset_src,
167		bi_array_ptr dest,
168		int offset_dest,
169		int length);
170
171// for debugging
172void dump_bi_array( char *field, const bi_array_ptr array);
173
174/***********************************************************************************
175	SAFE RANDOM
176*************************************************************************************/
177
178bi_ptr compute_random_number( bi_ptr result, const bi_ptr element);
179
180#if 0
181/***********************************************************************************
182	SAVE / LOAD
183*************************************************************************************/
184
185/* load an big integer in the already open ("r")  file */
186void bi_load( bi_ptr bi, FILE *file);
187
188/* load an big integer array  in the already open ("r")  file */
189void bi_load_array( bi_array_ptr array, FILE *file);
190
191/* save an big integer array  in the already open ("w")  file */
192void bi_save_array( const bi_array_ptr array, const char *name, FILE *file);
193
194/* save an big integer in the already open ("w")  file */
195void bi_save( const bi_ptr bi,const char *name,  FILE *file);
196#endif
197
198/***********************************************************************************
199	CONVERSION
200*************************************************************************************/
201
202/* dump the big integer as hexadecimal  */
203char *bi_2_hex_char(const bi_ptr i);
204
205 /* dump the big integer as decimal  */
206char *bi_2_dec_char(const bi_ptr i);
207
208 /* set <i> to the same value as <value> */
209 /*    <i> := <value>          */
210bi_ptr bi_set( bi_ptr i, const bi_ptr value);
211
212/* set <i> with the value represented by given hexadecimal <value> */
213 /*    <i> := <value>          */
214bi_ptr bi_set_as_hex( bi_ptr i, const char *value);
215
216/* set <i> with the value represented by given decimal <value> */
217 /*    <i> := <value>          */
218bi_ptr bi_set_as_dec( bi_ptr i, const char *value);
219
220/* set <i> with the value represented by unsigned int <value> */
221 /*    <i> := <value>          */
222bi_ptr bi_set_as_si( bi_ptr result, const int value);
223
224/* return (long)bi_t  */
225long bi_get_si(const bi_ptr i);
226
227/* return the size of a network byte order representation of <i>  */
228long bi_nbin_size(const bi_ptr i);
229
230/* return a BYTE * - in network byte order -  and update the length <length>  */
231/* the result is allocated internally */
232unsigned char *bi_2_nbin( int *length, const bi_ptr i);
233
234/* return a BYTE * - in network byte order -  and update the length <length>  */
235/* different from bi_2_nbin: you should reserve enough memory for the storage */
236void bi_2_nbin1( int *length, unsigned char *, const bi_ptr i);
237
238/* return a bi_ptr that correspond to the big endian encoded BYTE array of length <n_length> */
239bi_ptr bi_set_as_nbin( const unsigned long length, const unsigned char *buffer);
240
241/*
242 convert <bi> to a byte array of length result,
243 the beginning of this buffer is feel with '0' if needed
244*/
245void bi_2_byte_array( unsigned char *result, int length, bi_ptr bi);
246
247/* convert a bi to a openssl BIGNUM struct */
248BIGNUM *bi_2_BIGNUM( const bi_ptr);
249
250
251/***********************************************************************************
252	BITS OPERATION
253*************************************************************************************/
254/* set the bit to 1 */
255bi_ptr bi_setbit( bi_ptr result, const int bit);
256
257/* <result> := <i> << <n> */
258bi_ptr bi_shift_left( bi_ptr result, const bi_ptr i, const int n);
259
260/* <result> := <i> >> <n> */
261bi_ptr bi_shift_right( bi_ptr result, const bi_ptr i, const int n);
262
263/***********************************************************************************
264	NUMBER THEORIE OPERATION
265*************************************************************************************/
266/* create a random of length <length> bits */
267/*  res := random( length)  */
268bi_ptr bi_urandom( bi_ptr res, const long length);
269
270/* res := <n> mod <m> */
271bi_ptr bi_mod(bi_ptr res, const bi_ptr n, const bi_ptr m);
272
273/* res := <n> mod <m> */
274bi_ptr bi_mod_si(bi_ptr res, const bi_ptr n, const long m);
275
276/* generate prime number of <length> bits  */
277bi_ptr bi_generate_prime( bi_ptr i, const long length);
278
279/*
280return true (>0, bigger is better, but this is contextual to the plugin)
281if <i> is a probably prime
282*/
283int bi_is_probable_prime( const bi_ptr i);
284
285/* result := (inverse of <i>) mod <m> */
286/* if the inverse exist, return >0, otherwise 0 */
287int bi_invert_mod( bi_ptr result, const bi_ptr i, const bi_ptr m);
288
289/* generate a safe prime number of <length> bits  */
290/* by safe we mean a prime p so that (p-1)/2 is also prime */
291bi_ptr bi_generate_safe_prime( bi_ptr result, const long bit_length);
292
293/* return in <result> the greatest common divisor of <a> and <b> */
294/* <result> := gcd( <a>, <b>) */
295bi_ptr bi_gcd( bi_ptr result, bi_ptr a, bi_ptr b);
296
297/***********************************************************************************
298	BASIC MATH OPERATION
299*************************************************************************************/
300
301/* <result> := result++ */
302bi_ptr bi_inc(bi_ptr result);
303
304/* <result> := result-- */
305bi_ptr bi_dec(bi_ptr result);
306
307/* <result> := - <result> */
308bi_ptr bi_negate( bi_ptr result);
309
310/* set <result> by the multiplication of <i> by the long <n>  */
311/*  <result> := <i> * <n>   */
312bi_ptr bi_mul_si( bi_ptr result, const bi_ptr i, const long n);
313
314/*  <result> := <i> * <n>   */
315bi_ptr bi_mul( bi_ptr result, const bi_ptr i, const bi_ptr n);
316
317/* set <result> by the division of <i> by the long <n>  */
318/*  <result> := <i> / <n>   */
319bi_ptr bi_div_si( bi_ptr result, const bi_ptr i, const long n);
320
321/*  <result> := <i> / <n>   */
322bi_ptr bi_div( bi_ptr result, const bi_ptr i, const bi_ptr n);
323
324/* set <result> by the addition of <i> by the long <n>  */
325/*  <result> := <i> + <n>   */
326bi_ptr bi_add_si( bi_ptr result, const bi_ptr i, const long n);
327
328/*  <result> := <i> + <n>  */
329bi_ptr bi_add( bi_ptr result, const bi_ptr i, const bi_ptr n);
330
331/*  <result> := <i> - <n>   */
332bi_ptr bi_sub_si( bi_ptr result, const bi_ptr i, const long n);
333
334/*  <result> := <i> - <n>  */
335bi_ptr bi_sub( bi_ptr result, const bi_ptr i, const bi_ptr n);
336
337/*  <result> := ( <g> ^ <e> ) mod <m>  */
338bi_ptr bi_mod_exp_si( bi_ptr result, const bi_ptr g, const bi_ptr e, const long m);
339
340/*  <result> := ( <g> ^ <e> ) mod <m>  */
341bi_ptr bi_mod_exp( bi_ptr result, const bi_ptr g, const bi_ptr e, const bi_ptr m);
342
343/*
344multiple-exponentiation
345<result> := mod( Multi( <g>i, <e>i), number of byte <m>) with  0 <= i <= <n>
346bi_t[] is used for commodity (bi-ptr[] need allocation for each bi_ptr, something made
347in the stack with bi_t)
348*/
349bi_ptr bi_multi_mod_exp( bi_ptr result,
350			const int n,
351			const bi_t g[],
352			const long e[],
353			const int m);
354
355/***********************************************************************************
356	COMPARAISON
357*************************************************************************************/
358/*	n1<n2   return negative value
359	n1 = n2 return 0
360	n1>n2   return positive value
361*/
362int bi_cmp( const bi_ptr n1, const bi_ptr n2);
363
364/*	n1<n2   return negative value
365	n1 = n2 return 0
366	n1>n2   return positive value
367*/
368int bi_cmp_si( const bi_ptr n1, const int n2);
369
370/*	n1 == n2   return 1 (true)
371	else return 0
372*/
373int bi_equals( const bi_ptr n1, const bi_ptr n2);
374
375/*	n1 == n2   return 1 (true)
376	else return 0
377*/
378int bi_equals_si( const bi_ptr n1, const int n2);
379
380#endif /*BI_H_*/
381