1 /* Microsoft Reference Implementation for TPM 2.0
2 *
3 * The copyright in this software is being made available under the BSD License,
4 * included below. This software may be subject to other third party and
5 * contributor rights, including patent rights, and no such rights are granted
6 * under this license.
7 *
8 * Copyright (c) Microsoft Corporation
9 *
10 * All rights reserved.
11 *
12 * BSD License
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 *
17 * Redistributions of source code must retain the above copyright notice, this list
18 * of conditions and the following disclaimer.
19 *
20 * Redistributions in binary form must reproduce the above copyright notice, this
21 * list of conditions and the following disclaimer in the documentation and/or
22 * other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ""AS IS""
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 //** Introduction
36 //
37 // This file contains the implementation of the message authentication codes based
38 // on a symmetric block cipher. These functions only use the single block
39 // encryption functions of the selected symmetric cryptographic library.
40
41 //** Includes, Defines, and Typedefs
42 #define _CRYPT_HASH_C_
43 #include "Tpm.h"
44
45 #if SMAC_IMPLEMENTED
46
47 //*** CryptSmacStart()
48 // Function to start an SMAC.
49 UINT16
CryptSmacStart(HASH_STATE * state,TPMU_PUBLIC_PARMS * keyParameters,TPM_ALG_ID macAlg,TPM2B * key)50 CryptSmacStart(
51 HASH_STATE *state,
52 TPMU_PUBLIC_PARMS *keyParameters,
53 TPM_ALG_ID macAlg, // IN: the type of MAC
54 TPM2B *key
55 )
56 {
57 UINT16 retVal = 0;
58 //
59 // Make sure that the key size is correct. This should have been checked
60 // at key load, but...
61 if(BITS_TO_BYTES(keyParameters->symDetail.sym.keyBits.sym) == key->size)
62 {
63 switch(macAlg)
64 {
65 #if ALG_CMAC
66 case TPM_ALG_CMAC:
67 retVal = CryptCmacStart(&state->state.smac, keyParameters,
68 macAlg, key);
69 break;
70 #endif
71 default:
72 break;
73 }
74 }
75 state->type = (retVal != 0) ? HASH_STATE_SMAC : HASH_STATE_EMPTY;
76 return retVal;
77 }
78
79 //*** CryptMacStart()
80 // Function to start either an HMAC or an SMAC. Cannot reuse the CryptHmacStart
81 // function because of the difference in number of parameters.
82 UINT16
CryptMacStart(HMAC_STATE * state,TPMU_PUBLIC_PARMS * keyParameters,TPM_ALG_ID macAlg,TPM2B * key)83 CryptMacStart(
84 HMAC_STATE *state,
85 TPMU_PUBLIC_PARMS *keyParameters,
86 TPM_ALG_ID macAlg, // IN: the type of MAC
87 TPM2B *key
88 )
89 {
90 MemorySet(state, 0, sizeof(HMAC_STATE));
91 if(CryptHashIsValidAlg(macAlg, FALSE))
92 {
93 return CryptHmacStart(state, macAlg, key->size, key->buffer);
94 }
95 else if(CryptSmacIsValidAlg(macAlg, FALSE))
96 {
97 return CryptSmacStart(&state->hashState, keyParameters, macAlg, key);
98 }
99 else
100 return 0;
101 }
102
103 //*** CryptMacEnd()
104 // Dispatch to the MAC end function using a size and buffer pointer.
105 UINT16
CryptMacEnd(HMAC_STATE * state,UINT32 size,BYTE * buffer)106 CryptMacEnd(
107 HMAC_STATE *state,
108 UINT32 size,
109 BYTE *buffer
110 )
111 {
112 UINT16 retVal = 0;
113 if(state->hashState.type == HASH_STATE_SMAC)
114 retVal = (state->hashState.state.smac.smacMethods.end)(
115 &state->hashState.state.smac.state, size, buffer);
116 else if(state->hashState.type == HASH_STATE_HMAC)
117 retVal = CryptHmacEnd(state, size, buffer);
118 state->hashState.type = HASH_STATE_EMPTY;
119 return retVal;
120 }
121
122 //*** CryptMacEnd2B()
123 // Dispatch to the MAC end function using a 2B.
124 UINT16
CryptMacEnd2B(HMAC_STATE * state,TPM2B * data)125 CryptMacEnd2B (
126 HMAC_STATE *state,
127 TPM2B *data
128 )
129 {
130 return CryptMacEnd(state, data->size, data->buffer);
131 }
132 #endif // SMAC_IMPLEMENTED
133