xref: /aosp_15_r20/external/tink/cc/subtle/wycheproof_util.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang // Copyright 2018 Google Inc.
2*e7b1675dSTing-Kang Chang //
3*e7b1675dSTing-Kang Chang // Licensed under the Apache License, Version 2.0 (the "License");
4*e7b1675dSTing-Kang Chang // you may not use this file except in compliance with the License.
5*e7b1675dSTing-Kang Chang // You may obtain a copy of the License at
6*e7b1675dSTing-Kang Chang //
7*e7b1675dSTing-Kang Chang //      http://www.apache.org/licenses/LICENSE-2.0
8*e7b1675dSTing-Kang Chang //
9*e7b1675dSTing-Kang Chang // Unless required by applicable law or agreed to in writing, software
10*e7b1675dSTing-Kang Chang // distributed under the License is distributed on an "AS IS" BASIS,
11*e7b1675dSTing-Kang Chang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e7b1675dSTing-Kang Chang // See the License for the specific language governing permissions and
13*e7b1675dSTing-Kang Chang // limitations under the License.
14*e7b1675dSTing-Kang Chang //
15*e7b1675dSTing-Kang Chang ////////////////////////////////////////////////////////////////////////////////
16*e7b1675dSTing-Kang Chang 
17*e7b1675dSTing-Kang Chang #ifndef TINK_SUBTLE_WYCHEPROOF_UTIL_H_
18*e7b1675dSTing-Kang Chang #define TINK_SUBTLE_WYCHEPROOF_UTIL_H_
19*e7b1675dSTing-Kang Chang 
20*e7b1675dSTing-Kang Chang #include <memory>
21*e7b1675dSTing-Kang Chang #include <string>
22*e7b1675dSTing-Kang Chang 
23*e7b1675dSTing-Kang Chang #include "include/rapidjson/document.h"
24*e7b1675dSTing-Kang Chang #include "tink/subtle/common_enums.h"
25*e7b1675dSTing-Kang Chang 
26*e7b1675dSTing-Kang Chang namespace crypto {
27*e7b1675dSTing-Kang Chang namespace tink {
28*e7b1675dSTing-Kang Chang namespace subtle {
29*e7b1675dSTing-Kang Chang 
30*e7b1675dSTing-Kang Chang // WycheproofUtil is a util that is used to read test vectors from project
31*e7b1675dSTing-Kang Chang // Wycheproof and convert the values in the test vectors into corresponding
32*e7b1675dSTing-Kang Chang // values for tink.
33*e7b1675dSTing-Kang Chang class WycheproofUtil {
34*e7b1675dSTing-Kang Chang  public:
35*e7b1675dSTing-Kang Chang   // Converts a JSON value into a byte array.
36*e7b1675dSTing-Kang Chang   // Byte arrays are always hexadecimal representation.
37*e7b1675dSTing-Kang Chang   static std::string GetBytes(const rapidjson::Value &val);
38*e7b1675dSTing-Kang Chang 
39*e7b1675dSTing-Kang Chang   // Reads test vector from a file.
40*e7b1675dSTing-Kang Chang   // The filename is relative to the directory with the test vectors.
41*e7b1675dSTing-Kang Chang   static std::unique_ptr<rapidjson::Document> ReadTestVectors(
42*e7b1675dSTing-Kang Chang       const std::string &filename);
43*e7b1675dSTing-Kang Chang 
44*e7b1675dSTing-Kang Chang   static HashType GetHashType(const rapidjson::Value &val);
45*e7b1675dSTing-Kang Chang 
46*e7b1675dSTing-Kang Chang   static EllipticCurveType GetEllipticCurveType(const rapidjson::Value &val);
47*e7b1675dSTing-Kang Chang 
48*e7b1675dSTing-Kang Chang   // Integers in Wycheproof are represented as signed bigendian hexadecimal
49*e7b1675dSTing-Kang Chang   // strings in twos complement representation.
50*e7b1675dSTing-Kang Chang   // Integers in EcKey are unsigned and are represented as an array of bytes
51*e7b1675dSTing-Kang Chang   // using bigendian order.
52*e7b1675dSTing-Kang Chang   // GetInteger can assume that val is always 0 or a positive integer, since
53*e7b1675dSTing-Kang Chang   // they are values from the key: a convention in Wycheproof is that parameters
54*e7b1675dSTing-Kang Chang   // in the test group are valid, only values in the test vector itself may
55*e7b1675dSTing-Kang Chang   // be invalid.
56*e7b1675dSTing-Kang Chang   static std::string GetInteger(const rapidjson::Value &val);
57*e7b1675dSTing-Kang Chang };
58*e7b1675dSTing-Kang Chang 
59*e7b1675dSTing-Kang Chang }  // namespace subtle
60*e7b1675dSTing-Kang Chang }  // namespace tink
61*e7b1675dSTing-Kang Chang }  // namespace crypto
62*e7b1675dSTing-Kang Chang 
63*e7b1675dSTing-Kang Chang #endif  // TINK_SUBTLE_WYCHEPROOF_UTIL_H_
64