File: t_math.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 (387 lines) | stat: -rw-r--r-- 6,846 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
/*
 * 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 <sys/types.h>

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

/* Math library interface stubs */

BigInteger
BigIntegerFromInt(n)
     unsigned int n;
{
#ifdef CRYPTOLIB
  return bigInit(n);
#elif GNU_MP
  BigInteger rv;

  rv = (BigInteger) malloc(sizeof(MP_INT));
  mpz_init_set_ui(rv, n);
  return rv;
#endif
}

BigInteger
BigIntegerFromBytes(bytes, length)
     unsigned char * bytes;
     int length;
{
#ifdef CRYPTOLIB
  BigInteger rv, t;
  int i, n;

  rv = bigInit(0);
  if(length % 4 == 0)
    RSA_bufToBig(bytes, length, rv);
  else {	/* Wouldn't need this if cryptolib behaved better */
    i = length & 0x3;
    if(length > i)
      RSA_bufToBig(bytes + i, length - i, rv);
    for(n = 0; i > 0; --i)
      n = (n << 8) | *bytes++;
    t = bigInit(n);
    bigLeftShift(t, (length & ~0x3) << 3, t);
    bigAdd(rv, t, rv);
    freeBignum(t);
  }
  return rv;
#elif GNU_MP
  BigInteger rv;
  char hexbuf[MAXHEXPARAMLEN];
  char *p;

  rv = (BigInteger) malloc(sizeof(MP_INT));
  mpz_init_set_str(rv, t_tohex(hexbuf, bytes, length), 16);
  for(p = hexbuf; *p; ++p)
    *p = '\0';
  return rv;
#endif
}

int
BigIntegerToBytes(src, dest)
     BigInteger src;
     unsigned char * dest;
{
#ifdef CRYPTOLIB
  int i;

  trim(src);
  i = bigBytes(src);
  RSA_bigToBuf(src, i, dest);
  return i;
#elif GNU_MP
  char hexbuf[MAXHEXPARAMLEN];
  char * p;
  int r;

  mpz_get_str(hexbuf, 16, src);
  r = t_fromhex(dest, hexbuf);
  for(p = hexbuf; *p; ++p)
    *p = '\0';
  return r;
#endif
}

void
BigIntegerToHex(src, dest)
     BigInteger src;
     char * dest;
{
#ifdef CRYPTOLIB
  trim(src);
  bigsprint(src, dest);
#elif GNU_MP
  mpz_get_str(dest, 16, src);
#endif
}

int
BigIntegerBitLen(b)
     BigInteger b;
{
#ifdef CRYPTOLIB
  return bigBits(b);
#elif GNU_MP
  return mpz_sizeinbase(b, 2);
#endif
}

int
BigIntegerCmp(c1, c2)
     BigInteger c1, c2;
{
#ifdef CRYPTOLIB
  return bigCompare(c1, c2);
#elif GNU_MP
  return mpz_cmp(c1, c2);
#endif
}

int
BigIntegerCmpInt(c1, c2)
     BigInteger c1;
     unsigned int c2;
{
#ifdef CRYPTOLIB
  BigInteger t;
  int rv;

  t = bigInit(c2);
  rv = bigCompare(c1, t);
  freeBignum(t);
  return rv;
#elif GNU_MP
  return mpz_cmp_ui(c1, c2);
#endif
}

void
BigIntegerLShift(result, x, bits)
     BigInteger result, x;
     unsigned int bits;
{
#ifdef CRYPTOLIB
  bigLeftShift(x, bits, result);
#elif GNU_MP
  mpz_mul_2exp(result, x, bits);
#endif
}

void
BigIntegerAdd(result, a1, a2)
     BigInteger result, a1, a2;
{
#ifdef CRYPTOLIB
  bigAdd(a1, a2, result);
#elif GNU_MP
  mpz_add(result, a1, a2);
#endif
}

void
BigIntegerAddInt(result, a1, a2)
     BigInteger result, a1;
     unsigned int a2;
{
#ifdef CRYPTOLIB
  BigInteger t;

  t = bigInit(a2);
  bigAdd(a1, t, result);
  freeBignum(t);
#elif GNU_MP
  mpz_add_ui(result, a1, a2);
#endif
}

