ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crypto_math.h Source File

crypto_math.h

Go to the documentation of this file.
00001 /*
00002 MiniTLS - A super trimmed down TLS/SSL Library for embedded devices
00003 Author: Donatien Garnier
00004 Copyright (C) 2013-2014 AppNearMe Ltd
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 *//**
00020  * \file crypto_math.h
00021  * \copyright Copyright (c) AppNearMe Ltd 2013
00022  * \author Donatien Garnier
00023  */
00024 
00025 #ifndef CRYPTO_MATH_H_
00026 #define CRYPTO_MATH_H_
00027 
00028 #ifdef __cplusplus
00029 extern "C" {
00030 #endif
00031 
00032 #include "math/headers/tfm.h"
00033 
00034 #define MP_LT   -1
00035 #define MP_EQ    0
00036 #define MP_GT    1
00037 
00038 /* replies */
00039 #define MP_YES        1   /* yes response */
00040 #define MP_NO         0   /* no response */
00041 
00042 #define MP_DIGIT_BIT                 DIGIT_BIT
00043 
00044 /* some handy macros */
00045 #define mp_init(a)                   fp_init(a)
00046 #define mp_init_multi                ltc_init_multi
00047 extern int max_used;
00048 #define mp_clear(a)                  /*do{ max_used = MAX(max_used, ((fp_int*)a)->used); printf("max used=%ul\r\n", max_used);*/ memset((a), 0, sizeof(fp_int)); /*}while(0) //fp_deinit(a)*/
00049 #define mp_clear_multi               ltc_deinit_multi
00050 #define mp_init_copy(a, b)           fp_init_copy(a, b)
00051 
00052 #define mp_neg(a, b)                 fp_neg(a, b)
00053 #define mp_copy(a, b)                fp_copy(a, b)
00054 
00055 #define mp_set(a, b)                 fp_set(a, b)
00056 #define mp_set_int(a, b)             fp_set_int(a, b)
00057 #define mp_get_int(a)                fp_get_int(a)
00058 #define mp_get_digit(a, n)           (((n) >= ((fp_int*)a)->used || (n) < 0) ? 0 : ((fp_int*)a)->dp[(n)])
00059 #define mp_get_digit_count(a)        (((fp_int*)a)->used)
00060 #define mp_cmp(a, b)                 fp_cmp(a, b)
00061 #define mp_cmp_d(a, b)               fp_cmp_d(a, b)
00062 #define mp_count_bits(a)             fp_count_bits(a)
00063 #define mp_cnt_lsb(a)                fp_count_lsb_bits(a)
00064 #define mp_2expt(a, b)               fp_twoexpt(a, b)
00065 
00066 #define mp_read_radix(a, b, c)       fp_read_radix(a, b, c)
00067 #define mp_toradix(a, b, c)          fp_write_radix(a, b, c)
00068 #define mp_unsigned_bin_size(a)      fp_unsigned_bin_size(a)
00069 #define mp_to_unsigned_bin(a, b)     fp_to_unsigned_bin(a, b)
00070 #define mp_read_unsigned_bin(a, b, c) fp_read_unsigned_bin(a, b, c)
00071 
00072 #define mp_add(a, b, c)              fp_add(a, b, c)
00073 #define mp_add_d(a, b, c)            fp_addi(a, b, c)
00074 #define mp_sub(a, b, c)              fp_sub(a, b, c)
00075 #define mp_sub_d(a, b, c)            fp_subi(a, b, c)
00076 #define mp_mul(a, b, c)              fp_mul(a, b, c)
00077 #define mp_mul_d(a, b, c)            fp_muli(a, b, c)
00078 #define mp_sqr(a, b)                 fp_sqr(a, b)
00079 #define mp_div(a, b, c, d)           fp_mpdiv(a, b, c, d)
00080 #define mp_div_2(a, b)               fp_div_2(a, b)
00081 #define mp_mod(a, b, c)              fp_div(a, b, NULL, c)
00082 #define mp_mod_d(a, b, c)            fp_modi(a, b, c)
00083 #define mp_gcd(a, b, c)              fp_gcd(a, b, c)
00084 #define mp_lcm(a, b, c)              fp_lcm(a, b, c)
00085 
00086 #define mp_mulmod(a, b, c, d)        fp_mulmod(a, b, c, d)
00087 #define mp_sqrmod(a, b, c)           fp_sqrmod(a, b, c)
00088 #define mp_invmod(a, b, c)           fp_invmod(a, b, c)
00089 
00090 #define mp_montgomery_setup(a, b)    fp_montgomery_setup(a, b)
00091 #define mp_montgomery_normalization(a, b) fp_montgomery_calc_normalization(a, b)
00092 #define mp_montgomery_reduce(a, b, c)   fp_montgomery_reduce(a, b, *((fp_digit*)c))
00093 #define mp_montgomery_free(a)        memset((a), 0, sizeof(fp_digit)) // fp_montgomery_deinit(a)
00094 
00095 #define mp_exptmod(a,b,c,d)          fp_exptmod(a,b,c,d)
00096 #define mp_prime_is_prime(a, b, c)   fp_isprime(a, c)
00097 
00098 #define mp_iszero(a)                 (mp_cmp_d(a, 0) == MP_EQ ? MP_YES : MP_NO)
00099 #define mp_isodd(a)                  (mp_get_digit_count(a) > 0 ? (mp_get_digit(a, 0) & 1 ? MP_YES : MP_NO) : MP_NO)
00100 #define mp_exch(a, b)                do { void *ABC__tmp = a; a = b; b = ABC__tmp; } while(0);
00101 
00102 #define mp_tohex(a, b)               mp_toradix(a, b, 16)
00103 
00104 #ifdef __cplusplus
00105 }
00106 #endif
00107 
00108 #endif /* CRYPTO_MATH_H_ */