opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

[otbn] Running p256_ecdsa_sign application twice in a row locks up

Open colinoflynn opened this issue 5 years ago • 24 comments

In the otbn_ecdsa_p256_test.c file trying to run the sign operation twice in a row will lock up. The simplest test case is as follows:

bool test_main() {
  entropy_testutils_boot_mode_init();
  test_ecdsa_p256_roundtrip();

  entropy_testutils_boot_mode_init();
  test_ecdsa_p256_roundtrip();

  return true;
}

This will give an output as follows:

I00000 otbn_ecdsa_p256_test.c:113] Initialization took 134635 cycles or 1346 us @ 100 MHz.
I00001 otbn_ecdsa_p256_test.c:276] Signing
I00002 otbn_ecdsa_p256_test.c:113] Sign took 762488 cycles or 7624 us @ 100 MHz.
I00003 otbn_ecdsa_p256_test.c:282] Clearing OTBN memory and reloading app
I00004 otbn_ecdsa_p256_test.c:289] Verifying
I00005 otbn_ecdsa_p256_test.c:113] Verify took 482387 cycles or 4823 us @ 100 MHz.
I00006 otbn_ecdsa_p256_test.c:301] Clearing OTBN memory
I00007 otbn_ecdsa_p256_test.c:113] Initialization took 134635 cycles or 1346 us @ 100 MHz.
I00008 otbn_ecdsa_p256_test.c:276] Signing

And it will stick on the CHECK(otbn_busy_wait_for_done(otbn_ctx) == kOtbnOk); line.

Some other tests:

  • Adjusting test_ecdsa_p256_roundtrip so it runs sign only once shows the verify operation can run twice in a row without issue, running sign is the problematic one.
  • Adding the following reset code between runs does not fix the problem:
  dif_otbn_t otbn;
  mmio_region_t addr = mmio_region_from_addr(TOP_EARLGREY_OTBN_BASE_ADDR);
  CHECK_DIF_OK(dif_otbn_init(addr, &otbn));
  dif_otbn_reset(&otbn);

Other minor changes (not calling entropy_testutils_boot_mode_init();, not calling verify) also don't have any noticeable effect. Not sure if this is OTBN related or test application related, but seems suspicious around the sign being the only problematic area.

colinoflynn avatar Oct 28 '21 12:10 colinoflynn

I've run a simulation with tracing and can confirm OTBN is stalled on an EDN request, caused by an RND read at OTBN PC 0x6a0 which is line 1062 in p256.s.

@mwbranstad can comment on what might cause an EDN request to stall forever. It may be we simply can't pull much entropy out of the EDN setup in the current earlgrey testing system.

An easy workaround for now would be to switch all uses of RND to URND. URND only reseeds once per OTBN run, where p256.s seems to be doing lots of RND reads.

GregAC avatar Oct 29 '21 10:10 GregAC

Eep! With my DV hat on, this shows that we're missing something in (probably) block-level EDN verification. Can we guess at what OTBN is doing to cause EDN to stall? If so, maybe it would be worth adding a test on the EDN side that tries to reproduce the situation. (@senelson7: I think you're doing EDN DV?)

rswarbrick avatar Oct 29 '21 10:10 rswarbrick

I don't think OTBN is doing anything unusual, just a lot of reads from RND and at some point edn1 stops responding to them.

GregAC avatar Oct 29 '21 10:10 GregAC

oh and I switched out the RND uses for URND in the ecdsa test and @colinoflynn's original failing test case works fine.

GregAC avatar Oct 29 '21 10:10 GregAC

@rswarbrick, you're correct that I'm doing EDN DV but I've been focusing more on entropy_src and csrng lately so I'll try to put more into EDN too. That being said, I don't believe this is an edn issue as much as an entropy_src complex issue. The first (boot-up) entropy is returned relatively quickly but the second takes a long time. So I believe edn is just waiting for entropy, but without really analyzing waveforms/etc., this is mainly speculation. @mwbranstad can probably give more specific details on what might be happening or offer a better explanation.

senelson7 avatar Oct 29 '21 13:10 senelson7

but without really analyzing waveforms/etc

@senelson7 I still have my trace, are there any particular signals I can look at that indicate we're awaiting new entropy?

GregAC avatar Oct 29 '21 14:10 GregAC

@mwbranstad would be better able to help you so I will defer to him.

senelson7 avatar Oct 29 '21 14:10 senelson7

