88#include "sw/device/lib/base/hardened.h"
99#include "sw/device/lib/base/hardened_memory.h"
1010#include "sw/device/lib/crypto/drivers/otbn.h"
11+ #include "sw/device/lib/crypto/include/integrity.h"
1112
1213#include "hw/top_earlgrey/sw/autogen/top_earlgrey.h"
1314
@@ -31,6 +32,9 @@ OTBN_DECLARE_SYMBOL_ADDR(run_p256, k0_io); // Secret scalar k (share 0).
3132OTBN_DECLARE_SYMBOL_ADDR (run_p256 , k1_io ); // Secret scalar k (share 1).
3233OTBN_DECLARE_SYMBOL_ADDR (run_p256 , x_r ); // ECDSA verification result.
3334OTBN_DECLARE_SYMBOL_ADDR (run_p256 , ok ); // Status code.
35+ OTBN_DECLARE_SYMBOL_ADDR (
36+ run_p256 ,
37+ attestation_additional_seed ); // Additional seed for attestation keygen.
3438
3539static const otbn_addr_t kOtbnVarMode = OTBN_ADDR_T_INIT (run_p256 , mode );
3640static const otbn_addr_t kOtbnVarMsg = OTBN_ADDR_T_INIT (run_p256 , msg );
@@ -44,6 +48,8 @@ static const otbn_addr_t kOtbnVarK0 = OTBN_ADDR_T_INIT(run_p256, k0_io);
4448static const otbn_addr_t kOtbnVarK1 = OTBN_ADDR_T_INIT (run_p256 , k1_io );
4549static const otbn_addr_t kOtbnVarXr = OTBN_ADDR_T_INIT (run_p256 , x_r );
4650static const otbn_addr_t kOtbnVarOk = OTBN_ADDR_T_INIT (run_p256 , ok );
51+ static const otbn_addr_t kOtbnVarBootAttestationAdditionalSeed =
52+ OTBN_ADDR_T_INIT (run_p256 , attestation_additional_seed );
4753
4854// Declare mode constants.
4955OTBN_DECLARE_SYMBOL_ADDR (run_p256 , MODE_KEYGEN );
@@ -99,12 +105,12 @@ enum {
99105 * The expected instruction counts for constant time functions.
100106 */
101107 kModeKeygenInsCnt = 573915 ,
102- kModeKeygenSideloadInsCnt = 573800 ,
108+ kModeKeygenSideloadInsCnt = 573807 ,
103109 kModeEcdhInsCnt = 581600 ,
104- kModeEcdhSideloadInsCnt = 581658 ,
110+ kModeEcdhSideloadInsCnt = 581665 ,
105111 kModeEcdsaSignConfigKInsCnt = 606939 ,
106112 kModeEcdsaSignInsCnt = 607089 ,
107- kModeEcdsaSignSideloadInsCnt = 607147 ,
113+ kModeEcdsaSignSideloadInsCnt = 607154 ,
108114 kModePointOnCurveCheckInsCnt = 224 ,
109115 kModeBasePointMultInsCnt = 573749 ,
110116 kModeShareSecretKeyInsCnt = 147 ,
@@ -188,6 +194,36 @@ status_t p256_sideload_keygen_start(void) {
188194 // Set mode so start() will jump into sideload-keygen.
189195 uint32_t mode = kOtbnP256ModeSideloadKeygen ;
190196 HARDENED_TRY (otbn_dmem_write (kOtbnP256ModeWords , & mode , kOtbnVarMode ));
197+ // No attestation seed is used.
198+ HARDENED_TRY (otbn_dmem_set (kDiceAttestationMaxSeedLength , 0 ,
199+ kOtbnVarBootAttestationAdditionalSeed ));
200+
201+ // Start the OTBN routine.
202+ return otbn_execute ();
203+ }
204+
205+ status_t p256_sideload_attestation_keygen_start (
206+ otcrypto_const_word32_buf_t * attestation_seed ) {
207+ if (launder32 (attestation_seed -> len ) > kDiceAttestationMaxSeedLength ) {
208+ return OTCRYPTO_BAD_ARGS ;
209+ }
210+
211+ HARDENED_CHECK_EQ (kHardenedBoolTrue , OTCRYPTO_CHECK_BUF (attestation_seed ));
212+
213+ // Load the P-256 app. Fails if OTBN is non-idle.
214+ HARDENED_TRY (otbn_load_app (kOtbnAppP256 ));
215+
216+ // Set mode so start() will jump into sideload-keygen.
217+ uint32_t mode = kOtbnP256ModeSideloadKeygen ;
218+ HARDENED_TRY (otbn_dmem_write (kOtbnP256ModeWords , & mode , kOtbnVarMode ));
219+
220+ HARDENED_TRY (otbn_dmem_write (attestation_seed -> len , attestation_seed -> data ,
221+ kOtbnVarBootAttestationAdditionalSeed ));
222+ // Pad the remainder by zeros.
223+ HARDENED_TRY (
224+ otbn_dmem_set (kDiceAttestationMaxSeedLength - attestation_seed -> len , 0 ,
225+ kOtbnVarBootAttestationAdditionalSeed +
226+ attestation_seed -> len * sizeof (uint32_t )));
191227
192228 // Start the OTBN routine.
193229 return otbn_execute ();
@@ -309,6 +345,9 @@ status_t p256_ecdsa_sideload_sign_start(
309345 // Set mode so start() will jump into sideloaded signing.
310346 uint32_t mode = kOtbnP256ModeSideloadSign ;
311347 HARDENED_TRY (otbn_dmem_write (kOtbnP256ModeWords , & mode , kOtbnVarMode ));
348+ // No attestation seed is used.
349+ HARDENED_TRY (otbn_dmem_set (kDiceAttestationMaxSeedLength , 0 ,
350+ kOtbnVarBootAttestationAdditionalSeed ));
312351
313352 // Set the message digest.
314353 HARDENED_TRY (set_message_digest (digest ));
@@ -317,6 +356,37 @@ status_t p256_ecdsa_sideload_sign_start(
317356 return otbn_execute ();
318357}
319358
359+ status_t p256_sideload_attestation_sign_start (
360+ const uint32_t digest [kP256ScalarWords ],
361+ otcrypto_const_word32_buf_t * attestation_seed ) {
362+ if (launder32 (attestation_seed -> len ) > kDiceAttestationMaxSeedLength ) {
363+ return OTCRYPTO_BAD_ARGS ;
364+ }
365+ HARDENED_CHECK_EQ (kHardenedBoolTrue , OTCRYPTO_CHECK_BUF (attestation_seed ));
366+
367+ // Load the P-256 app. Fails if OTBN is non-idle.
368+ HARDENED_TRY (otbn_load_app (kOtbnAppP256 ));
369+
370+ // Set mode so start() will jump into sideloaded signing.
371+ uint32_t mode = kOtbnP256ModeSideloadSign ;
372+ HARDENED_TRY (otbn_dmem_write (kOtbnP256ModeWords , & mode , kOtbnVarMode ));
373+
374+ // Set the message digest.
375+ HARDENED_TRY (set_message_digest (digest ));
376+
377+ // Write the attestation seed to the extra variables area.
378+ HARDENED_TRY (otbn_dmem_write (attestation_seed -> len , attestation_seed -> data ,
379+ kOtbnVarBootAttestationAdditionalSeed ));
380+ // Pad the remainder by zeros.
381+ HARDENED_TRY (
382+ otbn_dmem_set (kDiceAttestationMaxSeedLength - attestation_seed -> len , 0 ,
383+ kOtbnVarBootAttestationAdditionalSeed +
384+ attestation_seed -> len * sizeof (uint32_t )));
385+
386+ // Start the OTBN routine.
387+ return otbn_execute ();
388+ }
389+
320390status_t p256_ecdsa_sign_finalize (p256_ecdsa_signature_t * result ) {
321391 uint32_t ins_cnt ;
322392 // Spin here waiting for OTBN to complete.
@@ -472,6 +542,10 @@ status_t p256_sideload_ecdh_start(const p256_point_t *public_key) {
472542 // Set the public key y coordinate.
473543 HARDENED_TRY (otbn_dmem_write (kP256CoordWords , public_key -> y , kOtbnVarY ));
474544
545+ // No attestation seed is used.
546+ HARDENED_TRY (otbn_dmem_set (kDiceAttestationMaxSeedLength , 0 ,
547+ kOtbnVarBootAttestationAdditionalSeed ));
548+
475549 // Start the OTBN routine.
476550 return otbn_execute ();
477551}
0 commit comments