コード例 #1
0
uint8_t read_bigint(bigint_t *a, char *prompt){
	uint16_t read_length, actual_length;
	uint8_t off;
	uint8_t *buffer;
	char read_int_str[18];
	cli_putstr(prompt);
	cli_putstr_P(PSTR("\r\n  length: "));
	cli_getsn(read_int_str, 16);
	read_length = own_atou(read_int_str);
	off = (sizeof(bigint_word_t) - (read_length % sizeof(bigint_word_t))) % sizeof(bigint_word_t);
	buffer = malloc(((read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t)) * sizeof(bigint_word_t));
	if(!buffer){
		cli_putstr_P(PSTR("\r\nERROR: OOM!"));
		return 2;
	}
	cli_putstr_P(PSTR("\r\n  data: "));
	memset(buffer, 0, sizeof(bigint_word_t));
	actual_length = read_os(buffer + off, read_length, NULL);
	if(actual_length != read_length){
		cli_putstr_P(PSTR("\r\nERROR: unexpected end of data!"));
		free(buffer);
		return 1;
	}
	a->wordv = (bigint_word_t*)buffer;
	a->length_W = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
	a->info = 0;
	bigint_changeendianess(a);
	bigint_adjust(a);
	return 0;
}
コード例 #2
0
ファイル: shavs.c プロジェクト: nerilex/avr-crypto-lib
void shavs_setalgo(char *param){
	param = strstrip(param);
	if(param[1]=='\0'){ /* single letter specified */
		uint8_t i, option = param[0] - 'a';

		if(!shavs_algolist){
			fputs_P(PSTR("\nERROR: shavs_algolist not set!"), shavs_out_file);
			return;
		}
		for(i=0; i<=option; ++i){
			if((void*)pgm_read_word(&(shavs_algolist[i]))==NULL){
				fputs_P(PSTR("\r\nERROR: invalid selection!"), shavs_out_file);
				return;
			}
		}
		shavs_algo=(hfdesc_t*)pgm_read_word(&(shavs_algolist[option]));
	} else { /* name specifyed */
		hfdesc_t *t=NULL;
		uint8_t i=0;
		while((t=(hfdesc_t*)pgm_read_word(&(shavs_algolist[i]))) &&
		       strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
			++i;
		if(t){
			shavs_algo=t;
		}else{
		    fprintf_P(shavs_out_file, PSTR("\nERROR: could not find \"%s\"!"), param);
			cli_putstr_P(PSTR("\r\nERROR: could not find \""));
			cli_putstr(param);
			cli_putstr_P(PSTR("\"!"));
		}
	}
}
コード例 #3
0
ファイル: main-des-test.c プロジェクト: Sergoi/avr-crypto-lib
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;
			}
		}
	}
}
コード例 #4
0
void testrun_serpent256(void){
	uint8_t key[32];
	uint8_t data[16];
	serpent_ctx_t ctx;
	memset(key, 0, 32);
	memset(data, 0, 16);
	key[0] = 0x80;
	cli_putstr("\r\n== small test Serpent-256 ==");
	cli_putstr("\r\n  key    = ");
	cli_hexdump(key, 32);
	cli_putstr("\r\n  plain  = ");
	cli_hexdump(data, 16);
	serpent_init(key, 256, &ctx);
	serpent_enc(data, &ctx);
	cli_putstr("\r\n  cipher = ");
	cli_hexdump(data, 16);
}
コード例 #5
0
void testrun_aes128_ctr(void){
	uint8_t key[16];
	uint8_t iv[16];
	uint8_t plain[64];

	bcal_ctr_ctx_t ctx;
	uint8_t r;

	memcpy(key,   modes_key,   16);
	memcpy(iv,    modes_ctriv, 16);
	memcpy(plain, modes_plain, 64);

	cli_putstr("\r\n** AES128-CTR-TEST **");
	r = bcal_ctr_init(&aes128_desc, key, 128, NULL, &ctx);
	cli_putstr("\r\n  init = 0x");
	cli_hexdump(&r, 1);
	cli_putstr("\r\n  key:   ");
	cli_hexdump(key, 128/8);
	cli_putstr("\r\n  IV:    ");
	cli_hexdump(iv, 128/8);
	cli_putstr("\r\n  plaintext:");
	cli_hexdump_block(plain, 4*128/8, 4, 16);
	if(r)
		return;
	bcal_ctr_encMsg(iv, plain, 4*128, &ctx);
	cli_putstr("\r\n  ciphertext:  ");
	cli_hexdump_block(plain, 4*128/8, 4, 16);
	bcal_ctr_decMsg(iv, plain, 4*128, &ctx);
	cli_putstr("\r\n  plaintext:   ");
	cli_hexdump_block(plain, 4*128/8, 4, 16);
	bcal_ctr_free(&ctx);
}
コード例 #6
0
void testrun_testkey_aes128(void){
	uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
	                    0x28, 0xae, 0xd2, 0xa6,
	                    0xab, 0xf7, 0x15, 0x88,
	                    0x09, 0xcf, 0x4f, 0x3c};
	aes128_ctx_t ctx;
	uint8_t i;
	aes128_init(key, &ctx);
	cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   ");
	cli_hexdump(key, 16);
	for(i=0; i<11; ++i){
		cli_putstr("\r\n index: ");
		cli_putc('0'+i/10);
		cli_putc('0'+i%10);
		cli_putstr(" roundkey ");
		cli_hexdump(ctx.key[i].ks, 16);
	}
}
コード例 #7
0
ファイル: cli_js.c プロジェクト: wosayttn/aos
static void tree(char *path)
{
    be_osal_dir_t *dir;
    be_osal_dirent_t *ent;
    char childpath[48] = {0};

    dir = be_osal_opendir(path);
    if (dir) {
        while ((ent = be_osal_readdir(dir)) != NULL) {
            sprintf(childpath, "%s %s\n", BE_CLI_REPLY, ent->d_name);
            cli_putstr(childpath);
        }
        be_osal_closedir(dir);
    }

    sprintf(childpath, "%s success\n", BE_CLI_REPLY);
    cli_putstr(childpath);
}
コード例 #8
0
ファイル: main-echo-test.c プロジェクト: EwanLu/chipwhisperer
int main (void){
	DEBUG_INIT();

	cli_rx = (cli_rx_fpt)uart0_getc;
	cli_tx = (cli_tx_fpt)uart0_putc;
	shavs_algolist=(hfdesc_t**)algolist;
	shavs_algo=(hfdesc_t*)&echo256_desc;
	for(;;){
		cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
		cli_putstr(algo_name);
		cli_putstr_P(PSTR("; "));
		cli_putstr(__DATE__);
		cli_putstr_P(PSTR(" "));
		cli_putstr(__TIME__);
		cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));

		cmd_interface(cmdlist);
	}
}
コード例 #9
0
ファイル: groestl_small.c プロジェクト: Astralix/ethernut32
 void dump_m(const uint8_t* m){
	 uint8_t i,j;
	 for(i=0; i<8; ++i){
		cli_putstr("\r\n");
		for(j=0; j<8; ++j){
			cli_putc(' ');
			cli_hexdump(m+8*i+j, 1);
		}
	 }
 }
