-
Notifications
You must be signed in to change notification settings - Fork 963
fix: validate ML-DSA s1/s2 coefficient bounds in wc_dilithium_check_key #10254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11456,39 +11456,58 @@ int wc_dilithium_check_key(dilithium_key* key) | |||||
| /* Get s1, s2 and t0 from private key. */ | ||||||
| dilithium_vec_decode_eta_bits(s1p, params->eta, s1, params->l); | ||||||
| dilithium_vec_decode_eta_bits(s2p, params->eta, s2, params->k); | ||||||
| dilithium_vec_decode_t0(t0p, params->k, t0); | ||||||
|
|
||||||
| /* Get t1 from public key. */ | ||||||
| dilithium_vec_decode_t1(t1p, params->k, t1); | ||||||
| /* Validate s1 and s2 coefficients are within [-eta, eta]. */ | ||||||
| { | ||||||
| sword32 eta = (sword32)params->eta; | ||||||
| word32 c; | ||||||
| for (c = 0; c < (word32)(params->l * DILITHIUM_N); c++) { | ||||||
| if (s1[c] < -eta || s1[c] > eta) { | ||||||
| ret = PUBLIC_KEY_E; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| for (c = 0; (ret == 0) && (c < (word32)(params->k * DILITHIUM_N)); c++) { | ||||||
| if (s2[c] < -eta || s2[c] > eta) { | ||||||
| ret = PUBLIC_KEY_E; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+11459
to
+11475
|
||||||
| if (ret == 0) { | ||||||
| dilithium_vec_decode_t0(t0p, params->k, t0); | ||||||
|
|
||||||
| /* Calcaluate t = NTT-1(A o NTT(s1)) + s2 */ | ||||||
| dilithium_vec_ntt_small_full(s1, params->l); | ||||||
| dilithium_matrix_mul(t, a, s1, params->k, params->l); | ||||||
| #ifdef WOLFSSL_DILITHIUM_SMALL | ||||||
| dilithium_vec_red(t, params->k); | ||||||
| #endif | ||||||
| dilithium_vec_invntt_full(t, params->k); | ||||||
| dilithium_vec_add(t, s2, params->k); | ||||||
| /* Subtract t0 from t. */ | ||||||
| dilithium_vec_sub(t, t0, params->k); | ||||||
| /* Make t positive to match t1. */ | ||||||
| dilithium_vec_make_pos(t, params->k); | ||||||
| /* Get t1 from public key. */ | ||||||
| dilithium_vec_decode_t1(t1p, params->k, t1); | ||||||
|
|
||||||
| /* Check t - t0 and t1 are the same. */ | ||||||
| for (i = 0; i < params->k; i++) { | ||||||
| for (j = 0; j < DILITHIUM_N; j++) { | ||||||
| x |= tt[j] ^ t1[j]; | ||||||
| /* Calcaluate t = NTT-1(A o NTT(s1)) + s2 */ | ||||||
|
||||||
| /* Calcaluate t = NTT-1(A o NTT(s1)) + s2 */ | |
| /* Calculate t = NTT-1(A o NTT(s1)) + s2 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This introduces data-dependent early exits (
break) while processing secret-key material (s1/s2). Ifwc_dilithium_check_key()is used in contexts where timing differences are observable, consider making this check branchless by accumulating a violation flag across all coefficients and settingret = PUBLIC_KEY_Eafter the loops complete (same behavior, less timing leakage).