Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright 2017, Fraunhofer SIT sponsored by Infineon Technologies AG |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | *******************************************************************************/ |
| 27 | #ifndef ESYS_CRYPTO_H |
| 28 | #define ESYS_CRYPTO_H |
| 29 | |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 30 | #include <stddef.h> |
Philip Tricca | 8ffd3c4 | 2018-03-09 16:27:24 -0800 | [diff] [blame] | 31 | #include "tss2_tpm2_types.h" |
Juergen Repp | 6209718 | 2018-03-19 18:04:42 +0100 | [diff] [blame] | 32 | #include "tss2-sys/sysapi_util.h" |
Philip Tricca | 57360da | 2018-03-15 17:22:10 -0700 | [diff] [blame] | 33 | |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 34 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | /** |
| 39 | * @addtogroup iesys |
| 40 | * @{ |
| 41 | */ |
| 42 | |
| 43 | #define AES_BLOCK_SIZE_IN_BYTES 16 |
| 44 | |
| 45 | typedef struct _IESYS_CRYPTO_CONTEXT IESYS_CRYPTO_CONTEXT_BLOB; |
| 46 | |
| 47 | TSS2_RC iesys_crypto_hash_get_digest_size(TPM2_ALG_ID hashAlg, size_t *size); |
| 48 | |
| 49 | TSS2_RC iesys_crypto_hash_get_block_size(TPM2_ALG_ID hashAlg, size_t *size); |
| 50 | |
| 51 | TSS2_RC iesys_cryptogcry_hash_start( |
| 52 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 53 | TPM2_ALG_ID hashAlg); |
| 54 | |
| 55 | TSS2_RC iesys_cryptogcry_hash_update( |
| 56 | IESYS_CRYPTO_CONTEXT_BLOB *context, |
| 57 | const uint8_t *buffer, size_t size); |
| 58 | |
| 59 | TSS2_RC iesys_cryptogcry_hash_update2b( |
| 60 | IESYS_CRYPTO_CONTEXT_BLOB *context, |
| 61 | TPM2B *b); |
| 62 | |
| 63 | TSS2_RC iesys_cryptogcry_hash_finish( |
| 64 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 65 | uint8_t *buffer, |
| 66 | size_t *size); |
| 67 | |
| 68 | TSS2_RC iesys_cryptogcry_hash_finish2b( |
| 69 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 70 | TPM2B *b); |
| 71 | |
| 72 | void iesys_cryptogcry_hash_abort(IESYS_CRYPTO_CONTEXT_BLOB **context); |
| 73 | |
| 74 | #define iesys_crypto_hash_start iesys_cryptogcry_hash_start |
| 75 | #define iesys_crypto_hash_update iesys_cryptogcry_hash_update |
| 76 | #define iesys_crypto_hash_update2b iesys_cryptogcry_hash_update2b |
| 77 | #define iesys_crypto_hash_finish iesys_cryptogcry_hash_finish |
| 78 | #define iesys_crypto_hash_finish2b iesys_cryptogcry_hash_finish2b |
| 79 | #define iesys_crypto_hash_abort iesys_cryptogcry_hash_abort |
| 80 | |
| 81 | TSS2_RC iesys_cryptogcry_hmac_start( |
| 82 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 83 | TPM2_ALG_ID hmacAlg, |
| 84 | const uint8_t *key, |
| 85 | size_t size); |
| 86 | |
| 87 | TSS2_RC iesys_cryptogcry_hmac_start2b( |
| 88 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 89 | TPM2_ALG_ID hmacAlg, |
| 90 | TPM2B *b); |
| 91 | |
| 92 | TSS2_RC iesys_cryptogcry_hmac_update( |
| 93 | IESYS_CRYPTO_CONTEXT_BLOB *context, |
| 94 | const uint8_t *buffer, |
| 95 | size_t size); |
| 96 | |
| 97 | TSS2_RC iesys_cryptogcry_hmac_update2b( |
| 98 | IESYS_CRYPTO_CONTEXT_BLOB *context, |
| 99 | TPM2B *b); |
| 100 | |
| 101 | TSS2_RC iesys_cryptogcry_hmac_finish( |
| 102 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 103 | uint8_t *buffer, |
| 104 | size_t *size); |
| 105 | |
| 106 | TSS2_RC iesys_cryptogcry_hmac_finish2b( |
| 107 | IESYS_CRYPTO_CONTEXT_BLOB **context, |
| 108 | TPM2B *b); |
| 109 | |
| 110 | void iesys_cryptogcry_hmac_abort(IESYS_CRYPTO_CONTEXT_BLOB **context); |
| 111 | |
| 112 | #define iesys_crypto_hmac_start iesys_cryptogcry_hmac_start |
| 113 | #define iesys_crypto_hmac_start2b iesys_cryptogcry_hmac_start2b |
| 114 | #define iesys_crypto_hmac_update iesys_cryptogcry_hmac_update |
| 115 | #define iesys_crypto_hmac_update2b iesys_cryptogcry_hmac_update2b |
| 116 | #define iesys_crypto_hmac_finish iesys_cryptogcry_hmac_finish |
| 117 | #define iesys_crypto_hmac_finish2b iesys_cryptogcry_hmac_finish2b |
| 118 | #define iesys_crypto_hmac_abort iesys_cryptogcry_hmac_abort |
| 119 | |
| 120 | TSS2_RC iesys_crypto_pHash( |
| 121 | TPM2_ALG_ID alg, |
| 122 | const uint8_t rcBuffer[4], |
| 123 | const uint8_t ccBuffer[4], |
| 124 | const TPM2B_NAME *name1, |
| 125 | const TPM2B_NAME *name2, |
| 126 | const TPM2B_NAME *name3, |
| 127 | const uint8_t *pBuffer, |
| 128 | size_t pBuffer_size, |
| 129 | uint8_t *pHash, |
| 130 | size_t *pHash_size); |
| 131 | |
| 132 | #define iesys_crypto_cpHash(alg, ccBuffer, name1, name2, name3, \ |
| 133 | cpBuffer, cpBuffer_size, cpHash, cpHash_size) \ |
| 134 | iesys_crypto_pHash(alg, NULL, ccBuffer, name1, name2, name3, cpBuffer, \ |
| 135 | cpBuffer_size, cpHash, cpHash_size) |
| 136 | #define iesys_crypto_rpHash(alg, rcBuffer, ccBuffer, rpBuffer, rpBuffer_size, \ |
| 137 | rpHash, rpHash_size) \ |
| 138 | iesys_crypto_pHash(alg, rcBuffer, ccBuffer, NULL, NULL, NULL, rpBuffer, \ |
| 139 | rpBuffer_size, rpHash, rpHash_size) |
| 140 | |
| 141 | |
| 142 | TSS2_RC iesys_crypto_authHmac( |
| 143 | TPM2_ALG_ID alg, |
| 144 | uint8_t *hmacKey, |
| 145 | size_t hmacKeySize, |
| 146 | const uint8_t *pHash, |
| 147 | size_t pHash_size, |
| 148 | const TPM2B_NONCE *nonceNewer, |
| 149 | const TPM2B_NONCE *nonceOlder, |
| 150 | const TPM2B_NONCE *nonceDecrypt, |
| 151 | const TPM2B_NONCE *nonceEncrypt, |
| 152 | TPMA_SESSION sessionAttributes, |
| 153 | TPM2B_AUTH *hmac); |
| 154 | |
| 155 | TSS2_RC iesys_cryptogcry_random2b(TPM2B_NONCE *nonce, size_t num_bytes); |
| 156 | #define iesys_crypto_random2b iesys_cryptogcry_random2b |
| 157 | |
| 158 | TSS2_RC iesys_cryptogcry_pk_encrypt( |
| 159 | TPM2B_PUBLIC *key, |
| 160 | size_t in_size, |
| 161 | BYTE *in_buffer, |
| 162 | size_t max_out_size, |
| 163 | BYTE *out_buffer, |
| 164 | size_t *out_size, |
| 165 | const char *label); |
| 166 | |
| 167 | #define iesys_crypto_pk_encrypt iesys_cryptogcry_pk_encrypt |
| 168 | |
| 169 | |
| 170 | TSS2_RC iesys_crypto_KDFaHmac( |
| 171 | TPM2_ALG_ID alg, |
| 172 | uint8_t *hmacKey, |
| 173 | size_t hmacKeySize, |
| 174 | uint32_t counter, |
| 175 | const char *label, |
| 176 | TPM2B_NONCE *contextU, |
| 177 | TPM2B_NONCE *contextV, |
| 178 | uint32_t bitlength, |
| 179 | uint8_t *hmac, |
| 180 | size_t *hmacSize); |
| 181 | |
| 182 | TSS2_RC iesys_crypto_KDFa( |
| 183 | TPM2_ALG_ID hashAlg, |
| 184 | uint8_t *hmacKey, |
| 185 | size_t hmacKeySize, |
| 186 | const char *label, |
| 187 | TPM2B_NONCE *contextU, |
| 188 | TPM2B_NONCE *contextV, |
| 189 | uint32_t bitLength, |
| 190 | uint32_t *counterInOut, |
Juergen Repp | f29cfef | 2018-03-27 14:18:07 +0200 | [diff] [blame] | 191 | BYTE *outKey, |
| 192 | BOOL use_digest_size); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 193 | |
Juergen Repp | 4a73889 | 2018-04-24 12:05:48 +0200 | [diff] [blame] | 194 | TSS2_RC iesys_cryptogcry_KDFe( |
| 195 | TPM2_ALG_ID hashAlg, |
| 196 | TPM2B_ECC_PARAMETER *Z, |
| 197 | const char *label, |
| 198 | TPM2B_ECC_PARAMETER *partyUInfo, |
| 199 | TPM2B_ECC_PARAMETER *partyVInfo, |
| 200 | UINT32 bit_size, |
| 201 | BYTE *key); |
| 202 | |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 203 | TSS2_RC iesys_cryptogcry_sym_aes_encrypt( |
| 204 | uint8_t *key, |
| 205 | TPM2_ALG_ID tpm_sym_alg, |
| 206 | TPMI_AES_KEY_BITS key_bits, |
| 207 | TPM2_ALG_ID tpm_mode, |
| 208 | size_t blk_len, |
| 209 | uint8_t *dst, |
| 210 | size_t dst_size, |
Juergen Repp | 131e29f | 2018-04-24 14:43:39 +0200 | [diff] [blame^] | 211 | uint8_t *iv); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 212 | |
| 213 | TSS2_RC iesys_cryptogcry_sym_aes_decrypt( |
| 214 | uint8_t *key, |
| 215 | TPM2_ALG_ID tpm_sym_alg, |
| 216 | TPMI_AES_KEY_BITS key_bits, |
| 217 | TPM2_ALG_ID tpm_mode, |
| 218 | size_t blk_len, |
| 219 | uint8_t *dst, |
| 220 | size_t dst_size, |
Juergen Repp | 131e29f | 2018-04-24 14:43:39 +0200 | [diff] [blame^] | 221 | uint8_t *iv); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 222 | |
Juergen Repp | f29cfef | 2018-03-27 14:18:07 +0200 | [diff] [blame] | 223 | TSS2_RC iesys_xor_parameter_obfuscation( |
| 224 | TPM2_ALG_ID hash_alg, |
| 225 | uint8_t *key, |
| 226 | size_t key_size, |
| 227 | TPM2B_NONCE * contextU, |
| 228 | TPM2B_NONCE * contextV, |
| 229 | BYTE *data, |
| 230 | size_t data_size); |
| 231 | |
Juergen Repp | 4a73889 | 2018-04-24 12:05:48 +0200 | [diff] [blame] | 232 | TSS2_RC iesys_cryptogcry_get_ecdh_point( |
| 233 | TPM2B_PUBLIC * key, |
| 234 | size_t max_out_size, |
| 235 | TPM2B_ECC_PARAMETER *Z, |
| 236 | TPMS_ECC_POINT *Q, |
| 237 | BYTE * out_buffer, |
| 238 | size_t * out_size); |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 239 | |
Juergen Repp | 4a73889 | 2018-04-24 12:05:48 +0200 | [diff] [blame] | 240 | #define iesys_crypto_get_ecdh_point iesys_cryptogcry_get_ecdh_point |
Juergen Repp | ff821bd | 2017-12-11 15:21:42 +0100 | [diff] [blame] | 241 | #define iesys_crypto_sym_aes_encrypt iesys_cryptogcry_sym_aes_encrypt |
| 242 | #define iesys_crypto_sym_aes_decrypt iesys_cryptogcry_sym_aes_decrypt |
| 243 | |
| 244 | /* @} */ |
| 245 | |
| 246 | #ifdef __cplusplus |
| 247 | } /* extern "C" */ |
| 248 | #endif |
| 249 | |
| 250 | #endif /* ESYS_CRYPTO_H */ |