Skip to content

Commit 3eb41af

Browse files
dgarskedanielinux
authored andcommitted
Fixes for ECC sign where the r/s is does not match key size and needs zero padded.
1 parent e73fcf3 commit 3eb41af

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

tools/keytools/sign.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -864,18 +864,26 @@ static int sign_digest(int sign, int hash_algo,
864864
sign == SIGN_ECC521)
865865
{
866866
mp_int r, s;
867-
int sigSz;
868-
if (sign == SIGN_ECC256) sigSz = 32;
869-
if (sign == SIGN_ECC384) sigSz = 48;
870-
if (sign == SIGN_ECC521) sigSz = 66;
867+
int keySz;
868+
if (sign == SIGN_ECC256) keySz = 32;
869+
if (sign == SIGN_ECC384) keySz = 48;
870+
if (sign == SIGN_ECC521) keySz = 66;
871+
872+
*signature_sz = keySz*2;
873+
memset(signature, 0, *signature_sz);
871874

872875
mp_init(&r); mp_init(&s);
873876
ret = wc_ecc_sign_hash_ex(digest, digest_sz, &rng, &key.ecc,
874877
&r, &s);
875-
mp_to_unsigned_bin(&r, &signature[0]);
876-
mp_to_unsigned_bin(&s, &signature[sigSz]);
878+
if (ret == 0) {
879+
word32 rSz, sSz;
880+
/* export sign r/s - zero pad to key size */
881+
rSz = mp_unsigned_bin_size(&r);
882+
mp_to_unsigned_bin(&r, &signature[keySz - rSz]);
883+
sSz = mp_unsigned_bin_size(&s);
884+
mp_to_unsigned_bin(&s, &signature[keySz + (keySz - sSz)]);
885+
}
877886
mp_clear(&r); mp_clear(&s);
878-
*signature_sz = sigSz*2;
879887
}
880888
else
881889
#endif

tools/tpm/policy_sign.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,16 @@ static int PolicySign(int alg, const char* keyFile, byte* hash, word32 hashSz,
140140
rc = wc_ecc_sign_hash_ex(hash, hashSz, &rng, &key.ecc, &r, &s);
141141
}
142142
if (rc == 0) {
143-
mp_to_unsigned_bin(&r, sig);
144-
mp_to_unsigned_bin(&s, sig + keySz);
143+
word32 rSz, sSz;
144+
*sigSz = keySz * 2;
145+
memset(sig, 0, *sigSz);
146+
/* export sign r/s - zero pad to key size */
147+
rSz = mp_unsigned_bin_size(&r);
148+
mp_to_unsigned_bin(&r, &sig[keySz - rSz]);
149+
sSz = mp_unsigned_bin_size(&s);
150+
mp_to_unsigned_bin(&s, &sig[keySz + (keySz - sSz)]);
145151
mp_clear(&r);
146152
mp_clear(&s);
147-
*sigSz = keySz * 2;
148153
}
149154
}
150155
wc_ecc_free(&key.ecc);

0 commit comments

Comments
 (0)