aboutsummaryrefslogtreecommitdiff
path: root/keystore-cts/java/com/google/security/wycheproof/JsonUtil.java
blob: 6ff9e2f5df6023d83f889e9a54269e0ceec2d60a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.security.wycheproof;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.math.BigInteger;

/** Utilities for reading test vectors in JSON format */
public class JsonUtil {

  /**
   * Reads a set of test vectors from a file.
   * @param filename the name of the file, local to the directory with the
   *        the test vectors.
   * @return a JsonObject with a test
   * @throws IOException if the test vectors could not be read.
   * @throws JsonParseException if the file is not valid JSON.
   */
  public static JsonObject getTestVectors(Class ref, String filename) throws 
      IOException {
    // The directory where the test vectors are.
    String testVectorsDir = "/";
    try (InputStream is = ref.getResourceAsStream(testVectorsDir + filename)) {
        JsonReader reader = new JsonReader(new InputStreamReader(is, UTF_8));
        JsonParser parser = new JsonParser();
        JsonElement elem = parser.parse(reader);
        return elem.getAsJsonObject();
    }
  }

  /** 
   * Converts a JsonElement into a byte array.
   * @param element a JsonElement containing an encoded byte array. 
   *        Wycheproof represents byte arrays as hexadeciamal strings.
   * @throws ClassCastException if element is not a valid string value.
   * @throws IllegalStateException - if element contains an array.
   */
  public static byte[] asByteArray(JsonElement element) {
    String hex = element.getAsString();
    return TestUtil.hexToBytes(hex);
  }

  /**
   * Converts a JsonElement into a BigInteger.
   * @param element a JsonElement containing a BigInteger. 
   * Wycheproof represents BigIntegers as hexadecimal strings using
   * twos complement representation.
   * <p> E.g., 31 is represented as "1f", -1 is represented as "f", and
   * 255 is represented as "0ff".
   * @throws ClassCastException if element is not a valid string value.
   * @throws IllegalStateException if element contains an array.
   * @throws NumberFormatException if representation of the BigInteger is invalid.
   */ 
  public static BigInteger asBigInteger(JsonElement element) {
    String hex = element.getAsString();
    return asBigInteger(hex);
  }
  public static BigInteger asBigInteger(String hex) {
    // TODO(bleichen): Consider to change the representation of BigIntegers in
    //   Wycheproof as hexadecimal string with a sign.
    if (hex.length() % 2 == 1) {
      if (hex.charAt(0) >= '0' && hex.charAt(0) <= '7') {
        hex = "0" + hex;
      } else {
        hex = "f" + hex;
      }
    }
    return new BigInteger(TestUtil.hexToBytes(hex));
  }
}