3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #ifdef CONFIG_INTERNAL_LIBTOMMATH
15 #include "libtommath.c"
16 #else /* CONFIG_INTERNAL_LIBTOMMATH */
18 #endif /* CONFIG_INTERNAL_LIBTOMMATH */
22 * The current version is just a wrapper for LibTomMath library, so
23 * struct bignum is just typecast to mp_int.
27 * bignum_init - Allocate memory for bignum
28 * Returns: Pointer to allocated bignum or %NULL on failure
30 struct bignum * bignum_init(void)
32 struct bignum *n = os_zalloc(sizeof(mp_int));
35 if (mp_init((mp_int *) n) != MP_OKAY) {
44 * bignum_deinit - Free bignum
45 * @n: Bignum from bignum_init()
47 void bignum_deinit(struct bignum *n)
50 mp_clear((mp_int *) n);
57 * bignum_get_unsigned_bin - Get length of bignum as an unsigned binary buffer
58 * @n: Bignum from bignum_init()
59 * Returns: Length of n if written to a binary buffer
61 size_t bignum_get_unsigned_bin_len(struct bignum *n)
63 return mp_unsigned_bin_size((mp_int *) n);
68 * bignum_get_unsigned_bin - Set binary buffer to unsigned bignum
69 * @n: Bignum from bignum_init()
70 * @buf: Buffer for the binary number
71 * @len: Length of the buffer, can be %NULL if buffer is known to be long
72 * enough. Set to used buffer length on success if not %NULL.
73 * Returns: 0 on success, -1 on failure
75 int bignum_get_unsigned_bin(const struct bignum *n, u8 *buf, size_t *len)
77 size_t need = mp_unsigned_bin_size((mp_int *) n);
78 if (len && need > *len) {
82 if (mp_to_unsigned_bin((mp_int *) n, buf) != MP_OKAY) {
83 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
93 * bignum_set_unsigned_bin - Set bignum based on unsigned binary buffer
94 * @n: Bignum from bignum_init(); to be set to the given value
95 * @buf: Buffer with unsigned binary value
96 * @len: Length of buf in octets
97 * Returns: 0 on success, -1 on failure
99 int bignum_set_unsigned_bin(struct bignum *n, const u8 *buf, size_t len)
101 if (mp_read_unsigned_bin((mp_int *) n, (u8 *) buf, len) != MP_OKAY) {
102 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
110 * bignum_cmp - Signed comparison
111 * @a: Bignum from bignum_init()
112 * @b: Bignum from bignum_init()
113 * Returns: 0 on success, -1 on failure
115 int bignum_cmp(const struct bignum *a, const struct bignum *b)
117 return mp_cmp((mp_int *) a, (mp_int *) b);
122 * bignum_cmd_d - Compare bignum to standard integer
123 * @a: Bignum from bignum_init()
125 * Returns: 0 on success, -1 on failure
127 int bignum_cmp_d(const struct bignum *a, unsigned long b)
129 return mp_cmp_d((mp_int *) a, b);
134 * bignum_add - c = a + b
135 * @a: Bignum from bignum_init()
136 * @b: Bignum from bignum_init()
137 * @c: Bignum from bignum_init(); used to store the result of a + b
138 * Returns: 0 on success, -1 on failure
140 int bignum_add(const struct bignum *a, const struct bignum *b,
143 if (mp_add((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
144 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
152 * bignum_sub - c = a - b
153 * @a: Bignum from bignum_init()
154 * @b: Bignum from bignum_init()
155 * @c: Bignum from bignum_init(); used to store the result of a - b
156 * Returns: 0 on success, -1 on failure
158 int bignum_sub(const struct bignum *a, const struct bignum *b,
161 if (mp_sub((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
162 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
170 * bignum_mul - c = a * b
171 * @a: Bignum from bignum_init()
172 * @b: Bignum from bignum_init()
173 * @c: Bignum from bignum_init(); used to store the result of a * b
174 * Returns: 0 on success, -1 on failure
176 int bignum_mul(const struct bignum *a, const struct bignum *b,
179 if (mp_mul((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
180 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
188 * bignum_mulmod - d = a * b (mod c)
189 * @a: Bignum from bignum_init()
190 * @b: Bignum from bignum_init()
191 * @c: Bignum from bignum_init(); modulus
192 * @d: Bignum from bignum_init(); used to store the result of a * b (mod c)
193 * Returns: 0 on success, -1 on failure
195 int bignum_mulmod(const struct bignum *a, const struct bignum *b,
196 const struct bignum *c, struct bignum *d)
198 if (mp_mulmod((mp_int *) a, (mp_int *) b, (mp_int *) c, (mp_int *) d)
200 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
208 * bignum_exptmod - Modular exponentiation: d = a^b (mod c)
209 * @a: Bignum from bignum_init(); base
210 * @b: Bignum from bignum_init(); exponent
211 * @c: Bignum from bignum_init(); modulus
212 * @d: Bignum from bignum_init(); used to store the result of a^b (mod c)
213 * Returns: 0 on success, -1 on failure
215 int bignum_exptmod(const struct bignum *a, const struct bignum *b,
216 const struct bignum *c, struct bignum *d)
218 if (mp_exptmod((mp_int *) a, (mp_int *) b, (mp_int *) c, (mp_int *) d)
220 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);