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