Coverage Report

Created: 2024-11-21 07:03

/src/SymCrypt/lib/sha3_256.c
Line
Count
Source (jump to first uncovered line)
1
//
2
// Sha3_256.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_256Algorithm_default = {
15
    &SymCryptSha3_256Init,
16
    &SymCryptSha3_256Append,
17
    &SymCryptSha3_256Result,
18
    NULL,                           // AppendBlocks function is not implemented for SHA-3
19
    &SymCryptSha3_256StateCopy,
20
    sizeof(SYMCRYPT_SHA3_256_STATE),
21
    SYMCRYPT_SHA3_256_RESULT_SIZE,
22
    SYMCRYPT_SHA3_256_INPUT_BLOCK_SIZE,
23
    SYMCRYPT_FIELD_OFFSET(SYMCRYPT_SHA3_256_STATE, ks.state),
24
    SYMCRYPT_FIELD_SIZE(SYMCRYPT_SHA3_256_STATE, ks.state),
25
};
26
27
const PCSYMCRYPT_HASH SymCryptSha3_256Algorithm = &SymCryptSha3_256Algorithm_default;
28
29
30
//
31
// SymCryptSha3_256
32
//
33
#define ALG     SHA3_256
34
#define Alg     Sha3_256
35
#include "hash_pattern.c"
36
#undef ALG
37
#undef Alg
38
39
40
//
41
// SymCryptSha3_256Init
42
//
43
VOID
44
SYMCRYPT_CALL
45
SymCryptSha3_256Init(_Out_ PSYMCRYPT_SHA3_256_STATE pState)
46
94
{
47
94
    SymCryptKeccakInit(&pState->ks,
48
94
                        SYMCRYPT_SHA3_256_INPUT_BLOCK_SIZE,
49
94
                        SYMCRYPT_SHA3_PADDING_VALUE);
50
    
51
94
    SYMCRYPT_SET_MAGIC(pState);
52
94
}
53
54
55
//
56
// SymCryptSha3_256Append
57
//
58
VOID
59
SYMCRYPT_CALL
60
SymCryptSha3_256Append(
61
    _Inout_             PSYMCRYPT_SHA3_256_STATE    pState,
62
    _In_reads_(cbData)  PCBYTE                      pbData,
63
                        SIZE_T                      cbData)
64
13.7k
{
65
13.7k
    SymCryptKeccakAppend(&pState->ks, pbData, cbData);
66
13.7k
}
67
68
69
//
70
// SymCryptSha3_256Result
71
//
72
VOID
73
SYMCRYPT_CALL
74
SymCryptSha3_256Result(
75
    _Inout_                                     PSYMCRYPT_SHA3_256_STATE    pState,
76
    _Out_writes_(SYMCRYPT_SHA3_256_RESULT_SIZE) PBYTE                       pbResult)
77
94
{
78
94
    SymCryptKeccakExtract(&pState->ks, pbResult, SYMCRYPT_SHA3_256_RESULT_SIZE, TRUE);
79
94
}
80
81
82
//
83
// SymCryptSha3_256StateExport
84
//
85
VOID
86
SYMCRYPT_CALL
87
SymCryptSha3_256StateExport(
88
    _In_                                                    PCSYMCRYPT_SHA3_256_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_256State, &pState->ks, pbBlob);
93
0
}
94
95
96
//
97
// SymCryptSha3_256StateImport
98
//
99
SYMCRYPT_ERROR
100
SYMCRYPT_CALL
101
SymCryptSha3_256StateImport(
102
    _Out_                                                   PSYMCRYPT_SHA3_256_STATE    pState,
103
    _In_reads_bytes_(SYMCRYPT_SHA3_256_STATE_EXPORT_SIZE)   PCBYTE                      pbBlob)
104
0
{
105
0
    SYMCRYPT_ERROR scError = SymCryptKeccakStateImport(SymCryptBlobTypeSha3_256State, &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_256KATAnswer[32] = {
121
    0x3a, 0x98, 0x5d, 0xa7, 0x4f, 0xe2, 0x25, 0xb2,
122
    0x04, 0x5c, 0x17, 0x2d, 0x6b, 0xd3, 0x90, 0xbd,
123
    0x85, 0x5f, 0x08, 0x6e, 0x3e, 0x9d, 0x52, 0x5b,
124
    0x46, 0xbf, 0xe2, 0x45, 0x11, 0x43, 0x15, 0x32
125
};
126
127
VOID
128
SYMCRYPT_CALL
129
SymCryptSha3_256Selftest(void)
130
0
{
131
0
    BYTE result[SYMCRYPT_SHA3_256_RESULT_SIZE];
132
133
0
    SymCryptSha3_256(SymCryptTestMsg3, sizeof(SymCryptTestMsg3), result);
134
135
0
    SymCryptInjectError(result, sizeof(result));
136
137
0
    if (memcmp(result, sha3_256KATAnswer, sizeof(result)) != 0)
138
0
    {
139
0
        SymCryptFatal('SHA3');
140
0
    }
141
0
}