Skip to content

Commit 02683bb

Browse files
siemen11nasahlpa
authored andcommitted
[crypto/test] Test mismatching finalize to start RSA functions
Verify that calling a wrong finalize from a different start function gives a bad status output for keygen. Context is that keygen is non deterministic hence we have no cycle count. Calling a wrong finalize function normally crashes the chip due to the instruction count. For keygen this a check by the mode of the OTBN. This test covers this. Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
1 parent 51cac8c commit 02683bb

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

sw/device/lib/crypto/impl/rsa/run_rsa.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,6 @@ static status_t keygen_finalize(uint32_t exp_mode, size_t num_words,
305305
uint32_t act_mode = 0;
306306
HARDENED_TRY(otbn_dmem_read(1, kOtbnVarRsaMode, &act_mode));
307307
if (act_mode != exp_mode) {
308-
// COVERAGE (MISSING) We do not cover calling different finalize functions
309-
// from their start functions.
310308
return OTCRYPTO_FATAL_ERR;
311309
}
312310
HARDENED_CHECK_EQ(launder32(act_mode), exp_mode);

sw/device/lib/crypto/impl/rsa/run_rsa_key_from_cofactor.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ status_t rsa_keygen_from_cofactor_2048_finalize(
7676
HARDENED_TRY_WIPE_DMEM(
7777
otbn_dmem_read(kOtbnRsaModeWords, kOtbnVarRsaMode, &act_mode));
7878
if (act_mode != kOtbnRsaModeCofactor2048) {
79-
// COVERAGE (MISSING) We do not cover calling different finalize functions
80-
// from their start functions.
8179
return OTCRYPTO_FATAL_ERR;
8280
}
8381
HARDENED_CHECK_EQ(launder32(act_mode), kOtbnRsaModeCofactor2048);

sw/device/tests/crypto/rsa_2048_keygen_functest.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,77 @@ static status_t run_keygen_negative_tests(void) {
200200
return OTCRYPTO_OK;
201201
}
202202

203+
static status_t run_async_wrong_finalize_tests(void) {
204+
LOG_INFO("Running RSA async wrong-finalize negative tests.");
205+
206+
uint32_t pub_data_2048[kOtcryptoRsa2048PublicKeyBytes / 4] = {0};
207+
otcrypto_unblinded_key_t valid_pub_2048 = {
208+
.key_mode = kOtcryptoKeyModeRsaSignPkcs,
209+
.key_length = kOtcryptoRsa2048PublicKeyBytes,
210+
.key = pub_data_2048,
211+
};
212+
213+
uint32_t priv_blob_2048[kOtcryptoRsa2048PrivateKeyblobBytes / 4] = {0};
214+
otcrypto_blinded_key_t valid_priv_2048 = {
215+
.config =
216+
{
217+
.version = kOtcryptoLibVersion1,
218+
.key_mode = kOtcryptoKeyModeRsaSignPkcs,
219+
.key_length = kOtcryptoRsa2048PrivateKeyBytes,
220+
.hw_backed = kHardenedBoolFalse,
221+
.security_level = kOtcryptoKeySecurityLevelLow,
222+
},
223+
.keyblob_length = kOtcryptoRsa2048PrivateKeyblobBytes,
224+
.keyblob = priv_blob_2048,
225+
};
226+
227+
uint32_t pub_data_3072[kOtcryptoRsa3072PublicKeyBytes / 4] = {0};
228+
otcrypto_unblinded_key_t valid_pub_3072 = {
229+
.key_mode = kOtcryptoKeyModeRsaSignPkcs,
230+
.key_length = kOtcryptoRsa3072PublicKeyBytes,
231+
.key = pub_data_3072,
232+
};
233+
234+
uint32_t priv_blob_3072[kOtcryptoRsa3072PrivateKeyblobBytes / 4] = {0};
235+
otcrypto_blinded_key_t valid_priv_3072 = {
236+
.config =
237+
{
238+
.version = kOtcryptoLibVersion1,
239+
.key_mode = kOtcryptoKeyModeRsaSignPkcs,
240+
.key_length = kOtcryptoRsa3072PrivateKeyBytes,
241+
.hw_backed = kHardenedBoolFalse,
242+
.security_level = kOtcryptoKeySecurityLevelLow,
243+
},
244+
.keyblob_length = kOtcryptoRsa3072PrivateKeyblobBytes,
245+
.keyblob = priv_blob_3072,
246+
};
247+
248+
LOG_INFO("Starting 2048 standard keygen, attempting cofactor finalize...");
249+
CHECK_STATUS_OK(otcrypto_rsa_keygen_async_start(kOtcryptoRsaSize2048));
250+
251+
CHECK(otcrypto_rsa_keypair_from_cofactor_async_finalize(&valid_pub_2048,
252+
&valid_priv_2048)
253+
.value == OTCRYPTO_FATAL_ERR.value);
254+
255+
LOG_INFO(
256+
"Starting 2048 standard keygen, attempting 3072 standard finalize...");
257+
CHECK_STATUS_OK(otcrypto_rsa_keygen_async_start(kOtcryptoRsaSize2048));
258+
259+
CHECK(otcrypto_rsa_keygen_async_finalize(&valid_pub_3072, &valid_priv_3072)
260+
.value == OTCRYPTO_FATAL_ERR.value);
261+
262+
return OK_STATUS();
263+
}
264+
203265
OTTF_DEFINE_TEST_CONFIG();
204266

205267
bool test_main(void) {
206268
CHECK_STATUS_OK(otcrypto_init(kOtcryptoKeySecurityLevelLow));
207269

208270
status_t test_result = OK_STATUS();
209271
EXECUTE_TEST(test_result, keygen_then_sign_test);
272+
EXECUTE_TEST(test_result, run_async_wrong_finalize_tests);
273+
// This test leaves the OTBN running, best call it at the end.
210274
EXECUTE_TEST(test_result, run_keygen_negative_tests);
211275
return status_ok(test_result);
212276
}

0 commit comments

Comments
 (0)