File: t_conf.c

package info (click to toggle)
libsrp-dev 1.1-1
  • links: PTS
  • area: main
  • in suites: potato, slink
  • size: 316 kB
  • ctags: 269
  • sloc: ansic: 2,627; sh: 274; makefile: 53
file content (477 lines) | stat: -rw-r--r-- 12,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright (c) 1997 Stanford University
 *
 * The use of this software for revenue-generating purposes may require a
 * license from the owners of the underlying intellectual property.
 * Specifically, the SRP protocol may not be used for revenue-generating
 * purposes without license.
 *
 * Within that constraint, permission to use, copy, modify, and distribute
 * this software and its documentation for any purpose is hereby granted
 * without fee, provided that the above copyright notices and this permission
 * notice appear in all copies of the software and related documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>

#include "t_defines.h"
#include "t_pwd.h"
#include "t_read.h"

struct t_conf *
t_openconf(fp)
     FILE * fp;
{
  struct t_conf * tc;
  char close_flag = 0;

  if(fp == NULL) {	/* NULL means to open the system default file */
    if((fp = fopen(DEFAULT_CONF, "r")) == NULL)
      return NULL;
    close_flag = 1;
  }
  else
    close_flag = 0;	/* If it's a real fd, don't close it automatically */

  if((tc = malloc(sizeof(struct t_conf))) == NULL)
    return NULL;
  tc->instream = fp;
  tc->close_on_exit = close_flag;

  return tc;
}

struct t_conf *
t_openconfbyname(confname)
     t_const char * confname;
{
  FILE * fp;
  struct t_conf * t;

  if(confname == NULL)
    return t_openconf(NULL);

  if((fp = fopen(confname, "r")) == NULL)
    return NULL;

  t = t_openconf(fp);
  t->close_on_exit = 1;		/* Since we opened it, we should close it */
  return t;
}

void
t_closeconf(tc)
     struct t_conf * tc;
{
  if(tc->close_on_exit)
    fclose(tc->instream);
  free(tc);
}

void
t_rewindconf(tc)
     struct t_conf * tc;
{
  rewind(tc->instream);
}

struct t_confent *
t_getconfent(tc)
     struct t_conf * tc;
{
  char indexbuf[16];
  char b64buf[MAXB64PARAMLEN];

  while(1) {
    if(t_nextfield(tc->instream, indexbuf, 16) > 0 &&
       (tc->tcbuf.index = atoi(indexbuf)) > 0 &&
       t_nextfield(tc->instream, b64buf, MAXB64PARAMLEN) > 0 &&
       (tc->tcbuf.modulus.len = t_fromb64(tc->modbuf, b64buf)) > 0 &&
       t_nextfield(tc->instream, b64buf, MAXB64PARAMLEN) > 0 &&
       (tc->tcbuf.generator.len = t_fromb64(tc->genbuf, b64buf)) > 0) {
      tc->tcbuf.modulus.data = tc->modbuf;
      tc->tcbuf.generator.data = tc->genbuf;
      t_nextline(tc->instream);
      return &tc->tcbuf;
    }
    else if(t_nextline(tc->instream) < 0)
      return NULL;
  }
}

struct t_confent *
t_getconflast(tc)
     struct t_conf * tc;
{
  int valid = 0;

  t_rewindconf(tc);
  while(t_getconfent(tc) != NULL)
    valid = 1;
  if(valid)
    return &tc->tcbuf;
  else
    return NULL;
}

struct t_confent *
t_getconfbyindex(tc, index)
     struct t_conf * tc;
     int index;
{
  char indexbuf[16];
  char b64buf[MAXB64PARAMLEN];
  int tindex;

  while(t_nextfield(tc->instream, indexbuf, 16) > 0) {
    if((tindex = atoi(indexbuf)) == index)
      if(t_nextfield(tc->instream, b64buf, MAXB64PARAMLEN) > 0 &&
	 (tc->tcbuf.modulus.len = t_fromb64(tc->modbuf, b64buf)) > 0 &&
	 t_nextfield(tc->instream, b64buf, MAXB64PARAMLEN) > 0 &&
	 (tc->tcbuf.generator.len = t_fromb64(tc->genbuf, b64buf)) > 0) {
	tc->tcbuf.index = tindex;
	tc->tcbuf.modulus.data = tc->modbuf;
	tc->tcbuf.generator.data = tc->genbuf;
	t_nextline(tc->instream);
	return &tc->tcbuf;
      }
    if(t_nextline(tc->instream) < 0)
      return NULL;
  }
  return NULL;
}

