Coverage Report

Created: 2025-07-11 07:00

/src/ibmswtpm2/src/CryptSmac.c
Line
Count
Source (jump to first uncovered line)
1
/********************************************************************************/
2
/*                    */
3
/*    Message Authentication Codes Based on a Symmetric Block Cipher  */
4
/*           Written by Ken Goldman       */
5
/*           IBM Thomas J. Watson Research Center     */
6
/*            $Id: CryptSmac.c 1311 2018-08-23 21:39:29Z kgoldman $   */
7
/*                    */
8
/*  Licenses and Notices              */
9
/*                    */
10
/*  1. Copyright Licenses:              */
11
/*                    */
12
/*  - Trusted Computing Group (TCG) grants to the user of the source code in  */
13
/*    this specification (the "Source Code") a worldwide, irrevocable,    */
14
/*    nonexclusive, royalty free, copyright license to reproduce, create  */
15
/*    derivative works, distribute, display and perform the Source Code and */
16
/*    derivative works thereof, and to grant others the rights granted herein.  */
17
/*                    */
18
/*  - The TCG grants to the user of the other parts of the specification  */
19
/*    (other than the Source Code) the rights to reproduce, distribute,   */
20
/*    display, and perform the specification solely for the purpose of    */
21
/*    developing products based on such documents.        */
22
/*                    */
23
/*  2. Source Code Distribution Conditions:         */
24
/*                    */
25
/*  - Redistributions of Source Code must retain the above copyright licenses,  */
26
/*    this list of conditions and the following disclaimers.      */
27
/*                    */
28
/*  - Redistributions in binary form must reproduce the above copyright   */
29
/*    licenses, this list of conditions and the following disclaimers in the  */
30
/*    documentation and/or other materials provided with the distribution.  */
31
/*                    */
32
/*  3. Disclaimers:               */
33
/*                    */
34
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF */
35
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH */
36
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES) */
37
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.   */
38
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for    */
39
/*  information on specification licensing rights available through TCG   */
40
/*  membership agreements.              */
41
/*                    */
42
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED   */
43
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR   */
44
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR    */
45
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY    */
46
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.   */
47
/*                    */
48
/*  - Without limitation, TCG and its members and licensors disclaim all  */
49
/*    liability, including liability for infringement of any proprietary  */
50
/*    rights, relating to use of information in this specification and to the */
51
/*    implementation of this specification, and TCG disclaims all liability for */
52
/*    cost of procurement of substitute goods or services, lost profits, loss   */
53
/*    of use, loss of data or any incidental, consequential, direct, indirect,  */
54
/*    or special damages, whether under contract, tort, warranty or otherwise,  */
55
/*    arising in any way out of use or reliance upon this specification or any  */
56
/*    information herein.             */
57
/*                    */
58
/*  (c) Copyright IBM Corp. and others, 2018          */
59
/*                    */
60
/********************************************************************************/
61
62
/* 10.2.20  CryptSmac.c */
63
/* 10.2.20.1  Introduction */
64
/* This file contains the implementation of the message authentication codes based on a symmetric
65
   block cipher. These functions only use the single block encryption functions of the selected
66
   symmetric cryptographic library. */
67
/* 10.2.20.2  Includes, Defines, and Typedefs */
68
#define _CRYPT_HASH_C_
69
#include "Tpm.h"
70
#if SMAC_IMPLEMENTED
71
    /* 10.2.20.2.1  CryptSmacStart() */
72
    /* Function to start an SMAC. */
73
UINT16
74
CryptSmacStart(
75
         HASH_STATE              *state,
76
         TPMU_PUBLIC_PARMS       *keyParameters,
77
         TPM_ALG_ID               macAlg,          // IN: the type of MAC
78
         TPM2B                   *key
79
         )
80
0
{
81
0
    UINT16                  retVal = 0;
82
    //
83
    // Make sure that the key size is correct. This should have been checked
84
    // at key load, but...
85
0
    if(BITS_TO_BYTES(keyParameters->symDetail.sym.keyBits.sym) == key->size)
86
0
  {
87
0
      switch(macAlg)
88
0
    {
89
0
#if ALG_CMAC
90
0
      case ALG_CMAC_VALUE:
91
0
        retVal = CryptCmacStart(&state->state.smac, keyParameters,
92
0
              macAlg, key);
93
0
        break;
94
0
#endif
95
0
      default:
96
0
        break;
97
0
    }
98
0
  }
99
0
    state->type = (retVal != 0) ? HASH_STATE_SMAC : HASH_STATE_EMPTY;
100
0
    return retVal;
101
0
}
102
/* 10.2.20.2.2  CryptMacStart() */
103
/* Function to start either an HMAC or an SMAC. Cannot reuse the CryptHmacStart() function because
104
   of the difference in number of parameters. */
105
UINT16
106
CryptMacStart(
107
        HMAC_STATE              *state,
108
        TPMU_PUBLIC_PARMS       *keyParameters,
109
        TPM_ALG_ID               macAlg,          // IN: the type of MAC
110
        TPM2B                   *key
111
        )
112
0
{
113
0
    MemorySet(state, 0, sizeof(HMAC_STATE));
114
0
    if(CryptHashIsValidAlg(macAlg, FALSE))
115
0
  {
116
0
      return CryptHmacStart(state, macAlg, key->size, key->buffer);
117
0
  }
118
0
    else if(CryptSmacIsValidAlg(macAlg, FALSE))
119
0
  {
120
0
      return CryptSmacStart(&state->hashState, keyParameters, macAlg, key);
121
0
  }
122
0
    else
123
0
  return 0;
124
0
}
125
/* 10.2.20.2.3  CryptMacEnd() */
126
/* Dispatch to the MAC end function using a size and buffer pointer. */
127
UINT16
128
CryptMacEnd(
129
      HMAC_STATE          *state,
130
      UINT32               size,
131
      BYTE                *buffer
132
      )
133
0
{
134
0
    UINT16              retVal = 0;
135
0
    if(state->hashState.type == HASH_STATE_SMAC)
136
0
  retVal = (state->hashState.state.smac.smacMethods.end)(
137
0
                     &state->hashState.state.smac.state, size, buffer);
138
0
    else if(state->hashState.type == HASH_STATE_HMAC)
139
0
  retVal = CryptHmacEnd(state, size, buffer);
140
0
    state->hashState.type = HASH_STATE_EMPTY;
141
0
    return retVal;
142
0
}
143
/* 10.2.20.2.4  CryptMacEnd2B() */
144
/* Dispatch to the MAC end function using a 2B. */
145
UINT16
146
CryptMacEnd2B (
147
         HMAC_STATE          *state,
148
         TPM2B               *data
149
         )
150
0
{
151
0
    return CryptMacEnd(state, data->size, data->buffer);
152
0
}
153
#endif // SMAC_IMPLEMENTED
154