void
BigIntegerSub(result, s1, s2)
     BigInteger result, s1, s2;
{
#ifdef CRYPTOLIB
  bigSubtract(s1, s2, result);
#elif GNU_MP
  mpz_sub(result, s1, s2);
#endif
}

void
BigIntegerSubInt(result, s1, s2)
     BigInteger result, s1;
     unsigned int s2;
{
#ifdef CRYPTOLIB
  BigInteger t;

  t = bigInit(s2);
  bigSubtract(s1, t, result);
  freeBignum(t);
#elif GNU_MP
  mpz_sub_ui(result, s1, s2);
#endif
}

void
BigIntegerMul(result, m1, m2)
     BigInteger result, m1, m2;
{
#ifdef CRYPTOLIB
  bigMultiply(m1, m2, result);
#elif GNU_MP
  mpz_mul(result, m1, m2);
#endif
}

void
BigIntegerMulInt(result, m1, m2)
     BigInteger result, m1;
     unsigned int m2;
{
#ifdef CRYPTOLIB
  BigInteger t;

  t = bigInit(m2);
  bigMultiply(m1, t, result);
  freeBignum(t);
#elif GNU_MP
  mpz_mul_ui(result, m1, m2);
#endif
}

void
BigIntegerDivInt(result, d, m)
     BigInteger result, d;
     unsigned int m;
{
#ifdef CRYPTOLIB
  BigInteger t, u;

  t = bigInit(m);
  u = bigInit(0);
  bigDivide(d, t, result, u);
  freeBignum(t);
  freeBignum(u);
#elif GNU_MP
# ifdef GMP2
  mpz_fdiv_q_ui(result, d, m);
# else
  mpz_div_ui(result, d, m);
# endif
#endif
}

void
BigIntegerMod(result, d, m)
     BigInteger result, d, m;
{
#ifdef CRYPTOLIB
  bigMod(d, m, result);
#elif GNU_MP
  mpz_mod(result, d, m);
#endif
}

unsigned int
BigIntegerModInt(d, m)
     BigInteger d;
     unsigned int m;
{
#ifdef CRYPTOLIB
  BigInteger t, u;
  unsigned char r[4];

  t = bigInit(m);
  u = bigInit(0);
  bigMod(d, t, u);
  bigToBuf(u, sizeof(r), r);
  freeBignum(t);
  freeBignum(u);
  return r[0] | (r[1] << 8) | (r[2] << 16) | (r[3] << 24);
#elif GNU_MP
  MP_INT result;
  unsigned int i;

  mpz_init(&result);

/* Define GMP2 if you're using an old gmp.h but want to link against a
 * newer libgmp.a (e.g. 2.0 or later). */

# ifdef GMP2
  mpz_fdiv_r_ui(&result, d, m);
# else
  mpz_mod_ui(&result, d, m);
# endif
  i = mpz_get_ui(&result);
  mpz_clear(&result);
  return i;
#endif
}

void
BigIntegerModMul(r, m1, m2, modulus)
     BigInteger r, m1, m2, modulus;
{
#ifdef CRYPTOLIB
  bigMultiply(m1, m2, r);
  bigMod(r, modulus, r);
#elif GNU_MP
  mpz_mul(r, m1, m2);
  mpz_mod(r, r, modulus);
#endif
}

void
BigIntegerModExp(r, b, e, m)
     BigInteger r, b, e, m;
{
#ifdef CRYPTOLIB
  bigPow(b, e, m, r);
#elif GNU_MP
  mpz_powm(r, b, e, m);
#endif
}

void
BigIntegerModExpInt(r, b, e, m)
     BigInteger r, b;
     unsigned int e;
     BigInteger m;
{
#ifdef CRYPTOLIB
  BigInteger t;

  t = bigInit(e);
  bigPow(b, t, m, r);
  freeBignum(t);
#elif GNU_MP
  mpz_powm_ui(r, b, e, m);
#endif
}

int
BigIntegerCheckPrime(n)
     BigInteger n;
{
#ifdef CRYPTOLIB
  return primeTest(n);
#elif GNU_MP
  return mpz_probab_prime_p(n, 25);
#endif
}

void
BigIntegerFree(b)
     BigInteger b;
{
#ifdef CRYPTOLIB
  freeBignum(b);
#elif GNU_MP
  mpz_clear(b);
  free(b);
#endif
}