summaryrefslogtreecommitdiffstats
path: root/src/tpm2/crypto/openssl/CryptSmac.c
blob: c4b3515f8055ccb41b5e389d18079293be1b2e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/********************************************************************************/
/*										*/
/*		Message Authentication Codes Based on a Symmetric Block Cipher	*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*            $Id: CryptSmac.c 1658 2021-01-22 23:14:01Z kgoldman $		*/
/*										*/
/*  Licenses and Notices							*/
/*										*/
/*  1. Copyright Licenses:							*/
/*										*/
/*  - Trusted Computing Group (TCG) grants to the user of the source code in	*/
/*    this specification (the "Source Code") a worldwide, irrevocable, 		*/
/*    nonexclusive, royalty free, copyright license to reproduce, create 	*/
/*    derivative works, distribute, display and perform the Source Code and	*/
/*    derivative works thereof, and to grant others the rights granted herein.	*/
/*										*/
/*  - The TCG grants to the user of the other parts of the specification 	*/
/*    (other than the Source Code) the rights to reproduce, distribute, 	*/
/*    display, and perform the specification solely for the purpose of 		*/
/*    developing products based on such documents.				*/
/*										*/
/*  2. Source Code Distribution Conditions:					*/
/*										*/
/*  - Redistributions of Source Code must retain the above copyright licenses, 	*/
/*    this list of conditions and the following disclaimers.			*/
/*										*/
/*  - Redistributions in binary form must reproduce the above copyright 	*/
/*    licenses, this list of conditions	and the following disclaimers in the 	*/
/*    documentation and/or other materials provided with the distribution.	*/
/*										*/
/*  3. Disclaimers:								*/
/*										*/
/*  - THE COPYRIGHT LICENSES SET FORTH ABOVE DO NOT REPRESENT ANY FORM OF	*/
/*  LICENSE OR WAIVER, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, WITH	*/
/*  RESPECT TO PATENT RIGHTS HELD BY TCG MEMBERS (OR OTHER THIRD PARTIES)	*/
/*  THAT MAY BE NECESSARY TO IMPLEMENT THIS SPECIFICATION OR OTHERWISE.		*/
/*  Contact TCG Administration (admin@trustedcomputinggroup.org) for 		*/
/*  information on specification licensing rights available through TCG 	*/
/*  membership agreements.							*/
/*										*/
/*  - THIS SPECIFICATION IS PROVIDED "AS IS" WITH NO EXPRESS OR IMPLIED 	*/
/*    WARRANTIES WHATSOEVER, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR 	*/
/*    FITNESS FOR A PARTICULAR PURPOSE, ACCURACY, COMPLETENESS, OR 		*/
/*    NONINFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS, OR ANY WARRANTY 		*/
/*    OTHERWISE ARISING OUT OF ANY PROPOSAL, SPECIFICATION OR SAMPLE.		*/
/*										*/
/*  - Without limitation, TCG and its members and licensors disclaim all 	*/
/*    liability, including liability for infringement of any proprietary 	*/
/*    rights, relating to use of information in this specification and to the	*/
/*    implementation of this specification, and TCG disclaims all liability for	*/
/*    cost of procurement of substitute goods or services, lost profits, loss 	*/
/*    of use, loss of data or any incidental, consequential, direct, indirect, 	*/
/*    or special damages, whether under contract, tort, warranty or otherwise, 	*/
/*    arising in any way out of use or reliance upon this specification or any 	*/
/*    information herein.							*/
/*										*/
/*  (c) Copyright IBM Corp. and others, 2018 - 2021				*/
/*										*/
/********************************************************************************/

/* 10.2.20	CryptSmac.c */
/* 10.2.20.1	Introduction */
/* This file contains the implementation of the message authentication codes based on a symmetric
   block cipher. These functions only use the single block encryption functions of the selected
   symmetric cryptographic library. */
/* 10.2.20.2	Includes, Defines, and Typedefs */
#define _CRYPT_HASH_C_
#include "Tpm.h"
#if SMAC_IMPLEMENTED
    /* 10.2.20.2.1	CryptSmacStart() */
    /* Function to start an SMAC. */
UINT16
CryptSmacStart(
	       HASH_STATE              *state,
	       TPMU_PUBLIC_PARMS       *keyParameters,
	       TPM_ALG_ID               macAlg,          // IN: the type of MAC
	       TPM2B                   *key
	       )
{
    UINT16                  retVal = 0;
    //
    // Make sure that the key size is correct. This should have been checked
    // at key load, but...
    if(BITS_TO_BYTES(keyParameters->symDetail.sym.keyBits.sym) == key->size)
	{
	    switch(macAlg)
		{
#if ALG_CMAC
		  case TPM_ALG_CMAC:
		    retVal = CryptCmacStart(&state->state.smac, keyParameters,
					    macAlg, key);
		    break;
#endif
		  default:
		    break;
		}
	}
    state->type = (retVal != 0) ? HASH_STATE_SMAC : HASH_STATE_EMPTY;
    return retVal;
}
/* 10.2.20.2.2	CryptMacStart() */
/* Function to start either an HMAC or an SMAC. Cannot reuse the CryptHmacStart() function because
   of the difference in number of parameters. */
UINT16
CryptMacStart(
	      HMAC_STATE              *state,
	      TPMU_PUBLIC_PARMS       *keyParameters,
	      TPM_ALG_ID               macAlg,          // IN: the type of MAC
	      TPM2B                   *key
	      )
{
    MemorySet(state, 0, sizeof(HMAC_STATE));
    if(CryptHashIsValidAlg(macAlg, FALSE))
	{
	    return CryptHmacStart(state, macAlg, key->size, key->buffer);
	}
    else if(CryptSmacIsValidAlg(macAlg, FALSE))
	{
	    return CryptSmacStart(&state->hashState, keyParameters, macAlg, key);
	}
    else
	return 0;
}
/* 10.2.20.2.3	CryptMacEnd() */
/* Dispatch to the MAC end function using a size and buffer pointer. */
UINT16
CryptMacEnd(
	    HMAC_STATE          *state,
	    UINT32               size,
	    BYTE                *buffer
	    )
{
    UINT16              retVal = 0;
    if(state->hashState.type == HASH_STATE_SMAC)
	retVal = (state->hashState.state.smac.smacMethods.end)(
							       &state->hashState.state.smac.state, size, buffer);
    else if(state->hashState.type == HASH_STATE_HMAC)
	retVal = CryptHmacEnd(state, size, buffer);
    state->hashState.type = HASH_STATE_EMPTY;
    return retVal;
}
#if 0 /* libtpms added */
/* 10.2.20.2.4	CryptMacEnd2B() */
/* Dispatch to the MAC end function using a 2B. */
UINT16
CryptMacEnd2B (
	       HMAC_STATE          *state,
	       TPM2B               *data
	       )
{
    return CryptMacEnd(state, data->size, data->buffer);
}
#endif /* libtpms added */
#endif // SMAC_IMPLEMENTED