]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - bigint/bigint_io.c
adding RSA-OAEP
[avr-crypto-lib.git] / bigint / bigint_io.c
index 221c61c0e3ffe7a810d27aea6e9fae077f7ad758..095fae91c59439cb509ff6129e4be9b9448673b0 100644 (file)
@@ -1,6 +1,6 @@
 /* bigint_io.c */
 /*
-    This file is part of the AVR-Crypto-Lib.
+    This file is part of the ARM-Crypto-Lib.
     Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
 
     This program is free software: you can redistribute it and/or modify
@@ -20,7 +20,6 @@
 #include "cli.h"
 #include "hexdigit_tab.h"
 #include "bigint.h"
-#include <avr/pgmspace.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -33,15 +32,37 @@ void bigint_print_hex(const bigint_t* a){
                cli_putc('-');
        }
 //     cli_putc((a->info&BIGINT_NEG_MASK)?'-':'+'); /* print sign */
-       if(a->wordv[a->length_B-1]<0x10){
-               cli_putc(pgm_read_byte(hexdigit_tab_uc_P+a->wordv[a->length_B-1]));
+/*     if(a->wordv[a->length_B-1]<0x10){
+               cli_putc(hexdigit_tab_uc[a->wordv[a->length_B-1]]);
                cli_hexdump_rev(a->wordv, a->length_B-1);
        } else {
-               cli_hexdump_rev(a->wordv, a->length_B);
+*/
+       //      cli_hexdump_rev(a->wordv, a->length_B*sizeof(bigint_word_t));
+//     }
+       uint32_t idx;
+       uint8_t print_zero=0;
+       uint8_t *p,x,y;
+       p = (uint8_t*)&(a->wordv[a->length_B-1])+sizeof(bigint_word_t)-1;
+       for(idx = a->length_B * 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'){
@@ -76,23 +97,24 @@ static uint16_t read_byte(void){
 uint8_t bigint_read_hex_echo(bigint_t* a){
        uint16_t allocated=0;
        uint8_t  shift4=0;
-       uint16_t  t;
+       uint16_t  t, idx = 0;
        a->length_B = 0;
        a->wordv = NULL;
        a->info = 0;
        for(;;){
-               if(allocated-a->length_B < 1){
-                       uint8_t *p;
-                       p = realloc(a->wordv, allocated+=BLOCKSIZE);
+               if(allocated - idx < 1){
+                       bigint_word_t *p;
+                       p = realloc(a->wordv, allocated += BLOCKSIZE);
                        if(p==NULL){
-                               cli_putstr_P(PSTR("\r\nERROR: Out of memory!"));
+                               cli_putstr("\r\nERROR: Out of memory!");
                                free(a->wordv);
                                return 0xff;
                        }
+                       memset((uint8_t*)p + allocated - BLOCKSIZE, 0, BLOCKSIZE);
                        a->wordv=p;
                }
                t = read_byte();
-               if(a->length_B==0){
+               if(idx==0){
                        if(t&0x0400){
                                /* got minus */
                                a->info |= BIGINT_NEG_MASK;
@@ -106,11 +128,11 @@ uint8_t bigint_read_hex_echo(bigint_t* a){
                        }
                }
                if(t<=0x00ff){
-                       a->wordv[a->length_B++] = (uint8_t)t;
+                       ((uint8_t*)(a->wordv))[idx++] = (uint8_t)t;
                }else{
                        if(t&0x0200){
                                shift4 = 1;
-                               a->wordv[a->length_B++] = (uint8_t)((t&0x0f)<<4);
+                               ((uint8_t*)(a->wordv))[idx++] = (uint8_t)((t&0x0f)<<4);
                        }
                        break;
                }
@@ -118,18 +140,18 @@ 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;
+       a->length_B = (idx + sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
+       p = (uint8_t*)(a->wordv);
+       q = (uint8_t*)a->wordv + a->length_B * sizeof(bigint_word_t) - 1;
        while(q>p){
                tmp = *p;
                *p = *q;
                *q = tmp;
                p++; q--;
        }
+       bigint_adjust(a);
        if(shift4){
-               bigint_adjust(a);
                bigint_shiftright(a, 4);
        }
-       bigint_adjust(a);
        return 0;
 }