Skip to content

Commit 6971a97

Browse files
committed
added support for SHA2/SHA3/KMAC key storage and retrieval
1 parent a912b69 commit 6971a97

File tree

8 files changed

+249
-31
lines changed

8 files changed

+249
-31
lines changed

prov/src/main/java/org/bouncycastle/jcajce/provider/digest/SHA1.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
1010
import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
1111
import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
12+
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
1213
import org.bouncycastle.jcajce.provider.symmetric.util.PBESecretKeyFactory;
1314

1415
public class SHA1
@@ -78,7 +79,16 @@ public static class PBEWithMacKeyFactory
7879
{
7980
public PBEWithMacKeyFactory()
8081
{
81-
super("PBEwithHmacSHA", null, false, PKCS12, SHA1, 160, 0);
82+
super("PBEwithHmacSHA1", null, false, PKCS12, SHA1, 160, 0);
83+
}
84+
}
85+
86+
static public class KeyFactory
87+
extends BaseSecretKeyFactory
88+
{
89+
public KeyFactory()
90+
{
91+
super("HmacSHA1", null);
8292
}
8393
}
8494

@@ -109,6 +119,8 @@ public void configure(ConfigurableProvider provider)
109119
provider.addAlgorithm("Alg.Alias.Mac." + OIWObjectIdentifiers.idSHA1, "PBEWITHHMACSHA");
110120

111121
provider.addAlgorithm("SecretKeyFactory.PBEWITHHMACSHA1", PREFIX + "$PBEWithMacKeyFactory");
122+
provider.addAlgorithm("SecretKeyFactory.HMACSHA1", PREFIX + "$KeyFactory");
123+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA1, "HMACSHA1");
112124
}
113125
}
114126
}

prov/src/main/java/org/bouncycastle/jcajce/provider/digest/SHA224.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
99
import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
1010
import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
11+
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
1112

1213
public class SHA224
1314
{
@@ -44,6 +45,15 @@ public HashMac()
4445
}
4546
}
4647

48+
static public class KeyFactory
49+
extends BaseSecretKeyFactory
50+
{
51+
public KeyFactory()
52+
{
53+
super("HmacSHA224", null);
54+
}
55+
}
56+
4757
public static class KeyGenerator
4858
extends BaseKeyGenerator
4959
{
@@ -73,6 +83,8 @@ public void configure(ConfigurableProvider provider)
7383
addHMACAlgorithm(provider, "SHA224", PREFIX + "$HashMac", PREFIX + "$KeyGenerator");
7484
addHMACAlias(provider, "SHA224", PKCSObjectIdentifiers.id_hmacWithSHA224);
7585

86+
provider.addAlgorithm("SecretKeyFactory.HMACSHA224", PREFIX + "$KeyFactory");
87+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA224, "HMACSHA224");
7688
}
7789
}
7890
}

prov/src/main/java/org/bouncycastle/jcajce/provider/digest/SHA256.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
99
import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
1010
import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
11+
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
1112
import org.bouncycastle.jcajce.provider.symmetric.util.PBESecretKeyFactory;
1213

1314
public class SHA256
@@ -57,6 +58,15 @@ public PBEWithMacKeyFactory()
5758
}
5859
}
5960

61+
static public class KeyFactory
62+
extends BaseSecretKeyFactory
63+
{
64+
public KeyFactory()
65+
{
66+
super("HmacSHA256", null);
67+
}
68+
}
69+
6070
/**
6171
* HMACSHA256
6272
*/
@@ -93,6 +103,9 @@ public void configure(ConfigurableProvider provider)
93103
addHMACAlgorithm(provider, "SHA256", PREFIX + "$HashMac", PREFIX + "$KeyGenerator");
94104
addHMACAlias(provider, "SHA256", PKCSObjectIdentifiers.id_hmacWithSHA256);
95105
addHMACAlias(provider, "SHA256", NISTObjectIdentifiers.id_sha256);
106+
107+
provider.addAlgorithm("SecretKeyFactory.HMACSHA256", PREFIX + "$KeyFactory");
108+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA256, "HMACSHA256");
96109
}
97110
}
98111
}

prov/src/main/java/org/bouncycastle/jcajce/provider/digest/SHA3.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.bouncycastle.jcajce.provider.digest;
22

3+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
34
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
45
import org.bouncycastle.crypto.CipherKeyGenerator;
56
import org.bouncycastle.crypto.digests.ParallelHash;
@@ -11,6 +12,7 @@
1112
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
1213
import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
1314
import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
15+
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
1416

1517
public class SHA3
1618
{
@@ -104,6 +106,78 @@ public HashMacSHA3(int size)
104106
}
105107
}
106108

