bignum.c
Go to the documentation of this file.
00001 /*
00002  * Big number math
00003  * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 
00017 #include "common.h"
00018 #include "bignum.h"
00019 
00020 #ifdef CONFIG_INTERNAL_LIBTOMMATH
00021 #include "libtommath.c"
00022 #else /* CONFIG_INTERNAL_LIBTOMMATH */
00023 #include <tommath.h>
00024 #endif /* CONFIG_INTERNAL_LIBTOMMATH */
00025 
00026 
00027 /*
00028  * The current version is just a wrapper for LibTomMath library, so
00029  * struct bignum is just typecast to mp_int.
00030  */
00031 
00036 struct bignum * bignum_init(void)
00037 {
00038         struct bignum *n = os_zalloc(sizeof(mp_int));
00039         if (n == NULL)
00040                 return NULL;
00041         if (mp_init((mp_int *) n) != MP_OKAY) {
00042                 os_free(n);
00043                 n = NULL;
00044         }
00045         return n;
00046 }
00047 
00048 
00053 void bignum_deinit(struct bignum *n)
00054 {
00055         if (n) {
00056                 mp_clear((mp_int *) n);
00057                 os_free(n);
00058         }
00059 }
00060 
00061 
00067 size_t bignum_get_unsigned_bin_len(struct bignum *n)
00068 {
00069         return mp_unsigned_bin_size((mp_int *) n);
00070 }
00071 
00072 
00081 int bignum_get_unsigned_bin(const struct bignum *n, u8 *buf, size_t *len)
00082 {
00083         size_t need = mp_unsigned_bin_size((mp_int *) n);
00084         if (len && need > *len) {
00085                 *len = need;
00086                 return -1;
00087         }
00088         if (mp_to_unsigned_bin((mp_int *) n, buf) != MP_OKAY) {
00089                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00090                 return -1;
00091         }
00092         if (len)
00093                 *len = need;
00094         return 0;
00095 }
00096 
00097 
00105 int bignum_set_unsigned_bin(struct bignum *n, const u8 *buf, size_t len)
00106 {
00107         if (mp_read_unsigned_bin((mp_int *) n, (u8 *) buf, len) != MP_OKAY) {
00108                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00109                 return -1;
00110         }
00111         return 0;
00112 }
00113 
00114 
00121 int bignum_cmp(const struct bignum *a, const struct bignum *b)
00122 {
00123         return mp_cmp((mp_int *) a, (mp_int *) b);
00124 }
00125 
00126 
00133 int bignum_cmp_d(const struct bignum *a, unsigned long b)
00134 {
00135         return mp_cmp_d((mp_int *) a, b);
00136 }
00137 
00138 
00146 int bignum_add(const struct bignum *a, const struct bignum *b,
00147                struct bignum *c)
00148 {
00149         if (mp_add((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
00150                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00151                 return -1;
00152         }
00153         return 0;
00154 }
00155 
00156 
00164 int bignum_sub(const struct bignum *a, const struct bignum *b,
00165                struct bignum *c)
00166 {
00167         if (mp_sub((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
00168                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00169                 return -1;
00170         }
00171         return 0;
00172 }
00173 
00174 
00182 int bignum_mul(const struct bignum *a, const struct bignum *b,
00183                struct bignum *c)
00184 {
00185         if (mp_mul((mp_int *) a, (mp_int *) b, (mp_int *) c) != MP_OKAY) {
00186                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00187                 return -1;
00188         }
00189         return 0;
00190 }
00191 
00192 
00201 int bignum_mulmod(const struct bignum *a, const struct bignum *b,
00202                   const struct bignum *c, struct bignum *d)
00203 {
00204         if (mp_mulmod((mp_int *) a, (mp_int *) b, (mp_int *) c, (mp_int *) d)
00205             != MP_OKAY) {
00206                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00207                 return -1;
00208         }
00209         return 0;
00210 }
00211 
00212 
00221 int bignum_exptmod(const struct bignum *a, const struct bignum *b,
00222                    const struct bignum *c, struct bignum *d)
00223 {
00224         if (mp_exptmod((mp_int *) a, (mp_int *) b, (mp_int *) c, (mp_int *) d)
00225             != MP_OKAY) {
00226                 wpa_printf(MSG_DEBUG, "BIGNUM: %s failed", __func__);
00227                 return -1;
00228         }
00229         return 0;
00230 }


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:19