crypto_internal-modexp.c revision 252726
1214501Srpaulo/* 2214501Srpaulo * Crypto wrapper for internal crypto implementation - modexp 3214501Srpaulo * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> 4214501Srpaulo * 5252726Srpaulo * This software may be distributed under the terms of the BSD license. 6252726Srpaulo * See README for more details. 7214501Srpaulo */ 8214501Srpaulo 9214501Srpaulo#include "includes.h" 10214501Srpaulo 11214501Srpaulo#include "common.h" 12214501Srpaulo#include "tls/bignum.h" 13214501Srpaulo#include "crypto.h" 14214501Srpaulo 15214501Srpaulo 16214501Srpauloint crypto_mod_exp(const u8 *base, size_t base_len, 17214501Srpaulo const u8 *power, size_t power_len, 18214501Srpaulo const u8 *modulus, size_t modulus_len, 19214501Srpaulo u8 *result, size_t *result_len) 20214501Srpaulo{ 21214501Srpaulo struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result; 22214501Srpaulo int ret = -1; 23214501Srpaulo 24214501Srpaulo bn_base = bignum_init(); 25214501Srpaulo bn_exp = bignum_init(); 26214501Srpaulo bn_modulus = bignum_init(); 27214501Srpaulo bn_result = bignum_init(); 28214501Srpaulo 29214501Srpaulo if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || 30214501Srpaulo bn_result == NULL) 31214501Srpaulo goto error; 32214501Srpaulo 33214501Srpaulo if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 || 34214501Srpaulo bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 || 35214501Srpaulo bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0) 36214501Srpaulo goto error; 37214501Srpaulo 38214501Srpaulo if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0) 39214501Srpaulo goto error; 40214501Srpaulo 41214501Srpaulo ret = bignum_get_unsigned_bin(bn_result, result, result_len); 42214501Srpaulo 43214501Srpauloerror: 44214501Srpaulo bignum_deinit(bn_base); 45214501Srpaulo bignum_deinit(bn_exp); 46214501Srpaulo bignum_deinit(bn_modulus); 47214501Srpaulo bignum_deinit(bn_result); 48214501Srpaulo return ret; 49214501Srpaulo} 50