avr-crypto-lib/main-sha1-test.c

92 lines
2.1 KiB
C

/*
* SHA-1 test-suit
*
*/
#include "config.h"
#include "serial-tools.h"
#include "uart.h"
#include "debug.h"
#include "sha1.h"
#include <stdint.h>
#include <string.h>
/*****************************************************************************
* additional validation-functions *
*****************************************************************************/
/*****************************************************************************
* self tests *
*****************************************************************************/
void sha1_ctx_dump(sha1_ctx_t *s){
uint8_t i;
uart_putstr("\r\n==== sha1_ctx_dump ====");
for(i=0;i<5;++i){
uart_putstr("\r\na["); uart_hexdump(&i, 1); uart_putstr("]: ");
uart_hexdump(&(s->h[i]), 4);
}
uart_putstr("\r\nlength"); uart_hexdump(&i, 8);
}
void testrun_sha1(void){
sha1_hash_t hash;
sha1(&hash,"abc",3*8);
uart_putstr("\r\nsha1(\"abc\") = \r\n\t");
uart_hexdump(hash,SHA1_HASH_BITS/8);
sha1(&hash,"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",448);
uart_putstr("\r\nsha1(\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\") = \r\n\t");
uart_hexdump(hash,SHA1_HASH_BITS/8);
uart_putstr("\r\nsha1(1,000,000 * 'a') = \r\n\t");
{
uint8_t block[SHA1_BLOCK_BITS/8];
uint16_t i;
sha1_ctx_t s;
memset(block,'a',SHA1_BLOCK_BITS/8);
sha1_init(&s);
for(i=0;i<15625; ++i){ /* (1000000/(SHA1_BLOCK_BITS/8)) */
sha1_nextBlock(&s, block);
}
sha1_lastBlock(&s,block,0);
sha1_ctx2hash(&hash, &s);
}
uart_hexdump(hash,SHA1_HASH_BITS/8);
uart_putstr("\r\nx");
}
/*****************************************************************************
* main *
*****************************************************************************/
int main (void){
char str[20];
DEBUG_INIT();
uart_putstr("\r\n");
uart_putstr("\r\n\r\nCrypto-VS (SHA-1)\r\nloaded and running\r\n");
restart:
while(1){
if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;}
if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
testrun_sha1();
goto restart;
continue;
error:
uart_putstr("ERROR\r\n");
}
}