I am speculating in the same way as explained by @senelson7, in that EDN is probably fine, and any long waits are most likely due to entropy_src, waiting on AST RNG. Some background: the LFSR mode was removed from entropy_src recently. This mode was added to make entropy_src seed generation go relatively quicker. Due to security concerns, this mode was removed. The rtl model for AST RNG set each entropy delivery (4 bits) at 120 clocks. I believe this value is arbitrary since in reality it is much much longer (mS range). Given this 120 clock value, I have measured these clock delays for entropy_src:

  1. boot seed = 11,316 clocks
  2. startup seed = 120,901 clocks (yeah, that's right)
  3. continuous seed = 61,343 clocks

So it could be this simulation sequence is waiting for the startup seed. I would look at the interface between CSRNG and ES and see if the request is high for this amount of time. Also, I can help look a waveform in a live session is that is useful. We do not seem to be able to run chip level simulations currently at WD. A potential time help would be to override the 120 clock value with a forced value from the test bench.

mwbranstad avatar Oct 29 '21 14:10 mwbranstad

I've looked into the trace a bit more, there's no outstanding request on the CSRNG <-> ES interface, instead it's something to do with the CSRNG <-> EDN interface.

The EDN issues two commands to the CSRNG, the first is 0x00000001 (instantiate), the second is 0x00FF0003 (generate). This takes some time to get a response and when it does the genbits_valid signal from the CSRNG into the EDN never goes high again and I believe it's this that stops the EDN from responding to the outstanding request from OTBN.

There's around 2.5 million clocks from when the second CSRNG response is received to the end of the trace.

GregAC avatar Oct 29 '21 15:10 GregAC

thanks for the extra details. Not what our theory was expecting. Based on these observations, I have no idea what is going on. Being it is late in the eurozone, let's plan a debug session next week sometime.

mwbranstad avatar Oct 29 '21 16:10 mwbranstad

@colinoflynn: Is this still something you're working on? Do you still see this being broken?

rswarbrick avatar Feb 08 '22 23:02 rswarbrick

We had worked around it for ECDSA testing using RND so it's not a real block. Running the test case above with a recent (earlier today) checkout of opentitan seems to have the same behaviour (test fails second time). It may be an "invalid" error related to something w.r.t. URND usage in the test code, in which case ok to close!

colinoflynn avatar Feb 09 '22 00:02 colinoflynn

Hmm, that's frustrating.

@mwbranstad: Looking back at the conversation above, it looks like there was a possible bug in the EDN/CSRNG interface. I know that this was all a bit flaky back in October, but it should be more robust now, I think? The fact that things are still not working for this system test suggests there's still something broken. Have you got time to look into this further? If not, someone at lowRISC can try to find time but it won't be for a few days and (at least historically) this bug doesn't seem to have been caused by our blocks.

rswarbrick avatar Feb 09 '22 09:02 rswarbrick

@rswarbrick to your question, yes, the entropy blocks are much more tested at the unit level. However, basic flow has not changed that much from when this thread started. I would like to confirm that the rtl is working properly, but we cannot seem to run chip level tests at WD because of tool issues. So I will again offer to debug this issue with a live meeting over a desktop share. Please schedule a time and let's take a look.

mwbranstad avatar Feb 09 '22 15:02 mwbranstad

@rswarbrick can you tell me what to run to reproduce this?

rasmus-madsen avatar Feb 10 '22 13:02 rasmus-madsen

If @rasmus-madsen can get this chip level test running in the WD env, I would be happy to look at this test and do local debug on it.

mwbranstad avatar Feb 10 '22 14:02 mwbranstad

  • Hack test_main in otbn_ecdsa_p256_test.c to duplicate things as described in the original report
  • Build everything (ninja -C build-out all and ci/scripts/build-chip-verilator.sh earlgrey)
  • Run the test with pytest test/systemtest/earlgrey/test_sim_verilator.py --log-cli-level=DEBUG -k otbn_ecdsa_p256_test
  • Assuming that works, you can get waves by looking at the long command line that appeared and tweaking it to add a -t argument.
    • This will run a factor of 10 or so slower. YAAAWN!

You'll end up with a sim.fst in the current directory.

rswarbrick avatar Feb 10 '22 16:02 rswarbrick

@mwbranstad and @rasmus-madsen: this should absolutely work on our servers. You will however want to make a custom nc job to get the CPU resources you need. It will not run on the log in nodes, because of the multiplicity of users. However no licenses are needed.

martin-lueker avatar Feb 10 '22 21:02 martin-lueker

@rswarbrick: I'm truly sorry, I was trying to quote your flow chart above, and instead I edited it (and deleted most of it). Most sincere apologies. Is there someway to unedit that?

martin-lueker avatar Feb 10 '22 21:02 martin-lueker

@rswarbrick: I'm truly sorry, I was trying to quote your flow chart above, and instead I edited it (and deleted most of it). Most sincere apologies. Is there someway to unedit that?

Never mind, I fixed it.

martin-lueker avatar Feb 10 '22 21:02 martin-lueker