Path: blob/master/test/jdk/sun/security/ec/xec/TestXECOps.java
41155 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 817127726* @summary Test XEC curve operations27* @modules jdk.crypto.ec/sun.security.ec28* @library /test/lib29* @build jdk.test.lib.Convert30* @run main TestXECOps31*/3233import sun.security.ec.*;3435import java.security.spec.NamedParameterSpec;36import java.util.*;37import jdk.test.lib.Convert;3839// Test vectors are from RFC 77484041public class TestXECOps {4243public static void main(String[] args) {44TestXECOps m = new TestXECOps();4546m.runTest("X25519",47"a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4",48"e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c",49"c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552");5051m.runTest("X25519",52"4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d",53"e5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493",54"95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957");5556m.runDiffieHellmanTest("X25519",57"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a",58"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb",59"4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742");606162m.runTest("X448",63"3d262fddf9ec8e88495266fea19a34d28882acef045104d0d1aae121700a77" +64"9c984c24f8cdd78fbff44943eba368f54b29259a4f1c600ad3",65"06fce640fa3487bfda5f6cf2d5263f8aad88334cbd07437f020f08f9814dc0" +66"31ddbdc38c19c6da2583fa5429db94ada18aa7a7fb4ef8a086",67"ce3e4ff95a60dc6697da1db1d85e6afbdf79b50a2412d7546d5f239fe14fba" +68"adeb445fc66a01b0779d98223961111e21766282f73dd96b6f");6970m.runTest("X448",71"203d494428b8399352665ddca42f9de8fef600908e0d461cb021f8c538345d" +72"d77c3e4806e25f46d3315c44e0a5b4371282dd2c8d5be3095f",73"0fbcc2f993cd56d3305b0b7d9e55d4c1a8fb5dbb52f8e9a1e9b6201b165d01" +74"5894e56c4d3570bee52fe205e28a78b91cdfbde71ce8d157db",75"884a02576239ff7a2f2f63b2db6a9ff37047ac13568e1e30fe63c4a7ad1b3e" +76"e3a5700df34321d62077e63633c575c1c954514e99da7c179d");7778m.runDiffieHellmanTest("X448",79"9a8f4925d1519f5775cf46b04b5800d4ee9ee8bae8bc5565d498c28dd9c9ba" +80"f574a9419744897391006382a6f127ab1d9ac2d8c0a598726b",81"1c306a7ac2a0e2e0990b294470cba339e6453772b075811d8fad0d1d6927c1" +82"20bb5ee8972b0d3e21374c9c921b09d1b0366f10b65173992d",83"07fff4181ac6cc95ec1c16a94a0f74d12da232ce40a77552281d282bb60c0b" +84"56fd2464c335543936521c24403085d59a449a5037514a879d");85}8687private void runDiffieHellmanTest(String opName, String a_str,88String b_str, String result_str) {8990NamedParameterSpec paramSpec = new NamedParameterSpec(opName);91XECParameters settings =92XECParameters.get(RuntimeException::new, paramSpec);93XECOperations ops = new XECOperations(settings);9495byte[] basePoint = Convert.byteToByteArray(settings.getBasePoint(),96settings.getBytes());97byte[] a = HexFormat.of().parseHex(a_str);98byte[] b = HexFormat.of().parseHex(b_str);99byte[] expectedResult = HexFormat.of().parseHex(result_str);100101byte[] a_copy = Arrays.copyOf(a, a.length);102byte[] b_copy = Arrays.copyOf(b, b.length);103byte[] basePoint_copy = Arrays.copyOf(basePoint, basePoint.length);104105byte[] resultA = ops.encodedPointMultiply(b,106ops.encodedPointMultiply(a, basePoint));107byte[] resultB = ops.encodedPointMultiply(a_copy,108ops.encodedPointMultiply(b_copy, basePoint_copy));109if (!Arrays.equals(resultA, expectedResult)) {110throw new RuntimeException("fail");111}112if (!Arrays.equals(resultB, expectedResult)) {113throw new RuntimeException("fail");114}115}116117private void runTest(String opName, String k_in_str,118String u_in_str, String u_out_str) {119120byte[] k_in = HexFormat.of().parseHex(k_in_str);121byte[] u_in = HexFormat.of().parseHex(u_in_str);122byte[] u_out_expected = HexFormat.of().parseHex(u_out_str);123124NamedParameterSpec paramSpec = new NamedParameterSpec(opName);125XECParameters settings =126XECParameters.get(RuntimeException::new, paramSpec);127XECOperations ops = new XECOperations(settings);128byte[] u_out = ops.encodedPointMultiply(k_in, u_in);129130if (!Arrays.equals(u_out, u_out_expected)) {131throw new RuntimeException("fail");132}133}134}135136137138