/*
 * This is the safe prime generation logic.
 * To generate a safe prime p (where p = 2q+1 and q is prime), we start
 * with a random odd q that is one bit shorter than the desired length
 * of p.  We use a simple 30-element sieve to filter the values of q
 * and consider only those that are 11, 23, or 29 (mod 30).  (If q were
 * anything else, either q or p would be divisible by 2, 3, or 5).
 * For the values of q that are left, we apply the following tests in
 * this order:
 *
 *   trial divide q
 *   let p = 2q + 1
 *   trial divide p
 *   apply Fermat test to q (2^q == 2 (mod q))
 *   apply Fermat test to p (2^p == 2 (mod p))
 *   apply real probablistic primality test to q
 *   apply real probablistic primality test to p
 *
 * A number that passes all these tests is considered a safe prime for
 * our purposes.  The tests are ordered this way for efficiency; the
 * slower tests are run rarely if ever at all.
 */

static int
trialdiv(x)
     t_const BigInteger x;
{
  static int primes[] = {		/* All odd primes < 256 */
      3,   5,   7,  11,  13,  17,  19,  23,  29,
     31,  37,  41,  43,  47,  53,  59,  61,  67,
     71,  73,  79,  83,  89,  93,  97, 101, 103,
    107, 109, 113, 127, 131, 137, 139, 149, 151,
    157, 163, 167, 173, 179, 181, 191, 193, 197,
    199, 211, 223, 227, 229, 233, 239, 241, 251
  };
  static int nprimes = sizeof(primes) / sizeof(int);
  int i;

  for(i = 0; i < nprimes; ++i) {
    if(BigIntegerModInt(x, primes[i]) == 0)
      return primes[i];
  }
  return 1;
}

/* x + sieve30[x%30] == 11, 23, or 29 (mod 30) */

static int sieve30[] =
{  11, 10,  9,  8,  7,  6,  5,  4,  3,  2,
    1, 12, 11, 10,  9,  8,  7,  6,  5,  4,
    3,  2,  1,  6,  5,  4,  3,  2,  1, 12
};

/* Find a Sophie-Germain prime between "lo" and "hi".  NOTE: this is not
   a "safe prime", but the smaller prime.  Take 2q+1 to get the safe prime. */

static void
sophie_germain(q, lo, hi)
     BigInteger q;		/* assumed initialized */
     t_const BigInteger lo;
     t_const BigInteger hi;
{
  BigInteger m, p, r;
  char hexbuf[MAXHEXPARAMLEN];
  char parambuf[MAXPARAMLEN];
  int foundprime = 0;
  int i, mod30;

  m = BigIntegerFromInt(0);
  BigIntegerSub(m, hi, lo);
  i = (BigIntegerBitLen(m) + 7) / 8;
  t_random(parambuf, i);
  r = BigIntegerFromBytes(parambuf, i);
  BigIntegerMod(r, r, m);

  BigIntegerAdd(q, r, lo);
  if(BigIntegerModInt(q, 2) == 0)
    BigIntegerAddInt(q, q, 1);		/* make q odd */

  mod30 = BigIntegerModInt(q, 30);	/* mod30 = q % 30 */

  BigIntegerFree(m);
  m = BigIntegerFromInt(2);			/* m = 2 */
  p = BigIntegerFromInt(0);

  while(BigIntegerCmp(q, hi) < 0) {
    if(trialdiv(q) < 2) {
      BigIntegerMulInt(p, q, 2);			/* p = 2 * q */
      BigIntegerAddInt(p, p, 1);		/* p += 1 */
      if(trialdiv(p) < 2) {
	BigIntegerModExp(r, m, q, q);		/* r = 2^q % q */
	if(BigIntegerCmpInt(r, 2) == 0) {	/* if(r == 2) */
	  BigIntegerModExp(r, m, p, p);		/* r = 2^p % p */
	  if(BigIntegerCmpInt(r, 2) == 0) {	/* if(r == 2) */
	    if(BigIntegerCheckPrime(q) && BigIntegerCheckPrime(p)) {
	      ++foundprime;
	      break;
	    }
	  }
	}
      }
    }

    i = sieve30[mod30];
    BigIntegerAddInt(q, q, i);		/* q += i */
    mod30 = (mod30 + i) % 30;
  }

  /* should wrap around on failure */
  if(!foundprime) {
    fprintf(stderr, "Prime generation failed!\n");
    exit(1);
  }

  BigIntegerFree(r);
  BigIntegerFree(m);
  BigIntegerFree(p);
}

