]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
new skipjack test
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 20 Jun 2012 21:58:55 +0000 (21:58 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 20 Jun 2012 21:58:55 +0000 (21:58 +0000)
24 files changed:
Makefile_en21_conf.inc
bigint/bigint.c
bigint/bigint.h
hmac-sha1/hmac-sha1.c
host/bigint_test.rb
host/get_test.rb
host/nessie_check.rb
rsa/rsa_basic.c
rsa/rsa_basic.h
sha1/sha1.c
sha1/sha1.h
shacal1/shacal1_enc.c
test_src/circularbytebuffer-asm.S
test_src/circularbytebuffer.h
test_src/cli-hexdump.S
test_src/main-aes-test.c
test_src/main-arcfour-test.c
test_src/main-des-test.c
test_src/main-present-test.c
test_src/main-rsaes_oaep-test.c
test_src/main-rsaes_pkcs1v15-test.c
test_src/main-skipjack-test.c
test_src/uart_i-asm.S
testvectors/skipjack_tv.pdf [new file with mode: 0644]

index 66f3ef97d18bf4634c87412f0fd9d07f26094a7b..bbdeeb2c67af52c22cd4d6f95072182f2cfbda92 100644 (file)
@@ -5,6 +5,8 @@ OPTIMIZE       = -Os # -Os
 EXTRALINK      = xram.o
 DEFS           = -D$(call uc, $(MCU_TARGET)) -DF_CPU=$(F_CPU)
 BOARD_NAME     = ethernut2.1
+FLASHCMD       = /bin/bash openocd_flash.sh #
+RESETCMD       = 
 
 override CFLAGS_A      = -MMD -MF$(DEP_DIR)$(patsubst %.o,%.d,$(notdir $(1))) $(DEBUG) $(WARNING) -std=$(CSTD) $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
 override CFLAGS        = -MMD -MF$(DEP_DIR)$(patsubst %.o,%.d,$(notdir $@))   $(DEBUG) $(WARNING) -std=$(CSTD) $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
index c69d42ec7d22d389b67324deebc92c7ea443aa86..3c0bb533c1bdb13bf23a81c9d92d892d80e54bfc 100644 (file)
@@ -53,9 +53,9 @@
 #define SET_NEG(a)   (a)->info |= BIGINT_NEG_MASK
 #define SET_POS(a)   (a)->info &= ~BIGINT_NEG_MASK
 #define XCHG(a,b)    do{(a)^=(b); (b)^=(a); (a)^=(b);}while(0)
-#define XCHG_PTR(a,b)    do{ a = (void*)(((bigint_ptr_int_t)(a)) ^ ((bigint_ptr_int_t)(b))); \
-                                b = (void*)(((bigint_ptr_int_t)(a)) ^ ((bigint_ptr_int_t)(b))); \
-                                a = (void*)(((bigint_ptr_int_t)(a)) ^ ((bigint_ptr_int_t)(b)));}while(0)
+#define XCHG_PTR(a,b)    do{ a = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
+                                b = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b))); \
+                                a = (void*)(((intptr_t)(a)) ^ ((intptr_t)(b)));}while(0)
 
 #define GET_SIGN(a) ((a)->info&BIGINT_NEG_MASK)
 
