Skip to content

Commit

Permalink
Fix macFactory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickfav committed Oct 3, 2017
1 parent 6f5b3a5 commit 68483fe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>at.favre.lib</groupId>
<artifactId>hkdf</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<packaging>jar</packaging>

<name>HKDF-RFC5869</name>
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/at/favre/lib/crypto/HkdfMacFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface HkdfMacFactory {
*/
final class Default implements HkdfMacFactory {
private final String macAlgorithmName;
private final int hashLengthBytes;
private final Provider provider;

/**
Expand All @@ -35,7 +34,7 @@ final class Default implements HkdfMacFactory {
* @return factory
*/
public static HkdfMacFactory hmacSha256() {
return new Default("HmacSHA256", 256 / 8, null);
return new Default("HmacSHA256", null);
}

/**
Expand All @@ -44,7 +43,7 @@ public static HkdfMacFactory hmacSha256() {
* @return factory
*/
public static HkdfMacFactory hmacSha512() {
return new Default("HmacSHA512", 512 / 8, null);
return new Default("HmacSHA512", null);
}

/**
Expand All @@ -55,29 +54,26 @@ public static HkdfMacFactory hmacSha512() {
*/
@Deprecated
public static HkdfMacFactory hmacSha1() {
return new Default("HmacSHA1", 160 / 8, null);
return new Default("HmacSHA1", null);
}

/**
* Creates a mac factory
*
* @param macAlgorithmName as used by {@link Mac#getInstance(String)}
* @param hashLengthBytes hash length size of used mac in bytes
*/
public Default(String macAlgorithmName, int hashLengthBytes) {
this(macAlgorithmName, hashLengthBytes, null);
public Default(String macAlgorithmName) {
this(macAlgorithmName, null);
}

/**
* Creates a mac factory
*
* @param macAlgorithmName as used by {@link Mac#getInstance(String)}
* @param hashLengthBytes hash length size of used mac in bytes
* @param provider what security provider, see {@link Mac#getInstance(String, Provider)}; may be null to use default
*/
public Default(String macAlgorithmName, int hashLengthBytes, Provider provider) {
public Default(String macAlgorithmName, Provider provider) {
this.macAlgorithmName = macAlgorithmName;
this.hashLengthBytes = hashLengthBytes;
this.provider = provider;
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/at/favre/lib/crypto/HKDFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void simpleUseCase() throws Exception {
@Test
public void customHmac() throws Exception {
//don't use md5, this is just an example
HKDF hkdfMd5 = HKDF.from(new HkdfMacFactory.Default("HmacMD5", 16, Security.getProvider("SunJCE")));
HKDF hkdfMd5 = HKDF.from(new HkdfMacFactory.Default("HmacMD5", Security.getProvider("SunJCE")));

byte[] lowEntropyInput = new byte[]{0x62, 0x58, (byte) 0x84, 0x2C};
byte[] outputKeyingMaterial = hkdfMd5.extractAndExpand(null, lowEntropyInput, null, 32);
Expand All @@ -93,7 +93,7 @@ public void checkLength() throws Exception {
checkLength(HKDF.fromHmacSha512().extract(salt, ikm), 64);
checkLength(HKDF.fromHmacSha512().extract(null, ikm), 64);
checkLength(HKDF.from(HkdfMacFactory.Default.hmacSha1()).extract(salt, ikm), 20);
checkLength(HKDF.from(new HkdfMacFactory.Default("HmacMD5", 16)).extract(ikm, salt), 16);
checkLength(HKDF.from(new HkdfMacFactory.Default("HmacMD5")).extract(ikm, salt), 16);

assertFalse(Arrays.equals(HKDF.fromHmacSha256().extract(salt, ikm), HKDF.fromHmacSha512().extract(salt, ikm)));
assertFalse(Arrays.equals(HKDF.fromHmacSha256().extract(salt, ikm), HKDF.from(HkdfMacFactory.Default.hmacSha1()).extract(salt, ikm)));
Expand Down Expand Up @@ -135,7 +135,7 @@ public void testExpand() throws Exception {
checkLength(HKDF.fromHmacSha512().expand(prk, info, lengthOut), lengthOut);
checkLength(HKDF.fromHmacSha512().expand(prk, null, lengthOut), lengthOut);
checkLength(HKDF.from(HkdfMacFactory.Default.hmacSha1()).expand(prk, info, lengthOut), lengthOut);
checkLength(HKDF.from(new HkdfMacFactory.Default("HmacMD5", 16)).expand(prk, info, lengthOut), lengthOut);
checkLength(HKDF.from(new HkdfMacFactory.Default("HmacMD5")).expand(prk, info, lengthOut), lengthOut);

if (lengthOut > 4) {
assertFalse(Arrays.equals(HKDF.fromHmacSha256().expand(prk, info, lengthOut), HKDF.fromHmacSha512().expand(prk, info, lengthOut)));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/at/favre/lib/crypto/HkdfMacFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ public void hmacSha512() throws Exception {

@Test
public void hmacSha1() throws Exception {
testHmacFactory(new HkdfMacFactory.Default("HmacSHA1", 20), 20);
testHmacFactory(new HkdfMacFactory.Default("HmacSHA1", null), 20);
}

@Test
public void hmacMd5() throws Exception {
testHmacFactory(new HkdfMacFactory.Default("HmacMD5", 16), 16);
testHmacFactory(new HkdfMacFactory.Default("HmacMD5"), 16);
}

@Test
public void customProvider() throws Exception {
testHmacFactory(new HkdfMacFactory.Default("HmacSHA1", 20, Security.getProvider("SunJCE")), 20);
testHmacFactory(new HkdfMacFactory.Default("HmacSHA1", Security.getProvider("SunJCE")), 20);
}

@Test(expected = RuntimeException.class)
public void hmacInstanceNotExisting() throws Exception {
new HkdfMacFactory.Default("HmacNotExisting", 16).createInstance(new byte[16]);
new HkdfMacFactory.Default("HmacNotExisting", null).createInstance(new byte[16]);
}

private void testHmacFactory(HkdfMacFactory macFactory, int refLength) {
Expand Down

0 comments on commit 68483fe

Please sign in to comment.