cavp_kas_test.cc
Go to the documentation of this file.
1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 // cavp_kas_test processes NIST CAVP ECC KAS test vector request files and
16 // emits the corresponding response.
17 
18 #include <vector>
19 
20 #include <openssl/bn.h>
21 #include <openssl/crypto.h>
22 #include <openssl/digest.h>
23 #include <openssl/ecdh.h>
24 #include <openssl/ecdsa.h>
25 #include <openssl/ec_key.h>
26 #include <openssl/err.h>
27 #include <openssl/nid.h>
28 #include <openssl/sha.h>
29 #include <openssl/span.h>
30 
31 #include "../crypto/internal.h"
32 #include "../crypto/test/file_test.h"
33 #include "../crypto/test/test_util.h"
34 #include "cavp_test_util.h"
35 
36 
37 static bool TestKAS(FileTest *t, void *arg) {
38  const bool validate = *reinterpret_cast<bool *>(arg);
39 
40  int nid = NID_undef;
41  size_t digest_len = 0;
42 
43  if (t->HasInstruction("EB - SHA224")) {
45  digest_len = SHA224_DIGEST_LENGTH;
46  } else if (t->HasInstruction("EC - SHA256")) {
48  digest_len = SHA256_DIGEST_LENGTH;
49  } else if (t->HasInstruction("ED - SHA384")) {
51  digest_len = SHA384_DIGEST_LENGTH;
52  } else if (t->HasInstruction("EE - SHA512")) {
54  digest_len = SHA512_DIGEST_LENGTH;
55  } else {
56  return false;
57  }
58 
59  if (!t->HasAttribute("COUNT")) {
60  return false;
61  }
62 
63  bssl::UniquePtr<BIGNUM> their_x(GetBIGNUM(t, "QeCAVSx"));
64  bssl::UniquePtr<BIGNUM> their_y(GetBIGNUM(t, "QeCAVSy"));
65  bssl::UniquePtr<EC_KEY> ec_key(EC_KEY_new_by_curve_name(nid));
66  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
67  if (!their_x || !their_y || !ec_key || !ctx) {
68  return false;
69  }
70 
71  const EC_GROUP *const group = EC_KEY_get0_group(ec_key.get());
72  bssl::UniquePtr<EC_POINT> their_point(EC_POINT_new(group));
73  if (!their_point ||
75  group, their_point.get(), their_x.get(), their_y.get(), ctx.get())) {
76  return false;
77  }
78 
79  if (validate) {
80  bssl::UniquePtr<BIGNUM> our_k(GetBIGNUM(t, "deIUT"));
81  if (!our_k ||
82  !EC_KEY_set_private_key(ec_key.get(), our_k.get()) ||
83  // These attributes are ignored.
84  !t->HasAttribute("QeIUTx") ||
85  !t->HasAttribute("QeIUTy")) {
86  return false;
87  }
88  } else if (!EC_KEY_generate_key(ec_key.get())) {
89  return false;
90  }
91 
92  uint8_t digest[EVP_MAX_MD_SIZE];
93  if (!ECDH_compute_key_fips(digest, digest_len, their_point.get(),
94  ec_key.get())) {
95  return false;
96  }
97 
98  if (validate) {
99  std::vector<uint8_t> expected_shared_bytes;
100  if (!t->GetBytes(&expected_shared_bytes, "CAVSHashZZ")) {
101  return false;
102  }
103  const bool ok =
104  digest_len == expected_shared_bytes.size() &&
105  OPENSSL_memcmp(digest, expected_shared_bytes.data(), digest_len) == 0;
106 
107  printf("%sIUTHashZZ = %s\r\nResult = %c\r\n\r\n\r\n",
108  t->CurrentTestToString().c_str(),
109  EncodeHex(bssl::MakeConstSpan(digest, digest_len)).c_str(),
110  ok ? 'P' : 'F');
111  } else {
112  const EC_POINT *pub = EC_KEY_get0_public_key(ec_key.get());
113  bssl::UniquePtr<BIGNUM> x(BN_new());
114  bssl::UniquePtr<BIGNUM> y(BN_new());
115  if (!x || !y ||
116  !EC_POINT_get_affine_coordinates_GFp(group, pub, x.get(), y.get(),
117  ctx.get())) {
118  return false;
119  }
120  bssl::UniquePtr<char> x_hex(BN_bn2hex(x.get()));
121  bssl::UniquePtr<char> y_hex(BN_bn2hex(y.get()));
122 
123  printf("%sQeIUTx = %s\r\nQeIUTy = %s\r\nHashZZ = %s\r\n",
124  t->CurrentTestToString().c_str(), x_hex.get(), y_hex.get(),
125  EncodeHex(bssl::MakeConstSpan(digest, digest_len)).c_str());
126  }
127 
128  return true;
129 }
130 
131 int cavp_kas_test_main(int argc, char **argv) {
132  if (argc != 3) {
133  fprintf(stderr, "usage: %s (validity|function) <test file>\n",
134  argv[0]);
135  return 1;
136  }
137 
138  bool validity;
139  if (strcmp(argv[1], "validity") == 0) {
140  validity = true;
141  } else if (strcmp(argv[1], "function") == 0) {
142  validity = false;
143  } else {
144  fprintf(stderr, "Unknown test type: %s\n", argv[1]);
145  return 1;
146  }
147 
149  opts.path = argv[2];
150  opts.arg = &validity;
151  opts.callback = TestKAS;
152  opts.silent = true;
153  opts.comment_callback = EchoComment;
154  opts.is_kas_test = true;
155  return FileTestMain(opts);
156 }
bn.h
EC_POINT_new
#define EC_POINT_new
Definition: boringssl_prefix_symbols.h:1384
EC_KEY_new_by_curve_name
#define EC_KEY_new_by_curve_name
Definition: boringssl_prefix_symbols.h:1356
BN_bn2hex
#define BN_bn2hex
Definition: boringssl_prefix_symbols.h:906
OPENSSL_memcmp
static int OPENSSL_memcmp(const void *s1, const void *s2, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:811
ctx
Definition: benchmark-async.c:30
NID_X9_62_prime256v1
#define NID_X9_62_prime256v1
Definition: nid.h:1914
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
EC_KEY_generate_key
#define EC_KEY_generate_key
Definition: boringssl_prefix_symbols.h:1342
ecdsa.h
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
ECDH_compute_key_fips
#define ECDH_compute_key_fips
Definition: boringssl_prefix_symbols.h:1297
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
ecdh.h
cstest_report.opts
opts
Definition: cstest_report.py:81
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
EC_KEY_get0_group
#define EC_KEY_get0_group
Definition: boringssl_prefix_symbols.h:1344
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
SHA384_DIGEST_LENGTH
#define SHA384_DIGEST_LENGTH
Definition: sha.h:203
EC_POINT_get_affine_coordinates_GFp
#define EC_POINT_get_affine_coordinates_GFp
Definition: boringssl_prefix_symbols.h:1379
cavp_kas_test_main
int cavp_kas_test_main(int argc, char **argv)
Definition: cavp_kas_test.cc:131
EC_POINT_set_affine_coordinates_GFp
#define EC_POINT_set_affine_coordinates_GFp
Definition: boringssl_prefix_symbols.h:1389
NID_secp521r1
#define NID_secp521r1
Definition: nid.h:3172
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
TestKAS
static bool TestKAS(FileTest *t, void *arg)
Definition: cavp_kas_test.cc:37
cavp_test_util.h
sha.h
EncodeHex
std::string EncodeHex(bssl::Span< const uint8_t > in)
Definition: boringssl-with-bazel/src/crypto/test/test_util.cc:75
FileTest
Definition: file_test.h:90
err.h
crypto.h
arg
Definition: cmdline.cc:40
EC_KEY_get0_public_key
#define EC_KEY_get0_public_key
Definition: boringssl_prefix_symbols.h:1346
ec_key.h
NID_undef
#define NID_undef
Definition: nid.h:85
SHA512_DIGEST_LENGTH
#define SHA512_DIGEST_LENGTH
Definition: sha.h:230
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
BN_CTX_new
#define BN_CTX_new
Definition: boringssl_prefix_symbols.h:885
GetBIGNUM
static bssl::UniquePtr< BIGNUM > GetBIGNUM(FileTest *t, const char *key)
Definition: ecdh_test.cc:62
nid
int nid
Definition: cipher_extra.c:71
nid.h
digest.h
SHA224_DIGEST_LENGTH
#define SHA224_DIGEST_LENGTH
Definition: sha.h:128
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
ec_point_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:615
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH
Definition: sha.h:155
EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE
Definition: digest.h:156
ok
bool ok
Definition: async_end2end_test.cc:197
arg
struct arg arg
EC_KEY_set_private_key
#define EC_KEY_set_private_key
Definition: boringssl_prefix_symbols.h:1366
FileTest::Options
Definition: file_test.h:104
NID_secp224r1
#define NID_secp224r1
Definition: nid.h:3160
span.h
NID_secp384r1
#define NID_secp384r1
Definition: nid.h:3168
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971
absl::MakeConstSpan
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
Definition: abseil-cpp/absl/types/span.h:707


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52