X-Git-Url: https://git.cryptolib.org/?p=avr-crypto-lib.git;a=blobdiff_plain;f=bigint%2Fbigint_io.c;h=a188c34a6da74e8c1a5cf8c9c269a1c0275ee547;hp=1a2b12c27bf8feb5ebdf0f47437c8372b1afae94;hb=4b5da1dc27a791b5c448274a3db09cd035b33493;hpb=eb09a2a6f447833e3cf73a71fc0113f84d51f41b diff --git a/bigint/bigint_io.c b/bigint/bigint_io.c index 1a2b12c..a188c34 100644 --- a/bigint/bigint_io.c +++ b/bigint/bigint_io.c @@ -1,7 +1,7 @@ /* bigint_io.c */ /* - This file is part of the AVR-Crypto-Lib. - Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de) + This file is part of the ARM-Crypto-Lib. + 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 @@ -18,92 +18,114 @@ */ #include "cli.h" +#include "hexdigit_tab.h" #include "bigint.h" #include #include -void bigint_print_hex(const bigint_t* a){ - if(a->info&BIGINT_NEG_MASK){ +void bigint_print_hex(const bigint_t *a) { + if (a->length_W == 0) { + cli_putc('0'); + return; + } + if (a->info&BIGINT_NEG_MASK) { cli_putc('-'); } -// cli_putc((a->info&BIGINT_NEG_MASK)?'-':'+'); /* print sign */ - if(a->length_B!=0){ - cli_hexdump_rev(a->wordv, a->length_B); - }else{ - cli_putc('0'); + size_t idx; + uint8_t print_zero = 0; + uint8_t *p, x, y; + p = (uint8_t*)&(a->wordv[a->length_W - 1]) + sizeof(bigint_word_t) - 1; + for (idx = a->length_W * sizeof(bigint_word_t); idx > 0; --idx) { + x = *p >> 4; + y = *p & 0xf; + if (x != 0 || print_zero != 0) { + cli_putc(pgm_read_byte(&hexdigit_tab_lc_P[x])); + } + if (x) { + print_zero = 1; + } + if (y != 0 || print_zero != 0) { + cli_putc(pgm_read_byte(&hexdigit_tab_lc_P[y])); + } + if (y) { + print_zero = 1; + } + --p; } } -#define BLOCKSIZE 20 +#define BLOCKSIZE 32 -static uint8_t char2nibble(char c){ - if(c>='0' && c <='9'){ - return c-'0'; +static uint8_t char2nibble(char c) { + if (c >= '0' && c <= '9') { + return c - '0'; } - c |= 'A'^'a'; /* to lower case */ - if(c>='a' && c <='f'){ - return c-'a'+10; + c |= 'A' ^ 'a'; /* to lower case */ + if ( c>= 'a' && c <= 'f') { + return c - 'a' + 10; } return 0xff; } -static uint16_t read_byte(void){ +static uint16_t read_byte(void) { uint8_t t1, t2; char c; c = cli_getc_cecho(); - if(c=='-'){ + if (c == '-') { return 0x0500; } t1 = char2nibble(c); - if(t1 == 0xff){ + if (t1 == 0xff) { return 0x0100; } c = cli_getc_cecho(); t2 = char2nibble(c); - if(t2 == 0xff){ + if (t2 == 0xff) { return 0x0200|t1; } - return (t1<<4)|t2; + return (t1 << 4)|t2; } -uint8_t bigint_read_hex_echo(bigint_t* a){ - uint16_t allocated=0; - uint8_t shift4=0; - uint16_t t; - a->length_B = 0; +uint8_t bigint_read_hex_echo(bigint_t *a) { + uint16_t allocated = 0; + uint8_t shift4 = 0; + uint16_t t, idx = 0; + uint8_t *buf = NULL; + a->length_W = 0; a->wordv = NULL; a->info = 0; - for(;;){ - if(allocated-a->length_B < 1){ + for (;;) { + if (allocated - idx < 1) { uint8_t *p; - p = realloc(a->wordv, allocated+=BLOCKSIZE); - if(p==NULL){ - cli_putstr_P(PSTR("\r\nERROR: Out of memory!")); - free(a->wordv); + p = realloc(buf, (allocated += BLOCKSIZE) * sizeof(bigint_word_t)); + if (p == NULL) { + cli_putstr("\r\nERROR: Out of memory!"); + free(buf); return 0xff; } - a->wordv=p; + memset((uint8_t*)p + (allocated - BLOCKSIZE) * sizeof(bigint_word_t), 0, BLOCKSIZE * sizeof(bigint_word_t)); + buf = p; } t = read_byte(); - if(a->length_B==0){ - if(t&0x0400){ + if (idx == 0) { + if (t & 0x0400) { /* got minus */ a->info |= BIGINT_NEG_MASK; continue; - }else{ - if(t==0x0100){ + } else { + if (t == 0x0100) { free(a->wordv); - a->wordv=NULL; + a->wordv = NULL; return 1; } } } - if(t<=0x00ff){ - a->wordv[a->length_B++] = (uint8_t)t; - }else{ - if(t&0x0200){ + if (t <= 0x00ff) { + buf[idx++] = (uint8_t)t; + } else { + if (t & 0x0200) { shift4 = 1; - a->wordv[a->length_B++] = (uint8_t)((t&0x0f)<<4); + buf[idx++] = (uint8_t)((t & 0x0f) << 4); } break; } @@ -111,18 +133,23 @@ uint8_t bigint_read_hex_echo(bigint_t* a){ /* we have to reverse the byte array */ uint8_t tmp; uint8_t *p, *q; - p = a->wordv; - q = a->wordv+a->length_B-1; - while(q>p){ + a->length_W = (idx + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); + p = buf; + q = buf + idx - 1; + while (q > p) { tmp = *p; *p = *q; *q = tmp; p++; q--; } - if(shift4){ - bigint_adjust(a); + a->wordv = (bigint_word_t*)buf; + bigint_adjust(a); + if (shift4) { bigint_shiftright(a, 4); } - bigint_adjust(a); + if(a->length_W == 1 && a->wordv[0] == 0){ + a->length_W = 0; + a->info = 0; + } return 0; }