move chain code outside pubkey

This commit is contained in:
Michele Balistreri 2022-11-04 10:33:05 +03:00
parent f3e834226f
commit a3aba74ffa
2 changed files with 24 additions and 21 deletions

View File

@ -1199,14 +1199,17 @@ public class KeycardApplet extends Applet {
apduBuffer[off++] = TLV_PUB_KEY;
off++;
len = secp256k1.derivePublicKey(derivationOutput, (short) 0, apduBuffer, off);
if (extendedPublic) {
Util.arrayCopyNonAtomic(derivationOutput, Crypto.KEY_SECRET_SIZE, apduBuffer, (short) (off + len), CHAIN_CODE_SIZE);
len += CHAIN_CODE_SIZE;
}
apduBuffer[(short) (off - 1)] = (byte) len;
off += len;
if (extendedPublic) {
apduBuffer[off++] = TLV_CHAIN_CODE;
off++;
Util.arrayCopyNonAtomic(derivationOutput, Crypto.KEY_SECRET_SIZE, apduBuffer, off, CHAIN_CODE_SIZE);
len = CHAIN_CODE_SIZE;
apduBuffer[(short) (off - 1)] = (byte) len;
off += len;
}
} else {
apduBuffer[off++] = TLV_PRIV_KEY;
off++;

View File

@ -1687,30 +1687,30 @@ public class KeycardTest {
return;
}
System.out.println(Hex.toHexString(keyTemplate));
DeterministicKey dk = deriveKey(keyPair, chainCode, path);
ECKey key = dk.decompress();
assertEquals(KeycardApplet.TLV_KEY_TEMPLATE, keyTemplate[0]);
int pubKeyLen = 0;
if (publicOnly) {
assertEquals(KeycardApplet.TLV_PUB_KEY, keyTemplate[2]);
byte[] pubKey = Arrays.copyOfRange(keyTemplate, 4, 4 + keyTemplate[3]);
byte[] correctPub = key.getPubKey();
if (extendedPublic) {
byte[] chain = dk.getChainCode();
int len = correctPub.length;
correctPub = Arrays.copyOf(correctPub, len + chain.length);
System.arraycopy(chain, 0, correctPub, len, chain.length);
}
assertArrayEquals(key.getPubKey(), pubKey);
int templateLen = 2 + pubKey.length;
assertArrayEquals(correctPub, pubKey);
pubKeyLen = 2 + pubKey.length;
assertEquals(pubKeyLen, keyTemplate[1]);
assertEquals(pubKeyLen + 2, keyTemplate.length);
if (extendedPublic) {
byte[] chain = Arrays.copyOfRange(keyTemplate, templateLen + 4, templateLen + 4 + keyTemplate[3 + templateLen]);
assertEquals(KeycardApplet.TLV_CHAIN_CODE, keyTemplate[2 + templateLen]);
assertArrayEquals(dk.getChainCode(), chain);
templateLen += 2 + chain.length;
}
assertEquals(templateLen, keyTemplate[1]);
assertEquals(templateLen + 2, keyTemplate.length);
} else {
assertEquals(KeycardApplet.TLV_PRIV_KEY, keyTemplate[2 + pubKeyLen]);
byte[] privateKey = Arrays.copyOfRange(keyTemplate, 4 + pubKeyLen, 4 + pubKeyLen + keyTemplate[3 + pubKeyLen]);
assertEquals(KeycardApplet.TLV_PRIV_KEY, keyTemplate[2]);
byte[] privateKey = Arrays.copyOfRange(keyTemplate, 4, 4 + keyTemplate[3]);
byte[] tPrivKey = key.getPrivKey().toByteArray();