security/nss/lib/freebl/mpi/utils/identest.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include <stdio.h>
michael@0 6 #include <stdlib.h>
michael@0 7 #include "mpi.h"
michael@0 8 #include "mpprime.h"
michael@0 9 #include <sys/types.h>
michael@0 10 #include <time.h>
michael@0 11
michael@0 12 #define MAX_PREC (4096 / MP_DIGIT_BIT)
michael@0 13
michael@0 14 mp_err identity_test(void)
michael@0 15 {
michael@0 16 mp_size preca, precb;
michael@0 17 mp_err res;
michael@0 18 mp_int a, b;
michael@0 19 mp_int t1, t2, t3, t4, t5;
michael@0 20
michael@0 21 preca = (rand() % MAX_PREC) + 1;
michael@0 22 precb = (rand() % MAX_PREC) + 1;
michael@0 23
michael@0 24 MP_DIGITS(&a) = 0;
michael@0 25 MP_DIGITS(&b) = 0;
michael@0 26 MP_DIGITS(&t1) = 0;
michael@0 27 MP_DIGITS(&t2) = 0;
michael@0 28 MP_DIGITS(&t3) = 0;
michael@0 29 MP_DIGITS(&t4) = 0;
michael@0 30 MP_DIGITS(&t5) = 0;
michael@0 31
michael@0 32 MP_CHECKOK( mp_init(&a) );
michael@0 33 MP_CHECKOK( mp_init(&b) );
michael@0 34 MP_CHECKOK( mp_init(&t1) );
michael@0 35 MP_CHECKOK( mp_init(&t2) );
michael@0 36 MP_CHECKOK( mp_init(&t3) );
michael@0 37 MP_CHECKOK( mp_init(&t4) );
michael@0 38 MP_CHECKOK( mp_init(&t5) );
michael@0 39
michael@0 40 MP_CHECKOK( mpp_random_size(&a, preca) );
michael@0 41 MP_CHECKOK( mpp_random_size(&b, precb) );
michael@0 42
michael@0 43 if (mp_cmp(&a, &b) < 0)
michael@0 44 mp_exch(&a, &b);
michael@0 45
michael@0 46 MP_CHECKOK( mp_mod(&a, &b, &t1) ); /* t1 = a%b */
michael@0 47 MP_CHECKOK( mp_div(&a, &b, &t2, NULL) ); /* t2 = a/b */
michael@0 48 MP_CHECKOK( mp_mul(&b, &t2, &t3) ); /* t3 = (a/b)*b */
michael@0 49 MP_CHECKOK( mp_add(&t1, &t3, &t4) ); /* t4 = a%b + (a/b)*b */
michael@0 50 MP_CHECKOK( mp_sub(&t4, &a, &t5) ); /* t5 = a%b + (a/b)*b - a */
michael@0 51 if (mp_cmp_z(&t5) != 0) {
michael@0 52 res = MP_UNDEF;
michael@0 53 goto CLEANUP;
michael@0 54 }
michael@0 55
michael@0 56 CLEANUP:
michael@0 57 mp_clear(&t5);
michael@0 58 mp_clear(&t4);
michael@0 59 mp_clear(&t3);
michael@0 60 mp_clear(&t2);
michael@0 61 mp_clear(&t1);
michael@0 62 mp_clear(&b);
michael@0 63 mp_clear(&a);
michael@0 64 return res;
michael@0 65 }
michael@0 66
michael@0 67 int
michael@0 68 main(void)
michael@0 69 {
michael@0 70 unsigned int seed = (unsigned int)time(NULL);
michael@0 71 unsigned long count = 0;
michael@0 72 mp_err res;
michael@0 73
michael@0 74 srand(seed);
michael@0 75
michael@0 76 while (MP_OKAY == (res = identity_test())) {
michael@0 77 if ((++count % 100) == 0)
michael@0 78 fputc('.', stderr);
michael@0 79 }
michael@0 80
michael@0 81 fprintf(stderr, "\ntest failed, err %d\n", res);
michael@0 82 return res;
michael@0 83 }

mercurial