blob: 198cca12dae54f953690b22634dded1b1430f4be [file] [log] [blame]
Kenny Rootb8494592015-09-25 02:29:14 +00001/* 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
Robert Sloan8ff03552017-06-14 12:40:58 -070020#include <gtest/gtest.h>
21
Kenny Rootb8494592015-09-25 02:29:14 +000022#include <openssl/poly1305.h>
23
David Benjamin4969cc92016-04-22 15:02:23 -040024#include "../internal.h"
Kenny Rootb8494592015-09-25 02:29:14 +000025#include "../test/file_test.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070026#include "../test/test_util.h"
Kenny Rootb8494592015-09-25 02:29:14 +000027
28
Robert Sloan8ff03552017-06-14 12:40:58 -070029static void TestSIMD(unsigned excess, const std::vector<uint8_t> &key,
David Benjamin4969cc92016-04-22 15:02:23 -040030 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);
Robert Sloan8ff03552017-06-14 12:40:58 -070068 EXPECT_EQ(Bytes(out), Bytes(mac)) << "SIMD pattern " << excess << " failed.";
David Benjamin4969cc92016-04-22 15:02:23 -040069}
Kenny Rootb8494592015-09-25 02:29:14 +000070
Robert Sloan8ff03552017-06-14 12:40:58 -070071TEST(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());
Kenny Rootb8494592015-09-25 02:29:14 +000079
Robert Sloan8ff03552017-06-14 12:40:58 -070080 // 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.";
Kenny Rootb8494592015-09-25 02:29:14 +000088
Robert Sloan8ff03552017-06-14 12:40:58 -070089 // 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.";
Kenny Rootb8494592015-09-25 02:29:14 +000096
Robert Sloan8ff03552017-06-14 12:40:58 -070097 // 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 });
Kenny Rootb8494592015-09-25 02:29:14 +0000104}