Path: blob/master/test/jdk/sun/security/ec/xec/XECIterative.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 XEC curve operations iterative test vectors27* @library /test/lib28* @build jdk.test.lib.Convert29* @modules jdk.crypto.ec/sun.security.ec30* @run main XECIterative 0 1000031* @run main XECIterative 10000 2000032* @run main XECIterative 20000 3000033* @run main XECIterative 30000 4000034* @run main XECIterative 40000 5000035* @run main XECIterative 50000 6000036* @run main XECIterative 60000 7000037* @run main XECIterative 70000 8000038* @run main XECIterative 80000 9000039* @run main XECIterative 90000 10000040*/4142import sun.security.ec.*;4344import java.io.*;45import java.security.spec.NamedParameterSpec;46import java.util.*;4748/*49* This test is derived from the iterative test in RFC 7748. To produce the50* test vectors, the implementation ran for 1,000,000 iterations and saved51* values of k and u at periodic checkpoints. The RFC includes the correct52* value of k after 1,000,000 iterations, and this value was used to ensure53* that the test vectors are correct. This test has multiple @run tags so that54* no single run takes too long.55*/5657public class XECIterative {5859private static class KU {6061public byte[] k;62public byte[] u;6364}6566public static void main(String[] args) throws IOException {6768long start = Long.parseLong(args[0]);69long end = Long.parseLong(args[1]);7071XECIterative m = new XECIterative();7273m.runIterativeTest("X25519", start, end);74m.runIterativeTest("X448", start, end);7576}7778private void runIterativeTest(String opName, long start, long end)79throws IOException {8081NamedParameterSpec paramSpec = new NamedParameterSpec(opName);82XECParameters settings =83XECParameters.get(RuntimeException::new, paramSpec);84XECOperations ops = new XECOperations(settings);8586File vectorFile = new File(System.getProperty("test.src", "."),87opName + ".iter");8889Map<Long, KU> testIters = new HashMap<Long, KU>();90BufferedReader in = new BufferedReader(new FileReader(vectorFile));91String line;92while ((line = in.readLine()) != null) {93StringTokenizer tok = new StringTokenizer(line, ",");94long iter = Long.parseLong(tok.nextToken());95String kOrU = tok.nextToken();96byte[] value = HexFormat.of().parseHex(tok.nextToken());97KU entry = testIters.get(iter);98if (entry == null) {99entry = new KU();100testIters.put(iter, entry);101}102if (kOrU.equals("k")) {103entry.k = value;104} else {105entry.u = value;106}107}108109KU startEntry = testIters.get(start);110byte[] k = startEntry.k.clone();111byte[] u = startEntry.u.clone();112113for (long i = start; i <= end; i++) {114KU curEntry;115if (i % 1000 == 0 && (curEntry = testIters.get(i)) != null) {116if (!Arrays.equals(k, curEntry.k)) {117throw new RuntimeException("At iter " + i + ": expected k: "118+ HexFormat.of().withUpperCase().formatHex(curEntry.k)119+ ", computed k: " + HexFormat.of().withUpperCase().formatHex(k));120}121if (!Arrays.equals(u, curEntry.u)) {122throw new RuntimeException("At iter " + i + ": expected u: "123+ HexFormat.of().withUpperCase().formatHex(curEntry.u)124+ ", computed u: " + HexFormat.of().withUpperCase().formatHex(u));125}126System.out.println(opName + " checkpoint passed at " + i);127}128129byte[] k_copy = Arrays.copyOf(k, k.length);130byte[] u_out = ops.encodedPointMultiply(k, u);131u = k_copy;132k = u_out;133}134}135136}137138139140141