/src/SymCrypt/lib/sha3_512.c
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Sha3_512.c |
3 | | // |
4 | | // Copyright (c) Microsoft Corporation. Licensed under the MIT license. |
5 | | // |
6 | | |
7 | | #include "precomp.h" |
8 | | |
9 | | // |
10 | | // See the symcrypt.h file for documentation on what the various functions do. |
11 | | // |
12 | | |
13 | | |
14 | | const SYMCRYPT_HASH SymCryptSha3_512Algorithm_default = { |
15 | | &SymCryptSha3_512Init, |
16 | | &SymCryptSha3_512Append, |
17 | | &SymCryptSha3_512Result, |
18 | | NULL, // AppendBlocks function is not implemented for SHA-3 |
19 | | &SymCryptSha3_512StateCopy, |
20 | | sizeof(SYMCRYPT_SHA3_512_STATE), |
21 | | SYMCRYPT_SHA3_512_RESULT_SIZE, |
22 | | SYMCRYPT_SHA3_512_INPUT_BLOCK_SIZE, |
23 | | SYMCRYPT_FIELD_OFFSET(SYMCRYPT_SHA3_512_STATE, ks.state), |
24 | | SYMCRYPT_FIELD_SIZE(SYMCRYPT_SHA3_512_STATE, ks.state), |
25 | | }; |
26 | | |
27 | | const PCSYMCRYPT_HASH SymCryptSha3_512Algorithm = &SymCryptSha3_512Algorithm_default; |
28 | | |
29 | | |
30 | | // |
31 | | // SymCryptSha3_512 |
32 | | // |
33 | | #define ALG SHA3_512 |
34 | | #define Alg Sha3_512 |
35 | | #include "hash_pattern.c" |
36 | | #undef ALG |
37 | | #undef Alg |
38 | | |
39 | | |
40 | | // |
41 | | // SymCryptSha3_512Init |
42 | | // |
43 | | VOID |
44 | | SYMCRYPT_CALL |
45 | | SymCryptSha3_512Init(_Out_ PSYMCRYPT_SHA3_512_STATE pState) |
46 | 84 | { |
47 | 84 | SymCryptKeccakInit(&pState->ks, |
48 | 84 | SYMCRYPT_SHA3_512_INPUT_BLOCK_SIZE, |
49 | 84 | SYMCRYPT_SHA3_PADDING_VALUE); |
50 | | |
51 | 84 | SYMCRYPT_SET_MAGIC(pState); |
52 | 84 | } |
53 | | |
54 | | |
55 | | // |
56 | | // SymCryptSha3_512Append |
57 | | // |
58 | | VOID |
59 | | SYMCRYPT_CALL |
60 | | SymCryptSha3_512Append( |
61 | | _Inout_ PSYMCRYPT_SHA3_512_STATE pState, |
62 | | _In_reads_(cbData) PCBYTE pbData, |
63 | | SIZE_T cbData) |
64 | 16.0k | { |
65 | 16.0k | SymCryptKeccakAppend(&pState->ks, pbData, cbData); |
66 | 16.0k | } |
67 | | |
68 | | |
69 | | // |
70 | | // SymCryptSha3_512Result |
71 | | // |
72 | | VOID |
73 | | SYMCRYPT_CALL |
74 | | SymCryptSha3_512Result( |
75 | | _Inout_ PSYMCRYPT_SHA3_512_STATE pState, |
76 | | _Out_writes_(SYMCRYPT_SHA3_512_RESULT_SIZE) PBYTE pbResult) |
77 | 84 | { |
78 | 84 | SymCryptKeccakExtract(&pState->ks, pbResult, SYMCRYPT_SHA3_512_RESULT_SIZE, TRUE); |
79 | 84 | } |
80 | | |
81 | | |
82 | | // |
83 | | // SymCryptSha3_512StateExport |
84 | | // |
85 | | VOID |
86 | | SYMCRYPT_CALL |
87 | | SymCryptSha3_512StateExport( |
88 | | _In_ PCSYMCRYPT_SHA3_512_STATE pState, |
89 | | _Out_writes_bytes_(SYMCRYPT_SHA3_512_STATE_EXPORT_SIZE) PBYTE pbBlob) |
90 | 0 | { |
91 | 0 | SYMCRYPT_CHECK_MAGIC(pState); |
92 | 0 | SymCryptKeccakStateExport(SymCryptBlobTypeSha3_512State, &pState->ks, pbBlob); |
93 | 0 | } |
94 | | |
95 | | // |
96 | | // SymCryptSha3_512StateExport |
97 | | // |
98 | | SYMCRYPT_ERROR |
99 | | SYMCRYPT_CALL |
100 | | SymCryptSha3_512StateImport( |
101 | | _Out_ PSYMCRYPT_SHA3_512_STATE pState, |
102 | | _In_reads_bytes_(SYMCRYPT_SHA3_512_STATE_EXPORT_SIZE) PCBYTE pbBlob) |
103 | 0 | { |
104 | 0 | SYMCRYPT_ERROR scError = SymCryptKeccakStateImport(SymCryptBlobTypeSha3_512State, &pState->ks, pbBlob); |
105 | |
|
106 | 0 | if (scError == SYMCRYPT_NO_ERROR) |
107 | 0 | { |
108 | 0 | SYMCRYPT_SET_MAGIC(pState); |
109 | 0 | } |
110 | |
|
111 | 0 | return scError; |
112 | 0 | } |
113 | | |
114 | | |
115 | | // |
116 | | // Simple test vector for FIPS module testing |
117 | | // |
118 | | |
119 | | static const BYTE sha3_512KATAnswer[64] = { |
120 | | 0xb7, 0x51, 0x85, 0x0b, 0x1a, 0x57, 0x16, 0x8a, |
121 | | 0x56, 0x93, 0xcd, 0x92, 0x4b, 0x6b, 0x09, 0x6e, |
122 | | 0x08, 0xf6, 0x21, 0x82, 0x74, 0x44, 0xf7, 0x0d, |
123 | | 0x88, 0x4f, 0x5d, 0x02, 0x40, 0xd2, 0x71, 0x2e, |
124 | | 0x10, 0xe1, 0x16, 0xe9, 0x19, 0x2a, 0xf3, 0xc9, |
125 | | 0x1a, 0x7e, 0xc5, 0x76, 0x47, 0xe3, 0x93, 0x40, |
126 | | 0x57, 0x34, 0x0b, 0x4c, 0xf4, 0x08, 0xd5, 0xa5, |
127 | | 0x65, 0x92, 0xf8, 0x27, 0x4e, 0xec, 0x53, 0xf0 |
128 | | }; |
129 | | |
130 | | VOID |
131 | | SYMCRYPT_CALL |
132 | | SymCryptSha3_512Selftest(void) |
133 | 0 | { |
134 | 0 | BYTE result[SYMCRYPT_SHA3_512_RESULT_SIZE]; |
135 | |
|
136 | 0 | SymCryptSha3_512(SymCryptTestMsg3, sizeof(SymCryptTestMsg3), result); |
137 | |
|
138 | 0 | SymCryptInjectError(result, sizeof(result)); |
139 | |
|
140 | 0 | if (memcmp(result, sha3_512KATAnswer, sizeof(result)) != 0) |
141 | 0 | { |
142 | 0 | SymCryptFatal('SHA3'); |
143 | 0 | } |
144 | 0 | } |
145 | | |