poly1305_test.cc
Go to the documentation of this file.
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 
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) {
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  }
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];
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.
81  CRYPTO_poly1305_init(&state, key.data());
82  CRYPTO_poly1305_update(&state, in.data(), in.size());
83  uint8_t out[16];
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++) {
91  }
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 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
string.h
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
FileTestGTest
void FileTestGTest(const char *path, std::function< void(FileTest *)> run_test)
Definition: file_test_gtest.cc:68
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
poly1305.h
FileTest
Definition: file_test.h:90
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
TestSIMD
static void TestSIMD(unsigned excess, const std::vector< uint8_t > &key, const std::vector< uint8_t > &in, const std::vector< uint8_t > &mac)
Definition: poly1305_test.cc:29
CRYPTO_poly1305_init
#define CRYPTO_poly1305_init
Definition: boringssl_prefix_symbols.h:1184
key
const char * key
Definition: hpack_parser_table.cc:164
CRYPTO_poly1305_finish
#define CRYPTO_poly1305_finish
Definition: boringssl_prefix_symbols.h:1183
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
TEST
TEST(Poly1305Test, TestVectors)
Definition: poly1305_test.cc:70
poly1305_state
uint8_t poly1305_state[512]
Definition: poly1305.h:25
mkowners.todo
todo
Definition: mkowners.py:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
CRYPTO_poly1305_update
#define CRYPTO_poly1305_update
Definition: boringssl_prefix_symbols.h:1185


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:44