/src/SymCrypt/lib/sha3_384.c
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Sha3_384.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_384Algorithm_default = { |
15 | | &SymCryptSha3_384Init, |
16 | | &SymCryptSha3_384Append, |
17 | | &SymCryptSha3_384Result, |
18 | | NULL, // AppendBlocks function is not implemented for SHA-3 |
19 | | &SymCryptSha3_384StateCopy, |
20 | | sizeof(SYMCRYPT_SHA3_384_STATE), |
21 | | SYMCRYPT_SHA3_384_RESULT_SIZE, |
22 | | SYMCRYPT_SHA3_384_INPUT_BLOCK_SIZE, |
23 | | SYMCRYPT_FIELD_OFFSET(SYMCRYPT_SHA3_384_STATE, ks.state), |
24 | | SYMCRYPT_FIELD_SIZE(SYMCRYPT_SHA3_384_STATE, ks.state), |
25 | | }; |
26 | | |
27 | | const PCSYMCRYPT_HASH SymCryptSha3_384Algorithm = &SymCryptSha3_384Algorithm_default; |
28 | | |
29 | | |
30 | | // |
31 | | // SymCryptSha3_384 |
32 | | // |
33 | | #define ALG SHA3_384 |
34 | | #define Alg Sha3_384 |
35 | | #include "hash_pattern.c" |
36 | | #undef ALG |
37 | | #undef Alg |
38 | | |
39 | | |
40 | | // |
41 | | // SymCryptSha3_384Init |
42 | | // |
43 | | VOID |
44 | | SYMCRYPT_CALL |
45 | | SymCryptSha3_384Init(_Out_ PSYMCRYPT_SHA3_384_STATE pState) |
46 | 85 | { |
47 | 85 | SymCryptKeccakInit(&pState->ks, |
48 | 85 | SYMCRYPT_SHA3_384_INPUT_BLOCK_SIZE, |
49 | 85 | SYMCRYPT_SHA3_PADDING_VALUE); |
50 | | |
51 | 85 | SYMCRYPT_SET_MAGIC(pState); |
52 | 85 | } |
53 | | |
54 | | |
55 | | // |
56 | | // SymCryptSha3_384Append |
57 | | // |
58 | | VOID |
59 | | SYMCRYPT_CALL |
60 | | SymCryptSha3_384Append( |
61 | | _Inout_ PSYMCRYPT_SHA3_384_STATE pState, |
62 | | _In_reads_(cbData) PCBYTE pbData, |
63 | | SIZE_T cbData) |
64 | 11.8k | { |
65 | 11.8k | SymCryptKeccakAppend(&pState->ks, pbData, cbData); |
66 | 11.8k | } |
67 | | |
68 | | |
69 | | // |
70 | | // SymCryptSha3_384Result |
71 | | // |
72 | | VOID |
73 | | SYMCRYPT_CALL |
74 | | SymCryptSha3_384Result( |
75 | | _Inout_ PSYMCRYPT_SHA3_384_STATE pState, |
76 | | _Out_writes_(SYMCRYPT_SHA3_384_RESULT_SIZE) PBYTE pbResult) |
77 | 85 | { |
78 | 85 | SymCryptKeccakExtract(&pState->ks, pbResult, SYMCRYPT_SHA3_384_RESULT_SIZE, TRUE); |
79 | 85 | } |
80 | | |
81 | | |
82 | | // |
83 | | // SymCryptSha3_384StateExport |
84 | | // |
85 | | VOID |
86 | | SYMCRYPT_CALL |
87 | | SymCryptSha3_384StateExport( |
88 | | _In_ PCSYMCRYPT_SHA3_384_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_384State, &pState->ks, pbBlob); |
93 | 0 | } |
94 | | |
95 | | |
96 | | // |
97 | | // SymCryptSha3_384StateImport |
98 | | // |
99 | | SYMCRYPT_ERROR |
100 | | SYMCRYPT_CALL |
101 | | SymCryptSha3_384StateImport( |
102 | | _Out_ PSYMCRYPT_SHA3_384_STATE pState, |
103 | | _In_reads_bytes_(SYMCRYPT_SHA3_384_STATE_EXPORT_SIZE) PCBYTE pbBlob) |
104 | 0 | { |
105 | 0 | SYMCRYPT_ERROR scError = SymCryptKeccakStateImport(SymCryptBlobTypeSha3_384State, &pState->ks, pbBlob); |
106 | |
|
107 | 0 | if (scError == SYMCRYPT_NO_ERROR) |
108 | 0 | { |
109 | 0 | SYMCRYPT_SET_MAGIC(pState); |
110 | 0 | } |
111 | |
|
112 | 0 | return scError; |
113 | 0 | } |
114 | | |
115 | | |
116 | | // |
117 | | // Simple test vector for FIPS module testing |
118 | | // |
119 | | |
120 | | static const BYTE sha3_384KATAnswer[48] = { |
121 | | 0xec, 0x01, 0x49, 0x82, 0x88, 0x51, 0x6f, 0xc9, |
122 | | 0x26, 0x45, 0x9f, 0x58, 0xe2, 0xc6, 0xad, 0x8d, |
123 | | 0xf9, 0xb4, 0x73, 0xcb, 0x0f, 0xc0, 0x8c, 0x25, |
124 | | 0x96, 0xda, 0x7c, 0xf0, 0xe4, 0x9b, 0xe4, 0xb2, |
125 | | 0x98, 0xd8, 0x8c, 0xea, 0x92, 0x7a, 0xc7, 0xf5, |
126 | | 0x39, 0xf1, 0xed, 0xf2, 0x28, 0x37, 0x6d, 0x25 |
127 | | }; |
128 | | |
129 | | VOID |
130 | | SYMCRYPT_CALL |
131 | | SymCryptSha3_384Selftest(void) |
132 | 0 | { |
133 | 0 | BYTE result[SYMCRYPT_SHA3_384_RESULT_SIZE]; |
134 | |
|
135 | 0 | SymCryptSha3_384(SymCryptTestMsg3, sizeof(SymCryptTestMsg3), result); |
136 | |
|
137 | 0 | SymCryptInjectError(result, sizeof(result)); |
138 | |
|
139 | 0 | if (memcmp(result, sha3_384KATAnswer, sizeof(result)) != 0) |
140 | 0 | { |
141 | 0 | SymCryptFatal('SHA3'); |
142 | 0 | } |
143 | 0 | } |