Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Crypto wrapper for internal crypto implementation - modexp |
| 3 | * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * Alternatively, this software may be distributed under the terms of BSD |
| 10 | * license. |
| 11 | * |
| 12 | * See README and COPYING for more details. |
| 13 | */ |
| 14 | |
| 15 | #include "includes.h" |
| 16 | |
| 17 | #include "common.h" |
| 18 | #include "tls/bignum.h" |
| 19 | #include "crypto.h" |
| 20 | |
| 21 | |
| 22 | int crypto_mod_exp(const u8 *base, size_t base_len, |
| 23 | const u8 *power, size_t power_len, |
| 24 | const u8 *modulus, size_t modulus_len, |
| 25 | u8 *result, size_t *result_len) |
| 26 | { |
| 27 | struct bignum *bn_base, *bn_exp, *bn_modulus, *bn_result; |
| 28 | int ret = -1; |
| 29 | |
| 30 | bn_base = bignum_init(); |
| 31 | bn_exp = bignum_init(); |
| 32 | bn_modulus = bignum_init(); |
| 33 | bn_result = bignum_init(); |
| 34 | |
| 35 | if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || |
| 36 | bn_result == NULL) |
| 37 | goto error; |
| 38 | |
| 39 | if (bignum_set_unsigned_bin(bn_base, base, base_len) < 0 || |
| 40 | bignum_set_unsigned_bin(bn_exp, power, power_len) < 0 || |
| 41 | bignum_set_unsigned_bin(bn_modulus, modulus, modulus_len) < 0) |
| 42 | goto error; |
| 43 | |
| 44 | if (bignum_exptmod(bn_base, bn_exp, bn_modulus, bn_result) < 0) |
| 45 | goto error; |
| 46 | |
| 47 | ret = bignum_get_unsigned_bin(bn_result, result, result_len); |
| 48 | |
| 49 | error: |
| 50 | bignum_deinit(bn_base); |
| 51 | bignum_deinit(bn_exp); |
| 52 | bignum_deinit(bn_modulus); |
| 53 | bignum_deinit(bn_result); |
| 54 | return ret; |
| 55 | } |