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 <openssl/crypto.h>
21 #include <openssl/poly1305.h>
22
23 #include "../internal.h"
24 #include "../test/file_test.h"
25
26
TestSIMD(FileTest * t,unsigned excess,const std::vector<uint8_t> & key,const std::vector<uint8_t> & in,const std::vector<uint8_t> & mac)27 static bool TestSIMD(FileTest *t, unsigned excess,
28 const std::vector<uint8_t> &key,
29 const std::vector<uint8_t> &in,
30 const std::vector<uint8_t> &mac) {
31 poly1305_state state;
32 CRYPTO_poly1305_init(&state, key.data());
33
34 size_t done = 0;
35
36 // Feed 16 bytes in. Some implementations begin in non-SIMD mode and upgrade
37 // on-demand. Stress the upgrade path.
38 size_t todo = 16;
39 if (todo > in.size()) {
40 todo = in.size();
41 }
42 CRYPTO_poly1305_update(&state, in.data(), todo);
43 done += todo;
44
45 for (;;) {
46 // Feed 128 + |excess| bytes to test SIMD mode.
47 if (done + 128 + excess > in.size()) {
48 break;
49 }
50 CRYPTO_poly1305_update(&state, in.data() + done, 128 + excess);
51 done += 128 + excess;
52
53 // Feed |excess| bytes to ensure SIMD mode can handle short inputs.
54 if (done + excess > in.size()) {
55 break;
56 }
57 CRYPTO_poly1305_update(&state, in.data() + done, excess);
58 done += excess;
59 }
60
61 // Consume the remainder and finish.
62 CRYPTO_poly1305_update(&state, in.data() + done, in.size() - done);
63
64 // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
65 alignas(16) uint8_t out[16];
66 CRYPTO_poly1305_finish(&state, out);
67 if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) {
68 t->PrintLine("SIMD pattern %u failed.", excess);
69 return false;
70 }
71
72 return true;
73 }
74
TestPoly1305(FileTest * t,void * arg)75 static bool TestPoly1305(FileTest *t, void *arg) {
76 std::vector<uint8_t> key, in, mac;
77 if (!t->GetBytes(&key, "Key") ||
78 !t->GetBytes(&in, "Input") ||
79 !t->GetBytes(&mac, "MAC")) {
80 return false;
81 }
82 if (key.size() != 32 || mac.size() != 16) {
83 t->PrintLine("Invalid test");
84 return false;
85 }
86
87 // Test single-shot operation.
88 poly1305_state state;
89 CRYPTO_poly1305_init(&state, key.data());
90 CRYPTO_poly1305_update(&state, in.data(), in.size());
91 // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
92 alignas(16) uint8_t out[16];
93 CRYPTO_poly1305_finish(&state, out);
94 if (!t->ExpectBytesEqual(out, 16, mac.data(), mac.size())) {
95 t->PrintLine("Single-shot Poly1305 failed.");
96 return false;
97 }
98
99 // Test streaming byte-by-byte.
100 CRYPTO_poly1305_init(&state, key.data());
101 for (size_t i = 0; i < in.size(); i++) {
102 CRYPTO_poly1305_update(&state, &in[i], 1);
103 }
104 CRYPTO_poly1305_finish(&state, out);
105 if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) {
106 t->PrintLine("Streaming Poly1305 failed.");
107 return false;
108 }
109
110 // Test SIMD stress patterns. OpenSSL's AVX2 assembly needs a multiple of
111 // four blocks, so test up to three blocks of excess.
112 if (!TestSIMD(t, 0, key, in, mac) ||
113 !TestSIMD(t, 16, key, in, mac) ||
114 !TestSIMD(t, 32, key, in, mac) ||
115 !TestSIMD(t, 48, key, in, mac)) {
116 return false;
117 }
118
119 return true;
120 }
121
main(int argc,char ** argv)122 int main(int argc, char **argv) {
123 CRYPTO_library_init();
124
125 if (argc != 2) {
126 fprintf(stderr, "%s <test file>\n", argv[0]);
127 return 1;
128 }
129
130 return FileTestMain(TestPoly1305, nullptr, argv[1]);
131 }
132