1 /*
2  *  Copyright(C) 2006 Cameron Rich
3  *
4  *  This library is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with this library; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 #ifndef BIGINT_HEADER
20 #define BIGINT_HEADER
21 
22 /* enable features based on a 'super-set' capbaility. */
23 #if defined(CONFIG_SSL_FULL_MODE)
24 #define CONFIG_SSL_ENABLE_CLIENT
25 #define CONFIG_SSL_CERT_VERIFICATION
26 #elif defined(CONFIG_SSL_ENABLE_CLIENT)
27 #define CONFIG_SSL_CERT_VERIFICATION
28 #endif
29 
30 #include "os_port.h"
31 #include "bigint_impl.h"
32 
33 #ifndef CONFIG_BIGINT_CHECK_ON
34 #define check(A)                /**< disappears in normal production mode */
35 #endif
36 BI_CTX *bi_initialize(void);
37 void bi_terminate(BI_CTX *ctx);
38 void bi_permanent(bigint *bi);
39 void bi_depermanent(bigint *bi);
40 void bi_free(BI_CTX *ctx, bigint *bi);
41 bigint *bi_copy(bigint *bi);
42 bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
43 void bi_export(BI_CTX *ctx, bigint *bi, uint8_t *data, int size);
44 bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int len);
45 bigint *int_to_bi(BI_CTX *ctx, comp i);
46 
47 /* the functions that actually do something interesting */
48 bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib);
49 bigint *bi_subtract(BI_CTX *ctx, bigint *bia,
50         bigint *bib, int *is_negative);
51 bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim, int is_mod);
52 bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib);
53 bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp);
54 bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp);
55 int bi_compare(bigint *bia, bigint *bib);
56 void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
57 void bi_free_mod(BI_CTX *ctx, int mod_offset);
58 
59 #ifdef CONFIG_SSL_FULL_MODE
60 void bi_print(const char *label, bigint *bi);
61 bigint *bi_str_import(BI_CTX *ctx, const char *data);
62 #endif
63 
64 /**
65  * @def bi_mod
66  * Find the residue of B. bi_set_mod() must be called before hand.
67  */
68 #define bi_mod(A, B)      bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1)
69 
70 /**
71  * bi_residue() is technically the same as bi_mod(), but it uses the
72  * appropriate reduction technique (which is bi_mod() when doing classical
73  * reduction).
74  */
75 #if defined(CONFIG_BIGINT_MONTGOMERY)
76 #define bi_residue(A, B)         bi_mont(A, B)
77 bigint *bi_mont(BI_CTX *ctx, bigint *bixy);
78 #elif defined(CONFIG_BIGINT_BARRETT)
79 #define bi_residue(A, B)         bi_barrett(A, B)
80 bigint *bi_barrett(BI_CTX *ctx, bigint *bi);
81 #else /* if defined(CONFIG_BIGINT_CLASSICAL) */
82 #define bi_residue(A, B)         bi_mod(A, B)
83 #endif
84 
85 #ifdef CONFIG_BIGINT_SQUARE
86 bigint *bi_square(BI_CTX *ctx, bigint *bi);
87 #else
88 #define bi_square(A, B)     bi_multiply(A, bi_copy(B), B)
89 #endif
90 
91 #endif
92