Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [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_aes_gcm_test processes a NIST CAVP AES GCM test vector request file and |
Martin Kreichgauer | bf21849 | 2017-05-03 17:10:14 -0700 | [diff] [blame] | 16 | // emits the corresponding response. |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 17 | |
| 18 | #include <stdlib.h> |
| 19 | |
| 20 | #include <openssl/aead.h> |
| 21 | #include <openssl/cipher.h> |
| 22 | #include <openssl/crypto.h> |
| 23 | #include <openssl/err.h> |
| 24 | |
Adam Langley | 58e4499 | 2017-04-28 16:45:49 -0700 | [diff] [blame] | 25 | #include "../crypto/test/file_test.h" |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 26 | #include "cavp_test_util.h" |
| 27 | |
| 28 | |
Martin Kreichgauer | ddfcc6a | 2017-05-03 14:12:39 -0700 | [diff] [blame] | 29 | namespace { |
| 30 | |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 31 | struct TestCtx { |
| 32 | const EVP_AEAD *aead; |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Martin Kreichgauer | ddfcc6a | 2017-05-03 14:12:39 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 37 | static const EVP_AEAD *GetAEAD(const std::string &name, const bool enc) { |
| 38 | if (name == "aes-128-gcm") { |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 39 | return EVP_aead_aes_128_gcm(); |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 40 | } else if (name == "aes-256-gcm") { |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 41 | return EVP_aead_aes_256_gcm(); |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 42 | } |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
| 46 | static bool TestAEADEncrypt(FileTest *t, void *arg) { |
| 47 | TestCtx *ctx = reinterpret_cast<TestCtx *>(arg); |
| 48 | |
| 49 | std::string key_len_str, iv_len_str, pt_len_str, aad_len_str, tag_len_str; |
| 50 | if (!t->GetInstruction(&key_len_str, "Keylen") || |
| 51 | !t->GetInstruction(&iv_len_str, "IVlen") || |
| 52 | !t->GetInstruction(&pt_len_str, "PTlen") || |
| 53 | !t->GetInstruction(&aad_len_str, "AADlen") || |
| 54 | !t->GetInstruction(&tag_len_str, "Taglen")) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | std::string count; |
| 59 | std::vector<uint8_t> key, iv, pt, aad, tag, ct; |
| 60 | if (!t->GetAttribute(&count, "Count") || |
| 61 | !t->GetBytes(&key, "Key") || |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 62 | !t->GetBytes(&iv, "IV") || |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 63 | !t->GetBytes(&pt, "PT") || |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 64 | !t->GetBytes(&aad, "AAD") || |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 65 | key.size() * 8 != strtoul(key_len_str.c_str(), nullptr, 0) || |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 66 | iv.size() * 8 != strtoul(iv_len_str.c_str(), nullptr, 0) || |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 67 | pt.size() * 8 != strtoul(pt_len_str.c_str(), nullptr, 0) || |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 68 | aad.size() * 8 != strtoul(aad_len_str.c_str(), nullptr, 0) || |
| 69 | iv.size() != 12) { |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 70 | return false; |
| 71 | } |
| 72 | |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 73 | const size_t tag_len = strtoul(tag_len_str.c_str(), nullptr, 0) / 8; |
| 74 | if (!AEADEncrypt(ctx->aead, &ct, &tag, tag_len, key, pt, aad, iv)) { |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | printf("%s", t->CurrentTestToString().c_str()); |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 78 | printf("CT = %s\r\n", EncodeHex(ct.data(), ct.size()).c_str()); |
| 79 | printf("Tag = %s\r\n\r\n", EncodeHex(tag.data(), tag.size()).c_str()); |
| 80 | |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | |
| 84 | static bool TestAEADDecrypt(FileTest *t, void *arg) { |
| 85 | TestCtx *ctx = reinterpret_cast<TestCtx *>(arg); |
| 86 | |
| 87 | std::string key_len, iv_len, pt_len_str, aad_len_str, tag_len; |
| 88 | if (!t->GetInstruction(&key_len, "Keylen") || |
| 89 | !t->GetInstruction(&iv_len, "IVlen") || |
| 90 | !t->GetInstruction(&pt_len_str, "PTlen") || |
| 91 | !t->GetInstruction(&aad_len_str, "AADlen") || |
| 92 | !t->GetInstruction(&tag_len, "Taglen")) { |
| 93 | t->PrintLine("Invalid instruction block."); |
| 94 | return false; |
| 95 | } |
| 96 | size_t aad_len = strtoul(aad_len_str.c_str(), nullptr, 0) / 8; |
| 97 | size_t pt_len = strtoul(pt_len_str.c_str(), nullptr, 0) / 8; |
| 98 | |
| 99 | std::string count; |
| 100 | std::vector<uint8_t> key, iv, ct, aad, tag, pt; |
| 101 | if (!t->GetAttribute(&count, "Count") || |
| 102 | !t->GetBytes(&key, "Key") || |
| 103 | !t->GetBytes(&aad, "AAD") || |
| 104 | !t->GetBytes(&tag, "Tag") || |
| 105 | !t->GetBytes(&iv, "IV") || |
| 106 | !t->GetBytes(&ct, "CT") || |
| 107 | key.size() * 8 != strtoul(key_len.c_str(), nullptr, 0) || |
| 108 | iv.size() * 8 != strtoul(iv_len.c_str(), nullptr, 0) || |
| 109 | ct.size() != pt_len || |
| 110 | aad.size() != aad_len || |
| 111 | tag.size() * 8 != strtoul(tag_len.c_str(), nullptr, 0)) { |
| 112 | t->PrintLine("Invalid test case"); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | printf("%s", t->CurrentTestToString().c_str()); |
| 117 | bool aead_result = |
Adam Langley | 563924b | 2017-05-30 11:43:25 -0700 | [diff] [blame] | 118 | AEADDecrypt(ctx->aead, &pt, pt_len, key, aad, ct, tag, iv); |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 119 | if (aead_result) { |
| 120 | printf("PT = %s\r\n\r\n", EncodeHex(pt.data(), pt.size()).c_str()); |
| 121 | } else { |
| 122 | printf("FAIL\r\n\r\n"); |
| 123 | } |
| 124 | |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
| 128 | static int usage(char *arg) { |
Martin Kreichgauer | bf21849 | 2017-05-03 17:10:14 -0700 | [diff] [blame] | 129 | fprintf(stderr, "usage: %s (enc|dec) <cipher> <test file>\n", arg); |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 130 | return 1; |
| 131 | } |
| 132 | |
Martin Kreichgauer | ddfcc6a | 2017-05-03 14:12:39 -0700 | [diff] [blame] | 133 | int cavp_aes_gcm_test_main(int argc, char **argv) { |
Martin Kreichgauer | bf21849 | 2017-05-03 17:10:14 -0700 | [diff] [blame] | 134 | if (argc != 4) { |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 135 | return usage(argv[0]); |
| 136 | } |
| 137 | |
| 138 | const std::string mode(argv[1]); |
| 139 | bool (*test_fn)(FileTest * t, void *arg); |
| 140 | if (mode == "enc") { |
| 141 | test_fn = &TestAEADEncrypt; |
| 142 | } else if (mode == "dec") { |
| 143 | test_fn = &TestAEADDecrypt; |
| 144 | } else { |
| 145 | return usage(argv[0]); |
| 146 | } |
| 147 | |
| 148 | const EVP_AEAD *aead = GetAEAD(argv[2], mode == "enc"); |
| 149 | if (aead == nullptr) { |
| 150 | fprintf(stderr, "invalid aead: %s\n", argv[2]); |
| 151 | return 1; |
| 152 | } |
| 153 | |
Martin Kreichgauer | bf21849 | 2017-05-03 17:10:14 -0700 | [diff] [blame] | 154 | TestCtx ctx = {aead}; |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 155 | |
Adam Langley | d79bc9d | 2017-05-30 15:37:27 -0700 | [diff] [blame] | 156 | FileTest::Options opts; |
| 157 | opts.path = argv[3]; |
| 158 | opts.callback = test_fn; |
| 159 | opts.arg = &ctx; |
| 160 | opts.silent = true; |
| 161 | opts.comment_callback = EchoComment; |
| 162 | return FileTestMain(opts); |
Martin Kreichgauer | 7c12587 | 2017-04-24 13:29:11 -0700 | [diff] [blame] | 163 | } |