Пример #1
0
void test_reduce_bigint(void){
	bigint_t a, b, c;
	cli_putstr_P(PSTR("\r\nreduce test\r\n"));
	for (;;) {
		cli_putstr_P(PSTR("\r\nenter a:"));
		if (bigint_read_hex_echo(&a, 0)) {
			cli_putstr_P(PSTR("\r\n end reduce test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if (bigint_read_hex_echo(&b, 0)) {
			free(a.wordv);
			cli_putstr_P(PSTR("\r\n end reduce test"));
			return;
		}
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(" % "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR(" = "));
		memset(&c, 0, sizeof(c));
		bigint_divide(NULL, &c, &a, &b);
		bigint_print_hex(&c);
		cli_putstr_P(PSTR("\r\n"));
        bigint_free(&c);
        bigint_free(&b);
		bigint_free(&a);
	}
}
Пример #2
0
void test_square_bigint(void){
	bigint_t a, c;
	cli_putstr_P(PSTR("\r\nsquare test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter a:"));
		if(bigint_read_hex_echo(&a, 0)){
			cli_putstr_P(PSTR("\r\n end square test"));
			return;
		}
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR("**2 = "));
		bigint_word_t *c_b;
		c_b = malloc(a.length_W * 2 * sizeof(bigint_word_t));
		if(c_b == NULL){
			cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
			free(a.wordv);
			continue;
		}
		c.wordv = c_b;
		bigint_square(&c, &a);
		bigint_print_hex(&c);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(c_b);
	}
}
Пример #3
0
void test_add_scale_bigint(void){
	bigint_t a, b, c;
	uint16_t scale;
	cli_putstr_P(PSTR("\r\nadd-scale test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter a:"));
		if(bigint_read_hex_echo(&a)){
			cli_putstr_P(PSTR("\r\n end add-scale test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if(bigint_read_hex_echo(&b)){
			cli_putstr_P(PSTR("\r\n end add-scale test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter scale:"));
		{
			char str[8];
			cli_getsn_cecho(str, 7);
			scale = atoi(str);
		}
	/*
		if(bigint_read_hex_echo(&scale)){
			free(scale.wordv);
			cli_putstr_P(PSTR("\r\n end add test"));
			return;
		}
	*/
		uint8_t *c_b;
		c_b = malloc(((a.length_B>(b.length_B+scale))?a.length_B:(b.length_B+scale))+2);
		if(c_b==NULL){
			cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
			free(a.wordv);
			free(b.wordv);
			continue;
		}
		c.wordv = c_b;
		bigint_copy(&c, &a);
		bigint_add_scale_u(&c, &b, scale);
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(" + "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR("<<8*"));
		cli_hexdump_rev(&scale, 2);
		cli_putstr_P(PSTR(" = "));
		bigint_print_hex(&c);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(b.wordv);
		free(c_b);
	}
}
Пример #4
0
/* d = a**b % c */
void test_expmod_bigint(void){
	bigint_t a, b, c, d;
	uint8_t *d_b;
	cli_putstr_P(PSTR("\r\nreduce test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter a:"));
		if(bigint_read_hex_echo(&a)){
			cli_putstr_P(PSTR("\r\n end expmod test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if(bigint_read_hex_echo(&b)){
			free(a.wordv);
			cli_putstr_P(PSTR("\r\n end expmod test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter c:"));
		if(bigint_read_hex_echo(&c)){
			free(a.wordv);
			free(b.wordv);
			cli_putstr_P(PSTR("\r\n end expmod test"));
			return;
		}
		d_b = malloc(c.length_B);
		if(d_b==NULL){
			cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
			free(a.wordv);
			free(b.wordv);
			free(c.wordv);
			continue;
		}
		d.wordv = d_b;
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR("**"));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR(" % "));
		bigint_print_hex(&c);
		cli_putstr_P(PSTR(" = "));
		bigint_expmod_u(&d, &a, &b, &c);
		bigint_print_hex(&d);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(b.wordv);
		free(c.wordv);
		free(d.wordv);

	}
}
Пример #5
0
/* d = a**b % c */
void test_expmod_mont_bigint(void){
    bigint_t a, b, c, d;
    bigint_word_t *d_b;
    cli_putstr_P(PSTR("\r\nexpnonentiation-modulo-montgomory test\r\n"));
    for (;;) {
        cli_putstr_P(PSTR("\r\nenter a:"));
        if (bigint_read_hex_echo(&a)) {
            cli_putstr_P(PSTR("\r\n end expmod test"));
            return;
        }
        cli_putstr_P(PSTR("\r\nenter b:"));
        if (bigint_read_hex_echo(&b)) {
            free(a.wordv);
            cli_putstr_P(PSTR("\r\n end expmod test"));
            return;
        }
        cli_putstr_P(PSTR("\r\nenter c:"));
        if (bigint_read_hex_echo(&c)) {
            free(a.wordv);
            free(b.wordv);
            cli_putstr_P(PSTR("\r\n end expmod test"));
            return;
        }
        d_b = malloc(c.length_W * sizeof(bigint_word_t));
        if (d_b == NULL) {
            cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
            free(a.wordv);
            free(b.wordv);
            free(c.wordv);
            continue;
        }
        d.wordv = d_b;
        cli_putstr_P(PSTR("\r\n "));
        bigint_print_hex(&a);
        cli_putstr_P(PSTR("**"));
        bigint_print_hex(&b);
        cli_putstr_P(PSTR(" % "));
        bigint_print_hex(&c);
        cli_putstr_P(PSTR(" = "));
        bigint_expmod_u_mont_sam(&d, &a, &b, &c);
        bigint_print_hex(&d);
        cli_putstr_P(PSTR("\r\n"));
        free(a.wordv);
        free(b.wordv);
        free(c.wordv);
        free(d.wordv);

    }
}
Пример #6
0
void test_mul_mont_bigint(void){
    bigint_t a, b, c, a_, b_, m_, res;
    bigint_length_t s;
    cli_putstr_P(PSTR("\r\nmul-mont test ( (a * b) % c )\r\n"));
    for (;;) {
        cli_putstr_P(PSTR("\r\nenter a:"));
        if (bigint_read_hex_echo(&a)) {
            cli_putstr_P(PSTR("\r\n end mul test"));
            return;
        }
        cli_putstr_P(PSTR("\r\nenter b:"));
        if (bigint_read_hex_echo(&b)) {
            free(a.wordv);
            cli_putstr_P(PSTR("\r\n end mul test"));
            return;
        }
        cli_putstr_P(PSTR("\r\nenter c:"));
        if (bigint_read_hex_echo(&c)) {
            free(a.wordv);
            free(b.wordv);
            cli_putstr_P(PSTR("\r\n end mul test"));
            return;
        }
        s = c.length_W;
        cli_putstr_P(PSTR("\r\n ("));
        bigint_print_hex(&a);
        cli_putstr_P(PSTR(" * "));
        bigint_print_hex(&b);
        cli_putstr_P(PSTR(") % "));
        bigint_print_hex(&c);
        cli_putstr_P(PSTR(" = "));
        bigint_word_t res_w[s], a_w_[s], b_w_[s], m_w_[s + 1];
        res.wordv = res_w;
        a_.wordv = a_w_;
        b_.wordv = b_w_;
        m_.wordv = m_w_;
        bigint_mont_gen_m_(&m_, &c);
        bigint_mont_trans(&a_, &a, &c);
        bigint_mont_trans(&b_, &b, &c);
        bigint_mont_mul(&res, &a_, &b_, &c, &m_);
        bigint_mont_red(&res, &res, &c, &m_);
        bigint_print_hex(&res);
        putchar('\n');
        free(a.wordv);
        free(b.wordv);
        free(c.wordv);
    }
}
Пример #7
0
void test_square_simple(void){
	bigint_t a, c;

	uint8_t a_b[11] = {0xe6, 0x70, 0x7d, 0x43, 0x74, 0x07, 0x20, 0x22, 0x6a, 0xb8, 0xf4};
	uint8_t c_b[22];
	a.wordv=a_b;
	c.wordv=c_b;
	a.length_B = 11;
	a.info=0x00;
	bigint_adjust(&a);
	bigint_square(&c, &a);
	cli_putstr_P(PSTR("\r\n test: "));
	bigint_print_hex(&a);
	cli_putstr_P(PSTR("**2 = "));
	bigint_print_hex(&c);
}
Пример #8
0
void test_mul_word_bigint(void){
    bigint_t a, b;
    bigint_word_t *t;
    cli_putstr_P(PSTR("\r\nmul test\r\n"));
    for (;;) {
        cli_putstr_P(PSTR("\r\nenter a:"));
        if (bigint_read_hex_echo(&a, 0)) {
            cli_putstr_P(PSTR("\r\n end mul test"));
            return;
        }
        cli_putstr_P(PSTR("\r\nenter b:"));
        if (bigint_read_hex_echo(&b, 0)) {
            free(a.wordv);
            cli_putstr_P(PSTR("\r\n end mul test"));
            return;
        }
        cli_putstr_P(PSTR("\r\n "));
        bigint_print_hex(&a);
        cli_putstr_P(PSTR(" * "));
        bigint_print_hex(&b);
        cli_putstr_P(PSTR(" = "));

        if (b.length_W > 1) {
            free(a.wordv);
            free(b.wordv);
            cli_putstr_P(PSTR("\r\n end mul test"));
        }

        t = realloc(a.wordv, (a.length_W + 3) * sizeof(bigint_word_t));
        if (t == NULL) {
            cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
            free(a.wordv);
            free(b.wordv);
            continue;
        }
        a.wordv = t;
        bigint_mul_word(&a, &a, b.wordv[0]);
        bigint_print_hex(&a);
        cli_putstr_P(PSTR("\r\n"));
        free(a.wordv);
        free(b.wordv);
    }
}
Пример #9
0
void test_gcdext_bigint(void){
	bigint_t a, b, c, d, e;
	cli_putstr_P(PSTR("\r\ngcdext test\r\n"));
	for (;;) {
		cli_putstr_P(PSTR("\r\nenter a:"));
		if (bigint_read_hex_echo(&a, 0)) {
			cli_putstr_P(PSTR("\r\n end gcdext test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if (bigint_read_hex_echo(&b, 0)) {
			bigint_free(&a);
			cli_putstr_P(PSTR("\r\n end gcdext test"));
			return;
		}

		memset(&c, 0, sizeof(c));
        memset(&d, 0, sizeof(d));
        memset(&e, 0, sizeof(e));
		cli_putstr_P(PSTR("\r\n gcdext( "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(", "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR(") => "));
		bigint_gcdext(&c, &d, &e, &a, &b);
		cli_putstr_P(PSTR("a = "));
		bigint_print_hex(&d);
		cli_putstr_P(PSTR("; b = "));
		bigint_print_hex(&e);
		cli_putstr_P(PSTR("; gcd = "));
		bigint_print_hex(&c);

		cli_putstr_P(PSTR("\r\n"));
		bigint_free(&a);
        bigint_free(&b);
        bigint_free(&c);
        bigint_free(&d);
        bigint_free(&e);
	}
}
Пример #10
0
void test_gcdext_bigint(void){
	bigint_t a, b, c, d, e;
	cli_putstr_P(PSTR("\r\ngcdext test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter a:"));
		if(bigint_read_hex_echo(&a)){
			cli_putstr_P(PSTR("\r\n end gcdext test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if(bigint_read_hex_echo(&b)){
			free(a.wordv);
			cli_putstr_P(PSTR("\r\n end gcdext test"));
			return;
		}
		c.wordv = malloc((a.length_B<b.length_B)?a.length_B:b.length_B);
		d.wordv = malloc(1+(a.length_B>b.length_B)?a.length_B:b.length_B);
		e.wordv = malloc(1+(a.length_B>b.length_B)?a.length_B:b.length_B);

		cli_putstr_P(PSTR("\r\n gcdext( "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(", "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR(") => "));
		bigint_gcdext(&c, &d, &e, &a, &b);
		cli_putstr_P(PSTR("a = "));
		bigint_print_hex(&d);
		cli_putstr_P(PSTR("; b = "));
		bigint_print_hex(&e);
		cli_putstr_P(PSTR("; gcd = "));
		bigint_print_hex(&c);

		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(b.wordv);
		free(c.wordv);
		free(d.wordv);
		free(e.wordv);
	}
}
Пример #11
0
void test_simple(void){
	bigint_t a, b, c;
	uint8_t a_b[1], b_b[1], c_b[2];
	a.wordv=a_b;
	b.wordv=b_b;
	c.wordv=c_b;
	a.length_B = 1;
	b.length_B = 1;
	a_b[0] = 1;
	b_b[0] = 2;
	bigint_add_u(&c, &a, &b);
	cli_putstr_P(PSTR("\r\n 1+2="));
	bigint_print_hex(&c);
}
Пример #12
0
void test_mul_bigint(void){
	bigint_t a, b, c;
	cli_putstr_P(PSTR("\r\nmul test\r\n"));
	for (;;) {
		cli_putstr_P(PSTR("\r\nenter a:"));
		if (bigint_read_hex_echo(&a, 0)) {
			cli_putstr_P(PSTR("\r\n end mul test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if (bigint_read_hex_echo(&b, 0)) {
			free(a.wordv);
			cli_putstr_P(PSTR("\r\n end mul test"));
			return;
		}
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(" * "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR(" = "));
		bigint_word_t *c_b;
		c_b = malloc((MAX(a.length_W, b.length_W) + 1) * 2 * sizeof(bigint_word_t));
		if (c_b==NULL) {
			cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
			free(a.wordv);
			free(b.wordv);
			continue;
		}
		c.wordv = c_b;
		bigint_mul_schoolbook(&c, &a, &b);
		bigint_print_hex(&c);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(b.wordv);
		free(c_b);
	}
}
Пример #13
0
void test_add_bigint(void){
	bigint_t a, b, c;
	cli_putstr_P(PSTR("\r\nadd test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter a:"));
		if(bigint_read_hex_echo(&a)){
			cli_putstr_P(PSTR("\r\n end add test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if(bigint_read_hex_echo(&b)){
			free(a.wordv);
			cli_putstr_P(PSTR("\r\n end add test"));
			return;
		}
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(" + "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR(" = "));
		uint8_t *c_b;
		c_b = malloc(((a.length_B>b.length_B)?a.length_B:b.length_B)+2);
		if(c_b==NULL){
			cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
			free(a.wordv);
			free(b.wordv);
			continue;
		}
		c.wordv = c_b;
		bigint_add_s(&c, &a, &b);
		bigint_print_hex(&c);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(b.wordv);
		free(c_b);
	}
}
Пример #14
0
/*****************************************************************************
 *  additional validation-functions											 *
 *****************************************************************************/
void test_echo_bigint(void){
	bigint_t a;
	cli_putstr_P(PSTR("\r\necho test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter hex number:"));
		if(bigint_read_hex_echo(&a)){
			cli_putstr_P(PSTR("\r\n end echo test"));
			return;
		}
		cli_putstr_P(PSTR("\r\necho: "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
	}
}
Пример #15
0
void testrun_interm(void){
    ecc_chudnovsky_point_t q;
    ecc_affine_point_t qa;
    uint32_t time;
    bigint_t k;
    uint8_t r;

    printf_P(PSTR("\n== testing key generation ==\n"));

    printf_P(PSTR("enter secret key d: "));
    bigint_read_hex_echo(&k);
    putchar('\n');

    if(ecc_chudnovsky_point_alloc(&q, 192)){
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }
    if(ecc_affine_point_alloc(&qa, 192)){
        ecc_chudnovsky_point_free(&q);
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }

    printf_P(PSTR("(naf)  k:  "));
    bigint_print_hex(&k);
    startTimer(1);
    START_TIMER;
    r = ecc_chudnovsky_naf_multiplication(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    STOP_TIMER;
    time = stopTimer();
    ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192);

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&qa.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&qa.y);
    printf_P(PSTR("\n time: %"PRIu32" cycles (r code: %"PRIu8")\n"), time, r);

    printf_P(PSTR("(d&a)  k:  "));
    bigint_print_hex(&k);
    startTimer(1);
    START_TIMER;
    r = ecc_chudnovsky_double_and_add(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    STOP_TIMER;
    time = stopTimer();
    ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192);

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&qa.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&qa.y);
    printf_P(PSTR("\n time: %"PRIu32" cycles (r code: %"PRIu8")\n"), time, r);
    free(k.wordv);
    ecc_chudnovsky_point_free(&q);
    ecc_affine_point_free(&qa);
}
Пример #16
0
// [fail (c)]:  A862 % 2752 = 0D1A ; should a862 % 2752 = b1a
void test_reduce_simple(void){
	bigint_t a, b, c;

	uint8_t a_b[2] = {0x62, 0xA8};
	uint8_t b_b[2] = {0x52, 0x27};
	uint8_t c_b[2];
	a.wordv=a_b;
	a.length_B = 2;
	a.info=0x00;
	bigint_adjust(&a);
	b.wordv=b_b;
	b.length_B = 2;
	b.info=0x00;
	bigint_adjust(&b);
	c.wordv = c_b;
	bigint_copy(&c, &a);
	bigint_reduce(&c, &b);
	cli_putstr_P(PSTR("\r\n test: "));
	bigint_print_hex(&a);
	cli_putstr_P(PSTR(" % "));
	bigint_print_hex(&b);
	cli_putstr_P(PSTR(" = "));
	bigint_print_hex(&c);
}
Пример #17
0
void testrun_genkey1(void){
    ecc_chudnovsky_point_t q;
    ecc_affine_point_t qa;

    uint8_t k_w[] = {
    //  e5ce89a34adddf25ff3bf1ffe6803f57d0220de3118798ea
        0xea, 0x98, 0x87, 0x11, 0xe3, 0x0d, 0x22, 0xd0,
        0x57, 0x3f, 0x80, 0xe6, 0xff, 0xf1, 0x3b, 0xff,
        0x25, 0xdf, 0xdd, 0x4a, 0xa3, 0x89, 0xce, 0xe5
    };

    bigint_t k = {
            .length_W = sizeof(k_w),
            .wordv = k_w,
            .info = 7
    };

    printf_P(PSTR("\n== testing key generation ==\n"));

    if(ecc_chudnovsky_point_alloc(&q, 192)){
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }
    if(ecc_affine_point_alloc(&qa, 192)){
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }

    printf_P(PSTR("  k:  "));
    bigint_print_hex(&k);
    ecc_chudnovsky_multiplication(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192);

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&qa.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&qa.y);
    puts("\n");

    ecc_affine_point_free(&qa);
    ecc_chudnovsky_point_free(&q);
}

void testrun_genkey3(void){
    ecc_chudnovsky_point_t q;
    ecc_affine_point_t qa;

    uint8_t k_w[] = {
        0xb2, 0x51, 0x97, 0xc3, 0x7c, 0x61, 0xf8, 0x8f,
        0x19, 0x91, 0xcc, 0x67, 0xb5, 0x1c, 0x34, 0x23,
        0xff, 0x13, 0xad, 0x14, 0x57, 0x43, 0x14, 0x7d
    };

    bigint_t k = {
            .length_W = sizeof(k_w),
            .wordv = k_w,
            .info = 6
    };

    printf_P(PSTR("\n== testing key generation ==\n"));

    if(ecc_chudnovsky_point_alloc(&q, 192)){
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }
    if(ecc_affine_point_alloc(&qa, 192)){
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }

    printf_P(PSTR("  k:  "));
    bigint_print_hex(&k);
    ecc_chudnovsky_double_and_add(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192);

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&qa.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&qa.y);
    puts("\n");

    ecc_affine_point_free(&qa);
    ecc_chudnovsky_point_free(&q);

}

void testrun_genkey(void){
    ecc_chudnovsky_point_t q;
    ecc_affine_point_t qa;
    uint32_t time;
    bigint_t k;
    uint8_t r;

    printf_P(PSTR("\n== testing key generation ==\n"));

    printf_P(PSTR("enter secret key d: "));
    bigint_read_hex_echo(&k);
    putchar('\n');

    if(ecc_chudnovsky_point_alloc(&q, 192)){
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }
    if(ecc_affine_point_alloc(&qa, 192)){
        ecc_chudnovsky_point_free(&q);
        printf_P(PSTR("ERROR: OOM! <%s %s %d>\n"), __FILE__, __func__, __LINE__);
        return;
    }

    printf_P(PSTR("(naf)  k:  "));
    bigint_print_hex(&k);
    startTimer(1);
    START_TIMER;
    r = ecc_chudnovsky_naf_multiplication(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    STOP_TIMER;
    time = stopTimer();
    ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192);

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&qa.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&qa.y);
    printf_P(PSTR("\n time: %"PRIu32" cycles (r code: %"PRIu8")\n"), time, r);

    printf_P(PSTR("(d&a)  k:  "));
    bigint_print_hex(&k);
    startTimer(1);
    START_TIMER;
    r = ecc_chudnovsky_double_and_add(&q, &k, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    STOP_TIMER;
    time = stopTimer();
    ecc_chudnovsky_to_affine_point(&qa, &q, &nist_curve_p192);

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&qa.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&qa.y);
    printf_P(PSTR("\n time: %"PRIu32" cycles (r code: %"PRIu8")\n"), time, r);
    free(k.wordv);
    ecc_chudnovsky_point_free(&q);
    ecc_affine_point_free(&qa);
}
Пример #18
0
void dsa_print_signature(const dsa_signature_t* sig){
	cli_putstr("\r\nDSA-Signature:\r\n r:");
	bigint_print_hex(&(sig->r));
	cli_putstr("\r\n s:");
	bigint_print_hex(&(sig->s));
}
Пример #19
0
void test_sign2(void){
    bigint_word_t d_w[sizeof(ecdsa_test_2_d)];
    uint8_t rnd[sizeof(ecdsa_test_2_k)];
    uint8_t *hash;
    bigint_t d;
    const hfdesc_t *hash_desc;
    ecc_combi_point_t q;
    ecdsa_signature_t sign;
    ecdsa_ctx_t ctx;
    uint8_t r;

    putchar('\n');
    d.wordv = d_w;
    memcpy_P(rnd, ecdsa_test_2_k, sizeof(ecdsa_test_2_k));
    memcpy_P(d_w, ecdsa_test_2_d, sizeof(ecdsa_test_2_d) * sizeof(bigint_word_t));
    d.length_W = sizeof(ecdsa_test_2_d) / sizeof(bigint_word_t);
    d.info = 0;
    bigint_adjust(&d);

    hash_desc = &sha224_desc; //hash_select();
    hash = malloc(hfal_hash_getHashsize(hash_desc) / 8);
    if(hash == NULL){
        printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
    }
    hash_mem_P(hash_desc, hash, ecdsa_test_2_msg, sizeof(ecdsa_test_1_msg) * 8);
    printf_P(PSTR("msg hash: "));
    cli_hexdump(hash, hfal_hash_getHashsize(hash_desc) / 8);
    putchar('\n');

    ecc_chudnovsky_point_alloc(&q.chudnovsky, nist_curve_p192_p.length_W * sizeof(bigint_word_t));
    ctx.basepoint = &nist_curve_p192_basepoint.chudnovsky;
    ctx.priv = &d;
    ctx.curve = &nist_curve_p192;

    printf("\n  d:  ");
    bigint_print_hex(&d);
    printf_P(PSTR("\n  Gx: "));
    bigint_print_hex(&nist_curve_p192_basepoint.affine.x);
    printf_P(PSTR("\n  Gy: "));
    bigint_print_hex(&nist_curve_p192_basepoint.affine.y);

    r = ecc_chudnovsky_multiplication(&q.chudnovsky, &d, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192);
    if(r){
        printf_P(PSTR("ERROR: ecc_chudnovsky_multiplication() returned: %"PRIu8"\n"), r);
    }
    r = ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, &nist_curve_p192);
    if(r){
        printf_P(PSTR("ERROR: ecc_chudnovsky_to_affine_point() returned: %"PRIu8"\n"), r);
    }

    printf_P(PSTR("\n  Qx: "));
    bigint_print_hex(&q.affine.x);
    printf_P(PSTR("\n  Qy: "));
    bigint_print_hex(&q.affine.y);
    putchar('\n');
    ctx.pub = &q.affine;

    ecdsa_signature_alloc(&sign, sizeof(ecdsa_test_2_d) * sizeof(bigint_word_t));

    r = ecdsa_sign_hash(&sign, hash, hfal_hash_getHashsize(hash_desc) / 8, &ctx, rnd);
    if(r){
        printf_P(PSTR("ERROR: ecdsa_sign_message() returned: %"PRIu8"\n"), r);
    }
    printf_P(PSTR("  r: "));
    bigint_print_hex(&sign.r);
    printf_P(PSTR("\n  s: "));
    bigint_print_hex(&sign.s);

    free(hash);
    ecdsa_signature_free(&sign);
    ecc_chudnovsky_point_free(&q.chudnovsky);
}
void run_seed_test(void){
	uint8_t *msg, *ciph, *msg_;
	uint16_t ciph_len, msg_len;
	uint16_t msg_len_;
	uint16_t seed_len;
	uint8_t *seed, *seed_out;
	char read_int_str[18];
	cli_putstr_P(PSTR("\r\n== test with given seed =="));
	cli_putstr_P(PSTR("\r\n = message ="));
	cli_putstr_P(PSTR("\r\n  length: "));
	cli_getsn(read_int_str, 16);
	msg_len = own_atou(read_int_str);
	seed_len = rsa_pkcs1v15_compute_padlength_B(&pub_key.modulus, msg_len);
	seed = malloc(seed_len);
#if DEBUG
	cli_putstr_P(PSTR("\r\nDBG: @seed: 0x"));
	cli_hexdump_rev(&seed, 2);
#endif
	if(!seed){
		cli_putstr_P(PSTR("\r\nERROR: OOM!"));
		return;
	}
	seed_out = malloc(seed_len);
#if DEBUG
	cli_putstr_P(PSTR("\r\nDBG: @seed_out: 0x"));
	cli_hexdump_rev(&seed_out, 2);
#endif
	if(!seed_out){
		cli_putstr_P(PSTR("\r\nERROR: OOM!"));
		return;
	}
	msg = malloc(msg_len);
#if DEBUG
	cli_putstr_P(PSTR("\r\nDBG: @msg: 0x"));
	cli_hexdump_rev(&msg, 2);
#endif
	if(!msg){
		cli_putstr_P(PSTR("\r\nERROR: OOM!"));
		return;
	}
	ciph = malloc(bigint_length_B(&pub_key.modulus));
#if DEBUG
	cli_putstr_P(PSTR("\r\nDBG: @ciph: 0x"));
	cli_hexdump_rev(&ciph, 2);
#endif
	if(!ciph){
		cli_putstr_P(PSTR("\r\nERROR: OOM!"));
		return;
	}
	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);
#endif
	if(!msg_){
		cli_putstr_P(PSTR("\r\nERROR: OOM!"));
		return;
	}
	cli_putstr_P(PSTR("\r\n  data: "));
	read_os(msg, msg_len, NULL);
	cli_putstr_P(PSTR("\r\n  seed (0x"));
	cli_hexdump_rev(&seed_len, 2);
	cli_putstr_P(PSTR(" bytes): "));
	read_os(seed, seed_len, NULL);

	cli_putstr_P(PSTR("\r\n  encrypting ..."));
/*
	cli_putstr_P(PSTR("\r\n plaintext:"));
	cli_hexdump_block(msg, msg_len, 4, 16);
	cli_putstr_P(PSTR("\r\n seed:"));
	cli_hexdump_block(seed, seed_len, 4, 16);
*/
#if DEBUG
	cli_putstr_P(PSTR("\r\n  first prime:"));
	bigint_print_hex(&priv_key.components[0]);
#endif
	rsa_encrypt_pkcs1v15(ciph, &ciph_len, msg, msg_len, &pub_key, seed);
	cli_putstr_P(PSTR("\r\n  ciphertext:"));
	cli_hexdump_block(ciph, ciph_len, 4, 16);
#if DEBUG
	cli_putstr_P(PSTR("\r\n  first prime:"));
	bigint_print_hex(&priv_key.components[0]);
#endif
	cli_putstr_P(PSTR("\r\n  decrypting ... "));

	rsa_decrypt_pkcs1v15(msg_, &msg_len_, ciph, ciph_len, &priv_key, seed_out);

	cli_putstr_P(PSTR("[done]"));
	if(msg_len != msg_len_){
		char tstr[16];
		cli_putstr_P(PSTR("\r\nERROR: wrong decrypted message length ("));
		itoa(msg_len_, tstr, 10);
		cli_putstr(tstr);
		cli_putstr_P(PSTR(" instead of "));
		itoa(msg_len, tstr, 10);
		cli_putstr(tstr);
		cli_putc(')');
		goto end;
	}
	if(memcmp(msg, msg_, msg_len)){
		cli_putstr_P(PSTR("\r\nERROR: wrong decrypted message:"));
		cli_hexdump_block(msg_, msg_len_, 4, 16);
		cli_putstr_P(PSTR("\r\nreference:"));
		cli_hexdump_block(msg, msg_len, 4, 16);
		goto end;
	}

	if(memcmp(seed, seed_out, seed_len)){
		cli_putstr_P(PSTR("\r\nERROR: wrong decrypted seed:"));
		cli_hexdump_block(seed_out, seed_len, 4, 16);
		cli_putstr_P(PSTR("\r\nreference:"));
		cli_hexdump_block(seed, seed_len, 4, 16);
		goto end;
	}

	cli_putstr_P(PSTR("\r\n  >>OK<<"));
end:
	free(msg_);
	free(ciph);
	free(msg);
	free(seed_out);
	free(seed);
}