コード例 #10
0
static
void printvalue(unsigned long v){
	char str[20];
	int i;
	ultoa(v, str, 10);
	for(i=0; i<10-strlen(str); ++i){
		cli_putc(' ');
	}
	cli_putstr(str);
}
コード例 #11
0
void crypto_test(void){
	uint8_t test_data[16], test_key[32];
	aes256_ctx_t ctx;
	memset(test_key, 0xA5, 32);
	memset(test_data, 0, 16);
	aes256_init(test_key, &ctx);
	aes256_enc(test_data, &ctx);
	cli_putstr("\r\ncrypto test data:\r\n");
	cli_hexdump_block(test_data, 16, 4, 8);
}
コード例 #12
0
void dsa_print_item(bigint_t* a, const char* pstr){
	uint8_t *p;
	cli_putstr("\r\n");
	cli_putstr(pstr);
	cli_putstr(": ");
	uint16_t i;
	p = (uint8_t*)a->wordv + a->length_W*sizeof(bigint_word_t) -1;
	for(i=0; i<a->length_W*sizeof(bigint_word_t)-1; ++i){
		if(i%16==0){
			cli_putstr("\r\n    ");
		}
		cli_hexdump(p, 1);
		cli_putc(':');
		--p;
	}
	if(i%16==0){
		cli_putstr("\r\n    ");
	}
	cli_hexdump(p, 1);
}
コード例 #13
0
int main (void){
	DEBUG_INIT();
	cli_rx = (cli_rx_fpt)uart0_getc;
	cli_tx = (cli_tx_fpt)uart0_putc;	 	
	for(;;){
		cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
		cli_putstr(algo_name);
		cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
		cmd_interface(cmdlist);
	}
}
コード例 #14
0
ファイル: groestl_small.c プロジェクト: Astralix/ethernut32
void groestl_small_ctx2hash(void* dest, const groestl_small_ctx_t* ctx, uint16_t outlength_b){
	uint8_t tmp[64];
	memcpy(tmp, ctx->h, 64);
	groestl_small_rounds(tmp, 0);
	memxor(tmp, ctx->h, 64);
#if DEBUG
	cli_putstr("\r\npost finalisation");
	dump_m(tmp);
#endif
	memcpy(dest, tmp+64-outlength_b/8, outlength_b/8);
}
コード例 #15
0
void testrun_cscipher(void){
	uint8_t data[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
	uint8_t key[] =  {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
					  0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
	cscipher_ctx_t ctx;
	cli_putstr("\r\n== CS-Cipher test==\r\nkey: ");
	cli_hexdump(key, 16);
	memset(&ctx, 0, 8*9);
	cscipher_init(key, &ctx);
	cli_putstr("\r\nround keys:\r\n");
	cli_hexdump_block(&ctx, 8*9, 4, 8);
	cli_putstr("\r\nplain:  ");
	cli_hexdump(data, 8);
	cscipher_enc(data, &ctx);
	cli_putstr("\r\ncipher: ");
	cli_hexdump(data, 8);
	cscipher_dec(data, &ctx);
	cli_putstr("\r\nplain:  ");
	cli_hexdump(data, 8);
}
void testrun_camellia128(void){
	uint8_t data[16];
	uint8_t data2[16];
	uint8_t key[16];
	char str[4];
	uint8_t i,j;
	str[3]= '\0';
	for(j=0; j<10; ++j){
		str[0] = ('0'+(j+1)/100);
		str[1] = ('0'+((j+1)/10)%10);
		str[2] = ('0'+(j+1)%10);
		memcpy_P(key, test_keys+j*16, 16);
		cli_putstr_P(PSTR("\r\nK No."));
		cli_putstr(str);
		cli_putstr_P(PSTR(" : "));
		hexdump128(key);
		camellia128_ctx_t ctx;
		camellia128_init(key, &ctx);
		for(i=0; i<128; ++i){
			memset(data, 0x00, 16);
			data[i/8] = 0x80>>i%8;
			memcpy(data2, data, 16);
			str[0] = ('0'+(i+1)/100);
			str[1] = ('0'+((i+1)/10)%10);
			str[2] = ('0'+(i+1)%10);
			cli_putstr_P(PSTR("\r\nP No."));
			cli_putstr(str);
			cli_putstr_P(PSTR(" : "));
			hexdump128(data);
			camellia128_enc(data, &ctx);
			cli_putstr_P(PSTR("\r\nC No."));
			cli_putstr(str);
			cli_putstr_P(PSTR(" : "));
			hexdump128(data);
			camellia128_dec(data, &ctx);
			if(memcmp(data, data2, 16)){
				cli_putstr_P(PSTR("\r\n DECRYPTION ERROR !!!"));
			}
		}
	}
}
コード例 #17
0
void testrun_testkey_aes192(void){
	uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7,
	                    0xda, 0x0e, 0x64, 0x52,
	                    0xc8, 0x10, 0xf3, 0x2b,
	                    0x80, 0x90, 0x79, 0xe5,
	                    0x62, 0xf8, 0xea, 0xd2,
	                    0x52, 0x2c, 0x6b, 0x7b};
	aes192_ctx_t ctx;
	uint8_t i;
	memset(&ctx, 0, sizeof(aes192_ctx_t));
	aes192_init(key, &ctx);
	cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   ");
	cli_hexdump(key, 24);
	for(i=0; i<13; ++i){
		cli_putstr("\r\n index: ");
		cli_putc('0'+i/10);
		cli_putc('0'+i%10);
		cli_putstr(" roundkey ");
		cli_hexdump(ctx.key[i].ks, 16);
	}
}
コード例 #18
0
void testrun_testkey_aes256(void){
	uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10,
	                    0x15, 0xca, 0x71, 0xbe,
	                    0x2b, 0x73, 0xae, 0xf0,
	                    0x85, 0x7d, 0x77, 0x81,
	                    0x1f, 0x35, 0x2c, 0x07,
	                    0x3b, 0x61, 0x08, 0xd7,
	                    0x2d, 0x98, 0x10, 0xa3,
	                    0x09, 0x14, 0xdf, 0xf4};
	aes256_ctx_t ctx;
	uint8_t i;
	memset(&ctx, 0, sizeof(aes256_ctx_t));
	aes256_init(key, &ctx);
	cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key:   ");
	cli_hexdump(key, 32);
	for(i=0; i<15; ++i){
		cli_putstr("\r\n index: ");
		cli_putc('0'+i/10);
		cli_putc('0'+i%10);
		cli_putstr(" roundkey ");
		cli_hexdump(ctx.key[i].ks, 16);
	}
}
int main (void){
	DEBUG_INIT();

	cli_rx = (cli_rx_fpt)uart0_getc;
	cli_tx = (cli_tx_fpt)uart0_putc;
	cmacvs_algolist=(bcdesc_t**)algolist;
	cmacvs_algo=(bcdesc_t*)&aes128_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"));
		cmd_interface(cmdlist);
	}
}
コード例 #20
0
void testrun_nessie2(void){
	uint16_t i;
	uint8_t j;
	uint8_t data[128], hash[64];
	whirlpool_ctx_t ctx;
	char str[8];
	memset(data, 0, 128);
	cli_putstr_P(PSTR("\r\nMessage digests of strings of 0-bits and length L:"));
	for(i=0; i<1024; ++i){
		cli_putstr_P(PSTR("\r\n    L = "));
		itoa(i, str, 10);
		j=4;
		j-= strlen(str);
		if(j>3){
			j=0;
		}
		while(j--){
			cli_putc(' ');
		}
		cli_putstr(str);
		cli_putstr_P(PSTR(": "));
		whirlpool_init(&ctx);
		whirlpool_lastBlock(&ctx, data, i);
		whirlpool_ctx2hash(hash, &ctx);
		cli_hexdump(hash, 64);
	}
	cli_putstr_P(PSTR("\r\nMessage digests of all 512-bit strings S containing a single 1-bit:"));
	for(i=0; i<512; ++i){
		cli_putstr_P(PSTR("\r\n    S = "));
		data[i/8] = 0x80>>(i&7);
		cli_hexdump(data, 64);
		cli_putstr_P(PSTR(": "));
		whirlpool_init(&ctx);
		whirlpool_lastBlock(&ctx, data, 512);
		whirlpool_ctx2hash(hash, &ctx);
		data[i/8] = 0x00;
		cli_hexdump(hash, 64);
	}
	cli_putstr_P(PSTR("\r\nIterated message digest computation (100000000 times): "));
	uint32_t c;
	memset(hash, 0, 64);
	for(c=0; c<1000000; ++c){
		whirlpool_init(&ctx);
		whirlpool_lastBlock(&ctx, hash, 512);
		whirlpool_ctx2hash(hash, &ctx);
	}
	cli_hexdump(hash, 64);
	cli_putstr_P(PSTR("\r\n/* finish generating testvectors */\r\n"));
}
void testrun_entropium(void){
	char c, str[16];
	uint8_t data[32];
	uint32_t i=0;
	while('q'!=cli_getc()){
		entropium_getRandomBlock(data);
		cli_putstr_P(PSTR("\r\n "));
		ultoa(i, str, 10);
		for(c=strlen(str); c<11; ++c){
			cli_putc(' ');
		}
		cli_putstr(str);
		++i;
		cli_putstr_P(PSTR(" : "));
		cli_hexdump(data, 32);
	}
	cli_putstr_P(PSTR("\r\n\r\n"));
}
static
void md5_core(uint32_t* a, void* block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi){
	uint32_t t;
	md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I};
	as &= 0x3;
	/* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
#ifdef DEBUG
	char funcc[]={'*', '-', '+', '~'};
	cli_putstr("\r\n DBG: md5_core [");
	cli_putc(funcc[fi]);
	cli_hexdump(&as, 1); cli_putc(' ');
	cli_hexdump(&k, 1); cli_putc(' ');
	cli_hexdump(&s, 1); cli_putc(' ');
	cli_hexdump(&i, 1); cli_putc(']');
#endif	
	t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) 
	    + *((uint32_t*)block) + pgm_read_dword(md5_T+i) ;
	a[as]=a[(as+1)&3] + ROTL32(t, s);
}
コード例 #23
0
void testrun_test_aes(void){
	uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16,
	                    0x28, 0xae, 0xd2, 0xa6,
	                    0xab, 0xf7, 0x15, 0x88,
	                    0x09, 0xcf, 0x4f, 0x3c };
	uint8_t data[16] = { 0x32, 0x43, 0xf6, 0xa8,
	                     0x88, 0x5a, 0x30, 0x8d,
	                     0x31, 0x31, 0x98, 0xa2,
	                     0xe0, 0x37, 0x07, 0x34 };
	aes128_ctx_t ctx;
	aes128_init(key, &ctx);
	cli_putstr("\r\n\r\n cipher test (FIPS 197):\r\n key:        ");
	cli_hexdump(key, 16);
	cli_putstr("\r\n plaintext:  ");
	cli_hexdump(data, 16);
	aes128_enc(data, &ctx);
	cli_putstr("\r\n ciphertext: ");
	cli_hexdump(data, 16);
	aes128_dec(data, &ctx);
	cli_putstr("\r\n plaintext:  ");
	cli_hexdump(data, 16);
	cli_putstr("\r\n testing bcal:");
	bcgen_ctx_t bcal_ctx;
	uint8_t r;
	r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx);
	cli_putstr("\r\n init = 0x");
	cli_hexdump(&r, 1);

	bcal_cipher_enc(data, &bcal_ctx);
	cli_putstr("\r\n ciphertext: ");
	cli_hexdump(data, 16);
	bcal_cipher_dec(data, &bcal_ctx);
	cli_putstr("\r\n plaintext:  ");
	cli_hexdump(data, 16);
	bcal_cipher_free(&bcal_ctx);
}
コード例 #24
0
void dsa_print_signature_b64(dsa_signature_t* s){
	uint16_t size_r, size_s, size_o, i,j;
	size_r = s->r.length_W*sizeof(bigint_word_t) +2;
	size_s = s->s.length_W*sizeof(bigint_word_t) +2;
	size_o = size_r + size_s +2;
	uint8_t bin_b[size_o];
	bin_b[0] = 0x30;
	bin_b[1] = size_o -2;
	bin_b[2] = 0x02;
	bin_b[3] = size_r-2;
	j=4;
	for(i=s->r.length_W*sizeof(bigint_word_t); i>0;  --i){
		bin_b[j++] = ((uint8_t*)s->r.wordv)[i-1];
	}
	bin_b[j++] = 0x02;
	bin_b[j++] = size_s -2;
	for(i=s->s.length_W*sizeof(bigint_word_t); i>0;  --i){
		bin_b[j++] = ((uint8_t*)s->s.wordv)[i-1];
	}
	char b64_b[size_o*4/3+5];
	base64enc(b64_b, bin_b, size_o);
	cli_putstr(b64_b);
}
コード例 #25
0
int main (void){
	sysclk_set_freq(SYS_FREQ);
	sysclk_mosc_verify_enable();
	uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE);
	gptm_set_timer_32periodic(TIMER0);
	
	cli_rx = (cli_rx_fpt)uart0_getc;
	cli_tx = (cli_tx_fpt)uart0_putc;	 	
	for(;;){
		cli_putstr("\r\n\r\nARM-Crypto-Lib VS (");
		cli_putstr(algo_name);
		cli_putstr("; ");
		cli_putstr(__DATE__);
		cli_putc(' ');
		cli_putstr(__TIME__);
		cli_putstr(")\r\nloaded and running\r\n");
    	cmd_interface(cmdlist);
    }
}
コード例 #26
0
ファイル: bigint_io.c プロジェクト: Sergoi/avr-crypto-lib
uint8_t bigint_read_hex_echo(bigint_t *a) {
	uint16_t allocated = 0;
	uint8_t  shift4 = 0;
	uint16_t  t, idx = 0;
	a->length_W = 0;
	a->wordv = NULL;
	a->info = 0;
	for (;;) {
		if (allocated - idx < 1) {
			bigint_word_t *p;
			p = realloc(a->wordv, allocated += BLOCKSIZE);
			if (p == NULL) {
				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 (idx == 0) {
			if (t & 0x0400) {
				/* got minus */
				a->info |= BIGINT_NEG_MASK;
				continue;
			} else {
				if (t == 0x0100) {
					free(a->wordv);
					a->wordv = NULL;
					return 1;
				}
			}
		}
		if (t <= 0x00ff) {
			((uint8_t*)(a->wordv))[idx++] = (uint8_t)t;
		} else {
			if (t & 0x0200) {
				shift4 = 1;
				((uint8_t*)(a->wordv))[idx++] = (uint8_t)((t & 0x0f) << 4);
			}
			break;
		}
	}
	/* we have to reverse the byte array */
	uint8_t tmp;
	uint8_t *p, *q;
	a->length_W = (idx + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t);
	p = (uint8_t*)(a->wordv);
	q = (uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - 1;
	while (q > p) {
		tmp = *p;
		*p = *q;
		*q = tmp;
		p++; q--;
	}
	bigint_adjust(a);
	if (shift4) {
		bigint_shiftright(a, 4);
	}
	if(a->length_W == 1 && a->wordv[0] == 0){
	    a->length_W = 0;
	    a->info = 0;
	}
	return 0;
}
コード例 #27
0
ファイル: shavs.c プロジェクト: nerilex/arm-crypto-lib
void shavs_test3(void){ /* Monte Carlo tests for SHA-3 */
	uint16_t expected_input;
	uint16_t count;
	uint8_t v;
	uint8_t index=0;
	char c;
	if(!shavs_algo){
			cli_putstr("\r\nERROR: select algorithm first!");
		return;
	}
	uint8_t ml=shavs_algo->hashsize_b/8;
	uint8_t m[ml+128];
	for(;;){
		while((c=cli_getc_cecho())!='S' && c!='s'){
			if(!isblank((uint8_t)c)){
				cli_putstr("\r\nERROR: wrong input (1) [0x");
				cli_hexdump(&c, 1);
				cli_putstr("]!\r\n");
				return;
			}
		}
		if((c=cli_getc_cecho())!='e' && c!='E'){
				cli_putstr("\r\nERROR: wrong input (2)!\r\n");
				return;
		}
		if((c=cli_getc_cecho())!='e' && c!='E'){
				cli_putstr("\r\nERROR: wrong input (3)!\r\n");
				return;
		}
		if((c=cli_getc_cecho())!='d' && c!='D'){
				cli_putstr("\r\nERROR: wrong input (4)!\r\n");
				return;
		}
		while((c=cli_getc_cecho())!='='){
			if(!isblank((uint8_t)c)){
				cli_putstr("\r\nERROR: wrong input (5)!\r\n");
				return;
			}
		}
		expected_input = 1024/4;
		memset(m+ml, 0, 1024/8);
		do{
			v=0xff;
			c=cli_getc_cecho();
			if(c>='0' && c<='9'){
				v = c - '0';
			}else{
				c |= 'A'^'a';
				if(c>='a' && c<='f'){
					v = c - 'a' +10;
				}
			}
			if(v<0x10){
				c=m[ml+index/2];
				if(index&1){
					c |= v;
				}else{
					c |=v<<4;
				}
				m[ml+index/2]=c;
				index++;
				expected_input--;
			}
		}while(expected_input);
		/* so we have the seed */
		cli_putstr("\r\nstarting processing");
		uint16_t j;
		for(count=0; count<100; ++count){
			for(j=0; j<1000; ++j){
				hfal_hash_mem(shavs_algo, m, m+ml, 1024);
				memmove(m+ml, m, 1024/8);
			}
			cli_putstr("\r\n\r\nj = ");
			if(count>=10){
				cli_putc(count/10+'0');
			}
			cli_putc(count%10+'0');
			cli_putstr("\r\nMD = ");
			cli_hexdump(m+ml, ml);

		}
	}
}
コード例 #28
0
ファイル: shavs.c プロジェクト: nerilex/arm-crypto-lib
void shavs_test1(void){ /* KAT tests */
	uint32_t length=0;
	int32_t expect_input=0;

	if(!shavs_algo){
			cli_putstr("\r\nERROR: select algorithm first!");
		return;
	}
	char c;
	uint8_t diggest[shavs_algo->hashsize_b/8];
	shavs_ctx.buffersize_B=shavs_algo->blocksize_b/8;
	uint8_t buffer[shavs_ctx.buffersize_B+5];
	shavs_ctx.buffer = buffer;
	cli_putstr("\r\nbuffer_size = 0x");
	cli_hexdump_rev(&(shavs_ctx.buffersize_B), 2);
	cli_putstr(" bytes");
	for(;;){
		shavs_ctx.blocks = 0;
		memset(buffer, 0, shavs_ctx.buffersize_B);
		length = getLength();
		if((int32_t)length<0){
#if DEBUG
			cli_putstr("\r\n(x) Len == ");
			cli_hexdump_rev(&length, 4);
			uart_flush(0);
#endif
			return;
		}

#if DEBUG
		cli_putstr("\r\nLen == ");
		cli_hexdump_rev(&length, 4);
		uart_flush(0);
#endif
		if(length==0){
			expect_input=2;
		}else{
			expect_input=((length + 7) >> 2) & (~1L);
		}
#if DEBUG
		cli_putstr("\r\nexpected_input == ");
		cli_hexdump_rev(&expect_input, 4);
		if(expect_input==0)
			cli_putstr("\r\nexpected_input == 0 !!!");
#endif
		shavs_ctx.buffer_idx = 0;
		shavs_ctx.in_byte    = 0;
		shavs_ctx.blocks     = 0;
		uint8_t ret;
#if DEBUG
		cli_putstr("\r\n HFAL init");
		cli_putstr("\r\n (2) expected_input == ");
		cli_hexdump_rev(&expect_input, 4);
#endif
		ret = hfal_hash_init(shavs_algo, &(shavs_ctx.ctx));
		if(ret){
			cli_putstr("\r\n HFAL init returned with: ");
			cli_hexdump(&ret, 1);
			return;
		}
#if DEBUG
		cli_putstr("\r\n (3) expected_input == ");
		cli_hexdump_rev(&expect_input, 4);
		cli_putstr("\r\n");
#endif
		while((c=cli_getc_cecho())!='M' && c!='m'){
			if(!isblank((uint8_t)c)){
				cli_putstr("\r\nERROR: wrong input (1) [0x");
				cli_hexdump(&c, 1);
				cli_putstr("]!\r\n");
				hfal_hash_free(&(shavs_ctx.ctx));
				return;
			}
		}
		if((c=cli_getc_cecho())!='s' && c!='S'){
				cli_putstr("\r\nERROR: wrong input (2)!\r\n");
				hfal_hash_free(&(shavs_ctx.ctx));
				return;
		}
		if((c=cli_getc_cecho())!='g' && c!='G'){
				cli_putstr("\r\nERROR: wrong input (3)!\r\n");
				hfal_hash_free(&(shavs_ctx.ctx));
				return;
		}
		while((c=cli_getc_cecho())!='='){
			if(!isblank((uint8_t)c)){
				cli_putstr("\r\nERROR: wrong input (4)!\r\n");
				hfal_hash_free(&(shavs_ctx.ctx));
				return;
			}
		}
#if DEBUG
		cli_putstr("\r\nparsing started");
#endif
		shavs_ctx.buffer_idx = 0;
		shavs_ctx.in_byte    = 0;
		shavs_ctx.blocks     = 0;
		while(expect_input>0){
			c=cli_getc_cecho();
#if DEBUG
			cli_putstr("\r\n\t(");
			cli_hexdump_rev(&expect_input, 4);
			cli_putstr(") ");
#endif
			if(buffer_add(c)==0){
				--expect_input;
			}else{
				if(!isblank((uint16_t)c)){
					cli_putstr("\r\nERROR: wrong input (5) (");
					cli_putc(c);
					cli_putstr(")!\r\n");
					hfal_hash_free(&(shavs_ctx.ctx));
					return;
				}
			}
		}
#if DEBUG
		cli_putstr("\r\nBuffer-A:");
		cli_hexdump_block(buffer, shavs_ctx.buffersize_B, 5, 8);

		cli_putstr("\r\n starting finalization");
		cli_putstr("\r\n\tblocks     == ");
		cli_hexdump_rev(&(shavs_ctx.blocks),4);
		cli_putstr("\r\n\tbuffer_idx == ");
		cli_hexdump_rev(&(shavs_ctx.buffer_idx),2);
		cli_putstr("\r\n\tin_byte    == ");
		cli_hexdump_rev(&(shavs_ctx.in_byte),1);

		cli_putstr("\r\n starting last block");
		cli_putstr("\r\n\tlength       == ");
		cli_hexdump_rev(&length,4);
		cli_putstr("\r\n\tbuffersize_B == ");
		cli_hexdump_rev(&(shavs_ctx.buffersize_B),2);
		uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
		cli_putstr("\r\n\t (temp)      == ");
		cli_hexdump_rev(&temp,2);
#else
		uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8);
#endif
		hfal_hash_lastBlock( &(shavs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */
		                    temp );
#if DEBUG
		cli_putstr("\r\n starting ctx2hash");
#endif
		hfal_hash_ctx2hash(diggest, &(shavs_ctx.ctx));
#if DEBUG
		cli_putstr("\r\n starting hash free");
#endif
		hfal_hash_free(&(shavs_ctx.ctx));
		cli_putstr("\r\n MD = ");
		cli_hexdump(diggest, shavs_algo->hashsize_b/8);

	}
}
コード例 #29
0
ファイル: bigint2_io.c プロジェクト: nerilex/avr-crypto-lib
uint8_t bigint_read_hex_echo(bigint_t *a, bigint_length_t length) {
	uint8_t  shift4 = 0;
	uint16_t  t, idx = 0;
	uint8_t *buf = NULL;
	memset(a, 0, sizeof(*a));
	if (length && a->allocated_W < length) {
	    uint8_t *p;
        p = int_realloc(buf, length * sizeof(bigint_word_t));
        if (p == NULL) {
            cli_putstr("\r\nERROR: Out of memory!");
            return 0xff;
        }
        memset((uint8_t*)p, 0, length * sizeof(bigint_word_t));
        buf = p;
        a->allocated_W = length;
	}
	for (;;) {
		if (a->allocated_W - idx < 1) {
			uint8_t *p;
			if (length) {
                if (buf) {
                    int_realloc(buf, 0);
                }
			    return 0xfe;
			}
			p = int_realloc(buf, (a->allocated_W += BLOCKSIZE) * sizeof(bigint_word_t));
			if (p == NULL) {
				cli_putstr("\r\nERROR: Out of memory!");
				if (buf) {
				    int_realloc(buf, 0);
				}
				return 0xff;
			}
			memset((uint8_t*)p + (a->allocated_W - BLOCKSIZE) * sizeof(bigint_word_t), 0, BLOCKSIZE * sizeof(bigint_word_t));
			buf = p;
		}
		t = read_byte();
		if (idx == 0) {
			if (t & 0x0400) {
				/* got minus */
				a->info |= BIGINT_SIGN_MASK;
				continue;
			} else {
				if (t == 0x0100) {
					free(a->wordv);
					memset(a, 0, sizeof(*a));
					return 1;
				}
			}
		}
		if (t <= 0x00ff) {
			buf[idx++] = (uint8_t)t;
		} else {
			if (t & 0x0200) {
				shift4 = 1;
				buf[idx++] = (uint8_t)((t & 0x0f) << 4);
			}
			break;
		}
	}
	/* we have to reverse the byte array */
	uint8_t tmp;
	uint8_t *p, *q;
	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--;
	}
	a->wordv = (bigint_word_t*)buf;
	if (shift4) {
		bigint_shiftright_1bit(a);
        bigint_shiftright_1bit(a);
        bigint_shiftright_1bit(a);
        bigint_shiftright_1bit(a);
	}
	if(a->length_W == 1 && a->wordv[0] == 0){
	    a->length_W = 0;
	    a->info = 0;
	}
	if (length) {
	    a->length_W = length;
	}
	return 0;
}
コード例 #30
0
void nessie_print_footer(void){
	cli_putstr("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n");
}