forked from wolfSSL/wolfTPM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpm2_types.h
693 lines (621 loc) · 19 KB
/
tpm2_types.h
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* tpm2_types.h
*
* Copyright (C) 2006-2022 wolfSSL Inc.
*
* This file is part of wolfTPM.
*
* wolfTPM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfTPM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifndef __TPM2_TYPES_H__
#define __TPM2_TYPES_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolftpm/visibility.h>
#include <stdint.h>
#ifndef WOLFTPM_USER_SETTINGS
/* use generated options.h or a custom one */
#include <wolftpm/options.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef WOLFTPM_WINAPI
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <windows.h>
#endif
/* ---------------------------------------------------------------------------*/
/* TPM TYPES */
/* ---------------------------------------------------------------------------*/
typedef uint8_t UINT8;
typedef uint8_t BYTE;
typedef int8_t INT8;
#if !defined(BOOL) && !defined(HAVE_BOOL)
typedef int BOOL;
#endif
typedef uint16_t UINT16;
typedef int16_t INT16;
typedef uint32_t UINT32;
typedef int32_t INT32;
#ifndef _W64
typedef uint64_t UINT64;
typedef int64_t INT64;
#endif
#ifndef YES
#define YES 1
#endif
#ifndef NO
#define NO 0
#endif
/* ---------------------------------------------------------------------------*/
/* WOLFCRYPT */
/* ---------------------------------------------------------------------------*/
#ifndef WOLFTPM2_NO_WOLFCRYPT
#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/types.h>
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/error-ssl.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/aes.h>
#ifdef WOLF_CRYPTO_CB
#include <wolfssl/wolfcrypt/cryptocb.h>
#elif defined(WOLF_CRYPTO_DEV)
/* old name for crypto callback support */
#include <wolfssl/wolfcrypt/cryptodev.h>
#endif
#ifndef WOLFCRYPT_ONLY
/* for additional error codes */
#include <wolfssl/ssl.h> /* for wolfSSL_ERR_reason_error_string */
#endif
#ifdef DEBUG_WOLFTPM
#include <stdio.h>
#endif
#include <wolfssl/version.h>
#if defined(LIBWOLFSSL_VERSION_HEX) && LIBWOLFSSL_VERSION_HEX < 0x03015004
/* The wc_HashFree was added in v3.15.4, so use stub to allow building */
#define wc_HashFree(h, t) (0)
#endif
#ifndef XFEOF
#define XFEOF feof
#endif
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef uint8_t byte;
typedef uint16_t word16;
typedef uint32_t word32;
typedef uint64_t word64;
/* Errors from wolfssl/wolfcrypt/error-crypt.h */
#define BAD_FUNC_ARG -173 /* Bad function argument provided */
#define BUFFER_E -132 /* output buffer too small or input too large */
#define NOT_COMPILED_IN -174 /* Feature not compiled in */
#define BAD_MUTEX_E -106 /* Bad mutex operation */
#define WC_TIMEOUT_E -107 /* timeout error */
/* Errors from wolfssl/error-ssl.h */
#define SOCKET_ERROR_E -308 /* error state on socket */
#ifndef WOLFTPM_CUSTOM_TYPES
#ifndef WOLFTPM2_NO_HEAP
#define XMALLOC(s, h, t) malloc((size_t)(s))
#define XFREE(p, h, t) free(p)
#endif
#define XMEMCPY(d,s,l) memcpy((d),(s),(l))
#define XMEMSET(b,c,l) memset((b),(c),(l))
#define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n))
#define XSTRLEN(s1) strlen((s1))
#define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n))
#define XSTRSTR(s1,s2) strstr((s1),(s2))
#endif /* !WOLFTPM_CUSTOM_TYPES */
/* Endianess */
#ifndef BIG_ENDIAN_ORDER
#define LITTLE_ENDIAN_ORDER
#endif
#ifndef OFFSETOF
#if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4))
#define OFFSETOF(type, field) __builtin_offsetof(type, field)
#else
#define OFFSETOF(type, field) ((size_t)&(((type *)0)->field))
#endif
#endif
/* GCC Version */
#ifndef __GNUC_PREREQ
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
#define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
#define __GNUC_PREREQ(maj, min) (0) /* not GNUC */
#endif
#endif
#if !defined(WOLFTPM_CUSTOM_STDIO) && !defined(NO_FILESYSTEM)
/* stdio, default case */
#define XFILE FILE*
#define XFOPEN fopen
#define XFSEEK fseek
#define XFTELL ftell
#define XREWIND rewind
#define XFREAD fread
#define XFWRITE fwrite
#define XFCLOSE fclose
#define XSEEK_END SEEK_END
#define XBADFILE NULL
#define XFGETS fgets
#define XFEOF feof
#endif
#endif /* !WOLFTPM2_NO_WOLFCRYPT */
/* enable way for customer to override printf */
#ifdef XPRINTF
#undef printf
#define printf XPRINTF
#endif
/* ---------------------------------------------------------------------------*/
/* TPM HARDWARE TYPE */
/* ---------------------------------------------------------------------------*/
/* Microchip ATTPM20 */
/* #define WOLFTPM_MCHP */
/* ST ST33TP TPM 2.0 */
/* #define WOLFTPM_ST33 */
/* Nuvoton NPCT75x TPM 2.0 module */
/* #define WOLFTPM_NUVOTON */
/* Infineon SLB9670 TPM 2.0 (default) */
/* #define WOLFTPM_SLB9670 */
/* #define WOLFTPM_SLB9672 */
/* Chip Specific Settings */
#ifdef WOLFTPM_MCHP
/* Microchip ATTPM20 */
/* Requires SPI wait states */
#ifndef WOLFTPM_CHECK_WAIT_STATE
#define WOLFTPM_CHECK_WAIT_STATE
#endif
/* Max: 36MHz (has issues so using 33MHz) */
#define TPM2_SPI_MAX_HZ_MICROCHIP 33000000
#ifndef TPM2_SPI_MAX_HZ
#define TPM2_SPI_MAX_HZ TPM2_SPI_MAX_HZ_MICROCHIP
#endif
#elif defined(WOLFTPM_ST33)
/* ST ST33TPM20 modules */
/* Requires wait state support */
#ifndef WOLFTPM_CHECK_WAIT_STATE
#define WOLFTPM_CHECK_WAIT_STATE
#endif
/* Max: 33MHz */
#define TPM2_SPI_MAX_HZ_ST 33000000
#ifndef TPM2_SPI_MAX_HZ
#define TPM2_SPI_MAX_HZ TPM2_SPI_MAX_HZ_ST
#endif
#elif defined(WOLFTPM_NUVOTON)
/* Nuvoton NPCT75x module */
/* Requires wait state support */
#ifndef WOLFTPM_CHECK_WAIT_STATE
#define WOLFTPM_CHECK_WAIT_STATE
#endif
#define TPM2_SPI_MAX_HZ_NUVOTON 43000000
#ifndef TPM2_SPI_MAX_HZ
/* Max: 43MHz */
#define TPM2_SPI_MAX_HZ TPM2_SPI_MAX_HZ_NUVOTON
#endif
#else
/* Infineon OPTIGA SLB9670/SLB9672 */
#ifdef WOLFTPM_SLB9670
/* Max: 43MHz */
#define TPM2_SPI_MAX_HZ_INFINEON 43000000
#else
#undef WOLFTPM_SLB9672
#define WOLFTPM_SLB9672
/* Max: 33MHz */
#define TPM2_SPI_MAX_HZ_INFINEON 33000000
#endif
#ifndef TPM2_SPI_MAX_HZ
#define TPM2_SPI_MAX_HZ TPM2_SPI_MAX_HZ_INFINEON
#endif
#endif
/* Auto-chip detection requires SPI wait state support and safe SPI bus speed */
#ifdef WOLFTPM_AUTODETECT
/* SPI wait state checking must be enabled */
#undef WOLFTPM_CHECK_WAIT_STATE
#define WOLFTPM_CHECK_WAIT_STATE
/* use a safe MHz (minimum of above) */
#undef TPM2_SPI_MAX_HZ
#define TPM2_SPI_MAX_HZ 33000000
/* always perform self-test (some chips require) */
#undef WOLFTPM_PERFORM_SELFTEST
#define WOLFTPM_PERFORM_SELFTEST
#endif
/* ---------------------------------------------------------------------------*/
/* ALGORITHMS */
/* ---------------------------------------------------------------------------*/
#define TPM_MD5_DIGEST_SIZE 16
#define TPM_SHA_DIGEST_SIZE 20
#define TPM_SHA224_DIGEST_SIZE 28
#define TPM_SHA256_DIGEST_SIZE 32
#define TPM_SHA384_DIGEST_SIZE 48
#define TPM_SHA512_DIGEST_SIZE 64
#ifndef MAX_ECC_KEY_BYTES
#define MAX_ECC_KEY_BYTES 66
#endif
#ifndef TPM_MAX_BLOCK_SIZE
#define TPM_MAX_BLOCK_SIZE 128
#endif
#ifndef TPM_MAX_DIGEST_SIZE
#define TPM_MAX_DIGEST_SIZE TPM_SHA512_DIGEST_SIZE
#endif
/* ---------------------------------------------------------------------------*/
/* CONFIGURABLE LIMITS */
/* ---------------------------------------------------------------------------*/
#ifndef MAX_SPI_FRAMESIZE
#define MAX_SPI_FRAMESIZE 64
#endif
#ifndef TPM_STARTUP_TEST_TRIES
#define TPM_STARTUP_TEST_TRIES 2
#endif
#ifndef TPM_TIMEOUT_TRIES
#if defined(WOLFTPM_LINUX_DEV) || defined(WOLFTPM_SWTPM) || defined(WOLFTPM_WINAPI)
#define TPM_TIMEOUT_TRIES 0
#else
#define TPM_TIMEOUT_TRIES 1000000
#endif
#endif
#ifndef TPM_SPI_WAIT_RETRY
#define TPM_SPI_WAIT_RETRY 50
#endif
#ifndef MAX_SYM_BLOCK_SIZE
#define MAX_SYM_BLOCK_SIZE 20
#endif
#ifndef MAX_SYM_KEY_BYTES
#define MAX_SYM_KEY_BYTES 256
#endif
#ifndef LABEL_MAX_BUFFER
#define LABEL_MAX_BUFFER 128
#endif
#ifndef MAX_RSA_KEY_BITS
#define MAX_RSA_KEY_BITS 2048
#endif
#ifndef MAX_RSA_KEY_BYTES
#define MAX_RSA_KEY_BYTES (((MAX_RSA_KEY_BITS+7)/8)*2)
#endif
#ifndef MAX_ECC_KEY_BITS
#define MAX_ECC_KEY_BITS 521
#endif
#ifndef MAX_ECC_KEY_BYTES
#define MAX_ECC_KEY_BYTES (((MAX_ECC_KEY_BITS+7)/8)*2)
#endif
#ifndef MAX_AES_KEY_BITS
#define MAX_AES_KEY_BITS 128
#endif
#ifndef MAX_AES_BLOCK_SIZE_BYTES
#define MAX_AES_BLOCK_SIZE_BYTES 16
#endif
#ifndef MAX_AES_KEY_BYTES
#define MAX_AES_KEY_BYTES (MAX_AES_KEY_BITS/8)
#endif
/* ---------------------------------------------------------------------------*/
/* IMPLEMENTATION SPECIFIC VALUES */
/* ---------------------------------------------------------------------------*/
/* Optional delay between polling */
#if (defined(WOLFTPM_SLB9670) || defined(WOLFTPM_SLB9672)) && !defined(XTPM_WAIT)
/* For Infineon SLB9670 and SLB9672 adding 10us delay improves performance
* and prevents issue with rapid use at higher speeds */
#ifdef __linux__
#ifndef XTPM_WAIT_POLLING_US
#define XTPM_WAIT_POLLING_US 10 /* 0.01ms */
#endif
#include <unistd.h>
#define XTPM_WAIT() usleep(XTPM_WAIT_POLLING_US);
#endif
#endif
#ifndef XTPM_WAIT
#define XTPM_WAIT() /* just poll without delay by default */
#endif
#ifndef BUFFER_ALIGNMENT
#define BUFFER_ALIGNMENT 4
#endif
#ifndef IMPLEMENTATION_PCR
#define IMPLEMENTATION_PCR 24
#endif
#ifndef PLATFORM_PCR
#define PLATFORM_PCR 24
#endif
#ifndef DRTM_PCR
#define DRTM_PCR 17
#endif
#ifndef HCRTM_PCR
#define HCRTM_PCR 0
#endif
#ifndef NUM_LOCALITIES
#define NUM_LOCALITIES 1
#endif
#ifndef MAX_HANDLE_NUM
#define MAX_HANDLE_NUM 3
#endif
#ifndef MAX_ACTIVE_SESSIONS
#define MAX_ACTIVE_SESSIONS 64
#endif
#ifndef MAX_LOADED_SESSIONS
#define MAX_LOADED_SESSIONS 3
#endif
#ifndef MAX_SESSION_NUM
#define MAX_SESSION_NUM 3
#endif
#ifndef MAX_LOADED_OBJECTS
#define MAX_LOADED_OBJECTS 3
#endif
#ifndef MIN_EVICT_OBJECTS
#define MIN_EVICT_OBJECTS 2
#endif
#ifndef PCR_SELECT_MIN
#define PCR_SELECT_MIN ((PLATFORM_PCR+7)/8)
#endif
#ifndef PCR_SELECT_MAX
#define PCR_SELECT_MAX ((IMPLEMENTATION_PCR+7)/8)
#endif
#ifndef MAX_CONTEXT_SIZE
#define MAX_CONTEXT_SIZE 2048
#endif
#ifndef MAX_DIGEST_BUFFER
#define MAX_DIGEST_BUFFER 1024
#endif
#ifndef MAX_NV_INDEX_SIZE
#define MAX_NV_INDEX_SIZE 2048
#endif
#ifndef MAX_NV_BUFFER_SIZE
#define MAX_NV_BUFFER_SIZE 768
#endif
#ifndef MAX_CAP_BUFFER
#define MAX_CAP_BUFFER 1024
#endif
#ifndef NV_MEMORY_SIZE
#define NV_MEMORY_SIZE 16384
#endif
#ifndef NUM_STATIC_PCR
#define NUM_STATIC_PCR 16
#endif
#ifndef MAX_ALG_LIST_SIZE
#define MAX_ALG_LIST_SIZE 64
#endif
#ifndef TIMER_PRESCALE
#define TIMER_PRESCALE 100000
#endif
#ifndef PRIMARY_SEED_SIZE
#define PRIMARY_SEED_SIZE 32
#endif
#ifndef CONTEXT_ENCRYPT_ALG
#define CONTEXT_ENCRYPT_ALG TPM_ALG_AES
#endif
#ifndef CONTEXT_ENCRYPT_KEY_BITS
#define CONTEXT_ENCRYPT_KEY_BITS MAX_SYM_KEY_BITS
#endif
#ifndef CONTEXT_ENCRYPT_KEY_BYTES
#define CONTEXT_ENCRYPT_KEY_BYTES ((CONTEXT_ENCRYPT_KEY_BITS+7 )/8)
#endif
#ifndef CONTEXT_INTEGRITY_HASH_ALG
#define CONTEXT_INTEGRITY_HASH_ALG TPM_ALG_SHA256
#endif
#ifndef CONTEXT_INTEGRITY_HASH_SIZE
#define CONTEXT_INTEGRITY_HASH_SIZE SHA256_DIGEST_SIZE
#endif
#ifndef PROOF_SIZE
#define PROOF_SIZE CONTEXT_INTEGRITY_HASH_SIZE
#endif
#ifndef NV_CLOCK_UPDATE_INTERVAL
#define NV_CLOCK_UPDATE_INTERVAL 12
#endif
#ifndef NUM_POLICY_PCR
#define NUM_POLICY_PCR 1
#endif
#ifndef MAX_COMMAND_SIZE
#define MAX_COMMAND_SIZE 4096
#endif
#ifndef MAX_RESPONSE_SIZE
#define MAX_RESPONSE_SIZE 4096
#endif
#ifndef ORDERLY_BITS
#define ORDERLY_BITS 8
#endif
#ifndef MAX_ORDERLY_COUNT
#define MAX_ORDERLY_COUNT ((1 << ORDERLY_BITS) - 1)
#endif
#ifndef ALG_ID_FIRST
#define ALG_ID_FIRST TPM_ALG_FIRST
#endif
#ifndef ALG_ID_LAST
#define ALG_ID_LAST TPM_ALG_LAST
#endif
#ifndef MAX_SYM_DATA
#define MAX_SYM_DATA 128
#endif
#ifndef MAX_RNG_ENTROPY_SIZE
#define MAX_RNG_ENTROPY_SIZE 64
#endif
#ifndef MAX_RNG_REQ_SIZE
#define MAX_RNG_REQ_SIZE 32 /* ST33 and SLB9670 max is 32-bytes */
#endif
#ifndef RAM_INDEX_SPACE
#define RAM_INDEX_SPACE 512
#endif
#ifndef RSA_DEFAULT_PUBLIC_EXPONENT
#define RSA_DEFAULT_PUBLIC_EXPONENT 0x00010001
#endif
#ifndef ENABLE_PCR_NO_INCREMENT
#define ENABLE_PCR_NO_INCREMENT 1
#endif
#ifndef CRT_FORMAT_RSA
#define CRT_FORMAT_RSA 1
#endif
#ifndef PRIVATE_VENDOR_SPECIFIC_BYTES
#define PRIVATE_VENDOR_SPECIFIC_BYTES ((MAX_RSA_KEY_BYTES/2) * (3 + CRT_FORMAT_RSA * 2))
#endif
#ifndef MAX_CAP_CC
#define MAX_CAP_CC ((TPM_CC_LAST - TPM_CC_FIRST) + 1)
#endif
#ifndef MAX_CAP_DATA
#define MAX_CAP_DATA (MAX_CAP_BUFFER - sizeof(TPM_CAP) - sizeof(UINT32))
#endif
#ifndef MAX_CAP_HANDLES
#define MAX_CAP_HANDLES (MAX_CAP_DATA / sizeof(TPM_HANDLE))
#endif
#ifndef HASH_COUNT
#define HASH_COUNT (2) /* SHA1 and SHA256 */
#endif
#ifndef MAX_CAP_ALGS
#define MAX_CAP_ALGS (MAX_CAP_DATA / sizeof(TPMS_ALG_PROPERTY))
#endif
#ifndef MAX_TPM_PROPERTIES
#define MAX_TPM_PROPERTIES (MAX_CAP_DATA / sizeof(TPMS_TAGGED_PROPERTY))
#endif
#ifndef MAX_PCR_PROPERTIES
#define MAX_PCR_PROPERTIES (MAX_CAP_DATA / sizeof(TPMS_TAGGED_PCR_SELECT))
#endif
#ifndef MAX_ECC_CURVES
#define MAX_ECC_CURVES (MAX_CAP_DATA / sizeof(TPM_ECC_CURVE))
#endif
#ifndef MAX_TAGGED_POLICIES
#define MAX_TAGGED_POLICIES (MAX_CAP_DATA / sizeof(TPMS_TAGGED_POLICY))
#endif
/* ---------------------------------------------------------------------------*/
/* WRAPPER CONFIGURATION */
/* ---------------------------------------------------------------------------*/
/* Defines the default digest algo type to use for the wrapper functions */
#ifndef WOLFTPM2_WRAP_DIGEST
#define WOLFTPM2_WRAP_DIGEST TPM_ALG_SHA256
#endif
/* Defines the default RSA key bits for the wrapper functions */
#ifndef WOLFTPM2_WRAP_RSA_KEY_BITS
#define WOLFTPM2_WRAP_RSA_KEY_BITS MAX_RSA_KEY_BITS
#endif
#ifndef WOLFTPM2_WRAP_RSA_EXPONENT
#define WOLFTPM2_WRAP_RSA_EXPONENT RSA_DEFAULT_PUBLIC_EXPONENT
#endif
#ifndef WOLFTPM2_WRAP_ECC_KEY_BITS
#define WOLFTPM2_WRAP_ECC_KEY_BITS (MAX_ECC_KEY_BITS*8)
#endif
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && \
(defined(WOLF_CRYPTO_DEV) || defined(WOLF_CRYPTO_CB))
/* Enable the crypto callback support */
#define WOLFTPM_CRYPTOCB
#endif
#if !defined(WOLFTPM2_NO_WOLFCRYPT) && defined(WOLFSSL_CERT_GEN) && \
(!defined(NO_RSA) || defined(HAVE_ECC))
/* Enable the certificate generation support */
#define WOLFTPM2_CERT_GEN
#endif
/* ---------------------------------------------------------------------------*/
/* ENDIANESS HELPERS */
/* ---------------------------------------------------------------------------*/
#ifdef __ICCARM__
#include "intrinsics.h"
#endif
#ifdef INTEL_INTRINSICS
/* for non visual studio probably need no long version, 32 bit only
* i.e., _rotl and _rotr */
#include <stdlib.h> /* get intrinsic definitions */
#pragma intrinsic(_lrotl, _lrotr)
static inline word32 rotlFixed(word32 x, word32 y) {
return y ? _lrotl(x, y) : x;
}
static inline word32 rotrFixed(word32 x, word32 y) {
return y ? _lrotr(x, y) : x;
}
#elif defined(__CCRX__)
#include <builtin.h> /* get intrinsic definitions */
static inline word32 rotlFixed(word32 x, word32 y) {
return _builtin_rotl(x, y);
}
static inline word32 rotrFixed(word32 x, word32 y) {
return _builtin_rotr(x, y);
}
#else /* generic */
/* This routine performs a left circular arithmetic shift of <x> by <y> value. */
static inline word32 rotlFixed(word32 x, word32 y) {
return (x << y) | (x >> (sizeof(y) * 8 - y));
}
/* This routine performs a right circular arithmetic shift of <x> by <y> value. */
static inline word32 rotrFixed(word32 x, word32 y)
{
return (x >> y) | (x << (sizeof(y) * 8 - y));
}
#endif
static inline word16 ByteReverseWord16(word16 value)
{
#if defined(__ICCARM__)
return (word16)__REV16(value);
#elif defined(KEIL_INTRINSICS)
return (word16)__rev16(value);
#elif defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
return (word16)__builtin_bswap16(value);
#else
return (value >> 8) | (value << 8);
#endif
}
static inline word32 ByteReverseWord32(word32 value)
{
#if !defined(WOLF_NO_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
return (word32)__builtin_bswap32(value);
#elif defined(PPC_INTRINSICS)
/* PPC: load reverse indexed instruction */
return (word32)__lwbrx(&value,0);
#elif defined(__ICCARM__)
return (word32)__REV(value);
#elif defined(KEIL_INTRINSICS)
return (word32)__rev(value);
#elif defined(__CCRX__)
return (word32)_builtin_revl(value);
#elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
defined(__aarch64__)
__asm__ volatile (
"REV32 %0, %0 \n"
: "+r" (value)
:
);
return value;
#elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \
(defined(__thumb__) || defined(__arm__))
__asm__ volatile (
"REV %0, %0 \n"
: "+r" (value)
:
);
return value;
#elif defined(FAST_ROTATE)
/* 5 instructions with rotate instruction, 9 without */
return (rotrFixed(value, 8U) & 0xff00ff00) |
(rotlFixed(value, 8U) & 0x00ff00ff);
#else
/* 6 instructions with rotate instruction, 8 without */
value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
return rotlFixed(value, 16U);
#endif
}
static inline word64 ByteReverseWord64(word64 value)
{
#if !defined(WOLF_NO_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
return (word64)__builtin_bswap64(value);
#else
return (word64)((word64)ByteReverseWord32((word32)value)) << 32 |
(word64)ByteReverseWord32((word32)(value >> 32));
#endif
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* __TPM2_TYPES_H__ */