source: trunk/src/sh_srp.c@ 148

Last change on this file since 148 was 138, checked in by rainer, 17 years ago

More fixes for compile and runtime errors.

File size: 14.4 KB
RevLine 
[1]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 1999, 2000 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26
27#include "samhain.h"
28
29#ifdef USE_SRP_PROTOCOL
30
31#if (defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER))
32
33#include "sh_tiger.h"
34#include "sh_mem.h"
35#include "sh_utils.h"
36#include "sh_srp.h"
37
38#if !defined(HAVE_LIBGMP) || !defined(HAVE_GMP_H)
39#include "bignum.h"
40#else
41
42#include <gmp.h>
43
44#define BIG_OK 0
45#define bigerr_t int
46int big_errno = BIG_OK;
47
48#define bignum MP_INT
49
50inline
51int big_create (bignum * a)
52{
53 mpz_init(a);
54 return 0;
55}
56
57inline
58int big_zerop (bignum * a)
59{
60 mpz_t b;
61 int i;
62 mpz_init_set_str(b, "0", 10);
63 i = mpz_cmp(a, b);
64 mpz_clear(b);
65 if (i)
66 return 0;
67 else
68 return 1;
69}
70
71inline
72int big_trunc (bignum * a, bignum * b, bignum * q, bignum *r)
73{
74 mpz_tdiv_qr(q, r, a, b);
75 return 0;
76}
77
78inline
79int big_exptmod (bignum * a, bignum * b, bignum * c, bignum *d)
80{
81 mpz_powm(d, a, b, c);
82 return 0;
83}
84
85char * get_str_internal = NULL;
86int siz_str_internal = 0;
87
88inline
89char * big_string (bignum * a, int base)
90{
91 char * str = NULL;
92 int size;
93 int i;
94 str = mpz_get_str (str, base, a);
95
96 if (get_str_internal == NULL)
97 {
98 get_str_internal = malloc(512); /* only once */
99 if (get_str_internal)
100 {
101 siz_str_internal = 512;
102 }
103 else
104 {
105 if (str != NULL)
106 free(str);
107 return 0;
108 }
109 get_str_internal[0] = '\0';
110 }
111
112 if (str != NULL)
113 {
114 size = strlen(str) + 1;
115 if (size > siz_str_internal)
116 get_str_internal = realloc (get_str_internal, size);
117 if (get_str_internal == NULL)
118 {
119 free(str);
120 return NULL;
121 }
122 siz_str_internal = size;
[22]123 sl_strlcpy (get_str_internal, str, siz_str_internal);
[1]124 for (i = 0; i < (size-1); ++i)
125 if (get_str_internal[i] >= 'a' && get_str_internal[i] <= 'f' )
126 get_str_internal[i] = get_str_internal[i] - 'a' + 'A';
127 free (str);
128 }
129 return get_str_internal;
130}
131
132inline
133int big_add(bignum * a, bignum * b, bignum * c)
134{
135 mpz_add(c, a, b);
136 return 0;
137}
138
139inline
140int big_sub(bignum * a, bignum * b, bignum * c)
141{
142 mpz_sub(c, a, b);
143 return 0;
144}
145
146inline
147int big_mul(bignum * a, bignum * b, bignum * c)
148{
149 mpz_mul(c, a, b);
150 return 0;
151}
152
153inline
154int big_greaterp(bignum * a, bignum * b)
155{
156 return mpz_cmp(a, b) > 0;
157}
158
159inline
160int big_set_big(bignum * a, bignum * b)
161{
162 mpz_set(b, a);
163 return 0;
164}
165
166
167inline
168int big_set_string(const char * str, int base, bignum * a)
169{
170 mpz_set_str (a, str, base);
171 return 0;
172}
173
174
175#define big_init_pkg() 0
176#define big_release_pkg()
177#define big_destroy mpz_clear
178
179/* #if defined(HAVE_LIBGMP)
180 */
181#endif
182
183#undef FIL__
184#define FIL__ _("sh_srp.c")
185
186typedef struct sh_srp_struc {
187 char x[KEY_LEN+1];
188 bignum a;
189 bignum p;
190 bignum g;
191} sh_srp_t;
192
193static sh_srp_t sh_srp;
194
195void sh_srp_x (char * salt, char * password)
196{
197
198 char *combi;
[34]199 size_t len, l2;
[1]200 register int i;
201 unsigned char * dez = NULL;
[133]202 char hashbuf[KEYBUF_SIZE];
[1]203
204 SL_ENTER(_("sh_srp_x"));
205
206 /* patch by Andreas Piesk
207 */
208 if (password == NULL)
209 dez = (unsigned char *) &(skey->pw[0]);
210 else
211 dez = (unsigned char *) password;
212
213 for (i = 0; i < PW_LEN; ++i)
214 {
215 skey->vernam[i] = (char)(*dez);
216 ++dez;
217 }
218 skey->vernam[PW_LEN] = '\0';
219
220 (void) sl_strlcpy (skey->vernam,
[133]221 sh_tiger_hash(skey->vernam, TIGER_DATA, PW_LEN,
222 hashbuf, sizeof(hashbuf)),
223 KEY_LEN);
[1]224 skey->vernam[KEY_LEN] = '\0';
225
[34]226 len = sl_strlen(salt) + 1;
227 l2 = sl_strlen(skey->vernam);
228 if (sl_ok_adds(len, l2))
229 len += l2;
[1]230
231 /* H(s,P)
232 */
233 combi = SH_ALLOC(len);
234 (void) sl_strlcpy (combi, salt, len);
235 (void) sl_strlcat (combi, skey->vernam, len);
236 (void) sl_strlcpy (sh_srp.x,
237 sh_tiger_hash(combi, TIGER_DATA,
[133]238 (unsigned long) sl_strlen(combi),
239 hashbuf, sizeof(hashbuf)),
[1]240 KEY_LEN+1);
241 SH_FREE (combi);
242
243 SL_RET0(_("sh_srp_x"));
244}
245
[133]246char * sh_srp_M (char * x1, char * x2, char * x3, char * hash, size_t size)
[1]247{
248 char *combi;
[34]249 size_t len, l2, l3;
[1]250
251 SL_ENTER(_("sh_srp_M"));
252
253 ASSERT_RET((x1 != NULL && x2 != NULL && x3 !=NULL),
254 _("x1 != NULL && x2 != NULL && x3 !=NULL"), NULL);
255
[34]256 len = sl_strlen(x1) + 1;
257 l2 = sl_strlen(x2);
258 l3 = sl_strlen(x3);
259
260 if (sl_ok_adds(len, l2))
261 len += l2;
262 if (sl_ok_adds(len, l3))
263 len += l3;
[1]264
265 /* H(x1,x2,x3)
266 */
267 combi = SH_ALLOC(len);
268 (void) sl_strlcpy (combi, x1, len);
269 (void) sl_strlcat (combi, x2, len);
270 (void) sl_strlcat (combi, x3, len);
[133]271 (void) sh_tiger_hash(combi, TIGER_DATA, (unsigned long) (len-1),
272 hash, size);
[1]273 SH_FREE (combi);
274
275 SL_RETURN(hash, _("sh_srp_M"));
276}
277
278
279void sh_srp_exit()
280{
281 SL_ENTER(_("sh_srp_exit"));
282 big_destroy(&sh_srp.g);
283 big_destroy(&sh_srp.p);
284 big_destroy(&sh_srp.a);
285
286 big_release_pkg();
287
288 big_errno = BIG_OK;
289 SL_RET0(_("sh_srp_exit"));
290}
291
292
293int sh_srp_init()
294{
295 bigerr_t res;
296 char modulus[80*4];
297
298 SL_ENTER(_("sh_srp_init"));
299
300 big_errno = BIG_OK;
301
302 res = big_init_pkg();
303
304 if (res == BIG_OK)
305 {
306 res = big_create(&sh_srp.p);
307 if (res == BIG_OK)
308 res = big_create(&sh_srp.g);
309 if (res == BIG_OK)
310 res = big_create(&sh_srp.a);
311 if (res == BIG_OK)
312 {
313 (void) sl_strlcpy(modulus, SRP_MODULUS_1024_1, sizeof(modulus));
314 (void) sl_strlcat(modulus, SRP_MODULUS_1024_2, sizeof(modulus));
315 (void) sl_strlcat(modulus, SRP_MODULUS_1024_3, sizeof(modulus));
316 (void) sl_strlcat(modulus, SRP_MODULUS_1024_4, sizeof(modulus));
317 }
318 if (res == BIG_OK)
319 res = big_set_string (modulus, 16, &sh_srp.p);
320 if (res == BIG_OK)
321 res = big_set_string (SRP_GENERATOR_1024, 16, &sh_srp.g);
322 if (res == BIG_OK)
323 {
324 SL_RETURN (0, _("sh_srp_init"));
325 }
326 else
327 sh_srp_exit();
328 }
329 SL_RETURN ((-1), _("sh_srp_init"));
330}
331
332
333int sh_srp_make_a ()
334{
335 UINT32 randl[6];
336 int i;
337 int res;
338 char hash[KEY_LEN+1];
[138]339 char hashbuf[KEYBUF_SIZE];
[1]340
341 SL_ENTER(_("sh_srp_make_a"));
342
343 for (i = 0; i < 6; ++i)
[137]344 randl[i] = (UINT32) taus_get ();
345
[1]346 (void) sl_strlcpy (hash,
347 sh_tiger_hash((char *)&randl[0], TIGER_DATA,
[138]348 (unsigned long) 6*sizeof(UINT32),
349 hashbuf, sizeof(hashbuf)),
[1]350 KEY_LEN+1);
351
352 hash[KEY_LEN] = '\0';
353
354 res = big_set_string (hash, 16, &sh_srp.a);
355 if (res == BIG_OK)
356 {
357 SL_RETURN((0), _("sh_srp_make_a"));
358 }
359 else
360 {
361 SL_RETURN((-1), _("sh_srp_make_a"));
362 }
363}
364
365/* return 0 if AB is NOT zero
366 */
367int sh_srp_check_zero (char * AB_str)
368{
369 bignum AB, q, r;
370 bigerr_t res;
371 int val;
372
373 SL_ENTER(_("sh_srp_check_zero"));
374
375 ASSERT_RET((AB_str != NULL), _("AB_str != NULL"), (-1));
376
377 res = big_create(&AB);
378 if (res == BIG_OK)
379 res = big_create(&q);
380 if (res == BIG_OK)
381 res = big_create(&r);
382
383 if (res == BIG_OK)
384 res = big_set_string (AB_str, 16, &AB);
385 if (res == BIG_OK)
386 res = big_trunc(&AB, &sh_srp.p, &q, &r); /* is last one the remainder ? */
387
388 if (res != BIG_OK) val = (-1);
389 else if (0 != big_zerop(&AB) ) val = (-1); /* 0 != (sign == 0) */
390 else val = 0;
391
392 big_destroy(&AB);
393 big_destroy(&q);
394 big_destroy(&r);
395
396 SL_RETURN((val), _("sh_srp_check_zero"));
397}
398
[27]399#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]400
401
402char * sh_srp_A ()
403{
404 bignum A;
405 char *str;
406 char *combi;
407 bigerr_t res;
408
409 SL_ENTER(_("sh_srp_A"));
410
411 res = big_create(&A);
412
413 if (res == BIG_OK)
414 res = big_exptmod (&sh_srp.g, &sh_srp.a, &sh_srp.p, &A);
415
416 if (res == BIG_OK)
417 str = big_string (&A, 16);
418 else
419 str = NULL;
420
421 if (str != NULL)
[34]422 combi = sh_util_strdup(str);
[1]423 else
424 combi = NULL;
425
426 big_destroy(&A);
427 SL_RETURN(combi, _("sh_srp_A"));
428}
429
430/* #ifdef SH_WITH_CLIENT */
431#endif
432
433#ifdef SH_WITH_SERVER
434
435char * sh_srp_B (char * verifier)
436{
437 bignum B, v, t, dummy;
438 char *str;
439 char *combi;
440 bigerr_t res;
441
442 SL_ENTER(_("sh_srp_B"));
443
444 ASSERT_RET((verifier != NULL), _("verifier != NULL"), (NULL));
445
446 res = big_create(&dummy);
447
448 if (res == BIG_OK)
449 res = big_create(&t);
450 if (res == BIG_OK)
451 res = big_create(&v);
452 if (res == BIG_OK)
453 res = big_create(&B);
454
455 if (res == BIG_OK)
456 res = big_exptmod (&sh_srp.g, &sh_srp.a, &sh_srp.p, &t);
457
458 if (res == BIG_OK)
459 big_set_string (verifier, 16, &v);
460
461 if (res == BIG_OK)
462 res = big_add (&t, &v, &dummy);
463
464 if (res == BIG_OK)
465 {
466 if ( big_greaterp(&dummy, &sh_srp.p) )
467 res = big_sub(&dummy, &sh_srp.p, &B);
468 else
469 res = big_set_big(&dummy, &B);
470 }
471
472 if (res == BIG_OK)
473 str = big_string (&B, 16);
474 else
475 str = NULL;
476
477 if (str != NULL)
[34]478 combi = sh_util_strdup(str);
[1]479 else
480 combi = NULL;
481
482 big_destroy(&B);
483 big_destroy(&v);
484 big_destroy(&t);
485 big_destroy(&dummy);
486
487 SL_RETURN(combi, _("sh_srp_B"));
488}
489/* #ifdef SH_WITH_SERVER */
490#endif
491
492
[27]493#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]494
495char * sh_srp_S_c (char * u_str, char * B_str)
496{
497 bignum u, B, x, t, base, z1, z2;
498 char *str;
499 char *combi;
500 bigerr_t res;
501
502 SL_ENTER(_("sh_srp_S_c"));
503
504 ASSERT_RET((u_str != NULL && B_str != NULL),
505 _("u_str != NULL && B_str != NULL"), (NULL));
506
507 big_errno = BIG_OK;
508
509 res = big_create(&z2);
510 if (res == BIG_OK)
511 res = big_create(&z1);
512 if (res == BIG_OK)
513 res = big_create(&base);
514 if (res == BIG_OK)
515 res = big_create(&t);
516 if (res == BIG_OK)
517 res = big_create(&x);
518 if (res == BIG_OK)
519 res = big_create(&B);
520 if (res == BIG_OK)
521 res = big_create(&u);
522
523 if (res == BIG_OK)
524 res = big_set_string (B_str, 16, &B);
525 if (res == BIG_OK)
526 res = big_set_string (sh_srp.x, 16, &x);
527 if (res == BIG_OK)
528 res = big_set_string (u_str, 16, &u);
529
530 /* the base (B - g^x)
531 */
532 if (res == BIG_OK)
533 res = big_exptmod (&sh_srp.g, &x, &sh_srp.p, &t);
534
535 if (res == BIG_OK)
536 {
537 if ( big_greaterp(&B, &t) != 0)
538 {
539 res = big_sub(&B, &t, &base);
540 }
541 else
542 {
543 res = big_add(&B, &sh_srp.p, &z2);
544 if (res == BIG_OK)
545 res = big_sub(&z2, &t, &base);
546 }
547 }
548
549 /* the exponent (a + ux)
550 */
551 if (res == BIG_OK)
552 res = big_mul (&u, &x, &t);
553 if (res == BIG_OK)
554 res = big_trunc(&t, &sh_srp.p, &z1, &z2); /* is last one the remainder ? */
555 if (res == BIG_OK)
556 res = big_add(&sh_srp.a, &z2, &z1);
557 if (res == BIG_OK)
558 {
559 if ( big_greaterp(&z1, &sh_srp.p) != 0)
560 res = big_sub(&z1, &sh_srp.p, &z2);
561 else
562 res = big_set_big(&z1, &z2);
563 }
564
565 if (res == BIG_OK)
566 res = big_exptmod (&base, &z2, &sh_srp.p, &t);
567
568 if (res == BIG_OK)
569 str = big_string (&t, 16);
570 else
571 str = NULL;
572
573 if (str != NULL)
[34]574 combi = sh_util_strdup(str);
[1]575 else
576 combi = NULL;
577
578 big_destroy(&z1);
579 big_destroy(&z2);
580 big_destroy(&base);
581 big_destroy(&t);
582 big_destroy(&x);
583 big_destroy(&B);
584 big_destroy(&u);
585
586 SL_RETURN(combi, _("sh_srp_S_c"));
587}
588
589/* #ifdef SH_WITH_CLIENT */
590#endif
591
592#ifdef SH_WITH_SERVER
593
594
595char * sh_srp_S_s (char * u_str, char * A_str, char * v_str)
596{
597 bignum u, A, v, t, base, z1, z2;
598 char *str;
599 char *combi;
600 bigerr_t res;
601
602 SL_ENTER(_("sh_srp_S_s"));
603
604 ASSERT_RET((u_str != NULL && A_str != NULL && v_str != NULL),
605 _("u_str != NULL && A_str != NULL && v_str != NULL"),
606 (NULL));
607
608 big_errno = BIG_OK;
609
610 res = big_create(&z2);
611 if (res == BIG_OK)
612 res = big_create(&z1);
613 if (res == BIG_OK)
614 res = big_create(&base);
615 if (res == BIG_OK)
616 res = big_create(&t);
617 if (res == BIG_OK)
618 res = big_create(&v);
619 if (res == BIG_OK)
620 res = big_create(&A);
621 if (res == BIG_OK)
622 res = big_create(&u);
623
624 if (res == BIG_OK)
625 res = big_set_string (A_str, 16, &A);
626 if (res == BIG_OK)
627 res = big_set_string (v_str, 16, &v);
628 if (res == BIG_OK)
629 res = big_set_string (u_str, 16, &u);
630
631 /* the base (Av^u)
632 */
633 if (res == BIG_OK)
634 res = big_exptmod (&v, &u, &sh_srp.p, &t);
635 if (res == BIG_OK)
636 res = big_mul (&A, &t, &z1);
637 if (res == BIG_OK)
638 res = big_trunc(&z1, &sh_srp.p, &z2, &base); /* is last the remainder ? */
639
640 if (res == BIG_OK)
641 res = big_exptmod (&base, &sh_srp.a, &sh_srp.p, &t);
642
643 if (res == BIG_OK)
644 str = big_string (&t, 16);
645 else
646 str = NULL;
647
648 if (str != NULL)
[34]649 combi = sh_util_strdup(str);
[1]650 else
651 combi = NULL;
652
653 big_destroy(&z1);
654 big_destroy(&z2);
655 big_destroy(&base);
656 big_destroy(&t);
657 big_destroy(&v);
658 big_destroy(&A);
659 big_destroy(&u);
660
661 SL_RETURN(combi, _("sh_srp_S_s"));
662}
663
664/* #ifdef SH_WITH_SERVER */
665#endif
666
667
668char * sh_srp_verifier (void)
669{
670 bignum x, v;
671 char *combi;
672 char *str;
673 bigerr_t res;
674
675 SL_ENTER(_("sh_srp_verifier"));
676
677 res = big_create(&x);
678 if (res == BIG_OK)
679 res = big_create(&v);
680
681 if (res == BIG_OK)
682 res = big_set_string (sh_srp.x, 16, &x);
683
684 if (res == BIG_OK)
685 res = big_exptmod (&sh_srp.g, &x, &sh_srp.p, &v);
686
687 if (res == BIG_OK)
688 str = big_string (&v, 16);
689 else
690 str = NULL;
691
692 if (str != NULL)
[34]693 combi = sh_util_strdup(str);
[1]694 else
695 combi = NULL;
696
697 big_destroy(&x);
698 big_destroy(&v);
699
700 SL_RETURN(combi, _("sh_srp_verifier"));
701}
702
703
704/* #if (defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER)) */
705
706#endif
707
708/* #ifdef USE_SRP_PROTOCOL */
709
710#endif
711
712
713
714
Note: See TracBrowser for help on using the repository browser.