001: package org.bouncycastle.jce.provider.test;
002:
003: import java.security.InvalidAlgorithmParameterException;
004: import java.security.InvalidKeyException;
005: import java.security.Security;
006:
007: import javax.crypto.KeyGenerator;
008: import javax.crypto.Mac;
009: import javax.crypto.SecretKey;
010: import javax.crypto.spec.RC5ParameterSpec;
011: import javax.crypto.spec.SecretKeySpec;
012:
013: import org.bouncycastle.jce.provider.BouncyCastleProvider;
014: import org.bouncycastle.util.encoders.Hex;
015: import org.bouncycastle.util.test.SimpleTest;
016: import org.bouncycastle.asn1.iana.IANAObjectIdentifiers;
017: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
018:
019: /**
020: * HMAC tester
021: */
022: public class HMacTest extends SimpleTest {
023: static byte[] keyBytes = Hex
024: .decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
025: static byte[] message = "Hi There".getBytes();
026: static byte[] output1 = Hex
027: .decode("b617318655057264e28bc0b6fb378c8ef146be00");
028: static byte[] outputMD5 = Hex
029: .decode("5ccec34ea9656392457fa1ac27f08fbc");
030: static byte[] outputMD2 = Hex
031: .decode("dc1923ef5f161d35bef839ca8c807808");
032: static byte[] outputMD4 = Hex
033: .decode("5570ce964ba8c11756cdc3970278ff5a");
034: static byte[] output224 = Hex
035: .decode("896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22");
036: static byte[] output256 = Hex
037: .decode("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
038: static byte[] output384 = Hex
039: .decode("afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6");
040: static byte[] output512 = Hex
041: .decode("87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854");
042: static byte[] outputRipeMD128 = Hex
043: .decode("fda5717fb7e20cf05d30bb286a44b05d");
044: static byte[] outputRipeMD160 = Hex
045: .decode("24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668");
046: static byte[] outputTiger = Hex
047: .decode("1d7a658c75f8f004916e7b07e2a2e10aec7de2ae124d3647");
048: static byte[] outputOld384 = Hex
049: .decode("0a046aaa0255e432912228f8ccda437c8a8363fb160afb0570ab5b1fd5ddc20eb1888b9ed4e5b6cb5bc034cd9ef70e40");
050: static byte[] outputOld512 = Hex
051: .decode("9656975ee5de55e75f2976ecce9a04501060b9dc22a6eda2eaef638966280182477fe09f080b2bf564649cad42af8607a2bd8d02979df3a980f15e2326a0a22a");
052:
053: public HMacTest() {
054: }
055:
056: public void testHMac(String hmacName, byte[] output)
057: throws Exception {
058: SecretKey key = new SecretKeySpec(keyBytes, hmacName);
059: byte[] out;
060: Mac mac;
061:
062: mac = Mac.getInstance(hmacName, "BC");
063:
064: mac.init(key);
065:
066: mac.reset();
067:
068: mac.update(message, 0, message.length);
069:
070: out = mac.doFinal();
071:
072: if (!areEqual(out, output)) {
073: fail("Failed - expected " + new String(Hex.encode(output))
074: + " got " + new String(Hex.encode(out)));
075: }
076:
077: // no key generator for the old algorithms
078: if (hmacName.startsWith("Old")) {
079: return;
080: }
081:
082: KeyGenerator kGen = KeyGenerator.getInstance(hmacName, "BC");
083:
084: mac.init(kGen.generateKey());
085:
086: mac.update(message);
087:
088: out = mac.doFinal();
089: }
090:
091: private void testExceptions() throws Exception {
092: Mac mac = null;
093:
094: mac = Mac.getInstance("HmacSHA1", "BC");
095:
096: byte[] b = { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5 };
097: SecretKeySpec sks = new SecretKeySpec(b, "HmacSHA1");
098: RC5ParameterSpec algPS = new RC5ParameterSpec(100, 100, 100);
099:
100: try {
101: mac.init(sks, algPS);
102: } catch (InvalidAlgorithmParameterException e) {
103: // ignore okay
104: }
105:
106: try {
107: mac.init(null, null);
108: } catch (InvalidKeyException e) {
109: // ignore okay
110: } catch (InvalidAlgorithmParameterException e) {
111: // ignore okay
112: }
113:
114: try {
115: mac.init(null);
116: } catch (InvalidKeyException e) {
117: // ignore okay
118: }
119: }
120:
121: public void performTest() throws Exception {
122: testHMac("HMac-SHA1", output1);
123: testHMac("HMac-MD5", outputMD5);
124: testHMac("HMac-MD4", outputMD4);
125: testHMac("HMac-MD2", outputMD2);
126: testHMac("HMac-SHA224", output224);
127: testHMac("HMac-SHA256", output256);
128: testHMac("HMac-SHA384", output384);
129: testHMac("HMac-SHA512", output512);
130: testHMac("HMac-RIPEMD128", outputRipeMD128);
131: testHMac("HMac-RIPEMD160", outputRipeMD160);
132: testHMac("HMac-TIGER", outputTiger);
133:
134: testHMac("HMac/SHA1", output1);
135: testHMac("HMac/MD5", outputMD5);
136: testHMac("HMac/MD4", outputMD4);
137: testHMac("HMac/MD2", outputMD2);
138: testHMac("HMac/SHA224", output224);
139: testHMac("HMac/SHA256", output256);
140: testHMac("HMac/SHA384", output384);
141: testHMac("HMac/SHA512", output512);
142: testHMac("HMac/RIPEMD128", outputRipeMD128);
143: testHMac("HMac/RIPEMD160", outputRipeMD160);
144: testHMac("HMac/TIGER", outputTiger);
145:
146: testHMac(PKCSObjectIdentifiers.id_hmacWithSHA1.getId(), output1);
147: testHMac(PKCSObjectIdentifiers.id_hmacWithSHA224.getId(),
148: output224);
149: testHMac(PKCSObjectIdentifiers.id_hmacWithSHA256.getId(),
150: output256);
151: testHMac(PKCSObjectIdentifiers.id_hmacWithSHA384.getId(),
152: output384);
153: testHMac(PKCSObjectIdentifiers.id_hmacWithSHA512.getId(),
154: output512);
155: testHMac(IANAObjectIdentifiers.hmacSHA1.getId(), output1);
156: testHMac(IANAObjectIdentifiers.hmacMD5.getId(), outputMD5);
157: testHMac(IANAObjectIdentifiers.hmacRIPEMD160.getId(),
158: outputRipeMD160);
159: testHMac(IANAObjectIdentifiers.hmacTIGER.getId(), outputTiger);
160:
161: // test for compatibility with broken HMac.
162: testHMac("OldHMacSHA384", outputOld384);
163: testHMac("OldHMacSHA512", outputOld512);
164:
165: testExceptions();
166: }
167:
168: public String getName() {
169: return "HMac";
170: }
171:
172: public static void main(String[] args) {
173: Security.addProvider(new BouncyCastleProvider());
174:
175: runTest(new HMacTest());
176: }
177: }
|