iodine 0.6.5 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of iodine might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +4 -4
- data/SPEC-Websocket-Draft.md +3 -6
- data/bin/mustache.rb +128 -0
- data/examples/test_template.mustache +16 -0
- data/ext/iodine/fio.c +9397 -0
- data/ext/iodine/fio.h +4723 -0
- data/ext/iodine/fio_ary.h +353 -54
- data/ext/iodine/fio_cli.c +351 -361
- data/ext/iodine/fio_cli.h +84 -105
- data/ext/iodine/fio_hashmap.h +70 -16
- data/ext/iodine/fio_json_parser.h +35 -24
- data/ext/iodine/fio_siphash.c +104 -4
- data/ext/iodine/fio_siphash.h +18 -2
- data/ext/iodine/fio_str.h +1218 -0
- data/ext/iodine/fio_tmpfile.h +1 -1
- data/ext/iodine/fiobj.h +13 -8
- data/ext/iodine/fiobj4sock.h +6 -8
- data/ext/iodine/fiobj_ary.c +107 -17
- data/ext/iodine/fiobj_ary.h +36 -4
- data/ext/iodine/fiobj_data.c +146 -127
- data/ext/iodine/fiobj_data.h +25 -23
- data/ext/iodine/fiobj_hash.c +7 -7
- data/ext/iodine/fiobj_hash.h +6 -5
- data/ext/iodine/fiobj_json.c +20 -17
- data/ext/iodine/fiobj_json.h +5 -5
- data/ext/iodine/fiobj_mem.h +71 -0
- data/ext/iodine/fiobj_mustache.c +310 -0
- data/ext/iodine/fiobj_mustache.h +40 -0
- data/ext/iodine/fiobj_numbers.c +199 -94
- data/ext/iodine/fiobj_numbers.h +7 -7
- data/ext/iodine/fiobj_str.c +142 -333
- data/ext/iodine/fiobj_str.h +65 -55
- data/ext/iodine/fiobject.c +49 -11
- data/ext/iodine/fiobject.h +40 -39
- data/ext/iodine/http.c +382 -190
- data/ext/iodine/http.h +124 -80
- data/ext/iodine/http1.c +99 -127
- data/ext/iodine/http1.h +5 -5
- data/ext/iodine/http1_parser.c +3 -2
- data/ext/iodine/http1_parser.h +2 -2
- data/ext/iodine/http_internal.c +14 -12
- data/ext/iodine/http_internal.h +25 -19
- data/ext/iodine/iodine.c +37 -18
- data/ext/iodine/iodine.h +4 -0
- data/ext/iodine/iodine_caller.c +9 -2
- data/ext/iodine/iodine_caller.h +2 -0
- data/ext/iodine/iodine_connection.c +82 -117
- data/ext/iodine/iodine_defer.c +57 -50
- data/ext/iodine/iodine_defer.h +0 -1
- data/ext/iodine/iodine_fiobj2rb.h +4 -2
- data/ext/iodine/iodine_helpers.c +4 -4
- data/ext/iodine/iodine_http.c +25 -32
- data/ext/iodine/iodine_json.c +2 -1
- data/ext/iodine/iodine_mustache.c +423 -0
- data/ext/iodine/iodine_mustache.h +6 -0
- data/ext/iodine/iodine_pubsub.c +48 -153
- data/ext/iodine/iodine_pubsub.h +5 -4
- data/ext/iodine/iodine_rack_io.c +7 -5
- data/ext/iodine/iodine_store.c +16 -13
- data/ext/iodine/iodine_tcp.c +26 -34
- data/ext/iodine/mustache_parser.h +1085 -0
- data/ext/iodine/redis_engine.c +740 -646
- data/ext/iodine/redis_engine.h +13 -15
- data/ext/iodine/resp_parser.h +11 -5
- data/ext/iodine/websocket_parser.h +13 -13
- data/ext/iodine/websockets.c +240 -393
- data/ext/iodine/websockets.h +52 -113
- data/lib/iodine.rb +1 -1
- data/lib/iodine/mustache.rb +140 -0
- data/lib/iodine/version.rb +1 -1
- metadata +15 -28
- data/ext/iodine/defer.c +0 -566
- data/ext/iodine/defer.h +0 -148
- data/ext/iodine/evio.c +0 -26
- data/ext/iodine/evio.h +0 -161
- data/ext/iodine/evio_callbacks.c +0 -26
- data/ext/iodine/evio_epoll.c +0 -251
- data/ext/iodine/evio_kqueue.c +0 -194
- data/ext/iodine/facil.c +0 -2325
- data/ext/iodine/facil.h +0 -616
- data/ext/iodine/fio_base64.c +0 -277
- data/ext/iodine/fio_base64.h +0 -71
- data/ext/iodine/fio_llist.h +0 -257
- data/ext/iodine/fio_mem.c +0 -675
- data/ext/iodine/fio_mem.h +0 -143
- data/ext/iodine/fio_random.c +0 -248
- data/ext/iodine/fio_random.h +0 -45
- data/ext/iodine/fio_sha1.c +0 -362
- data/ext/iodine/fio_sha1.h +0 -107
- data/ext/iodine/fio_sha2.c +0 -842
- data/ext/iodine/fio_sha2.h +0 -169
- data/ext/iodine/pubsub.c +0 -867
- data/ext/iodine/pubsub.h +0 -221
- data/ext/iodine/sock.c +0 -1366
- data/ext/iodine/sock.h +0 -566
- data/ext/iodine/spnlock.inc +0 -111
data/ext/iodine/fio_sha1.h
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
Copyright: Boaz segev, 2016-2017
|
3
|
-
License: MIT except for any non-public-domain algorithms (none that I'm aware
|
4
|
-
of), which might be subject to their own licenses.
|
5
|
-
|
6
|
-
Feel free to copy, use and enjoy in accordance with to the license(s).
|
7
|
-
*/
|
8
|
-
#ifndef H_FIO_SHA1_H
|
9
|
-
#define H_FIO_SHA1_H
|
10
|
-
|
11
|
-
#include <stdint.h>
|
12
|
-
#include <stdlib.h>
|
13
|
-
|
14
|
-
// clang-format off
|
15
|
-
#if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
|
16
|
-
# if defined(__has_include)
|
17
|
-
# if __has_include(<endian.h>)
|
18
|
-
# include <endian.h>
|
19
|
-
# elif __has_include(<sys/endian.h>)
|
20
|
-
# include <sys/endian.h>
|
21
|
-
# endif
|
22
|
-
# endif
|
23
|
-
# if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) && \
|
24
|
-
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
25
|
-
# define __BIG_ENDIAN__
|
26
|
-
# endif
|
27
|
-
#endif
|
28
|
-
|
29
|
-
#ifndef UNUSED_FUNC
|
30
|
-
# define UNUSED_FUNC __attribute__((unused))
|
31
|
-
#endif
|
32
|
-
// clang-format on
|
33
|
-
|
34
|
-
/* *****************************************************************************
|
35
|
-
C++ extern
|
36
|
-
*/
|
37
|
-
#if defined(__cplusplus)
|
38
|
-
extern "C" {
|
39
|
-
#endif
|
40
|
-
|
41
|
-
/* ***************************************************************************
|
42
|
-
SHA-1 hashing
|
43
|
-
*/
|
44
|
-
|
45
|
-
/**
|
46
|
-
SHA-1 hashing container - you should ignore the contents of this struct.
|
47
|
-
|
48
|
-
The `sha1_s` type will contain all the sha1 data required to perform the
|
49
|
-
hashing, managing it's encoding. If it's stack allocated, no freeing will be
|
50
|
-
required.
|
51
|
-
|
52
|
-
Use, for example:
|
53
|
-
|
54
|
-
sha1_s sha1;
|
55
|
-
fio_sha1_init(&sha1);
|
56
|
-
fio_sha1_write(&sha1,
|
57
|
-
"The quick brown fox jumps over the lazy dog", 43);
|
58
|
-
char *hashed_result = fio_sha1_result(&sha1);
|
59
|
-
*/
|
60
|
-
typedef struct {
|
61
|
-
uint64_t length;
|
62
|
-
uint8_t buffer[64];
|
63
|
-
union {
|
64
|
-
uint32_t i[5];
|
65
|
-
unsigned char str[21];
|
66
|
-
} digest;
|
67
|
-
} sha1_s;
|
68
|
-
|
69
|
-
/**
|
70
|
-
Initialize or reset the `sha1` object. This must be performed before hashing
|
71
|
-
data using sha1.
|
72
|
-
*/
|
73
|
-
sha1_s fio_sha1_init(void);
|
74
|
-
/**
|
75
|
-
Writes data to the sha1 buffer.
|
76
|
-
*/
|
77
|
-
void fio_sha1_write(sha1_s *s, const void *data, size_t len);
|
78
|
-
/**
|
79
|
-
Finalizes the SHA1 hash, returning the Hashed data.
|
80
|
-
|
81
|
-
`sha1_result` can be called for the same object multiple times, but the
|
82
|
-
finalization will only be performed the first time this function is called.
|
83
|
-
*/
|
84
|
-
char *fio_sha1_result(sha1_s *s);
|
85
|
-
|
86
|
-
/**
|
87
|
-
An SHA1 helper function that performs initialiation, writing and finalizing.
|
88
|
-
*/
|
89
|
-
static inline UNUSED_FUNC char *fio_sha1(sha1_s *s, const void *data,
|
90
|
-
size_t len) {
|
91
|
-
*s = fio_sha1_init();
|
92
|
-
fio_sha1_write(s, data, len);
|
93
|
-
return fio_sha1_result(s);
|
94
|
-
}
|
95
|
-
|
96
|
-
#if defined(DEBUG) && DEBUG == 1
|
97
|
-
void fio_sha1_test(void);
|
98
|
-
#endif
|
99
|
-
|
100
|
-
/* *****************************************************************************
|
101
|
-
C++ extern finish
|
102
|
-
*/
|
103
|
-
#if defined(__cplusplus)
|
104
|
-
}
|
105
|
-
#endif
|
106
|
-
|
107
|
-
#endif
|
data/ext/iodine/fio_sha2.c
DELETED
@@ -1,842 +0,0 @@
|
|
1
|
-
/*
|
2
|
-
Copyright: Boaz segev, 2016-2017
|
3
|
-
License: MIT except for any non-public-domain algorithms (none that I'm aware
|
4
|
-
of), which might be subject to their own licenses.
|
5
|
-
|
6
|
-
Feel free to copy, use and enjoy in accordance with to the license(s).
|
7
|
-
*/
|
8
|
-
#ifndef _GNU_SOURCE
|
9
|
-
#define _GNU_SOURCE
|
10
|
-
#endif
|
11
|
-
#include "fio_sha2.h"
|
12
|
-
|
13
|
-
#include <stdio.h>
|
14
|
-
#include <string.h>
|
15
|
-
|
16
|
-
#ifndef __has_include
|
17
|
-
#define __has_include(x) 0
|
18
|
-
#endif
|
19
|
-
|
20
|
-
/* include intrinsics if supported */
|
21
|
-
#if __has_include(<x86intrin.h>)
|
22
|
-
#include <x86intrin.h>
|
23
|
-
#define HAVE_X86Intrin
|
24
|
-
#endif
|
25
|
-
|
26
|
-
/*****************************************************************************
|
27
|
-
Useful Macros
|
28
|
-
*/
|
29
|
-
|
30
|
-
/** 32Bit left rotation, inlined. */
|
31
|
-
#define left_rotate32(i, bits) \
|
32
|
-
(((uint32_t)(i) << (bits)) | ((uint32_t)(i) >> (32 - (bits))))
|
33
|
-
/** 32Bit right rotation, inlined. */
|
34
|
-
#define right_rotate32(i, bits) \
|
35
|
-
(((uint32_t)(i) >> (bits)) | ((uint32_t)(i) << (32 - (bits))))
|
36
|
-
/** 64Bit left rotation, inlined. */
|
37
|
-
#define left_rotate64(i, bits) \
|
38
|
-
(((uint64_t)(i) << (bits)) | ((uint64_t)(i) >> (64 - (bits))))
|
39
|
-
/** 64Bit right rotation, inlined. */
|
40
|
-
#define right_rotate64(i, bits) \
|
41
|
-
(((uint64_t)(i) >> (bits)) | ((uint64_t)(i) << (64 - (bits))))
|
42
|
-
/** unknown size element - left rotation, inlined. */
|
43
|
-
#define left_rotate(i, bits) (((i) << (bits)) | ((i) >> (sizeof((i)) - (bits))))
|
44
|
-
/** unknown size element - right rotation, inlined. */
|
45
|
-
#define right_rotate(i, bits) \
|
46
|
-
(((i) >> (bits)) | ((i) << (sizeof((i)) - (bits))))
|
47
|
-
/** inplace byte swap 16 bit integer */
|
48
|
-
#define bswap16(i) \
|
49
|
-
do { \
|
50
|
-
(i) = (((i)&0xFFU) << 8) | (((i)&0xFF00U) >> 8); \
|
51
|
-
} while (0);
|
52
|
-
/** inplace byte swap 32 bit integer */
|
53
|
-
#define bswap32(i) \
|
54
|
-
do { \
|
55
|
-
(i) = (((i)&0xFFUL) << 24) | (((i)&0xFF00UL) << 8) | \
|
56
|
-
(((i)&0xFF0000UL) >> 8) | (((i)&0xFF000000UL) >> 24); \
|
57
|
-
} while (0);
|
58
|
-
/** inplace byte swap 64 bit integer */
|
59
|
-
#define bswap64(i) \
|
60
|
-
do { \
|
61
|
-
(i) = (((i)&0xFFULL) << 56) | (((i)&0xFF00ULL) << 40) | \
|
62
|
-
(((i)&0xFF0000ULL) << 24) | (((i)&0xFF000000ULL) << 8) | \
|
63
|
-
(((i)&0xFF00000000ULL) >> 8) | (((i)&0xFF0000000000ULL) >> 24) | \
|
64
|
-
(((i)&0xFF000000000000ULL) >> 40) | \
|
65
|
-
(((i)&0xFF00000000000000ULL) >> 56); \
|
66
|
-
} while (0);
|
67
|
-
#ifdef __SIZEOF_INT128__
|
68
|
-
/** get the byte swap value of a 128 bit ...??? */
|
69
|
-
#define gbswap128(c) \
|
70
|
-
(((*((__uint128_t *)(c))) & 0xFFULL) << 120) | \
|
71
|
-
(((*((__uint128_t *)(c))) & 0xFF00ULL) << 104) | \
|
72
|
-
(((*((__uint128_t *)(c))) & 0xFF0000ULL) << 88) | \
|
73
|
-
(((*((__uint128_t *)(c))) & 0xFF000000ULL) << 72) | \
|
74
|
-
(((*((__uint128_t *)(c))) & 0xFF00000000ULL) << 56) | \
|
75
|
-
(((*((__uint128_t *)(c))) & 0xFF0000000000ULL) << 40) | \
|
76
|
-
(((*((__uint128_t *)(c))) & 0xFF000000000000ULL) << 24) | \
|
77
|
-
(((*((__uint128_t *)(c))) & 0xFF00000000000000ULL) << 8) | \
|
78
|
-
(((*((__uint128_t *)(c))) & 0xFF0000000000000000ULL) >> 8) | \
|
79
|
-
(((*((__uint128_t *)(c))) & 0xFF000000000000000000ULL) >> 24) | \
|
80
|
-
(((*((__uint128_t *)(c))) & 0xFF00000000000000000000ULL) >> 40) | \
|
81
|
-
(((*((__uint128_t *)(c))) & 0xFF0000000000000000000000ULL) >> 56) | \
|
82
|
-
(((*((__uint128_t *)(c))) & 0xFF000000000000000000000000ULL) >> 72) | \
|
83
|
-
(((*((__uint128_t *)(c))) & 0xFF00000000000000000000000000ULL) >> 88) | \
|
84
|
-
(((*((__uint128_t *)(c))) & 0xFF0000000000000000000000000000ULL) >> 104)
|
85
|
-
#endif
|
86
|
-
|
87
|
-
#ifdef HAVE_X86Intrin
|
88
|
-
#undef bswap64
|
89
|
-
#define bswap64(i) \
|
90
|
-
{ __asm__("bswapq %0" : "+r"(i) :); }
|
91
|
-
|
92
|
-
// shadow sched_yield as _mm_pause for spinwait
|
93
|
-
#define sched_yield() _mm_pause()
|
94
|
-
#endif
|
95
|
-
|
96
|
-
/* ***************************************************************************
|
97
|
-
SHA-2 hashing
|
98
|
-
*/
|
99
|
-
|
100
|
-
static const uint8_t sha2_padding[128] = {0x80, 0};
|
101
|
-
|
102
|
-
/* SHA-224 and SHA-256 constants */
|
103
|
-
static uint32_t sha2_256_words[] = {
|
104
|
-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
|
105
|
-
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
106
|
-
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
|
107
|
-
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
108
|
-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
109
|
-
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
110
|
-
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
111
|
-
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
112
|
-
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
|
113
|
-
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
114
|
-
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
115
|
-
|
116
|
-
/* SHA-512 and friends constants */
|
117
|
-
static uint64_t sha2_512_words[] = {
|
118
|
-
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
|
119
|
-
0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,
|
120
|
-
0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,
|
121
|
-
0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
|
122
|
-
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
|
123
|
-
0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
|
124
|
-
0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,
|
125
|
-
0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
|
126
|
-
0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,
|
127
|
-
0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
|
128
|
-
0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,
|
129
|
-
0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
|
130
|
-
0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,
|
131
|
-
0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
|
132
|
-
0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
|
133
|
-
0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
|
134
|
-
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,
|
135
|
-
0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
|
136
|
-
0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,
|
137
|
-
0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
|
138
|
-
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,
|
139
|
-
0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,
|
140
|
-
0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,
|
141
|
-
0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
|
142
|
-
0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
|
143
|
-
0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
|
144
|
-
0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
|
145
|
-
|
146
|
-
/* Specific Macros for the SHA-2 processing */
|
147
|
-
|
148
|
-
#define Ch(x, y, z) (((x) & (y)) ^ ((~(x)) & z))
|
149
|
-
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
|
150
|
-
|
151
|
-
#define Eps0_32(x) \
|
152
|
-
(right_rotate32((x), 2) ^ right_rotate32((x), 13) ^ right_rotate32((x), 22))
|
153
|
-
#define Eps1_32(x) \
|
154
|
-
(right_rotate32((x), 6) ^ right_rotate32((x), 11) ^ right_rotate32((x), 25))
|
155
|
-
#define Omg0_32(x) \
|
156
|
-
(right_rotate32((x), 7) ^ right_rotate32((x), 18) ^ (((x) >> 3)))
|
157
|
-
#define Omg1_32(x) \
|
158
|
-
(right_rotate32((x), 17) ^ right_rotate32((x), 19) ^ (((x) >> 10)))
|
159
|
-
|
160
|
-
#define Eps0_64(x) \
|
161
|
-
(right_rotate64((x), 28) ^ right_rotate64((x), 34) ^ right_rotate64((x), 39))
|
162
|
-
#define Eps1_64(x) \
|
163
|
-
(right_rotate64((x), 14) ^ right_rotate64((x), 18) ^ right_rotate64((x), 41))
|
164
|
-
#define Omg0_64(x) \
|
165
|
-
(right_rotate64((x), 1) ^ right_rotate64((x), 8) ^ (((x) >> 7)))
|
166
|
-
#define Omg1_64(x) \
|
167
|
-
(right_rotate64((x), 19) ^ right_rotate64((x), 61) ^ (((x) >> 6)))
|
168
|
-
|
169
|
-
#ifdef __BIG_ENDIAN__
|
170
|
-
/** Converts a 4 byte string to a uint32_t word. careful with alignment! */
|
171
|
-
#define str2word32(c) (*((uint32_t *)(c)))
|
172
|
-
#define str2word64(c) (*((uint64_t *)(c)))
|
173
|
-
#else
|
174
|
-
/**
|
175
|
-
Converts a 4 byte string to a Big Endian uint32_t word. (ignores alignment!)
|
176
|
-
*/
|
177
|
-
#define str2word32(c) \
|
178
|
-
(((*((uint32_t *)(c))) & 0xFFUL) << 24) | \
|
179
|
-
(((*((uint32_t *)(c))) & 0xFF00UL) << 8) | \
|
180
|
-
(((*((uint32_t *)(c))) & 0xFF0000UL) >> 8) | \
|
181
|
-
(((*((uint32_t *)(c))) & 0xFF000000UL) >> 24)
|
182
|
-
#define str2word64(c) \
|
183
|
-
(((*((uint64_t *)(c))) & 0xFFULL) << 56) | \
|
184
|
-
(((*((uint64_t *)(c))) & 0xFF00ULL) << 40) | \
|
185
|
-
(((*((uint64_t *)(c))) & 0xFF0000ULL) << 24) | \
|
186
|
-
(((*((uint64_t *)(c))) & 0xFF000000ULL) << 8) | \
|
187
|
-
(((*((uint64_t *)(c))) & 0xFF00000000ULL) >> 8) | \
|
188
|
-
(((*((uint64_t *)(c))) & 0xFF0000000000ULL) >> 24) | \
|
189
|
-
(((*((uint64_t *)(c))) & 0xFF000000000000ULL) >> 40) | \
|
190
|
-
(((*((uint64_t *)(c))) & 0xFF00000000000000ULL) >> 56);
|
191
|
-
#endif
|
192
|
-
|
193
|
-
/**
|
194
|
-
Process the buffer once full.
|
195
|
-
*/
|
196
|
-
static inline void perform_all_rounds(sha2_s *s, const uint8_t *data) {
|
197
|
-
if (s->type & 1) { /* 512 derived type */
|
198
|
-
// process values for the 64bit words
|
199
|
-
uint64_t a = s->digest.i64[0];
|
200
|
-
uint64_t b = s->digest.i64[1];
|
201
|
-
uint64_t c = s->digest.i64[2];
|
202
|
-
uint64_t d = s->digest.i64[3];
|
203
|
-
uint64_t e = s->digest.i64[4];
|
204
|
-
uint64_t f = s->digest.i64[5];
|
205
|
-
uint64_t g = s->digest.i64[6];
|
206
|
-
uint64_t h = s->digest.i64[7];
|
207
|
-
uint64_t t1, t2, w[80];
|
208
|
-
w[0] = str2word64(data);
|
209
|
-
w[1] = str2word64(data + 8);
|
210
|
-
w[2] = str2word64(data + 16);
|
211
|
-
w[3] = str2word64(data + 24);
|
212
|
-
w[4] = str2word64(data + 32);
|
213
|
-
w[5] = str2word64(data + 40);
|
214
|
-
w[6] = str2word64(data + 48);
|
215
|
-
w[7] = str2word64(data + 56);
|
216
|
-
w[8] = str2word64(data + 64);
|
217
|
-
w[9] = str2word64(data + 72);
|
218
|
-
w[10] = str2word64(data + 80);
|
219
|
-
w[11] = str2word64(data + 88);
|
220
|
-
w[12] = str2word64(data + 96);
|
221
|
-
w[13] = str2word64(data + 104);
|
222
|
-
w[14] = str2word64(data + 112);
|
223
|
-
w[15] = str2word64(data + 120);
|
224
|
-
|
225
|
-
#define perform_single_round(i) \
|
226
|
-
t1 = h + Eps1_64(e) + Ch(e, f, g) + sha2_512_words[i] + w[i]; \
|
227
|
-
t2 = Eps0_64(a) + Maj(a, b, c); \
|
228
|
-
h = g; \
|
229
|
-
g = f; \
|
230
|
-
f = e; \
|
231
|
-
e = d + t1; \
|
232
|
-
d = c; \
|
233
|
-
c = b; \
|
234
|
-
b = a; \
|
235
|
-
a = t1 + t2;
|
236
|
-
|
237
|
-
#define perform_4rounds(i) \
|
238
|
-
perform_single_round(i); \
|
239
|
-
perform_single_round(i + 1); \
|
240
|
-
perform_single_round(i + 2); \
|
241
|
-
perform_single_round(i + 3);
|
242
|
-
|
243
|
-
perform_4rounds(0);
|
244
|
-
perform_4rounds(4);
|
245
|
-
perform_4rounds(8);
|
246
|
-
perform_4rounds(12);
|
247
|
-
|
248
|
-
#undef perform_single_round
|
249
|
-
#define perform_single_round(i) \
|
250
|
-
w[i] = Omg1_64(w[i - 2]) + w[i - 7] + Omg0_64(w[i - 15]) + w[i - 16]; \
|
251
|
-
t1 = h + Eps1_64(e) + Ch(e, f, g) + sha2_512_words[i] + w[i]; \
|
252
|
-
t2 = Eps0_64(a) + Maj(a, b, c); \
|
253
|
-
h = g; \
|
254
|
-
g = f; \
|
255
|
-
f = e; \
|
256
|
-
e = d + t1; \
|
257
|
-
d = c; \
|
258
|
-
c = b; \
|
259
|
-
b = a; \
|
260
|
-
a = t1 + t2;
|
261
|
-
|
262
|
-
perform_4rounds(16);
|
263
|
-
perform_4rounds(20);
|
264
|
-
perform_4rounds(24);
|
265
|
-
perform_4rounds(28);
|
266
|
-
perform_4rounds(32);
|
267
|
-
perform_4rounds(36);
|
268
|
-
perform_4rounds(40);
|
269
|
-
perform_4rounds(44);
|
270
|
-
perform_4rounds(48);
|
271
|
-
perform_4rounds(52);
|
272
|
-
perform_4rounds(56);
|
273
|
-
perform_4rounds(60);
|
274
|
-
perform_4rounds(64);
|
275
|
-
perform_4rounds(68);
|
276
|
-
perform_4rounds(72);
|
277
|
-
perform_4rounds(76);
|
278
|
-
|
279
|
-
s->digest.i64[0] += a;
|
280
|
-
s->digest.i64[1] += b;
|
281
|
-
s->digest.i64[2] += c;
|
282
|
-
s->digest.i64[3] += d;
|
283
|
-
s->digest.i64[4] += e;
|
284
|
-
s->digest.i64[5] += f;
|
285
|
-
s->digest.i64[6] += g;
|
286
|
-
s->digest.i64[7] += h;
|
287
|
-
return;
|
288
|
-
} else {
|
289
|
-
// process values for the 32bit words
|
290
|
-
uint32_t a = s->digest.i32[0];
|
291
|
-
uint32_t b = s->digest.i32[1];
|
292
|
-
uint32_t c = s->digest.i32[2];
|
293
|
-
uint32_t d = s->digest.i32[3];
|
294
|
-
uint32_t e = s->digest.i32[4];
|
295
|
-
uint32_t f = s->digest.i32[5];
|
296
|
-
uint32_t g = s->digest.i32[6];
|
297
|
-
uint32_t h = s->digest.i32[7];
|
298
|
-
uint32_t t1, t2, w[64];
|
299
|
-
|
300
|
-
w[0] = str2word32(data);
|
301
|
-
w[1] = str2word32(data + 4);
|
302
|
-
w[2] = str2word32(data + 8);
|
303
|
-
w[3] = str2word32(data + 12);
|
304
|
-
w[4] = str2word32(data + 16);
|
305
|
-
w[5] = str2word32(data + 20);
|
306
|
-
w[6] = str2word32(data + 24);
|
307
|
-
w[7] = str2word32(data + 28);
|
308
|
-
w[8] = str2word32(data + 32);
|
309
|
-
w[9] = str2word32(data + 36);
|
310
|
-
w[10] = str2word32(data + 40);
|
311
|
-
w[11] = str2word32(data + 44);
|
312
|
-
w[12] = str2word32(data + 48);
|
313
|
-
w[13] = str2word32(data + 52);
|
314
|
-
w[14] = str2word32(data + 56);
|
315
|
-
w[15] = str2word32(data + 60);
|
316
|
-
|
317
|
-
#undef perform_single_round
|
318
|
-
#define perform_single_round(i) \
|
319
|
-
t1 = h + Eps1_32(e) + Ch(e, f, g) + sha2_256_words[i] + w[i]; \
|
320
|
-
t2 = Eps0_32(a) + Maj(a, b, c); \
|
321
|
-
h = g; \
|
322
|
-
g = f; \
|
323
|
-
f = e; \
|
324
|
-
e = d + t1; \
|
325
|
-
d = c; \
|
326
|
-
c = b; \
|
327
|
-
b = a; \
|
328
|
-
a = t1 + t2;
|
329
|
-
|
330
|
-
perform_4rounds(0);
|
331
|
-
perform_4rounds(4);
|
332
|
-
perform_4rounds(8);
|
333
|
-
perform_4rounds(12);
|
334
|
-
|
335
|
-
#undef perform_single_round
|
336
|
-
#define perform_single_round(i) \
|
337
|
-
w[i] = Omg1_32(w[i - 2]) + w[i - 7] + Omg0_32(w[i - 15]) + w[i - 16]; \
|
338
|
-
t1 = h + Eps1_32(e) + Ch(e, f, g) + sha2_256_words[i] + w[i]; \
|
339
|
-
t2 = Eps0_32(a) + Maj(a, b, c); \
|
340
|
-
h = g; \
|
341
|
-
g = f; \
|
342
|
-
f = e; \
|
343
|
-
e = d + t1; \
|
344
|
-
d = c; \
|
345
|
-
c = b; \
|
346
|
-
b = a; \
|
347
|
-
a = t1 + t2;
|
348
|
-
|
349
|
-
perform_4rounds(16);
|
350
|
-
perform_4rounds(20);
|
351
|
-
perform_4rounds(24);
|
352
|
-
perform_4rounds(28);
|
353
|
-
perform_4rounds(32);
|
354
|
-
perform_4rounds(36);
|
355
|
-
perform_4rounds(40);
|
356
|
-
perform_4rounds(44);
|
357
|
-
perform_4rounds(48);
|
358
|
-
perform_4rounds(52);
|
359
|
-
perform_4rounds(56);
|
360
|
-
perform_4rounds(60);
|
361
|
-
|
362
|
-
s->digest.i32[0] += a;
|
363
|
-
s->digest.i32[1] += b;
|
364
|
-
s->digest.i32[2] += c;
|
365
|
-
s->digest.i32[3] += d;
|
366
|
-
s->digest.i32[4] += e;
|
367
|
-
s->digest.i32[5] += f;
|
368
|
-
s->digest.i32[6] += g;
|
369
|
-
s->digest.i32[7] += h;
|
370
|
-
}
|
371
|
-
}
|
372
|
-
|
373
|
-
/*****************************************************************************
|
374
|
-
API
|
375
|
-
*/
|
376
|
-
|
377
|
-
/**
|
378
|
-
Initialize/reset the SHA-2 object.
|
379
|
-
|
380
|
-
SHA-2 is actually a family of functions with different variants. When
|
381
|
-
initializing the SHA-2 container, you must select the variant you intend to
|
382
|
-
apply. The following are valid options (see the sha2_variant enum):
|
383
|
-
|
384
|
-
- SHA_512 (== 0)
|
385
|
-
- SHA_384
|
386
|
-
- SHA_512_224
|
387
|
-
- SHA_512_256
|
388
|
-
- SHA_256
|
389
|
-
- SHA_224
|
390
|
-
|
391
|
-
*/
|
392
|
-
sha2_s fio_sha2_init(sha2_variant variant) {
|
393
|
-
if (variant == SHA_256) {
|
394
|
-
return (sha2_s){
|
395
|
-
.type = SHA_256,
|
396
|
-
.digest.i32[0] = 0x6a09e667,
|
397
|
-
.digest.i32[1] = 0xbb67ae85,
|
398
|
-
.digest.i32[2] = 0x3c6ef372,
|
399
|
-
.digest.i32[3] = 0xa54ff53a,
|
400
|
-
.digest.i32[4] = 0x510e527f,
|
401
|
-
.digest.i32[5] = 0x9b05688c,
|
402
|
-
.digest.i32[6] = 0x1f83d9ab,
|
403
|
-
.digest.i32[7] = 0x5be0cd19,
|
404
|
-
};
|
405
|
-
} else if (variant == SHA_384) {
|
406
|
-
return (sha2_s){
|
407
|
-
.type = SHA_384,
|
408
|
-
.digest.i64[0] = 0xcbbb9d5dc1059ed8,
|
409
|
-
.digest.i64[1] = 0x629a292a367cd507,
|
410
|
-
.digest.i64[2] = 0x9159015a3070dd17,
|
411
|
-
.digest.i64[3] = 0x152fecd8f70e5939,
|
412
|
-
.digest.i64[4] = 0x67332667ffc00b31,
|
413
|
-
.digest.i64[5] = 0x8eb44a8768581511,
|
414
|
-
.digest.i64[6] = 0xdb0c2e0d64f98fa7,
|
415
|
-
.digest.i64[7] = 0x47b5481dbefa4fa4,
|
416
|
-
};
|
417
|
-
} else if (variant == SHA_512) {
|
418
|
-
return (sha2_s){
|
419
|
-
.type = SHA_512,
|
420
|
-
.digest.i64[0] = 0x6a09e667f3bcc908,
|
421
|
-
.digest.i64[1] = 0xbb67ae8584caa73b,
|
422
|
-
.digest.i64[2] = 0x3c6ef372fe94f82b,
|
423
|
-
.digest.i64[3] = 0xa54ff53a5f1d36f1,
|
424
|
-
.digest.i64[4] = 0x510e527fade682d1,
|
425
|
-
.digest.i64[5] = 0x9b05688c2b3e6c1f,
|
426
|
-
.digest.i64[6] = 0x1f83d9abfb41bd6b,
|
427
|
-
.digest.i64[7] = 0x5be0cd19137e2179,
|
428
|
-
};
|
429
|
-
} else if (variant == SHA_224) {
|
430
|
-
return (sha2_s){
|
431
|
-
.type = SHA_224,
|
432
|
-
.digest.i32[0] = 0xc1059ed8,
|
433
|
-
.digest.i32[1] = 0x367cd507,
|
434
|
-
.digest.i32[2] = 0x3070dd17,
|
435
|
-
.digest.i32[3] = 0xf70e5939,
|
436
|
-
.digest.i32[4] = 0xffc00b31,
|
437
|
-
.digest.i32[5] = 0x68581511,
|
438
|
-
.digest.i32[6] = 0x64f98fa7,
|
439
|
-
.digest.i32[7] = 0xbefa4fa4,
|
440
|
-
};
|
441
|
-
} else if (variant == SHA_512_224) {
|
442
|
-
return (sha2_s){
|
443
|
-
.type = SHA_512_224,
|
444
|
-
.digest.i64[0] = 0x8c3d37c819544da2,
|
445
|
-
.digest.i64[1] = 0x73e1996689dcd4d6,
|
446
|
-
.digest.i64[2] = 0x1dfab7ae32ff9c82,
|
447
|
-
.digest.i64[3] = 0x679dd514582f9fcf,
|
448
|
-
.digest.i64[4] = 0x0f6d2b697bd44da8,
|
449
|
-
.digest.i64[5] = 0x77e36f7304c48942,
|
450
|
-
.digest.i64[6] = 0x3f9d85a86a1d36c8,
|
451
|
-
.digest.i64[7] = 0x1112e6ad91d692a1,
|
452
|
-
};
|
453
|
-
} else if (variant == SHA_512_256) {
|
454
|
-
return (sha2_s){
|
455
|
-
.type = SHA_512_256,
|
456
|
-
.digest.i64[0] = 0x22312194fc2bf72c,
|
457
|
-
.digest.i64[1] = 0x9f555fa3c84c64c2,
|
458
|
-
.digest.i64[2] = 0x2393b86b6f53b151,
|
459
|
-
.digest.i64[3] = 0x963877195940eabd,
|
460
|
-
.digest.i64[4] = 0x96283ee2a88effe3,
|
461
|
-
.digest.i64[5] = 0xbe5e1e2553863992,
|
462
|
-
.digest.i64[6] = 0x2b0199fc2c85b8aa,
|
463
|
-
.digest.i64[7] = 0x0eb72ddc81c52ca2,
|
464
|
-
};
|
465
|
-
}
|
466
|
-
fprintf(stderr, "SHA2 ERROR - variant unknown\n");
|
467
|
-
exit(2);
|
468
|
-
}
|
469
|
-
|
470
|
-
/**
|
471
|
-
Writes data to the SHA-2 buffer.
|
472
|
-
*/
|
473
|
-
void fio_sha2_write(sha2_s *s, const void *data, size_t len) {
|
474
|
-
size_t in_buffer;
|
475
|
-
size_t partial;
|
476
|
-
if (s->type & 1) { /* 512 type derived */
|
477
|
-
#if defined(HAS_UINT128)
|
478
|
-
in_buffer = s->length.i & 127;
|
479
|
-
s->length.i += len;
|
480
|
-
#else
|
481
|
-
in_buffer = s->length.words[0] & 127;
|
482
|
-
if (s->length.words[0] + len < s->length.words[0]) {
|
483
|
-
/* we are at wraping around the 64bit limit */
|
484
|
-
s->length.words[1] = (s->length.words[1] << 1) | 1;
|
485
|
-
}
|
486
|
-
s->length.words[0] += len;
|
487
|
-
#endif
|
488
|
-
partial = 128 - in_buffer;
|
489
|
-
|
490
|
-
if (partial > len) {
|
491
|
-
memcpy(s->buffer + in_buffer, data, len);
|
492
|
-
return;
|
493
|
-
}
|
494
|
-
if (in_buffer) {
|
495
|
-
memcpy(s->buffer + in_buffer, data, partial);
|
496
|
-
len -= partial;
|
497
|
-
data = (void *)((uintptr_t)data + partial);
|
498
|
-
perform_all_rounds(s, s->buffer);
|
499
|
-
}
|
500
|
-
while (len >= 128) {
|
501
|
-
perform_all_rounds(s, data);
|
502
|
-
data = (void *)((uintptr_t)data + 128);
|
503
|
-
len -= 128;
|
504
|
-
}
|
505
|
-
if (len) {
|
506
|
-
memcpy(s->buffer + in_buffer, data, len);
|
507
|
-
}
|
508
|
-
return;
|
509
|
-
}
|
510
|
-
/* else... NOT 512 bits derived (64bit base) */
|
511
|
-
|
512
|
-
in_buffer = s->length.words[0] & 63;
|
513
|
-
partial = 64 - in_buffer;
|
514
|
-
|
515
|
-
s->length.words[0] += len;
|
516
|
-
|
517
|
-
if (partial > len) {
|
518
|
-
memcpy(s->buffer + in_buffer, data, len);
|
519
|
-
return;
|
520
|
-
}
|
521
|
-
if (in_buffer) {
|
522
|
-
memcpy(s->buffer + in_buffer, data, partial);
|
523
|
-
len -= partial;
|
524
|
-
data = (void *)((uintptr_t)data + partial);
|
525
|
-
perform_all_rounds(s, s->buffer);
|
526
|
-
}
|
527
|
-
while (len >= 64) {
|
528
|
-
perform_all_rounds(s, data);
|
529
|
-
data = (void *)((uintptr_t)data + 64);
|
530
|
-
len -= 64;
|
531
|
-
}
|
532
|
-
if (len) {
|
533
|
-
memcpy(s->buffer + in_buffer, data, len);
|
534
|
-
}
|
535
|
-
return;
|
536
|
-
}
|
537
|
-
|
538
|
-
/**
|
539
|
-
Finalizes the SHA-2 hash, returning the Hashed data.
|
540
|
-
|
541
|
-
`sha2_result` can be called for the same object multiple times, but the
|
542
|
-
finalization will only be performed the first time this function is called.
|
543
|
-
*/
|
544
|
-
char *fio_sha2_result(sha2_s *s) {
|
545
|
-
if (s->type & 1) {
|
546
|
-
/* 512 bits derived hashing */
|
547
|
-
|
548
|
-
#if defined(HAS_UINT128)
|
549
|
-
size_t in_buffer = s->length.i & 127;
|
550
|
-
#else
|
551
|
-
size_t in_buffer = s->length.words[0] & 127;
|
552
|
-
#endif
|
553
|
-
|
554
|
-
if (in_buffer > 111) {
|
555
|
-
memcpy(s->buffer + in_buffer, sha2_padding, 128 - in_buffer);
|
556
|
-
perform_all_rounds(s, s->buffer);
|
557
|
-
memcpy(s->buffer, sha2_padding + 1, 112);
|
558
|
-
} else if (in_buffer != 111) {
|
559
|
-
memcpy(s->buffer + in_buffer, sha2_padding, 112 - in_buffer);
|
560
|
-
} else {
|
561
|
-
s->buffer[111] = sha2_padding[0];
|
562
|
-
}
|
563
|
-
/* store the length in BITS - alignment should be promised by struct */
|
564
|
-
/* this must the number in BITS, encoded as a BIG ENDIAN 64 bit number */
|
565
|
-
|
566
|
-
#if defined(HAS_UINT128)
|
567
|
-
s->length.i = s->length.i << 3;
|
568
|
-
#else
|
569
|
-
s->length.words[1] = (s->length.words[1] << 3) | (s->length.words[0] >> 61);
|
570
|
-
s->length.words[0] = s->length.words[0] << 3;
|
571
|
-
#endif
|
572
|
-
|
573
|
-
#ifndef __BIG_ENDIAN__
|
574
|
-
bswap64(s->length.words[0]);
|
575
|
-
bswap64(s->length.words[1]);
|
576
|
-
{
|
577
|
-
uint_fast64_t tmp = s->length.words[0];
|
578
|
-
s->length.words[0] = s->length.words[1];
|
579
|
-
s->length.words[1] = tmp;
|
580
|
-
}
|
581
|
-
#endif
|
582
|
-
|
583
|
-
#if defined(HAS_UINT128)
|
584
|
-
__uint128_t *len = (__uint128_t *)(s->buffer + 112);
|
585
|
-
*len = s->length.i;
|
586
|
-
#else
|
587
|
-
uint64_t *len = (uint64_t *)(s->buffer + 112);
|
588
|
-
len[0] = s->length.words[0];
|
589
|
-
len[1] = s->length.words[1];
|
590
|
-
#endif
|
591
|
-
perform_all_rounds(s, s->buffer);
|
592
|
-
|
593
|
-
/* change back to little endian, if required */
|
594
|
-
#ifndef __BIG_ENDIAN__
|
595
|
-
bswap64(s->digest.i64[0]);
|
596
|
-
bswap64(s->digest.i64[1]);
|
597
|
-
bswap64(s->digest.i64[2]);
|
598
|
-
bswap64(s->digest.i64[3]);
|
599
|
-
bswap64(s->digest.i64[4]);
|
600
|
-
bswap64(s->digest.i64[5]);
|
601
|
-
bswap64(s->digest.i64[6]);
|
602
|
-
bswap64(s->digest.i64[7]);
|
603
|
-
#endif
|
604
|
-
// set NULL bytes for SHA_224
|
605
|
-
if (s->type == SHA_512_224)
|
606
|
-
s->digest.str[28] = 0;
|
607
|
-
// set NULL bytes for SHA_256
|
608
|
-
else if (s->type == SHA_512_256)
|
609
|
-
s->digest.str[32] = 0;
|
610
|
-
// set NULL bytes for SHA_384
|
611
|
-
else if (s->type == SHA_384)
|
612
|
-
s->digest.str[48] = 0;
|
613
|
-
s->digest.str[64] = 0; /* sometimes the optimizer messes the NUL sequence */
|
614
|
-
// fprintf(stderr, "result requested, in hex, is:");
|
615
|
-
// for (size_t i = 0; i < 20; i++)
|
616
|
-
// fprintf(stderr, "%02x", (unsigned int)(s->digest.str[i] & 0xFF));
|
617
|
-
// fprintf(stderr, "\r\n");
|
618
|
-
return (char *)s->digest.str;
|
619
|
-
}
|
620
|
-
|
621
|
-
size_t in_buffer = s->length.words[0] & 63;
|
622
|
-
if (in_buffer > 55) {
|
623
|
-
memcpy(s->buffer + in_buffer, sha2_padding, 64 - in_buffer);
|
624
|
-
perform_all_rounds(s, s->buffer);
|
625
|
-
memcpy(s->buffer, sha2_padding + 1, 56);
|
626
|
-
} else if (in_buffer != 55) {
|
627
|
-
memcpy(s->buffer + in_buffer, sha2_padding, 56 - in_buffer);
|
628
|
-
} else {
|
629
|
-
s->buffer[55] = sha2_padding[0];
|
630
|
-
}
|
631
|
-
/* store the length in BITS - alignment should be promised by struct */
|
632
|
-
/* this must the number in BITS, encoded as a BIG ENDIAN 64 bit number */
|
633
|
-
uint64_t *len = (uint64_t *)(s->buffer + 56);
|
634
|
-
*len = s->length.words[0] << 3;
|
635
|
-
#ifndef __BIG_ENDIAN__
|
636
|
-
bswap64(*len);
|
637
|
-
#endif
|
638
|
-
perform_all_rounds(s, s->buffer);
|
639
|
-
|
640
|
-
/* change back to little endian, if required */
|
641
|
-
#ifndef __BIG_ENDIAN__
|
642
|
-
|
643
|
-
bswap32(s->digest.i32[0]);
|
644
|
-
bswap32(s->digest.i32[1]);
|
645
|
-
bswap32(s->digest.i32[2]);
|
646
|
-
bswap32(s->digest.i32[3]);
|
647
|
-
bswap32(s->digest.i32[4]);
|
648
|
-
bswap32(s->digest.i32[5]);
|
649
|
-
bswap32(s->digest.i32[6]);
|
650
|
-
bswap32(s->digest.i32[7]);
|
651
|
-
|
652
|
-
#endif
|
653
|
-
// set NULL bytes for SHA_224
|
654
|
-
if (s->type == SHA_224)
|
655
|
-
s->digest.str[28] = 0;
|
656
|
-
// fprintf(stderr, "SHA-2 result requested, in hex, is:");
|
657
|
-
// for (size_t i = 0; i < (s->type_512 ? 64 : 32); i++)
|
658
|
-
// fprintf(stderr, "%02x", (unsigned int)(s->digest.str[i] & 0xFF));
|
659
|
-
// fprintf(stderr, "\r\n");
|
660
|
-
return (char *)s->digest.str;
|
661
|
-
}
|
662
|
-
|
663
|
-
/*******************************************************************************
|
664
|
-
SHA-2 testing
|
665
|
-
*/
|
666
|
-
#if defined(DEBUG) && DEBUG == 1
|
667
|
-
#include <time.h>
|
668
|
-
|
669
|
-
// SHA_512 = 1, SHA_512_256 = 3, SHA_512_224 = 5, SHA_384 = 7, SHA_256 = 2,
|
670
|
-
// SHA_224 = 4,
|
671
|
-
|
672
|
-
static char *sha2_variant_names[] = {
|
673
|
-
"unknown", "SHA_512", "SHA_256", "SHA_512_256",
|
674
|
-
"SHA_224", "SHA_512_224", "none", "SHA_384",
|
675
|
-
};
|
676
|
-
|
677
|
-
// clang-format off
|
678
|
-
#if defined(HAVE_OPENSSL)
|
679
|
-
# include <openssl/sha.h>
|
680
|
-
#endif
|
681
|
-
// clang-format on
|
682
|
-
|
683
|
-
void fio_sha2_test(void) {
|
684
|
-
sha2_s s;
|
685
|
-
char *expect;
|
686
|
-
char *got;
|
687
|
-
char *str = "";
|
688
|
-
fprintf(stderr, "===================================\n");
|
689
|
-
fprintf(stderr, "fio SHA-2 struct size: %zu\n", sizeof(sha2_s));
|
690
|
-
fprintf(stderr, "+ fio");
|
691
|
-
// start tests
|
692
|
-
s = fio_sha2_init(SHA_224);
|
693
|
-
fio_sha2_write(&s, str, 0);
|
694
|
-
expect = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34\xc4"
|
695
|
-
"\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
|
696
|
-
got = fio_sha2_result(&s);
|
697
|
-
if (strcmp(expect, got))
|
698
|
-
goto error;
|
699
|
-
|
700
|
-
s = fio_sha2_init(SHA_256);
|
701
|
-
fio_sha2_write(&s, str, 0);
|
702
|
-
expect =
|
703
|
-
"\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24\x27"
|
704
|
-
"\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55";
|
705
|
-
got = fio_sha2_result(&s);
|
706
|
-
if (strcmp(expect, got))
|
707
|
-
goto error;
|
708
|
-
|
709
|
-
s = fio_sha2_init(SHA_384);
|
710
|
-
fio_sha2_write(&s, str, 0);
|
711
|
-
expect = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e"
|
712
|
-
"\xb1\xb1\xe3\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c"
|
713
|
-
"\xc7\xbf\x63\xf6\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb"
|
714
|
-
"\xd5\x1a\xd2\xf1\x48\x98\xb9\x5b";
|
715
|
-
got = fio_sha2_result(&s);
|
716
|
-
if (strcmp(expect, got))
|
717
|
-
goto error;
|
718
|
-
|
719
|
-
s = fio_sha2_init(SHA_512);
|
720
|
-
fio_sha2_write(&s, str, 0);
|
721
|
-
expect = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d"
|
722
|
-
"\x80\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21"
|
723
|
-
"\xd3\x6c\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83"
|
724
|
-
"\x18\xd2\x87\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81"
|
725
|
-
"\xa5\x38\x32\x7a\xf9\x27\xda\x3e";
|
726
|
-
got = fio_sha2_result(&s);
|
727
|
-
if (strcmp(expect, got))
|
728
|
-
goto error;
|
729
|
-
|
730
|
-
s = fio_sha2_init(SHA_512_224);
|
731
|
-
fio_sha2_write(&s, str, 0);
|
732
|
-
expect = "\x6e\xd0\xdd\x02\x80\x6f\xa8\x9e\x25\xde\x06\x0c\x19\xd3"
|
733
|
-
"\xac\x86\xca\xbb\x87\xd6\xa0\xdd\xd0\x5c\x33\x3b\x84\xf4";
|
734
|
-
got = fio_sha2_result(&s);
|
735
|
-
if (strcmp(expect, got))
|
736
|
-
goto error;
|
737
|
-
|
738
|
-
s = fio_sha2_init(SHA_512_256);
|
739
|
-
fio_sha2_write(&s, str, 0);
|
740
|
-
expect = "\xc6\x72\xb8\xd1\xef\x56\xed\x28\xab\x87\xc3\x62\x2c\x51\x14\x06"
|
741
|
-
"\x9b\xdd\x3a\xd7\xb8\xf9\x73\x74\x98\xd0\xc0\x1e\xce\xf0\x96\x7a";
|
742
|
-
got = fio_sha2_result(&s);
|
743
|
-
if (strcmp(expect, got))
|
744
|
-
goto error;
|
745
|
-
|
746
|
-
s = fio_sha2_init(SHA_512);
|
747
|
-
str = "god is a rotten tomato";
|
748
|
-
fio_sha2_write(&s, str, strlen(str));
|
749
|
-
expect = "\x61\x97\x4d\x41\x9f\x77\x45\x21\x09\x4e\x95\xa3\xcb\x4d\xe4\x79"
|
750
|
-
"\x26\x32\x2f\x2b\xe2\x62\x64\x5a\xb4\x5d\x3f\x73\x69\xef\x46\x20"
|
751
|
-
"\xb2\xd3\xce\xda\xa9\xc2\x2c\xac\xe3\xf9\x02\xb2\x20\x5d\x2e\xfd"
|
752
|
-
"\x40\xca\xa0\xc1\x67\xe0\xdc\xdf\x60\x04\x3e\x4e\x76\x87\x82\x74";
|
753
|
-
got = fio_sha2_result(&s);
|
754
|
-
if (strcmp(expect, got))
|
755
|
-
goto error;
|
756
|
-
|
757
|
-
// s = fio_sha2_init(SHA_256);
|
758
|
-
// str = "The quick brown fox jumps over the lazy dog";
|
759
|
-
// fio_sha2_write(&s, str, strlen(str));
|
760
|
-
// expect =
|
761
|
-
// "\xd7\xa8\xfb\xb3\x07\xd7\x80\x94\x69\xca\x9a\xbc\xb0\x08\x2e\x4f"
|
762
|
-
// "\x8d\x56\x51\xe4\x6d\x3c\xdb\x76\x2d\x02\xd0\xbf\x37\xc9\xe5\x92";
|
763
|
-
// got = fio_sha2_result(&s);
|
764
|
-
// if (strcmp(expect, got))
|
765
|
-
// goto error;
|
766
|
-
|
767
|
-
s = fio_sha2_init(SHA_224);
|
768
|
-
str = "The quick brown fox jumps over the lazy dog";
|
769
|
-
fio_sha2_write(&s, str, strlen(str));
|
770
|
-
expect = "\x73\x0e\x10\x9b\xd7\xa8\xa3\x2b\x1c\xb9\xd9\xa0\x9a\xa2"
|
771
|
-
"\x32\x5d\x24\x30\x58\x7d\xdb\xc0\xc3\x8b\xad\x91\x15\x25";
|
772
|
-
got = fio_sha2_result(&s);
|
773
|
-
if (strcmp(expect, got))
|
774
|
-
goto error;
|
775
|
-
|
776
|
-
fprintf(stderr, " SHA-2 passed.\n");
|
777
|
-
|
778
|
-
#ifdef HAVE_OPENSSL
|
779
|
-
fprintf(stderr, "===================================\n");
|
780
|
-
fprintf(stderr, "fio SHA-2 struct size: %zu\n", sizeof(sha2_s));
|
781
|
-
fprintf(stderr, "OpenSSL SHA-2/256 struct size: %zu\n", sizeof(SHA256_CTX));
|
782
|
-
fprintf(stderr, "OpenSSL SHA-2/512 struct size: %zu\n", sizeof(SHA512_CTX));
|
783
|
-
fprintf(stderr, "===================================\n");
|
784
|
-
SHA512_CTX s2;
|
785
|
-
SHA256_CTX s3;
|
786
|
-
unsigned char hash[SHA512_DIGEST_LENGTH + 1];
|
787
|
-
hash[SHA512_DIGEST_LENGTH] = 0;
|
788
|
-
clock_t start = clock();
|
789
|
-
for (size_t i = 0; i < 100000; i++) {
|
790
|
-
s = fio_sha2_init(SHA_512);
|
791
|
-
fio_sha2_write(&s, "The quick brown fox jumps over the lazy dog", 43);
|
792
|
-
fio_sha2_result(&s);
|
793
|
-
}
|
794
|
-
fprintf(stderr, "fio 100K SHA-2/512: %lf\n",
|
795
|
-
(double)(clock() - start) / CLOCKS_PER_SEC);
|
796
|
-
|
797
|
-
start = clock();
|
798
|
-
for (size_t i = 0; i < 100000; i++) {
|
799
|
-
SHA512_Init(&s2);
|
800
|
-
SHA512_Update(&s2, "The quick brown fox jumps over the lazy dog", 43);
|
801
|
-
SHA512_Final(hash, &s2);
|
802
|
-
}
|
803
|
-
fprintf(stderr, "OpenSSL 100K SHA-2/512: %lf\n",
|
804
|
-
(double)(clock() - start) / CLOCKS_PER_SEC);
|
805
|
-
|
806
|
-
start = clock();
|
807
|
-
for (size_t i = 0; i < 100000; i++) {
|
808
|
-
s = fio_sha2_init(SHA_256);
|
809
|
-
fio_sha2_write(&s, "The quick brown fox jumps over the lazy dog", 43);
|
810
|
-
fio_sha2_result(&s);
|
811
|
-
}
|
812
|
-
fprintf(stderr, "fio 100K SHA-2/256: %lf\n",
|
813
|
-
(double)(clock() - start) / CLOCKS_PER_SEC);
|
814
|
-
|
815
|
-
hash[SHA256_DIGEST_LENGTH] = 0;
|
816
|
-
start = clock();
|
817
|
-
for (size_t i = 0; i < 100000; i++) {
|
818
|
-
SHA256_Init(&s3);
|
819
|
-
SHA256_Update(&s3, "The quick brown fox jumps over the lazy dog", 43);
|
820
|
-
SHA256_Final(hash, &s3);
|
821
|
-
}
|
822
|
-
fprintf(stderr, "OpenSSL 100K SHA-2/256: %lf\n",
|
823
|
-
(double)(clock() - start) / CLOCKS_PER_SEC);
|
824
|
-
|
825
|
-
fprintf(stderr, "===================================\n");
|
826
|
-
#endif
|
827
|
-
|
828
|
-
return;
|
829
|
-
|
830
|
-
error:
|
831
|
-
fprintf(stderr,
|
832
|
-
":\n--- fio SHA-2 Test FAILED!\ntype: "
|
833
|
-
"%s (%d)\nstring %s\nexpected:\n",
|
834
|
-
sha2_variant_names[s.type], s.type, str);
|
835
|
-
while (*expect)
|
836
|
-
fprintf(stderr, "%02x", *(expect++) & 0xFF);
|
837
|
-
fprintf(stderr, "\ngot:\n");
|
838
|
-
while (*got)
|
839
|
-
fprintf(stderr, "%02x", *(got++) & 0xFF);
|
840
|
-
fprintf(stderr, "\n");
|
841
|
-
}
|
842
|
-
#endif
|