Coverage Report

Created: 2024-11-21 07:03

/src/SymCrypt/lib/hmacsha1.c
Line
Count
Source (jump to first uncovered line)
1
//
2
// HmacSha1.c
3
//
4
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
5
//
6
7
#include "precomp.h"
8
9
//
10
// This implementation of HMAC uses extensive knowledge of the internal workings of the
11
// SHA1 implementation and uses internal routines. 
12
// This reduces the overhead per HMAC computation by up to 20%, which is significant
13
// enough to take on the added complexity.
14
//
15
16
#define ALG SHA1
17
#define Alg Sha1
18
22.1k
#define SET_DATALENGTH( state, len )    {state.dataLengthL = len;}
19
#include "hmac_pattern.c"
20
#undef SET_DATALENGTH
21
#undef Alg
22
#undef ALG
23
24
const SYMCRYPT_MAC SymCryptHmacSha1Algorithm_default = {
25
    SymCryptHmacSha1ExpandKey,
26
    SymCryptHmacSha1Init,
27
    SymCryptHmacSha1Append,
28
    SymCryptHmacSha1Result,
29
    sizeof(SYMCRYPT_HMAC_SHA1_EXPANDED_KEY),
30
    sizeof(SYMCRYPT_HMAC_SHA1_STATE),
31
    SYMCRYPT_HMAC_SHA1_RESULT_SIZE,
32
    &SymCryptSha1Algorithm,
33
    SYMCRYPT_FIELD_OFFSET( SYMCRYPT_HMAC_SHA1_EXPANDED_KEY, outerState ),
34
};
35
36
const PCSYMCRYPT_MAC SymCryptHmacSha1Algorithm = &SymCryptHmacSha1Algorithm_default;
37
38
static const BYTE hmacSha1Kat[20] = {
39
    0x2a, 0x29, 0x85, 0x40, 0x23, 0xba, 0x2e, 0xf1,
40
    0x49, 0x0f, 0x8c, 0xd8, 0x97, 0xa8, 0xcc, 0x6b, 
41
    0x55, 0x7b, 0x2a, 0x12,
42
};
43
44
VOID
45
SYMCRYPT_CALL
46
SymCryptHmacSha1Selftest(void)
47
0
{
48
0
    SYMCRYPT_HMAC_SHA1_EXPANDED_KEY xKey;
49
0
    BYTE res[SYMCRYPT_HMAC_SHA1_RESULT_SIZE];
50
51
0
    SymCryptHmacSha1ExpandKey( &xKey, SymCryptTestKey32, 16 );
52
0
    SymCryptHmacSha1( &xKey, SymCryptTestMsg3, sizeof( SymCryptTestMsg3 ), res );
53
54
0
    SymCryptInjectError( res, sizeof( res ) );
55
    
56
0
    if( memcmp( res, hmacSha1Kat, sizeof( res ) ) != 0 ) 
57
0
    {
58
0
        SymCryptFatal( 'hSh1' );
59
0
    }
60
61
    //
62
    // Normally we would wipe the expanded key structure here,
63
    // but as this is a selftest with known data this is not needed.
64
    //
65
0
}
66