ttcrypt 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +27 -0
- data/.rspec +2 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +44 -0
- data/README.md +45 -0
- data/Rakefile +10 -0
- data/ext/ttcrypt/.gitignore +1 -0
- data/ext/ttcrypt/big_integer.cpp +59 -0
- data/ext/ttcrypt/big_integer.h +321 -0
- data/ext/ttcrypt/byte_buffer.cpp +84 -0
- data/ext/ttcrypt/byte_buffer.h +352 -0
- data/ext/ttcrypt/common_utils.cpp +24 -0
- data/ext/ttcrypt/common_utils.h +61 -0
- data/ext/ttcrypt/extconf.rb +62 -0
- data/ext/ttcrypt/pollard_rho.cpp +83 -0
- data/ext/ttcrypt/pollard_rho.h +55 -0
- data/ext/ttcrypt/rsa_key.cpp +373 -0
- data/ext/ttcrypt/rsa_key.h +248 -0
- data/ext/ttcrypt/ruby_cpp_tools.cpp +66 -0
- data/ext/ttcrypt/ruby_cpp_tools.h +77 -0
- data/ext/ttcrypt/sha1.cpp +185 -0
- data/ext/ttcrypt/sha1.h +49 -0
- data/ext/ttcrypt/sha256.c +379 -0
- data/ext/ttcrypt/sha256.h +67 -0
- data/ext/ttcrypt/text_utils.cpp +63 -0
- data/ext/ttcrypt/text_utils.h +64 -0
- data/ext/ttcrypt/ttcrypt.cpp +51 -0
- data/ext/ttcrypt/ttcrypt.h +45 -0
- data/ext/ttcrypt/ttcrypt_ruby.cpp +234 -0
- data/lib/ttcrypt/version.rb +5 -0
- data/lib/ttcrypt.rb +142 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/ttcrypt_spec.rb +318 -0
- data/ttcrypt.gemspec +47 -0
- metadata +139 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
/*
|
2
|
+
* ruby_cpp_tools.h
|
3
|
+
*
|
4
|
+
* Created on: 18 июня 2014 г.
|
5
|
+
* Author: sergeych
|
6
|
+
* Copyright (C) 2014 Thrift, Sergey S. Chernov
|
7
|
+
*/
|
8
|
+
|
9
|
+
/*
|
10
|
+
This program is free software: you can redistribute it and/or modify
|
11
|
+
it under the terms of the GNU General Public License as published by
|
12
|
+
the Free Software Foundation, either version 3 of the License, or
|
13
|
+
(at your option) any later version.
|
14
|
+
|
15
|
+
This program is distributed in the hope that it will be useful,
|
16
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
GNU General Public License for more details.
|
19
|
+
|
20
|
+
You should have received a copy of the GNU General Public License
|
21
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
*/
|
23
|
+
|
24
|
+
|
25
|
+
#ifndef RUBY_CPP_TOOLS_H_
|
26
|
+
#define RUBY_CPP_TOOLS_H_
|
27
|
+
|
28
|
+
#include "byte_buffer.h"
|
29
|
+
#include "big_integer.h"
|
30
|
+
using namespace thrift;
|
31
|
+
using namespace std;
|
32
|
+
|
33
|
+
extern "C" {
|
34
|
+
#include <ruby.h>
|
35
|
+
#include <ruby/thread.h>
|
36
|
+
}
|
37
|
+
|
38
|
+
typedef VALUE (*ruby_method)(...);
|
39
|
+
|
40
|
+
inline byte_buffer value_to_byte_buffer(VALUE val) {
|
41
|
+
val = StringValue(val);
|
42
|
+
return byte_buffer(RSTRING_PTR(val), RSTRING_LEN(val));
|
43
|
+
}
|
44
|
+
|
45
|
+
inline string value_to_string(VALUE val) {
|
46
|
+
val = StringValue(val);
|
47
|
+
return string(RSTRING_PTR(val), RSTRING_LEN(val));
|
48
|
+
}
|
49
|
+
|
50
|
+
inline VALUE to_hex_value(const big_integer& value) {
|
51
|
+
string s = value.to_string(16);
|
52
|
+
return rb_str_new(s.c_str(), s.length());
|
53
|
+
}
|
54
|
+
|
55
|
+
inline VALUE to_value(const byte_buffer& value) {
|
56
|
+
return rb_str_new((const char*)value.data().get(), value.size());
|
57
|
+
}
|
58
|
+
|
59
|
+
inline VALUE to_rb_string(const byte_buffer& bb) {
|
60
|
+
return rb_str_new((const char*) bb.data().get(), bb.size());
|
61
|
+
}
|
62
|
+
|
63
|
+
inline VALUE to_rb_string(const string& s) {
|
64
|
+
return rb_str_new(s.c_str(), s.length());
|
65
|
+
}
|
66
|
+
|
67
|
+
inline VALUE to_rb_sym(const string& s) {
|
68
|
+
return ID2SYM(rb_intern(s.c_str()));
|
69
|
+
}
|
70
|
+
|
71
|
+
VALUE ruby_unblock2(const std::function<void*()>& block);
|
72
|
+
|
73
|
+
void ruby_unblock(const std::function<void(void)>& block);
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
#endif /* RUBY_CPP_TOOLS_H_ */
|
@@ -0,0 +1,185 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (c) 2011, Micael Hildenborg
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
* Redistributions of source code must retain the above copyright
|
8
|
+
notice, this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
11
|
+
documentation and/or other materials provided with the distribution.
|
12
|
+
* Neither the name of Micael Hildenborg nor the
|
13
|
+
names of its contributors may be used to endorse or promote products
|
14
|
+
derived from this software without specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY Micael Hildenborg ''AS IS'' AND ANY
|
17
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL Micael Hildenborg BE LIABLE FOR ANY
|
20
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
21
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
22
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
23
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
25
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
*/
|
27
|
+
|
28
|
+
/*
|
29
|
+
Contributors:
|
30
|
+
Gustav
|
31
|
+
Several members in the gamedev.se forum.
|
32
|
+
Gregory Petrosyan
|
33
|
+
*/
|
34
|
+
|
35
|
+
#include "sha1.h"
|
36
|
+
|
37
|
+
namespace sha1
|
38
|
+
{
|
39
|
+
namespace // local
|
40
|
+
{
|
41
|
+
// Rotate an integer value to left.
|
42
|
+
inline const unsigned int rol(const unsigned int value,
|
43
|
+
const unsigned int steps)
|
44
|
+
{
|
45
|
+
return ((value << steps) | (value >> (32 - steps)));
|
46
|
+
}
|
47
|
+
|
48
|
+
// Sets the first 16 integers in the buffert to zero.
|
49
|
+
// Used for clearing the W buffert.
|
50
|
+
inline void clearWBuffert(unsigned int* buffert)
|
51
|
+
{
|
52
|
+
for (int pos = 16; --pos >= 0;)
|
53
|
+
{
|
54
|
+
buffert[pos] = 0;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
void innerHash(unsigned int* result, unsigned int* w)
|
59
|
+
{
|
60
|
+
unsigned int a = result[0];
|
61
|
+
unsigned int b = result[1];
|
62
|
+
unsigned int c = result[2];
|
63
|
+
unsigned int d = result[3];
|
64
|
+
unsigned int e = result[4];
|
65
|
+
|
66
|
+
int round = 0;
|
67
|
+
|
68
|
+
#define sha1macro(func,val) \
|
69
|
+
{ \
|
70
|
+
const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
|
71
|
+
e = d; \
|
72
|
+
d = c; \
|
73
|
+
c = rol(b, 30); \
|
74
|
+
b = a; \
|
75
|
+
a = t; \
|
76
|
+
}
|
77
|
+
|
78
|
+
while (round < 16)
|
79
|
+
{
|
80
|
+
sha1macro((b & c) | (~b & d), 0x5a827999)
|
81
|
+
++round;
|
82
|
+
}
|
83
|
+
while (round < 20)
|
84
|
+
{
|
85
|
+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
|
86
|
+
sha1macro((b & c) | (~b & d), 0x5a827999)
|
87
|
+
++round;
|
88
|
+
}
|
89
|
+
while (round < 40)
|
90
|
+
{
|
91
|
+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
|
92
|
+
sha1macro(b ^ c ^ d, 0x6ed9eba1)
|
93
|
+
++round;
|
94
|
+
}
|
95
|
+
while (round < 60)
|
96
|
+
{
|
97
|
+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
|
98
|
+
sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
|
99
|
+
++round;
|
100
|
+
}
|
101
|
+
while (round < 80)
|
102
|
+
{
|
103
|
+
w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
|
104
|
+
sha1macro(b ^ c ^ d, 0xca62c1d6)
|
105
|
+
++round;
|
106
|
+
}
|
107
|
+
|
108
|
+
#undef sha1macro
|
109
|
+
|
110
|
+
result[0] += a;
|
111
|
+
result[1] += b;
|
112
|
+
result[2] += c;
|
113
|
+
result[3] += d;
|
114
|
+
result[4] += e;
|
115
|
+
}
|
116
|
+
} // namespace
|
117
|
+
|
118
|
+
void calc(const void* src, const int bytelength, unsigned char* hash)
|
119
|
+
{
|
120
|
+
// Init the result array.
|
121
|
+
unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 };
|
122
|
+
|
123
|
+
// Cast the void src pointer to be the byte array we can work with.
|
124
|
+
const unsigned char* sarray = (const unsigned char*) src;
|
125
|
+
|
126
|
+
// The reusable round buffer
|
127
|
+
unsigned int w[80];
|
128
|
+
|
129
|
+
// Loop through all complete 64byte blocks.
|
130
|
+
const int endOfFullBlocks = bytelength - 64;
|
131
|
+
int endCurrentBlock;
|
132
|
+
int currentBlock = 0;
|
133
|
+
|
134
|
+
while (currentBlock <= endOfFullBlocks)
|
135
|
+
{
|
136
|
+
endCurrentBlock = currentBlock + 64;
|
137
|
+
|
138
|
+
// Init the round buffer with the 64 byte block data.
|
139
|
+
for (int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4)
|
140
|
+
{
|
141
|
+
// This line will swap endian on big endian and keep endian on little endian.
|
142
|
+
w[roundPos++] = (unsigned int) sarray[currentBlock + 3]
|
143
|
+
| (((unsigned int) sarray[currentBlock + 2]) << 8)
|
144
|
+
| (((unsigned int) sarray[currentBlock + 1]) << 16)
|
145
|
+
| (((unsigned int) sarray[currentBlock]) << 24);
|
146
|
+
}
|
147
|
+
innerHash(result, w);
|
148
|
+
}
|
149
|
+
|
150
|
+
// Handle the last and not full 64 byte block if existing.
|
151
|
+
endCurrentBlock = bytelength - currentBlock;
|
152
|
+
clearWBuffert(w);
|
153
|
+
int lastBlockBytes = 0;
|
154
|
+
for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes)
|
155
|
+
{
|
156
|
+
w[lastBlockBytes >> 2] |= (unsigned int) sarray[lastBlockBytes + currentBlock] << ((3 - (lastBlockBytes & 3)) << 3);
|
157
|
+
}
|
158
|
+
w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
|
159
|
+
if (endCurrentBlock >= 56)
|
160
|
+
{
|
161
|
+
innerHash(result, w);
|
162
|
+
clearWBuffert(w);
|
163
|
+
}
|
164
|
+
w[15] = bytelength << 3;
|
165
|
+
innerHash(result, w);
|
166
|
+
|
167
|
+
// Store hash in result pointer, and make sure we get in in the correct order on both endian models.
|
168
|
+
for (int hashByte = 20; --hashByte >= 0;)
|
169
|
+
{
|
170
|
+
hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
|
171
|
+
}
|
172
|
+
}
|
173
|
+
|
174
|
+
void toHexString(const unsigned char* hash, char* hexstring)
|
175
|
+
{
|
176
|
+
const char hexDigits[] = { "0123456789abcdef" };
|
177
|
+
|
178
|
+
for (int hashByte = 20; --hashByte >= 0;)
|
179
|
+
{
|
180
|
+
hexstring[hashByte << 1] = hexDigits[(hash[hashByte] >> 4) & 0xf];
|
181
|
+
hexstring[(hashByte << 1) + 1] = hexDigits[hash[hashByte] & 0xf];
|
182
|
+
}
|
183
|
+
hexstring[40] = 0;
|
184
|
+
}
|
185
|
+
} // namespace sha1
|
data/ext/ttcrypt/sha1.h
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (c) 2011, Micael Hildenborg
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
* Redistributions of source code must retain the above copyright
|
8
|
+
notice, this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
11
|
+
documentation and/or other materials provided with the distribution.
|
12
|
+
* Neither the name of Micael Hildenborg nor the
|
13
|
+
names of its contributors may be used to endorse or promote products
|
14
|
+
derived from this software without specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY Micael Hildenborg ''AS IS'' AND ANY
|
17
|
+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL Micael Hildenborg BE LIABLE FOR ANY
|
20
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
21
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
22
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
23
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
25
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26
|
+
*/
|
27
|
+
|
28
|
+
#ifndef SHA1_DEFINED
|
29
|
+
#define SHA1_DEFINED
|
30
|
+
|
31
|
+
namespace sha1
|
32
|
+
{
|
33
|
+
|
34
|
+
/**
|
35
|
+
@param src points to any kind of data to be hashed.
|
36
|
+
@param bytelength the number of bytes to hash from the src pointer.
|
37
|
+
@param hash should point to a buffer of at least 20 bytes of size for storing the sha1 result in.
|
38
|
+
*/
|
39
|
+
void calc(const void* src, const int bytelength, unsigned char* hash);
|
40
|
+
|
41
|
+
/**
|
42
|
+
@param hash is 20 bytes of sha1 hash. This is the same data that is the result from the calc function.
|
43
|
+
@param hexstring should point to a buffer of at least 41 bytes of size for storing the hexadecimal representation of the hash. A zero will be written at position 40, so the buffer will be a valid zero ended string.
|
44
|
+
*/
|
45
|
+
void toHexString(const unsigned char* hash, char* hexstring);
|
46
|
+
|
47
|
+
} // namespace sha1
|
48
|
+
|
49
|
+
#endif // SHA1_DEFINED
|
@@ -0,0 +1,379 @@
|
|
1
|
+
//
|
2
|
+
// sha256.c
|
3
|
+
// iGlomp
|
4
|
+
//
|
5
|
+
// Created by Sergey Chernov on 28.10.11.
|
6
|
+
// Copyright (c) 2011 Thrift. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
/*
|
10
|
+
* FIPS-180-2 compliant SHA-256 implementation
|
11
|
+
*
|
12
|
+
* Copyright (C) 2001-2003 Christophe Devine
|
13
|
+
*
|
14
|
+
* This program is free software; you can redistribute it and/or modify
|
15
|
+
* it under the terms of the GNU General Public License as published by
|
16
|
+
* the Free Software Foundation; either version 2 of the License, or
|
17
|
+
* (at your option) any later version.
|
18
|
+
*
|
19
|
+
* This program is distributed in the hope that it will be useful,
|
20
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
+
* GNU General Public License for more details.
|
23
|
+
*
|
24
|
+
* You should have received a copy of the GNU General Public License
|
25
|
+
* along with this program; if not, write to the Free Software
|
26
|
+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include <string.h>
|
30
|
+
|
31
|
+
#include "sha256.h"
|
32
|
+
|
33
|
+
void sha256_process( sha256_context *ctx, uint8 data[64] );
|
34
|
+
|
35
|
+
#define GET_UINT32(n,b,i) \
|
36
|
+
{ \
|
37
|
+
(n) = ( (uint32) (b)[(i) ] << 24 ) \
|
38
|
+
| ( (uint32) (b)[(i) + 1] << 16 ) \
|
39
|
+
| ( (uint32) (b)[(i) + 2] << 8 ) \
|
40
|
+
| ( (uint32) (b)[(i) + 3] ); \
|
41
|
+
}
|
42
|
+
|
43
|
+
#define PUT_UINT32(n,b,i) \
|
44
|
+
{ \
|
45
|
+
(b)[(i) ] = (uint8) ( (n) >> 24 ); \
|
46
|
+
(b)[(i) + 1] = (uint8) ( (n) >> 16 ); \
|
47
|
+
(b)[(i) + 2] = (uint8) ( (n) >> 8 ); \
|
48
|
+
(b)[(i) + 3] = (uint8) ( (n) ); \
|
49
|
+
}
|
50
|
+
|
51
|
+
void sha256_starts( sha256_context *ctx )
|
52
|
+
{
|
53
|
+
ctx->total[0] = 0;
|
54
|
+
ctx->total[1] = 0;
|
55
|
+
|
56
|
+
ctx->state[0] = 0x6A09E667;
|
57
|
+
ctx->state[1] = 0xBB67AE85;
|
58
|
+
ctx->state[2] = 0x3C6EF372;
|
59
|
+
ctx->state[3] = 0xA54FF53A;
|
60
|
+
ctx->state[4] = 0x510E527F;
|
61
|
+
ctx->state[5] = 0x9B05688C;
|
62
|
+
ctx->state[6] = 0x1F83D9AB;
|
63
|
+
ctx->state[7] = 0x5BE0CD19;
|
64
|
+
}
|
65
|
+
|
66
|
+
void sha256_process( sha256_context *ctx, uint8 data[64] )
|
67
|
+
{
|
68
|
+
uint32 temp1, temp2, W[64];
|
69
|
+
uint32 A, B, C, D, E, F, G, H;
|
70
|
+
|
71
|
+
GET_UINT32( W[0], data, 0 );
|
72
|
+
GET_UINT32( W[1], data, 4 );
|
73
|
+
GET_UINT32( W[2], data, 8 );
|
74
|
+
GET_UINT32( W[3], data, 12 );
|
75
|
+
GET_UINT32( W[4], data, 16 );
|
76
|
+
GET_UINT32( W[5], data, 20 );
|
77
|
+
GET_UINT32( W[6], data, 24 );
|
78
|
+
GET_UINT32( W[7], data, 28 );
|
79
|
+
GET_UINT32( W[8], data, 32 );
|
80
|
+
GET_UINT32( W[9], data, 36 );
|
81
|
+
GET_UINT32( W[10], data, 40 );
|
82
|
+
GET_UINT32( W[11], data, 44 );
|
83
|
+
GET_UINT32( W[12], data, 48 );
|
84
|
+
GET_UINT32( W[13], data, 52 );
|
85
|
+
GET_UINT32( W[14], data, 56 );
|
86
|
+
GET_UINT32( W[15], data, 60 );
|
87
|
+
|
88
|
+
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
|
89
|
+
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
|
90
|
+
|
91
|
+
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
|
92
|
+
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
|
93
|
+
|
94
|
+
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
|
95
|
+
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
|
96
|
+
|
97
|
+
#define F0(x,y,z) ((x & y) | (z & (x | y)))
|
98
|
+
#define F1(x,y,z) (z ^ (x & (y ^ z)))
|
99
|
+
|
100
|
+
#define R(t) \
|
101
|
+
( \
|
102
|
+
W[t] = S1(W[t - 2]) + W[t - 7] + \
|
103
|
+
S0(W[t - 15]) + W[t - 16] \
|
104
|
+
)
|
105
|
+
|
106
|
+
#define P(a,b,c,d,e,f,g,h,x,K) \
|
107
|
+
{ \
|
108
|
+
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
|
109
|
+
temp2 = S2(a) + F0(a,b,c); \
|
110
|
+
d += temp1; h = temp1 + temp2; \
|
111
|
+
}
|
112
|
+
|
113
|
+
A = ctx->state[0];
|
114
|
+
B = ctx->state[1];
|
115
|
+
C = ctx->state[2];
|
116
|
+
D = ctx->state[3];
|
117
|
+
E = ctx->state[4];
|
118
|
+
F = ctx->state[5];
|
119
|
+
G = ctx->state[6];
|
120
|
+
H = ctx->state[7];
|
121
|
+
|
122
|
+
P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
|
123
|
+
P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
|
124
|
+
P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
|
125
|
+
P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
|
126
|
+
P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
|
127
|
+
P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
|
128
|
+
P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
|
129
|
+
P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
|
130
|
+
P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
|
131
|
+
P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
|
132
|
+
P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
|
133
|
+
P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
|
134
|
+
P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
|
135
|
+
P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
|
136
|
+
P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
|
137
|
+
P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
|
138
|
+
P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
|
139
|
+
P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
|
140
|
+
P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
|
141
|
+
P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
|
142
|
+
P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
|
143
|
+
P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
|
144
|
+
P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
|
145
|
+
P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
|
146
|
+
P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
|
147
|
+
P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
|
148
|
+
P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
|
149
|
+
P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
|
150
|
+
P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
|
151
|
+
P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
|
152
|
+
P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
|
153
|
+
P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
|
154
|
+
P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
|
155
|
+
P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
|
156
|
+
P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
|
157
|
+
P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
|
158
|
+
P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
|
159
|
+
P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
|
160
|
+
P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
|
161
|
+
P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
|
162
|
+
P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
|
163
|
+
P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
|
164
|
+
P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
|
165
|
+
P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
|
166
|
+
P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
|
167
|
+
P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
|
168
|
+
P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
|
169
|
+
P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
|
170
|
+
P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
|
171
|
+
P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
|
172
|
+
P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
|
173
|
+
P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
|
174
|
+
P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
|
175
|
+
P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
|
176
|
+
P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
|
177
|
+
P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
|
178
|
+
P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
|
179
|
+
P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
|
180
|
+
P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
|
181
|
+
P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
|
182
|
+
P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
|
183
|
+
P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
|
184
|
+
P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
|
185
|
+
P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
|
186
|
+
|
187
|
+
ctx->state[0] += A;
|
188
|
+
ctx->state[1] += B;
|
189
|
+
ctx->state[2] += C;
|
190
|
+
ctx->state[3] += D;
|
191
|
+
ctx->state[4] += E;
|
192
|
+
ctx->state[5] += F;
|
193
|
+
ctx->state[6] += G;
|
194
|
+
ctx->state[7] += H;
|
195
|
+
}
|
196
|
+
|
197
|
+
void sha256_update( sha256_context *ctx, uint8 *input, uint32 length )
|
198
|
+
{
|
199
|
+
uint32 left, fill;
|
200
|
+
|
201
|
+
if( ! length ) return;
|
202
|
+
|
203
|
+
left = ctx->total[0] & 0x3F;
|
204
|
+
fill = 64 - left;
|
205
|
+
|
206
|
+
ctx->total[0] += length;
|
207
|
+
ctx->total[0] &= 0xFFFFFFFF;
|
208
|
+
|
209
|
+
if( ctx->total[0] < length )
|
210
|
+
ctx->total[1]++;
|
211
|
+
|
212
|
+
if( left && length >= fill )
|
213
|
+
{
|
214
|
+
memcpy( (void *) (ctx->buffer + left),
|
215
|
+
(void *) input, fill );
|
216
|
+
sha256_process( ctx, ctx->buffer );
|
217
|
+
length -= fill;
|
218
|
+
input += fill;
|
219
|
+
left = 0;
|
220
|
+
}
|
221
|
+
|
222
|
+
while( length >= 64 )
|
223
|
+
{
|
224
|
+
sha256_process( ctx, input );
|
225
|
+
length -= 64;
|
226
|
+
input += 64;
|
227
|
+
}
|
228
|
+
|
229
|
+
if( length )
|
230
|
+
{
|
231
|
+
memcpy( (void *) (ctx->buffer + left),
|
232
|
+
(void *) input, length );
|
233
|
+
}
|
234
|
+
}
|
235
|
+
|
236
|
+
static uint8 sha256_padding[64] =
|
237
|
+
{
|
238
|
+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
239
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
240
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
241
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
242
|
+
};
|
243
|
+
|
244
|
+
void sha256_finish( sha256_context *ctx, uint8 digest[32] )
|
245
|
+
{
|
246
|
+
uint32 last, padn;
|
247
|
+
uint32 high, low;
|
248
|
+
uint8 msglen[8];
|
249
|
+
|
250
|
+
high = ( ctx->total[0] >> 29 )
|
251
|
+
| ( ctx->total[1] << 3 );
|
252
|
+
low = ( ctx->total[0] << 3 );
|
253
|
+
|
254
|
+
PUT_UINT32( high, msglen, 0 );
|
255
|
+
PUT_UINT32( low, msglen, 4 );
|
256
|
+
|
257
|
+
last = ctx->total[0] & 0x3F;
|
258
|
+
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
|
259
|
+
|
260
|
+
sha256_update( ctx, sha256_padding, padn );
|
261
|
+
sha256_update( ctx, msglen, 8 );
|
262
|
+
|
263
|
+
PUT_UINT32( ctx->state[0], digest, 0 );
|
264
|
+
PUT_UINT32( ctx->state[1], digest, 4 );
|
265
|
+
PUT_UINT32( ctx->state[2], digest, 8 );
|
266
|
+
PUT_UINT32( ctx->state[3], digest, 12 );
|
267
|
+
PUT_UINT32( ctx->state[4], digest, 16 );
|
268
|
+
PUT_UINT32( ctx->state[5], digest, 20 );
|
269
|
+
PUT_UINT32( ctx->state[6], digest, 24 );
|
270
|
+
PUT_UINT32( ctx->state[7], digest, 28 );
|
271
|
+
}
|
272
|
+
|
273
|
+
#ifdef TEST_SHA256
|
274
|
+
|
275
|
+
#include <stdlib.h>
|
276
|
+
#include <stdio.h>
|
277
|
+
|
278
|
+
/*
|
279
|
+
* those are the standard FIPS-180-2 test vectors
|
280
|
+
*/
|
281
|
+
|
282
|
+
static char *msg[] =
|
283
|
+
{
|
284
|
+
"abc",
|
285
|
+
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
286
|
+
NULL
|
287
|
+
};
|
288
|
+
|
289
|
+
static char *val[] =
|
290
|
+
{
|
291
|
+
"ba7816bf8f01cfea414140de5dae2223" \
|
292
|
+
"b00361a396177a9cb410ff61f20015ad",
|
293
|
+
"248d6a61d20638b8e5c026930c3e6039" \
|
294
|
+
"a33ce45964ff2167f6ecedd419db06c1",
|
295
|
+
"cdc76e5c9914fb9281a1c7e284d73e67" \
|
296
|
+
"f1809a48a497200e046d39ccc7112cd0"
|
297
|
+
};
|
298
|
+
|
299
|
+
int main( int argc, char *argv[] )
|
300
|
+
{
|
301
|
+
FILE *f;
|
302
|
+
int i, j;
|
303
|
+
char output[65];
|
304
|
+
sha256_context ctx;
|
305
|
+
unsigned char buf[1000];
|
306
|
+
unsigned char sha256sum[32];
|
307
|
+
|
308
|
+
if( argc < 2 )
|
309
|
+
{
|
310
|
+
printf( "\n SHA-256 Validation Tests:\n\n" );
|
311
|
+
|
312
|
+
for( i = 0; i < 3; i++ )
|
313
|
+
{
|
314
|
+
printf( " Test %d ", i + 1 );
|
315
|
+
|
316
|
+
sha256_starts( &ctx );
|
317
|
+
|
318
|
+
if( i < 2 )
|
319
|
+
{
|
320
|
+
sha256_update( &ctx, (uint8 *) msg[i],
|
321
|
+
strlen( msg[i] ) );
|
322
|
+
}
|
323
|
+
else
|
324
|
+
{
|
325
|
+
memset( buf, 'a', 1000 );
|
326
|
+
|
327
|
+
for( j = 0; j < 1000; j++ )
|
328
|
+
{
|
329
|
+
sha256_update( &ctx, (uint8 *) buf, 1000 );
|
330
|
+
}
|
331
|
+
}
|
332
|
+
|
333
|
+
sha256_finish( &ctx, sha256sum );
|
334
|
+
|
335
|
+
for( j = 0; j < 32; j++ )
|
336
|
+
{
|
337
|
+
sprintf( output + j * 2, "%02x", sha256sum[j] );
|
338
|
+
}
|
339
|
+
|
340
|
+
if( memcmp( output, val[i], 64 ) )
|
341
|
+
{
|
342
|
+
printf( "failed!\n" );
|
343
|
+
return( 1 );
|
344
|
+
}
|
345
|
+
|
346
|
+
printf( "passed.\n" );
|
347
|
+
}
|
348
|
+
|
349
|
+
printf( "\n" );
|
350
|
+
}
|
351
|
+
else
|
352
|
+
{
|
353
|
+
if( ! ( f = fopen( argv[1], "rb" ) ) )
|
354
|
+
{
|
355
|
+
perror( "fopen" );
|
356
|
+
return( 1 );
|
357
|
+
}
|
358
|
+
|
359
|
+
sha256_starts( &ctx );
|
360
|
+
|
361
|
+
while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
|
362
|
+
{
|
363
|
+
sha256_update( &ctx, buf, i );
|
364
|
+
}
|
365
|
+
|
366
|
+
sha256_finish( &ctx, sha256sum );
|
367
|
+
|
368
|
+
for( j = 0; j < 32; j++ )
|
369
|
+
{
|
370
|
+
printf( "%02x", sha256sum[j] );
|
371
|
+
}
|
372
|
+
|
373
|
+
printf( " %s\n", argv[1] );
|
374
|
+
}
|
375
|
+
|
376
|
+
return( 0 );
|
377
|
+
}
|
378
|
+
|
379
|
+
#endif
|