3 * Provides an interface for managing nonces to govern the lifetime of a
4 * personalization performed with TSS. A nonce managed by this interface may
5 * be used in a TSS signing request as the value for the BNCH tag.
7 * These interfaces require the caller to possess the
9 * com.apple.private.security.AppleImage4.user-client
13 * @section Threat Model
14 * The adversary possesses the following:
16 * 1. a manifest which was previously valid but has since been invalidated
17 * by rolling the nonce associated with it
18 * 2. user-level code execution
19 * 3. knowledge of the raw nonce value for the previously-valid manifest
21 * The defense against this adversary is a system in which knowledge of the raw
22 * nonce is insufficient to impact the evaluation of a personalization. This
23 * system has the following characteristics:
25 * 1. A nonce seed is stored in an nvram variable which is only writable by
27 * 2. When making a new signing request, the nonce seed is encrypted by a
28 * UID1-derived key in-kernel and then hashed -- the output of this
29 * operation the nonce to be used in the signing request
30 * 3. On boot, AppleImage4 obtains the nonce seed from nvram and stores it
31 * in a data structure which will be covered by KTRR
32 * 4. When evaluating a manifest, AppleImage4 reads the raw nonce from the
33 * KTRR-covered data structure and validates it with the same
34 * transformation as was done in (2)
35 * 5. When the nonce is to be invalidated, AppleImage4 sets a flag in an
36 * nvram variable which is only writable by the kernel
37 * 6. On the subsequent boot, AppleImage4 notices the flag, generates a new
38 * nonce and repeats the procedure in (3)
40 * In this system, the raw nonce seed never leaves the kernel, and the nonce
41 * itself is a non-reversible representation of the seed.
45 #ifndef __IMG4_NONCE_H
46 #define __IMG4_NONCE_H
48 #ifndef __IMG4_INDIRECT
49 #error "Please #include <img4/firmware.h> instead of this file directly"
50 #endif // __IMG4_INDIRECT
57 * @typedef img4_nonce_domain_t
58 * An opaque type describing a nonce domain.
60 IMG4_API_AVAILABLE_20181106
61 typedef struct _img4_nonce_domain img4_nonce_domain_t
;
64 * @const IMG4_NONCE_STRUCT_VERSION
65 * The version of the {@link img4_nonce_t} structure supported by the
68 #define IMG4_NONCE_STRUCT_VERSION ((img4_struct_version_t)0)
69 #define IMG4_NONCE_VERSION IMG4_NONCE_STRUCT_VERSION
72 * @const IMG4_NONCE_MAX_LENGTH
73 * The maximum length of a nonce. Currently, this is the length of a SHA2-384
76 #define IMG4_NONCE_MAX_LENGTH (48u)
79 * @typedef img4_nonce_t
80 * A structure describing a nonce.
83 * The version of the structure. When declaring this structure, you must
84 * initialize this field to {@link IMG4_NONCE_VERSION}.
87 * The bytes comprising the nonce.
90 * The length of the nonce. Will be at most {@link IMG4_NONCE_MAX_LENGTH}.
92 IMG4_API_AVAILABLE_20181106
93 typedef struct _img4_nonce
{
94 img4_struct_version_t i4n_version
;
95 const uint8_t i4n_nonce
[IMG4_NONCE_MAX_LENGTH
];
100 * @const IMG4_NONCE_INIT
101 * A convenience initializer for {@link img4_nonce_t} which ensures that the
102 * {@link i4n_version} field is properly initialized.
104 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
105 #define IMG4_NONCE_INIT (img4_nonce_t){.i4n_version = IMG4_NONCE_STRUCT_VERSION}
106 #elif defined(__cplusplus) && __cplusplus >= 201103L
107 #define IMG4_NONCE_INIT (img4_nonce_t{IMG4_NONCE_STRUCT_VERSION})
108 #elif defined(__cplusplus)
109 #define IMG4_NONCE_INIT \
110 (img4_nonce_t((img4_nonce_t){IMG4_NONCE_STRUCT_VERSION}))
112 #define IMG4_NONCE_INIT {IMG4_NONCE_STRUCT_VERSION}
116 * @const IMG4_NONCE_ZERO
117 * A convenience initializer for {@link img4_nonce_t} which initializes a 48-
118 * byte nonce of all zeroes.
120 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
121 #define IMG4_NONCE_ZERO (img4_nonce_t){ \
122 .i4n_version = IMG4_NONCE_STRUCT_VERSION, \
124 .i4n_length = IMG4_NONCE_MAX_LENGTH, \
129 * @const IMG4_NONCE_DOMAIN_TRUST_CACHE
130 * The nonce domain governing trust cache personalizations. Use of this domain
133 * com.apple.private.img4.nonce.trust-cache
137 #if !XNU_KERNEL_PRIVATE
138 IMG4_API_AVAILABLE_20181106
140 const struct _img4_nonce_domain _img4_nonce_domain_trust_cache
;
141 #define IMG4_NONCE_DOMAIN_TRUST_CACHE (&_img4_nonce_domain_trust_cache)
143 #define IMG4_NONCE_DOMAIN_TRUST_CACHE (img4if->i4if_v1.nonce_domain_trust_cache)
147 * @const IMG4_NONCE_DOMAIN_PDI
148 * The nonce domain governing disk image personalizations. Use of this domain
151 * com.apple.private.img4.nonce.pdi
153 * entitlement. The nonce for this domain is regenerated once every boot.
155 #if !XNU_KERNEL_PRIVATE
156 IMG4_API_AVAILABLE_20181106
158 const struct _img4_nonce_domain _img4_nonce_domain_pdi
;
159 #define IMG4_NONCE_DOMAIN_PDI (&_img4_nonce_domain_pdi)
161 #define IMG4_NONCE_DOMAIN_PDI (img4if->i4if_v3.nonce_domain_pdi)
165 * @const IMG4_NONCE_DOMAIN_CRYPTEX
166 * The nonce domain governing cryptex personalizations. Use of this domain
169 * com.apple.private.img4.nonce.cryptex
173 #if !XNU_KERNEL_PRIVATE
174 IMG4_API_AVAILABLE_20181106
176 const struct _img4_nonce_domain _img4_nonce_domain_cryptex
;
177 #define IMG4_NONCE_DOMAIN_CRYPTEX (&_img4_nonce_domain_cryptex)
179 #define IMG4_NONCE_DOMAIN_CRYPTEX (img4if->i4if_v3.nonce_domain_cryptex)
183 * @function img4_nonce_domain_copy_nonce
184 * Copies the current value of the nonce in the given domain.
190 * Upon successful return, storage that will contain the current nonce. The
191 * provided structure's {@link i4n_version} must be initialized to
192 * {@link IMG4_NONCE_VERSION}.
195 * Upon success, zero is returned. The implementation may also return one of the
196 * following error codes directly:
198 * [ESTALE] The nonce for the given domain has been invalidated, and the
199 * host must reboot in order to generate a new one
200 * [EPERM] The caller lacked the entitlement necessary to read the
203 #if !XNU_KERNEL_PRIVATE
204 IMG4_API_AVAILABLE_20181106
205 OS_EXPORT OS_WARN_RESULT OS_NONNULL1 OS_NONNULL2
207 img4_nonce_domain_copy_nonce(const img4_nonce_domain_t
*nd
, img4_nonce_t
*n
);
209 #define img4_nonce_domain_copy_nonce(...) \
210 (img4if->i4if_v1.nonce_domain_copy_nonce(__VA_ARGS__))
214 * @function img4_nonce_domain_roll_nonce
215 * Invalidates the current nonce for the given domain and forces a re-generation
216 * of the domain's nonce seed at the next boot.
222 * Upon success, zero is returned. The kernel implementation will never return
223 * a non-zero code. The userspace implementation may return one of the following
224 * error codes directly:
226 * [EPERM] The caller lacked the entitlement necessary to roll the
229 #if !XNU_KERNEL_PRIVATE
230 IMG4_API_AVAILABLE_20181106
231 OS_EXPORT OS_NONNULL1
233 img4_nonce_domain_roll_nonce(const img4_nonce_domain_t
*nd
);
235 #define img4_nonce_domain_roll_nonce(...) \
236 (img4if->i4if_v1.nonce_domain_roll_nonce(__VA_ARGS__))
239 #endif // __IMG4_NONCE_H