Skip to content

Commit

Permalink
8172680: Support SHA-3 based Hmac algorithms
Browse files Browse the repository at this point in the history
Enhanced SunJCE provider with Hmac with SHA3 digests

Reviewed-by: weijun
  • Loading branch information
Valerie Peng committed Apr 14, 2020
1 parent 4c7d85b commit 7bce9a9
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 55 deletions.
49 changes: 45 additions & 4 deletions src/java.base/share/classes/com/sun/crypto/provider/HmacCore.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,12 +34,20 @@
import java.security.*;
import java.security.spec.*;

import sun.security.x509.AlgorithmId;

/**
* This class constitutes the core of HMAC-<MD> algorithms, where
* <MD> can be SHA1 or MD5, etc. See RFC 2104 for spec.
* <MD> is the digest algorithm used by HMAC as in RFC 2104
* "HMAC: Keyed-Hashing for Message Authentication".
*
* It also contains the implementation classes for SHA-224, SHA-256,
* SHA-384, and SHA-512 HMACs.
* It also contains implementation classes for:
* - HmacMD5
* - HmacSHA1
* - HMAC with SHA-2 family of digests, i.e. HmacSHA224, HmacSHA256,
* HmacSHA384, HmacSHA512, HmacSHA512/224, HmacSHA512/256, and
* - HMAC with SHA-3 family of digests, i.e. HmacSHA3-224, HmacSHA3-256,
* HmacSHA3-384, HmacSHA3-512
*
* @author Jan Luehe
*/
Expand Down Expand Up @@ -281,14 +289,47 @@ public HmacSHA512() throws NoSuchAlgorithmException {
super("SHA-512", 128);
}
}

// nested static class for the HmacSHA512/224 implementation
public static final class HmacSHA512_224 extends HmacCore {
public HmacSHA512_224() throws NoSuchAlgorithmException {
super("SHA-512/224", 128);
}
}

// nested static class for the HmacSHA512/256 implementation
public static final class HmacSHA512_256 extends HmacCore {
public HmacSHA512_256() throws NoSuchAlgorithmException {
super("SHA-512/256", 128);
}
}

// nested static class for the HmacSHA3-224 implementation
public static final class HmacSHA3_224 extends HmacCore {
public HmacSHA3_224() throws NoSuchAlgorithmException {
super("SHA3-224", 144);
}
}

// nested static class for the HmacSHA3-256 implementation
public static final class HmacSHA3_256 extends HmacCore {
public HmacSHA3_256() throws NoSuchAlgorithmException {
super("SHA3-256", 136);
}
}

// nested static class for the HmacSHA3-384 implementation
public static final class HmacSHA3_384 extends HmacCore {
public HmacSHA3_384() throws NoSuchAlgorithmException {
super("SHA3-384", 104);
}
}