@@ -80,7 +80,7 @@ void bigint_adjust(bigint_t* a){
 
 /******************************************************************************/
 
-uint16_t bigint_length_b(bigint_t* a){
+uint16_t bigint_length_b(const bigint_t* a){
        if(!a->length_B || a->length_B==0){
                return 0;
        }
@@ -89,13 +89,13 @@ uint16_t bigint_length_b(bigint_t* a){
 
 /******************************************************************************/
 
-uint16_t bigint_length_B(bigint_t* a){
+uint16_t bigint_length_B(const bigint_t* a){
        return a->length_B * sizeof(bigint_word_t);
 }
 
 /******************************************************************************/
 
-uint32_t bigint_get_first_set_bit(bigint_t* a){
+uint32_t bigint_get_first_set_bit(const bigint_t* a){
        if(a->length_B==0){
                return (uint32_t)(-1);
        }
@@ -105,7 +105,7 @@ uint32_t bigint_get_first_set_bit(bigint_t* a){
 
 /******************************************************************************/
 
-uint32_t bigint_get_last_set_bit(bigint_t* a){
+uint32_t bigint_get_last_set_bit(const bigint_t* a){
        uint32_t r=0;
        uint8_t b=0;
        bigint_word_t x=1;
@@ -138,23 +138,24 @@ void bigint_copy(bigint_t* dest, const bigint_t* src){
 /* this should be implemented in assembly */
 void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
        uint16_t i;
-       bigint_wordplus_t t=0LL;
+       bigint_wordplus_t t = 0LL;
        if(a->length_B < b->length_B){
                XCHG_PTR(a,b);
        }
-       for(i=0; i<b->length_B; ++i){
-//             t = (bigint_wordplus_t)(a->wordv[i]) + (bigint_wordplus_t)(b->wordv[i]) + t;
+       for(i = 0; i < b->length_B; ++i){
                t += a->wordv[i];
                t += b->wordv[i];
                dest->wordv[i] = (bigint_word_t)t;
-               t>>=BIGINT_WORD_SIZE;
+               t >>= BIGINT_WORD_SIZE;
        }
-       for(; i<a->length_B; ++i){
+       for(; i < a->length_B; ++i){
                t += a->wordv[i];
                dest->wordv[i] = (bigint_word_t)t;
-               t>>=BIGINT_WORD_SIZE;
+               t >>= BIGINT_WORD_SIZE;
+       }
+       if(t){
+               dest->wordv[i++] = (bigint_word_t)t;
        }
-       dest->wordv[i++] = (bigint_word_t)t;
        dest->length_B = i;
        bigint_adjust(dest);
 }
@@ -163,20 +164,66 @@ void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
 
 /* this should be implemented in assembly */
 void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale){
-       bigint_t x;
+       if(a->length_B == 0){
+               return;
+       }
+       if(scale == 0){
+               bigint_add_u(dest, dest, a);
+               return;
+       }
+#if DEBUG_ADD_SCALE
+       cli_putstr_P(PSTR("\r\nDBG: bigint_add_scale("));
+       bigint_print_hex(dest);
+       cli_putc(',');
+       bigint_print_hex(dest);
+       cli_putc(',');
+       cli_hexdump_rev(&scale, 2);
+       cli_putc(')');
+#endif
 #if BIGINT_WORD_SIZE == 8
-       memset(dest->wordv + dest->length_B, 0, MAX(dest->length_B, a->length_B + scale) - dest->length_B);
-       x.wordv = dest->wordv + scale;
+       if(scale >= dest->length_B){
+#if DEBUG_ADD_SCALE
+               cli_putstr_P(PSTR("\r\n\tpath one"));
+#endif
+               memset(dest->wordv + dest->length_B, 0, scale - dest->length_B);
+               memcpy(dest->wordv + scale, a->wordv, a->length_B);
+               dest->info = a->info;
+               dest->length_B = a->length_B + scale;
+               return;
+       }
+       bigint_t x;
+#if DEBUG_ADD_SCALE
+       cli_putstr_P(PSTR("\r\n\tpath two"));
+#endif
        x.length_B = dest->length_B - scale;
-       if((int16_t)x.length_B < 0)
-               x.length_B = 0;
        x.info = dest->info;
+       x.wordv = dest->wordv + scale;
        bigint_add_u(&x, &x, a);
        dest->length_B = x.length_B + scale;
        dest->info = 0;
        bigint_adjust(dest);
 #else
-#error unimplemented!
+       bigint_t s;
+       uint16_t word_shift = scale / sizeof(bigint_word_t), byte_shift = scale % sizeof(bigint_word_t);
+       bigint_word_t bv[a->length_B + 1];
+       s.wordv = bv;
+       bv[0] = bv[a->length_B] = 0;
+       memcpy((uint8_t*)bv + byte_shift, a->wordv, a->length_B * sizeof(bigint_word_t));
+       s.length_B = a->length_B + 1;
+       bigint_adjust(&s);
+       memset(dest->wordv + dest->length_B, 0, (MAX(dest->length_B, s.length_B + word_shift) - dest->length_B) * sizeof(bigint_word_t));
+       x.wordv = dest->wordv + word_shift;
+       x.length_B = dest->length_B - word_shift;
+       if((int16_t)x.length_B < 0){
+               x.length_B = 0;
+               x.info = 0;
+       }else{
+               x.info = dest->info;
+       }
+       bigint_add_u(&x, &x, &s);
+       dest->length_B = x.length_B + word_shift;
+       dest->info = 0;
+       bigint_adjust(dest);
 #endif
 
 
@@ -220,9 +267,7 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
        int8_t borrow=0;
        int8_t  r;
        bigint_wordplus_signed_t t=0LL;
-       uint16_t i, min, max;
-       min = MIN(a->length_B, b->length_B);
-       max = MAX(a->length_B, b->length_B);
+       uint16_t i;
        r = bigint_cmp_u(a,b);
        if(r==0){
                bigint_set_zero(dest);
@@ -243,9 +288,9 @@ void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
                SET_NEG(dest);
                return;
        }
-       for(i=0; i<max; ++i){
+       for(i=0; i < a->length_B; ++i){
                t = a->wordv[i];
-               if(i<min){
+               if(i < b->length_B){
                        t -= b->wordv[i];
                }
                t -= borrow;
@@ -291,34 +336,26 @@ int8_t bigint_cmp_u(const bigint_t* a, const bigint_t* b){
 
 void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
        uint8_t s;
-       int8_t d = 0;
        s  = GET_SIGN(a)?2:0;
        s |= GET_SIGN(b)?1:0;
        switch(s){
                case 0: /* both positive */
-                       d = 1;
                        bigint_add_u(dest, a,b);
+                       SET_POS(dest);
                        break;
                case 1: /* a positive, b negative */
-                       d = bigint_cmp_u(a,b);
                        bigint_sub_u(dest, a, b);
                        break;
                case 2: /* a negative, b positive */
-                       d = bigint_cmp_u(b,a);
                        bigint_sub_u(dest, b, a);
                        break;
                case 3: /* both negative */
-                       d = -1;
                        bigint_add_u(dest, a, b);
+                       SET_NEG(dest);
                        break;
                default: /* how can this happen?*/
                        break;
        }
-       if(d<0){
-               SET_NEG(dest);
-       }else{
-               SET_POS(dest);
-       }
 }
 
 /******************************************************************************/
@@ -379,35 +416,33 @@ int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b){
 /******************************************************************************/
 
 void bigint_shiftleft(bigint_t* a, uint16_t shift){
-       uint16_t byteshift, word_alloc;
+       uint16_t byteshift, words_to_shift;
        int16_t i;
        uint8_t bitshift;
        bigint_word_t *p;
        bigint_wordplus_t t=0;
-       if(shift==0){
+       if(shift == 0){
                return;
        }
-       byteshift = shift/8;
-       bitshift = shift&7;
-       for(i=0;i<=byteshift/sizeof(bigint_word_t); ++i){
-               a->wordv[a->length_B+i] = 0;
-       }
+       byteshift = shift / 8;
+       bitshift = shift & 7;
+       memset(&a->wordv[a->length_B], 0x00, byteshift);
        if(byteshift){
-               memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_B*sizeof(bigint_word_t));
+               memmove(((uint8_t*)a->wordv)+byteshift, a->wordv, a->length_B * sizeof(bigint_word_t));
                memset(a->wordv, 0, byteshift);
        }
-       p = (bigint_word_t*)(((uint8_t*)a->wordv)+byteshift);
-       word_alloc = a->length_B+(byteshift+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t)+1;
-       a->wordv[word_alloc-1]=0;
-       if(bitshift!=0){
-               for(i=0; i<a->length_B; ++i){
-                       t |= ((bigint_wordplus_t)p[i])<<bitshift;
+       if(bitshift != 0){
+               p = (bigint_word_t*)((uint8_t*)a->wordv + byteshift / sizeof(bigint_word_t));
+               words_to_shift = a->length_B + ((byteshift % sizeof(bigint_word_t))?1:0);
+               /* XXX */
+               for(i = 0; i < words_to_shift; ++i){
+                       t |= ((bigint_wordplus_t)p[i]) << bitshift;
                        p[i] = (bigint_word_t)t;
                        t >>= BIGINT_WORD_SIZE;
                }
                p[i] = (bigint_word_t)t;
        }
-       a->length_B = word_alloc;
+       a->length_B += (shift + BIGINT_WORD_SIZE - 1) / BIGINT_WORD_SIZE;
        bigint_adjust(a);
 }
 
@@ -420,30 +455,29 @@ void bigint_shiftright(bigint_t* a, uint16_t shift){
        bigint_wordplus_t t=0;
        byteshift = shift/8;
        bitshift = shift&7;
-       if(byteshift >= a->length_B*sizeof(bigint_word_t)){ /* we would shift out more than we have */
+       if(byteshift >= a->length_B * sizeof(bigint_word_t)){ /* we would shift out more than we have */
                bigint_set_zero(a);
                return;
        }
-       if(byteshift == a->length_B*sizeof(bigint_word_t)-1 && bitshift>GET_FBS(a)){
+       if(byteshift == a->length_B * sizeof(bigint_word_t) - 1 && bitshift > GET_FBS(a)){
                bigint_set_zero(a);
                return;
        }
        if(byteshift){
-               memmove(a->wordv, (uint8_t*)a->wordv+byteshift, a->length_B-byteshift);
-               memset((uint8_t*)a->wordv+a->length_B-byteshift, 0,  byteshift);
+               memmove(a->wordv, (uint8_t*)a->wordv + byteshift, a->length_B * sizeof(bigint_word_t) - byteshift);
+               memset((uint8_t*)a->wordv + a->length_B * sizeof(bigint_word_t) - byteshift, 0,  byteshift);
        }
        byteshift /= sizeof(bigint_word_t);
-       if(bitshift!=0){
+    a->length_B -= (byteshift  + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
+       if(bitshift != 0 && a->length_B){
         /* shift to the right */
-               for(i=a->length_B-byteshift-1; i>0; --i){
-                       t |= ((bigint_wordplus_t)(a->wordv[i]))<<(BIGINT_WORD_SIZE-bitshift);
-                       a->wordv[i] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
+               i = a->length_B - 1;
+               do{
+                       t |= ((bigint_wordplus_t)(a->wordv[i])) << (BIGINT_WORD_SIZE - bitshift);
+                       a->wordv[i] = (bigint_word_t)(t >> BIGINT_WORD_SIZE);
                        t <<= BIGINT_WORD_SIZE;
-               }
-               t |= ((bigint_wordplus_t)(a->wordv[0]))<<(BIGINT_WORD_SIZE-bitshift);
-               a->wordv[0] = (bigint_word_t)(t>>BIGINT_WORD_SIZE);
+               }while(i--);
        }
-    a->length_B -= ((shift/8)+sizeof(bigint_word_t)-1)/sizeof(bigint_word_t);
        bigint_adjust(a);
 }
 
@@ -474,21 +508,21 @@ void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
        }
        if(dest==a || dest==b){
                bigint_t d;
-               bigint_word_t d_b[a->length_B+b->length_B];
+               bigint_word_t d_b[a->length_B + b->length_B];
                d.wordv = d_b;
                bigint_mul_u(&d, a, b);
                bigint_copy(dest, &d);
                return;
        }
        if(a->length_B==1 || b->length_B==1){
-               if(a->length_B!=1){
+               if(a->length_B != 1){
                        XCHG_PTR(a,b);
                }
                bigint_wordplus_t t=0;
                uint16_t i;
                bigint_word_t x = a->wordv[0];
                for(i=0; i < b->length_B; ++i){
-                       t += ((bigint_wordplus_t)b->wordv[i])*((bigint_wordplus_t)x);
+                       t += ((bigint_wordplus_t)b->wordv[i]) * ((bigint_wordplus_t)x);
                        dest->wordv[i] = (bigint_word_t)t;
                        t>>=BIGINT_WORD_SIZE;
                }
@@ -508,30 +542,57 @@ void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
                bigint_adjust(dest);
                return;
        }
-       bigint_set_zero(dest);
+#if BIGINT_WORD_SIZE == 8
+       if(a->length_B <= 4 || b->length_B <= 4){
+               if(a->length_B > 4){
+                       XCHG_PTR(a,b);
+               }
+               uint32_t x = 0, y = 0;
+               uint16_t j = b->length_B / 4, idx = 0;
+               uint64_t r = 0;
+               memcpy(&x, a->wordv, a->length_B);
+               while(j){
+                       r += (uint64_t)((uint32_t*)b->wordv)[idx] * (uint64_t)x;
+                       ((uint32_t*)dest->wordv)[idx] = (uint32_t)r;
+                       r >>= 32;
+                       ++idx;
+                       --j;
+               }
+               idx *= 4;
+               memcpy(&y, b->wordv + idx, b->length_B - idx);
+               r += (uint64_t)y * (uint64_t)x;
+               while(r){
+                       dest->wordv[idx++] = (uint8_t)r;
+                       r >>= 8;
+               }
+               dest->length_B = idx;
+               bigint_adjust(dest);
+               return;
+       }
+#endif
        /* split a in xh & xl; split b in yh & yl */
        const uint16_t n = (MAX(a->length_B, b->length_B)+1)/2;
        bigint_t xl, xh, yl, yh;
        xl.wordv = a->wordv;
        yl.wordv = b->wordv;
-       if(a->length_B<=n){
+       if(a->length_B <= n){
                bigint_set_zero(&xh);
                xl.length_B = a->length_B;
                xl.info = a->info;
        }else{
-               xl.length_B=n;
+               xl.length_B = n;
                xl.info = 0;
                bigint_adjust(&xl);
                xh.wordv = &(a->wordv[n]);
                xh.length_B = a->length_B-n;
                xh.info = a->info;
        }
-       if(b->length_B<=n){
+       if(b->length_B <= n){
                bigint_set_zero(&yh);
                yl.length_B = b->length_B;
                yl.info = b->info;
        }else{
-               yl.length_B=n;
+               yl.length_B = n;
                yl.info = 0;
                bigint_adjust(&yl);
                yh.wordv = &(b->wordv[n]);
@@ -540,24 +601,39 @@ void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b){
        }
        /* now we have split up a and b */
        /* remember we want to do:
-        * x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + yh*yl
+        * x*y = (xh * b ** n + xl) * (yh * b ** n + yl)
+        * x*y = (xh*yh)*b**2n + ((xh+xl)*(yh+yl) - xh*yh - xl*yl)*b**n + xl*yl
         *          5          9     2   4   3    7   5   6   1         8   1
         */
-       bigint_word_t  tmp_b[2*n+2], m_b[2*(n+1)];
-       bigint_t tmp, tmp2, m;
-       tmp.wordv = tmp_b;
-       tmp2.wordv = &(tmp_b[n+1]);
-       m.wordv = m_b;
-
-       bigint_mul_u(dest, &xl, &yl);  /* 1: dest <= xl*yl     */
-       bigint_add_u(&tmp2, &xh, &xl); /* 2: tmp2 <= xh+xl     */
-       bigint_add_u(&tmp, &yh, &yl);  /* 3: tmp  <= yh+yl     */
-       bigint_mul_u(&m, &tmp2, &tmp); /* 4: m    <= tmp2*tmp  */
-       bigint_mul_u(&tmp, &xh, &yh);  /* 5: h    <= xh*yh     */
-       bigint_sub_u(&m, &m, dest);    /* 6: m    <= m-dest    */
-    bigint_sub_u(&m, &m, &tmp);    /* 7: m    <= m-h       */
-       bigint_add_scale_u(dest, &m, n*sizeof(bigint_word_t)); /* 8: dest <= dest+m**n*/
-       bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest+tmp**(2*n) */
+       bigint_word_t  tmp1_b[n*2+2];
+       bigint_t tmp1, tmp2;
+       tmp1.wordv = tmp1_b;
+    tmp2.wordv = &tmp1_b[n+1];
+
+       bigint_add_u(&tmp2,  &xh,   &xl);                            /* 2: tmp2 <= xh + xl     */
+       bigint_add_u(&tmp1, &yh,   &yl);                            /* 3: tmp1 <= yh + yl     */
+       bigint_mul_u(dest,  &tmp2, &tmp1);                          /* 4: dest <= tmp2 * tmp1  */
+       bigint_mul_u(&tmp1, &xh,   &yh);                            /* 5: tmp1 <= xh * yh     */
+    bigint_sub_u(dest,  dest,  &tmp1);                          /* 7: dest <= dest - tmp1       */
+    bigint_word_t tmp3_b[2*n];
+    tmp2.wordv = tmp3_b;
+    bigint_mul_u(&tmp2, &xl,   &yl);                            /* 1: tmp3 <= xl * yl     */
+       bigint_sub_u(dest,  dest,  &tmp2);                          /* 6: dest <= dest - tmp3    */
+    bigint_shiftleft(dest, n * sizeof(bigint_word_t) * 8);
+       bigint_add_u(dest, dest, &tmp2);                            /* 8: dest <= tmp3 + dest ** n       */
+       bigint_add_scale_u(dest, &tmp1, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest + tmp1 ** (2 * n) */
+
+#if 0
+       bigint_mul_u(dest,  &xl,   &yl);                            /* 1: dest <= xl * yl     */
+       bigint_add_u(&tmp2, &xh,   &xl);                            /* 2: tmp2 <= xh + xl     */
+       bigint_add_u(&tmp1, &yh,   &yl);                            /* 3: tmp1 <= yh + yl     */
+       bigint_mul_u(&tmp3, &tmp2, &tmp1);                          /* 4: tmp3 <= tmp2 * tmp1  */
+       bigint_mul_u(&tmp1, &xh,   &yh);                            /* 5: h    <= xh * yh     */
+       bigint_sub_u(&tmp3, &tmp3, dest);                           /* 6: tmp3 <= tmp3 - dest    */
+    bigint_sub_u(&tmp3, &tmp3, &tmp1);                          /* 7: tmp3 <= tmp3 - h       */
+       bigint_add_scale_u(dest, &tmp3, n*sizeof(bigint_word_t));   /* 8: dest <= dest + tmp3 ** n       */
+       bigint_add_scale_u(dest, &tmp1, 2*n*sizeof(bigint_word_t)); /* 9: dest <= dest + tmp1 ** (2 * n) */
+#endif
 }
 
 /******************************************************************************/
@@ -593,36 +669,35 @@ void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b){
 /* square */
 /* (xh*b^n+xl)^2 = xh^2*b^2n + 2*xh*xl*b^n + xl^2 */
 void bigint_square(bigint_t* dest, const bigint_t* a){
-       if(a->length_B*sizeof(bigint_word_t)<=4){
-               uint64_t r=0;
-               memcpy(&r, a->wordv, a->length_B*sizeof(bigint_word_t));
-               r = r*r;
-               memcpy(dest->wordv, &r, 2*a->length_B*sizeof(bigint_word_t));
+       if(a->length_B * sizeof(bigint_word_t) <= 4){
+               uint64_t r = 0;
+               memcpy(&r, a->wordv, a->length_B * sizeof(bigint_word_t));
+               r = r * r;
+               memcpy(dest->wordv, &r, 2 * a->length_B*sizeof(bigint_word_t));
                SET_POS(dest);
-               dest->length_B=2*a->length_B;
+               dest->length_B = 2 * a->length_B;
                bigint_adjust(dest);
                return;
        }
        if(dest==a){
                bigint_t d;
-               bigint_word_t d_b[a->length_B*2];
+               bigint_word_t d_b[a->length_B * 2];
                d.wordv = d_b;
                bigint_square(&d, a);
                bigint_copy(dest, &d);
                return;
        }
        uint16_t n;
-       n=(a->length_B+1)/2;
+       n = (a->length_B + 1) / 2;
        bigint_t xh, xl, tmp; /* x-high, x-low, temp */
-       bigint_word_t buffer[2*n+1];
+       bigint_word_t buffer[2 * n + 1];
        xl.wordv = a->wordv;
        xl.length_B = n;
        xl.info = 0;
        xh.wordv = &(a->wordv[n]);
-       xh.length_B = a->length_B-n;
-       xh.info = 0;
+       xh.length_B = a->length_B - n;
+       xh.info = a->info;
        bigint_adjust(&xl);
-       bigint_adjust(&xh);
        tmp.wordv = buffer;
 /* (xh * b**n + xl)**2 = xh**2 * b**2n + 2 * xh * xl * b**n + xl**2 */
 
@@ -632,7 +707,7 @@ void bigint_square(bigint_t* dest, const bigint_t* a){
 //     cli_putstr("\r\nDBG (1): xl**2: "); bigint_print_hex(dest);
        bigint_square(&tmp, &xh);
 //     cli_putstr("\r\nDBG (2): xh**2: "); bigint_print_hex(&tmp);
-       bigint_add_scale_u(dest, &tmp, 2*n*sizeof(bigint_word_t));
+       bigint_add_scale_u(dest, &tmp, 2 * n * sizeof(bigint_word_t));
 //     cli_putstr("\r\nDBG (3): xl**2 + xh**2*n**2: "); bigint_print_hex(dest);
        bigint_mul_u(&tmp, &xl, &xh);
 //     cli_putstr("\r\nDBG (4): xl*xh: "); bigint_print_hex(&tmp);
@@ -660,11 +735,14 @@ void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale){
        bigint_copy(&tmp, b);
        bigint_shiftleft(&tmp, bitscale % BIGINT_WORD_SIZE);
 
+//     cli_putstr_P(PSTR("\r\nDBG: shifted value: ")); bigint_print_hex(&tmp);
+
        x.info = a->info;
        x.wordv = &(a->wordv[word_shift]);
        x.length_B = a->length_B - word_shift;
 
        bigint_sub_u(&x, &x, &tmp);
+       a->length_B = x.length_B + word_shift;
        bigint_adjust(a);
        return;
 }
@@ -712,14 +790,16 @@ void bigint_reduce(bigint_t* a, const bigint_t* r){
        }
        while((GET_FBS(a) > rfbs) && (a->length_B == r->length_B)){
                shift = GET_FBS(a)-rfbs-1;
+//             cli_putstr("\r\nDBG: (2a) = "); bigint_print_hex(a);
 //             cli_putstr("\r\nDBG: (q) shift = "); cli_hexdump_rev(&shift, 2);
                bigint_sub_u_bitscale(a, r, shift);
-//             cli_putstr("\r\nDBG: (2) = "); bigint_print_hex(a);
+//             cli_putstr("\r\nDBG: (2b) = "); bigint_print_hex(a);
        }
        while(bigint_cmp_u(a,r)>=0){
                bigint_sub_u(a,a,r);
 //             cli_putstr("\r\nDBG: (3) = "); bigint_print_hex(a);
        }
+//     cli_putc(' ');
        bigint_adjust(a);
 //     cli_putstr("\r\nDBG: (a) = "); bigint_print_hex(a);
 //     cli_putstr("\r\n");
@@ -932,7 +1012,7 @@ void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m){
 void bigint_changeendianess(bigint_t* a){
        uint8_t t, *p, *q;
        p = (uint8_t*)(a->wordv);
-       q = ((uint8_t*)p)+a->length_B*sizeof(bigint_word_t)-1;
+       q = p + a->length_B * sizeof(bigint_word_t) - 1;
        while(p<q){
                t = *p;
                *p = *q;
index 1256079aec8b2c91d0e7e9f2875379d70427bb75..2acfdaa5877fdd41db9c94fb5d03330ab6d3864b 100644 (file)
@@ -33,7 +33,6 @@
 typedef uint8_t bigint_word_t;
 typedef uint16_t bigint_wordplus_t;
 typedef int16_t  bigint_wordplus_signed_t;
-typedef uint16_t bigint_ptr_int_t; /* this must be an integer of the size of a pointer for the target architecture */
 #define BIGINT_WORD_SIZE 8
 
 #define BIGINT_FBS_MASK (BIGINT_WORD_SIZE-1) /* the last five bits indicate which is the first bit set */
@@ -49,10 +48,10 @@ typedef struct{
 /******************************************************************************/
 
 void   bigint_adjust(bigint_t* a);
-uint32_t bigint_get_first_set_bit(bigint_t* a);
-uint32_t bigint_get_last_set_bit(bigint_t* a);
-uint16_t bigint_length_b(bigint_t* a);
-uint16_t bigint_length_B(bigint_t* a);
+uint32_t bigint_get_first_set_bit(const bigint_t* a);
+uint32_t bigint_get_last_set_bit(const bigint_t* a);
+uint16_t bigint_length_b(const bigint_t* a);
+uint16_t bigint_length_B(const bigint_t* a);
 void   bigint_copy(bigint_t* dest, const bigint_t* src);
 void   bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
 void   bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale);
index 56d90acc62a352b3b329dffb1280f58ecaf13abd..3ac88722ca814eb8f870e8157d96bcdc79be1e1b 100644 (file)
@@ -74,17 +74,17 @@ void hmac_sha1_nextBlock(hmac_sha1_ctx_t *s, const void* block){
 }
 void hmac_sha1_lastBlock(hmac_sha1_ctx_t *s, const void* block, uint16_t length_b){
        while(length_b>=SHA1_BLOCK_BITS){
-               sha1_nextBlock(&(s->a), block);
+               sha1_nextBlock(&s->a, block);
                block = (uint8_t*)block + SHA1_BLOCK_BYTES;
                length_b -= SHA1_BLOCK_BITS;
        }
-       sha1_lastBlock(&(s->a), block, length_b);
+       sha1_lastBlock(&s->a, block, length_b);
 }
 
 void hmac_sha1_final(void* dest, hmac_sha1_ctx_t *s){
-       sha1_ctx2hash((sha1_hash_t*)dest, &(s->a));
-       sha1_lastBlock(&(s->b), dest, SHA1_HASH_BITS);
-       sha1_ctx2hash((sha1_hash_t*)dest, &(s->b));
+       sha1_ctx2hash(dest, &s->a);
+       sha1_lastBlock(&s->b, dest, SHA1_HASH_BITS);
+       sha1_ctx2hash(dest, &(s->b));
 }
 
 #endif
index d942b3748737cc6ca5bbdd8f76a6f67507a1487c..25a9f0c9ea1c4159aaaa2330f380c61ec09de5fd 100644 (file)
@@ -412,6 +412,7 @@ end
 
 def expmod_test(a,b,c)
   begin
+    printf("[testing] expmod(%#x, %#x, %#x)\n",a,b,c) if $debug
     line = $sp.gets()
     line = "" if line==nil
     puts("DBG got: "+line) if $debug
@@ -848,7 +849,7 @@ if File.exists?(logfilename)
 end
 $logfile = File.open(logfilename, 'w')
 printf("logfile: %s\n", logfilename)
-
+$logfile.sync = true
 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
index df16955b5d7a65c3075f7283171491314d0d3c26..3c88cfd524531b4b7aec24debf967b14137aceaf 100644 (file)
@@ -22,6 +22,7 @@ require 'rubygems'
 require 'serialport'
 
 $debug = false
+$progress_dots = false
 
 def read_line(error_msg=true)
   begin
@@ -34,7 +35,7 @@ def read_line(error_msg=true)
       puts("ERROR: read timeout!\n") if error_msg
       return nil
     end
-  putc('.') if s.include?(6.chr)       
+  putc('.') if s.include?(6.chr) && $progress_dots     
   puts(s.inspect) if $debug
   end while s == 6.chr
   s.gsub(/\006/, '')
@@ -124,7 +125,7 @@ puts("\nPort: "+ARGV[0]+ "@"+ARGV[1]+" "+ARGV[2]+"N"+ARGV[3]+"\n");
 $linewidth = 16
 $sp = SerialPort.new(ARGV[0], ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, SerialPort::NONE);
 $sp.read_timeout=1000; # 1 second
-$extended_wait=10;
+$extended_wait=100000;
 $sp.write(command);
 
 if(readTestVector(param)==false)
index d2ed46ad43c6d68fd258d487b0cd15bd494fdc03..cea8925e31438c8295f1b01ab5c89279d0677d13 100644 (file)
@@ -18,6 +18,8 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 =end
 
+$debug = true
+
 def skip_header(file)
   begin
     l = file.gets().strip
@@ -89,8 +91,8 @@ def compare(fname1, fname2)
        end
        if(a!=b and a!=nil and b!=nil)
          $error += 1
-#        puts("a key: "+a[0]+" value: "+a[1])
-#        puts("b key: "+b[0]+" value: "+b[1])
+         puts("a key: "+a[0]+" value: "+a[1]) if $debug
+         puts("b key: "+b[0]+" value: "+b[1]) if $debug
        end
   end until a==nil or b==nil
 end
index 0c697530a89dfdceea72b1dfb429425a0df49441..49e0919e145d0b266bffbcde70ee095d13762299 100644 (file)
@@ -30,7 +30,7 @@
 #include "cli.h"
 #endif
 
-void rsa_enc(bigint_t* data, rsa_publickey_t* key){
+void rsa_enc(bigint_t* data, const rsa_publickey_t* key){
 /*
        cli_putstr_P(PSTR("\r\n -->rsa_enc()\r\n m = "));
        bigint_print_hex(data);
@@ -50,10 +50,10 @@ h = (m1 - m2) * qinv % p
 m = m2 + q * h
 */
 
-uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){
+uint8_t rsa_dec_crt_mono(bigint_t* data, const rsa_privatekey_t* key){
        bigint_t m1, m2;
-       m1.wordv = malloc((key->components[0].length_B + 1) * sizeof(bigint_word_t));
-       m2.wordv = malloc((key->components[1].length_B + 1) * sizeof(bigint_word_t));
+       m1.wordv = malloc((key->components[0].length_B /* + 1 */) * sizeof(bigint_word_t));
+       m2.wordv = malloc((key->components[1].length_B /* + 1 */) * sizeof(bigint_word_t));
        if(!m1.wordv || !m2.wordv){
 #if DEBUG
                cli_putstr_P(PSTR("\r\nERROR: OOM!"));
@@ -160,7 +160,7 @@ uint8_t rsa_dec_crt_mono(bigint_t* data, rsa_privatekey_t* key){
        return 0;
 }
 
-uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key){
+uint8_t rsa_dec(bigint_t* data, const rsa_privatekey_t* key){
        if(key->n == 1){
                bigint_expmod_u(data, data, &(key->components[0]), &key->modulus);
                return 0;
index 54854a6db60c980e89b8e3b09de23136da3d98c9..3146540235ce6c3c23ef7142776084571eaaa150 100644 (file)
@@ -40,8 +40,8 @@ typedef struct {
 } rsa_fullkey_t;
 
 
-void rsa_enc(bigint_t* data, rsa_publickey_t* key);
-uint8_t rsa_dec(bigint_t* data, rsa_privatekey_t* key);
+void    rsa_enc(bigint_t* data, const rsa_publickey_t* key);
+uint8_t rsa_dec(bigint_t* data, const rsa_privatekey_t* key);
 void rsa_os2ip(bigint_t* dest, const void* data, uint32_t length_B);
 void rsa_i2osp(void* dest, bigint_t* src, uint16_t* out_length_B);
 
index df448d1d886ad3c46f95a9078e53fe0d393f5ad2..2e22b8e5e45c04c73ea895ca1df53caa0440c33b 100644 (file)
@@ -207,7 +207,7 @@ void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){
 
 /********************************************************************************************************/
 
-void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state){
+void sha1_ctx2hash (void *dest, sha1_ctx_t *state){
 #if defined LITTLE_ENDIAN
        uint8_t i;
        for(i=0; i<5; ++i){
@@ -226,7 +226,7 @@ void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state){
  *
  *
  */
-void sha1 (sha1_hash_t *dest, const void* msg, uint32_t length){
+void sha1 (void *dest, const void* msg, uint32_t length){
        sha1_ctx_t s;
        DEBUG_S("\r\nBLA BLUB");
        sha1_init(&s);
index 6675d20a19182997addf152717d2f5dd44b19974..19667866563b8444b2cc6c9695333dbcdddcbb08 100644 (file)
@@ -65,7 +65,9 @@ typedef struct {
  * \brief hash value type
  * A variable of this type may hold a SHA-1 hash value 
  */
+/*
 typedef uint8_t sha1_hash_t[SHA1_HASH_BITS/8];
+*/
 
 /** \fn sha1_init(sha1_ctx_t *state)
  * \brief initializes a SHA-1 context
@@ -100,7 +102,7 @@ void sha1_lastBlock (sha1_ctx_t *state, const void* block, uint16_t length_b);
  * \param dest pointer to the hash value destination
  * \param state pointer to the hash context
  */ 
-void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state);
+void sha1_ctx2hash (void *dest, sha1_ctx_t *state);
 
 /** \fn sha1(sha1_hash_t *dest, const void* msg, uint32_t length_b)
  * \brief hashing a message which in located entirely in RAM
@@ -110,7 +112,7 @@ void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state);
  * \param msg  pointer to the message which should be hashed
  * \param length_b length of the message in bits
  */ 
-void sha1(sha1_hash_t *dest, const void* msg, uint32_t length_b);
+void sha1(void *dest, const void* msg, uint32_t length_b);
 
 
 
index 634f18d3d42e1375d897f2353db9de57bfccf4d0..87e5d456d52690269623fe3246ba751cbc527937 100644 (file)
@@ -43,7 +43,7 @@ void shacal1_enc(void* buffer, void* key, uint16_t keysize_b){
        memcpy(keybuffer, key, (keysize_b+7)/8);
        
        memcpy(t_ctx.h, buffer, SHA1_HASH_BITS/8);
-       sha1_ctx2hash((sha1_hash_t*)(&(ctx.h[0])), &t_ctx);
+       sha1_ctx2hash(&ctx.h[0], &t_ctx);
        memcpy(t_ctx.h, ctx.h, SHA1_HASH_BITS/8);
        sha1_nextBlock(&ctx, keybuffer);
        for(i=0; i<5; ++i)
index f76cebee45ee66a7fd9256171cb82e5706b54a9e..199c86a38327cf38292f840b9d48ac9318b1c738 100644 (file)
@@ -287,6 +287,7 @@ circularbytebuffer_append:
        ldd r23, Z+BUFFER_SIZE_OFFSET
        cp r22, r23
        brne 10f
+5:
        ldi r24, 1
        ret
 10:
@@ -343,14 +344,15 @@ circularbytebuffer_push:
        ldd r22, Z+FILLCOUNT_OFFSET
        ldd r23, Z+BUFFER_SIZE_OFFSET
        cp r22, r23
-       brne 10f
-       ldi r24, 1
-       ret
+       brlo 10f
+       rjmp 5b
+;      ldi r24, 1
+;      ret
 10:
-       clt
-       tst r22
-       brne 11f
-       set
+;      clt
+;      tst r22
+;      brne 11f
+;      set
 11:    
        inc r22
        std Z+FILLCOUNT_OFFSET, r22
@@ -368,7 +370,7 @@ circularbytebuffer_push:
 20:
        std Z+HEAD_OFFSET, r26
        std Z+HEAD_OFFSET+1, r27
-       brtc 30b
-       std Z+TAIL_OFFSET, r26
-       std Z+TAIL_OFFSET+1, r27
+;      brtc 30b
+;      std Z+TAIL_OFFSET, r26
+;      std Z+TAIL_OFFSET+1, r27
        rjmp 30b
index d1e40e4c8dca70663925e8ff597af4cf82a7120f..9f2900328dfde17a497c214d17288db58473e0dd 100644 (file)
 /**
  * \file     circularbytebuffer.h
  * \email    daniel.otte@rub.de
- * \author   Daniel Otte 
+ * \author   Daniel Otte
  * \date     2009-07-24
  * \license  GPLv3 or later
- * \ingroup  circularbytebuffer
+ * \addtogroup  circularbytebuffer
  * \brief    declaration for circular byte buffer
  */
+/*@{*/
 #ifndef CIRCULARBYTEBUFFER_H_
 #define CIRCULARBYTEBUFFER_H_
 
 #include <stdint.h>
 #include <stdlib.h>
 #include "config.h"
+ /**
+  * \brief type holding the managment information for the buffer
+  *
+  * A variable of this type may hold all the information to control the buffer
+  */
  typedef struct {
-        uint8_t buffer_size;
-        uint8_t fillcount;
-        uint8_t* buffer;
-        uint8_t* head;
-        uint8_t* tail;
-        uint8_t* top;
+        uint8_t buffer_size; /**< holds the amount of bytes which may be stored in the buffer */
+        uint8_t fillcount; /**< holds the amount of bytes actually stored in the buffer */
+        uint8_t* buffer; /**< pointer to the actual  buffer */
+        uint8_t* head; /**< pointer to the head of the buffer */
+        uint8_t* tail; /**< pointer to the tail of the buffer */
+        uint8_t* top; /**< pointer to the last free address in the buffer */
 } circularbytebuffer_t;
 
 
 #if CIRCULARBYTEBUFFER_NO_MALLOC==0
+/** \brief buffer initialisation with automatic allocation
+ *
+ * This function initializes the given buffer context and allocates memory for
+ * it by calling malloc.
+ * \param buffersize size of the buffer to allocate
+ * \param cb buffer context to be initialized
+ */
 uint8_t circularbytebuffer_init(uint8_t buffersize, circularbytebuffer_t* cb);
 #endif
 #if CIRCULARBYTEBUFFER_NO_INIT2==0
+/** \brief buffer initialisation without automatic allocation
+ *
+ * This function initializes the given buffer context and uses the given buffer
+ * for storage, so no malloc is needed.
+ * \param buffersize size of the buffer
+ * \param cb buffer context to be initialized
+ * \param buffer buffer for the storage of data (you are responisble for allocation and freeing)
+ */
 void    circularbytebuffer_init2(uint8_t buffersize, circularbytebuffer_t* cb, void* buffer);
 #endif
+/** \brief
+ *
+ *
+ */
 uint16_t circularbytebuffer_get_lifo(circularbytebuffer_t* cb);
 uint16_t circularbytebuffer_get_fifo(circularbytebuffer_t* cb);
 uint8_t circularbytebuffer_append(uint8_t, circularbytebuffer_t* cb);
@@ -56,4 +79,5 @@ uint8_t circularbytebuffer_push(uint8_t, circularbytebuffer_t* cb);
 uint8_t circularbytebuffer_cnt(circularbytebuffer_t* cb);
 void circularbytebuffer_free(circularbytebuffer_t* cb);
 
+/*@}*/
 #endif /* CIRCULARBYTEBUFFER_H_ */
index ce151b9cc4773a5b6d4cfdfb09f01450cf6eb462..032f59b0f6662928dc7c24d550fffc00cffe5003 100644 (file)
@@ -142,11 +142,9 @@ LENG_1 = 17
 
 .global cli_hexdump_block
 cli_hexdump_block:
-       tst r22
-       brne 1f
-       tst r23
-       brne 1f
-       ret
+       movw r26, r22
+       adiw r26, 0
+       breq simple_ret
 1:
        push WIDTH
        push INDENT
@@ -160,16 +158,16 @@ cli_hexdump_block:
        movw DATA_0, r24
        movw LENG_0, r22
 2:
-       clr r25
+;      clr r25
        ldi r24, '\r'
        rcall cli_putc
-       clr r25
+;      clr r25
        ldi r24, '\n'
        rcall cli_putc
        mov r4, INDENT
        tst r4
        breq 4f
-3:     clr r25
+3:;    clr r25
        ldi r24, ' '
        rcall cli_putc
        dec r4
@@ -181,8 +179,7 @@ cli_hexdump_block:
        tst LENG_1
        brne 7f
        cp WIDTH, LENG_0
-       breq 6f
-       brcs 7f
+       brlo 7f
        mov r22, LENG_0
 6:     inc r4
 7:
@@ -200,6 +197,7 @@ cli_hexdump_block:
        pop DATA_0
        pop INDENT
        pop WIDTH
+simple_ret:
        ret
 
 
index e7b20713430094a63bd26d6bd27606d2560b50cc..71d7bef3a479531345d8f9bd0905f07755e2c3f3 100644 (file)
  * AES test-suit
  *
 */
-
-#include "config.h"
-
-#include "uart_i.h"
-#include "debug.h"
-
+#include "main-test-common.h"
 #include "aes.h"
 
-#include "cli.h"
 #include "performance_test.h"
 #include "dump.h"
 
 #include "bcal-performance.h"
 #include "bcal-nessie.h"
 
-#include <stdint.h>
-#include <string.h>
-#include <stdlib.h>
-#include <avr/pgmspace.h>
-
 const char* algo_name = "AES";
 
 const bcdesc_t* const const algolist[] PROGMEM = {
@@ -718,4 +707,16 @@ int main (void){
                cmd_interface(cmdlist);
        }
 }
+int main(void) {
+       main_setup();
+
+       cmacvs_algolist=(bcdesc_t**)algolist;
+       cmacvs_algo=(bcdesc_t*)&aes128_desc;
+
+       for(;;){
+               welcome_msg(algo_name);
+               cmd_interface(cmdlist);
+    }
+
+}
 
index 9228188d97012cfd0fd809eed01c3c2a34f977df..30801f8a6052a3bbe908a8ca296e6d2eac7cb281 100644 (file)
  * 
 */
 
-#include "config.h"
 
-#include "uart_i.h"
-#include "debug.h"
+#include "main-test-common.h"
 
 #include <arcfour.h>
-#include "cli.h"
 #include "performance_test.h"
 
 #include "scal_arcfour.h"
 #include "scal-basic.h"
 #include "scal-nessie.h"
 
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-
 char* algo_name = "Arcfour";
 
 /*****************************************************************************
@@ -97,16 +90,17 @@ const cmdlist_entry_t cmdlist[] PROGMEM = {
        { NULL,            NULL, NULL}
 };
 
-int main (void){
-       DEBUG_INIT();
-       
-       cli_rx = (cli_rx_fpt)uart0_getc;
-       cli_tx = (cli_tx_fpt)uart0_putc;                
+int main(void) {
+       main_setup();
+
+       shavs_algolist=(hfdesc_t**)algolist;
+       shavs_algo=(hfdesc_t*)&sha256_desc;
+
        for(;;){
-               cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
-               cli_putstr(algo_name);
-               cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
+               welcome_msg(algo_name);
                cmd_interface(cmdlist);
-       }
+    }
+
 }
 
+
index c1ef54f48d931a92a3d71541099a927be09978fc..d85a8303d656b77295d851ec711a66ac3680bdf9 100644 (file)
  * 
 */
 
-#include "config.h"
-
-#include "uart_i.h"
-#include "debug.h"
+#include "main-test-common.h"
 
 #include "des.h"
-#include "cli.h"
 #include "performance_test.h"
 #include "bcal-performance.h"
 #include "bcal-nessie.h"
 #include "bcal_tdes.h"
 #include "bcal_tdes2.h"
 
-#include <stdint.h>
-#include <string.h>
-#include <stdlib.h>
-
 char* algo_name = "DES";
 
 const bcdesc_t* const algolist[] PROGMEM = {
@@ -51,8 +43,31 @@ const bcdesc_t* const algolist[] PROGMEM = {
  *  additional validation-functions                                                                                     *
  *****************************************************************************/
 
-void testrun_nessie_des(void){
-       bcal_nessie_multiple(algolist);
+void testrun_nessie_des(const char* param){
+       if(!param){
+               bcal_nessie_multiple(algolist);
+       }else{
+               uint8_t i=0;
+               bcdesc_t* ptr;
+               for(;;){
+                       ptr = (bcdesc_t*)pgm_read_word(&algolist[i++]);
+                       if(ptr == NULL){
+                               cli_putstr_P(PSTR("\r\nunknown algorithm: "));
+                               cli_putstr(param);
+                               cli_putstr_P(PSTR("\r\navailable algorithms are:"));
+                               i = 0;
+                               while((ptr = (bcdesc_t*)pgm_read_word(&algolist[i++]))){
+                                       cli_putstr_P(PSTR("\r\n\t"));
+                                       cli_putstr_P((const char*)pgm_read_word(&ptr->name));
+                               }
+                               return;
+                       }
+                       if(!strcmp_P(param, (const char*)pgm_read_word(&ptr->name))){
+                               bcal_nessie(ptr);
+                               return;
+                       }
+               }
+       }
 }
 
 void testrun_performance_des(void){
@@ -68,22 +83,20 @@ const char performance_str[] PROGMEM = "performance";
 const char echo_str[]        PROGMEM = "echo";
 
 const cmdlist_entry_t cmdlist[] PROGMEM = {
-       { nessie_str,      NULL, testrun_nessie_des },
-       { test_str,        NULL, testrun_nessie_des },
+       { nessie_str,  (void*)1, (void_fpt)testrun_nessie_des },
+       { test_str,    (void*)1, (void_fpt)testrun_nessie_des },
        { performance_str, NULL, testrun_performance_des},
        { echo_str,    (void*)1, (void_fpt)echo_ctrl},
        { NULL,            NULL, NULL}
 };
 
-int main (void){
-       DEBUG_INIT();
-       
-       cli_rx = (cli_rx_fpt)uart0_getc;
-       cli_tx = (cli_tx_fpt)uart0_putc;                
+int main(void) {
+       main_setup();
+
        for(;;){
-               cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
-               cli_putstr(algo_name);
-               cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
+               welcome_msg(algo_name);
                cmd_interface(cmdlist);
-       }
+    }
+
 }
+
index 442e14237d62f1954c13ba50abfdf630abd8def1..3a08c120fe0efa2b8522eec8966c3f0a7807a92f 100644 (file)
  * 
 */
 
-#include "config.h"
-
-#include "uart_i.h"
-#include "debug.h"
+#include "main-test-common.h"
 
 #include <present80.h>
 #include <present128.h>
-#include "cli.h"
 #include "performance_test.h"
 #include "bcal-performance.h"
 #include "bcal-nessie.h"
 #include "bcal_present80.h"
 #include "bcal_present128.h"
 
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-
 char* algo_name = "Present";
 
 const bcdesc_t* const algolist[] PROGMEM = {
@@ -146,15 +138,15 @@ const cmdlist_entry_t cmdlist[] PROGMEM = {
        { NULL,            NULL, NULL}
 };
 
-int main (void){
-       DEBUG_INIT();
-       
-       cli_rx = (cli_rx_fpt)uart0_getc;
-       cli_tx = (cli_tx_fpt)uart0_putc;                
+int main(void) {
+       main_setup();
+
+       shavs_algolist=(hfdesc_t**)algolist;
+       shavs_algo=(hfdesc_t*)&sha256_desc;
+
        for(;;){
-               cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
-               cli_putstr(algo_name);
-               cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
+               welcome_msg(algo_name);
                cmd_interface(cmdlist);
-       }
+    }
+
 }
index 26cb449697d6c7c6bed47601c8ef90e954539b73..53a6c22af97b81137e80a55c4624e2c2c48acde3 100644 (file)
@@ -531,12 +531,15 @@ void quick_test(void){
        uint8_t *ciphertext, *plaintext, rc;
        uint8_t seed[sizeof(SEED)];
        uint16_t clen, plen;
+       if(!keys_allocated){
+               load_fix_rsa();
+       }
        ciphertext = malloc(clen = pub_key.modulus.length_B * sizeof(bigint_word_t));
        plaintext = malloc(pub_key.modulus.length_B * sizeof(bigint_word_t));
        memcpy_P(plaintext, MSG, sizeof(MSG));
        memcpy_P(seed, SEED, sizeof(SEED));
        cli_putstr_P(PSTR("\r\nplaintext:"));
-       cli_hexdump_block(plaintext, sizeof(MSG), 4, 8);
+       cli_hexdump_block(plaintext, sizeof(MSG), 4, 16);
        cli_putstr_P(PSTR("\r\nencrypting: ..."));
        rc = rsa_encrypt_oaep(ciphertext, &clen, plaintext, sizeof(MSG), &pub_key, NULL, NULL, seed);
        if(rc){
index a5a3a88e93863cd2af028d3efad44f697286c937..00f12ed5a88f5c75e1d611c7cb0b19847f94549d 100644 (file)
@@ -32,7 +32,7 @@
 
 #include "performance_test.h"
 
-#define DEBUG 0
+#define DEBUG 1
 
 const char* algo_name = "RSAES-PKCS1V15";
 
@@ -663,7 +663,7 @@ void run_seed_test(void){
                cli_putstr_P(PSTR("\r\nERROR: OOM!"));
                return;
        }
-       msg_ = malloc(bigint_length_B(&pub_key.modulus) + sizeof(bigint_word_t));
+       msg_ = malloc(bigint_length_B(&pub_key.modulus) /* + sizeof(bigint_word_t) */ );
 #if DEBUG
        cli_putstr_P(PSTR("\r\nDBG: @msg_: 0x"));
        cli_hexdump_rev(&msg_, 2);
index e3f68f5d3c0010aeeb41d0473d8936d230cdd6ea..48de3cf327ab1cd9bace25b7dd92039460681b7c 100644 (file)
@@ -61,6 +61,54 @@ void testrun_performance_skipjack(void){
        bcal_performance_multiple(algolist);
 }
 
+int test_enc(const void* buffer, void* key){
+       uint8_t data[8];
+       int r;
+       memcpy(data, buffer, 8);
+       skipjack_enc(data, key);
+       cli_putstr_P(PSTR("  key = "));
+       cli_hexdump(key, 10);
+       cli_putstr_P(PSTR(" plaintext = "));
+       cli_hexdump(buffer, 8);
+       cli_putstr_P(PSTR(" ciphertext = "));
+       cli_hexdump(data, 8);
+       skipjack_dec(data, key);
+       r = memcmp(data, buffer, 8);
+       cli_putstr_P(PSTR(" decrypt: "));
+       if(r){
+               cli_putstr_P(PSTR("fail"));
+       }else{
+               cli_putstr_P(PSTR("ok"));
+       }
+       return r;
+}
+
+void testrun_nist_vectors(void){
+       uint8_t key[10];
+       uint8_t data[8];
+       uint8_t i;
+
+       cli_putstr_P(PSTR("\r\n\r\n=== NIST vectors run 1 ==="));
+       memset(key, 0, 10);
+       for(i=0; i<64; ++i){
+               memset(data, 0, 8);
+               data[i>>3] |= 0x80 >> (i & 7);
+               cli_putstr_P(PSTR("\r\n round: 0x"));
+               cli_hexdump_byte(i);
+               test_enc(data, key);
+       }
+
+       cli_putstr_P(PSTR("\r\n\r\n=== NIST vectors run 2 ==="));
+       memset(data, 0, 8);
+       for(i=0; i<80; ++i){
+                memset(key, 0, 10);
+                key[i>>3] |= 0x80 >> (i & 7);
+                cli_putstr_P(PSTR("\r\n round: 0x"));
+                cli_hexdump_byte(i);
+                test_enc(data, key);
+        }
+}
+
 /*****************************************************************************
  *  self tests                                                                                                                          *
  *****************************************************************************/
@@ -113,12 +161,14 @@ void testrun_skipjack(void){
 
 const char nessie_str[]      PROGMEM = "nessie";
 const char test_str[]        PROGMEM = "test";
+const char nist_str[]        PROGMEM = "nist";
 const char performance_str[] PROGMEM = "performance";
 const char echo_str[]        PROGMEM = "echo";
 
 const cmdlist_entry_t cmdlist[] PROGMEM = {
        { nessie_str,      NULL, testrun_nessie_skipjack},
        { test_str,        NULL, testrun_skipjack},
+       { nist_str,        NULL, testrun_nist_vectors},
        { performance_str, NULL, testrun_performance_skipjack},
        { echo_str,    (void*)1, (void_fpt)echo_ctrl},
        { NULL,            NULL, NULL}
index ebd9663830dd757ed6788659fee40e35af2294d7..be0aee64b554cf009b7d8d67ee3a38337569cd49 100644 (file)
@@ -294,10 +294,7 @@ uart0_putc:
        ldi r26, lo8(uart0_ctx+UART0_CBB_TX_OFFSET)
        ldi r27, hi8(uart0_ctx+UART0_CBB_TX_OFFSET)
 20:
-;      sei
        movw r24, r26
-;      nop
-;      nop
        cli
        rcall circularbytebuffer_cnt
        sei
@@ -308,9 +305,8 @@ uart0_putc:
        clr r25
        cli
        rcall circularbytebuffer_append
-       sei
        SET_BIT_IO UCSR0B, UDRIE0, r24
-       ret
+       reti
 
 /******************************************************************************/
 /*
@@ -364,19 +360,48 @@ uart0_putc:
        clr r1
        LOAD_IO r24, UDR0
 #if UART0_SWFLOWCTRL
-       ldi r26, lo8(uart0_ctx+UART0_TXON_OFFSET)
+    ldi r26, lo8(uart0_ctx+UART0_TXON_OFFSET)
        ldi r27, hi8(uart0_ctx+UART0_TXON_OFFSET)
        cpi r24, XON_VALUE
-       brne 10f
-       ldi r24, 1
-       st X, r24
-       rjmp 99f
-10:    
+       breq 11f
        cpi r24, XOFF_VALUE
-       brne 20f
+       brne 12f
        clr r24
-       st X, r24
+11:    st X, r24
        rjmp 99f
+12:
+       push r24
+/* now the "sending" part*/
+       ldi r24, lo8(uart0_ctx+UART0_CBB_RX_OFFSET)
+       ldi r25, hi8(uart0_ctx+UART0_CBB_RX_OFFSET)
+       rcall circularbytebuffer_cnt
+       ldi r30, lo8(uart0_ctx+UART0_RXON_OFFSET)
+       ldi r31, hi8(uart0_ctx+UART0_RXON_OFFSET)
+       ld r18, Z
+       tst r18
+       breq 15f/* branch if rxon inactive -> we had send an XOFF earlier */
+       /* ok, we did not have send an XOFF, should we? */
+       cpi r24, UART0_THRESH_HIGH
+       brlo 90f /* ok, nothing critical, go on */
+       st Z, r1
+       ldi r24, XOFF_VALUE
+;      sbi _SFR_IO_ADDR(PORTD), 5
+       rjmp 16f
+15:
+       cpi r24, UART0_THRESH_LOW
+       brsh 90f /* nothing has changed */
+       /* if we get here, we had send an XOFF and are now below threshold */
+       /* so we sen an XON */
+       ldi r24, XON_VALUE
+       cbi _SFR_IO_ADDR(PORTD), 5
+       st Z, r24
+16:
+       ldi r22, lo8(uart0_ctx+UART0_CBB_TX_OFFSET)
+       ldi r23, hi8(uart0_ctx+UART0_CBB_TX_OFFSET)
+       rcall circularbytebuffer_push
+       SET_BIT_IO UCSR0B, UDRIE0, r24
+90:
+       pop r24
 #endif /* UART0_SWFLOWCTRL */  
 20:    
 #if UART0_HOOK
@@ -409,33 +434,8 @@ uart0_putc:
        ldi r22, lo8(uart0_ctx+UART0_CBB_RX_OFFSET)
        ldi r23, hi8(uart0_ctx+UART0_CBB_RX_OFFSET)
        clr r25
+;      sbi _SFR_IO_ADDR(PORTD), 6
        rcall circularbytebuffer_append
-#if UART0_SWFLOWCTRL
-       ldi r24, lo8(uart0_ctx+UART0_CBB_RX_OFFSET)
-       ldi r25, hi8(uart0_ctx+UART0_CBB_RX_OFFSET)
-       rcall circularbytebuffer_cnt
-       ldi r22, lo8(uart0_ctx+UART0_CBB_TX_OFFSET)
-       ldi r23, hi8(uart0_ctx+UART0_CBB_TX_OFFSET)
-       ldi r30, lo8(uart0_ctx+UART0_RXON_OFFSET)
-       ldi r31, hi8(uart0_ctx+UART0_RXON_OFFSET)
-       ld r18, Z
-       tst r18
-       breq 60f
-       cpi r24, UART0_THRESH_HIGH+1
-       brlo 99f
-       clr r25
-       ldi r24, XOFF_VALUE
-       rcall circularbytebuffer_push
-       SET_BIT_IO UCSR0B, UDRIE0, r24
-       rjmp 99f
-60:
-       cpi r24, UART0_THRESH_LOW
-       brge 99f
-       clr r25
-       ldi r24, XON_VALUE
-       rcall circularbytebuffer_push
-       SET_BIT_IO UCSR0B, UDRIE0, r24
-#endif /* UART0_SWFLOWCTRL */
 99:    
        out _SFR_IO_ADDR(SREG), r16
        pop_range 16, 31
diff --git a/testvectors/skipjack_tv.pdf b/testvectors/skipjack_tv.pdf
new file mode 100644 (file)
index 0000000..8457c41
Binary files /dev/null and b/testvectors/skipjack_tv.pdf differ