J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 0000000 |
| 27 | * @summary Checks compliance of the HMAC-MD5 implementation with |
| 28 | * RFC 2104, using the test vectors specified there. |
| 29 | * @author Jan Luehe |
| 30 | */ |
| 31 | |
| 32 | import java.security.*; |
| 33 | import javax.crypto.*; |
| 34 | import javax.crypto.spec.*; |
| 35 | |
| 36 | public class HmacMD5 { |
| 37 | |
| 38 | public static void main(String argv[]) throws Exception { |
| 39 | int i, j, n; |
| 40 | Mac mac; |
| 41 | |
| 42 | Provider jce = new com.sun.crypto.provider.SunJCE(); |
| 43 | Security.addProvider(jce); |
| 44 | |
| 45 | byte[][][] test_data = { |
| 46 | { |
| 47 | { (byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b, |
| 48 | (byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b, |
| 49 | (byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b, |
| 50 | (byte)0x0b, (byte)0x0b, (byte)0x0b, (byte)0x0b }, // key1 |
| 51 | "Hi There".getBytes(), // data1 |
| 52 | { (byte)0x92, (byte)0x94, (byte)0x72, (byte)0x7a, |
| 53 | (byte)0x36, (byte)0x38, (byte)0xbb, (byte)0x1c, |
| 54 | (byte)0x13, (byte)0xf4, (byte)0x8e, (byte)0xf8, |
| 55 | (byte)0x15, (byte)0x8b, (byte)0xfc, (byte)0x9d // result1 |
| 56 | } |
| 57 | }, |
| 58 | { |
| 59 | "Jefe".getBytes(), // key2 |
| 60 | "what do ya want for nothing?".getBytes(), // data2 |
| 61 | { (byte)0x75, (byte)0x0c, (byte)0x78, (byte)0x3e, |
| 62 | (byte)0x6a, (byte)0xb0, (byte)0xb5, (byte)0x03, |
| 63 | (byte)0xea, (byte)0xa8, (byte)0x6e, (byte)0x31, |
| 64 | (byte)0x0a, (byte)0x5d, (byte)0xb7, (byte)0x38 // result2 |
| 65 | } |
| 66 | }, |
| 67 | { |
| 68 | { (byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA, |
| 69 | (byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA, |
| 70 | (byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA, |
| 71 | (byte)0xAA, (byte)0xAA, (byte)0xAA, (byte)0xAA // key3 |
| 72 | }, |
| 73 | { (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 74 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 75 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 76 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 77 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 78 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 79 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 80 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 81 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 82 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 83 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 84 | (byte)0xDD, (byte)0xDD, (byte)0xDD, (byte)0xDD, |
| 85 | (byte)0xDD, (byte)0xDD // data3 |
| 86 | }, |
| 87 | { (byte)0x56, (byte)0xbe, (byte)0x34, (byte)0x52, |
| 88 | (byte)0x1d, (byte)0x14, (byte)0x4c, (byte)0x88, |
| 89 | (byte)0xdb, (byte)0xb8, (byte)0xc7, (byte)0x33, |
| 90 | (byte)0xf0, (byte)0xe8, (byte)0xb3, (byte)0xf6 // result3 |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | mac = Mac.getInstance("HmacMD5"); |
| 96 | for (i=0; i<3; i++) { |
| 97 | j=0; |
| 98 | |
| 99 | mac.init(new SecretKeySpec(test_data[i][j++], "HMAC")); |
| 100 | byte[] result = mac.doFinal(test_data[i][j++]); |
| 101 | if (result.length != test_data[i][j].length) { |
| 102 | throw new Exception("Different result length"); |
| 103 | } |
| 104 | for (n=0; n<result.length; n++) { |
| 105 | if (result[n] != test_data[i][j][n]) { |
| 106 | throw new Exception("Different"); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // now test multiple-part operation, using the 2nd test vector |
| 112 | mac = Mac.getInstance("HmacMD5"); |
| 113 | mac.init(new SecretKeySpec("Jefe".getBytes(), "HMAC")); |
| 114 | mac.update("what do ya ".getBytes()); |
| 115 | mac.update("want for ".getBytes()); |
| 116 | mac.update("nothing?".getBytes()); |
| 117 | byte[] result = mac.doFinal(); |
| 118 | if (result.length != test_data[1][2].length) { |
| 119 | throw new Exception("Different result length"); |
| 120 | } |
| 121 | for (i=0; i<result.length; i++) { |
| 122 | if (result[i] != test_data[1][2][i]) { |
| 123 | throw new Exception("Different"); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | System.out.println("Test SUCCEEDED"); |
| 128 | } |
| 129 | } |