// nested static class for the HmacSHA3-512 implementation
public static final class HmacSHA3_512 extends HmacCore {
public HmacSHA3_512() throws NoSuchAlgorithmException {
super("SHA3-512", 72);
System.out.println(AlgorithmId.get("HmacSHA3-512"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -105,10 +105,10 @@ SecretKey implGenerateKey() {
return new SecretKeySpec(b, name);
}

// nested static classes for the HmacSHA-2 family of key generator
abstract static class HmacSHA2KG extends KeyGeneratorSpi {
// nested static classes for the Hmac key generator
abstract static class HmacKG extends KeyGeneratorSpi {
private final KeyGeneratorCore core;
protected HmacSHA2KG(String algoName, int len) {
protected HmacKG(String algoName, int len) {
core = new KeyGeneratorCore(algoName, len);
}
@Override
Expand All @@ -129,26 +129,56 @@ protected SecretKey engineGenerateKey() {
return core.implGenerateKey();
}

public static final class SHA224 extends HmacSHA2KG {
public static final class SHA224 extends HmacKG {
public SHA224() {
super("HmacSHA224", 224);
}
}
public static final class SHA256 extends HmacSHA2KG {
public static final class SHA256 extends HmacKG {
public SHA256() {
super("HmacSHA256", 256);
}
}
public static final class SHA384 extends HmacSHA2KG {
public static final class SHA384 extends HmacKG {
public SHA384() {
super("HmacSHA384", 384);
}
}
public static final class SHA512 extends HmacSHA2KG {
public static final class SHA512 extends HmacKG {
public SHA512() {
super("HmacSHA512", 512);
}
}
public static final class SHA512_224 extends HmacKG {
public SHA512_224() {
super("HmacSHA512/224", 224);
}
}
public static final class SHA512_256 extends HmacKG {
public SHA512_256() {
super("HmacSHA512/256", 256);
}
}
public static final class SHA3_224 extends HmacKG {
public SHA3_224() {
super("HmacSHA3-224", 224);
}
}
public static final class SHA3_256 extends HmacKG {
public SHA3_256() {
super("HmacSHA3-256", 256);
}
}
public static final class SHA3_384 extends HmacKG {
public SHA3_384() {
super("HmacSHA3-384", 384);
}
}
public static final class SHA3_512 extends HmacKG {
public SHA3_512() {
super("HmacSHA3-512", 512);
}
}
}

// nested static class for the RC2 key generator
Expand Down
62 changes: 53 additions & 9 deletions src/java.base/share/classes/com/sun/crypto/provider/SunJCE.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -73,7 +73,7 @@
*
* - Diffie-Hellman Key Agreement
*
* - HMAC-MD5, HMAC-SHA1, HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, HMAC-SHA-512
* - HMAC-MD5, HMAC-SHA1, HMAC with SHA2 family and SHA3 family of digests
*
*/

Expand Down Expand Up @@ -178,6 +178,18 @@ void putEntries() {
List<String> macSHA256Aliases = createAliasesWithOid(macOidBase + "9");
List<String> macSHA384Aliases = createAliasesWithOid(macOidBase + "10");
List<String> macSHA512Aliases = createAliasesWithOid(macOidBase + "11");
List<String> macSHA512_224Aliases = createAliasesWithOid(macOidBase + "12");
List<String> macSHA512_256Aliases = createAliasesWithOid(macOidBase + "13");

String nistHashAlgsOidBase = "2.16.840.1.101.3.4.2.";
List<String> macSHA3_224Aliases =
createAliasesWithOid(nistHashAlgsOidBase + "13");
List<String> macSHA3_256Aliases =
createAliasesWithOid(nistHashAlgsOidBase + "14");
List<String> macSHA3_384Aliases =
createAliasesWithOid(nistHashAlgsOidBase + "15");
List<String> macSHA3_512Aliases =
createAliasesWithOid(nistHashAlgsOidBase + "16");

// reuse attribute map and reset before each reuse
HashMap<String, String> attrs = new HashMap<>(3);
Expand Down Expand Up @@ -409,17 +421,36 @@ void putEntries() {
"com.sun.crypto.provider.HmacSHA1KeyGenerator",
macSHA1Aliases, null);
ps("KeyGenerator", "HmacSHA224",
"com.sun.crypto.provider.KeyGeneratorCore$HmacSHA2KG$SHA224",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA224",
macSHA224Aliases, null);
ps("KeyGenerator", "HmacSHA256",
"com.sun.crypto.provider.KeyGeneratorCore$HmacSHA2KG$SHA256",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA256",
macSHA256Aliases, null);
ps("KeyGenerator", "HmacSHA384",
"com.sun.crypto.provider.KeyGeneratorCore$HmacSHA2KG$SHA384",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA384",
macSHA384Aliases, null);
ps("KeyGenerator", "HmacSHA512",
"com.sun.crypto.provider.KeyGeneratorCore$HmacSHA2KG$SHA512",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512",
macSHA512Aliases, null);
ps("KeyGenerator", "HmacSHA512/224",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512_224",
macSHA512_224Aliases, null);
ps("KeyGenerator", "HmacSHA512/256",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA512_256",
macSHA512_256Aliases, null);

ps("KeyGenerator", "HmacSHA3-224",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_224",
macSHA3_224Aliases, null);
ps("KeyGenerator", "HmacSHA3-256",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_256",
macSHA3_256Aliases, null);
ps("KeyGenerator", "HmacSHA3-384",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_384",
macSHA3_384Aliases, null);
ps("KeyGenerator", "HmacSHA3-512",
"com.sun.crypto.provider.KeyGeneratorCore$HmacKG$SHA3_512",
macSHA3_512Aliases, null);

ps("KeyPairGenerator", "DiffieHellman",
"com.sun.crypto.provider.DHKeyPairGenerator",
Expand Down Expand Up @@ -678,13 +709,26 @@ void putEntries() {
macSHA384Aliases, attrs);
ps("Mac", "HmacSHA512", "com.sun.crypto.provider.HmacCore$HmacSHA512",
macSHA512Aliases, attrs);
// TODO: aliases with OIDs
ps("Mac", "HmacSHA512/224",
"com.sun.crypto.provider.HmacCore$HmacSHA512_224",
null, attrs);
macSHA512_224Aliases, attrs);
ps("Mac", "HmacSHA512/256",
"com.sun.crypto.provider.HmacCore$HmacSHA512_256",
null, attrs);
macSHA512_256Aliases, attrs);

ps("Mac", "HmacSHA3-224",
"com.sun.crypto.provider.HmacCore$HmacSHA3_224",
macSHA3_224Aliases, attrs);
ps("Mac", "HmacSHA3-256",
"com.sun.crypto.provider.HmacCore$HmacSHA3_256",
macSHA3_256Aliases, attrs);
ps("Mac", "HmacSHA3-384",
"com.sun.crypto.provider.HmacCore$HmacSHA3_384",
macSHA3_384Aliases, attrs);
ps("Mac", "HmacSHA3-512",
"com.sun.crypto.provider.HmacCore$HmacSHA3_512",
macSHA3_512Aliases, attrs);

ps("Mac", "HmacPBESHA1",
"com.sun.crypto.provider.HmacPKCS12PBECore$HmacPKCS12PBE_SHA1",
null, attrs);
Expand Down
Loading

0 comments on commit 7bce9a9

Please sign in to comment.