| 1 | /* |
| 2 | * Copyright (c) 2007, Cameron Rich |
| 3 | * |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright notice, |
| 10 | * this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * * Neither the name of the axTLS project nor the names of its contributors |
| 15 | * may be used to endorse or promote products derived from this software |
| 16 | * without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef BIGINT_HEADER |
| 32 | #define BIGINT_HEADER |
| 33 | |
| 34 | #include "crypto.h" |
| 35 | |
| 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_clear_cache(BI_CTX *ctx); |
| 41 | void bi_free(BI_CTX *ctx, bigint *bi); |
| 42 | bigint *bi_copy(bigint *bi); |
| 43 | bigint *bi_clone(BI_CTX *ctx, const bigint *bi); |
| 44 | void bi_export(BI_CTX *ctx, bigint *bi, uint8_t *data, int size); |
| 45 | bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int len); |
| 46 | bigint *int_to_bi(BI_CTX *ctx, comp i); |
| 47 | |
| 48 | /* the functions that actually do something interesting */ |
| 49 | bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib); |
| 50 | bigint *bi_subtract(BI_CTX *ctx, bigint *bia, |
| 51 | bigint *bib, int *is_negative); |
| 52 | bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim, int is_mod); |
| 53 | bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib); |
| 54 | bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp); |
| 55 | bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp); |
| 56 | int bi_compare(bigint *bia, bigint *bib); |
| 57 | void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset); |
| 58 | void bi_free_mod(BI_CTX *ctx, int mod_offset); |
| 59 | |
| 60 | #ifdef CONFIG_SSL_FULL_MODE |
| 61 | void bi_print(const char *label, bigint *bi); |
| 62 | bigint *bi_str_import(BI_CTX *ctx, const char *data); |
| 63 | #endif |
| 64 | |
| 65 | /** |
| 66 | * @def bi_mod |
| 67 | * Find the residue of B. bi_set_mod() must be called before hand. |
| 68 | */ |
| 69 | #define bi_mod(A, B) bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1) |
| 70 | |
| 71 | /** |
| 72 | * bi_residue() is technically the same as bi_mod(), but it uses the |
| 73 | * appropriate reduction technique (which is bi_mod() when doing classical |
| 74 | * reduction). |
| 75 | */ |
| 76 | #if defined(CONFIG_BIGINT_MONTGOMERY) |
| 77 | #define bi_residue(A, B) bi_mont(A, B) |
| 78 | bigint *bi_mont(BI_CTX *ctx, bigint *bixy); |
| 79 | #elif defined(CONFIG_BIGINT_BARRETT) |
| 80 | #define bi_residue(A, B) bi_barrett(A, B) |
| 81 | bigint *bi_barrett(BI_CTX *ctx, bigint *bi); |
| 82 | #else /* if defined(CONFIG_BIGINT_CLASSICAL) */ |
| 83 | #define bi_residue(A, B) bi_mod(A, B) |
| 84 | #endif |
| 85 | |
| 86 | #ifdef CONFIG_BIGINT_SQUARE |
| 87 | bigint *bi_square(BI_CTX *ctx, bigint *bi); |
| 88 | #else |
| 89 | #define bi_square(A, B) bi_multiply(A, bi_copy(B), B) |
| 90 | #endif |
| 91 | |
| 92 | #ifdef CONFIG_BIGINT_CRT |
| 93 | bigint *bi_crt(BI_CTX *ctx, bigint *bi, |
| 94 | bigint *dP, bigint *dQ, |
| 95 | bigint *p, bigint *q, |
| 96 | bigint *qInv); |
| 97 | #endif |
| 98 | |
| 99 | #endif |