diff options
author | Kenny Root <kenny@the-b.org> | 2013-10-05 21:01:58 -0700 |
---|---|---|
committer | Kenny Root <kenny@the-b.org> | 2013-10-06 10:11:56 -0700 |
commit | b03fe90574ce2e3f24f8376b0af64d32f8c03515 (patch) | |
tree | b9fabb57c39657432a6f218e31efe63a81af4d1d | |
parent | b06bdfb83db5900a276a1dc5a4f9957bdf4ff9c0 (diff) | |
download | connectbot-b03fe90574ce2e3f24f8376b0af64d32f8c03515.tar.gz connectbot-b03fe90574ce2e3f24f8376b0af64d32f8c03515.tar.bz2 connectbot-b03fe90574ce2e3f24f8376b0af64d32f8c03515.zip |
Add support for HMAC-SHA2-256 and HMAC-SHA2-512
This adds support for the new MAC modes using SHA-2 family of hashes as
set forth in RFC 6668.
See http://tools.ietf.org/html/rfc6668 for more information.
Change-Id: I09dfe44efe230021a77d81546fccc6b124c958d3
-rw-r--r-- | src/com/trilead/ssh2/crypto/digest/MAC.java | 57 |
1 files changed, 48 insertions, 9 deletions
diff --git a/src/com/trilead/ssh2/crypto/digest/MAC.java b/src/com/trilead/ssh2/crypto/digest/MAC.java index 20c52fa..561599c 100644 --- a/src/com/trilead/ssh2/crypto/digest/MAC.java +++ b/src/com/trilead/ssh2/crypto/digest/MAC.java @@ -16,6 +16,36 @@ import javax.crypto.spec.SecretKeySpec; */ public final class MAC { + /** + * From http://tools.ietf.org/html/rfc4253 + */ + private static final String HMAC_MD5 = "hmac-md5"; + + /** + * From http://tools.ietf.org/html/rfc4253 + */ + private static final String HMAC_MD5_96 = "hmac-md5-96"; + + /** + * From http://tools.ietf.org/html/rfc4253 + */ + private static final String HMAC_SHA1 = "hmac-sha1"; + + /** + * From http://tools.ietf.org/html/rfc4253 + */ + private static final String HMAC_SHA1_96 = "hmac-sha1-96"; + + /** + * From http://tools.ietf.org/html/rfc6668 + */ + private static final String HMAC_SHA2_256 = "hmac-sha2-256"; + + /** + * From http://tools.ietf.org/html/rfc6668 + */ + private static final String HMAC_SHA2_512 = "hmac-sha2-512"; + Mac mac; int outSize; int macSize; @@ -23,7 +53,8 @@ public final class MAC /* Higher Priority First */ private static final String[] MAC_LIST = { - "hmac-sha1-96", "hmac-sha1", "hmac-md5-96", "hmac-md5" + HMAC_SHA2_256, HMAC_SHA2_512, + HMAC_SHA1_96, HMAC_SHA1, HMAC_MD5_96, HMAC_MD5 }; public final static String[] getMacList() @@ -39,28 +70,36 @@ public final class MAC public final static int getKeyLen(String type) { - if (type.equals("hmac-sha1")) + if (HMAC_SHA1.equals(type) || HMAC_SHA1_96.equals(type)) return 20; - if (type.equals("hmac-sha1-96")) - return 20; - if (type.equals("hmac-md5")) - return 16; - if (type.equals("hmac-md5-96")) + if (HMAC_MD5.equals(type) || HMAC_MD5_96.equals(type)) return 16; + if (HMAC_SHA2_256.equals(type)) + return 32; + if (HMAC_SHA2_512.equals(type)) + return 64; throw new IllegalArgumentException("Unkown algorithm " + type); } public MAC(String type, byte[] key) { try { - if ("hmac-sha1".equals(type) || "hmac-sha1-96".equals(type)) + if (HMAC_SHA1.equals(type) || HMAC_SHA1_96.equals(type)) { mac = Mac.getInstance("HmacSHA1"); } - else if ("hmac-md5".equals(type) || "hmac-md5-96".equals(type)) + else if (HMAC_MD5.equals(type) || HMAC_MD5_96.equals(type)) { mac = Mac.getInstance("HmacMD5"); } + else if (HMAC_SHA2_256.equals(type)) + { + mac = Mac.getInstance("HmacSHA256"); + } + else if (HMAC_SHA2_512.equals(type)) + { + mac = Mac.getInstance("HmacSHA512"); + } else throw new IllegalArgumentException("Unkown algorithm " + type); } catch (NoSuchAlgorithmException e) { |