wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue May 02 08:44:26 2017 +0000
Revision:
6:fa3bd0ca5896
wolfSSL3.10.2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 6:fa3bd0ca5896 1 /* wolfmath.c
wolfSSL 6:fa3bd0ca5896 2 *
wolfSSL 6:fa3bd0ca5896 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 6:fa3bd0ca5896 4 *
wolfSSL 6:fa3bd0ca5896 5 * This file is part of wolfSSL.
wolfSSL 6:fa3bd0ca5896 6 *
wolfSSL 6:fa3bd0ca5896 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 6:fa3bd0ca5896 8 * it under the terms of the GNU General Public License as published by
wolfSSL 6:fa3bd0ca5896 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 6:fa3bd0ca5896 10 * (at your option) any later version.
wolfSSL 6:fa3bd0ca5896 11 *
wolfSSL 6:fa3bd0ca5896 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 6:fa3bd0ca5896 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 6:fa3bd0ca5896 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 6:fa3bd0ca5896 15 * GNU General Public License for more details.
wolfSSL 6:fa3bd0ca5896 16 *
wolfSSL 6:fa3bd0ca5896 17 * You should have received a copy of the GNU General Public License
wolfSSL 6:fa3bd0ca5896 18 * along with this program; if not, write to the Free Software
wolfSSL 6:fa3bd0ca5896 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 6:fa3bd0ca5896 20 */
wolfSSL 6:fa3bd0ca5896 21
wolfSSL 6:fa3bd0ca5896 22
wolfSSL 6:fa3bd0ca5896 23 /* common functions for either math library */
wolfSSL 6:fa3bd0ca5896 24
wolfSSL 6:fa3bd0ca5896 25 #ifdef HAVE_CONFIG_H
wolfSSL 6:fa3bd0ca5896 26 #include <config.h>
wolfSSL 6:fa3bd0ca5896 27 #endif
wolfSSL 6:fa3bd0ca5896 28
wolfSSL 6:fa3bd0ca5896 29 /* in case user set USE_FAST_MATH there */
wolfSSL 6:fa3bd0ca5896 30 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 6:fa3bd0ca5896 31
wolfSSL 6:fa3bd0ca5896 32 #ifdef USE_FAST_MATH
wolfSSL 6:fa3bd0ca5896 33 #include <wolfssl/wolfcrypt/tfm.h>
wolfSSL 6:fa3bd0ca5896 34 #else
wolfSSL 6:fa3bd0ca5896 35 #include <wolfssl/wolfcrypt/integer.h>
wolfSSL 6:fa3bd0ca5896 36 #endif
wolfSSL 6:fa3bd0ca5896 37
wolfSSL 6:fa3bd0ca5896 38 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 6:fa3bd0ca5896 39 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 6:fa3bd0ca5896 40
wolfSSL 6:fa3bd0ca5896 41 #if defined(USE_FAST_MATH) || !defined(NO_BIG_INT)
wolfSSL 6:fa3bd0ca5896 42
wolfSSL 6:fa3bd0ca5896 43 int get_digit_count(mp_int* a)
wolfSSL 6:fa3bd0ca5896 44 {
wolfSSL 6:fa3bd0ca5896 45 if (a == NULL)
wolfSSL 6:fa3bd0ca5896 46 return 0;
wolfSSL 6:fa3bd0ca5896 47
wolfSSL 6:fa3bd0ca5896 48 return a->used;
wolfSSL 6:fa3bd0ca5896 49 }
wolfSSL 6:fa3bd0ca5896 50
wolfSSL 6:fa3bd0ca5896 51 mp_digit get_digit(mp_int* a, int n)
wolfSSL 6:fa3bd0ca5896 52 {
wolfSSL 6:fa3bd0ca5896 53 if (a == NULL)
wolfSSL 6:fa3bd0ca5896 54 return 0;
wolfSSL 6:fa3bd0ca5896 55
wolfSSL 6:fa3bd0ca5896 56 return (n >= a->used || n < 0) ? 0 : a->dp[n];
wolfSSL 6:fa3bd0ca5896 57 }
wolfSSL 6:fa3bd0ca5896 58
wolfSSL 6:fa3bd0ca5896 59 int get_rand_digit(WC_RNG* rng, mp_digit* d)
wolfSSL 6:fa3bd0ca5896 60 {
wolfSSL 6:fa3bd0ca5896 61 return wc_RNG_GenerateBlock(rng, (byte*)d, sizeof(mp_digit));
wolfSSL 6:fa3bd0ca5896 62 }
wolfSSL 6:fa3bd0ca5896 63
wolfSSL 6:fa3bd0ca5896 64 int mp_rand(mp_int* a, int digits, WC_RNG* rng)
wolfSSL 6:fa3bd0ca5896 65 {
wolfSSL 6:fa3bd0ca5896 66 int ret;
wolfSSL 6:fa3bd0ca5896 67 mp_digit d;
wolfSSL 6:fa3bd0ca5896 68
wolfSSL 6:fa3bd0ca5896 69 if (rng == NULL)
wolfSSL 6:fa3bd0ca5896 70 return MISSING_RNG_E;
wolfSSL 6:fa3bd0ca5896 71
wolfSSL 6:fa3bd0ca5896 72 if (a == NULL)
wolfSSL 6:fa3bd0ca5896 73 return BAD_FUNC_ARG;
wolfSSL 6:fa3bd0ca5896 74
wolfSSL 6:fa3bd0ca5896 75 mp_zero(a);
wolfSSL 6:fa3bd0ca5896 76 if (digits <= 0) {
wolfSSL 6:fa3bd0ca5896 77 return MP_OKAY;
wolfSSL 6:fa3bd0ca5896 78 }
wolfSSL 6:fa3bd0ca5896 79
wolfSSL 6:fa3bd0ca5896 80 /* first place a random non-zero digit */
wolfSSL 6:fa3bd0ca5896 81 do {
wolfSSL 6:fa3bd0ca5896 82 ret = get_rand_digit(rng, &d);
wolfSSL 6:fa3bd0ca5896 83 if (ret != 0) {
wolfSSL 6:fa3bd0ca5896 84 return ret;
wolfSSL 6:fa3bd0ca5896 85 }
wolfSSL 6:fa3bd0ca5896 86 } while (d == 0);
wolfSSL 6:fa3bd0ca5896 87
wolfSSL 6:fa3bd0ca5896 88 if ((ret = mp_add_d(a, d, a)) != MP_OKAY) {
wolfSSL 6:fa3bd0ca5896 89 return ret;
wolfSSL 6:fa3bd0ca5896 90 }
wolfSSL 6:fa3bd0ca5896 91
wolfSSL 6:fa3bd0ca5896 92 while (--digits > 0) {
wolfSSL 6:fa3bd0ca5896 93 if ((ret = mp_lshd(a, 1)) != MP_OKAY) {
wolfSSL 6:fa3bd0ca5896 94 return ret;
wolfSSL 6:fa3bd0ca5896 95 }
wolfSSL 6:fa3bd0ca5896 96 if ((ret = get_rand_digit(rng, &d)) != 0) {
wolfSSL 6:fa3bd0ca5896 97 return ret;
wolfSSL 6:fa3bd0ca5896 98 }
wolfSSL 6:fa3bd0ca5896 99 if ((ret = mp_add_d(a, d, a)) != MP_OKAY) {
wolfSSL 6:fa3bd0ca5896 100 return ret;
wolfSSL 6:fa3bd0ca5896 101 }
wolfSSL 6:fa3bd0ca5896 102 }
wolfSSL 6:fa3bd0ca5896 103
wolfSSL 6:fa3bd0ca5896 104 return ret;
wolfSSL 6:fa3bd0ca5896 105 }
wolfSSL 6:fa3bd0ca5896 106
wolfSSL 6:fa3bd0ca5896 107 #endif
wolfSSL 6:fa3bd0ca5896 108