Bläddra i källkod

Finished key generator

thajohns 11 månader sedan
förälder
incheckning
985ecf5093
4 ändrade filer med 173 tillägg och 2 borttagningar
  1. 143
    0
      keygen.c
  2. 20
    0
      randsource.c
  3. 8
    0
      randsource.h
  4. 2
    2
      sgprime.h

+ 143
- 0
keygen.c Visa fil

@@ -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
+

+ 20
- 0
randsource.c Visa fil

@@ -0,0 +1,20 @@
1
+// Change this file if your system uses a different randomness source.
2
+
3
+#include <stdio.h>
4
+#include <stdlib.h>
5
+#include <errno.h>
6
+
7
+int fill_random(void *buf, int size)
8
+{
9
+  FILE *random = fopen("/dev/urandom", "r");
10
+  
11
+  if (!random)
12
+  {
13
+    return errno;
14
+  }
15
+
16
+  fread(buf, size, 1, random);
17
+  fclose(random);
18
+  return 0;
19
+}
20
+

+ 8
- 0
randsource.h Visa fil

@@ -0,0 +1,8 @@
1
+
2
+#ifndef RANDSOURCE_H
3
+#define RANDSOURCE_H
4
+
5
+int fill_random(void *buf, int size);
6
+
7
+#endif
8
+

+ 2
- 2
sgprime.h Visa fil

@@ -3,8 +3,8 @@
3 3
 
4 4
 #include <gmp.h>
5 5
 
6
-void *make_difflist(mpz_t lower_bound, unsigned long int *offset);
7
-mpz_t get_sg_prime(void *difflist, unsigned long int offset, unsigned long int primorial, mpz_t start_search, unsigned int threads, unsigned int reps, unsigned int chunksize)
6
+void *make_difflist(mpz_t lower_bound, unsigned long int *offset, unsigned long int *primorial);
7
+void get_sg_prime(void *difflist, unsigned long int offset, unsigned long int primorial, mpz_t start_search, unsigned int threads, unsigned int reps, unsigned int chunksize);
8 8
 void destroy_difflist(void *difflist);
9 9
 
10 10
 

Laddar…
Avbryt
Spara