Skip to content

Commit

Permalink
add more debug outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
contrun committed Dec 20, 2023
1 parent 0a20345 commit 5e400ab
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/hash/copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/sig/ecdsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions src/sig/sig_algs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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:

Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/dbg_sig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {\
Expand Down
4 changes: 2 additions & 2 deletions src/utils/print_nn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 5e400ab

Please sign in to comment.