| 1 |
// Copyright (c) 2018, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "./wycheproof_util.h" |
| 5 |
|
| 6 |
#include <limits.h> |
| 7 |
#include <stdlib.h> |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
|
| 11 |
#include <openssl/bn.h> |
| 12 |
#include <openssl/digest.h> |
| 13 |
#include <openssl/ec.h> |
| 14 |
#include <openssl/nid.h> |
| 15 |
|
| 16 |
#include "./file_test.h" |
| 17 |
|
| 18 |
|
| 19 |
bool WycheproofResult::IsValid( |
| 20 |
const std::vector<std::string> &acceptable_flags) const { |
| 21 |
switch (raw_result) { |
| 22 |
case WycheproofRawResult::kValid: |
| 23 |
return true; |
| 24 |
case WycheproofRawResult::kInvalid: |
| 25 |
return false; |
| 26 |
case WycheproofRawResult::kAcceptable: |
| 27 |
for (const auto &flag : flags) { |
| 28 |
if (std::find(acceptable_flags.begin(), acceptable_flags.end(), flag) == |
| 29 |
acceptable_flags.end()) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
} |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
abort(); |
| 37 |
} |
| 38 |
|
| 39 |
bool WycheproofResult::HasFlag(const std::string &flag) const { |
| 40 |
return std::find(flags.begin(), flags.end(), flag) != flags.end(); |
| 41 |
} |
| 42 |
|
| 43 |
std::string WycheproofResult::StringifyFlags() { |
| 44 |
std::string flags_str; |
| 45 |
for (size_t i = 0; i < flags.size(); i++) { |
| 46 |
if (i > 0) { |
| 47 |
flags_str += ", "; |
| 48 |
} |
| 49 |
flags_str += flags[i]; |
| 50 |
} |
| 51 |
return flags_str; |
| 52 |
} |
| 53 |
|
| 54 |
bool GetWycheproofResult(FileTest *t, WycheproofResult *out) { |
| 55 |
std::string result; |
| 56 |
if (!t->GetAttribute(&result, "result")) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
if (result == "valid") { |
| 60 |
out->raw_result = WycheproofRawResult::kValid; |
| 61 |
} else if (result == "invalid") { |
| 62 |
out->raw_result = WycheproofRawResult::kInvalid; |
| 63 |
} else if (result == "acceptable") { |
| 64 |
out->raw_result = WycheproofRawResult::kAcceptable; |
| 65 |
} else { |
| 66 |
t->PrintLine("Bad result string '%s'", result.c_str()); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
out->flags.clear(); |
| 71 |
if (t->HasAttribute("flags")) { |
| 72 |
std::string flags = t->GetAttributeOrDie("flags"); |
| 73 |
size_t idx = 0; |
| 74 |
while (idx < flags.size()) { |
| 75 |
size_t comma = flags.find(',', idx); |
| 76 |
if (comma == std::string::npos) { |
| 77 |
comma = flags.size(); |
| 78 |
} |
| 79 |
out->flags.push_back(flags.substr(idx, comma - idx)); |
| 80 |
idx = comma + 1; |
| 81 |
} |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
const EVP_MD *GetWycheproofDigest(FileTest *t, const char *key, |
| 87 |
bool instruction) { |
| 88 |
std::string name; |
| 89 |
bool ok = |
| 90 |
instruction ? t->GetInstruction(&name, key) : t->GetAttribute(&name, key); |
| 91 |
if (!ok) { |
| 92 |
return nullptr; |
| 93 |
} |
| 94 |
if (name == "SHA-1") { |
| 95 |
return EVP_sha1(); |
| 96 |
} |
| 97 |
if (name == "SHA-224") { |
| 98 |
return EVP_sha224(); |
| 99 |
} |
| 100 |
if (name == "SHA-256") { |
| 101 |
return EVP_sha256(); |
| 102 |
} |
| 103 |
if (name == "SHA-384") { |
| 104 |
return EVP_sha384(); |
| 105 |
} |
| 106 |
if (name == "SHA-512") { |
| 107 |
return EVP_sha512(); |
| 108 |
} |
| 109 |
t->PrintLine("Unknown digest '%s'", name.c_str()); |
| 110 |
return nullptr; |
| 111 |
} |
| 112 |
|
| 113 |
const EC_GROUP *GetWycheproofCurve(FileTest *t, const char *key, |
| 114 |
bool instruction) { |
| 115 |
std::string name; |
| 116 |
bool ok = |
| 117 |
instruction ? t->GetInstruction(&name, key) : t->GetAttribute(&name, key); |
| 118 |
if (!ok) { |
| 119 |
return nullptr; |
| 120 |
} |
| 121 |
if (name == "secp224r1") { |
| 122 |
return EC_group_p224(); |
| 123 |
} |
| 124 |
if (name == "secp256r1") { |
| 125 |
return EC_group_p256(); |
| 126 |
} |
| 127 |
if (name == "secp384r1") { |
| 128 |
return EC_group_p384(); |
| 129 |
} |
| 130 |
if (name == "secp521r1") { |
| 131 |
return EC_group_p521(); |
| 132 |
} |
| 133 |
t->PrintLine("Unknown curve '%s'", name.c_str()); |
| 134 |
return nullptr; |
| 135 |
} |
| 136 |
|
| 137 |
bssl::UniquePtr<BIGNUM> GetWycheproofBIGNUM(FileTest *t, const char *key, |
| 138 |
bool instruction) { |
| 139 |
std::string value; |
| 140 |
bool ok = instruction ? t->GetInstruction(&value, key) |
| 141 |
: t->GetAttribute(&value, key); |
| 142 |
if (!ok) { |
| 143 |
return nullptr; |
| 144 |
} |
| 145 |
BIGNUM *bn = nullptr; |
| 146 |
if (value.size() > INT_MAX || |
| 147 |
BN_hex2bn(&bn, value.c_str()) != static_cast<int>(value.size())) { |
| 148 |
BN_free(bn); |
| 149 |
t->PrintLine("Could not decode value '%s'", value.c_str()); |
| 150 |
return nullptr; |
| 151 |
} |
| 152 |
bssl::UniquePtr<BIGNUM> ret(bn); |
| 153 |
if (!value.empty()) { |
| 154 |
// If the high bit is one, this is a negative number in Wycheproof. |
| 155 |
// Wycheproof's tests generally mimic Java APIs, including all their |
| 156 |
// mistakes. See |
| 157 |
// https://github.com/google/wycheproof/blob/0329f5b751ef102bd6b7b7181b6e049522a887f5/java/com/google/security/wycheproof/JsonUtil.java#L62. |
| 158 |
if ('0' > value[0] || value[0] > '7') { |
| 159 |
bssl::UniquePtr<BIGNUM> tmp(BN_new()); |
| 160 |
if (!tmp || // |
| 161 |
value.size() > INT_MAX / 4 || |
| 162 |
!BN_set_bit(tmp.get(), static_cast<int>(value.size() * 4)) || |
| 163 |
!BN_sub(ret.get(), ret.get(), tmp.get())) { |
| 164 |
return nullptr; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
return ret; |
| 169 |
} |