xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/include/daa/bi.h (revision 1023804e3833a0bd94414f2545512128f6502c74)
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 
26 void * (*bi_alloc)(size_t size);
27 
28 // keep the list of allocated memory, usually used for the format functions
29 extern 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 
47 struct _bi_array{
48 	bi_ptr *array;
49 	int length;
50 };
51 
52 typedef struct _bi_array bi_array[1];
53 typedef struct _bi_array *bi_array_ptr;
54 
55 /***********************************************************************************
56 	CONSTANT
57 *************************************************************************************/
58 
59 extern bi_t bi_0;
60 extern bi_t bi_1;
61 extern bi_t bi_2;
62 
63 /***********************************************************************************
64 	TEMPORARY (WORK)
65 *************************************************************************************/
66 
67 /*
68 extern bi_t bi_tmp;
69 extern bi_t bi_tmp1;
70 extern bi_t bi_tmp2;
71 extern bi_t bi_tmp3;
72 extern bi_t bi_tmp4;
73 extern bi_t bi_tmp5;
74 extern bi_t bi_tmp6;
75 extern bi_t bi_tmp7;
76 extern bi_t bi_tmp8;
77 extern 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 
112 char *dump_byte_array(int len, unsigned char *array);
113 
114 /* convert <strings> and return it into a byte array <result> of length <length> */
115 unsigned 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 */
125 void bi_init( void * (*bi_alloc_p)(size_t size));
126 
127 /* release resources used by the library */
128 void bi_release(void);
129 
130 /* return >0 if the library was initialized */
131 int bi_is_initialized(void);
132 
133 /* free the list of internally allocated memory, usually used for the format functions */
134 void bi_flush_memory(void);
135 
136 /***********************************************************************************
137 	ALLOCATION & BASIC SETTINGS
138 *************************************************************************************/
139 
140 /* create a big integer */
141 bi_ptr bi_new( bi_ptr result);
142 
143 /* create a big integer pointer */
144 bi_ptr bi_new_ptr(void);
145 
146 /* free resources allocated to the big integer <i> */
147 void bi_free(const bi_ptr i);
148 
149 /* free resources allocated to the big integer pointer <i> */
150 void bi_free_ptr(const bi_ptr i);
151 
152 /* return the current number of bits of the number */
153 long bi_length( const bi_ptr res);
154 
155 /* create a <big integer> array */
156 void bi_new_array( bi_array_ptr array, const int length);
157 
158 /* create a <big integer> array */
159 void bi_new_array2( bi_array_ptr array, const int length);
160 
161 /* free resources allocated to the big integer <i> */
162 void bi_free_array(bi_array_ptr array);
163 
164 /* copy length pointers from the array <src, offset_src> to array <dest, offset_dest> */
165 void 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
172 void dump_bi_array( char *field, const bi_array_ptr array);
173 
174 /***********************************************************************************
175 	SAFE RANDOM
176 *************************************************************************************/
177 
178 bi_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 */
186 void bi_load( bi_ptr bi, FILE *file);
187 
188 /* load an big integer array  in the already open ("r")  file */
189 void bi_load_array( bi_array_ptr array, FILE *file);
190 
191 /* save an big integer array  in the already open ("w")  file */
192 void 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 */
195 void 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  */
203 char *bi_2_hex_char(const bi_ptr i);
204 
205  /* dump the big integer as decimal  */
206 char *bi_2_dec_char(const bi_ptr i);
207 
208  /* set <i> to the same value as <value> */
209  /*    <i> := <value>          */
210 bi_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>          */
214 bi_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>          */
218 bi_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>          */
222 bi_ptr bi_set_as_si( bi_ptr result, const int value);
223 
224 /* return (long)bi_t  */
225 long bi_get_si(const bi_ptr i);
226 
227 /* return the size of a network byte order representation of <i>  */
228 long 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 */
232 unsigned 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 */
236 void 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> */
239 bi_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 */
245 void bi_2_byte_array( unsigned char *result, int length, bi_ptr bi);
246 
247 /* convert a bi to a openssl BIGNUM struct */
248 BIGNUM *bi_2_BIGNUM( const bi_ptr);
249 
250 
251 /***********************************************************************************
252 	BITS OPERATION
253 *************************************************************************************/
254 /* set the bit to 1 */
255 bi_ptr bi_setbit( bi_ptr result, const int bit);
256 
257 /* <result> := <i> << <n> */
258 bi_ptr bi_shift_left( bi_ptr result, const bi_ptr i, const int n);
259 
260 /* <result> := <i> >> <n> */
261 bi_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)  */
268 bi_ptr bi_urandom( bi_ptr res, const long length);
269 
270 /* res := <n> mod <m> */
271 bi_ptr bi_mod(bi_ptr res, const bi_ptr n, const bi_ptr m);
272 
273 /* res := <n> mod <m> */
274 bi_ptr bi_mod_si(bi_ptr res, const bi_ptr n, const long m);
275 
276 /* generate prime number of <length> bits  */
277 bi_ptr bi_generate_prime( bi_ptr i, const long length);
278 
279 /*
280 return true (>0, bigger is better, but this is contextual to the plugin)
281 if <i> is a probably prime
282 */
283 int 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 */
287 int 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 */
291 bi_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>) */
295 bi_ptr bi_gcd( bi_ptr result, bi_ptr a, bi_ptr b);
296 
297 /***********************************************************************************
298 	BASIC MATH OPERATION
299 *************************************************************************************/
300 
301 /* <result> := result++ */
302 bi_ptr bi_inc(bi_ptr result);
303 
304 /* <result> := result-- */
305 bi_ptr bi_dec(bi_ptr result);
306 
307 /* <result> := - <result> */
308 bi_ptr bi_negate( bi_ptr result);
309 
310 /* set <result> by the multiplication of <i> by the long <n>  */
311 /*  <result> := <i> * <n>   */
312 bi_ptr bi_mul_si( bi_ptr result, const bi_ptr i, const long n);
313 
314 /*  <result> := <i> * <n>   */
315 bi_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>   */
319 bi_ptr bi_div_si( bi_ptr result, const bi_ptr i, const long n);
320 
321 /*  <result> := <i> / <n>   */
322 bi_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>   */
326 bi_ptr bi_add_si( bi_ptr result, const bi_ptr i, const long n);
327 
328 /*  <result> := <i> + <n>  */
329 bi_ptr bi_add( bi_ptr result, const bi_ptr i, const bi_ptr n);
330 
331 /*  <result> := <i> - <n>   */
332 bi_ptr bi_sub_si( bi_ptr result, const bi_ptr i, const long n);
333 
334 /*  <result> := <i> - <n>  */
335 bi_ptr bi_sub( bi_ptr result, const bi_ptr i, const bi_ptr n);
336 
337 /*  <result> := ( <g> ^ <e> ) mod <m>  */
338 bi_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>  */
341 bi_ptr bi_mod_exp( bi_ptr result, const bi_ptr g, const bi_ptr e, const bi_ptr m);
342 
343 /*
344 multiple-exponentiation
345 <result> := mod( Multi( <g>i, <e>i), number of byte <m>) with  0 <= i <= <n>
346 bi_t[] is used for commodity (bi-ptr[] need allocation for each bi_ptr, something made
347 in the stack with bi_t)
348 */
349 bi_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 */
362 int 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 */
368 int bi_cmp_si( const bi_ptr n1, const int n2);
369 
370 /*	n1 == n2   return 1 (true)
371 	else return 0
372 */
373 int bi_equals( const bi_ptr n1, const bi_ptr n2);
374 
375 /*	n1 == n2   return 1 (true)
376 	else return 0
377 */
378 int bi_equals_si( const bi_ptr n1, const int n2);
379 
380 #endif /*BI_H_*/
381