1/*
2 * Crypto wrapper for internal crypto implementation - modexp
3 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "tls/bignum.h"
13#include "crypto.h"
14
15
16int crypto_mod_exp(const u8 *base, size_t base_len,
17		   const u8 *power, size_t power_len,
18		   const u8 *modulus, size_t modulus_len,
19		   u8 *result, size_t *result_len)
20{
21	struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result;
22	int ret = -1;
23
24	bn_base = bignum_init();
25	bn_exp = bignum_init();
26	bn_modulus = bignum_init();
27	bn_result = bignum_init();
28
29	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
30	    bn_result == NULL)
31		goto error;
32
33	if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 ||
34	    bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 ||
35	    bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0)
36		goto error;
37
38	if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0)
39		goto error;
40
41	ret = bignum_get_unsigned_bin(bn_result, result, result_len);
42
43error:
44	bignum_deinit(bn_base);
45	bignum_deinit(bn_exp);
46	bignum_deinit(bn_modulus);
47	bignum_deinit(bn_result);
48	return ret;
49}
50