#include "xyssl/config.h"#include "xyssl/sha4.h"#include <string.h>#include <stdio.h>Go to the source code of this file.
Defines | |
| #define | GET_UINT64_BE(n, b, i) |
| #define | PUT_UINT64_BE(n, b, i) |
| #define | SHR(x, n) (x >> n) |
| #define | ROTR(x, n) (SHR(x,n) | (x << (64 - n))) |
| #define | S0(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7)) |
| #define | S1(x) (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6)) |
| #define | S2(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39)) |
| #define | S3(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41)) |
| #define | F0(x, y, z) ((x & y) | (z & (x | y))) |
| #define | F1(x, y, z) (z ^ (x & (y ^ z))) |
| #define | P(a, b, c, d, e, f, g, h, x, K) |
Functions | |
| void | sha4_starts (sha4_context *ctx, int is384) |
| SHA-512 context setup. | |
| static void | sha4_process (sha4_context *ctx, unsigned char data[128]) |
| void | sha4_update (sha4_context *ctx, unsigned char *input, int ilen) |
| SHA-512 process buffer. | |
| void | sha4_finish (sha4_context *ctx, unsigned char output[64]) |
| SHA-512 final digest. | |
| void | sha4 (unsigned char *input, int ilen, unsigned char output[64], int is384) |
| Output = SHA-512( input buffer ). | |
| int | sha4_file (char *path, unsigned char output[64], int is384) |
| Output = SHA-512( file contents ). | |
| void | sha4_hmac_starts (sha4_context *ctx, unsigned char *key, int keylen, int is384) |
| SHA-512 HMAC context setup. | |
| void | sha4_hmac_update (sha4_context *ctx, unsigned char *input, int ilen) |
| SHA-512 HMAC process buffer. | |
| void | sha4_hmac_finish (sha4_context *ctx, unsigned char output[64]) |
| SHA-512 HMAC final digest. | |
| void | sha4_hmac (unsigned char *key, int keylen, unsigned char *input, int ilen, unsigned char output[64], int is384) |
| Output = HMAC-SHA-512( hmac key, input buffer ). | |
| int | sha4_self_test (int verbose) |
| Checkup routine. | |
Variables | |
| static const unsigned int64 | K [80] |
| static const unsigned char | sha4_padding [128] |
| static unsigned char | sha4_test_buf [3][113] |
| static const int | sha4_test_buflen [3] |
| static const unsigned char | sha4_test_sum [6][64] |
| static unsigned char | sha4_hmac_test_key [7][26] |
| static const int | sha4_hmac_test_keylen [7] |
| static unsigned char | sha4_hmac_test_buf [7][153] |
| static const int | sha4_hmac_test_buflen [7] |
| static const unsigned char | sha4_hmac_test_sum [14][64] |
| #define F0 | ( | x, | |||
| y, | |||||
| z | ) | ((x & y) | (z & (x | y))) |
| #define F1 | ( | x, | |||
| y, | |||||
| z | ) | (z ^ (x & (y ^ z))) |
| #define GET_UINT64_BE | ( | n, | |||
| b, | |||||
| i | ) |
{ \
(n) = ( (unsigned int64) (b)[(i) ] << 56 ) \
| ( (unsigned int64) (b)[(i) + 1] << 48 ) \
| ( (unsigned int64) (b)[(i) + 2] << 40 ) \
| ( (unsigned int64) (b)[(i) + 3] << 32 ) \
| ( (unsigned int64) (b)[(i) + 4] << 24 ) \
| ( (unsigned int64) (b)[(i) + 5] << 16 ) \
| ( (unsigned int64) (b)[(i) + 6] << 8 ) \
| ( (unsigned int64) (b)[(i) + 7] ); \
}
Definition at line 39 of file sha4.c.
Referenced by sha4_process().
| #define PUT_UINT64_BE | ( | n, | |||
| b, | |||||
| i | ) |
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 56 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \
(b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \
(b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 7] = (unsigned char) ( (n) ); \
}
Definition at line 53 of file sha4.c.
Referenced by sha4_finish().
| #define ROTR | ( | x, | |||
| n | ) | (SHR(x,n) | (x << (64 - n))) |
| #define S0 | ( | x | ) | (ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7)) |
| #define S1 | ( | x | ) | (ROTR(x,19) ^ ROTR(x,61) ^ SHR(x, 6)) |
| #define S2 | ( | x | ) | (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39)) |
| #define S3 | ( | x | ) | (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41)) |
| #define SHR | ( | x, | |||
| n | ) | (x >> n) |
| void sha4 | ( | unsigned char * | input, | |
| int | ilen, | |||
| unsigned char | output[64], | |||
| int | is384 | |||
| ) |
Output = SHA-512( input buffer ).
| input | buffer holding the data | |
| ilen | length of the input data | |
| output | SHA-384/512 checksum result | |
| is384 | 0 = use SHA512, 1 = use SHA384 |
Definition at line 312 of file sha4.c.
References sha4_finish(), sha4_starts(), and sha4_update().
Referenced by sha4_hmac_starts().
| int sha4_file | ( | char * | path, | |
| unsigned char | output[64], | |||
| int | is384 | |||
| ) |
Output = SHA-512( file contents ).
| path | input file name | |
| output | SHA-384/512 checksum result | |
| is384 | 0 = use SHA512, 1 = use SHA384 |
Definition at line 327 of file sha4.c.
References buf, f, sha4_finish(), sha4_starts(), and sha4_update().
| void sha4_finish | ( | sha4_context * | ctx, | |
| unsigned char | output[64] | |||
| ) |
SHA-512 final digest.
| ctx | SHA-512 context | |
| output | SHA-384/512 checksum result |
Definition at line 276 of file sha4.c.
References int64, sha4_context::is384, PUT_UINT64_BE, sha4_padding, sha4_update(), sha4_context::state, and sha4_context::total.
Referenced by sha4(), sha4_file(), sha4_hmac_finish(), and sha4_self_test().
| void sha4_hmac | ( | unsigned char * | key, | |
| int | keylen, | |||
| unsigned char * | input, | |||
| int | ilen, | |||
| unsigned char | output[64], | |||
| int | is384 | |||
| ) |
Output = HMAC-SHA-512( hmac key, input buffer ).
| key | HMAC secret key | |
| keylen | length of the HMAC key | |
| input | buffer holding the data | |
| ilen | length of the input data | |
| output | HMAC-SHA-384/512 result | |
| is384 | 0 = use SHA512, 1 = use SHA384 |
Definition at line 419 of file sha4.c.
References sha4_hmac_finish(), sha4_hmac_starts(), and sha4_hmac_update().
| void sha4_hmac_finish | ( | sha4_context * | ctx, | |
| unsigned char | output[64] | |||
| ) |
SHA-512 HMAC final digest.
| ctx | HMAC context | |
| output | SHA-384/512 HMAC checksum result |
Definition at line 399 of file sha4.c.
References sha4_context::is384, sha4_context::opad, sha4_finish(), sha4_starts(), and sha4_update().
Referenced by sha4_hmac(), and sha4_self_test().
| void sha4_hmac_starts | ( | sha4_context * | ctx, | |
| unsigned char * | key, | |||
| int | keylen, | |||
| int | is384 | |||
| ) |
SHA-512 HMAC context setup.
| ctx | HMAC context to be initialized | |
| is384 | 0 = use SHA512, 1 = use SHA384 | |
| key | HMAC secret key | |
| keylen | length of the HMAC key |
Definition at line 359 of file sha4.c.
References sha4_context::ipad, sha4_context::opad, sha4(), sha4_starts(), and sha4_update().
Referenced by sha4_hmac(), and sha4_self_test().
| void sha4_hmac_update | ( | sha4_context * | ctx, | |
| unsigned char * | input, | |||
| int | ilen | |||
| ) |
SHA-512 HMAC process buffer.
| ctx | HMAC context | |
| input | buffer holding the data | |
| ilen | length of the input data |
Definition at line 390 of file sha4.c.
References sha4_update().
Referenced by sha4_hmac(), and sha4_self_test().
| static void sha4_process | ( | sha4_context * | ctx, | |
| unsigned char | data[128] | |||
| ) | [static] |
Definition at line 149 of file sha4.c.
References F, GET_UINT64_BE, int64, K, P, S0, S1, and sha4_context::state.
Referenced by sha4_update().
| int sha4_self_test | ( | int | verbose | ) |
Checkup routine.
Definition at line 654 of file sha4.c.
References buf, sha4_finish(), sha4_hmac_finish(), sha4_hmac_starts(), sha4_hmac_test_buf, sha4_hmac_test_buflen, sha4_hmac_test_key, sha4_hmac_test_keylen, sha4_hmac_test_sum, sha4_hmac_update(), sha4_starts(), sha4_test_buf, sha4_test_buflen, sha4_test_sum, and sha4_update().
Referenced by main().
| void sha4_starts | ( | sha4_context * | ctx, | |
| int | is384 | |||
| ) |
SHA-512 context setup.
| ctx | context to be initialized | |
| is384 | 0 = use SHA512, 1 = use SHA384 |
Definition at line 116 of file sha4.c.
References sha4_context::is384, sha4_context::state, sha4_context::total, and UL64.
Referenced by sha4(), sha4_file(), sha4_hmac_finish(), sha4_hmac_starts(), and sha4_self_test().
| void sha4_update | ( | sha4_context * | ctx, | |
| unsigned char * | input, | |||
| int | ilen | |||
| ) |
SHA-512 process buffer.
| ctx | SHA-512 context | |
| input | buffer holding the data | |
| ilen | length of the input data |
Definition at line 221 of file sha4.c.
References sha4_context::buffer, int64, sha4_process(), and sha4_context::total.
Referenced by sha4(), sha4_file(), sha4_finish(), sha4_hmac_finish(), sha4_hmac_starts(), sha4_hmac_update(), and sha4_self_test().
unsigned char sha4_hmac_test_buf[7][153] [static] |
{
{ "Hi There" },
{ "what do ya want for nothing?" },
{ "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
{ "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
{ "Test With Truncation" },
{ "Test Using Larger Than Block-Size Key - Hash Key First" },
{ "This is a test using a larger than block-size key "
"and a larger than block-size data. The key needs to "
"be hashed before being used by the HMAC algorithm." }
}
Definition at line 526 of file sha4.c.
Referenced by sha4_self_test().
const int sha4_hmac_test_buflen[7] [static] |
{
8, 28, 50, 50, 20, 54, 152
}
Definition at line 547 of file sha4.c.
Referenced by sha4_self_test().
unsigned char sha4_hmac_test_key[7][26] [static] |
{
{ "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
"\x0B\x0B\x0B\x0B" },
{ "Jefe" },
{ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
"\xAA\xAA\xAA\xAA" },
{ "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
{ "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
"\x0C\x0C\x0C\x0C" },
{ "" },
{ "" }
}
Definition at line 506 of file sha4.c.
Referenced by sha4_self_test().
const int sha4_hmac_test_keylen[7] [static] |
{
20, 4, 20, 25, 20, 131, 131
}
Definition at line 521 of file sha4.c.
Referenced by sha4_self_test().
const unsigned char sha4_hmac_test_sum[14][64] [static] |
Definition at line 552 of file sha4.c.
Referenced by sha4_self_test().
const unsigned char sha4_padding[128] [static] |
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
Definition at line 261 of file sha4.c.
Referenced by sha4_finish().
unsigned char sha4_test_buf[3][113] [static] |
{
{ "abc" },
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
{ "" }
}
Definition at line 437 of file sha4.c.
Referenced by sha4_self_test().
const int sha4_test_buflen[3] [static] |
{
3, 112, 1000
}
Definition at line 445 of file sha4.c.
Referenced by sha4_self_test().
const unsigned char sha4_test_sum[6][64] [static] |
Definition at line 450 of file sha4.c.
Referenced by sha4_self_test().
1.6.3