Skip to content
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

Fix #29: don't zero-out HKDF salt argument. #31

Merged
merged 1 commit into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/software/pando/crypto/nacl/HKDF.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Neil Madden.
* Copyright 2022-2023 Neil Madden.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,6 +59,9 @@ final class HKDF {
CryptoSecretKey extract(byte[] salt, byte[] inputKeyMaterial) {
if (salt == null || salt.length == 0) {
salt = new byte[saltLenBytes];
} else {
// Clone the salt, otherwise it will be zeroed out when the HMAC key is destroyed
salt = salt.clone();
}
try (var saltAsKey = hmacKey(salt)) {
return hmacKey(hmac(saltAsKey, inputKeyMaterial));
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/software/pando/crypto/nacl/HKDFTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Neil Madden.
* Copyright 2022-2023 Neil Madden.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,9 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.Arrays;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.internal.Digests.fromHex;

Expand Down Expand Up @@ -151,4 +154,19 @@ public void shouldMatchRfc5869TestVectors(String hashAlg, String ikmHex, String
assertThat(prk.getEncoded()).asHexString().isEqualToIgnoringCase(expectedPrkHex);
assertThat(okm).asHexString().isEqualToIgnoringCase(outputKeyMaterialHex);
}

@Test
public void shouldNotZeroOutSaltParameter() {
// Given
byte[] salt = "Test Salt".getBytes(UTF_8);
byte[] ikm = new byte[32];
Arrays.fill(ikm, (byte) 42);
var hkdf = new HKDF("HmacSHA256");

// When
hkdf.extract(salt, ikm).close();

// Then
assertThat(salt).asString(UTF_8).isEqualTo("Test Salt");
}
}