Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * primegen.c |
michael@0 | 3 | * |
michael@0 | 4 | * Generates random integers which are prime with a high degree of |
michael@0 | 5 | * probability using the Miller-Rabin probabilistic primality testing |
michael@0 | 6 | * algorithm. |
michael@0 | 7 | * |
michael@0 | 8 | * Usage: |
michael@0 | 9 | * primegen <bits> [<num>] |
michael@0 | 10 | * |
michael@0 | 11 | * <bits> - number of significant bits each prime should have |
michael@0 | 12 | * <num> - number of primes to generate |
michael@0 | 13 | * |
michael@0 | 14 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 15 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 16 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 17 | |
michael@0 | 18 | #include <stdio.h> |
michael@0 | 19 | #include <stdlib.h> |
michael@0 | 20 | #include <string.h> |
michael@0 | 21 | #include <limits.h> |
michael@0 | 22 | #include <time.h> |
michael@0 | 23 | |
michael@0 | 24 | #include "mpi.h" |
michael@0 | 25 | #include "mplogic.h" |
michael@0 | 26 | #include "mpprime.h" |
michael@0 | 27 | |
michael@0 | 28 | #define NUM_TESTS 5 /* Number of Rabin-Miller iterations to test with */ |
michael@0 | 29 | |
michael@0 | 30 | #ifdef DEBUG |
michael@0 | 31 | #define FPUTC(x,y) fputc(x,y) |
michael@0 | 32 | #else |
michael@0 | 33 | #define FPUTC(x,y) |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | int main(int argc, char *argv[]) |
michael@0 | 37 | { |
michael@0 | 38 | unsigned char *raw; |
michael@0 | 39 | char *out; |
michael@0 | 40 | unsigned long nTries; |
michael@0 | 41 | int rawlen, bits, outlen, ngen, ix, jx; |
michael@0 | 42 | int g_strong = 0; |
michael@0 | 43 | mp_int testval; |
michael@0 | 44 | mp_err res; |
michael@0 | 45 | clock_t start, end; |
michael@0 | 46 | |
michael@0 | 47 | /* We'll just use the C library's rand() for now, although this |
michael@0 | 48 | won't be good enough for cryptographic purposes */ |
michael@0 | 49 | if((out = getenv("SEED")) == NULL) { |
michael@0 | 50 | srand((unsigned int)time(NULL)); |
michael@0 | 51 | } else { |
michael@0 | 52 | srand((unsigned int)atoi(out)); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | if(argc < 2) { |
michael@0 | 56 | fprintf(stderr, "Usage: %s <bits> [<count> [strong]]\n", argv[0]); |
michael@0 | 57 | return 1; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | if((bits = abs(atoi(argv[1]))) < CHAR_BIT) { |
michael@0 | 61 | fprintf(stderr, "%s: please request at least %d bits.\n", |
michael@0 | 62 | argv[0], CHAR_BIT); |
michael@0 | 63 | return 1; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | /* If optional third argument is given, use that as the number of |
michael@0 | 67 | primes to generate; otherwise generate one prime only. |
michael@0 | 68 | */ |
michael@0 | 69 | if(argc < 3) { |
michael@0 | 70 | ngen = 1; |
michael@0 | 71 | } else { |
michael@0 | 72 | ngen = abs(atoi(argv[2])); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | /* If fourth argument is given, and is the word "strong", we'll |
michael@0 | 76 | generate strong (Sophie Germain) primes. |
michael@0 | 77 | */ |
michael@0 | 78 | if(argc > 3 && strcmp(argv[3], "strong") == 0) |
michael@0 | 79 | g_strong = 1; |
michael@0 | 80 | |
michael@0 | 81 | /* testval - candidate being tested; nTries - number tried so far */ |
michael@0 | 82 | if ((res = mp_init(&testval)) != MP_OKAY) { |
michael@0 | 83 | fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res)); |
michael@0 | 84 | return 1; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | if(g_strong) { |
michael@0 | 88 | printf("Requested %d strong prime value(s) of %d bits.\n", |
michael@0 | 89 | ngen, bits); |
michael@0 | 90 | } else { |
michael@0 | 91 | printf("Requested %d prime value(s) of %d bits.\n", ngen, bits); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | rawlen = (bits / CHAR_BIT) + ((bits % CHAR_BIT) ? 1 : 0) + 1; |
michael@0 | 95 | |
michael@0 | 96 | if((raw = calloc(rawlen, sizeof(unsigned char))) == NULL) { |
michael@0 | 97 | fprintf(stderr, "%s: out of memory, sorry.\n", argv[0]); |
michael@0 | 98 | return 1; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /* This loop is one for each prime we need to generate */ |
michael@0 | 102 | for(jx = 0; jx < ngen; jx++) { |
michael@0 | 103 | |
michael@0 | 104 | raw[0] = 0; /* sign is positive */ |
michael@0 | 105 | |
michael@0 | 106 | /* Pack the initializer with random bytes */ |
michael@0 | 107 | for(ix = 1; ix < rawlen; ix++) |
michael@0 | 108 | raw[ix] = (rand() * rand()) & UCHAR_MAX; |
michael@0 | 109 | |
michael@0 | 110 | raw[1] |= 0x80; /* set high-order bit of test value */ |
michael@0 | 111 | raw[rawlen - 1] |= 1; /* set low-order bit of test value */ |
michael@0 | 112 | |
michael@0 | 113 | /* Make an mp_int out of the initializer */ |
michael@0 | 114 | mp_read_raw(&testval, (char *)raw, rawlen); |
michael@0 | 115 | |
michael@0 | 116 | /* Initialize candidate counter */ |
michael@0 | 117 | nTries = 0; |
michael@0 | 118 | |
michael@0 | 119 | start = clock(); /* time generation for this prime */ |
michael@0 | 120 | do { |
michael@0 | 121 | res = mpp_make_prime(&testval, bits, g_strong, &nTries); |
michael@0 | 122 | if (res != MP_NO) |
michael@0 | 123 | break; |
michael@0 | 124 | /* This code works whether digits are 16 or 32 bits */ |
michael@0 | 125 | res = mp_add_d(&testval, 32 * 1024, &testval); |
michael@0 | 126 | res = mp_add_d(&testval, 32 * 1024, &testval); |
michael@0 | 127 | FPUTC(',', stderr); |
michael@0 | 128 | } while (1); |
michael@0 | 129 | end = clock(); |
michael@0 | 130 | |
michael@0 | 131 | if (res != MP_YES) { |
michael@0 | 132 | break; |
michael@0 | 133 | } |
michael@0 | 134 | FPUTC('\n', stderr); |
michael@0 | 135 | puts("The following value is probably prime:"); |
michael@0 | 136 | outlen = mp_radix_size(&testval, 10); |
michael@0 | 137 | out = calloc(outlen, sizeof(unsigned char)); |
michael@0 | 138 | mp_toradix(&testval, (char *)out, 10); |
michael@0 | 139 | printf("10: %s\n", out); |
michael@0 | 140 | mp_toradix(&testval, (char *)out, 16); |
michael@0 | 141 | printf("16: %s\n\n", out); |
michael@0 | 142 | free(out); |
michael@0 | 143 | |
michael@0 | 144 | printf("Number of candidates tried: %lu\n", nTries); |
michael@0 | 145 | printf("This computation took %ld clock ticks (%.2f seconds)\n", |
michael@0 | 146 | (end - start), ((double)(end - start) / CLOCKS_PER_SEC)); |
michael@0 | 147 | |
michael@0 | 148 | FPUTC('\n', stderr); |
michael@0 | 149 | } /* end of loop to generate all requested primes */ |
michael@0 | 150 | |
michael@0 | 151 | if(res != MP_OKAY) |
michael@0 | 152 | fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res)); |
michael@0 | 153 | |
michael@0 | 154 | free(raw); |
michael@0 | 155 | mp_clear(&testval); |
michael@0 | 156 | |
michael@0 | 157 | return 0; |
michael@0 | 158 | } |