Exemplo n.º 1
0
/*
 * YGH  5 
 * @signature* 需要释放的结构体
 * @pk_algorithm 外部传入参数,看协议中signature的定义
 */
static void signature_free(signature* signature, pk_algorithm algorithm  ){
	switch(algorithm){
		case ECDSA_NISTP224_WITH_SHA224:
		case ECDSA_NISTP256_WITH_SHA256:
			ecdsa_signature_free(&signature->u.ecdsa_signature);
			break;
		default:
			ARRAY_FREE(&signature->u.signature);
	}			
}
Exemplo n.º 2
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);
}