3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "hexdigit_tab.h"
27 void bigint_print_hex(const bigint_t *a) {
28 if (a->length_W == 0) {
32 if (a->info & BIGINT_SIGN_MASK) {
36 uint8_t print_zero = 0;
38 p = (uint8_t*)&(a->wordv[a->length_W - 1]) + sizeof(bigint_word_t) - 1;
39 for (idx = a->length_W * sizeof(bigint_word_t); idx > 0; --idx) {
42 if (x != 0 || print_zero != 0) {
43 putchar(pgm_read_byte(&hexdigit_tab_lc_P[x]));
48 if (y != 0 || print_zero != 0) {
49 putchar(pgm_read_byte(&hexdigit_tab_lc_P[y]));
57 putchar(pgm_read_byte(&hexdigit_tab_lc_P[0]));
63 static uint8_t char2nibble(char c) {
64 if (c >= '0' && c <= '9') {
67 c |= 'A' ^ 'a'; /* to lower case */
68 if ( c >= 'a' && c <= 'f') {
74 static uint16_t read_byte(void) {
93 uint8_t bigint_read_hex_echo(bigint_t *a, bigint_length_t length) {
97 memset(a, 0, sizeof(*a));
98 if (length && a->allocated_W < length) {
100 p = int_realloc(buf, length * sizeof(bigint_word_t));
102 cli_putstr("\r\nERROR: Out of memory!");
105 memset((uint8_t*)p, 0, length * sizeof(bigint_word_t));
107 a->allocated_W = length;
110 if (a->allocated_W - idx < 1) {
118 p = int_realloc(buf, (a->allocated_W += BLOCKSIZE) * sizeof(bigint_word_t));
120 cli_putstr("\r\nERROR: Out of memory!");
126 memset((uint8_t*)p + (a->allocated_W - BLOCKSIZE) * sizeof(bigint_word_t), 0, BLOCKSIZE * sizeof(bigint_word_t));
133 a->info |= BIGINT_SIGN_MASK;
138 memset(a, 0, sizeof(*a));
144 buf[idx++] = (uint8_t)t;
148 buf[idx++] = (uint8_t)((t & 0x0f) << 4);
153 /* we have to reverse the byte array */
156 a->length_W = (idx + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
165 a->wordv = (bigint_word_t*)buf;
167 bigint_shiftright_1bit(a);
168 bigint_shiftright_1bit(a);
169 bigint_shiftright_1bit(a);
170 bigint_shiftright_1bit(a);
172 if(a->length_W == 1 && a->wordv[0] == 0){
177 a->length_W = length;