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