change some parameters in the library to meet the needs of the website httpbin.org

Fork of MiniTLS-GPL by Donatien Garnier

Committer:
shiyilei
Date:
Fri Feb 06 06:17:33 2015 +0000
Revision:
5:95f70ebfe61f
Parent:
0:35aa5be3b78d
change some parameters in the library to meet the needs of httpbin.org

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MiniTLS 0:35aa5be3b78d 1 /* TomsFastMath, a fast ISO C bignum library.
MiniTLS 0:35aa5be3b78d 2 *
MiniTLS 0:35aa5be3b78d 3 * This project is meant to fill in where LibTomMath
MiniTLS 0:35aa5be3b78d 4 * falls short. That is speed ;-)
MiniTLS 0:35aa5be3b78d 5 *
MiniTLS 0:35aa5be3b78d 6 * This project is public domain and free for all purposes.
MiniTLS 0:35aa5be3b78d 7 *
MiniTLS 0:35aa5be3b78d 8 * Tom St Denis, tomstdenis@gmail.com
MiniTLS 0:35aa5be3b78d 9 */
MiniTLS 0:35aa5be3b78d 10 #include <tfm.h>
MiniTLS 0:35aa5be3b78d 11
MiniTLS 0:35aa5be3b78d 12 /* This is possibly the mother of all prime generation functions, muahahahahaha! */
MiniTLS 0:35aa5be3b78d 13 int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat)
MiniTLS 0:35aa5be3b78d 14 {
MiniTLS 0:35aa5be3b78d 15 unsigned maskAND, maskOR_msb, maskOR_lsb;
MiniTLS 0:35aa5be3b78d 16 int res, err, bsize, maskOR_msb_offset;
MiniTLS 0:35aa5be3b78d 17
MiniTLS 0:35aa5be3b78d 18 /* sanity check the input */
MiniTLS 0:35aa5be3b78d 19 if (size <= 1 || t <= 0) {
MiniTLS 0:35aa5be3b78d 20 return FP_VAL;
MiniTLS 0:35aa5be3b78d 21 }
MiniTLS 0:35aa5be3b78d 22
MiniTLS 0:35aa5be3b78d 23 /* TFM_PRIME_SAFE implies TFM_PRIME_BBS */
MiniTLS 0:35aa5be3b78d 24 if (flags & TFM_PRIME_SAFE) {
MiniTLS 0:35aa5be3b78d 25 flags |= TFM_PRIME_BBS;
MiniTLS 0:35aa5be3b78d 26 }
MiniTLS 0:35aa5be3b78d 27
MiniTLS 0:35aa5be3b78d 28 /* calc the byte size */
MiniTLS 0:35aa5be3b78d 29 bsize = (size>>3)+(size&7?1:0);
MiniTLS 0:35aa5be3b78d 30
MiniTLS 0:35aa5be3b78d 31 /* we need a buffer of bsize bytes */
MiniTLS 0:35aa5be3b78d 32 #if 0
MiniTLS 0:35aa5be3b78d 33 tmp = malloc(bsize);
MiniTLS 0:35aa5be3b78d 34 if (tmp == NULL) {
MiniTLS 0:35aa5be3b78d 35 return FP_MEM;
MiniTLS 0:35aa5be3b78d 36 }
MiniTLS 0:35aa5be3b78d 37 #endif
MiniTLS 0:35aa5be3b78d 38 //DG alloc on stack
MiniTLS 0:35aa5be3b78d 39 uint8_t tmp[bsize];
MiniTLS 0:35aa5be3b78d 40
MiniTLS 0:35aa5be3b78d 41 /* calc the maskAND value for the MSbyte*/
MiniTLS 0:35aa5be3b78d 42 maskAND = 0xFF >> (8 - (size & 7));
MiniTLS 0:35aa5be3b78d 43
MiniTLS 0:35aa5be3b78d 44 /* calc the maskOR_msb */
MiniTLS 0:35aa5be3b78d 45 maskOR_msb = 0;
MiniTLS 0:35aa5be3b78d 46 maskOR_msb_offset = (size - 2) >> 3;
MiniTLS 0:35aa5be3b78d 47 if (flags & TFM_PRIME_2MSB_ON) {
MiniTLS 0:35aa5be3b78d 48 maskOR_msb |= 1 << ((size - 2) & 7);
MiniTLS 0:35aa5be3b78d 49 } else if (flags & TFM_PRIME_2MSB_OFF) {
MiniTLS 0:35aa5be3b78d 50 maskAND &= ~(1 << ((size - 2) & 7));
MiniTLS 0:35aa5be3b78d 51 }
MiniTLS 0:35aa5be3b78d 52
MiniTLS 0:35aa5be3b78d 53 /* get the maskOR_lsb */
MiniTLS 0:35aa5be3b78d 54 maskOR_lsb = 1;
MiniTLS 0:35aa5be3b78d 55 if (flags & TFM_PRIME_BBS) {
MiniTLS 0:35aa5be3b78d 56 maskOR_lsb |= 3;
MiniTLS 0:35aa5be3b78d 57 }
MiniTLS 0:35aa5be3b78d 58
MiniTLS 0:35aa5be3b78d 59 do {
MiniTLS 0:35aa5be3b78d 60 /* read the bytes */
MiniTLS 0:35aa5be3b78d 61 if (cb(tmp, bsize, dat) != bsize) {
MiniTLS 0:35aa5be3b78d 62 err = FP_VAL;
MiniTLS 0:35aa5be3b78d 63 goto error;
MiniTLS 0:35aa5be3b78d 64 }
MiniTLS 0:35aa5be3b78d 65
MiniTLS 0:35aa5be3b78d 66 /* work over the MSbyte */
MiniTLS 0:35aa5be3b78d 67 tmp[0] &= maskAND;
MiniTLS 0:35aa5be3b78d 68 tmp[0] |= 1 << ((size - 1) & 7);
MiniTLS 0:35aa5be3b78d 69
MiniTLS 0:35aa5be3b78d 70 /* mix in the maskORs */
MiniTLS 0:35aa5be3b78d 71 tmp[maskOR_msb_offset] |= maskOR_msb;
MiniTLS 0:35aa5be3b78d 72 tmp[bsize-1] |= maskOR_lsb;
MiniTLS 0:35aa5be3b78d 73
MiniTLS 0:35aa5be3b78d 74 /* read it in */
MiniTLS 0:35aa5be3b78d 75 fp_read_unsigned_bin(a, tmp, bsize);
MiniTLS 0:35aa5be3b78d 76
MiniTLS 0:35aa5be3b78d 77 /* is it prime? */
MiniTLS 0:35aa5be3b78d 78 res = fp_isprime(a);
MiniTLS 0:35aa5be3b78d 79 if (res == FP_NO) continue;
MiniTLS 0:35aa5be3b78d 80
MiniTLS 0:35aa5be3b78d 81 if (flags & TFM_PRIME_SAFE) {
MiniTLS 0:35aa5be3b78d 82 /* see if (a-1)/2 is prime */
MiniTLS 0:35aa5be3b78d 83 fp_sub_d(a, 1, a);
MiniTLS 0:35aa5be3b78d 84 fp_div_2(a, a);
MiniTLS 0:35aa5be3b78d 85
MiniTLS 0:35aa5be3b78d 86 /* is it prime? */
MiniTLS 0:35aa5be3b78d 87 res = fp_isprime(a);
MiniTLS 0:35aa5be3b78d 88 }
MiniTLS 0:35aa5be3b78d 89 } while (res == FP_NO);
MiniTLS 0:35aa5be3b78d 90
MiniTLS 0:35aa5be3b78d 91 if (flags & TFM_PRIME_SAFE) {
MiniTLS 0:35aa5be3b78d 92 /* restore a to the original value */
MiniTLS 0:35aa5be3b78d 93 fp_mul_2(a, a);
MiniTLS 0:35aa5be3b78d 94 fp_add_d(a, 1, a);
MiniTLS 0:35aa5be3b78d 95 }
MiniTLS 0:35aa5be3b78d 96
MiniTLS 0:35aa5be3b78d 97 err = FP_OKAY;
MiniTLS 0:35aa5be3b78d 98 error:
MiniTLS 0:35aa5be3b78d 99 #if 0
MiniTLS 0:35aa5be3b78d 100 free(tmp);
MiniTLS 0:35aa5be3b78d 101 #endif
MiniTLS 0:35aa5be3b78d 102 return err;
MiniTLS 0:35aa5be3b78d 103 }
MiniTLS 0:35aa5be3b78d 104
MiniTLS 0:35aa5be3b78d 105 /* $Source: /cvs/libtom/tomsfastmath/src/numtheory/fp_prime_random_ex.c,v $ */
MiniTLS 0:35aa5be3b78d 106 /* $Revision: 1.1 $ */
MiniTLS 0:35aa5be3b78d 107 /* $Date: 2007/01/24 21:25:19 $ */