Skip to content

Commit 71e89a8

Browse files
committed
avoid expensive call to Mac.getInstance
1 parent 3191970 commit 71e89a8

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

src/main/java/com/amazonaws/encryptionsdk/CryptoAlgorithm.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.amazonaws.encryptionsdk.internal.CommittedKey;
88
import com.amazonaws.encryptionsdk.internal.Constants;
99
import com.amazonaws.encryptionsdk.internal.HmacKeyDerivationFunction;
10+
import com.amazonaws.encryptionsdk.internal.MacAlgorithm;
1011
import com.amazonaws.encryptionsdk.model.CiphertextHeaders;
1112
import java.nio.ByteBuffer;
1213
import java.nio.ByteOrder;
@@ -342,7 +343,7 @@ private SecretKey getCommittedEncryptionKey(
342343

343344
private SecretKey getNonCommittedEncryptionKey(
344345
final SecretKey dataKey, final CiphertextHeaders headers) throws InvalidKeyException {
345-
final String macAlgorithm;
346+
final MacAlgorithm macAlgorithm;
346347

347348
switch (this) {
348349
case ALG_AES_128_GCM_IV12_TAG16_NO_KDF:
@@ -353,11 +354,11 @@ private SecretKey getNonCommittedEncryptionKey(
353354
case ALG_AES_192_GCM_IV12_TAG16_HKDF_SHA256:
354355
case ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA256:
355356
case ALG_AES_128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256:
356-
macAlgorithm = "HmacSHA256";
357+
macAlgorithm = MacAlgorithm.HmacSHA256;
357358
break;
358359
case ALG_AES_192_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384:
359360
case ALG_AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384:
360-
macAlgorithm = "HmacSHA384";
361+
macAlgorithm = MacAlgorithm.HmacSHA384;
361362
break;
362363
default:
363364
throw new UnsupportedOperationException("Support for " + this + " not yet built.");
@@ -393,3 +394,4 @@ private SecretKey getNonCommittedEncryptionKey(
393394
return new SecretKeySpec(hkdf.deriveKey(info.array(), getKeyLength()), getKeyAlgo());
394395
}
395396
}
397+

src/main/java/com/amazonaws/encryptionsdk/internal/CommittedKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public static CommittedKey generate(CryptoAlgorithm alg, SecretKey dataKey, byte
8080
+ rawDataKey.length);
8181
}
8282

83-
final String macAlgorithm;
83+
final MacAlgorithm macAlgorithm;
8484
switch (alg.getKeyCommitmentAlgo_()) {
8585
case HKDF_SHA_512:
86-
macAlgorithm = HMAC_SHA_512;
86+
macAlgorithm = MacAlgorithm.HkdfSHA512;
8787
break;
8888
default:
8989
throw new UnsupportedOperationException(

src/main/java/com/amazonaws/encryptionsdk/internal/HmacKeyDerivationFunction.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ public final class HmacKeyDerivationFunction {
4848
* @throws NoSuchAlgorithmException if no Provider supports a MacSpi implementation for the
4949
* specified algorithm.
5050
*/
51-
public static HmacKeyDerivationFunction getInstance(final String algorithm)
51+
public static HmacKeyDerivationFunction getInstance(final MacAlgorithm algorithm)
5252
throws NoSuchAlgorithmException {
53-
// Constructed specifically to sanity-test arguments.
54-
Mac mac = Mac.getInstance(algorithm);
55-
return new HmacKeyDerivationFunction(algorithm, mac.getProvider());
53+
return new HmacKeyDerivationFunction(algorithm);
5654
}
5755

5856
/**
@@ -94,12 +92,9 @@ public void init(final byte[] ikm, final byte[] salt) {
9492
}
9593
}
9694

97-
private HmacKeyDerivationFunction(final String algorithm, final Provider provider) {
98-
isTrue(
99-
algorithm.startsWith("Hmac"),
100-
"Invalid algorithm " + algorithm + ". Hkdf may only be used with Hmac algorithms.");
101-
this.algorithm = algorithm;
102-
this.provider = provider;
95+
private HmacKeyDerivationFunction(final MacAlgorithm algorithm) throws NoSuchAlgorithmException {
96+
this.algorithm = algorithm.getAlgorithm();
97+
this.provider = algorithm.getProvider();
10398
}
10499

105100
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.amazonaws.encryptionsdk.internal;
2+
3+
import javax.crypto.Mac;
4+
import java.security.NoSuchAlgorithmException;
5+
import java.security.Provider;
6+
7+
public enum MacAlgorithm {
8+
HmacSHA256("HmacSHA256"),
9+
HmacSHA384("HmacSHA384"),
10+
HmacSHA512("HmacSHA512"),
11+
HkdfSHA512("HkdfSHA512"),
12+
HmacSHA1("HmacSHA1");
13+
private final String algorithm;
14+
private Provider provider;
15+
16+
MacAlgorithm(String algorithm) {
17+
this.algorithm = algorithm;
18+
}
19+
20+
Provider getProvider() throws NoSuchAlgorithmException {
21+
Provider provider = this.provider;
22+
if (provider == null) {
23+
provider = Mac.getInstance(algorithm).getProvider();
24+
this.provider = provider;
25+
}
26+
return provider;
27+
}
28+
29+
String getAlgorithm() {
30+
return algorithm;
31+
}
32+
}

src/test/java/com/amazonaws/encryptionsdk/internal/CommittedKeyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void testGenerateCommittedKeySmokeTest() throws Exception {
108108
final byte[] n1 = insecureRandomBytes(32);
109109

110110
// Hash for HKDF is SHA-512
111-
final HmacKeyDerivationFunction hkdf = HmacKeyDerivationFunction.getInstance("HmacSHA512");
111+
final HmacKeyDerivationFunction hkdf = HmacKeyDerivationFunction.getInstance(MacAlgorithm.HmacSHA512);
112112

113113
// K_R (Raw keying material, a.k.a. data key) is 256 bits (32 bytes)
114114
// Normally this needs to be cryptographically secure, but we can relax this for improved

src/test/java/com/amazonaws/encryptionsdk/internal/HmacKeyDerivationFunctionTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class HmacKeyDerivationFunctionTest {
2121
private static final testCase[] testCases =
2222
new testCase[] {
2323
new testCase(
24-
"HmacSHA256",
24+
MacAlgorithm.HmacSHA256,
2525
fromCHex(
2626
"\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"
2727
+ "\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"),
@@ -30,7 +30,7 @@ public class HmacKeyDerivationFunctionTest {
3030
fromHex(
3131
"3CB25F25FAACD57A90434F64D0362F2A2D2D0A90CF1A5A4C5DB02D56ECC4C5BF34007208D5B887185865")),
3232
new testCase(
33-
"HmacSHA256",
33+
MacAlgorithm.HmacSHA256,
3434
fromCHex(
3535
"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d"
3636
+ "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b"
@@ -60,7 +60,7 @@ public class HmacKeyDerivationFunctionTest {
6060
+ "CC30C58179EC3E87C14C01D5C1F3434F"
6161
+ "1D87")),
6262
new testCase(
63-
"HmacSHA256",
63+
MacAlgorithm.HmacSHA256,
6464
fromCHex(
6565
"\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"
6666
+ "\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"),
@@ -71,7 +71,7 @@ public class HmacKeyDerivationFunctionTest {
7171
+ "B8A11F5C5EE1879EC3454E5F3C738D2D"
7272
+ "9D201395FAA4B61A96C8")),
7373
new testCase(
74-
"HmacSHA1",
74+
MacAlgorithm.HmacSHA1,
7575
fromCHex("\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"),
7676
fromCHex("\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c"),
7777
fromCHex("\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9"),
@@ -80,7 +80,7 @@ public class HmacKeyDerivationFunctionTest {
8080
+ "A4F14B822F5B091568A9CDD4F155FDA2"
8181
+ "C22E422478D305F3F896")),
8282
new testCase(
83-
"HmacSHA1",
83+
MacAlgorithm.HmacSHA1,
8484
fromCHex(
8585
"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\\x0a\\x0b\\x0c\\x0d"
8686
+ "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b"
@@ -109,15 +109,15 @@ public class HmacKeyDerivationFunctionTest {
109109
+ "486EA37CE3D397ED034C7F9DFEB15C5E"
110110
+ "927336D0441F4C4300E2CFF0D0900B52D3B4")),
111111
new testCase(
112-
"HmacSHA1",
112+
MacAlgorithm.HmacSHA1,
113113
fromCHex(
114114
"\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"
115115
+ "\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b\\x0b"),
116116
new byte[0],
117117
new byte[0],
118118
fromHex("0AC1AF7002B3D761D1E55298DA9D0506" + "B9AE52057220A306E07B6B87E8DF21D0")),
119119
new testCase(
120-
"HmacSHA1",
120+
MacAlgorithm.HmacSHA1,
121121
fromCHex(
122122
"\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c"
123123
+ "\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c\\x0c"),
@@ -200,13 +200,13 @@ private static byte[] fromCHex(String data) {
200200
}
201201

202202
private static class testCase {
203-
public final String algo;
203+
public final MacAlgorithm algo;
204204
public final byte[] ikm;
205205
public final byte[] salt;
206206
public final byte[] info;
207207
public final byte[] expected;
208208

209-
testCase(String algo, byte[] ikm, byte[] salt, byte[] info, byte[] expected) {
209+
testCase(MacAlgorithm algo, byte[] ikm, byte[] salt, byte[] info, byte[] expected) {
210210
super();
211211
this.algo = algo;
212212
this.ikm = ikm;

0 commit comments

Comments
 (0)