/* bigint.c */
/*
This file is part of the ARM-Crypto-Lib.
- Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
+ Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "bigint.h"
#include <string.h>
+#include <stdio.h>
#define PREFERE_HEAP_SPACE 1
#if PREFERE_HEAP_SPACE
#include <stdlib.h>
-#define ALLOC_BIGINT_WORDS(var,words) bigint_word_t *(var) = malloc((words) * sizeof(bigint_word_t))
+#define ALLOC_BIGINT_WORDS(var,words) \
+ bigint_word_t *(var) = malloc((words) * sizeof(bigint_word_t)); \
+ if (!(var)) { \
+ puts_P(PSTR("\n\nDBG: OOM ERROR (in arithmeics)!\n")); \
+ uart0_flush(); \
+ for(;;) \
+ ; \
+ }
+
#define FREE(x) free(x)
#else
#define DEBUG 1
-#if DEBUG || 1
+#if DEBUG
#include "cli.h"
-#include "uart_i.h"
+#include "uart.h"
#include "bigint_io.h"
-#include <stdio.h>
#endif
#ifndef MAX
while(x->wordv[i] == 0 && y->wordv[i] == 0){
++i;
}
- bigint_word_t g_b[i + 2], x_b[x->length_W - i], y_b[y->length_W - i];
- bigint_word_t u_b[x->length_W - i], v_b[y->length_W - i];
- bigint_word_t a_b[y->length_W + 2], c_b[y->length_W + 2];
- bigint_word_t b_b[x->length_W + 2], d_b[x->length_W + 2];
- bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
-
- g.wordv = g_b;
- x_.wordv = x_b;
- y_.wordv = y_b;
- memset(g_b, 0, i * sizeof(bigint_word_t));
- g_b[i] = 1;
+
+ ALLOC_BIGINT_WORDS(g_w, i + 2);
+ ALLOC_BIGINT_WORDS(x_w, x->length_W - i);
+ ALLOC_BIGINT_WORDS(y_w, y->length_W - i);
+ ALLOC_BIGINT_WORDS(u_w, x->length_W - i);
+ ALLOC_BIGINT_WORDS(v_w, y->length_W - i);
+ ALLOC_BIGINT_WORDS(a_w, y->length_W + 2);
+ ALLOC_BIGINT_WORDS(c_w, y->length_W + 2);
+ ALLOC_BIGINT_WORDS(b_w, x->length_W + 2);
+ ALLOC_BIGINT_WORDS(d_w, x->length_W + 2);
+
+ bigint_t g, x_, y_, u, v, a_, b_, c_, d_;
+
+ g.wordv = g_w;
+ x_.wordv = x_w;
+ y_.wordv = y_w;
+ memset(g_w, 0, i * sizeof(bigint_word_t));
+ g_w[i] = 1;
g.length_W = i + 1;
g.info = 0;
x_.info = y_.info = 0;
bigint_shiftright_bits(&y_, i);
}
- u.wordv = u_b;
- v.wordv = v_b;
- a_.wordv = a_b;
- b_.wordv = b_b;
- c_.wordv = c_b;
- d_.wordv = d_b;
+ u.wordv = u_w;
+ v.wordv = v_w;
+ a_.wordv = a_w;
+ b_.wordv = b_w;
+ c_.wordv = c_w;
+ d_.wordv = d_w;
bigint_copy(&u, &x_);
bigint_copy(&v, &y_);
if(b){
bigint_copy(b, &d_);
}
+
+ FREE(d_w);
+ FREE(b_w);
+ FREE(c_w);
+ FREE(a_w);
+ FREE(v_w);
+ FREE(u_w);
+ FREE(y_w);
+ FREE(x_w);
+ FREE(g_w);
}
/******************************************************************************/
}
/******************************************************************************/
-#if 1
void bigint_clip(bigint_t *dest, bigint_length_t length_W){
if(dest->length_W > length_W){
}
bigint_adjust(dest);
}
-/******************************************************************************/
+/******************************************************************************/
/*
* m_ = m * m'[0]
* dest = (a * b) % m (?)
/******************************************************************************/
-#endif
-
void bigint_expmod_u(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r){
#if 0
printf("\nDBG: expmod_u (a ** e %% m) <%s %s %d>\n\ta: ", __FILE__, __func__, __LINE__);
printf("\n\tm: ");
bigint_print_hex(r);
#endif
- if (0 && r->wordv[0] & 1) {
+ if (r->wordv[0] & 1) {
bigint_expmod_u_mont_sam(dest, a, exp, r);
} else {
bigint_expmod_u_sam(dest, a, exp, r);