struct t_confent *
t_makeconfent(tc, nsize)
     struct t_conf * tc;
     int nsize;
{
  BigInteger n, g, q, t, u;
  char hexbuf[MAXHEXPARAMLEN];

  t = BigIntegerFromInt(0);
  u = BigIntegerFromInt(1);		/* u = 1 */
  BigIntegerLShift(t, u, nsize - 2);	/* t = 2^(nsize-2) */
  BigIntegerMulInt(u, t, 2);		/* u = 2^(nsize-1) */

  q = BigIntegerFromInt(0);
  sophie_germain(q, t, u);

  n = BigIntegerFromInt(0);
  BigIntegerMulInt(n, q, 2);
  BigIntegerAddInt(n, n, 1);

  /* Look for a generator mod n */
  g = BigIntegerFromInt(2);
  while(1) {
    BigIntegerModExp(t, g, q, n);		/* t = g^q % n */
    if(BigIntegerCmpInt(t, 1) == 0)		/* if(t == 1) */
      BigIntegerAddInt(g, g, 1);		/* ++g */
    else
      break;
  }
  BigIntegerFree(t);
  BigIntegerFree(u);
  BigIntegerFree(q);

  tc->tcbuf.modulus.data = tc->modbuf;
  tc->tcbuf.modulus.len = BigIntegerToBytes(n, tc->tcbuf.modulus.data);
  BigIntegerFree(n);

  tc->tcbuf.generator.data = tc->genbuf;
  tc->tcbuf.generator.len = BigIntegerToBytes(g, tc->tcbuf.generator.data);
  BigIntegerFree(g);
  
  tc->tcbuf.index = 1;
  return &tc->tcbuf;
}

