bignum.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "bignum.h"
00020 
00021 #ifdef CONFIG_INTERNAL_LIBTOMMATH
00022 #include "libtommath.c"
00023 #else /* CONFIG_INTERNAL_LIBTOMMATH */
00024 #include <tommath.h>
00025 #endif /* CONFIG_INTERNAL_LIBTOMMATH */
00026 
00027 
00028 /*
00029  * The current version is just a wrapper for LibTomMath library, so
00030  * struct bignum is just typecast to mp_int.
00031  */
00032 
00038 struct bignum * bignum_init(void)
00039 {
00040         struct bignum *n = os_zalloc(sizeof(mp_int));
00041         if (n == NULL)
00042                 return NULL;
00043         if (mp_init((mp_int *) n) != MP_OKAY) {
00044                 os_free(n);
00045                 n = NULL;
00046         }
00047         return n;
00048 }
00049 
00050 
00056 void bignum_deinit(struct bignum *n)
00057 {
00058         if (n) {
00059                 mp_clear((mp_int *) n);
00060                 os_free(n);
00061         }
00062 }
00063 
00064 
00071 size_t bignum_get_unsigned_bin_len(struct bignum *n)
00072 {
00073         return mp_unsigned_bin_size((mp_int *) n);
00074 }
00075 
00076 
00086 int bignum_get_unsigned_bin(const struct bignum *n, u8 *buf, size_t *len)
00087 {
00088         size_t need = mp_unsigned_bin_size((mp_int *) n);
00089         if (len && need > *len) {
00090                 *len = need;
00091                 return -1;
00092         }
00093         if (mp_to_unsigned_bin((mp_int *) n, buf) != MP_OKAY) {
00094                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00095                 return -1;
00096         }
00097         if (len)
00098                 *len = need;
00099         return 0;
00100 }
00101 
00102 
00111 int bignum_set_unsigned_bin(struct bignum *n, const u8 *buf, size_t len)
00112 {
00113         if (mp_read_unsigned_bin((mp_int *) n, (u8 *) buf, len) != MP_OKAY) {
00114                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00115                 return -1;
00116         }
00117         return 0;
00118 }
00119 
00120 
00128 int bignum_cmp(const struct bignum *a, const struct bignum *b)
00129 {
00130         return mp_cmp((mp_int *) a, (mp_int *) b);
00131 }
00132 
00133 
00141 int bignum_cmp_d(const struct bignum *a, unsigned long b)
00142 {
00143         return mp_cmp_d((mp_int *) a, b);
00144 }
00145 
00146 
00155 int bignum_add(const struct bignum *a, const struct bignum *b,
00156                struct bignum *c)
00157 {
00158         if (mp_add((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
00159                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00160                 return -1;
00161         }
00162         return 0;
00163 }
00164 
00165 
00174 int bignum_sub(const struct bignum *a, const struct bignum *b,
00175                struct bignum *c)
00176 {
00177         if (mp_sub((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
00178                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00179                 return -1;
00180         }
00181         return 0;
00182 }
00183 
00184 
00193 int bignum_mul(const struct bignum *a, const struct bignum *b,
00194                struct bignum *c)
00195 {
00196         if (mp_mul((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
00197                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00198                 return -1;
00199         }
00200         return 0;
00201 }
00202 
00203 
00213 int bignum_mulmod(const struct bignum *a, const struct bignum *b,
00214                   const struct bignum *c, struct bignum *d)
00215 {
00216         if (mp_mulmod((mp_int *) a, (mp_int *) b, (mp_int *) c, (mp_int *) d)
00217             != MP_OKAY) {
00218                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00219                 return -1;
00220         }
00221         return 0;
00222 }
00223 
00224 
00234 int bignum_exptmod(const struct bignum *a, const struct bignum *b,
00235                    const struct bignum *c, struct bignum *d)
00236 {
00237         if (mp_exptmod((mp_int *) a, (mp_int *) b, (mp_int *) c, (mp_int *) d)
00238             != MP_OKAY) {
00239                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00240                 return -1;
00241         }
00242         return 0;
00243 }   
00244 

Generated on Sun Dec 31 13:48:51 2006 for wpa_supplicant by  doxygen 1.4.2