Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1 | /* Copyright (c) 2015, 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 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #include <vector> |
| 19 | |
| 20 | #include <openssl/crypto.h> |
| 21 | #include <openssl/poly1305.h> |
| 22 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 23 | #include "../internal.h" |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 24 | #include "../test/file_test.h" |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 25 | |
| 26 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 27 | static bool TestSIMD(FileTest *t, unsigned excess, |
| 28 | const std::vector<uint8_t> &key, |
| 29 | const std::vector<uint8_t> &in, |
| 30 | const std::vector<uint8_t> &mac) { |
| 31 | poly1305_state state; |
| 32 | CRYPTO_poly1305_init(&state, key.data()); |
| 33 | |
| 34 | size_t done = 0; |
| 35 | |
| 36 | // Feed 16 bytes in. Some implementations begin in non-SIMD mode and upgrade |
| 37 | // on-demand. Stress the upgrade path. |
| 38 | size_t todo = 16; |
| 39 | if (todo > in.size()) { |
| 40 | todo = in.size(); |
| 41 | } |
| 42 | CRYPTO_poly1305_update(&state, in.data(), todo); |
| 43 | done += todo; |
| 44 | |
| 45 | for (;;) { |
| 46 | // Feed 128 + |excess| bytes to test SIMD mode. |
| 47 | if (done + 128 + excess > in.size()) { |
| 48 | break; |
| 49 | } |
| 50 | CRYPTO_poly1305_update(&state, in.data() + done, 128 + excess); |
| 51 | done += 128 + excess; |
| 52 | |
| 53 | // Feed |excess| bytes to ensure SIMD mode can handle short inputs. |
| 54 | if (done + excess > in.size()) { |
| 55 | break; |
| 56 | } |
| 57 | CRYPTO_poly1305_update(&state, in.data() + done, excess); |
| 58 | done += excess; |
| 59 | } |
| 60 | |
| 61 | // Consume the remainder and finish. |
| 62 | CRYPTO_poly1305_update(&state, in.data() + done, in.size() - done); |
| 63 | |
| 64 | // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output. |
| 65 | alignas(16) uint8_t out[16]; |
| 66 | CRYPTO_poly1305_finish(&state, out); |
| 67 | if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) { |
| 68 | t->PrintLine("SIMD pattern %u failed.", excess); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 74 | |
| 75 | static bool TestPoly1305(FileTest *t, void *arg) { |
| 76 | std::vector<uint8_t> key, in, mac; |
| 77 | if (!t->GetBytes(&key, "Key") || |
| 78 | !t->GetBytes(&in, "Input") || |
| 79 | !t->GetBytes(&mac, "MAC")) { |
| 80 | return false; |
| 81 | } |
| 82 | if (key.size() != 32 || mac.size() != 16) { |
| 83 | t->PrintLine("Invalid test"); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // Test single-shot operation. |
| 88 | poly1305_state state; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 89 | CRYPTO_poly1305_init(&state, key.data()); |
| 90 | CRYPTO_poly1305_update(&state, in.data(), in.size()); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 91 | // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output. |
| 92 | alignas(16) uint8_t out[16]; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 93 | CRYPTO_poly1305_finish(&state, out); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 94 | if (!t->ExpectBytesEqual(out, 16, mac.data(), mac.size())) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 95 | t->PrintLine("Single-shot Poly1305 failed."); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | // Test streaming byte-by-byte. |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 100 | CRYPTO_poly1305_init(&state, key.data()); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 101 | for (size_t i = 0; i < in.size(); i++) { |
| 102 | CRYPTO_poly1305_update(&state, &in[i], 1); |
| 103 | } |
| 104 | CRYPTO_poly1305_finish(&state, out); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 105 | if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 106 | t->PrintLine("Streaming Poly1305 failed."); |
| 107 | return false; |
| 108 | } |
| 109 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 110 | // Test SIMD stress patterns. OpenSSL's AVX2 assembly needs a multiple of |
| 111 | // four blocks, so test up to three blocks of excess. |
| 112 | if (!TestSIMD(t, 0, key, in, mac) || |
| 113 | !TestSIMD(t, 16, key, in, mac) || |
| 114 | !TestSIMD(t, 32, key, in, mac) || |
| 115 | !TestSIMD(t, 48, key, in, mac)) { |
| 116 | return false; |
| 117 | } |
| 118 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | int main(int argc, char **argv) { |
| 123 | CRYPTO_library_init(); |
| 124 | |
| 125 | if (argc != 2) { |
| 126 | fprintf(stderr, "%s <test file>\n", argv[0]); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | return FileTestMain(TestPoly1305, nullptr, argv[1]); |
| 131 | } |