struct t_confent *
t_makeconfent_c(tc, nsize)
     struct t_conf * tc;
     int nsize;
{
  BigInteger g, n, p, q, j, k, t, u;
  char hexbuf[MAXHEXPARAMLEN];
  int psize, qsize;

  psize = nsize / 2;
  qsize = nsize - psize;

  t = BigIntegerFromInt(1);		/* t = 1 */
  u = BigIntegerFromInt(0);
  BigIntegerLShift(u, t, psize - 3);	/* u = t*2^(psize-3) = 2^(psize-3) */
  BigIntegerMulInt(t, u, 3);			/* t = 3*u = 1.5*2^(psize-2) */
  BigIntegerAdd(u, u, t);			/* u += t [u = 2^(psize-1)] */
  j = BigIntegerFromInt(0);
  sophie_germain(j, t, u);

  k = BigIntegerFromInt(0);
  if(qsize != psize) {
    BigIntegerFree(t);
    t = BigIntegerFromInt(1);		/* t = 1 */
    BigIntegerLShift(u, t, qsize - 3);	/* u = t*2^(qsize-3) = 2^(qsize-3) */
    BigIntegerMulInt(t, u, 3);		/* t = 3*u = 1.5*2^(qsize-2) */
    BigIntegerAdd(u, u, t);		/* u += t [u = 2^(qsize-1)] */
  }
  sophie_germain(k, t, u);

  p = BigIntegerFromInt(0);
  BigIntegerMulInt(p, j, 2);		/* p = 2 * j */
  BigIntegerAddInt(p, p, 1);		/* p += 1 */

  q = BigIntegerFromInt(0);
  BigIntegerMulInt(q, k, 2);		/* q = 2 * k */
  BigIntegerAddInt(q, q, 1);		/* q += 1 */

  n = BigIntegerFromInt(0);
  BigIntegerMul(n, p, q);		/* n = p * q */
  BigIntegerMul(u, j, k);		/* u = j * k */

  BigIntegerFree(p);
  BigIntegerFree(q);
  BigIntegerFree(j);
  BigIntegerFree(k);

  g = BigIntegerFromInt(2);		/* g = 2 */

  /* Look for a generator mod n */
  while(1) {
    BigIntegerModExp(t, g, u, n);	/* t = g^u % n */
    if(BigIntegerCmpInt(t, 1) == 0)
      BigIntegerAddInt(g, g, 1);	/* ++g */
    else
      break;
  }

  BigIntegerFree(u);
  BigIntegerFree(t);

  tc->tcbuf.modulus.data = tc->modbuf;
  tc->tcbuf.modulus.len = BigIntegerToBytes(n, tc->tcbuf.modulus.data);
  BigIntegerFree(n);

  tc->tcbuf.generator.data = tc->genbuf;
  tc->tcbuf.generator.len = BigIntegerToBytes(g, tc->tcbuf.generator.data);
  BigIntegerFree(g);
  
  tc->tcbuf.index = 1;
  return &tc->tcbuf;
}

struct t_confent *
t_newconfent(tc)
    struct t_conf * tc;
{
  tc->tcbuf.index = 0;
  tc->tcbuf.modulus.data = tc->modbuf;
  tc->tcbuf.modulus.len = 0;
  tc->tcbuf.generator.data = tc->genbuf;
  tc->tcbuf.generator.len = 0;
  return &tc->tcbuf;
}

int
t_cmpconfent(cf1, cf2)
     t_const struct t_confent * cf1;
     t_const struct t_confent * cf2;
{
  int diff;

  diff = cf1->modulus.len - cf2->modulus.len;
  if(diff != 0)
    return diff;
  diff = cf1->generator.len - cf2->generator.len;
  if(diff != 0)
    return diff;
  diff = memcmp(cf1->modulus.data, cf2->modulus.data, cf1->modulus.len);
  if(diff != 0)
    return diff;
  return memcmp(cf1->generator.data, cf2->generator.data, cf1->generator.len);
}

void
t_putconfent(ent, fp)
     t_const struct t_confent * ent;
     FILE * fp;
{
  char strbuf[MAXB64PARAMLEN];

  fprintf(fp, "%d:%s:", ent->index,
	  t_tob64(strbuf, ent->modulus.data, ent->modulus.len));
  fprintf(fp, "%s\n",
	  t_tob64(strbuf, ent->generator.data, ent->generator.len));
}

static int
t_isprime(x)
     BigInteger x;
{
  BigInteger t;
  int iscomp;

  if(trialdiv(x) > 1)
    return 0;

  t = BigIntegerFromInt(2);
  BigIntegerModExp(t, t, x, x);
  iscomp = (BigIntegerCmpInt(t, 2) != 0);
  BigIntegerFree(t);
  if(iscomp)
    return 0;
  else
    return 1;
}

int
t_checkprime(num)
     t_const struct t_num * num;
{
  char hexbuf[MAXHEXPARAMLEN];
  BigInteger x, halfx;
  int retval;

  retval = NUM_SAFE;
  x = BigIntegerFromBytes(num->data, num->len);
  if(!t_isprime(x))
    retval = NUM_NOTPRIME;
  else {
    halfx = BigIntegerFromInt(0);
    BigIntegerSubInt(x, x, 1);
    BigIntegerDivInt(halfx, x, 2);
    if(!t_isprime(halfx))
      retval = NUM_NOTSAFE;
    BigIntegerFree(halfx);
  }
  BigIntegerFree(x);
  return retval;
}