crypto_internal-modexp.c revision 214734
143902Sbrian/* 243902Sbrian * Crypto wrapper for internal crypto implementation - modexp 343948Sbrian * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> 443902Sbrian * 543948Sbrian * This program is free software; you can redistribute it and/or modify 643902Sbrian * it under the terms of the GNU General Public License version 2 as 750476Speter * published by the Free Software Foundation. 843902Sbrian * 943902Sbrian * Alternatively, this software may be distributed under the terms of BSD 1043902Sbrian * license. 1143902Sbrian * 1243902Sbrian * See README and COPYING for more details. 1376363Sbrian */ 1476363Sbrian 1576363Sbrian#include "includes.h" 1676363Sbrian 1776363Sbrian#include "common.h" 1843902Sbrian#include "tls/bignum.h" 1943902Sbrian#include "crypto.h" 2043902Sbrian 2143902Sbrian 2243902Sbrianint crypto_mod_exp(const u8 *base, size_t base_len, 2343902Sbrian const u8 *power, size_t power_len, 2443948Sbrian const u8 *modulus, size_t modulus_len, 2543902Sbrian u8 *result, size_t *result_len) 2679086Sbrian{ 2743948Sbrian struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result; 2843948Sbrian int ret = -1; 2943902Sbrian 3043902Sbrian bn_base = bignum_init(); 3143902Sbrian bn_exp = bignum_init(); 32244040Seadler bn_modulus = bignum_init(); 3343902Sbrian bn_result = bignum_init(); 3443948Sbrian 3543948Sbrian if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || 3643902Sbrian bn_result == NULL) 3743902Sbrian goto error; 3843902Sbrian 3943902Sbrian if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 || 4043948Sbrian bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 || 4143948Sbrian bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0) 4247634Sbillf goto error; 4343902Sbrian 4443902Sbrian if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0) 4543902Sbrian goto error; 4643948Sbrian 4743948Sbrian ret = bignum_get_unsigned_bin(bn_result, result, result_len); 4843948Sbrian 4943902Sbrianerror: 5043902Sbrian bignum_deinit(bn_base); 5143948Sbrian bignum_deinit(bn_exp); 5243948Sbrian bignum_deinit(bn_modulus); 5343902Sbrian bignum_deinit(bn_result); 5443902Sbrian return ret; 5543902Sbrian} 5643948Sbrian