crypto_internal-modexp.c revision 214501
1214501Srpaulo/* 2214501Srpaulo * Crypto wrapper for internal crypto implementation - modexp 3214501Srpaulo * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> 4214501Srpaulo * 5214501Srpaulo * This program is free software; you can redistribute it and/or modify 6214501Srpaulo * it under the terms of the GNU General Public License version 2 as 7214501Srpaulo * published by the Free Software Foundation. 8214501Srpaulo * 9214501Srpaulo * Alternatively, this software may be distributed under the terms of BSD 10214501Srpaulo * license. 11214501Srpaulo * 12214501Srpaulo * See README and COPYING for more details. 13214501Srpaulo */ 14214501Srpaulo 15214501Srpaulo#include "includes.h" 16214501Srpaulo 17214501Srpaulo#include "common.h" 18214501Srpaulo#include "tls/bignum.h" 19214501Srpaulo#include "crypto.h" 20214501Srpaulo 21214501Srpaulo 22214501Srpauloint crypto_mod_exp(const u8 *base, size_t base_len, 23214501Srpaulo const u8 *power, size_t power_len, 24214501Srpaulo const u8 *modulus, size_t modulus_len, 25214501Srpaulo u8 *result, size_t *result_len) 26214501Srpaulo{ 27214501Srpaulo struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result; 28214501Srpaulo int ret = -1; 29214501Srpaulo 30214501Srpaulo bn_base = bignum_init(); 31214501Srpaulo bn_exp = bignum_init(); 32214501Srpaulo bn_modulus = bignum_init(); 33214501Srpaulo bn_result = bignum_init(); 34214501Srpaulo 35214501Srpaulo if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || 36214501Srpaulo bn_result == NULL) 37214501Srpaulo goto error; 38214501Srpaulo 39214501Srpaulo if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 || 40214501Srpaulo bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 || 41214501Srpaulo bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0) 42214501Srpaulo goto error; 43214501Srpaulo 44214501Srpaulo if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0) 45214501Srpaulo goto error; 46214501Srpaulo 47214501Srpaulo ret = bignum_get_unsigned_bin(bn_result, result, result_len); 48214501Srpaulo 49214501Srpauloerror: 50214501Srpaulo bignum_deinit(bn_base); 51214501Srpaulo bignum_deinit(bn_exp); 52214501Srpaulo bignum_deinit(bn_modulus); 53214501Srpaulo bignum_deinit(bn_result); 54214501Srpaulo return ret; 55214501Srpaulo} 56