3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #ifdef CONFIG_INTERNAL_LIBTOMMATH
21 #include "libtommath.c"
22 #else /* CONFIG_INTERNAL_LIBTOMMATH */
24 #endif /* CONFIG_INTERNAL_LIBTOMMATH */
28 * The current version is just a wrapper for LibTomMath library, so
29 * struct bignum is just typecast to mp_int.
33 * bignum_init - Allocate memory for bignum
34 * Returns: Pointer to allocated bignum or %NULL on failure
36 struct bignum
* bignum_init(void)
38 struct bignum
*n
= os_zalloc(sizeof(mp_int
));
41 if (mp_init((mp_int
*) n
) != MP_OKAY
) {
50 * bignum_deinit - Free bignum
51 * @n: Bignum from bignum_init()
53 void bignum_deinit(struct bignum
*n
)
56 mp_clear((mp_int
*) n
);
63 * bignum_get_unsigned_bin - Get length of bignum as an unsigned binary buffer
64 * @n: Bignum from bignum_init()
65 * Returns: Length of n if written to a binary buffer
67 size_t bignum_get_unsigned_bin_len(struct bignum
*n
)
69 return mp_unsigned_bin_size((mp_int
*) n
);
74 * bignum_get_unsigned_bin - Set binary buffer to unsigned bignum
75 * @n: Bignum from bignum_init()
76 * @buf: Buffer for the binary number
77 * @len: Length of the buffer, can be %NULL if buffer is known to be long
78 * enough. Set to used buffer length on success if not %NULL.
79 * Returns: 0 on success, -1 on failure
81 int bignum_get_unsigned_bin(const struct bignum
*n
, u8
*buf
, size_t *len
)
83 size_t need
= mp_unsigned_bin_size((mp_int
*) n
);
84 if (len
&& need
> *len
) {
88 if (mp_to_unsigned_bin((mp_int
*) n
, buf
) != MP_OKAY
) {
89 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);
99 * bignum_set_unsigned_bin - Set bignum based on unsigned binary buffer
100 * @a: Bignum from bignum_init(); to be set to the given value
101 * @buf: Buffer with unsigned binary value
102 * @len: Length of buf in octets
103 * Returns: 0 on success, -1 on failure
105 int bignum_set_unsigned_bin(struct bignum
*n
, const u8
*buf
, size_t len
)
107 if (mp_read_unsigned_bin((mp_int
*) n
, (u8
*) buf
, len
) != MP_OKAY
) {
108 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);
116 * bignum_cmp - Signed comparison
117 * @a: Bignum from bignum_init()
118 * @b: Bignum from bignum_init()
119 * Returns: 0 on success, -1 on failure
121 int bignum_cmp(const struct bignum
*a
, const struct bignum
*b
)
123 return mp_cmp((mp_int
*) a
, (mp_int
*) b
);
128 * bignum_cmd_d - Compare bignum to standard integer
129 * @a: Bignum from bignum_init()
131 * Returns: 0 on success, -1 on failure
133 int bignum_cmp_d(const struct bignum
*a
, unsigned long b
)
135 return mp_cmp_d((mp_int
*) a
, b
);
140 * bignum_add - c = a + b
141 * @a: Bignum from bignum_init()
142 * @b: Bignum from bignum_init()
143 * @c: Bignum from bignum_init(); used to store the result of a + b
144 * Returns: 0 on success, -1 on failure
146 int bignum_add(const struct bignum
*a
, const struct bignum
*b
,
149 if (mp_add((mp_int
*) a
, (mp_int
*) b
, (mp_int
*) c
) != MP_OKAY
) {
150 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);
158 * bignum_sub - c = a - b
159 * @a: Bignum from bignum_init()
160 * @b: Bignum from bignum_init()
161 * @c: Bignum from bignum_init(); used to store the result of a - b
162 * Returns: 0 on success, -1 on failure
164 int bignum_sub(const struct bignum
*a
, const struct bignum
*b
,
167 if (mp_sub((mp_int
*) a
, (mp_int
*) b
, (mp_int
*) c
) != MP_OKAY
) {
168 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);
176 * bignum_mul - c = a * b
177 * @a: Bignum from bignum_init()
178 * @b: Bignum from bignum_init()
179 * @c: Bignum from bignum_init(); used to store the result of a * b
180 * Returns: 0 on success, -1 on failure
182 int bignum_mul(const struct bignum
*a
, const struct bignum
*b
,
185 if (mp_mul((mp_int
*) a
, (mp_int
*) b
, (mp_int
*) c
) != MP_OKAY
) {
186 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);
194 * bignum_mulmod - d = a * b (mod c)
195 * @a: Bignum from bignum_init()
196 * @b: Bignum from bignum_init()
197 * @c: Bignum from bignum_init(); modulus
198 * @d: Bignum from bignum_init(); used to store the result of a * b (mod c)
199 * Returns: 0 on success, -1 on failure
201 int bignum_mulmod(const struct bignum
*a
, const struct bignum
*b
,
202 const struct bignum
*c
, struct bignum
*d
)
204 if (mp_mulmod((mp_int
*) a
, (mp_int
*) b
, (mp_int
*) c
, (mp_int
*) d
)
206 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);
214 * bignum_exptmod - Modular exponentiation: d = a^b (mod c)
215 * @a: Bignum from bignum_init(); base
216 * @b: Bignum from bignum_init(); exponent
217 * @c: Bignum from bignum_init(); modulus
218 * @d: Bignum from bignum_init(); used to store the result of a^b (mod c)
219 * Returns: 0 on success, -1 on failure
221 int bignum_exptmod(const struct bignum
*a
, const struct bignum
*b
,
222 const struct bignum
*c
, struct bignum
*d
)
224 if (mp_exptmod((mp_int
*) a
, (mp_int
*) b
, (mp_int
*) c
, (mp_int
*) d
)
226 wpa_printf(MSG_DEBUG
, "BIGNUM: %s failed", __func__
);