Debian Codegraph @

tests/unit_tests/keccak.cpp

raw
1 // Copyright (c) 2018-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "gtest/gtest.h"
31
32 extern "C" {
33 #include "crypto/keccak.h"
34 }
35
36 #define KECCAK_BLOCKLEN 136
37
38 #define TEST_KECCAK(sz, chunks) \
39 std::string data; \
40 data.resize(sz); \
41 for (size_t i = 0; i < data.size(); ++i) \
42 data[i] = i * 17; \
43 uint8_t md0[32], md1[32]; \
44 keccak((const uint8_t*)data.data(), data.size(), md0, 32); \
45 KECCAK_CTX ctx; \
46 keccak_init(&ctx); \
47 size_t offset = 0; \
48 for (size_t i = 0; i < sizeof(chunks) / sizeof(chunks[0]); ++i) \
49 { \
50 ASSERT_TRUE(offset + chunks[i] <= data.size()); \
51 keccak_update(&ctx, (const uint8_t*)data.data() + offset, chunks[i]); \
52 offset += chunks[i]; \
53 } \
54 ASSERT_TRUE(offset == data.size()); \
55 keccak_finish(&ctx, md1); \
56 ASSERT_EQ(memcmp(md0, md1, 32), 0);
57
58 TEST(keccak, 0_and_0)
59 {
60 static const size_t chunks[] = {0};
61 TEST_KECCAK(0, chunks);
62 }
63
64 TEST(keccak, 1_and_1)
65 {
66 static const size_t chunks[] = {1};
67 TEST_KECCAK(1, chunks);
68 }
69
70 TEST(keccak, 1_and_0_1_0)
71 {
72 static const size_t chunks[] = {0, 1, 0};
73 TEST_KECCAK(1, chunks);
74 }
75
76 TEST(keccak, 2_and_1_1)
77 {
78 static const size_t chunks[] = {1, 1};
79 TEST_KECCAK(2, chunks);
80 }
81
82 TEST(keccak, 4_and_0_0_1_0_2_1_0)
83 {
84 static const size_t chunks[] = {0, 0, 1, 0, 2, 1, 0};
85 TEST_KECCAK(4, chunks);
86 }
87
88 TEST(keccak, 15_and_1_14)
89 {
90 static const size_t chunks[] = {1, 14};
91 TEST_KECCAK(15, chunks);
92 }
93
94 TEST(keccak, 135_and_134_1)
95 {
96 static const size_t chunks[] = {134, 1};
97 TEST_KECCAK(135, chunks);
98 }
99
100 TEST(keccak, 135_and_135_0)
101 {
102 static const size_t chunks[] = {135, 0};
103 TEST_KECCAK(135, chunks);
104 }
105
106 TEST(keccak, 135_and_0_135)
107 {
108 static const size_t chunks[] = {0, 135};
109 TEST_KECCAK(135, chunks);
110 }
111
112 TEST(keccak, 136_and_135_1)
113 {
114 static const size_t chunks[] = {135, 1};
115 TEST_KECCAK(136, chunks);
116 }
117
118 TEST(keccak, 136_and_136_0)
119 {
120 static const size_t chunks[] = {136, 0};
121 TEST_KECCAK(136, chunks);
122 }
123
124 TEST(keccak, 136_and_0_136)
125 {
126 static const size_t chunks[] = {0, 136};
127 TEST_KECCAK(136, chunks);
128 }
129
130 TEST(keccak, 136_and_136)
131 {
132 static const size_t chunks[] = {136};
133 TEST_KECCAK(136, chunks);
134 }
135
136 TEST(keccak, 137_and_136_1)
137 {
138 static const size_t chunks[] = {136, 1};
139 TEST_KECCAK(137, chunks);
140 }
141
142 TEST(keccak, 137_and_1_136)
143 {
144 static const size_t chunks[] = {1, 136};
145 TEST_KECCAK(137, chunks);
146 }
147
148 TEST(keccak, alignment)
149 {
150 uint8_t data[6064];
151 __attribute__ ((aligned(16))) char adata[6000];
152
153 for (size_t i = 0; i < sizeof(data) / sizeof(data[0]); ++i)
154 data[i] = i & 1;
155
156 uint8_t md[32], amd[32];
157 for (int offset = 0; offset < 64; ++offset)
158 {
159 memcpy(adata, data + offset, 6000);
160 keccak((const uint8_t*)&data[offset], 6000, md, 32);
161 keccak((const uint8_t*)adata, 6000, amd, 32);
162 ASSERT_TRUE(!memcmp(md, amd, 32));
163 }
164 }