-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreebog.h
224 lines (196 loc) · 12 KB
/
Streebog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#include <string>
#include <vector>
#include <list>
#include "BinNum.h"
/*
* _____ __ __
* / ___// /_________ ___ / /_ ____ ____ _
* \__ \/ __/ ___/ _ \/ _ \/ __ \/ __ \/ __ `/
* ___/ / /_/ / / __/ __/ /_/ / /_/ / /_/ /
* /____/\__/_/ \___/\___/_.___/\____/\__, /
* /____/
*
************************************************************************************************************************
* Streebog, named after the Slavic god of rash wind, is a cryptographic hash function specified in GOST R 34.11-2012.
* It operates with 512 bit blocks, utilizing the Merkle-Damgard construction to handle arbitrary input size. Streebog
* employs 12 rounds per call to the compression function, which utilizes a table of precomputed constants to encode
* data via lossy compression. Both Streebog-512 and Streebog-256 operate on blocks of 512 bits, but begin with different
* states, with the 256 bit version truncating the output to the 256 most significant bits. Streebog's best public
* cryptanalysis at the time of writing was a second preimage attack (given data X, find Y : Y != X, h(Y) = h(X)) with
* 2^266 time complexity. For further information on Streebog, please consult the README.
*
* This project was migraine inducing, and there are a lot of bad implementations out there. Please verify
* that ANY implementation you use at minimum meets the specifications set forth in GOST R 34.11-2012, available in the
* repo in English. You can confirm this implementation meets those standards by running
* ./corto -str --verify
*
* Music Recommendation: shinyflvres - F.M.L. (Acid Techno) https://www.youtube.com/watch?v=TiQxphHv5Ec
************************************************************************************************************************
*/
class Streebog
{
friend class StrFunctor;
private:
BinNum h = initNum512;
BinNum sigma = h;
BinNum N = h;
BinNum initKey = h;
unsigned int transformMatrix[256] = {
252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250, 218, 35, 197, 4, 77,
233, 119, 240, 219, 147, 46, 153, 186, 23, 54, 241, 187, 20, 205, 95, 193,
249, 24, 101, 90, 226, 92, 239, 33, 129, 28, 60, 66, 139, 1, 142, 79,
5, 132, 2, 174, 227, 106, 143, 160, 6, 11, 237, 152, 127, 212, 211, 31,
235, 52, 44, 81, 234, 200, 72, 171, 242, 42, 104, 162, 253, 58, 206, 204,
181, 112, 14, 86, 8, 12, 118, 18, 191, 114, 19, 71, 156, 183, 93, 135,
21, 161, 150, 41, 16, 123, 154, 199, 243, 145, 120, 111, 157, 158, 178, 177,
50, 117, 25, 61, 255, 53, 138, 126, 109, 84, 198, 128, 195, 189, 13, 87,
223, 245, 36, 169, 62, 168, 67, 201, 215, 121, 214, 246, 124, 34, 185, 3,
224, 15, 236, 222, 122, 148, 176, 188, 220, 232, 40, 80, 78, 51, 10, 74,
167, 151, 96, 115, 30, 0, 98, 68, 26, 184, 56, 130, 100, 159, 38, 65,
173, 69, 70, 146, 39, 94, 85, 47, 140, 163, 165, 125, 105, 213, 149, 59,
7, 88, 179, 64, 134, 172, 29, 247, 48, 55, 107, 228, 136, 217, 231, 137,
225, 27, 131, 73, 76, 63, 248, 254, 141, 83, 170, 144, 202, 216, 133, 97,
32, 113, 103, 164, 45, 43, 9, 91, 203, 155, 37, 208, 190, 229, 108, 82,
89, 166, 116, 210, 230, 244, 180, 192, 209, 102, 175, 194, 57, 75, 99, 182
};
unsigned int transformMatrixInv[256] = {
165, 45, 50, 143, 14, 48, 56, 192, 84, 230, 158, 57, 85, 126, 82, 145,
100, 3, 87, 90, 28, 96, 7, 24, 33, 114, 168, 209, 41, 198, 164, 63,
224, 39, 141, 12, 130, 234, 174, 180, 154, 99, 73, 229, 66, 228, 21, 183,
200, 6, 112, 157, 65, 117, 25, 201, 170, 252, 77, 191, 42, 115, 132, 213,
195, 175, 43, 134, 167, 177, 178, 91, 70, 211, 159, 253, 212, 15, 156, 47,
155, 67, 239, 217, 121, 182, 83, 127, 193, 240, 35, 231, 37, 94, 181, 30,
162, 223, 166, 254, 172, 34, 249, 226, 74, 188, 53, 202, 238, 120, 5, 107,
81, 225, 89, 163, 242, 113, 86, 17, 106, 137, 148, 101, 140, 187, 119, 60,
123, 40, 171, 210, 49, 222, 196, 95, 204, 207, 118, 44, 184, 216, 46, 54,
219, 105, 179, 20, 149, 190, 98, 161, 59, 22, 102, 233, 92, 108, 109, 173,
55, 97, 75, 185, 227, 186, 241, 160, 133, 131, 218, 71, 197, 176, 51, 250,
150, 111, 110, 194, 246, 80, 255, 93, 169, 142, 23, 27, 151, 125, 236, 88,
247, 31, 251, 124, 9, 13, 122, 103, 69, 135, 220, 232, 79, 29, 78, 4,
235, 248, 243, 62, 61, 189, 138, 136, 221, 205, 11, 19, 152, 2, 147, 128,
144, 208, 36, 52, 203, 237, 244, 206, 153, 16, 68, 64, 146, 58, 1, 38,
18, 26, 72, 104, 245, 129, 139, 199, 214, 32, 10, 8, 0, 76, 215, 116
};
unsigned int tau[64] = {
0, 8, 16, 24, 32, 40, 48, 56, 1, 9, 17, 25, 33, 41, 49, 57, 2, 10, 18, 26,
34, 42, 50, 58, 3, 11, 19, 27, 35, 43, 51, 59, 4, 12, 20, 28, 36, 44, 52,
60, 5, 13, 21, 29, 37, 45, 53, 61, 6, 14, 22, 30, 38, 46, 54, 62, 7, 15, 23,
31, 39, 47, 55, 63
};
BinNum itConstants[12] = {
BinNum("b1085bda1ecadae9ebcb2f81c0657c1f2f6a76432e45d016714eb88d7585c4fc4b7ce09192676901a2422a08a460d31505767436cc744d23dd806559f2a64507", 512, 16),
BinNum("6fa3b58aa99d2f1a4fe39d460f70b5d7f3feea720a232b9861d55e0f16b501319ab5176b12d699585cb561c2db0aa7ca55dda21bd7cbcd56e679047021b19bb7", 512, 16),
BinNum("f574dcac2bce2fc70a39fc286a3d843506f15e5f529c1f8bf2ea7514b1297b7bd3e20fe490359eb1c1c93a376062db09c2b6f443867adb31991e96f50aba0ab2", 512, 16),
BinNum("ef1fdfb3e81566d2f948e1a05d71e4dd488e857e335c3c7d9d721cad685e353fa9d72c82ed03d675d8b71333935203be3453eaa193e837f1220cbebc84e3d12e", 512, 16),
BinNum("4bea6bacad4747999a3f410c6ca923637f151c1f1686104a359e35d7800fffbdbfcd1747253af5a3dfff00b723271a167a56a27ea9ea63f5601758fd7c6cfe57", 512, 16),
BinNum("ae4faeae1d3ad3d96fa4c33b7a3039c02d66c4f95142a46c187f9ab49af08ec6cffaa6b71c9ab7b40af21f66c2bec6b6bf71c57236904f35fa68407a46647d6e", 512, 16),
BinNum("f4c70e16eeaac5ec51ac86febf240954399ec6c7e6bf87c9d3473e33197a93c90992abc52d822c3706476983284a05043517454ca23c4af38886564d3a14d493", 512, 16),
BinNum("9b1f5b424d93c9a703e7aa020c6e41414eb7f8719c36de1e89b4443b4ddbc49af4892bcb929b069069d18d2bd1a5c42f36acc2355951a8d9a47f0dd4bf02e71e", 512, 16),
BinNum("378f5a541631229b944c9ad8ec165fde3a7d3a1b258942243cd955b7e00d0984800a440bdbb2ceb17b2b8a9aa6079c540e38dc92cb1f2a607261445183235adb", 512, 16),
BinNum("abbedea680056f52382ae548b2e4f3f38941e71cff8a78db1fffe18a1b3361039fe76702af69334b7a1e6c303b7652f43698fad1153bb6c374b4c7fb98459ced", 512, 16),
BinNum("7bcd9ed0efc889fb3002c6cd635afe94d8fa6bbbebab076120018021148466798a1d71efea48b9caefbacd1d7d476e98dea2594ac06fd85d6bcaa4cd81f32d1b", 512, 16),
BinNum("378ee767f11631bad21380b00449b17acda43c32bcdf1d77f82012d430219f9b5d80ef9d1891cc86e71da4aa88e12852faf417d5d9b21b9948bc924af11bd720", 512, 16),
};
std::vector<uint64_t> AManual = {
0x8e20faa72ba0b470, 0x47107ddd9b505a38, 0xad08b0e0c3282d1c, 0xd8045870ef14980e,
0x6c022c38f90a4c07, 0x3601161cf205268d, 0x1b8e0b0e798c13c8, 0x83478b07b2468764,
0xa011d380818e8f40, 0x5086e740ce47c920, 0x2843fd2067adea10, 0x14aff010bdd87508,
0x0ad97808d06cb404, 0x05e23c0468365a02, 0x8c711e02341b2d01, 0x46b60f011a83988e,
0x90dab52a387ae76f, 0x486dd4151c3dfdb9, 0x24b86a840e90f0d2, 0x125c354207487869,
0x092e94218d243cba, 0x8a174a9ec8121e5d, 0x4585254f64090fa0, 0xaccc9ca9328a8950,
0x9d4df05d5f661451, 0xc0a878a0a1330aa6, 0x60543c50de970553, 0x302a1e286fc58ca7,
0x18150f14b9ec46dd, 0x0c84890ad27623e0, 0x0642ca05693b9f70, 0x0321658cba93c138,
0x86275df09ce8aaa8, 0x439da0784e745554, 0xafc0503c273aa42a, 0xd960281e9d1d5215,
0xe230140fc0802984, 0x71180a8960409a42, 0xb60c05ca30204d21, 0x5b068c651810a89e,
0x456c34887a3805b9, 0xac361a443d1c8cd2, 0x561b0d22900e4669, 0x2b838811480723ba,
0x9bcf4486248d9f5d, 0xc3e9224312c8c1a0, 0xeffa11af0964ee50, 0xf97d86d98a327728,
0xe4fa2054a80b329c, 0x727d102a548b194e, 0x39b008152acb8227, 0x9258048415eb419d,
0x492c024284fbaec0, 0xaa16012142f35760, 0x550b8e9e21f7a530, 0xa48b474f9ef5dc18,
0x70a6a56e2440598e, 0x3853dc371220a247, 0x1ca76e95091051ad, 0x0edd37c48a08a6d8,
0x07e095624504536c, 0x8d70c431ac02a736, 0xc83862965601dd1b, 0x641c314b2b8ee083
};
// sBox(BinNum number) const | S
// PRE: 512 bit BinNum passed
// POST: All bytes substituted and returned as 512 bit BinNum
// WARNINGS: None
BinNum sBox(BinNum number) const;
// pBox(BinNum number) const | P
// PRE: 512 bit BinNum passed
// POST: All bytes substituted with tau and returned as 512 bit BinNum
// WARNINGS: None
BinNum pBox(BinNum number) const;
// precompShift(BinNum number) const | L
// PRE: 512 bit BinNum passed
// POST: All bytes XOR'd with constants and returned as 512 bit BinNum
// WARNINGS: None
BinNum precompShift(BinNum number) const;
// keyGen(BinNum initKey) const
// PRE: 512 bit initial key passed
// POST: Vector of all 13 keys returned
// WARNINGS: None
std::vector<BinNum> keyGen(BinNum initKey) const;
// setH(BinNum number)
// PRE: 512 bit BinNum passed
// POST: internal H value set as new number
// WARNINGS: None
void setH(BinNum number);
// setN(BinNum number)
// PRE: 512 bit BinNum passed
// POST: internal N value set as new number
// WARNINGS: None
void setN(BinNum number);
// setSigma(BinNum number)
// PRE: 512 bit BinNum passed
// POST: internal sigma value set as new number
// WARNINGS: None
void setSigma(BinNum number);
// setInitKey(BinNum number)
// PRE: 512 bit BinNum passed
// POST: internal initial key value set as new number
// WARNINGS: None
void setInitKey(BinNum number);
// getH() const
// PRE: H is initialized
// POST: internal H value returned as BinNum
// WARNINGS: None
BinNum getH() const;
// getN() const
// PRE: N is initialized
// POST: internal N value returned as BinNum
// WARNINGS: None
BinNum getN() const;
// getSigma() const
// PRE: sigma is initialized
// POST: internal sigma value returned as BinNum
// WARNINGS: None
BinNum getSigma() const;
// getInitKey() const
// PRE: initKey is initialized
// POST: internal initial key value returned as BinNum
// WARNINGS: None
BinNum getInitKey() const;
// gN(BinNum N, BinNum h, BinNum m) const
// PRE: N, h, m passed as BinNum
// POST: Returns result of compression/round function.
// WARNINGS: Note, N here is not the same as the internal N. Refer to GOST R 34.11-2012 "Round-functions".
BinNum gN(BinNum N, BinNum h, BinNum m) const;
// E(BinNum left, BinNum m) const
// PRE: 512 bit BinNums passed
// POST: Returns result of key mixing with all rounds of LPS for each key as 512 bit BinNum
// WARNINGS: None
BinNum E(BinNum left, BinNum m) const;
public:
BinNum initNum256{ "01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101", 512, 16 }; // 64 bytes of 1 for 256 bits
BinNum initNum512{ "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 512, 16 }; // 64 bytes of 0 for 512 bits
// hash(std::list<std::string> cleartext, int size)
// PRE: Target data is formatted as a list, with a byte per node as a std string, size = 512 || 256
// POST: Returns digest as a string containing the hexadecimal digest.
// WARNINGS: None
std::string hash(std::list<std::string> cleartext, int size);
// verify() const
// PRE: Verification argument provided
// POST: Calls verification functor, demonstrating compliance with GOST R 34.11-2012 testing standards.
// WARNINGS: None
void verify() const;
};