1 /** 2 * Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 package com.google.security.wycheproof; 15 16 import static java.nio.charset.StandardCharsets.UTF_8; 17 18 import com.google.gson.JsonElement; 19 import com.google.gson.JsonObject; 20 import com.google.gson.JsonParseException; 21 import com.google.gson.JsonParser; 22 import com.google.gson.stream.JsonReader; 23 import java.io.FileInputStream; 24 import java.io.InputStream; 25 import java.io.InputStreamReader; 26 import java.io.IOException; 27 import java.math.BigInteger; 28 29 /** Utilities for reading test vectors in JSON format */ 30 public class JsonUtil { 31 32 /** 33 * Reads a set of test vectors from a file. 34 * @param filename the name of the file, local to the directory with the 35 * the test vectors. 36 * @return a JsonObject with a test 37 * @throws IOException if the test vectors could not be read. 38 * @throws JsonParseException if the file is not valid JSON. 39 */ getTestVectors(Class ref, String filename)40 public static JsonObject getTestVectors(Class ref, String filename) throws 41 IOException { 42 // The directory where the test vectors are. 43 String testVectorsDir = "/"; 44 try (InputStream is = ref.getResourceAsStream(testVectorsDir + filename)) { 45 JsonReader reader = new JsonReader(new InputStreamReader(is, UTF_8)); 46 JsonParser parser = new JsonParser(); 47 JsonElement elem = parser.parse(reader); 48 return elem.getAsJsonObject(); 49 } 50 } 51 52 /** 53 * Converts a JsonElement into a byte array. 54 * @param element a JsonElement containing an encoded byte array. 55 * Wycheproof represents byte arrays as hexadeciamal strings. 56 * @throws ClassCastException if element is not a valid string value. 57 * @throws IllegalStateException - if element contains an array. 58 */ asByteArray(JsonElement element)59 public static byte[] asByteArray(JsonElement element) { 60 String hex = element.getAsString(); 61 return TestUtil.hexToBytes(hex); 62 } 63 64 /** 65 * Converts a JsonElement into a BigInteger. 66 * @param element a JsonElement containing a BigInteger. 67 * Wycheproof represents BigIntegers as hexadecimal strings using 68 * twos complement representation. 69 * <p> E.g., 31 is represented as "1f", -1 is represented as "f", and 70 * 255 is represented as "0ff". 71 * @throws ClassCastException if element is not a valid string value. 72 * @throws IllegalStateException if element contains an array. 73 * @throws NumberFormatException if representation of the BigInteger is invalid. 74 */ asBigInteger(JsonElement element)75 public static BigInteger asBigInteger(JsonElement element) { 76 String hex = element.getAsString(); 77 return asBigInteger(hex); 78 } asBigInteger(String hex)79 public static BigInteger asBigInteger(String hex) { 80 // TODO(bleichen): Consider to change the representation of BigIntegers in 81 // Wycheproof as hexadecimal string with a sign. 82 if (hex.length() % 2 == 1) { 83 if (hex.charAt(0) >= '0' && hex.charAt(0) <= '7') { 84 hex = "0" + hex; 85 } else { 86 hex = "f" + hex; 87 } 88 } 89 return new BigInteger(TestUtil.hexToBytes(hex)); 90 } 91 } 92