Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/poly1305/poly1305_test.cc

raw
1 // Copyright (c) 2015, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <stdio.h>
5 #include <string.h>
6
7 #include <vector>
8
9 #include <gtest/gtest.h>
10
11 #include <openssl/poly1305.h>
12
13 #include "../internal.h"
14 #include "../test/file_test.h"
15 #include "../test/test_util.h"
16
17
18 static void TestSIMD(unsigned excess, const std::vector<uint8_t> &key,
19 const std::vector<uint8_t> &in,
20 const std::vector<uint8_t> &mac) {
21 poly1305_state state;
22 CRYPTO_poly1305_init(&state, key.data());
23
24 size_t done = 0;
25
26 // Feed 16 bytes in. Some implementations begin in non-SIMD mode and upgrade
27 // on-demand. Stress the upgrade path.
28 size_t todo = 16;
29 if (todo > in.size()) {
30 todo = in.size();
31 }
32 CRYPTO_poly1305_update(&state, in.data(), todo);
33 done += todo;
34
35 for (;;) {
36 // Feed 128 + |excess| bytes to test SIMD mode.
37 if (done + 128 + excess > in.size()) {
38 break;
39 }
40 CRYPTO_poly1305_update(&state, in.data() + done, 128 + excess);
41 done += 128 + excess;
42
43 // Feed |excess| bytes to ensure SIMD mode can handle short inputs.
44 if (done + excess > in.size()) {
45 break;
46 }
47 CRYPTO_poly1305_update(&state, in.data() + done, excess);
48 done += excess;
49 }
50
51 // Consume the remainder and finish.
52 CRYPTO_poly1305_update(&state, in.data() + done, in.size() - done);
53
54 uint8_t out[16];
55 CRYPTO_poly1305_finish(&state, out);
56 EXPECT_EQ(Bytes(out), Bytes(mac)) << "SIMD pattern " << excess << " failed.";
57 }
58
59 TEST(Poly1305Test, TestVectors) {
60 FileTestGTest("crypto/poly1305/poly1305_tests.txt", [](FileTest *t) {
61 std::vector<uint8_t> key, in, mac;
62 ASSERT_TRUE(t->GetBytes(&key, "Key"));
63 ASSERT_TRUE(t->GetBytes(&in, "Input"));
64 ASSERT_TRUE(t->GetBytes(&mac, "MAC"));
65 ASSERT_EQ(32u, key.size());
66 ASSERT_EQ(16u, mac.size());
67
68 // Test single-shot operation.
69 poly1305_state state;
70 CRYPTO_poly1305_init(&state, key.data());
71 CRYPTO_poly1305_update(&state, in.data(), in.size());
72 uint8_t out[16];
73 CRYPTO_poly1305_finish(&state, out);
74 EXPECT_EQ(Bytes(out), Bytes(mac)) << "Single-shot Poly1305 failed.";
75
76 // Test streaming byte-by-byte.
77 CRYPTO_poly1305_init(&state, key.data());
78 for (size_t i = 0; i < in.size(); i++) {
79 CRYPTO_poly1305_update(&state, &in[i], 1);
80 }
81 CRYPTO_poly1305_finish(&state, out);
82 EXPECT_EQ(Bytes(out), Bytes(mac)) << "Streaming Poly1305 failed.";
83
84 // Test |CRYPTO_poly1305_init| and |CRYPTO_poly1305_finish| work on
85 // unaligned values.
86 alignas(8) uint8_t unaligned_key[32 + 1];
87 OPENSSL_memcpy(unaligned_key + 1, key.data(), 32);
88 CRYPTO_poly1305_init(&state, unaligned_key + 1);
89 CRYPTO_poly1305_update(&state, in.data(), in.size());
90 alignas(8) uint8_t unaligned_out[16 + 1];
91 CRYPTO_poly1305_finish(&state, unaligned_out + 1);
92 EXPECT_EQ(Bytes(unaligned_out + 1, 16), Bytes(mac))
93 << "Unaligned Poly1305 failed.";
94
95 // Test SIMD stress patterns. OpenSSL's AVX2 assembly needs a multiple of
96 // four blocks, so test up to three blocks of excess.
97 TestSIMD(0, key, in, mac);
98 TestSIMD(16, key, in, mac);
99 TestSIMD(32, key, in, mac);
100 TestSIMD(48, key, in, mac);
101 });
102 }