Steven Valdez | b8a3550 | 2017-04-28 16:17:54 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2017, 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_sha_test processes a NIST CAVP SHA test vector request file and emits |
Martin Kreichgauer | bf21849 | 2017-05-03 17:10:14 -0700 | [diff] [blame^] | 16 | // the corresponding response. |
Steven Valdez | b8a3550 | 2017-04-28 16:17:54 -0400 | [diff] [blame] | 17 | |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <openssl/crypto.h> |
| 21 | #include <openssl/digest.h> |
| 22 | |
Adam Langley | 58e4499 | 2017-04-28 16:45:49 -0700 | [diff] [blame] | 23 | #include "../crypto/test/file_test.h" |
Steven Valdez | b8a3550 | 2017-04-28 16:17:54 -0400 | [diff] [blame] | 24 | #include "cavp_test_util.h" |
| 25 | |
Martin Kreichgauer | ddfcc6a | 2017-05-03 14:12:39 -0700 | [diff] [blame] | 26 | namespace { |
Steven Valdez | b8a3550 | 2017-04-28 16:17:54 -0400 | [diff] [blame] | 27 | |
| 28 | struct TestCtx { |
| 29 | std::string hash; |
| 30 | }; |
| 31 | |
Martin Kreichgauer | ddfcc6a | 2017-05-03 14:12:39 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Steven Valdez | b8a3550 | 2017-04-28 16:17:54 -0400 | [diff] [blame] | 34 | static bool TestSHA(FileTest *t, void *arg) { |
| 35 | TestCtx *ctx = reinterpret_cast<TestCtx *>(arg); |
| 36 | |
| 37 | const EVP_MD *md = EVP_get_digestbyname(ctx->hash.c_str()); |
| 38 | if (md == nullptr) { |
| 39 | return false; |
| 40 | } |
| 41 | const size_t md_len = EVP_MD_size(md); |
| 42 | |
| 43 | std::string out_len; |
| 44 | if (!t->GetInstruction(&out_len, "L") || |
| 45 | md_len != strtoul(out_len.c_str(), nullptr, 0)) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | std::string msg_len_str; |
| 50 | std::vector<uint8_t> msg; |
| 51 | if (!t->GetAttribute(&msg_len_str, "Len") || |
| 52 | !t->GetBytes(&msg, "Msg")) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | size_t msg_len = strtoul(msg_len_str.c_str(), nullptr, 0); |
| 57 | if (msg_len % 8 != 0 || |
| 58 | msg_len / 8 > msg.size()) { |
| 59 | return false; |
| 60 | } |
| 61 | msg_len /= 8; |
| 62 | |
| 63 | std::vector<uint8_t> out; |
| 64 | out.resize(md_len); |
| 65 | unsigned digest_len; |
| 66 | if (!EVP_Digest(msg.data(), msg_len, out.data(), &digest_len, md, nullptr) || |
| 67 | digest_len != out.size()) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | printf("%s", t->CurrentTestToString().c_str()); |
| 72 | printf("MD = %s\r\n\r\n", EncodeHex(out.data(), out.size()).c_str()); |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | static int usage(char *arg) { |
| 78 | fprintf(stderr, "usage: %s <hash> <test file>\n", arg); |
| 79 | return 1; |
| 80 | } |
| 81 | |
Martin Kreichgauer | ddfcc6a | 2017-05-03 14:12:39 -0700 | [diff] [blame] | 82 | int cavp_sha_test_main(int argc, char **argv) { |
Steven Valdez | b8a3550 | 2017-04-28 16:17:54 -0400 | [diff] [blame] | 83 | if (argc != 3) { |
| 84 | return usage(argv[0]); |
| 85 | } |
| 86 | |
| 87 | TestCtx ctx = {std::string(argv[1])}; |
| 88 | |
| 89 | printf("# Generated by"); |
| 90 | for (int i = 0; i < argc; i++) { |
| 91 | printf(" %s", argv[i]); |
| 92 | } |
| 93 | printf("\r\n\r\n"); |
| 94 | |
| 95 | return FileTestMainSilent(TestSHA, &ctx, argv[2]); |
| 96 | } |