blob: 2c25e93b6a92cf7e349c74b63669cbd61c6a799d [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
20#include <openssl/crypto.h>
21#include <openssl/poly1305.h>
22
David Benjamin4969cc92016-04-22 15:02:23 -040023#include "../internal.h"
Kenny Rootb8494592015-09-25 02:29:14 +000024#include "../test/file_test.h"
Kenny Rootb8494592015-09-25 02:29:14 +000025
26
David Benjamin4969cc92016-04-22 15:02:23 -040027static 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}
Kenny Rootb8494592015-09-25 02:29:14 +000074
75static 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;
Adam Langley4139edb2016-01-13 15:00:54 -080089 CRYPTO_poly1305_init(&state, key.data());
90 CRYPTO_poly1305_update(&state, in.data(), in.size());
David Benjamin4969cc92016-04-22 15:02:23 -040091 // |CRYPTO_poly1305_finish| requires a 16-byte-aligned output.
92 alignas(16) uint8_t out[16];
Kenny Rootb8494592015-09-25 02:29:14 +000093 CRYPTO_poly1305_finish(&state, out);
Adam Langley4139edb2016-01-13 15:00:54 -080094 if (!t->ExpectBytesEqual(out, 16, mac.data(), mac.size())) {
Kenny Rootb8494592015-09-25 02:29:14 +000095 t->PrintLine("Single-shot Poly1305 failed.");
96 return false;
97 }
98
99 // Test streaming byte-by-byte.
Adam Langley4139edb2016-01-13 15:00:54 -0800100 CRYPTO_poly1305_init(&state, key.data());
Kenny Rootb8494592015-09-25 02:29:14 +0000101 for (size_t i = 0; i < in.size(); i++) {
102 CRYPTO_poly1305_update(&state, &in[i], 1);
103 }
104 CRYPTO_poly1305_finish(&state, out);
David Benjamin4969cc92016-04-22 15:02:23 -0400105 if (!t->ExpectBytesEqual(mac.data(), mac.size(), out, 16)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000106 t->PrintLine("Streaming Poly1305 failed.");
107 return false;
108 }
109
David Benjamin4969cc92016-04-22 15:02:23 -0400110 // 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
Kenny Rootb8494592015-09-25 02:29:14 +0000119 return true;
120}
121
122int 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}