| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include <vector> |
| 19 |
| 20 | #include <gtest/gtest.h> |
| 21 |
| 22 | #include <openssl/poly1305.h> |
| 23 |
| 24 | #include "../internal.h" |
| 25 | #include "../test/file_test.h" |
| 26 | #include "../test/test_util.h" |
| 27 |
| 28 |
| 29 | static void TestSIMD(unsigned excess, const std::vector<uint8_t> &key, |
| 30 | const std::vector<uint8_t> &in, |
| 31 | const std::vector<uint8_t> &mac) { |
| 32 | poly1305_state state; |
| 33 | CRYPTO_poly1305_init(&state, key.data()); |
| 34 |
| 35 | size_t done = 0; |
| 36 |
| 37 | // Feed 16 bytes in. Some implementations begin in non-SIMD mode and upgrade |
| 38 | // on-demand. Stress the upgrade path. |
| 39 | size_t todo = 16; |
| 40 | if (todo > in.size()) { |
| 41 | todo = in.size(); |
| 42 | } |
| 43 | CRYPTO_poly1305_update(&state, in.data(), todo); |
| 44 | done += todo; |
| 45 |
| 46 | for (;;) { |
| 47 | // Feed 128 + |excess| bytes to test SIMD mode. |
| 48 | if (done + 128 + excess > in.size()) { |
| 49 | break; |
| 50 | } |
| 51 | CRYPTO_poly1305_update(&state, in.data() + done, 128 + excess); |
| 52 | done += 128 + excess; |
| 53 |
| 54 | // Feed |excess| bytes to ensure SIMD mode can handle short inputs. |
| 55 | if (done + excess > in.size()) { |
| 56 | break; |
| 57 | } |
| 58 | CRYPTO_poly1305_update(&state, in.data() + done, excess); |
| 59 | done += excess; |
| 60 | } |
| 61 |
| 62 | // Consume the remainder and finish. |
| 63 | CRYPTO_poly1305_update(&state, in.data() + done, in.size() - done); |
| 64 |
| 65 | uint8_t out[16]; |
| 66 | CRYPTO_poly1305_finish(&state, out); |
| 67 | EXPECT_EQ(Bytes(out), Bytes(mac)) << "SIMD pattern " << excess << " failed."; |
| 68 | } |
| 69 |
| 70 | TEST(Poly1305Test, TestVectors) { |
| 71 | FileTestGTest("crypto/poly1305/poly1305_tests.txt", [](FileTest *t) { |
| 72 | std::vector<uint8_t> key, in, mac; |
| 73 | ASSERT_TRUE(t->GetBytes(&key, "Key")); |
| 74 | ASSERT_TRUE(t->GetBytes(&in, "Input")); |
| 75 | ASSERT_TRUE(t->GetBytes(&mac, "MAC")); |
| 76 | ASSERT_EQ(32u, key.size()); |
| 77 | ASSERT_EQ(16u, mac.size()); |
| 78 |
| 79 | // Test single-shot operation. |
| 80 | poly1305_state state; |
| 81 | CRYPTO_poly1305_init(&state, key.data()); |
| 82 | CRYPTO_poly1305_update(&state, in.data(), in.size()); |
| 83 | uint8_t out[16]; |
| 84 | CRYPTO_poly1305_finish(&state, out); |
| 85 | EXPECT_EQ(Bytes(out), Bytes(mac)) << "Single-shot Poly1305 failed."; |
| 86 |
| 87 | // Test streaming byte-by-byte. |
| 88 | CRYPTO_poly1305_init(&state, key.data()); |
| 89 | for (size_t i = 0; i < in.size(); i++) { |
| 90 | CRYPTO_poly1305_update(&state, &in[i], 1); |
| 91 | } |
| 92 | CRYPTO_poly1305_finish(&state, out); |
| 93 | EXPECT_EQ(Bytes(out), Bytes(mac)) << "Streaming Poly1305 failed."; |
| 94 |
| 95 | // Test |CRYPTO_poly1305_init| and |CRYPTO_poly1305_finish| work on |
| 96 | // unaligned values. |
| 97 | alignas(8) uint8_t unaligned_key[32 + 1]; |
| 98 | OPENSSL_memcpy(unaligned_key + 1, key.data(), 32); |
| 99 | CRYPTO_poly1305_init(&state, unaligned_key + 1); |
| 100 | CRYPTO_poly1305_update(&state, in.data(), in.size()); |
| 101 | alignas(8) uint8_t unaligned_out[16 + 1]; |
| 102 | CRYPTO_poly1305_finish(&state, unaligned_out + 1); |
| 103 | EXPECT_EQ(Bytes(unaligned_out + 1, 16), Bytes(mac)) |
| 104 | << "Unaligned Poly1305 failed."; |
| 105 |
| 106 | // Test SIMD stress patterns. OpenSSL's AVX2 assembly needs a multiple of |
| 107 | // four blocks, so test up to three blocks of excess. |
| 108 | TestSIMD(0, key, in, mac); |
| 109 | TestSIMD(16, key, in, mac); |
| 110 | TestSIMD(32, key, in, mac); |
| 111 | TestSIMD(48, key, in, mac); |
| 112 | }); |
| 113 | } |
| 114 |