109+
static public class KeyFactorySHA3
110+
extends BaseSecretKeyFactory
111+
{
112+
public KeyFactorySHA3(int size, ASN1ObjectIdentifier algOid)
113+
{
114+
super("HmacSHA3-" + size, algOid);
115+
}
116+
}
117+
118+
static public class KeyFactory224
119+
extends KeyFactorySHA3
120+
{
121+
public KeyFactory224()
122+
{
123+
super(224, NISTObjectIdentifiers.id_hmacWithSHA3_224);
124+
}
125+
}
126+
127+
static public class KeyFactory256
128+
extends KeyFactorySHA3
129+
{
130+
public KeyFactory256()
131+
{
132+
super(256, NISTObjectIdentifiers.id_hmacWithSHA3_256);
133+
}
134+
}
135+
136+
static public class KeyFactory384
137+
extends KeyFactorySHA3
138+
{
139+
public KeyFactory384()
140+
{
141+
super(384, NISTObjectIdentifiers.id_hmacWithSHA3_384);
142+
}
143+
}
144+
145+
static public class KeyFactory512
146+
extends KeyFactorySHA3
147+
{
148+
public KeyFactory512()
149+
{
150+
super(512, NISTObjectIdentifiers.id_hmacWithSHA3_512);
151+
}
152+
}
153+
154+
static public class KeyFactoryKMAC
155+
extends BaseSecretKeyFactory
156+
{
157+
public KeyFactoryKMAC(int size, ASN1ObjectIdentifier algOid)
158+
{
159+
super("KMAC" + size, algOid);
160+
}
161+
}
162+
163+
static public class KeyFactoryKMAC128
164+
extends KeyFactoryKMAC
165+
{
166+
public KeyFactoryKMAC128()
167+
{
168+
super(128, NISTObjectIdentifiers.id_KmacWithSHAKE128);
169+
}
170+
}
171+
172+
static public class KeyFactoryKMAC256
173+
extends KeyFactoryKMAC
174+
{
175+
public KeyFactoryKMAC256()
176+
{
177+
super(256, NISTObjectIdentifiers.id_KmacWithSHAKE256);
178+
}
179+
}
180+
107181
public static class KeyGeneratorSHA3
108182
extends BaseKeyGenerator
109183
{
@@ -321,18 +395,31 @@ public void configure(ConfigurableProvider provider)
321395

322396
addHMACAlgorithm(provider, "SHA3-224", PREFIX + "$HashMac224", PREFIX + "$KeyGenerator224");
323397
addHMACAlias(provider, "SHA3-224", NISTObjectIdentifiers.id_hmacWithSHA3_224);
398+
provider.addAlgorithm("SecretKeyFactory.HMACSHA3-224", PREFIX + "$KeyFactory224");
399+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_hmacWithSHA3_224, "HMACSHA3-224");
324400

325401
addHMACAlgorithm(provider, "SHA3-256", PREFIX + "$HashMac256", PREFIX + "$KeyGenerator256");
326402
addHMACAlias(provider, "SHA3-256", NISTObjectIdentifiers.id_hmacWithSHA3_256);
403+
provider.addAlgorithm("SecretKeyFactory.HMACSHA3-256", PREFIX + "$KeyFactory256");
404+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_hmacWithSHA3_256, "HMACSHA3-256");
327405

328406
addHMACAlgorithm(provider, "SHA3-384", PREFIX + "$HashMac384", PREFIX + "$KeyGenerator384");
329407
addHMACAlias(provider, "SHA3-384", NISTObjectIdentifiers.id_hmacWithSHA3_384);
408+
provider.addAlgorithm("SecretKeyFactory.HMACSHA3-384", PREFIX + "$KeyFactory384");
409+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_hmacWithSHA3_384, "HMACSHA3-384");
330410

331411
addHMACAlgorithm(provider, "SHA3-512", PREFIX + "$HashMac512", PREFIX + "$KeyGenerator512");
332412
addHMACAlias(provider, "SHA3-512", NISTObjectIdentifiers.id_hmacWithSHA3_512);
413+
provider.addAlgorithm("SecretKeyFactory.HMACSHA3-512", PREFIX + "$KeyFactory512");
414+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_hmacWithSHA3_512, "HMACSHA3-512");
333415

334416
addKMACAlgorithm(provider, "128", PREFIX + "$KMac128", PREFIX + "$KeyGenerator256");
417+
provider.addAlgorithm("SecretKeyFactory.KMAC128", PREFIX + "$KeyFactoryKMAC128");
418+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_Kmac128, "KMAC128");
419+
335420
addKMACAlgorithm(provider, "256", PREFIX + "$KMac256", PREFIX + "$KeyGenerator512");
421+
provider.addAlgorithm("SecretKeyFactory.KMAC256", PREFIX + "$KeyFactoryKMAC256");
422+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + NISTObjectIdentifiers.id_Kmac256, "KMAC256");
336423

337424
provider.addAlgorithm("MessageDigest.TUPLEHASH256-512", PREFIX + "$DigestTupleHash256_512");
338425
provider.addAlgorithm("MessageDigest.TUPLEHASH128-256", PREFIX + "$DigestTupleHash128_256");

prov/src/main/java/org/bouncycastle/jcajce/provider/digest/SHA384.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
1010
import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
1111
import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
12+
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
1213

1314
public class SHA384
1415
{
@@ -45,6 +46,15 @@ public HashMac()
4546
}
4647
}
4748

