Skip to content

Commit 2c7cfcf

Browse files
siemen11nasahlpa
authored andcommitted
[crypto/test] Add a p384 on curve test
The on curve check for p384 consists of two equality verifications. This is due to the coordinate size being 384 bits large which only fit in two OTBN registers. Create a negative test that triggers exactly that second equality check. Signed-off-by: Siemen Dhooghe <sdhooghe@google.com>
1 parent 3866395 commit 2c7cfcf

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

sw/device/tests/crypto/ecc_p384_point_on_curve_check_functest.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ status_t point_valid_test(void) {
8080
return OTCRYPTO_OK;
8181
}
8282

83+
status_t point_partial_collision_test(void) {
84+
// This constructs a point where (Y^2 % p) and (X^3 - 3X + b % p)
85+
// share the same lower 256 bits, but differ in the upper 128 bits.
86+
// x
87+
p384_point_t point_invalid_raw = {
88+
.x = {0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
89+
0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
90+
0x00000000, 0x00000000},
91+
.y = {0x8d32a89f, 0xf4b1cd74, 0x274c130d, 0xcf3a3e8a, 0x0116d5e6,
92+
0x64d700b3, 0x51d7182c, 0x5465e170, 0x080a67b2, 0x23b8ad4d,
93+
0x983109dc, 0x0de970b2}};
94+
95+
uint32_t pt_invld_buf[kP384PointWords];
96+
memcpy(pt_invld_buf, &point_invalid_raw, sizeof(point_invalid_raw));
97+
98+
otcrypto_unblinded_key_t point_invalid = {
99+
.key_length = sizeof(pt_invld_buf),
100+
.key = pt_invld_buf,
101+
};
102+
103+
hardened_bool_t result;
104+
TRY(otcrypto_ecc_p384_point_on_curve(&point_invalid, &result));
105+
106+
// The OTBN routine will pass the `bn.cmp w4, w6` check,
107+
// but fail the subsequent `bn.cmp w5, w7` check, trigger an ecall,
108+
// and safely return HARDENED_BOOL_FALSE to the host.
109+
if (result != kHardenedBoolFalse) {
110+
LOG_ERROR("Partial collision point bypassed point check.");
111+
return OTCRYPTO_RECOV_ERR;
112+
}
113+
114+
return OTCRYPTO_OK;
115+
}
116+
83117
OTTF_DEFINE_TEST_CONFIG();
84118

85119
bool test_main(void) {
@@ -95,5 +129,13 @@ bool test_main(void) {
95129
return false;
96130
}
97131

132+
err = point_partial_collision_test();
133+
if (!status_ok(err)) {
134+
LOG_INFO("OTBN error bits: 0x%08x", otbn_err_bits_get());
135+
LOG_INFO("OTBN instruction count: 0x%08x", otbn_instruction_count_get());
136+
CHECK_STATUS_OK(err);
137+
return false;
138+
}
139+
98140
return true;
99141
}

0 commit comments

Comments
 (0)