1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/mpi/utils/identest.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,83 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include <stdio.h> 1.9 +#include <stdlib.h> 1.10 +#include "mpi.h" 1.11 +#include "mpprime.h" 1.12 +#include <sys/types.h> 1.13 +#include <time.h> 1.14 + 1.15 +#define MAX_PREC (4096 / MP_DIGIT_BIT) 1.16 + 1.17 +mp_err identity_test(void) 1.18 +{ 1.19 + mp_size preca, precb; 1.20 + mp_err res; 1.21 + mp_int a, b; 1.22 + mp_int t1, t2, t3, t4, t5; 1.23 + 1.24 + preca = (rand() % MAX_PREC) + 1; 1.25 + precb = (rand() % MAX_PREC) + 1; 1.26 + 1.27 + MP_DIGITS(&a) = 0; 1.28 + MP_DIGITS(&b) = 0; 1.29 + MP_DIGITS(&t1) = 0; 1.30 + MP_DIGITS(&t2) = 0; 1.31 + MP_DIGITS(&t3) = 0; 1.32 + MP_DIGITS(&t4) = 0; 1.33 + MP_DIGITS(&t5) = 0; 1.34 + 1.35 + MP_CHECKOK( mp_init(&a) ); 1.36 + MP_CHECKOK( mp_init(&b) ); 1.37 + MP_CHECKOK( mp_init(&t1) ); 1.38 + MP_CHECKOK( mp_init(&t2) ); 1.39 + MP_CHECKOK( mp_init(&t3) ); 1.40 + MP_CHECKOK( mp_init(&t4) ); 1.41 + MP_CHECKOK( mp_init(&t5) ); 1.42 + 1.43 + MP_CHECKOK( mpp_random_size(&a, preca) ); 1.44 + MP_CHECKOK( mpp_random_size(&b, precb) ); 1.45 + 1.46 + if (mp_cmp(&a, &b) < 0) 1.47 + mp_exch(&a, &b); 1.48 + 1.49 + MP_CHECKOK( mp_mod(&a, &b, &t1) ); /* t1 = a%b */ 1.50 + MP_CHECKOK( mp_div(&a, &b, &t2, NULL) ); /* t2 = a/b */ 1.51 + MP_CHECKOK( mp_mul(&b, &t2, &t3) ); /* t3 = (a/b)*b */ 1.52 + MP_CHECKOK( mp_add(&t1, &t3, &t4) ); /* t4 = a%b + (a/b)*b */ 1.53 + MP_CHECKOK( mp_sub(&t4, &a, &t5) ); /* t5 = a%b + (a/b)*b - a */ 1.54 + if (mp_cmp_z(&t5) != 0) { 1.55 + res = MP_UNDEF; 1.56 + goto CLEANUP; 1.57 + } 1.58 + 1.59 +CLEANUP: 1.60 + mp_clear(&t5); 1.61 + mp_clear(&t4); 1.62 + mp_clear(&t3); 1.63 + mp_clear(&t2); 1.64 + mp_clear(&t1); 1.65 + mp_clear(&b); 1.66 + mp_clear(&a); 1.67 + return res; 1.68 +} 1.69 + 1.70 +int 1.71 +main(void) 1.72 +{ 1.73 + unsigned int seed = (unsigned int)time(NULL); 1.74 + unsigned long count = 0; 1.75 + mp_err res; 1.76 + 1.77 + srand(seed); 1.78 + 1.79 + while (MP_OKAY == (res = identity_test())) { 1.80 + if ((++count % 100) == 0) 1.81 + fputc('.', stderr); 1.82 + } 1.83 + 1.84 + fprintf(stderr, "\ntest failed, err %d\n", res); 1.85 + return res; 1.86 +}