49+
static public class KeyFactory
50+
extends BaseSecretKeyFactory
51+
{
52+
public KeyFactory()
53+
{
54+
super("HmacSHA384", null);
55+
}
56+
}
57+
4858
/**
4959
* HMACSHA384
5060
*/
@@ -86,6 +96,9 @@ public void configure(ConfigurableProvider provider)
8696

8797
addHMACAlgorithm(provider, "SHA384", PREFIX + "$HashMac", PREFIX + "$KeyGenerator");
8898
addHMACAlias(provider, "SHA384", PKCSObjectIdentifiers.id_hmacWithSHA384);
99+
100+
provider.addAlgorithm("SecretKeyFactory.HMACSHA384", PREFIX + "$KeyFactory");
101+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA384, "HMACSHA384");
89102
}
90103
}
91104
}

prov/src/main/java/org/bouncycastle/jcajce/provider/digest/SHA512.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
1111
import org.bouncycastle.jcajce.provider.symmetric.util.BaseKeyGenerator;
1212
import org.bouncycastle.jcajce.provider.symmetric.util.BaseMac;
13+
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
1314

1415
public class SHA512
1516
{
@@ -101,6 +102,33 @@ public HashMacT256()
101102
}
102103
}
103104

105+
static public class KeyFactory
106+
extends BaseSecretKeyFactory
107+
{
108+
public KeyFactory()
109+
{
110+
super("HmacSHA512", null);
111+
}
112+
}
113+
114+
static public class KeyFactory224
115+
extends BaseSecretKeyFactory
116+
{
117+
public KeyFactory224()
118+
{
119+
super("HmacSHA512/224", null);
120+
}
121+
}
122+
123+
static public class KeyFactory256
124+
extends BaseSecretKeyFactory
125+
{
126+
public KeyFactory256()
127+
{
128+
super("HmacSHA512/256", null);
129+
}
130+
}
131+
104132
/**
105133
* SHA-512 HMac
106134
*/
@@ -181,6 +209,17 @@ public void configure(ConfigurableProvider provider)
181209

182210
addHMACAlgorithm(provider, "SHA512/224", PREFIX + "$HashMacT224", PREFIX + "$KeyGeneratorT224");
183211
addHMACAlgorithm(provider, "SHA512/256", PREFIX + "$HashMacT256", PREFIX + "$KeyGeneratorT256");
212+
213+
provider.addAlgorithm("SecretKeyFactory.HMACSHA512", PREFIX + "$KeyFactory");
214+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA512, "HMACSHA512");
215+
216+
provider.addAlgorithm("SecretKeyFactory.HMACSHA512/224", PREFIX + "$KeyFactory224");
217+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory.HMACSHA512(224)", "HMACSHA512/224");
218+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA512_224, "HMACSHA512/224");
219+
220+
provider.addAlgorithm("SecretKeyFactory.HMACSHA512/256", PREFIX + "$KeyFactory256");
221+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory.HMACSHA512(256)", "HMACSHA512/256");
222+
provider.addAlgorithm("Alg.Alias.SecretKeyFactory." + PKCSObjectIdentifiers.id_hmacWithSHA512_256, "HMACSHA512/256");
184223
}
185224
}
186225

prov/src/main/java/org/bouncycastle/jcajce/provider/keystore/bcfks/BcFKSKeyStoreSpi.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ class BcFKSKeyStoreSpi
118118
oidMap.put("HMACSHA256", PKCSObjectIdentifiers.id_hmacWithSHA256);
119119
oidMap.put("HMACSHA384", PKCSObjectIdentifiers.id_hmacWithSHA384);
120120
oidMap.put("HMACSHA512", PKCSObjectIdentifiers.id_hmacWithSHA512);
121+
oidMap.put("HMACSHA512/224", PKCSObjectIdentifiers.id_hmacWithSHA512_224);
122+
oidMap.put("HMACSHA512/256", PKCSObjectIdentifiers.id_hmacWithSHA512_256);
123+
oidMap.put("HMACSHA512(224)", PKCSObjectIdentifiers.id_hmacWithSHA512_224);
124+
oidMap.put("HMACSHA512(256)", PKCSObjectIdentifiers.id_hmacWithSHA512_256);
125+
oidMap.put("HMACSHA3-224", NISTObjectIdentifiers.id_hmacWithSHA3_224);
126+
oidMap.put("HMACSHA3-256", NISTObjectIdentifiers.id_hmacWithSHA3_256);
127+
oidMap.put("HMACSHA3-384", NISTObjectIdentifiers.id_hmacWithSHA3_384);
128+
oidMap.put("HMACSHA3-512", NISTObjectIdentifiers.id_hmacWithSHA3_512);
129+
oidMap.put("KMAC128", NISTObjectIdentifiers.id_Kmac128);
130+
oidMap.put("KMAC256", NISTObjectIdentifiers.id_Kmac256);
121131
oidMap.put("SEED", KISAObjectIdentifiers.id_seedCBC);
122132

123133
oidMap.put("CAMELLIA.128", NTTObjectIdentifiers.id_camellia128_cbc);

0 commit comments

Comments
 (0)