Skip to content

Commit 784d77c

Browse files
committed
Merge PR #112.
2 parents 24fff28 + 5525a7a commit 784d77c

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

fido/src/main/java/com/yubico/yubikit/fido/ctap/Hkdf.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,29 @@ byte[] expand(byte[] prk, byte[] info, int length) throws InvalidKeyException {
6161
.put(info)
6262
.put(i)
6363
.array();
64-
t = hmacDigest(prk, data);
64+
Arrays.fill(t, (byte) 0);
65+
byte[] digest = hmacDigest(prk, data);
6566

66-
okm = ByteBuffer.allocate(okm.length + t.length)
67+
byte[] result = ByteBuffer.allocate(okm.length + digest.length)
6768
.put(okm)
68-
.put(t)
69+
.put(digest)
6970
.array();
71+
Arrays.fill(okm, (byte) 0);
72+
Arrays.fill(data, (byte) 0);
73+
okm = result;
74+
t = digest;
7075
}
7176

72-
return Arrays.copyOf(okm, length);
77+
byte[] result = Arrays.copyOf(okm, length);
78+
Arrays.fill(okm, (byte) 0);
79+
return result;
7380
}
7481

7582
byte[] digest(byte[] ikm, byte[] salt, byte[] info, int length)
7683
throws NoSuchAlgorithmException, InvalidKeyException {
7784
byte[] prk = extract(salt, ikm);
78-
return expand(prk, info, length);
85+
byte[] result = expand(prk, info, length);
86+
Arrays.fill(prk, (byte) 0);
87+
return result;
7988
}
8089
}

fido/src/main/java/com/yubico/yubikit/fido/ctap/PinUvAuthProtocolV2.java

+25-8
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ public int getVersion() {
5656

5757
@Override
5858
public byte[] kdf(byte[] z) {
59+
byte[] hmacKey = null;
60+
byte[] aesKey = null;
5961
try {
60-
byte[] hmacKey = new Hkdf(HKDF_ALG).digest(
62+
hmacKey = new Hkdf(HKDF_ALG).digest(
6163
z,
6264
HKDF_SALT,
6365
HKDF_INFO_HMAC,
6466
HKDF_LENGTH);
6567

66-
byte[] aesKey = new Hkdf(HKDF_ALG).digest(
68+
aesKey = new Hkdf(HKDF_ALG).digest(
6769
z,
6870
HKDF_SALT,
6971
HKDF_INFO_AES,
@@ -75,13 +77,21 @@ public byte[] kdf(byte[] z) {
7577
.array();
7678
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
7779
throw new IllegalStateException(e);
80+
} finally {
81+
if (hmacKey != null) {
82+
Arrays.fill(hmacKey, (byte) 0);
83+
}
84+
if (aesKey != null) {
85+
Arrays.fill(aesKey, (byte) 0);
86+
}
7887
}
7988
}
8089

8190
@Override
8291
public byte[] encrypt(byte[] key, byte[] plaintext) {
92+
byte[] aesKey = null;
8393
try {
84-
byte[] aesKey = Arrays.copyOfRange(key, 32, key.length);
94+
aesKey = Arrays.copyOfRange(key, 32, key.length);
8595
byte[] iv = RandomUtils.getRandomBytes(16);
8696

8797
final byte[] ciphertext =
@@ -93,19 +103,27 @@ public byte[] encrypt(byte[] key, byte[] plaintext) {
93103
.array();
94104
} catch (IllegalBlockSizeException | BadPaddingException e) {
95105
throw new IllegalStateException(e);
106+
} finally {
107+
if (aesKey != null) {
108+
Arrays.fill(aesKey, (byte) 0);
109+
}
96110
}
97111
}
98112

99113
@Override
100114
public byte[] decrypt(byte[] key, byte[] ciphertext) {
115+
byte[] aesKey = null;
101116
try {
102-
byte[] aesKey = Arrays.copyOfRange(key, 32, key.length);
117+
aesKey = Arrays.copyOfRange(key, 32, key.length);
103118
byte[] iv = Arrays.copyOf(ciphertext, 16);
104119
byte[] ct = Arrays.copyOfRange(ciphertext, 16, ciphertext.length);
105-
byte[] plaintext = getCipher(Cipher.DECRYPT_MODE, aesKey, iv).doFinal(ct);
106-
return Arrays.copyOf(plaintext, plaintext.length);
120+
return getCipher(Cipher.DECRYPT_MODE, aesKey, iv).doFinal(ct);
107121
} catch (BadPaddingException | IllegalBlockSizeException e) {
108122
throw new IllegalStateException(e);
123+
} finally {
124+
if (aesKey != null) {
125+
Arrays.fill(aesKey, (byte) 0);
126+
}
109127
}
110128
}
111129

@@ -120,8 +138,7 @@ public byte[] authenticate(byte[] key, byte[] message) {
120138
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
121139
throw new RuntimeException(e);
122140
}
123-
byte[] result = mac.doFinal(message);
124-
return Arrays.copyOf(result, result.length);
141+
return mac.doFinal(message);
125142
}
126143

127144
private Cipher getCipher(int mode, byte[] secret, byte[] iv) {

0 commit comments

Comments
 (0)