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