1 /* Copyright 2010 The ChromiumOS Authors
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #ifndef VBOOT_REFERENCE_TLCL_INTERNAL_H_
7 #define VBOOT_REFERENCE_TLCL_INTERNAL_H_
8
9 /*
10 * These numbers derive from adding the sizes of command fields as shown in the
11 * TPM commands manual.
12 */
13 #define kTpmRequestHeaderLength 10
14 #define kTpmResponseHeaderLength 10
15 #define kTpmReadInfoLength 12
16 #define kEncAuthLength 20
17 #define kPcrDigestLength 20
18 #define kTpmRequestAuthBlockLength \
19 (sizeof(uint32_t) + sizeof(TPM_NONCE) + 1 + TPM_AUTH_DATA_LEN)
20 #define kTpmResponseAuthBlockLength (sizeof(TPM_NONCE) + 1 + TPM_AUTH_DATA_LEN)
21
22
23 /*
24 * Conversion functions. ToTpmTYPE puts a value of type TYPE into a TPM
25 * command buffer. FromTpmTYPE gets a value of type TYPE from a TPM command
26 * buffer into a variable. ReadTpmTYPE reads a value of type TYPE from a buffer
27 * and advances the buffer pointer to after the field.
28 */
29 __attribute__((unused))
ToTpmUint32(uint8_t * buffer,uint32_t x)30 static inline void ToTpmUint32(uint8_t *buffer, uint32_t x) {
31 buffer[0] = (uint8_t)(x >> 24);
32 buffer[1] = (uint8_t)((x >> 16) & 0xff);
33 buffer[2] = (uint8_t)((x >> 8) & 0xff);
34 buffer[3] = (uint8_t)(x & 0xff);
35 }
36
37 /*
38 * See comment for above function.
39 */
40 __attribute__((unused))
FromTpmUint32(const uint8_t * buffer,uint32_t * x)41 static inline void FromTpmUint32(const uint8_t *buffer, uint32_t *x) {
42 *x = (((uint32_t)buffer[0] << 24) |
43 (buffer[1] << 16) |
44 (buffer[2] << 8) |
45 buffer[3]);
46 }
47
48 /*
49 * See comment for above function.
50 */
51 __attribute__((unused))
ReadTpmUint32(const uint8_t ** buffer)52 static inline uint32_t ReadTpmUint32(const uint8_t **buffer) {
53 uint32_t value;
54 FromTpmUint32(*buffer, &value);
55 *buffer += sizeof(value);
56 return value;
57 }
58
59 /*
60 * See comment for above function.
61 */
62 __attribute__((unused))
ToTpmUint16(uint8_t * buffer,uint16_t x)63 static inline void ToTpmUint16(uint8_t *buffer, uint16_t x) {
64 buffer[0] = (uint8_t)(x >> 8);
65 buffer[1] = (uint8_t)(x & 0xff);
66 }
67
68 /*
69 * See comment for above function.
70 */
71 __attribute__((unused))
FromTpmUint16(const uint8_t * buffer,uint16_t * x)72 static inline void FromTpmUint16(const uint8_t *buffer, uint16_t *x) {
73 *x = (buffer[0] << 8) | buffer[1];
74 }
75
76 /*
77 * See comment for above function.
78 */
79 __attribute__((unused))
ReadTpmUint16(const uint8_t ** buffer)80 static inline uint16_t ReadTpmUint16(const uint8_t **buffer) {
81 uint16_t value;
82 FromTpmUint16(*buffer, &value);
83 *buffer += sizeof(value);
84 return value;
85 }
86
87 #endif /* VBOOT_REFERENCE_TLCL_INTERNAL_H_ */
88