Debian Codegraph @

simulator/ms-tpm-20-ref/TPMCmd/tpm/include/CryptSym.h

raw
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 symmetric block cipher modes
38 // allowed for a TPM. These functions only use the single block encryption functions
39 // of the selected symmetric cryptographic library.
40
41 //** Includes, Defines, and Typedefs
42 #ifndef CRYPT_SYM_H
43 #define CRYPT_SYM_H
44
45 typedef union tpmCryptKeySchedule_t {
46 #if ALG_AES
47 tpmKeyScheduleAES AES;
48 #endif
49 #if ALG_SM4
50 tpmKeyScheduleSM4 SM4;
51 #endif
52 #if ALG_CAMELLIA
53 tpmKeyScheduleCAMELLIA CAMELLIA;
54 #endif
55
56 #if ALG_TDES
57 tpmKeyScheduleTDES TDES[3];
58 #endif
59 #if SYMMETRIC_ALIGNMENT == 8
60 uint64_t alignment;
61 #else
62 uint32_t alignment;
63 #endif
64 } tpmCryptKeySchedule_t;
65
66
67 // Each block cipher within a library is expected to conform to the same calling
68 // conventions with three parameters ('keySchedule', 'in', and 'out') in the same
69 // order. That means that all algorithms would use the same order of the same
70 // parameters. The code is written assuming the ('keySchedule', 'in', and 'out')
71 // order. However, if the library uses a different order, the order can be changed
72 // with a SWIZZLE macro that puts the parameters in the correct order.
73 // Note that all algorithms have to use the same order and number of parameters
74 // because the code to build the calling list is common for each call to encrypt
75 // or decrypt with the algorithm chosen by setting a function pointer to select
76 // the algorithm that is used.
77
78 # define ENCRYPT(keySchedule, in, out) \
79 encrypt(SWIZZLE(keySchedule, in, out))
80
81 # define DECRYPT(keySchedule, in, out) \
82 decrypt(SWIZZLE(keySchedule, in, out))
83
84
85 // Note that the macros rely on 'encrypt' as local values in the
86 // functions that use these macros. Those parameters are set by the macro that
87 // set the key schedule to be used for the call.
88
89
90 #define ENCRYPT_CASE(ALG) \
91 case TPM_ALG_##ALG: \
92 TpmCryptSetEncryptKey##ALG(key, keySizeInBits, &keySchedule.ALG); \
93 encrypt = (TpmCryptSetSymKeyCall_t)TpmCryptEncrypt##ALG; \
94 break;
95 #define DECRYPT_CASE(ALG) \
96 case TPM_ALG_##ALG: \
97 TpmCryptSetDecryptKey##ALG(key, keySizeInBits, &keySchedule.ALG); \
98 decrypt = (TpmCryptSetSymKeyCall_t)TpmCryptDecrypt##ALG; \
99 break;
100
101 #if ALG_AES
102 #define ENCRYPT_CASE_AES ENCRYPT_CASE(AES)
103 #define DECRYPT_CASE_AES DECRYPT_CASE(AES)
104 #else
105 #define ENCRYPT_CASE_AES
106 #define DECRYPT_CASE_AES
107 #endif
108 #if ALG_SM4
109 #define ENCRYPT_CASE_SM4 ENCRYPT_CASE(SM4)
110 #define DECRYPT_CASE_SM4 DECRYPT_CASE(SM4)
111 #else
112 #define ENCRYPT_CASE_SM4
113 #define DECRYPT_CASE_SM4
114 #endif
115 #if ALG_CAMELLIA
116 #define ENCRYPT_CASE_CAMELLIA ENCRYPT_CASE(CAMELLIA)
117 #define DECRYPT_CASE_CAMELLIA DECRYPT_CASE(CAMELLIA)
118 #else
119 #define ENCRYPT_CASE_CAMELLIA
120 #define DECRYPT_CASE_CAMELLIA
121 #endif
122 #if ALG_TDES
123 #define ENCRYPT_CASE_TDES ENCRYPT_CASE(TDES)
124 #define DECRYPT_CASE_TDES DECRYPT_CASE(TDES)
125 #else
126 #define ENCRYPT_CASE_TDES
127 #define DECRYPT_CASE_TDES
128 #endif
129
130 // For each algorithm the case will either be defined or null.
131 #define SELECT(direction) \
132 switch(algorithm) \
133 { \
134 direction##_CASE_AES \
135 direction##_CASE_SM4 \
136 direction##_CASE_CAMELLIA \
137 direction##_CASE_TDES \
138 default: \
139 FAIL(FATAL_ERROR_INTERNAL); \
140 }
141
142
143 #endif // CRYPT_SYM_H