source: trunk/src/sh_srp.c@ 137

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

Fix compile errors.

File size: 14.3 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];
339
340 SL_ENTER(_("sh_srp_make_a"));
341
342 for (i = 0; i < 6; ++i)
[137]343 randl[i] = (UINT32) taus_get ();
344
[1]345 (void) sl_strlcpy (hash,
346 sh_tiger_hash((char *)&randl[0], TIGER_DATA,
347 (unsigned long) 6*sizeof(UINT32)),
348 KEY_LEN+1);
349
350 hash[KEY_LEN] = '\0';
351
352 res = big_set_string (hash, 16, &sh_srp.a);
353 if (res == BIG_OK)
354 {
355 SL_RETURN((0), _("sh_srp_make_a"));
356 }
357 else
358 {
359 SL_RETURN((-1), _("sh_srp_make_a"));
360 }
361}
362
363/* return 0 if AB is NOT zero
364 */
365int sh_srp_check_zero (char * AB_str)
366{
367 bignum AB, q, r;
368 bigerr_t res;
369 int val;
370
371 SL_ENTER(_("sh_srp_check_zero"));
372
373 ASSERT_RET((AB_str != NULL), _("AB_str != NULL"), (-1));
374
375 res = big_create(&AB);
376 if (res == BIG_OK)
377 res = big_create(&q);
378 if (res == BIG_OK)
379 res = big_create(&r);
380
381 if (res == BIG_OK)
382 res = big_set_string (AB_str, 16, &AB);
383 if (res == BIG_OK)
384 res = big_trunc(&AB, &sh_srp.p, &q, &r); /* is last one the remainder ? */
385
386 if (res != BIG_OK) val = (-1);
387 else if (0 != big_zerop(&AB) ) val = (-1); /* 0 != (sign == 0) */
388 else val = 0;
389
390 big_destroy(&AB);
391 big_destroy(&q);
392 big_destroy(&r);
393
394 SL_RETURN((val), _("sh_srp_check_zero"));
395}
396
[27]397#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]398
399
400char * sh_srp_A ()
401{
402 bignum A;
403 char *str;
404 char *combi;
405 bigerr_t res;
406
407 SL_ENTER(_("sh_srp_A"));
408
409 res = big_create(&A);
410
411 if (res == BIG_OK)
412 res = big_exptmod (&sh_srp.g, &sh_srp.a, &sh_srp.p, &A);
413
414 if (res == BIG_OK)
415 str = big_string (&A, 16);
416 else
417 str = NULL;
418
419 if (str != NULL)
[34]420 combi = sh_util_strdup(str);
[1]421 else
422 combi = NULL;
423
424 big_destroy(&A);
425 SL_RETURN(combi, _("sh_srp_A"));
426}
427
428/* #ifdef SH_WITH_CLIENT */
429#endif
430
431#ifdef SH_WITH_SERVER
432
433char * sh_srp_B (char * verifier)
434{
435 bignum B, v, t, dummy;
436 char *str;
437 char *combi;
438 bigerr_t res;
439
440 SL_ENTER(_("sh_srp_B"));
441
442 ASSERT_RET((verifier != NULL), _("verifier != NULL"), (NULL));
443
444 res = big_create(&dummy);
445
446 if (res == BIG_OK)
447 res = big_create(&t);
448 if (res == BIG_OK)
449 res = big_create(&v);
450 if (res == BIG_OK)
451 res = big_create(&B);
452
453 if (res == BIG_OK)
454 res = big_exptmod (&sh_srp.g, &sh_srp.a, &sh_srp.p, &t);
455
456 if (res == BIG_OK)
457 big_set_string (verifier, 16, &v);
458
459 if (res == BIG_OK)
460 res = big_add (&t, &v, &dummy);
461
462 if (res == BIG_OK)
463 {
464 if ( big_greaterp(&dummy, &sh_srp.p) )
465 res = big_sub(&dummy, &sh_srp.p, &B);
466 else
467 res = big_set_big(&dummy, &B);
468 }
469
470 if (res == BIG_OK)
471 str = big_string (&B, 16);
472 else
473 str = NULL;
474
475 if (str != NULL)
[34]476 combi = sh_util_strdup(str);
[1]477 else
478 combi = NULL;
479
480 big_destroy(&B);
481 big_destroy(&v);
482 big_destroy(&t);
483 big_destroy(&dummy);
484
485 SL_RETURN(combi, _("sh_srp_B"));
486}
487/* #ifdef SH_WITH_SERVER */
488#endif
489
490
[27]491#if defined(SH_WITH_CLIENT) || defined(SH_WITH_SERVER)
[1]492
493char * sh_srp_S_c (char * u_str, char * B_str)
494{
495 bignum u, B, x, t, base, z1, z2;
496 char *str;
497 char *combi;
498 bigerr_t res;
499
500 SL_ENTER(_("sh_srp_S_c"));
501
502 ASSERT_RET((u_str != NULL && B_str != NULL),
503 _("u_str != NULL && B_str != NULL"), (NULL));
504
505 big_errno = BIG_OK;
506
507 res = big_create(&z2);
508 if (res == BIG_OK)
509 res = big_create(&z1);
510 if (res == BIG_OK)
511 res = big_create(&base);
512 if (res == BIG_OK)
513 res = big_create(&t);
514 if (res == BIG_OK)
515 res = big_create(&x);
516 if (res == BIG_OK)
517 res = big_create(&B);
518 if (res == BIG_OK)
519 res = big_create(&u);
520
521 if (res == BIG_OK)
522 res = big_set_string (B_str, 16, &B);
523 if (res == BIG_OK)
524 res = big_set_string (sh_srp.x, 16, &x);
525 if (res == BIG_OK)
526 res = big_set_string (u_str, 16, &u);
527
528 /* the base (B - g^x)
529 */
530 if (res == BIG_OK)
531 res = big_exptmod (&sh_srp.g, &x, &sh_srp.p, &t);
532
533 if (res == BIG_OK)
534 {
535 if ( big_greaterp(&B, &t) != 0)
536 {
537 res = big_sub(&B, &t, &base);
538 }
539 else
540 {
541 res = big_add(&B, &sh_srp.p, &z2);
542 if (res == BIG_OK)
543 res = big_sub(&z2, &t, &base);
544 }
545 }
546
547 /* the exponent (a + ux)
548 */
549 if (res == BIG_OK)
550 res = big_mul (&u, &x, &t);
551 if (res == BIG_OK)
552 res = big_trunc(&t, &sh_srp.p, &z1, &z2); /* is last one the remainder ? */
553 if (res == BIG_OK)
554 res = big_add(&sh_srp.a, &z2, &z1);
555 if (res == BIG_OK)
556 {
557 if ( big_greaterp(&z1, &sh_srp.p) != 0)
558 res = big_sub(&z1, &sh_srp.p, &z2);
559 else
560 res = big_set_big(&z1, &z2);
561 }
562
563 if (res == BIG_OK)
564 res = big_exptmod (&base, &z2, &sh_srp.p, &t);
565
566 if (res == BIG_OK)
567 str = big_string (&t, 16);
568 else
569 str = NULL;
570
571 if (str != NULL)
[34]572 combi = sh_util_strdup(str);
[1]573 else
574 combi = NULL;
575
576 big_destroy(&z1);
577 big_destroy(&z2);
578 big_destroy(&base);
579 big_destroy(&t);
580 big_destroy(&x);
581 big_destroy(&B);
582 big_destroy(&u);
583
584 SL_RETURN(combi, _("sh_srp_S_c"));
585}
586
587/* #ifdef SH_WITH_CLIENT */
588#endif
589
590#ifdef SH_WITH_SERVER
591
592
593char * sh_srp_S_s (char * u_str, char * A_str, char * v_str)
594{
595 bignum u, A, v, t, base, z1, z2;
596 char *str;
597 char *combi;
598 bigerr_t res;
599
600 SL_ENTER(_("sh_srp_S_s"));
601
602 ASSERT_RET((u_str != NULL && A_str != NULL && v_str != NULL),
603 _("u_str != NULL && A_str != NULL && v_str != NULL"),
604 (NULL));
605
606 big_errno = BIG_OK;
607
608 res = big_create(&z2);
609 if (res == BIG_OK)
610 res = big_create(&z1);
611 if (res == BIG_OK)
612 res = big_create(&base);
613 if (res == BIG_OK)
614 res = big_create(&t);
615 if (res == BIG_OK)
616 res = big_create(&v);
617 if (res == BIG_OK)
618 res = big_create(&A);
619 if (res == BIG_OK)
620 res = big_create(&u);
621
622 if (res == BIG_OK)
623 res = big_set_string (A_str, 16, &A);
624 if (res == BIG_OK)
625 res = big_set_string (v_str, 16, &v);
626 if (res == BIG_OK)
627 res = big_set_string (u_str, 16, &u);
628
629 /* the base (Av^u)
630 */
631 if (res == BIG_OK)
632 res = big_exptmod (&v, &u, &sh_srp.p, &t);
633 if (res == BIG_OK)
634 res = big_mul (&A, &t, &z1);
635 if (res == BIG_OK)
636 res = big_trunc(&z1, &sh_srp.p, &z2, &base); /* is last the remainder ? */
637
638 if (res == BIG_OK)
639 res = big_exptmod (&base, &sh_srp.a, &sh_srp.p, &t);
640
641 if (res == BIG_OK)
642 str = big_string (&t, 16);
643 else
644 str = NULL;
645
646 if (str != NULL)
[34]647 combi = sh_util_strdup(str);
[1]648 else
649 combi = NULL;
650
651 big_destroy(&z1);
652 big_destroy(&z2);
653 big_destroy(&base);
654 big_destroy(&t);
655 big_destroy(&v);
656 big_destroy(&A);
657 big_destroy(&u);
658
659 SL_RETURN(combi, _("sh_srp_S_s"));
660}
661
662/* #ifdef SH_WITH_SERVER */
663#endif
664
665
666char * sh_srp_verifier (void)
667{
668 bignum x, v;
669 char *combi;
670 char *str;
671 bigerr_t res;
672
673 SL_ENTER(_("sh_srp_verifier"));
674
675 res = big_create(&x);
676 if (res == BIG_OK)
677 res = big_create(&v);
678
679 if (res == BIG_OK)
680 res = big_set_string (sh_srp.x, 16, &x);
681
682 if (res == BIG_OK)
683 res = big_exptmod (&sh_srp.g, &x, &sh_srp.p, &v);
684
685 if (res == BIG_OK)
686 str = big_string (&v, 16);
687 else
688 str = NULL;
689
690 if (str != NULL)
[34]691 combi = sh_util_strdup(str);
[1]692 else
693 combi = NULL;
694
695 big_destroy(&x);
696 big_destroy(&v);
697
698 SL_RETURN(combi, _("sh_srp_verifier"));
699}
700
701
702/* #if (defined (SH_WITH_CLIENT) || defined (SH_WITH_SERVER)) */
703
704#endif
705
706/* #ifdef USE_SRP_PROTOCOL */
707
708#endif
709
710
711
712
Note: See TracBrowser for help on using the repository browser.