|
@@ -0,0 +1,143 @@
|
|
1
|
+
|
|
2
|
+#include "randsource.h"
|
|
3
|
+#include "sgprime.h"
|
|
4
|
+
|
|
5
|
+#include <stdlib.h>
|
|
6
|
+#include <stdio.h>
|
|
7
|
+#include <errno.h>
|
|
8
|
+
|
|
9
|
+#define RAND_SIZE 512
|
|
10
|
+#define CHUNK_SIZE 1
|
|
11
|
+
|
|
12
|
+int main(int argc, char **argv)
|
|
13
|
+{
|
|
14
|
+ unsigned int threads;
|
|
15
|
+ unsigned int bits;
|
|
16
|
+ FILE *keyoutf;
|
|
17
|
+
|
|
18
|
+ if (argc < 3 || argc > 4)
|
|
19
|
+ {
|
|
20
|
+ printf("Usage: %s <bits> <output file> [threads]\n", argv[0]);
|
|
21
|
+ return 0;
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ if (argc == 3)
|
|
25
|
+ {
|
|
26
|
+ threads = 0;
|
|
27
|
+ } else
|
|
28
|
+ {
|
|
29
|
+ if (1 != sscanf(argv[3], "%u", &threads) || threads == 0)
|
|
30
|
+ {
|
|
31
|
+ printf("number of threads must be a positive integer\n");
|
|
32
|
+ return 0;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ if (1 != sscanf(argv[1], "%u", &bits) || bits == 0)
|
|
38
|
+ {
|
|
39
|
+ printf("number of bits must be a positive integer\n");
|
|
40
|
+ return 0;
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ keyoutf = fopen(argv[2], "w");
|
|
44
|
+ if (keyoutf == NULL)
|
|
45
|
+ {
|
|
46
|
+ perror("could not open key out file: ");
|
|
47
|
+ return 1;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ printf("Key Generation Tool\n");
|
|
51
|
+
|
|
52
|
+ printf("generating a key with %u bits ...\n", bits);
|
|
53
|
+
|
|
54
|
+ fflush(stdout);
|
|
55
|
+
|
|
56
|
+ mpz_t l, p, seed, rc, g, t;
|
|
57
|
+ mpz_init(p);
|
|
58
|
+ mpz_init(l);
|
|
59
|
+ mpz_init(g);
|
|
60
|
+ mpz_init(t);
|
|
61
|
+ mpz_init(seed);
|
|
62
|
+
|
|
63
|
+ mpz_set_ui(l, 0);
|
|
64
|
+ mpz_setbit(l, bits);
|
|
65
|
+
|
|
66
|
+ unsigned long int offset, primorial;
|
|
67
|
+ void *difflist = make_difflist(l, &offset, &primorial);
|
|
68
|
+
|
|
69
|
+ gmp_randstate_t rs;
|
|
70
|
+ gmp_randinit_default(rs);
|
|
71
|
+
|
|
72
|
+ char *randbuff = malloc(RAND_SIZE);
|
|
73
|
+ if (fill_random(randbuff, RAND_SIZE))
|
|
74
|
+ {
|
|
75
|
+ perror("unable to read randomness source: ");
|
|
76
|
+ return 2;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ mpz_import(seed, RAND_SIZE, 1, 1, 0, 0, randbuff);
|
|
80
|
+ gmp_randseed(rs, seed);
|
|
81
|
+
|
|
82
|
+ free(randbuff);
|
|
83
|
+
|
|
84
|
+ mpz_urandomb(p, rs, bits);
|
|
85
|
+ mpz_setbit(p, bits);
|
|
86
|
+
|
|
87
|
+ printf("generating safe prime ... ");
|
|
88
|
+ fflush(stdout);
|
|
89
|
+ get_sg_prime(difflist, offset, primorial, p, threads, bits + 4, CHUNK_SIZE);
|
|
90
|
+ printf("done\n");
|
|
91
|
+ fflush(stdout);
|
|
92
|
+
|
|
93
|
+ destroy_difflist(difflist);
|
|
94
|
+
|
|
95
|
+ mpz_mul_ui(l, p, 2);
|
|
96
|
+
|
|
97
|
+ rc[0] = seed[0]; // Moving variable names -- not portable
|
|
98
|
+ mpz_set(rc, l);
|
|
99
|
+
|
|
100
|
+ mpz_add_ui(l, l, 1);
|
|
101
|
+
|
|
102
|
+ printf("finding a generator ... ");
|
|
103
|
+ fflush(stdout);
|
|
104
|
+ while (1)
|
|
105
|
+ {
|
|
106
|
+ mpz_urandomm(g, rs, rc);
|
|
107
|
+ mpz_add_ui(rc, rc, 1);
|
|
108
|
+ mpz_powm_ui(t, g, 2, l);
|
|
109
|
+ if (mpz_cmp_ui(g, 1) != 0)
|
|
110
|
+ {
|
|
111
|
+ mpz_powm(t, g, p, l);
|
|
112
|
+ if (mpz_cmp_ui(g, 1) != 0)
|
|
113
|
+ break;
|
|
114
|
+ }
|
|
115
|
+ }
|
|
116
|
+ printf("done\n");
|
|
117
|
+ fflush(stdout);
|
|
118
|
+
|
|
119
|
+ printf("creating a secret exponent ... ");
|
|
120
|
+ fflush(stdout);
|
|
121
|
+ mpz_urandomm(p, rs, l);
|
|
122
|
+ printf("done\n");
|
|
123
|
+ fflush(stdout);
|
|
124
|
+
|
|
125
|
+ printf("computing public point ... ");
|
|
126
|
+ fflush(stdout);
|
|
127
|
+ mpz_powm(rc, g, p, l);
|
|
128
|
+ printf("done\n");
|
|
129
|
+ fflush(stdout);
|
|
130
|
+
|
|
131
|
+ gmp_randclear(rs);
|
|
132
|
+
|
|
133
|
+ gmp_fprintf(keyoutf, "m=%Zd\ng=%Zd\nx=%Zd\np=%Zd\n", l, g, p, rc);
|
|
134
|
+
|
|
135
|
+ mpz_clear(seed);
|
|
136
|
+ mpz_clear(p);
|
|
137
|
+ mpz_clear(l);
|
|
138
|
+ mpz_clear(g);
|
|
139
|
+ mpz_clear(t);
|
|
140
|
+
|
|
141
|
+ fclose(keyoutf);
|
|
142
|
+}
|
|
143
|
+
|