From 5e400ab003c1db68daf17892a7056de6c91aa5c0 Mon Sep 17 00:00:00 2001 From: YI Date: Wed, 20 Dec 2023 21:03:59 +0800 Subject: [PATCH] add more debug outputs --- src/hash/copy.c | 6 +++++- src/sig/ecdsa.c | 9 ++++++++- src/sig/sig_algs.c | 12 ++++++++++++ src/utils/dbg_sig.h | 2 ++ src/utils/print_nn.c | 4 ++-- 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/hash/copy.c b/src/hash/copy.c index 103336bc..6fde3af0 100644 --- a/src/hash/copy.c +++ b/src/hash/copy.c @@ -18,6 +18,7 @@ void copy256_update(copy256_context *ctx, const u8 *input, u32 ilen) MUST_HAVE((ctx != NULL) && (input != NULL)); + hex_dump("bytes to update", input, ilen); /* Nothing to process, return */ if (ilen == 0) { return; @@ -39,7 +40,10 @@ void copy256_final(copy256_context *ctx, u8 output[COPY256_SIZE]) { MUST_HAVE((ctx != NULL) && (output != NULL)); MUST_HAVE(ctx->copied_bytes == COPY256_SIZE); - local_memcpy(ctx->buffer, output, COPY256_SIZE); + printf("Copied byts %d, COPY256_SIZE %d, sizeof(output)%d\n", ctx->copied_bytes, COPY256_SIZE, sizeof(output)); + hex_dump("ctx->buffer", ctx->buffer, COPY256_SIZE); + local_memcpy(output, ctx->buffer, COPY256_SIZE); + hex_dump("output", output, COPY256_SIZE); } void copy256_scattered(const u8 **inputs, const u32 *ilens, diff --git a/src/sig/ecdsa.c b/src/sig/ecdsa.c index d5cde302..52cc9131 100644 --- a/src/sig/ecdsa.c +++ b/src/sig/ecdsa.c @@ -24,7 +24,7 @@ #include "ec_key.h" #include "../utils/utils.h" #ifdef VERBOSE_INNER_VALUES -#define EC_SIG_ALG "ECDSA" +#define EC_SIG_ALG " ECDSA" #endif #include "../utils/dbg_sig.h" @@ -443,6 +443,7 @@ int _ecdsa_verify_init(struct ec_verify_context *ctx, const u8 *sig, u8 siglen) goto err; } + dbg_buf_print("sig", sig, siglen); /* Import r and s values from signature buffer */ nn_init_from_buf(r, sig, q_len); nn_init_from_buf(s, sig + q_len, q_len); @@ -506,6 +507,7 @@ int _ecdsa_verify_finalize(struct ec_verify_context *ctx) u8 hsize; int ret; + dbg_buf_print("hash", hash, sizeof(hash)); /* * First, verify context has been initialized and public * part too. This guarantees the context is an ECDSA @@ -522,6 +524,8 @@ int _ecdsa_verify_finalize(struct ec_verify_context *ctx) hsize = ctx->h->digest_size; r = &(ctx->verify_data.ecdsa.r); s = &(ctx->verify_data.ecdsa.s); + dbg_nn_print("r: ", r); + dbg_nn_print("s: ", s); /* 2. Compute h = H(m) */ ctx->h->hfunc_finalize(&(ctx->verify_data.ecdsa.h_ctx), hash); @@ -585,6 +589,7 @@ int _ecdsa_verify_finalize(struct ec_verify_context *ctx) // prj_pt_copy(&W_prime, Y); // prj_pt_copy(&W_prime, G); + printf("calculating wnaf \n", __func__, ret); prj_pt_ec_mult_wnaf(&W_prime, &u, G, &v, Y); // prj_pt_copy(&uG, G); // prj_pt_copy(&vY, Y); @@ -599,6 +604,7 @@ int _ecdsa_verify_finalize(struct ec_verify_context *ctx) nn_uninit(&v); /* 8. If W' is the point at infinity, reject the signature. */ + printf("checking W_prime is zero \n", __func__, ret); if (prj_pt_iszero(&W_prime)) { ret = -1; goto err; @@ -614,6 +620,7 @@ int _ecdsa_verify_finalize(struct ec_verify_context *ctx) // aff_pt_uninit(&W_prime_aff); /* 10. Accept the signature if and only if r equals r' */ + printf("checking r_prime is zero \n", __func__, ret); ret = (nn_cmp(&r_prime, r) != 0) ? -1 : 0; nn_uninit(&r_prime); diff --git a/src/sig/sig_algs.c b/src/sig/sig_algs.c index cb105e85..25a5d930 100644 --- a/src/sig/sig_algs.c +++ b/src/sig/sig_algs.c @@ -281,6 +281,7 @@ int ec_verify_init(struct ec_verify_context *ctx, const ec_pub_key *pub_key, u8 i; int ret; + printf("Hello from %s, ret: %d\n", __func__, ret); MUST_HAVE(ctx != NULL); pub_key_check_initialized(pub_key); if (pub_key->key_type != sig_type) { @@ -301,6 +302,8 @@ int ec_verify_init(struct ec_verify_context *ctx, const ec_pub_key *pub_key, goto err; } + printf("Hello from %s, ret: %d\n", __func__, ret); + printf("Hello from %s, ret: %d\n", __func__, ret); /* Now, let's try and get the specific key algorithm which was requested */ ret = -1; for (i = 0, sm = &ec_sig_maps[i]; @@ -314,6 +317,7 @@ int ec_verify_init(struct ec_verify_context *ctx, const ec_pub_key *pub_key, goto err; } + printf("Hello from %s, ret: %d\n", __func__, ret); /* Sanity checks on our mappings */ HASH_MAPPING_SANITY_CHECK(hm); SIG_MAPPING_SANITY_CHECK(sm); @@ -324,7 +328,9 @@ int ec_verify_init(struct ec_verify_context *ctx, const ec_pub_key *pub_key, ctx->sig = sm; ctx->ctx_magic = SIG_VERIFY_MAGIC; + printf("Hello from before verify init %s, ret: %d\n", __func__, ret); ret = sm->verify_init(ctx, sig, siglen); + printf("Hello from %s, ret: %d\n", __func__, ret); err: @@ -363,7 +369,9 @@ int ec_verify_finalize(struct ec_verify_context *ctx) SIG_MAPPING_SANITY_CHECK(ctx->sig); HASH_MAPPING_SANITY_CHECK(ctx->h); + printf("Hello from %s, ret: %d\n", __func__, ret); ret = ctx->sig->verify_finalize(ctx); + printf("Hello from %s, ret: %d\n", __func__, ret); /* Clear the whole context to prevent future reuse */ local_memset(ctx, 0, sizeof(struct ec_verify_context)); @@ -378,17 +386,21 @@ int ec_verify(const u8 *sig, u8 siglen, const ec_pub_key *pub_key, int ret; struct ec_verify_context ctx; + printf("Hello from %s, ret: %d\n", __func__, ret); ret = ec_verify_init(&ctx, pub_key, sig, siglen, sig_type, hash_type); if (ret) { goto err; } + printf("Hello from %s, ret: %d\n", __func__, ret); ret = ec_verify_update(&ctx, m, mlen); if (ret) { goto err; } + printf("Hello from %s, ret: %d\n", __func__, ret); ret = ec_verify_finalize(&ctx); + printf("Hello from %s, ret: %d\n", __func__, ret); err: return ret; diff --git a/src/utils/dbg_sig.h b/src/utils/dbg_sig.h index 1788a302..654c3fc6 100644 --- a/src/utils/dbg_sig.h +++ b/src/utils/dbg_sig.h @@ -25,8 +25,10 @@ #endif #define dbg_buf_print(msg, ...) do {\ + hex_dump(msg, __VA_ARGS__);\ } while(0); #define dbg_nn_print(msg, ...) do {\ + printf("Running from %s", __func__);\ nn_print(EC_SIG_ALG " " msg, __VA_ARGS__);\ } while(0); #define dbg_ec_point_print(msg, ...) do {\ diff --git a/src/utils/print_nn.c b/src/utils/print_nn.c index 7a2a1c61..7f4ca94d 100644 --- a/src/utils/print_nn.c +++ b/src/utils/print_nn.c @@ -22,11 +22,11 @@ void nn_print(const char *msg, nn_src_t a) nn_check_initialized(a); - ext_printf("%s (%d words, i.e. %d bits): 0x", msg, a->wlen, + printf("%s (%d words, i.e. %d bits): 0x", msg, a->wlen, a->wlen * WORD_BYTES * 8); for (w = a->wlen - 1; w >= 0; w--) { - ext_printf(PRINTF_WORD_HEX_FMT, a->val[w]); + printf(PRINTF_WORD_HEX_FMT, a->val[w]); } ext_printf("\n");