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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/mpi/utils/exptmod.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,50 @@
     1.4 +/*
     1.5 + *  exptmod.c
     1.6 + *
     1.7 + * Command line tool to perform modular exponentiation on arbitrary
     1.8 + * precision integers.
     1.9 + *
    1.10 + * This Source Code Form is subject to the terms of the Mozilla Public
    1.11 + * License, v. 2.0. If a copy of the MPL was not distributed with this
    1.12 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.13 +
    1.14 +#include <stdio.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <string.h>
    1.17 +
    1.18 +#include "mpi.h"
    1.19 +
    1.20 +int main(int argc, char *argv[])
    1.21 +{
    1.22 +  mp_int  a, b, m;
    1.23 +  mp_err  res;
    1.24 +  char   *str;
    1.25 +  int     len, rval = 0;
    1.26 +
    1.27 +  if(argc < 3) {
    1.28 +    fprintf(stderr, "Usage: %s <a> <b> <m>\n", argv[0]);
    1.29 +    return 1;
    1.30 +  }
    1.31 +
    1.32 +  mp_init(&a); mp_init(&b); mp_init(&m);
    1.33 +  mp_read_radix(&a, argv[1], 10);
    1.34 +  mp_read_radix(&b, argv[2], 10);
    1.35 +  mp_read_radix(&m, argv[3], 10);
    1.36 +
    1.37 +  if((res = mp_exptmod(&a, &b, &m, &a)) != MP_OKAY) {
    1.38 +    fprintf(stderr, "%s: error: %s\n", argv[0], mp_strerror(res));
    1.39 +    rval = 1;
    1.40 +  } else {
    1.41 +    len = mp_radix_size(&a, 10);
    1.42 +    str = calloc(len, sizeof(char));
    1.43 +    mp_toradix(&a, str, 10);
    1.44 +
    1.45 +    printf("%s\n", str);
    1.46 +
    1.47 +    free(str);
    1.48 +  }
    1.49 +
    1.50 +  mp_clear(&a); mp_clear(&b); mp_clear(&m);
    1.51 +
    1.52 +  return rval;
    1.53 +}

mercurial