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