Skip to content

Commit a13dc9a

Browse files
committed
Left pad the ECDSA r and s so a leading zero cannot shift the signature
1 parent 2ef04f0 commit a13dc9a

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

pk/ecc/ecc_sign.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,15 @@ int crypto_ecc_sign(const uint8_t *key, uint32_t keySz,
263263
&r, &s /* r/s as mp_int */
264264
);
265265

266-
/* export r/s */
267-
mp_to_unsigned_bin(&r, sig);
268-
mp_to_unsigned_bin(&s, sig + curveSz);
266+
/* export r/s, left-padded: verify reads them back at fixed curveSz
267+
* offsets, and mp_to_unsigned_bin writes only the significant bytes, so
268+
* an r or s with a leading zero (~1 in 256) would shift s and fail */
269+
if (ret == 0) {
270+
ret = mp_to_unsigned_bin_len(&r, sig, curveSz);
271+
}
272+
if (ret == 0) {
273+
ret = mp_to_unsigned_bin_len(&s, sig + curveSz, curveSz);
274+
}
269275
}
270276

271277
mp_clear(&r);

0 commit comments

Comments
 (0)