1 /* 2 * Copyright (c) 2020, Huawei Technologies Co. Ltd. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import jdk.test.lib.Asserts; 25 26 import java.security.MessageDigest; 27 import java.util.Arrays; 28 29 /** 30 * @test 31 * @bug 8252204 32 * @library /test/lib 33 * @summary testing SHA3-224/256/384/512. 34 */ 35 public class SHA3 { 36 37 static byte[] msg1600bits; 38 static { 39 msg1600bits = new byte[200]; 40 for (int i = 0; i < 200; i++) 41 msg1600bits[i] = (byte) 0xa3; 42 } 43 44 public static void main(String[] args) throws Exception { 45 46 MessageDigest md; 47 48 // Test vectors obtained from 49 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-224_Msg0.pdf 50 md = MessageDigest.getInstance("SHA3-224"); 51 Asserts.assertTrue(Arrays.equals(md.digest("".getBytes()), 52 xeh("6B4E0342 3667DBB7 3B6E1545 4F0EB1AB D4597F9A 1B078E3F 5B5A6BC7"))); 53 // Test vectors obtained from 54 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-224_1600.pdf 55 Asserts.assertTrue(Arrays.equals(md.digest(msg1600bits), 56 xeh("9376816A BA503F72 F96CE7EB 65AC095D EEE3BE4B F9BBC2A1 CB7E11E0"))); 57 58 // Test vectors obtained from 59 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_Msg0.pdf 60 md = MessageDigest.getInstance("SHA3-256"); 61 Asserts.assertTrue(Arrays.equals(md.digest("".getBytes()), 62 xeh("A7FFC6F8 BF1ED766 51C14756 A061D662 F580FF4D E43B49FA 82D80A4B 80F8434A"))); 63 // Test vectors obtained from 64 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-256_1600.pdf 65 Asserts.assertTrue(Arrays.equals(md.digest(msg1600bits), 66 xeh("79F38ADE C5C20307 A98EF76E 8324AFBF D46CFD81 B22E3973 C65FA1BD 9DE31787"))); 67 68 // Test vectors obtained from 69 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_Msg0.pdf 70 md = MessageDigest.getInstance("SHA3-384"); 71 Asserts.assertTrue(Arrays.equals(md.digest("".getBytes()), 72 xeh("0C63A75B 845E4F7D 01107D85 2E4C2485 C51A50AA AA94FC61 995E71BB EE983A2A" + 73 "C3713831 264ADB47 FB6BD1E0 58D5F004"))); 74 // Test vectors obtained from 75 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-384_1600.pdf 76 Asserts.assertTrue(Arrays.equals(md.digest(msg1600bits), 77 xeh("1881DE2C A7E41EF9 5DC4732B 8F5F002B 189CC1E4 2B74168E D1732649 CE1DBCDD" + 78 "76197A31 FD55EE98 9F2D7050 DD473E8F"))); 79 80 // Test vectors obtained from 81 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_Msg0.pdf 82 md = MessageDigest.getInstance("SHA3-512"); 83 Asserts.assertTrue(Arrays.equals(md.digest("".getBytes()), 84 xeh("A69F73CC A23A9AC5 C8B567DC 185A756E 97C98216 4FE25859 E0D1DCC1 475C80A6" + 85 "15B2123A F1F5F94C 11E3E940 2C3AC558 F500199D 95B6D3E3 01758586 281DCD26"))); 86 // Test vectors obtaned from 87 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA3-512_1600.pdf 88 Asserts.assertTrue(Arrays.equals(md.digest(msg1600bits), 89 xeh("E76DFAD2 2084A8B1 467FCF2F FA58361B EC7628ED F5F3FDC0 E4805DC4 8CAEECA8" + 90 "1B7C13C3 0ADF52A3 65958473 9A2DF46B E589C51C A1A4A841 6DF6545A 1CE8BA00"))); 91 } 92 93 static byte[] xeh(String in) { 94 in = in.replaceAll(" ", ""); 95 int len = in.length() / 2; 96 byte[] out = new byte[len]; 97 for (int i = 0; i < len; i++) { 98 out[i] = (byte)Integer.parseInt(in.substring(i * 2, i * 2 + 2), 16); 99 } 100 return out; 101 } 102 103 }