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 <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
TestSIMD(unsigned excess,const std::vector<uint8_t> & key,const std::vector<uint8_t> & in,const std::vector<uint8_t> & mac)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 // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
66 alignas(16) uint8_t out[16];
67 CRYPTO_poly1305_finish(&state, out);
68 EXPECT_EQ(Bytes(out), Bytes(mac)) << "SIMD pattern " << excess << " failed.";
69 }
70
TEST(Poly1305Test,TestVectors)71 TEST(Poly1305Test, TestVectors) {
72 FileTestGTest("crypto/poly1305/poly1305_tests.txt", [](FileTest *t) {
73 std::vector<uint8_t> key, in, mac;
74 ASSERT_TRUE(t->GetBytes(&key, "Key"));
75 ASSERT_TRUE(t->GetBytes(&in, "Input"));
76 ASSERT_TRUE(t->GetBytes(&mac, "MAC"));
77 ASSERT_EQ(32u, key.size());
78 ASSERT_EQ(16u, mac.size());
79
80 // Test single-shot operation.
81 poly1305_state state;
82 CRYPTO_poly1305_init(&state, key.data());
83 CRYPTO_poly1305_update(&state, in.data(), in.size());
84 // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
85 alignas(16) uint8_t out[16];
86 CRYPTO_poly1305_finish(&state, out);
87 EXPECT_EQ(Bytes(out), Bytes(mac)) << "Single-shot Poly1305 failed.";
88
89 // Test streaming byte-by-byte.
90 CRYPTO_poly1305_init(&state, key.data());
91 for (size_t i = 0; i < in.size(); i++) {
92 CRYPTO_poly1305_update(&state, &in[i], 1);
93 }
94 CRYPTO_poly1305_finish(&state, out);
95 EXPECT_EQ(Bytes(out), Bytes(mac)) << "Streaming Poly1305 failed.";
96
97 // Test SIMD stress patterns. OpenSSL's AVX2 assembly needs a multiple of
98 // four blocks, so test up to three blocks of excess.
99 TestSIMD(0, key, in, mac);
100 TestSIMD(16, key, in, mac);
101 TestSIMD(32, key, in, mac);
102 TestSIMD(48, key, in, mac);
103 });
104 }
105