xref: /aosp_15_r20/hardware/interfaces/keymaster/4.0/IKeymasterDevice.hal (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1*4d7e907cSAndroid Build Coastguard Worker/*
2*4d7e907cSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*4d7e907cSAndroid Build Coastguard Worker *
4*4d7e907cSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*4d7e907cSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*4d7e907cSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*4d7e907cSAndroid Build Coastguard Worker *
8*4d7e907cSAndroid Build Coastguard Worker *      http://www.apache.org/licenses/LICENSE-2.0
9*4d7e907cSAndroid Build Coastguard Worker *
10*4d7e907cSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*4d7e907cSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*4d7e907cSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4d7e907cSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*4d7e907cSAndroid Build Coastguard Worker * limitations under the License.
15*4d7e907cSAndroid Build Coastguard Worker */
16*4d7e907cSAndroid Build Coastguard Worker
17*4d7e907cSAndroid Build Coastguard Workerpackage [email protected];
18*4d7e907cSAndroid Build Coastguard Worker
19*4d7e907cSAndroid Build Coastguard Workerimport [email protected]::ErrorCode;
20*4d7e907cSAndroid Build Coastguard Workerimport [email protected]::KeyFormat;
21*4d7e907cSAndroid Build Coastguard Worker
22*4d7e907cSAndroid Build Coastguard Worker/**
23*4d7e907cSAndroid Build Coastguard Worker * Keymaster device definition.
24*4d7e907cSAndroid Build Coastguard Worker *
25*4d7e907cSAndroid Build Coastguard Worker * == Features ==
26*4d7e907cSAndroid Build Coastguard Worker *
27*4d7e907cSAndroid Build Coastguard Worker * An IKeymasterDevice provides cryptographic services, including the following categories of
28*4d7e907cSAndroid Build Coastguard Worker * operations:
29*4d7e907cSAndroid Build Coastguard Worker *
30*4d7e907cSAndroid Build Coastguard Worker * o   Key generation
31*4d7e907cSAndroid Build Coastguard Worker * o   Import and export (public only) of asymmetric keys
32*4d7e907cSAndroid Build Coastguard Worker * o   Import of raw symmetric keys
33*4d7e907cSAndroid Build Coastguard Worker * o   Asymmetric encryption and decryption with appropriate padding modes
34*4d7e907cSAndroid Build Coastguard Worker * o   Asymmetric signing and verification with digesting and appropriate padding modes
35*4d7e907cSAndroid Build Coastguard Worker * o   Symmetric encryption and decryption in appropriate modes, including an AEAD mode
36*4d7e907cSAndroid Build Coastguard Worker * o   Generation and verification of symmetric message authentication codes
37*4d7e907cSAndroid Build Coastguard Worker * o   Attestation to the presence and configuration of asymmetric keys.
38*4d7e907cSAndroid Build Coastguard Worker *
39*4d7e907cSAndroid Build Coastguard Worker * Protocol elements, such as purpose, mode and padding, as well as access control constraints, must
40*4d7e907cSAndroid Build Coastguard Worker * be specified by the caller when keys are generated or imported and must be permanently bound to
41*4d7e907cSAndroid Build Coastguard Worker * the key, ensuring that the key cannot be used in any other way.
42*4d7e907cSAndroid Build Coastguard Worker *
43*4d7e907cSAndroid Build Coastguard Worker * In addition to the list above, IKeymasterDevice implementations must provide one more service
44*4d7e907cSAndroid Build Coastguard Worker * which is not exposed as an API but used internally: Random number generation.  The random number
45*4d7e907cSAndroid Build Coastguard Worker * generator must be high-quality and must be used for generation of keys, initialization vectors,
46*4d7e907cSAndroid Build Coastguard Worker * random padding and other elements of secure protocols that require randomness.
47*4d7e907cSAndroid Build Coastguard Worker *
48*4d7e907cSAndroid Build Coastguard Worker * == Types of IKeymasterDevices ==
49*4d7e907cSAndroid Build Coastguard Worker *
50*4d7e907cSAndroid Build Coastguard Worker * All of the operations and storage of key material must occur in a secure environment.  Secure
51*4d7e907cSAndroid Build Coastguard Worker * environments may be either:
52*4d7e907cSAndroid Build Coastguard Worker *
53*4d7e907cSAndroid Build Coastguard Worker * 1.  Isolated execution environments, such as a separate virtual machine, hypervisor or
54*4d7e907cSAndroid Build Coastguard Worker *      purpose-built trusted execution environment like ARM TrustZone.  The isolated environment
55*4d7e907cSAndroid Build Coastguard Worker *      must provide complete separation from the Android kernel and user space (collectively called
56*4d7e907cSAndroid Build Coastguard Worker *      the "non-secure world", or NSW) so that nothing running in the NSW can observe or manipulate
57*4d7e907cSAndroid Build Coastguard Worker *      the results of any computation in the isolated environment.  Isolated execution environments
58*4d7e907cSAndroid Build Coastguard Worker *      are identified by the SecurityLevel TRUSTED_ENVIRONMENT.
59*4d7e907cSAndroid Build Coastguard Worker *
60*4d7e907cSAndroid Build Coastguard Worker * 2.  Completely separate, purpose-built and certified secure CPUs, called "StrongBox" devices.
61*4d7e907cSAndroid Build Coastguard Worker *      Examples of StrongBox devices are embedded Secure Elements (eSE) or on-SoC secure processing
62*4d7e907cSAndroid Build Coastguard Worker *      units (SPU).  StrongBox environments are identified by the SecurityLevel STRONGBOX.  To
63*4d7e907cSAndroid Build Coastguard Worker *      qualify as a StrongBox, a device must meet the requirements specified in CDD 9.11.2.
64*4d7e907cSAndroid Build Coastguard Worker *
65*4d7e907cSAndroid Build Coastguard Worker * == Necessary Primitives ==
66*4d7e907cSAndroid Build Coastguard Worker *
67*4d7e907cSAndroid Build Coastguard Worker * All IKeymasterDevice implementations must provide support for the following:
68*4d7e907cSAndroid Build Coastguard Worker *
69*4d7e907cSAndroid Build Coastguard Worker * o   RSA
70*4d7e907cSAndroid Build Coastguard Worker *
71*4d7e907cSAndroid Build Coastguard Worker *      - TRUSTED_ENVIRONMENT IKeymasterDevices must support 2048, 3072 and 4096-bit keys.
72*4d7e907cSAndroid Build Coastguard Worker *        STRONGBOX IKeymasterDevices must support 2048-bit keys.
73*4d7e907cSAndroid Build Coastguard Worker *      - Public exponent F4 (2^16+1)
74*4d7e907cSAndroid Build Coastguard Worker *      - Unpadded, RSASSA-PSS and RSASSA-PKCS1-v1_5 padding modes for RSA signing
75*4d7e907cSAndroid Build Coastguard Worker *      - TRUSTED_ENVIRONMENT IKeymasterDevices must support MD5, SHA1, SHA-2 224, SHA-2 256, SHA-2
76*4d7e907cSAndroid Build Coastguard Worker *        384 and SHA-2 512 digest modes for RSA signing.  STRONGBOX IKeymasterDevices must support
77*4d7e907cSAndroid Build Coastguard Worker *        SHA-2 256.
78*4d7e907cSAndroid Build Coastguard Worker *      - Unpadded, RSAES-OAEP and RSAES-PKCS1-v1_5 padding modes for RSA encryption.
79*4d7e907cSAndroid Build Coastguard Worker *
80*4d7e907cSAndroid Build Coastguard Worker * o   ECDSA
81*4d7e907cSAndroid Build Coastguard Worker *
82*4d7e907cSAndroid Build Coastguard Worker *      - TRUSTED_ENVIRONMENT IKeymasterDevices must support NIST curves P-224, P-256, P-384 and
83*4d7e907cSAndroid Build Coastguard Worker *        P-521.  STRONGBOX IKeymasterDevices must support NIST curve P-256.
84*4d7e907cSAndroid Build Coastguard Worker *      - TRUSTED_ENVIRONMENT IKeymasterDevices must support SHA1, SHA-2 224, SHA-2 256, SHA-2
85*4d7e907cSAndroid Build Coastguard Worker *        384 and SHA-2 512 digest modes.  STRONGBOX IKeymasterDevices must support SHA-2 256.
86*4d7e907cSAndroid Build Coastguard Worker *
87*4d7e907cSAndroid Build Coastguard Worker * o   AES
88*4d7e907cSAndroid Build Coastguard Worker *
89*4d7e907cSAndroid Build Coastguard Worker *      - 128 and 256-bit keys
90*4d7e907cSAndroid Build Coastguard Worker *      - CBC, CTR, ECB and GCM modes.  The GCM mode must not allow the use of tags smaller than 96
91*4d7e907cSAndroid Build Coastguard Worker *        bits or nonce lengths other than 96 bits.
92*4d7e907cSAndroid Build Coastguard Worker *      - CBC and ECB modes must support unpadded and PKCS7 padding modes.  With no padding CBC and
93*4d7e907cSAndroid Build Coastguard Worker *        ECB-mode operations must fail with ErrorCode::INVALID_INPUT_LENGTH if the input isn't a
94*4d7e907cSAndroid Build Coastguard Worker *        multiple of the AES block size.  With PKCS7 padding, GCM and CTR operations must fail with
95*4d7e907cSAndroid Build Coastguard Worker *        ErrorCode::INCOMPATIBLE_PADDING_MODE.
96*4d7e907cSAndroid Build Coastguard Worker *
97*4d7e907cSAndroid Build Coastguard Worker * o   3DES
98*4d7e907cSAndroid Build Coastguard Worker *
99*4d7e907cSAndroid Build Coastguard Worker *      - 168-bit keys.
100*4d7e907cSAndroid Build Coastguard Worker *      - CBC and ECB mode.
101*4d7e907cSAndroid Build Coastguard Worker
102*4d7e907cSAndroid Build Coastguard Worker *      - CBC and ECB modes must support unpadded and PKCS7 padding modes.  With no padding CBC and
103*4d7e907cSAndroid Build Coastguard Worker *        ECB-mode operations must fail with ErrorCode::INVALID_INPUT_LENGTH if the input isn't a
104*4d7e907cSAndroid Build Coastguard Worker *        multiple of the DES block size.
105*4d7e907cSAndroid Build Coastguard Worker *
106*4d7e907cSAndroid Build Coastguard Worker * o   HMAC
107*4d7e907cSAndroid Build Coastguard Worker *
108*4d7e907cSAndroid Build Coastguard Worker *      - Any key size that is between 64 and 512 bits (inclusive) and a multiple of 8 must be
109*4d7e907cSAndroid Build Coastguard Worker *        supported.  STRONGBOX IKeymasterDevices must not support keys larger than 512 bits.
110*4d7e907cSAndroid Build Coastguard Worker *      - TRUSTED_ENVIRONMENT IKeymasterDevices must support MD-5, SHA1, SHA-2-224, SHA-2-256,
111*4d7e907cSAndroid Build Coastguard Worker *        SHA-2-384 and SHA-2-512.  STRONGBOX IKeymasterDevices must support SHA-2-256.
112*4d7e907cSAndroid Build Coastguard Worker *
113*4d7e907cSAndroid Build Coastguard Worker * == Key Access Control ==
114*4d7e907cSAndroid Build Coastguard Worker *
115*4d7e907cSAndroid Build Coastguard Worker * Hardware-based keys that can never be extracted from the device don't provide much security if an
116*4d7e907cSAndroid Build Coastguard Worker * attacker can use them at will (though they're more secure than keys which can be
117*4d7e907cSAndroid Build Coastguard Worker * exfiltrated).  Therefore, IKeymasterDevice must enforce access controls.
118*4d7e907cSAndroid Build Coastguard Worker *
119*4d7e907cSAndroid Build Coastguard Worker * Access controls are defined as an "authorization list" of tag/value pairs.  Authorization tags
120*4d7e907cSAndroid Build Coastguard Worker * are 32-bit integers from the Tag enum, and the values are a variety of types, defined in the
121*4d7e907cSAndroid Build Coastguard Worker * TagType enum.  Some tags may be repeated to specify multiple values.  Whether a tag may be
122*4d7e907cSAndroid Build Coastguard Worker * repeated is specified in the documentation for the tag and in the TagType.  When a key is created
123*4d7e907cSAndroid Build Coastguard Worker * or imported, the caller specifies an authorization list.  The IKeymasterDevice must divide the
124*4d7e907cSAndroid Build Coastguard Worker * caller-provided authorizations into two lists, those it enforces in hardware and those it does
125*4d7e907cSAndroid Build Coastguard Worker * not.  These two lists are returned as the "hardwareEnforced" and "softwareEnforced" elements of
126*4d7e907cSAndroid Build Coastguard Worker * the KeyCharacteristics struct.  The IKeymasterDevice must also add the following authorizations
127*4d7e907cSAndroid Build Coastguard Worker * to the appropriate list:
128*4d7e907cSAndroid Build Coastguard Worker *
129*4d7e907cSAndroid Build Coastguard Worker * o    Tag::OS_VERSION, must be hardware-enforced.
130*4d7e907cSAndroid Build Coastguard Worker * o    Tag::OS_PATCHLEVEL, must be hardware-enforced.
131*4d7e907cSAndroid Build Coastguard Worker * o    Tag::VENDOR_PATCHLEVEL, must be hardware-enforced.
132*4d7e907cSAndroid Build Coastguard Worker * o    Tag::BOOT_PATCHLEVEL, must be hardware-enforced.
133*4d7e907cSAndroid Build Coastguard Worker * o    Tag::CREATION_DATETIME, must be software-enforced, unless the IKeymasterDevice has access to
134*4d7e907cSAndroid Build Coastguard Worker *      a secure time service.
135*4d7e907cSAndroid Build Coastguard Worker * o    Tag::ORIGIN, must be hardware-enforced.
136*4d7e907cSAndroid Build Coastguard Worker *
137*4d7e907cSAndroid Build Coastguard Worker * The IKeymasterDevice must accept arbitrary, unknown tags and return them in the softwareEnforced
138*4d7e907cSAndroid Build Coastguard Worker * list.
139*4d7e907cSAndroid Build Coastguard Worker *
140*4d7e907cSAndroid Build Coastguard Worker * All authorization tags and their values, both hardwareEnforced and softwareEnforced, including
141*4d7e907cSAndroid Build Coastguard Worker * unknown tags, must be cryptographically bound to the private/secret key material such that any
142*4d7e907cSAndroid Build Coastguard Worker * modification of the portion of the key blob that contains the authorization list makes it
143*4d7e907cSAndroid Build Coastguard Worker * impossible for the secure environment to obtain the private/secret key material.  The recommended
144*4d7e907cSAndroid Build Coastguard Worker * approach to meet this requirement is to use the full set of authorization tags associated with a
145*4d7e907cSAndroid Build Coastguard Worker * key as input to a secure key derivation function used to derive a key that is used to encrypt the
146*4d7e907cSAndroid Build Coastguard Worker * private/secret key material.
147*4d7e907cSAndroid Build Coastguard Worker *
148*4d7e907cSAndroid Build Coastguard Worker * IKeymasterDevice implementations must place any tags they cannot fully and completely enforce in
149*4d7e907cSAndroid Build Coastguard Worker * the softwareEnforced list.  For example, Tag::ORIGINATION_EXPIRE_DATETIME provides the date and
150*4d7e907cSAndroid Build Coastguard Worker * time after which a key may not be used to encrypt or sign new messages.  Unless the
151*4d7e907cSAndroid Build Coastguard Worker * IKeymasterDevice has access to a secure source of current date/time information, it is not
152*4d7e907cSAndroid Build Coastguard Worker * possible for the IKeymasterDevice to enforce this tag.  An IKeymasterDevice implementation may
153*4d7e907cSAndroid Build Coastguard Worker * not rely on the non-secure world's notion of time, because it could be controlled by an attacker.
154*4d7e907cSAndroid Build Coastguard Worker * Similarly, it cannot rely on GPSr time, even if it has exclusive control of the GPSr, because
155*4d7e907cSAndroid Build Coastguard Worker * that might be spoofed by attacker RF signals.
156*4d7e907cSAndroid Build Coastguard Worker *
157*4d7e907cSAndroid Build Coastguard Worker * It is recommended that IKeymasterDevices not enforce any tags they place in the softwareEnforced
158*4d7e907cSAndroid Build Coastguard Worker * list.  The IKeymasterDevice caller must enforce them, and it is unnecessary to enforce them
159*4d7e907cSAndroid Build Coastguard Worker * twice.
160*4d7e907cSAndroid Build Coastguard Worker *
161*4d7e907cSAndroid Build Coastguard Worker * Some tags must be enforced by the IKeymasterDevice.  See the detailed documentation on each Tag
162*4d7e907cSAndroid Build Coastguard Worker * in types.hal.
163*4d7e907cSAndroid Build Coastguard Worker *
164*4d7e907cSAndroid Build Coastguard Worker * == Root of Trust Binding ==
165*4d7e907cSAndroid Build Coastguard Worker *
166*4d7e907cSAndroid Build Coastguard Worker * IKeymasterDevice keys must be bound to a root of trust, which is a bitstring that must be
167*4d7e907cSAndroid Build Coastguard Worker * provided to the secure environment (by an unspecified, implementation-defined mechanism) during
168*4d7e907cSAndroid Build Coastguard Worker * startup, preferably by the bootloader.  This bitstring must be cryptographically bound to every
169*4d7e907cSAndroid Build Coastguard Worker * key managed by the IKeymasterDevice.  As above, the recommended mechanism for this cryptographic
170*4d7e907cSAndroid Build Coastguard Worker * binding is to include the Root of Trust data in the input to the key derivation function used to
171*4d7e907cSAndroid Build Coastguard Worker * derive a key that is used to encrypt the private/secret key material.
172*4d7e907cSAndroid Build Coastguard Worker *
173*4d7e907cSAndroid Build Coastguard Worker * The root of trust consists of a bitstring that must be derived from the public key used by
174*4d7e907cSAndroid Build Coastguard Worker * Verified Boot to verify the signature on the boot image and from the the lock state of the
175*4d7e907cSAndroid Build Coastguard Worker * device.  If the public key is changed to allow a different system image to be used or if the lock
176*4d7e907cSAndroid Build Coastguard Worker * state is changed, then all of the IKeymasterDevice-protected keys created by the previous system
177*4d7e907cSAndroid Build Coastguard Worker * state must be unusable, unless the previous state is restored.  The goal is to increase the value
178*4d7e907cSAndroid Build Coastguard Worker * of the software-enforced key access controls by making it impossible for an attacker-installed
179*4d7e907cSAndroid Build Coastguard Worker * operating system to use IKeymasterDevice keys.
180*4d7e907cSAndroid Build Coastguard Worker *
181*4d7e907cSAndroid Build Coastguard Worker * == Version Binding ==
182*4d7e907cSAndroid Build Coastguard Worker *
183*4d7e907cSAndroid Build Coastguard Worker * All keys must also be bound to the operating system and patch level of the system image and the
184*4d7e907cSAndroid Build Coastguard Worker * patch levels of the vendor image and boot image.  This ensures that an attacker who discovers a
185*4d7e907cSAndroid Build Coastguard Worker * weakness in an old version of the software cannot roll a device back to the vulnerable version
186*4d7e907cSAndroid Build Coastguard Worker * and use keys created with the newer version.  In addition, when a key with a given version and
187*4d7e907cSAndroid Build Coastguard Worker * patch level is used on a device that has been upgraded to a newer version or patch level, the key
188*4d7e907cSAndroid Build Coastguard Worker * must be upgraded (See IKeymasterDevice::upgradeKey()) before it can be used, and the previous
189*4d7e907cSAndroid Build Coastguard Worker * version of the key must be invalidated.  In this way, as the device is upgraded, the keys will
190*4d7e907cSAndroid Build Coastguard Worker * "ratchet" forward along with the device, but any reversion of the device to a previous release
191*4d7e907cSAndroid Build Coastguard Worker * will cause the keys to be unusable.
192*4d7e907cSAndroid Build Coastguard Worker *
193*4d7e907cSAndroid Build Coastguard Worker * This version information must be associated with every key as a set of tag/value pairs in the
194*4d7e907cSAndroid Build Coastguard Worker * hardwareEnforced authorization list.  Tag::OS_VERSION, Tag::OS_PATCHLEVEL,
195*4d7e907cSAndroid Build Coastguard Worker * Tag::VENDOR_PATCHLEVEL, and Tag::BOOT_PATCHLEVEL must be cryptographically bound to every
196*4d7e907cSAndroid Build Coastguard Worker * IKeymasterDevice key, as described in the Key Access Control section above.
197*4d7e907cSAndroid Build Coastguard Worker */
198*4d7e907cSAndroid Build Coastguard Worker@SensitiveData
199*4d7e907cSAndroid Build Coastguard Workerinterface IKeymasterDevice {
200*4d7e907cSAndroid Build Coastguard Worker
201*4d7e907cSAndroid Build Coastguard Worker    /**
202*4d7e907cSAndroid Build Coastguard Worker     * Returns information about the underlying IKeymasterDevice hardware.
203*4d7e907cSAndroid Build Coastguard Worker     *
204*4d7e907cSAndroid Build Coastguard Worker     * @return security level of the IKeymasterDevice implementation accessed through this HAL.
205*4d7e907cSAndroid Build Coastguard Worker     *
206*4d7e907cSAndroid Build Coastguard Worker     * @return keymasterName is the name of the IKeymasterDevice implementation.
207*4d7e907cSAndroid Build Coastguard Worker     *
208*4d7e907cSAndroid Build Coastguard Worker     * @return keymasterAuthorName is the name of the author of the IKeymasterDevice implementation
209*4d7e907cSAndroid Build Coastguard Worker     *         (organization name, not individual).
210*4d7e907cSAndroid Build Coastguard Worker     */
211*4d7e907cSAndroid Build Coastguard Worker    getHardwareInfo()
212*4d7e907cSAndroid Build Coastguard Worker        generates (SecurityLevel securityLevel, string keymasterName, string keymasterAuthorName);
213*4d7e907cSAndroid Build Coastguard Worker
214*4d7e907cSAndroid Build Coastguard Worker    /**
215*4d7e907cSAndroid Build Coastguard Worker     * Start the creation of an HMAC key, shared with another IKeymasterDevice implementation.  Any
216*4d7e907cSAndroid Build Coastguard Worker     * device with a StrongBox IKeymasterDevice has two IKeymasterDevice instances, because there
217*4d7e907cSAndroid Build Coastguard Worker     * must be a TEE Keymaster as well.  The HMAC key used to MAC and verify authentication tokens
218*4d7e907cSAndroid Build Coastguard Worker     * (HardwareAuthToken, VerificationToken and ConfirmationToken all use this HMAC key) must be
219*4d7e907cSAndroid Build Coastguard Worker     * shared between TEE and StrongBox so they can each validate tokens produced by the other.
220*4d7e907cSAndroid Build Coastguard Worker     * This method is the first step in the process for agreeing on a shared key.  It is called by
221*4d7e907cSAndroid Build Coastguard Worker     * Android during startup.  The system calls it on each of the HAL instances and collects the
222*4d7e907cSAndroid Build Coastguard Worker     * results in preparation for the second step.
223*4d7e907cSAndroid Build Coastguard Worker     *
224*4d7e907cSAndroid Build Coastguard Worker     * @return error ErrorCode::OK on success, ErrorCode::UNIMPLEMENTED if HMAC agreement is not
225*4d7e907cSAndroid Build Coastguard Worker     *         implemented (note that all 4.0::IKeymasterDevice HALS must implement HMAC agreement,
226*4d7e907cSAndroid Build Coastguard Worker     *         regardless of whether or not the HAL will be used on a device with StrongBox), or
227*4d7e907cSAndroid Build Coastguard Worker     *         ErrorCode::UNKNOWN_ERROR if the parameters cannot be returned.
228*4d7e907cSAndroid Build Coastguard Worker     *
229*4d7e907cSAndroid Build Coastguard Worker     * @return params The HmacSharingParameters to use.  As specified in the HmacSharingParameters
230*4d7e907cSAndroid Build Coastguard Worker     *         documentation in types.hal, the seed must contain the same value in every invocation
231*4d7e907cSAndroid Build Coastguard Worker     *         of the method on a given device, and the nonce must return the same value for every
232*4d7e907cSAndroid Build Coastguard Worker     *         invocation during a boot session.
233*4d7e907cSAndroid Build Coastguard Worker     */
234*4d7e907cSAndroid Build Coastguard Worker    getHmacSharingParameters() generates (ErrorCode error, HmacSharingParameters params);
235*4d7e907cSAndroid Build Coastguard Worker
236*4d7e907cSAndroid Build Coastguard Worker    /**
237*4d7e907cSAndroid Build Coastguard Worker     * Complete the creation of an HMAC key, shared with another IKeymasterDevice implementation.
238*4d7e907cSAndroid Build Coastguard Worker     * Any device with a StrongBox IKeymasterDevice has two IKeymasterDevice instances, because
239*4d7e907cSAndroid Build Coastguard Worker     * there must be a TEE IKeymasterDevice as well.  The HMAC key used to MAC and verify
240*4d7e907cSAndroid Build Coastguard Worker     * authentication tokens must be shared between TEE and StrongBox so they can each validate
241*4d7e907cSAndroid Build Coastguard Worker     * tokens produced by the other.  This method is the second and final step in the process for
242*4d7e907cSAndroid Build Coastguard Worker     * agreeing on a shared key.  It is called by Android during startup.  The system calls it on
243*4d7e907cSAndroid Build Coastguard Worker     * each of the HAL instances, and sends to it all of the HmacSharingParameters returned by all
244*4d7e907cSAndroid Build Coastguard Worker     * HALs.
245*4d7e907cSAndroid Build Coastguard Worker     *
246*4d7e907cSAndroid Build Coastguard Worker     * To ensure consistent ordering of the HmacSharingParameters, the caller must sort the
247*4d7e907cSAndroid Build Coastguard Worker     * parameters lexicographically.  See the support/keymaster_utils.cpp for an operator< that
248*4d7e907cSAndroid Build Coastguard Worker     * defines the appropriate ordering.
249*4d7e907cSAndroid Build Coastguard Worker     *
250*4d7e907cSAndroid Build Coastguard Worker     * This method computes the shared 32-byte HMAC ``H'' as follows (all IKeymasterDevice instances
251*4d7e907cSAndroid Build Coastguard Worker     * perform the same computation to arrive at the same result):
252*4d7e907cSAndroid Build Coastguard Worker     *
253*4d7e907cSAndroid Build Coastguard Worker     *     H = CKDF(key = K,
254*4d7e907cSAndroid Build Coastguard Worker     *              context = P1 || P2 || ... || Pn,
255*4d7e907cSAndroid Build Coastguard Worker     *              label = "KeymasterSharedMac")
256*4d7e907cSAndroid Build Coastguard Worker     *
257*4d7e907cSAndroid Build Coastguard Worker     * where:
258*4d7e907cSAndroid Build Coastguard Worker     *
259*4d7e907cSAndroid Build Coastguard Worker     *     ``CKDF'' is the standard AES-CMAC KDF from NIST SP 800-108 in counter mode (see Section
260*4d7e907cSAndroid Build Coastguard Worker     *           5.1 of the referenced publication).  ``key'', ``context'', and ``label'' are
261*4d7e907cSAndroid Build Coastguard Worker     *           defined in the standard.  The counter is prefixed and length L appended, as shown
262*4d7e907cSAndroid Build Coastguard Worker     *           in the construction on page 12 of the standard.  The label string is UTF-8 encoded.
263*4d7e907cSAndroid Build Coastguard Worker     *
264*4d7e907cSAndroid Build Coastguard Worker     *     ``K'' is a pre-established shared secret, set up during factory reset.  The mechanism for
265*4d7e907cSAndroid Build Coastguard Worker     *           establishing this shared secret is implementation-defined, but see below for a
266*4d7e907cSAndroid Build Coastguard Worker     *           recommended approach, which assumes that the TEE IKeymasterDevice does not have
267*4d7e907cSAndroid Build Coastguard Worker     *           storage available to it, but the StrongBox IKeymasterDevice does.
268*4d7e907cSAndroid Build Coastguard Worker     *
269*4d7e907cSAndroid Build Coastguard Worker     *           CRITICAL SECURITY REQUIREMENT: All keys created by a IKeymasterDevice instance must
270*4d7e907cSAndroid Build Coastguard Worker     *           be cryptographically bound to the value of K, such that establishing a new K
271*4d7e907cSAndroid Build Coastguard Worker     *           permanently destroys them.
272*4d7e907cSAndroid Build Coastguard Worker     *
273*4d7e907cSAndroid Build Coastguard Worker     *     ``||'' represents concatenation.
274*4d7e907cSAndroid Build Coastguard Worker     *
275*4d7e907cSAndroid Build Coastguard Worker     *     ``Pi'' is the i'th HmacSharingParameters value in the params vector.  Note that at
276*4d7e907cSAndroid Build Coastguard Worker     *           present only two IKeymasterDevice implementations are supported, but this mechanism
277*4d7e907cSAndroid Build Coastguard Worker     *           extends without modification to any number of implementations.  Encoding of an
278*4d7e907cSAndroid Build Coastguard Worker     *           HmacSharingParameters is the concatenation of its two fields, i.e. seed || nonce.
279*4d7e907cSAndroid Build Coastguard Worker     *
280*4d7e907cSAndroid Build Coastguard Worker     * Note that the label "KeymasterSharedMac" is the 18-byte UTF-8 encoding of the string.
281*4d7e907cSAndroid Build Coastguard Worker     *
282*4d7e907cSAndroid Build Coastguard Worker     * Process for establishing K:
283*4d7e907cSAndroid Build Coastguard Worker     *
284*4d7e907cSAndroid Build Coastguard Worker     *     Any method of securely establishing K that ensures that an attacker cannot obtain or
285*4d7e907cSAndroid Build Coastguard Worker     *     derive its value is acceptable.  What follows is a recommended approach, to be executed
286*4d7e907cSAndroid Build Coastguard Worker     *     during each factory reset.  It relies on use of the factory-installed attestation keys to
287*4d7e907cSAndroid Build Coastguard Worker     *     mitigate man-in-the-middle attacks.  This protocol requires that one of the instances
288*4d7e907cSAndroid Build Coastguard Worker     *     have secure persistent storage.  This model was chosen because StrongBox has secure
289*4d7e907cSAndroid Build Coastguard Worker     *     persistent storage (by definition), but the TEE may not.  The instance without storage is
290*4d7e907cSAndroid Build Coastguard Worker     *     assumed to be able to derive a unique hardware-bound key (HBK) which is used only for
291*4d7e907cSAndroid Build Coastguard Worker     *     this purpose, and is not derivable outside the secure environment.
292*4d7e907cSAndroid Build Coastguard Worker     *
293*4d7e907cSAndroid Build Coastguard Worker     *     In what follows, T is the IKeymasterDevice instance without storage, S is the
294*4d7e907cSAndroid Build Coastguard Worker     *     IKeymasterDevice instance with storage:
295*4d7e907cSAndroid Build Coastguard Worker     *
296*4d7e907cSAndroid Build Coastguard Worker     *     1. T generates an ephemeral EC P-256 key pair K1.
297*4d7e907cSAndroid Build Coastguard Worker     *     2. T sends K1_pub to S, signed with T's attestation key.
298*4d7e907cSAndroid Build Coastguard Worker     *     3. S validates the signature on K1_pub.
299*4d7e907cSAndroid Build Coastguard Worker     *     4. S generates an ephemeral EC P-256 key pair K2.
300*4d7e907cSAndroid Build Coastguard Worker     *     5. S sends {K1_pub, K2_pub}, to T, signed with S's attestation key.
301*4d7e907cSAndroid Build Coastguard Worker     *     6. T validates the signature on {K1_pub, K2_pub}.
302*4d7e907cSAndroid Build Coastguard Worker     *     7. T uses {K1_priv, K2_pub} with ECDH to compute session secret Q.
303*4d7e907cSAndroid Build Coastguard Worker     *     8. T generates a random seed S.
304*4d7e907cSAndroid Build Coastguard Worker     *     9. T computes K = KDF(HBK, S), where KDF is some secure key derivation function.
305*4d7e907cSAndroid Build Coastguard Worker     *     10. T sends M = AES-GCM-ENCRYPT(Q, {S || K}) to S.
306*4d7e907cSAndroid Build Coastguard Worker     *     10. S uses {K2_priv, K1_pub} with ECDH to compute session secret Q.
307*4d7e907cSAndroid Build Coastguard Worker     *     11. S computes S || K = AES-GCM-DECRYPT(Q, M) and stores S and K.
308*4d7e907cSAndroid Build Coastguard Worker     *
309*4d7e907cSAndroid Build Coastguard Worker     *     When S receives the getHmacSharingParameters call, it returns the stored S as the seed
310*4d7e907cSAndroid Build Coastguard Worker     *     and a nonce.  When T receives the same call, it returns an empty seed and a nonce.  When
311*4d7e907cSAndroid Build Coastguard Worker     *     T receives the computeSharedHmac call, it uses the seed provided by S to compute K.  S,
312*4d7e907cSAndroid Build Coastguard Worker     *     of course, has K stored.
313*4d7e907cSAndroid Build Coastguard Worker     *
314*4d7e907cSAndroid Build Coastguard Worker     * @param params The HmacSharingParameters data returned by all IKeymasterDevice instances when
315*4d7e907cSAndroid Build Coastguard Worker     *        getHmacSharingParameters was called.
316*4d7e907cSAndroid Build Coastguard Worker     *
317*4d7e907cSAndroid Build Coastguard Worker     * @return error ErrorCode::OK in the event that there is no error.  ErrorCode::INVALID_ARGUMENT
318*4d7e907cSAndroid Build Coastguard Worker     *         if one of the provided parameters is not the value returned by the prior call to
319*4d7e907cSAndroid Build Coastguard Worker     *         getHmacParameters().
320*4d7e907cSAndroid Build Coastguard Worker     *
321*4d7e907cSAndroid Build Coastguard Worker     * @return sharingCheck A 32-byte value used to verify that all IKeymasterDevice instances have
322*4d7e907cSAndroid Build Coastguard Worker     *         computed the same shared HMAC key.  The sharingCheck value is computed as follows:
323*4d7e907cSAndroid Build Coastguard Worker     *
324*4d7e907cSAndroid Build Coastguard Worker     *             sharingCheck = HMAC(H, "Keymaster HMAC Verification")
325*4d7e907cSAndroid Build Coastguard Worker     *
326*4d7e907cSAndroid Build Coastguard Worker     *         The string is UTF-8 encoded, 27 bytes in length.  If the returned values of all
327*4d7e907cSAndroid Build Coastguard Worker     *         IKeymasterDevice instances don't match, clients must assume that HMAC agreement
328*4d7e907cSAndroid Build Coastguard Worker     *         failed.
329*4d7e907cSAndroid Build Coastguard Worker     */
330*4d7e907cSAndroid Build Coastguard Worker    computeSharedHmac(vec<HmacSharingParameters> params)
331*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<uint8_t> sharingCheck);
332*4d7e907cSAndroid Build Coastguard Worker
333*4d7e907cSAndroid Build Coastguard Worker    /**
334*4d7e907cSAndroid Build Coastguard Worker     * Verify authorizations for another IKeymasterDevice instance.
335*4d7e907cSAndroid Build Coastguard Worker     *
336*4d7e907cSAndroid Build Coastguard Worker     * On systems with both a StrongBox and a TEE IKeymasterDevice instance it is sometimes useful
337*4d7e907cSAndroid Build Coastguard Worker     * to ask the TEE IKeymasterDevice to verify authorizations for a key hosted in StrongBox.
338*4d7e907cSAndroid Build Coastguard Worker     *
339*4d7e907cSAndroid Build Coastguard Worker     * For every StrongBox operation, Keystore is required to call this method on the TEE Keymaster,
340*4d7e907cSAndroid Build Coastguard Worker     * passing in the StrongBox key's hardwareEnforced authorization list and the operation handle
341*4d7e907cSAndroid Build Coastguard Worker     * returned by StrongBox begin().  The TEE IKeymasterDevice must validate all of the
342*4d7e907cSAndroid Build Coastguard Worker     * authorizations it can and return those it validated in the VerificationToken.  If it cannot
343*4d7e907cSAndroid Build Coastguard Worker     * verify any, the parametersVerified field of the VerificationToken must be empty.  Keystore
344*4d7e907cSAndroid Build Coastguard Worker     * must then pass the VerificationToken to the subsequent invocations of StrongBox update() and
345*4d7e907cSAndroid Build Coastguard Worker     * finish().
346*4d7e907cSAndroid Build Coastguard Worker     *
347*4d7e907cSAndroid Build Coastguard Worker     * StrongBox implementations must return ErrorCode::UNIMPLEMENTED.
348*4d7e907cSAndroid Build Coastguard Worker     *
349*4d7e907cSAndroid Build Coastguard Worker     * @param operationHandle the operation handle returned by StrongBox Keymaster's begin().
350*4d7e907cSAndroid Build Coastguard Worker     *
351*4d7e907cSAndroid Build Coastguard Worker     * @param parametersToVerify Set of authorizations to verify.  The caller may provide an empty
352*4d7e907cSAndroid Build Coastguard Worker     *        vector if the only required information is the TEE timestamp.
353*4d7e907cSAndroid Build Coastguard Worker     *
354*4d7e907cSAndroid Build Coastguard Worker     * @param authToken A HardwareAuthToken if needed to authorize key usage.
355*4d7e907cSAndroid Build Coastguard Worker     *
356*4d7e907cSAndroid Build Coastguard Worker     * @return error ErrorCode::OK on success or ErrorCode::UNIMPLEMENTED if the IKeymasterDevice is
357*4d7e907cSAndroid Build Coastguard Worker     *         a StrongBox.  If the IKeymasterDevice cannot verify one or more elements of
358*4d7e907cSAndroid Build Coastguard Worker     *         parametersToVerify it must not return an error code, but just omit the unverified
359*4d7e907cSAndroid Build Coastguard Worker     *         parameter from the VerificationToken.
360*4d7e907cSAndroid Build Coastguard Worker     *
361*4d7e907cSAndroid Build Coastguard Worker     * @return token the verification token.  See VerificationToken in types.hal for details.
362*4d7e907cSAndroid Build Coastguard Worker     */
363*4d7e907cSAndroid Build Coastguard Worker    verifyAuthorization(uint64_t operationHandle, vec<KeyParameter> parametersToVerify,
364*4d7e907cSAndroid Build Coastguard Worker                        HardwareAuthToken authToken)
365*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, VerificationToken token);
366*4d7e907cSAndroid Build Coastguard Worker
367*4d7e907cSAndroid Build Coastguard Worker
368*4d7e907cSAndroid Build Coastguard Worker    /**
369*4d7e907cSAndroid Build Coastguard Worker     * Adds entropy to the RNG used by Keymaster.  Entropy added through this method must not be the
370*4d7e907cSAndroid Build Coastguard Worker     * only source of entropy used, and a secure mixing function must be used to mix the entropy
371*4d7e907cSAndroid Build Coastguard Worker     * provided by this method with internally-generated entropy.  The mixing function must be
372*4d7e907cSAndroid Build Coastguard Worker     * secure in the sense that if any one of the mixing function inputs is provided with any data
373*4d7e907cSAndroid Build Coastguard Worker     * the attacker cannot predict (or control), then the output of the seeded CRNG is
374*4d7e907cSAndroid Build Coastguard Worker     * indistinguishable from random.  Thus, if the entropy from any source is good, the output must
375*4d7e907cSAndroid Build Coastguard Worker     * be good.
376*4d7e907cSAndroid Build Coastguard Worker     *
377*4d7e907cSAndroid Build Coastguard Worker     * @param data Bytes to be mixed into the CRNG seed.  The caller must not provide more than 2
378*4d7e907cSAndroid Build Coastguard Worker     *        KiB of data per invocation.
379*4d7e907cSAndroid Build Coastguard Worker     *
380*4d7e907cSAndroid Build Coastguard Worker     * @return error ErrorCode::OK on success; ErrorCode::INVALID_INPUT_LENGTH if the caller
381*4d7e907cSAndroid Build Coastguard Worker     *         provides more than 2 KiB of data.
382*4d7e907cSAndroid Build Coastguard Worker     */
383*4d7e907cSAndroid Build Coastguard Worker    addRngEntropy(vec<uint8_t> data) generates (ErrorCode error);
384*4d7e907cSAndroid Build Coastguard Worker
385*4d7e907cSAndroid Build Coastguard Worker    /**
386*4d7e907cSAndroid Build Coastguard Worker     * Generates a new cryptographic key, specifying associated parameters, which must be
387*4d7e907cSAndroid Build Coastguard Worker     * cryptographically bound to the key.  IKeymasterDevice implementations must disallow any use
388*4d7e907cSAndroid Build Coastguard Worker     * of a key in any way inconsistent with the authorizations specified at generation time.  With
389*4d7e907cSAndroid Build Coastguard Worker     * respect to parameters that the secure environment cannot enforce, the secure environment's
390*4d7e907cSAndroid Build Coastguard Worker     * obligation is limited to ensuring that the unenforceable parameters associated with the key
391*4d7e907cSAndroid Build Coastguard Worker     * cannot be modified, so that every call to getKeyCharacteristics returns the original
392*4d7e907cSAndroid Build Coastguard Worker     * values.  In addition, the characteristics returned by generateKey places parameters correctly
393*4d7e907cSAndroid Build Coastguard Worker     * in the hardware-enforced and software-enforced lists.  See getKeyCharacteristics for more
394*4d7e907cSAndroid Build Coastguard Worker     * details.
395*4d7e907cSAndroid Build Coastguard Worker     *
396*4d7e907cSAndroid Build Coastguard Worker     * In addition to the parameters provided, generateKey must add the following to the returned
397*4d7e907cSAndroid Build Coastguard Worker     * characteristics.
398*4d7e907cSAndroid Build Coastguard Worker     *
399*4d7e907cSAndroid Build Coastguard Worker     * o Tag::ORIGIN with the value KeyOrigin::GENERATED.
400*4d7e907cSAndroid Build Coastguard Worker     *
401*4d7e907cSAndroid Build Coastguard Worker     * o Tag::BLOB_USAGE_REQUIREMENTS with the appropriate value (see KeyBlobUsageRequirements in
402*4d7e907cSAndroid Build Coastguard Worker     *   types.hal).
403*4d7e907cSAndroid Build Coastguard Worker     *
404*4d7e907cSAndroid Build Coastguard Worker     * o Tag::CREATION_DATETIME with the appropriate value.  Note that it is expected that this will
405*4d7e907cSAndroid Build Coastguard Worker     *   generally be added by the HAL, not by the secure environment, and that it will be in the
406*4d7e907cSAndroid Build Coastguard Worker     *   software-enforced list.  It must be cryptographically bound to the key, like all tags.
407*4d7e907cSAndroid Build Coastguard Worker     *
408*4d7e907cSAndroid Build Coastguard Worker     * o Tag::OS_VERSION, Tag::OS_PATCHLEVEL, Tag::VENDOR_PATCHLEVEL and Tag::BOOT_PATCHLEVEL with
409*4d7e907cSAndroid Build Coastguard Worker     *   appropriate values.
410*4d7e907cSAndroid Build Coastguard Worker     *
411*4d7e907cSAndroid Build Coastguard Worker     * The parameters provided to generateKey depend on the type of key being generated.  This
412*4d7e907cSAndroid Build Coastguard Worker     * section summarizes the necessary and optional tags for each type of key.  Tag::ALGORITHM is
413*4d7e907cSAndroid Build Coastguard Worker     * always necessary, to specify the type.
414*4d7e907cSAndroid Build Coastguard Worker     *
415*4d7e907cSAndroid Build Coastguard Worker     * == RSA Keys ==
416*4d7e907cSAndroid Build Coastguard Worker     *
417*4d7e907cSAndroid Build Coastguard Worker     * The following parameters are required to generate an RSA key:
418*4d7e907cSAndroid Build Coastguard Worker     *
419*4d7e907cSAndroid Build Coastguard Worker     * o Tag::Key_SIZE specifies the size of the public modulus, in bits.  If omitted, generateKey
420*4d7e907cSAndroid Build Coastguard Worker     *   must return ErrorCode::UNSUPPORTED_KEY_SIZE.  Required values for TEE IKeymasterDevice
421*4d7e907cSAndroid Build Coastguard Worker     *   implementations are 1024, 2048, 3072 and 4096.  StrongBox IKeymasterDevice implementations
422*4d7e907cSAndroid Build Coastguard Worker     *   must support 2048.
423*4d7e907cSAndroid Build Coastguard Worker     *
424*4d7e907cSAndroid Build Coastguard Worker     * o Tag::RSA_PUBLIC_EXPONENT specifies the RSA public exponent value.  If omitted, generateKey
425*4d7e907cSAndroid Build Coastguard Worker     *   must return ErrorCode::INVALID_ARGUMENT.  The values 3 and 65537 must be supported.  It is
426*4d7e907cSAndroid Build Coastguard Worker     *   recommended to support all prime values up to 2^64.  If provided with a non-prime value,
427*4d7e907cSAndroid Build Coastguard Worker     *   generateKey must return ErrorCode::INVALID_ARGUMENT.
428*4d7e907cSAndroid Build Coastguard Worker     *
429*4d7e907cSAndroid Build Coastguard Worker     * The following parameters are not necessary to generate a usable RSA key, but generateKey must
430*4d7e907cSAndroid Build Coastguard Worker     * not return an error if they are omitted:
431*4d7e907cSAndroid Build Coastguard Worker     *
432*4d7e907cSAndroid Build Coastguard Worker     * o Tag::PURPOSE specifies allowed purposes.  All KeyPurpose values (see types.hal) must be
433*4d7e907cSAndroid Build Coastguard Worker     *   supported for RSA keys.
434*4d7e907cSAndroid Build Coastguard Worker     *
435*4d7e907cSAndroid Build Coastguard Worker     * o Tag::DIGEST specifies digest algorithms that may be used with the new key.  TEE
436*4d7e907cSAndroid Build Coastguard Worker     *   IKeymasterDevice implementations must support all Digest values (see types.hal) for RSA
437*4d7e907cSAndroid Build Coastguard Worker     *   keys.  StrongBox IKeymasterDevice implementations must support SHA_2_256.
438*4d7e907cSAndroid Build Coastguard Worker     *
439*4d7e907cSAndroid Build Coastguard Worker     * o Tag::PADDING specifies the padding modes that may be used with the new
440*4d7e907cSAndroid Build Coastguard Worker     *   key.  IKeymasterDevice implementations must support PaddingMode::NONE,
441*4d7e907cSAndroid Build Coastguard Worker     *   PaddingMode::RSA_OAEP, PaddingMode::RSA_PSS, PaddingMode::RSA_PKCS1_1_5_ENCRYPT and
442*4d7e907cSAndroid Build Coastguard Worker     *   PaddingMode::RSA_PKCS1_1_5_SIGN for RSA keys.
443*4d7e907cSAndroid Build Coastguard Worker     *
444*4d7e907cSAndroid Build Coastguard Worker     * == ECDSA Keys ==
445*4d7e907cSAndroid Build Coastguard Worker     *
446*4d7e907cSAndroid Build Coastguard Worker     * Either Tag::KEY_SIZE or Tag::EC_CURVE must be provided to generate an ECDSA key.  If neither
447*4d7e907cSAndroid Build Coastguard Worker     * is provided, generateKey must return ErrorCode::UNSUPPORTED_KEY_SIZE.  If Tag::KEY_SIZE is
448*4d7e907cSAndroid Build Coastguard Worker     * provided, the possible values are 224, 256, 384 and 521, and must be mapped to Tag::EC_CURVE
449*4d7e907cSAndroid Build Coastguard Worker     * values P_224, P_256, P_384 and P_521, respectively.  TEE IKeymasterDevice implementations
450*4d7e907cSAndroid Build Coastguard Worker     * must support all curves.  StrongBox implementations must support P_256.
451*4d7e907cSAndroid Build Coastguard Worker     *
452*4d7e907cSAndroid Build Coastguard Worker     * == AES Keys ==
453*4d7e907cSAndroid Build Coastguard Worker     *
454*4d7e907cSAndroid Build Coastguard Worker     * Only Tag::KEY_SIZE is required to generate an AES key.  If omitted, generateKey must return
455*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::UNSUPPORTED_KEY_SIZE.  128 and 256-bit key sizes must be supported.
456*4d7e907cSAndroid Build Coastguard Worker     *
457*4d7e907cSAndroid Build Coastguard Worker     * If Tag::BLOCK_MODE is specified with value BlockMode::GCM, then the caller must also provide
458*4d7e907cSAndroid Build Coastguard Worker     * Tag::MIN_MAC_LENGTH.  If omitted, generateKey must return ErrorCode::MISSING_MIN_MAC_LENGTH.
459*4d7e907cSAndroid Build Coastguard Worker     *
460*4d7e907cSAndroid Build Coastguard Worker     *
461*4d7e907cSAndroid Build Coastguard Worker     * @param keyParams Key generation parameters are defined as IKeymasterDevice tag/value pairs,
462*4d7e907cSAndroid Build Coastguard Worker     *        provided in params.  See above for detailed specifications of which tags are required
463*4d7e907cSAndroid Build Coastguard Worker     *        for which types of keys.
464*4d7e907cSAndroid Build Coastguard Worker     *
465*4d7e907cSAndroid Build Coastguard Worker     * @return keyBlob Opaque descriptor of the generated key.  The recommended implementation
466*4d7e907cSAndroid Build Coastguard Worker     *         strategy is to include an encrypted copy of the key material, wrapped in a key
467*4d7e907cSAndroid Build Coastguard Worker     *         unavailable outside secure hardware.
468*4d7e907cSAndroid Build Coastguard Worker     *
469*4d7e907cSAndroid Build Coastguard Worker     * @return keyCharacteristics Description of the generated key.  See the getKeyCharacteristics
470*4d7e907cSAndroid Build Coastguard Worker     *         method below.
471*4d7e907cSAndroid Build Coastguard Worker     */
472*4d7e907cSAndroid Build Coastguard Worker    generateKey(vec<KeyParameter> keyParams)
473*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<uint8_t> keyBlob, KeyCharacteristics keyCharacteristics);
474*4d7e907cSAndroid Build Coastguard Worker
475*4d7e907cSAndroid Build Coastguard Worker    /**
476*4d7e907cSAndroid Build Coastguard Worker     * Imports key material into an IKeymasterDevice.  Key definition parameters and return values
477*4d7e907cSAndroid Build Coastguard Worker     * are the same as for generateKey, with the following exceptions:
478*4d7e907cSAndroid Build Coastguard Worker     *
479*4d7e907cSAndroid Build Coastguard Worker     * o Tag::KEY_SIZE is not necessary in the input parameters.  If not provided, the
480*4d7e907cSAndroid Build Coastguard Worker     *   IKeymasterDevice must deduce the value from the provided key material and add the tag and
481*4d7e907cSAndroid Build Coastguard Worker     *   value to the key characteristics.  If Tag::KEY_SIZE is provided, the IKeymasterDevice must
482*4d7e907cSAndroid Build Coastguard Worker     *   validate it against the key material.  In the event of a mismatch, importKey must return
483*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::IMPORT_PARAMETER_MISMATCH.
484*4d7e907cSAndroid Build Coastguard Worker     *
485*4d7e907cSAndroid Build Coastguard Worker     * o Tag::RSA_PUBLIC_EXPONENT (for RSA keys only) is not necessary in the input parameters.  If
486*4d7e907cSAndroid Build Coastguard Worker     *   not provided, the IKeymasterDevice must deduce the value from the provided key material and
487*4d7e907cSAndroid Build Coastguard Worker     *   add the tag and value to the key characteristics.  If Tag::RSA_PUBLIC_EXPONENT is provided,
488*4d7e907cSAndroid Build Coastguard Worker     *   the IKeymasterDevice must validate it against the key material.  In the event of a
489*4d7e907cSAndroid Build Coastguard Worker     *   mismatch, importKey must return ErrorCode::IMPORT_PARAMETER_MISMATCH.
490*4d7e907cSAndroid Build Coastguard Worker     *
491*4d7e907cSAndroid Build Coastguard Worker     * o Tag::ORIGIN (returned in keyCharacteristics) must have the value KeyOrigin::IMPORTED.
492*4d7e907cSAndroid Build Coastguard Worker     *
493*4d7e907cSAndroid Build Coastguard Worker     * @param keyParams Key generation parameters are defined as IKeymasterDevice tag/value pairs,
494*4d7e907cSAndroid Build Coastguard Worker     *        provided in params.
495*4d7e907cSAndroid Build Coastguard Worker     *
496*4d7e907cSAndroid Build Coastguard Worker     * @param keyFormat The format of the key material to import.  See KeyFormat in types.hal.
497*4d7e907cSAndroid Build Coastguard Worker     *
498*4d7e907cSAndroid Build Coastguard Worker     * @pram keyData The key material to import, in the format specified in keyFormat.
499*4d7e907cSAndroid Build Coastguard Worker     *
500*4d7e907cSAndroid Build Coastguard Worker     * @return keyBlob Opaque descriptor of the imported key.  The recommended implementation
501*4d7e907cSAndroid Build Coastguard Worker     *         strategy is to include an encrypted copy of the key material, wrapped in a key
502*4d7e907cSAndroid Build Coastguard Worker     *         unavailable outside secure hardware.
503*4d7e907cSAndroid Build Coastguard Worker     *
504*4d7e907cSAndroid Build Coastguard Worker     * @return keyCharacteristics Description of the generated key.  See the getKeyCharacteristics
505*4d7e907cSAndroid Build Coastguard Worker     *         method below.
506*4d7e907cSAndroid Build Coastguard Worker     */
507*4d7e907cSAndroid Build Coastguard Worker    importKey(vec<KeyParameter> keyParams, KeyFormat keyFormat, vec<uint8_t> keyData)
508*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<uint8_t> keyBlob, KeyCharacteristics keyCharacteristics);
509*4d7e907cSAndroid Build Coastguard Worker
510*4d7e907cSAndroid Build Coastguard Worker    /**
511*4d7e907cSAndroid Build Coastguard Worker     * Securely imports a key, or key pair, returning a key blob and a description of the imported
512*4d7e907cSAndroid Build Coastguard Worker     * key.
513*4d7e907cSAndroid Build Coastguard Worker     *
514*4d7e907cSAndroid Build Coastguard Worker     * @param wrappedKeyData The wrapped key material to import.  The wrapped key is in DER-encoded
515*4d7e907cSAndroid Build Coastguard Worker     * ASN.1 format, specified by the following schema:
516*4d7e907cSAndroid Build Coastguard Worker     *
517*4d7e907cSAndroid Build Coastguard Worker     *     KeyDescription ::= SEQUENCE(
518*4d7e907cSAndroid Build Coastguard Worker     *         keyFormat INTEGER,                   # Values from KeyFormat enum.
519*4d7e907cSAndroid Build Coastguard Worker     *         keyParams AuthorizationList,
520*4d7e907cSAndroid Build Coastguard Worker     *     )
521*4d7e907cSAndroid Build Coastguard Worker     *
522*4d7e907cSAndroid Build Coastguard Worker     *     SecureKeyWrapper ::= SEQUENCE(
523*4d7e907cSAndroid Build Coastguard Worker     *         version INTEGER,                     # Contains value 0
524*4d7e907cSAndroid Build Coastguard Worker     *         encryptedTransportKey OCTET_STRING,
525*4d7e907cSAndroid Build Coastguard Worker     *         initializationVector OCTET_STRING,
526*4d7e907cSAndroid Build Coastguard Worker     *         keyDescription KeyDescription,
527*4d7e907cSAndroid Build Coastguard Worker     *         encryptedKey OCTET_STRING,
528*4d7e907cSAndroid Build Coastguard Worker     *         tag OCTET_STRING
529*4d7e907cSAndroid Build Coastguard Worker     *     )
530*4d7e907cSAndroid Build Coastguard Worker     *
531*4d7e907cSAndroid Build Coastguard Worker     *     Where:
532*4d7e907cSAndroid Build Coastguard Worker     *
533*4d7e907cSAndroid Build Coastguard Worker     *     o keyFormat is an integer from the KeyFormat enum, defining the format of the plaintext
534*4d7e907cSAndroid Build Coastguard Worker     *       key material.
535*4d7e907cSAndroid Build Coastguard Worker     *     o keyParams is the characteristics of the key to be imported (as with generateKey or
536*4d7e907cSAndroid Build Coastguard Worker     *       importKey).  If the secure import is successful, these characteristics must be
537*4d7e907cSAndroid Build Coastguard Worker     *       associated with the key exactly as if the key material had been insecurely imported
538*4d7e907cSAndroid Build Coastguard Worker     *       with the @3.0::IKeymasterDevice::importKey.  See attestKey() for documentation of the
539*4d7e907cSAndroid Build Coastguard Worker     *       AuthorizationList schema.
540*4d7e907cSAndroid Build Coastguard Worker     *     o encryptedTransportKey is a 256-bit AES key, XORed with a masking key and then encrypted
541*4d7e907cSAndroid Build Coastguard Worker     *       with the wrapping key specified by wrappingKeyBlob.
542*4d7e907cSAndroid Build Coastguard Worker     *     o keyDescription is a KeyDescription, above.
543*4d7e907cSAndroid Build Coastguard Worker     *     o encryptedKey is the key material of the key to be imported, in format keyFormat, and
544*4d7e907cSAndroid Build Coastguard Worker     *       encrypted with encryptedEphemeralKey in AES-GCM mode, with the DER-encoded
545*4d7e907cSAndroid Build Coastguard Worker     *       representation of keyDescription provided as additional authenticated data.
546*4d7e907cSAndroid Build Coastguard Worker     *     o tag is the tag produced by the AES-GCM encryption of encryptedKey.
547*4d7e907cSAndroid Build Coastguard Worker     *
548*4d7e907cSAndroid Build Coastguard Worker     * So, importWrappedKey does the following:
549*4d7e907cSAndroid Build Coastguard Worker     *
550*4d7e907cSAndroid Build Coastguard Worker     *     1. Get the private key material for wrappingKeyBlob, verifying that the wrapping key has
551*4d7e907cSAndroid Build Coastguard Worker     *        purpose KEY_WRAP, padding mode RSA_OAEP, and digest SHA_2_256, returning the
552*4d7e907cSAndroid Build Coastguard Worker     *        appropriate error if any of those requirements fail.
553*4d7e907cSAndroid Build Coastguard Worker     *     2. Extract the encryptedTransportKey field from the SecureKeyWrapper, and decrypt
554*4d7e907cSAndroid Build Coastguard Worker     *        it with the wrapping key.
555*4d7e907cSAndroid Build Coastguard Worker     *     3. XOR the result of step 2 with maskingKey.
556*4d7e907cSAndroid Build Coastguard Worker     *     4. Use the result of step 3 as an AES-GCM key to decrypt encryptedKey, using the encoded
557*4d7e907cSAndroid Build Coastguard Worker     *        value of keyDescription as the additional authenticated data.  Call the result
558*4d7e907cSAndroid Build Coastguard Worker     *        "keyData" for the next step.
559*4d7e907cSAndroid Build Coastguard Worker     *     5. Perform the equivalent of calling importKey(keyParams, keyFormat, keyData), except
560*4d7e907cSAndroid Build Coastguard Worker     *        that the origin tag should be set to SECURELY_IMPORTED.
561*4d7e907cSAndroid Build Coastguard Worker     *
562*4d7e907cSAndroid Build Coastguard Worker     * @param wrappingKeyBlob The opaque key descriptor returned by generateKey() or importKey().
563*4d7e907cSAndroid Build Coastguard Worker     *        This key must have been created with Purpose::WRAP_KEY.
564*4d7e907cSAndroid Build Coastguard Worker     *
565*4d7e907cSAndroid Build Coastguard Worker     * @param maskingKey The 32-byte value XOR'd with the transport key in the SecureWrappedKey
566*4d7e907cSAndroid Build Coastguard Worker     *        structure.
567*4d7e907cSAndroid Build Coastguard Worker     *
568*4d7e907cSAndroid Build Coastguard Worker     * @param unwrappingParams must contain any parameters needed to perform the unwrapping
569*4d7e907cSAndroid Build Coastguard Worker     *        operation.  For example, if the wrapping key is an AES key the block and padding modes
570*4d7e907cSAndroid Build Coastguard Worker     *        must be specified in this argument.
571*4d7e907cSAndroid Build Coastguard Worker     *
572*4d7e907cSAndroid Build Coastguard Worker     * @param passwordSid specifies the password secure ID (SID) of the user that owns the key being
573*4d7e907cSAndroid Build Coastguard Worker     *        installed.  If the authorization list in wrappedKeyData contains a Tag::USER_SECURE_ID
574*4d7e907cSAndroid Build Coastguard Worker     *        with a value that has the HardwareAuthenticatorType::PASSWORD bit set, the constructed
575*4d7e907cSAndroid Build Coastguard Worker     *        key must be bound to the SID value provided by this argument.  If the wrappedKeyData
576*4d7e907cSAndroid Build Coastguard Worker     *        does not contain such a tag and value, this argument must be ignored.
577*4d7e907cSAndroid Build Coastguard Worker     *
578*4d7e907cSAndroid Build Coastguard Worker     * @param biometricSid specifies the biometric secure ID (SID) of the user that owns the key
579*4d7e907cSAndroid Build Coastguard Worker     *        being installed.  If the authorization list in wrappedKeyData contains a
580*4d7e907cSAndroid Build Coastguard Worker     *        Tag::USER_SECURE_ID with a value that has the HardwareAuthenticatorType::FINGERPRINT
581*4d7e907cSAndroid Build Coastguard Worker     *        bit set, the constructed key must be bound to the SID value provided by this argument.
582*4d7e907cSAndroid Build Coastguard Worker     *        If the wrappedKeyData does not contain such a tag and value, this argument must be
583*4d7e907cSAndroid Build Coastguard Worker     *        ignored.
584*4d7e907cSAndroid Build Coastguard Worker     *
585*4d7e907cSAndroid Build Coastguard Worker     * @return keyBlob Opaque descriptor of the imported key.  It is recommended that the keyBlob
586*4d7e907cSAndroid Build Coastguard Worker     *         contain a copy of the key material, wrapped in a key unavailable outside secure
587*4d7e907cSAndroid Build Coastguard Worker     *         hardware.
588*4d7e907cSAndroid Build Coastguard Worker     */
589*4d7e907cSAndroid Build Coastguard Worker    importWrappedKey(vec<uint8_t> wrappedKeyData, vec<uint8_t> wrappingKeyBlob,
590*4d7e907cSAndroid Build Coastguard Worker                     vec<uint8_t> maskingKey, vec<KeyParameter> unwrappingParams,
591*4d7e907cSAndroid Build Coastguard Worker                     uint64_t passwordSid, uint64_t biometricSid)
592*4d7e907cSAndroid Build Coastguard Worker        generates(ErrorCode error, vec<uint8_t> keyBlob, KeyCharacteristics keyCharacteristics);
593*4d7e907cSAndroid Build Coastguard Worker
594*4d7e907cSAndroid Build Coastguard Worker    /**
595*4d7e907cSAndroid Build Coastguard Worker     * Returns parameters associated with the provided key, divided into two sets: hardware-enforced
596*4d7e907cSAndroid Build Coastguard Worker     * and software-enforced.  The description here applies equally to the key characteristics lists
597*4d7e907cSAndroid Build Coastguard Worker     * returned by generateKey, importKey and importWrappedKey.  The characteristics returned by
598*4d7e907cSAndroid Build Coastguard Worker     * this method completely describe the type and usage of the specified key.
599*4d7e907cSAndroid Build Coastguard Worker     *
600*4d7e907cSAndroid Build Coastguard Worker     * The rule that IKeymasterDevice implementations must use for deciding whether a given tag
601*4d7e907cSAndroid Build Coastguard Worker     * belongs in the hardware-enforced or software-enforced list is that if the meaning of the tag
602*4d7e907cSAndroid Build Coastguard Worker     * is fully assured by secure hardware, it is hardware enforced.  Otherwise, it's software
603*4d7e907cSAndroid Build Coastguard Worker     * enforced.
604*4d7e907cSAndroid Build Coastguard Worker     *
605*4d7e907cSAndroid Build Coastguard Worker     *
606*4d7e907cSAndroid Build Coastguard Worker     * @param keyBlob The opaque descriptor returned by generateKey, importKey or importWrappedKey.
607*4d7e907cSAndroid Build Coastguard Worker     *
608*4d7e907cSAndroid Build Coastguard Worker     * @param clientId An opaque byte string identifying the client.  This value must match the
609*4d7e907cSAndroid Build Coastguard Worker     *        Tag::APPLICATION_ID data provided during key generation/import.  Without the correct
610*4d7e907cSAndroid Build Coastguard Worker     *        value, it must be computationally infeasible for the secure hardware to obtain the key
611*4d7e907cSAndroid Build Coastguard Worker     *        material.
612*4d7e907cSAndroid Build Coastguard Worker     *
613*4d7e907cSAndroid Build Coastguard Worker     * @param appData An opaque byte string provided by the application.  This value must match the
614*4d7e907cSAndroid Build Coastguard Worker     *        Tag::APPLICATION_DATA data provided during key generation/import.  Without the correct
615*4d7e907cSAndroid Build Coastguard Worker     *        value, it must be computationally infeasible for the secure hardware to obtain the key
616*4d7e907cSAndroid Build Coastguard Worker     *        material.
617*4d7e907cSAndroid Build Coastguard Worker     *
618*4d7e907cSAndroid Build Coastguard Worker     * @return keyCharacteristics Description of the generated key.  See KeyCharacteristics in
619*4d7e907cSAndroid Build Coastguard Worker     *         types.hal.
620*4d7e907cSAndroid Build Coastguard Worker     */
621*4d7e907cSAndroid Build Coastguard Worker    getKeyCharacteristics(vec<uint8_t> keyBlob, vec<uint8_t> clientId, vec<uint8_t> appData)
622*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, KeyCharacteristics keyCharacteristics);
623*4d7e907cSAndroid Build Coastguard Worker
624*4d7e907cSAndroid Build Coastguard Worker    /**
625*4d7e907cSAndroid Build Coastguard Worker     * Exports a public key, returning the key in the specified format.
626*4d7e907cSAndroid Build Coastguard Worker     *
627*4d7e907cSAndroid Build Coastguard Worker     * @parm keyFormat The format used for export.  Must be KeyFormat::X509.
628*4d7e907cSAndroid Build Coastguard Worker     *
629*4d7e907cSAndroid Build Coastguard Worker     * @param keyBlob The opaque descriptor returned by generateKey() or importKey().  The
630*4d7e907cSAndroid Build Coastguard Worker     *        referenced key must be asymmetric.
631*4d7e907cSAndroid Build Coastguard Worker     *
632*4d7e907cSAndroid Build Coastguard Worker     * @param clientId An opaque byte string identifying the client.  This value must match the
633*4d7e907cSAndroid Build Coastguard Worker     *        Tag::APPLICATION_ID data provided during key generation/import.  Without the correct
634*4d7e907cSAndroid Build Coastguard Worker     *        value, it must be computationally infeasible for the secure hardware to obtain the key
635*4d7e907cSAndroid Build Coastguard Worker     *        material.
636*4d7e907cSAndroid Build Coastguard Worker     *
637*4d7e907cSAndroid Build Coastguard Worker     * @param appData An opaque byte string provided by the application.  This value must match the
638*4d7e907cSAndroid Build Coastguard Worker     *        Tag::APPLICATION_DATA data provided during key generation/import.  Without the correct
639*4d7e907cSAndroid Build Coastguard Worker     *        value, it must be computationally infeasible for the secure hardware to obtain the key
640*4d7e907cSAndroid Build Coastguard Worker     *        material.
641*4d7e907cSAndroid Build Coastguard Worker     *
642*4d7e907cSAndroid Build Coastguard Worker     * @return keyMaterial The public key material in X.509 format.
643*4d7e907cSAndroid Build Coastguard Worker     */
644*4d7e907cSAndroid Build Coastguard Worker    exportKey(KeyFormat keyFormat, vec<uint8_t> keyBlob, vec<uint8_t> clientId,
645*4d7e907cSAndroid Build Coastguard Worker              vec<uint8_t> appData) generates (ErrorCode error, vec<uint8_t> keyMaterial);
646*4d7e907cSAndroid Build Coastguard Worker
647*4d7e907cSAndroid Build Coastguard Worker    /**
648*4d7e907cSAndroid Build Coastguard Worker     * Generates a signed X.509 certificate chain attesting to the presence of keyToAttest in
649*4d7e907cSAndroid Build Coastguard Worker     * Keymaster.
650*4d7e907cSAndroid Build Coastguard Worker     *
651*4d7e907cSAndroid Build Coastguard Worker     * The certificates in the chain must be ordered such that each certificate is signed by the
652*4d7e907cSAndroid Build Coastguard Worker     * subsequent one, up to the root which must be self-signed.  The first certificate in the chain
653*4d7e907cSAndroid Build Coastguard Worker     * signs the public key info of the attested key and must contain the following entries (see RFC
654*4d7e907cSAndroid Build Coastguard Worker     * 5280 for details on each):
655*4d7e907cSAndroid Build Coastguard Worker     *
656*4d7e907cSAndroid Build Coastguard Worker     * o version -- with value 2
657*4d7e907cSAndroid Build Coastguard Worker     *
658*4d7e907cSAndroid Build Coastguard Worker     * o serialNumber -- with value 1 (same value for all keys)
659*4d7e907cSAndroid Build Coastguard Worker     *
660*4d7e907cSAndroid Build Coastguard Worker     * o signature -- contains an the AlgorithmIdentifier of the algorithm used to sign, must be
661*4d7e907cSAndroid Build Coastguard Worker     *   ECDSA for EC keys, RSA for RSA keys.
662*4d7e907cSAndroid Build Coastguard Worker     *
663*4d7e907cSAndroid Build Coastguard Worker     * o issuer -- must contain the same value as the Subject field of the next certificate.
664*4d7e907cSAndroid Build Coastguard Worker     *
665*4d7e907cSAndroid Build Coastguard Worker     * o validity -- SEQUENCE of two dates, containing the values of Tag::ACTIVE_DATETIME and
666*4d7e907cSAndroid Build Coastguard Worker     *   Tag::USAGE_EXPIRE_DATETIME.  The tag values are in milliseconds since Jan 1, 1970; see RFD
667*4d7e907cSAndroid Build Coastguard Worker     *   5280 for the correct representation in certificates.  If Tag::ACTIVE_DATETIME is not
668*4d7e907cSAndroid Build Coastguard Worker     *   present in the key, the IKeymasterDevice must use the value of Tag::CREATION_DATETIME.  If
669*4d7e907cSAndroid Build Coastguard Worker     *   Tag::USAGE_EXPIRE_DATETIME is not present, the IKeymasterDevice must use the expiration
670*4d7e907cSAndroid Build Coastguard Worker     *   date of the batch attestation certificate (see below).
671*4d7e907cSAndroid Build Coastguard Worker     *
672*4d7e907cSAndroid Build Coastguard Worker     * o subject -- CN="Android Keystore Key" (same value for all keys)
673*4d7e907cSAndroid Build Coastguard Worker     *
674*4d7e907cSAndroid Build Coastguard Worker     * o subjectPublicKeyInfo -- X.509 SubjectPublicKeyInfo containing the attested public key.
675*4d7e907cSAndroid Build Coastguard Worker     *
676*4d7e907cSAndroid Build Coastguard Worker     * o Key Usage extension -- digitalSignature bit must be set iff the attested key has
677*4d7e907cSAndroid Build Coastguard Worker     *   KeyPurpose::SIGN.  dataEncipherment bit must be set iff the attested key has
678*4d7e907cSAndroid Build Coastguard Worker     *   KeyPurpose::DECRYPT.  keyEncipherment bit must be set iff the attested key has
679*4d7e907cSAndroid Build Coastguard Worker     *   KeyPurpose::KEY_WRAP.  All other bits must be clear.
680*4d7e907cSAndroid Build Coastguard Worker     *
681*4d7e907cSAndroid Build Coastguard Worker     * In addition to the above, the attestation certificate must contain an extension with OID
682*4d7e907cSAndroid Build Coastguard Worker     * 1.3.6.1.4.1.11129.2.1.17 and value according to the KeyDescription schema defined as:
683*4d7e907cSAndroid Build Coastguard Worker     *
684*4d7e907cSAndroid Build Coastguard Worker     * KeyDescription ::= SEQUENCE {
685*4d7e907cSAndroid Build Coastguard Worker     *     attestationVersion         INTEGER, # Value 3
686*4d7e907cSAndroid Build Coastguard Worker     *     attestationSecurityLevel   SecurityLevel, # See below
687*4d7e907cSAndroid Build Coastguard Worker     *     keymasterVersion           INTEGER, # Value 4
688*4d7e907cSAndroid Build Coastguard Worker     *     keymasterSecurityLevel     SecurityLevel, # See below
689*4d7e907cSAndroid Build Coastguard Worker     *     attestationChallenge       OCTET_STRING, # Tag::ATTESTATION_CHALLENGE from attestParams
690*4d7e907cSAndroid Build Coastguard Worker     *     uniqueId                   OCTET_STRING, # Empty unless key has Tag::INCLUDE_UNIQUE_ID
691*4d7e907cSAndroid Build Coastguard Worker     *     softwareEnforced           AuthorizationList, # See below
692*4d7e907cSAndroid Build Coastguard Worker     *     hardwareEnforced           AuthorizationList, # See below
693*4d7e907cSAndroid Build Coastguard Worker     * }
694*4d7e907cSAndroid Build Coastguard Worker     *
695*4d7e907cSAndroid Build Coastguard Worker     * SecurityLevel ::= ENUMERATED {
696*4d7e907cSAndroid Build Coastguard Worker     *     Software                   (0),
697*4d7e907cSAndroid Build Coastguard Worker     *     TrustedEnvironment         (1),
698*4d7e907cSAndroid Build Coastguard Worker     *     StrongBox                  (2),
699*4d7e907cSAndroid Build Coastguard Worker     * }
700*4d7e907cSAndroid Build Coastguard Worker     *
701*4d7e907cSAndroid Build Coastguard Worker     * RootOfTrust ::= SEQUENCE {
702*4d7e907cSAndroid Build Coastguard Worker     *     verifiedBootKey            OCTET_STRING,
703*4d7e907cSAndroid Build Coastguard Worker     *     deviceLocked               BOOLEAN,
704*4d7e907cSAndroid Build Coastguard Worker     *     verifiedBootState          VerifiedBootState,
705*4d7e907cSAndroid Build Coastguard Worker     *     # verifiedBootHash must contain 32-byte value that represents the state of all binaries
706*4d7e907cSAndroid Build Coastguard Worker     *     # or other components validated by verified boot.  Updating any verified binary or
707*4d7e907cSAndroid Build Coastguard Worker     *     # component must cause this value to change.
708*4d7e907cSAndroid Build Coastguard Worker     *     verifiedBootHash           OCTET_STRING,
709*4d7e907cSAndroid Build Coastguard Worker     * }
710*4d7e907cSAndroid Build Coastguard Worker     *
711*4d7e907cSAndroid Build Coastguard Worker     * VerifiedBootState ::= ENUMERATED {
712*4d7e907cSAndroid Build Coastguard Worker     *     Verified                   (0),
713*4d7e907cSAndroid Build Coastguard Worker     *     SelfSigned                 (1),
714*4d7e907cSAndroid Build Coastguard Worker     *     Unverified                 (2),
715*4d7e907cSAndroid Build Coastguard Worker     *     Failed                     (3),
716*4d7e907cSAndroid Build Coastguard Worker     * }
717*4d7e907cSAndroid Build Coastguard Worker     *
718*4d7e907cSAndroid Build Coastguard Worker     * AuthorizationList ::= SEQUENCE {
719*4d7e907cSAndroid Build Coastguard Worker     *     purpose                    [1] EXPLICIT SET OF INTEGER OPTIONAL,
720*4d7e907cSAndroid Build Coastguard Worker     *     algorithm                  [2] EXPLICIT INTEGER OPTIONAL,
721*4d7e907cSAndroid Build Coastguard Worker     *     keySize                    [3] EXPLICIT INTEGER OPTIONAL,
722*4d7e907cSAndroid Build Coastguard Worker     *     blockMode                  [4] EXPLICIT SET OF INTEGER OPTIONAL,
723*4d7e907cSAndroid Build Coastguard Worker     *     digest                     [5] EXPLICIT SET OF INTEGER OPTIONAL,
724*4d7e907cSAndroid Build Coastguard Worker     *     padding                    [6] EXPLICIT SET OF INTEGER OPTIONAL,
725*4d7e907cSAndroid Build Coastguard Worker     *     callerNonce                [7] EXPLICIT NULL OPTIONAL,
726*4d7e907cSAndroid Build Coastguard Worker     *     minMacLength               [8] EXPLICIT INTEGER OPTIONAL,
727*4d7e907cSAndroid Build Coastguard Worker     *     ecCurve                    [10] EXPLICIT INTEGER OPTIONAL,
728*4d7e907cSAndroid Build Coastguard Worker     *     rsaPublicExponent          [200] EXPLICIT INTEGER OPTIONAL,
729*4d7e907cSAndroid Build Coastguard Worker     *     rollbackResistance         [303] EXPLICIT NULL OPTIONAL,
730*4d7e907cSAndroid Build Coastguard Worker     *     activeDateTime             [400] EXPLICIT INTEGER OPTIONAL,
731*4d7e907cSAndroid Build Coastguard Worker     *     originationExpireDateTime  [401] EXPLICIT INTEGER OPTIONAL,
732*4d7e907cSAndroid Build Coastguard Worker     *     usageExpireDateTime        [402] EXPLICIT INTEGER OPTIONAL,
733*4d7e907cSAndroid Build Coastguard Worker     *     userSecureId               [502] EXPLICIT INTEGER OPTIONAL,
734*4d7e907cSAndroid Build Coastguard Worker     *     noAuthRequired             [503] EXPLICIT NULL OPTIONAL,
735*4d7e907cSAndroid Build Coastguard Worker     *     userAuthType               [504] EXPLICIT INTEGER OPTIONAL,
736*4d7e907cSAndroid Build Coastguard Worker     *     authTimeout                [505] EXPLICIT INTEGER OPTIONAL,
737*4d7e907cSAndroid Build Coastguard Worker     *     allowWhileOnBody           [506] EXPLICIT NULL OPTIONAL,
738*4d7e907cSAndroid Build Coastguard Worker     *     trustedUserPresenceReq     [507] EXPLICIT NULL OPTIONAL,
739*4d7e907cSAndroid Build Coastguard Worker     *     trustedConfirmationReq     [508] EXPLICIT NULL OPTIONAL,
740*4d7e907cSAndroid Build Coastguard Worker     *     unlockedDeviceReq          [509] EXPLICIT NULL OPTIONAL,
741*4d7e907cSAndroid Build Coastguard Worker     *     creationDateTime           [701] EXPLICIT INTEGER OPTIONAL,
742*4d7e907cSAndroid Build Coastguard Worker     *     origin                     [702] EXPLICIT INTEGER OPTIONAL,
743*4d7e907cSAndroid Build Coastguard Worker     *     rootOfTrust                [704] EXPLICIT RootOfTrust OPTIONAL,
744*4d7e907cSAndroid Build Coastguard Worker     *     osVersion                  [705] EXPLICIT INTEGER OPTIONAL,
745*4d7e907cSAndroid Build Coastguard Worker     *     osPatchLevel               [706] EXPLICIT INTEGER OPTIONAL,
746*4d7e907cSAndroid Build Coastguard Worker     *     attestationApplicationId   [709] EXPLICIT OCTET_STRING OPTIONAL,
747*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdBrand         [710] EXPLICIT OCTET_STRING OPTIONAL,
748*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdDevice        [711] EXPLICIT OCTET_STRING OPTIONAL,
749*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdProduct       [712] EXPLICIT OCTET_STRING OPTIONAL,
750*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdSerial        [713] EXPLICIT OCTET_STRING OPTIONAL,
751*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdImei          [714] EXPLICIT OCTET_STRING OPTIONAL,
752*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdMeid          [715] EXPLICIT OCTET_STRING OPTIONAL,
753*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdManufacturer  [716] EXPLICIT OCTET_STRING OPTIONAL,
754*4d7e907cSAndroid Build Coastguard Worker     *     attestationIdModel         [717] EXPLICIT OCTET_STRING OPTIONAL,
755*4d7e907cSAndroid Build Coastguard Worker     *     vendorPatchLevel           [718] EXPLICIT INTEGER OPTIONAL,
756*4d7e907cSAndroid Build Coastguard Worker     *     bootPatchLevel             [719] EXPLICIT INTEGER OPTIONAL,
757*4d7e907cSAndroid Build Coastguard Worker     * }
758*4d7e907cSAndroid Build Coastguard Worker     *
759*4d7e907cSAndroid Build Coastguard Worker     * The above schema is mostly a straightforward translation of the IKeymasterDevice tag/value
760*4d7e907cSAndroid Build Coastguard Worker     * parameter lists to ASN.1:
761*4d7e907cSAndroid Build Coastguard Worker     *
762*4d7e907cSAndroid Build Coastguard Worker     * o TagType::ENUM, TagType::UINT, TagType::ULONG and TagType::DATE tags are represented as
763*4d7e907cSAndroid Build Coastguard Worker     *   ASN.1 INTEGER.
764*4d7e907cSAndroid Build Coastguard Worker     *
765*4d7e907cSAndroid Build Coastguard Worker     * o TagType::ENUM_REP, TagType::UINT_REP and TagType::ULONG_REP tags are represented as ASN.1
766*4d7e907cSAndroid Build Coastguard Worker     *   SET of INTEGER.
767*4d7e907cSAndroid Build Coastguard Worker     *
768*4d7e907cSAndroid Build Coastguard Worker     * o TagType::BOOL tags are represented as ASN.1 NULL.  All entries in AuthorizationList are
769*4d7e907cSAndroid Build Coastguard Worker     *   OPTIONAL, so the presence of the tag means "true", absence means "false".
770*4d7e907cSAndroid Build Coastguard Worker     *
771*4d7e907cSAndroid Build Coastguard Worker     * o TagType::BYTES tags are represented as ASN.1 OCTET_STRING.
772*4d7e907cSAndroid Build Coastguard Worker     *
773*4d7e907cSAndroid Build Coastguard Worker     * The numeric ASN.1 tag numbers are the same values as the IKeymasterDevice Tag enum values,
774*4d7e907cSAndroid Build Coastguard Worker     * except with the TagType modifier stripped.
775*4d7e907cSAndroid Build Coastguard Worker     *
776*4d7e907cSAndroid Build Coastguard Worker     * The attestation certificate must be signed by a "batch" key, which must be securely
777*4d7e907cSAndroid Build Coastguard Worker     * pre-installed into the device, generally in the factory, and securely stored to prevent
778*4d7e907cSAndroid Build Coastguard Worker     * access or extraction.  The batch key must be used only for signing attestation certificates.
779*4d7e907cSAndroid Build Coastguard Worker     * The batch attestation certificate must be signed by a chain or zero or more intermediates
780*4d7e907cSAndroid Build Coastguard Worker     * leading to a self-signed roots.  The intermediate and root certificate signing keys must not
781*4d7e907cSAndroid Build Coastguard Worker     * exist anywhere on the device.
782*4d7e907cSAndroid Build Coastguard Worker     *
783*4d7e907cSAndroid Build Coastguard Worker     * == ID Attestation ==
784*4d7e907cSAndroid Build Coastguard Worker     *
785*4d7e907cSAndroid Build Coastguard Worker     * ID attestation is a special case of key attestation in which unique device ID values are
786*4d7e907cSAndroid Build Coastguard Worker     * included in the signed attestation certificate.
787*4d7e907cSAndroid Build Coastguard Worker     *
788*4d7e907cSAndroid Build Coastguard Worker     * @param keyToAttest The opaque descriptor returned by generateKey() or importKey().  The
789*4d7e907cSAndroid Build Coastguard Worker     *        referenced key must be asymmetric.
790*4d7e907cSAndroid Build Coastguard Worker     *
791*4d7e907cSAndroid Build Coastguard Worker     * @param attestParams Parameters for the attestation.  Must contain Tag::ATTESTATION_CHALLENGE,
792*4d7e907cSAndroid Build Coastguard Worker     *        the value of which must be put in the attestationChallenge field of the KeyDescription
793*4d7e907cSAndroid Build Coastguard Worker     *        ASN.1 structure defined above.
794*4d7e907cSAndroid Build Coastguard Worker     *
795*4d7e907cSAndroid Build Coastguard Worker     * @return certChain The attestation certificate, and additional certificates back to the root
796*4d7e907cSAndroid Build Coastguard Worker     *         attestation certificate, which clients will need to check against a known-good value.
797*4d7e907cSAndroid Build Coastguard Worker     *         The certificates must be DER-encoded.
798*4d7e907cSAndroid Build Coastguard Worker     */
799*4d7e907cSAndroid Build Coastguard Worker    attestKey(vec<uint8_t> keyToAttest, vec<KeyParameter> attestParams)
800*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<vec<uint8_t>> certChain);
801*4d7e907cSAndroid Build Coastguard Worker
802*4d7e907cSAndroid Build Coastguard Worker    /**
803*4d7e907cSAndroid Build Coastguard Worker     * Upgrades an old key blob.  Keys can become "old" in two ways: IKeymasterDevice can be
804*4d7e907cSAndroid Build Coastguard Worker     * upgraded to a new version with an incompatible key blob format, or the system can be updated
805*4d7e907cSAndroid Build Coastguard Worker     * to invalidate the OS version (OS_VERSION tag), system patch level (OS_PATCHLEVEL tag), vendor
806*4d7e907cSAndroid Build Coastguard Worker     * patch level (VENDOR_PATCH_LEVEL tag), boot patch level (BOOT_PATCH_LEVEL tag) or other,
807*4d7e907cSAndroid Build Coastguard Worker     * implementation-defined patch level (keymaster implementers are encouraged to extend this HAL
808*4d7e907cSAndroid Build Coastguard Worker     * with a minor version extension to define validatable patch levels for other images; tags must
809*4d7e907cSAndroid Build Coastguard Worker     * be defined in the implementer's namespace, starting at 10000).  In either case, attempts to
810*4d7e907cSAndroid Build Coastguard Worker     * use an old key blob with getKeyCharacteristics(), exportKey(), attestKey() or begin() must
811*4d7e907cSAndroid Build Coastguard Worker     * result in IKeymasterDevice returning ErrorCode::KEY_REQUIRES_UPGRADE.  The caller must use
812*4d7e907cSAndroid Build Coastguard Worker     * this method to upgrade the key blob.
813*4d7e907cSAndroid Build Coastguard Worker     *
814*4d7e907cSAndroid Build Coastguard Worker     * The upgradeKey method must examine each version or patch level associated with the key.  If
815*4d7e907cSAndroid Build Coastguard Worker     * any one of them is higher than the corresponding current device value upgradeKey() must
816*4d7e907cSAndroid Build Coastguard Worker     * return ErrorCode::INVALID_ARGUMENT.  There is one exception: it is always permissible to
817*4d7e907cSAndroid Build Coastguard Worker     * "downgrade" from any OS_VERSION number to OS_VERSION 0.  For example, if the key has
818*4d7e907cSAndroid Build Coastguard Worker     * OS_VERSION 080001, it is permissible to upgrade the key if the current system version is
819*4d7e907cSAndroid Build Coastguard Worker     * 080100, because the new version is larger, or if the current system version is 0, because
820*4d7e907cSAndroid Build Coastguard Worker     * upgrades to 0 are always allowed.  If the system version were 080000, however, keymaster must
821*4d7e907cSAndroid Build Coastguard Worker     * return ErrorCode::INVALID_ARGUMENT because that value is smaller than 080001.  Values other
822*4d7e907cSAndroid Build Coastguard Worker     * than OS_VERSION must never be downgraded.
823*4d7e907cSAndroid Build Coastguard Worker     *
824*4d7e907cSAndroid Build Coastguard Worker     * Note that Keymaster versions 2 and 3 required that the system and boot images have the same
825*4d7e907cSAndroid Build Coastguard Worker     * patch level and OS version.  This requirement is relaxed for 4.0::IKeymasterDevice, and the
826*4d7e907cSAndroid Build Coastguard Worker     * OS version in the boot image footer is no longer used.
827*4d7e907cSAndroid Build Coastguard Worker     *
828*4d7e907cSAndroid Build Coastguard Worker     * @param keyBlobToUpgrade The opaque descriptor returned by generateKey() or importKey();
829*4d7e907cSAndroid Build Coastguard Worker     *
830*4d7e907cSAndroid Build Coastguard Worker     * @param upgradeParams A parameter list containing any parameters needed to complete the
831*4d7e907cSAndroid Build Coastguard Worker     *        upgrade, including Tag::APPLICATION_ID and Tag::APPLICATION_DATA.
832*4d7e907cSAndroid Build Coastguard Worker     *
833*4d7e907cSAndroid Build Coastguard Worker     * @return upgradedKeyBlob A new key blob that references the same key as keyBlobToUpgrade, but
834*4d7e907cSAndroid Build Coastguard Worker     *         is in the new format, or has the new version data.
835*4d7e907cSAndroid Build Coastguard Worker     */
836*4d7e907cSAndroid Build Coastguard Worker    upgradeKey(vec<uint8_t> keyBlobToUpgrade, vec<KeyParameter> upgradeParams)
837*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<uint8_t> upgradedKeyBlob);
838*4d7e907cSAndroid Build Coastguard Worker
839*4d7e907cSAndroid Build Coastguard Worker    /**
840*4d7e907cSAndroid Build Coastguard Worker     * Deletes the key, or key pair, associated with the key blob.  Calling this function on a key
841*4d7e907cSAndroid Build Coastguard Worker     * with Tag::ROLLBACK_RESISTANCE in its hardware-enforced authorization list must render the key
842*4d7e907cSAndroid Build Coastguard Worker     * permanently unusable.  Keys without Tag::ROLLBACK_RESISTANCE may or may not be rendered
843*4d7e907cSAndroid Build Coastguard Worker     * unusable.
844*4d7e907cSAndroid Build Coastguard Worker     *
845*4d7e907cSAndroid Build Coastguard Worker     * @param keyBlob The opaque descriptor returned by generateKey() or importKey();
846*4d7e907cSAndroid Build Coastguard Worker     */
847*4d7e907cSAndroid Build Coastguard Worker    deleteKey(vec<uint8_t> keyBlob) generates (ErrorCode error);
848*4d7e907cSAndroid Build Coastguard Worker
849*4d7e907cSAndroid Build Coastguard Worker    /**
850*4d7e907cSAndroid Build Coastguard Worker     * Deletes all keys in the hardware keystore.  Used when keystore is reset completely.  After
851*4d7e907cSAndroid Build Coastguard Worker     * this function is called all keys with Tag::ROLLBACK_RESISTANCE in their hardware-enforced
852*4d7e907cSAndroid Build Coastguard Worker     * authorization lists must be rendered permanently unusable.  Keys without
853*4d7e907cSAndroid Build Coastguard Worker     * Tag::ROLLBACK_RESISTANCE may or may not be rendered unusable.
854*4d7e907cSAndroid Build Coastguard Worker     *
855*4d7e907cSAndroid Build Coastguard Worker     * @return error See the ErrorCode enum.
856*4d7e907cSAndroid Build Coastguard Worker     */
857*4d7e907cSAndroid Build Coastguard Worker    deleteAllKeys() generates (ErrorCode error);
858*4d7e907cSAndroid Build Coastguard Worker
859*4d7e907cSAndroid Build Coastguard Worker    /**
860*4d7e907cSAndroid Build Coastguard Worker     * Destroys knowledge of the device's ids.  This prevents all device id attestation in the
861*4d7e907cSAndroid Build Coastguard Worker     * future.  The destruction must be permanent so that not even a factory reset will restore the
862*4d7e907cSAndroid Build Coastguard Worker     * device ids.
863*4d7e907cSAndroid Build Coastguard Worker     *
864*4d7e907cSAndroid Build Coastguard Worker     * Device id attestation may be provided only if this method is fully implemented, allowing the
865*4d7e907cSAndroid Build Coastguard Worker     * user to permanently disable device id attestation.  If this cannot be guaranteed, the device
866*4d7e907cSAndroid Build Coastguard Worker     * must never attest any device ids.
867*4d7e907cSAndroid Build Coastguard Worker     *
868*4d7e907cSAndroid Build Coastguard Worker     * This is a NOP if device id attestation is not supported.
869*4d7e907cSAndroid Build Coastguard Worker     */
870*4d7e907cSAndroid Build Coastguard Worker    destroyAttestationIds() generates (ErrorCode error);
871*4d7e907cSAndroid Build Coastguard Worker
872*4d7e907cSAndroid Build Coastguard Worker    /**
873*4d7e907cSAndroid Build Coastguard Worker     * Begins a cryptographic operation using the specified key.  If all is well, begin() must
874*4d7e907cSAndroid Build Coastguard Worker     * return ErrorCode::OK and create an operation handle which must be passed to subsequent calls
875*4d7e907cSAndroid Build Coastguard Worker     * to update(), finish() or abort().
876*4d7e907cSAndroid Build Coastguard Worker     *
877*4d7e907cSAndroid Build Coastguard Worker     * It is critical that each call to begin() be paired with a subsequent call to finish() or
878*4d7e907cSAndroid Build Coastguard Worker     * abort(), to allow the IKeymasterDevice implementation to clean up any internal operation
879*4d7e907cSAndroid Build Coastguard Worker     * state.  The caller's failure to do this may leak internal state space or other internal
880*4d7e907cSAndroid Build Coastguard Worker     * resources and may eventually cause begin() to return ErrorCode::TOO_MANY_OPERATIONS when it
881*4d7e907cSAndroid Build Coastguard Worker     * runs out of space for operations.  Any result other than ErrorCode::OK from begin(), update()
882*4d7e907cSAndroid Build Coastguard Worker     * or finish() implicitly aborts the operation, in which case abort() need not be called (and
883*4d7e907cSAndroid Build Coastguard Worker     * must return ErrorCode::INVALID_OPERATION_HANDLE if called).  IKeymasterDevice implementations
884*4d7e907cSAndroid Build Coastguard Worker     * must support 16 concurrent operations.
885*4d7e907cSAndroid Build Coastguard Worker     *
886*4d7e907cSAndroid Build Coastguard Worker     * If Tag::APPLICATION_ID or Tag::APPLICATION_DATA were specified during key generation or
887*4d7e907cSAndroid Build Coastguard Worker     * import, calls to begin must include those tags with the originally-specified values in the
888*4d7e907cSAndroid Build Coastguard Worker     * inParams argument to this method.  If not, begin() must return ErrorCode::INVALID_KEY_BLOB.
889*4d7e907cSAndroid Build Coastguard Worker     *
890*4d7e907cSAndroid Build Coastguard Worker     * == Authorization Enforcement ==
891*4d7e907cSAndroid Build Coastguard Worker     *
892*4d7e907cSAndroid Build Coastguard Worker     * The following key authorization parameters must be enforced by the IKeymasterDevice secure
893*4d7e907cSAndroid Build Coastguard Worker     * environment if the tags were returned in the "hardwareEnforced" list in the
894*4d7e907cSAndroid Build Coastguard Worker     * KeyCharacteristics.  Public key operations, meaning KeyPurpose::ENCRYPT and
895*4d7e907cSAndroid Build Coastguard Worker     * KeyPurpose::VERIFY must be allowed to succeed even if authorization requirements are not met.
896*4d7e907cSAndroid Build Coastguard Worker     *
897*4d7e907cSAndroid Build Coastguard Worker     * -- All Key Types --
898*4d7e907cSAndroid Build Coastguard Worker     *
899*4d7e907cSAndroid Build Coastguard Worker     * The tags in this section apply to all key types.  See below for additional key type-specific
900*4d7e907cSAndroid Build Coastguard Worker     * tags.
901*4d7e907cSAndroid Build Coastguard Worker     *
902*4d7e907cSAndroid Build Coastguard Worker     * o Tag::PURPOSE: The purpose specified in the begin() call must match one of the purposes in
903*4d7e907cSAndroid Build Coastguard Worker     *   the key authorizations.  If the specified purpose does not match, begin() must return
904*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::UNSUPPORTED_PURPOSE.
905*4d7e907cSAndroid Build Coastguard Worker     *
906*4d7e907cSAndroid Build Coastguard Worker     * o Tag::ACTIVE_DATETIME can only be enforced if a trusted UTC time source is available.  If
907*4d7e907cSAndroid Build Coastguard Worker     *   the current date and time is prior to the tag value, begin() must return
908*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::KEY_NOT_YET_VALID.
909*4d7e907cSAndroid Build Coastguard Worker     *
910*4d7e907cSAndroid Build Coastguard Worker     * o Tag::ORIGINATION_EXPIRE_DATETIME can only be enforced if a trusted UTC time source is
911*4d7e907cSAndroid Build Coastguard Worker     *   available.  If the current date and time is later than the tag value and the purpose is
912*4d7e907cSAndroid Build Coastguard Worker     *   KeyPurpose::ENCRYPT or KeyPurpose::SIGN, begin() must return ErrorCode::KEY_EXPIRED.
913*4d7e907cSAndroid Build Coastguard Worker     *
914*4d7e907cSAndroid Build Coastguard Worker     * o Tag::USAGE_EXPIRE_DATETIME can only be enforced if a trusted UTC time source is
915*4d7e907cSAndroid Build Coastguard Worker     *   available.  If the current date and time is later than the tag value and the purpose is
916*4d7e907cSAndroid Build Coastguard Worker     *   KeyPurpose::DECRYPT or KeyPurpose::VERIFY, begin() must return ErrorCode::KEY_EXPIRED.
917*4d7e907cSAndroid Build Coastguard Worker
918*4d7e907cSAndroid Build Coastguard Worker     * o Tag::MIN_SECONDS_BETWEEN_OPS must be compared with a trusted relative timer indicating the
919*4d7e907cSAndroid Build Coastguard Worker     *   last use of the key.  If the last use time plus the tag value is less than the current
920*4d7e907cSAndroid Build Coastguard Worker     *   time, begin() must return ErrorCode::KEY_RATE_LIMIT_EXCEEDED.  See the tag description for
921*4d7e907cSAndroid Build Coastguard Worker     *   important implementation details.
922*4d7e907cSAndroid Build Coastguard Worker
923*4d7e907cSAndroid Build Coastguard Worker     * o Tag::MAX_USES_PER_BOOT must be compared against a secure counter that tracks the uses of
924*4d7e907cSAndroid Build Coastguard Worker     *   the key since boot time.  If the count of previous uses exceeds the tag value, begin() must
925*4d7e907cSAndroid Build Coastguard Worker     *   return ErrorCode::KEY_MAX_OPS_EXCEEDED.
926*4d7e907cSAndroid Build Coastguard Worker     *
927*4d7e907cSAndroid Build Coastguard Worker     * o Tag::USER_SECURE_ID must be enforced by this method if and only if the key also has
928*4d7e907cSAndroid Build Coastguard Worker     *   Tag::AUTH_TIMEOUT (if it does not have Tag::AUTH_TIMEOUT, the Tag::USER_SECURE_ID
929*4d7e907cSAndroid Build Coastguard Worker     *   requirement must be enforced by update() and finish()).  If the key has both, then this
930*4d7e907cSAndroid Build Coastguard Worker     *   method must receive a non-empty HardwareAuthToken in the authToken argument.  For the auth
931*4d7e907cSAndroid Build Coastguard Worker     *   token to be valid, all of the following have to be true:
932*4d7e907cSAndroid Build Coastguard Worker     *
933*4d7e907cSAndroid Build Coastguard Worker     *   o The HMAC field must validate correctly.
934*4d7e907cSAndroid Build Coastguard Worker     *
935*4d7e907cSAndroid Build Coastguard Worker     *   o At least one of the Tag::USER_SECURE_ID values from the key must match at least one of
936*4d7e907cSAndroid Build Coastguard Worker     *     the secure ID values in the token.
937*4d7e907cSAndroid Build Coastguard Worker     *
938*4d7e907cSAndroid Build Coastguard Worker     *   o The key must have a Tag::USER_AUTH_TYPE that matches the auth type in the token.
939*4d7e907cSAndroid Build Coastguard Worker     *
940*4d7e907cSAndroid Build Coastguard Worker     *   o The timestamp in the auth token plus the value of the Tag::AUTH_TIMEOUT must be less than
941*4d7e907cSAndroid Build Coastguard Worker     *     the current secure timestamp (which is a monotonic timer counting milliseconds since
942*4d7e907cSAndroid Build Coastguard Worker     *     boot.)
943*4d7e907cSAndroid Build Coastguard Worker     *
944*4d7e907cSAndroid Build Coastguard Worker     *   If any of these conditions are not met, begin() must return
945*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::KEY_USER_NOT_AUTHENTICATED.
946*4d7e907cSAndroid Build Coastguard Worker     *
947*4d7e907cSAndroid Build Coastguard Worker     * o Tag::CALLER_NONCE allows the caller to specify a nonce or initialization vector (IV).  If
948*4d7e907cSAndroid Build Coastguard Worker     *   the key doesn't have this tag, but the caller provided Tag::NONCE to this method,
949*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::CALLER_NONCE_PROHIBITED must be returned.
950*4d7e907cSAndroid Build Coastguard Worker     *
951*4d7e907cSAndroid Build Coastguard Worker     * o Tag::BOOTLOADER_ONLY specifies that only the bootloader may use the key.  If this method is
952*4d7e907cSAndroid Build Coastguard Worker     *   called with a bootloader-only key after the bootloader has finished executing, it must
953*4d7e907cSAndroid Build Coastguard Worker     *   return ErrorCode::INVALID_KEY_BLOB.  The mechanism for notifying the IKeymasterDevice that
954*4d7e907cSAndroid Build Coastguard Worker     *   the bootloader has finished executing is implementation-defined.
955*4d7e907cSAndroid Build Coastguard Worker     *
956*4d7e907cSAndroid Build Coastguard Worker     * -- RSA Keys --
957*4d7e907cSAndroid Build Coastguard Worker     *
958*4d7e907cSAndroid Build Coastguard Worker     * All RSA key operations must specify exactly one padding mode in inParams.  If unspecified or
959*4d7e907cSAndroid Build Coastguard Worker     * specified more than once, the begin() must return ErrorCode::UNSUPPORTED_PADDING_MODE.
960*4d7e907cSAndroid Build Coastguard Worker     *
961*4d7e907cSAndroid Build Coastguard Worker     * RSA signing and verification operations need a digest, as do RSA encryption and decryption
962*4d7e907cSAndroid Build Coastguard Worker     * operations with OAEP padding mode.  For those cases, the caller must specify exactly one
963*4d7e907cSAndroid Build Coastguard Worker     * digest in inParams.  If unspecified or specified more than once, begin() must return
964*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::UNSUPPORTED_DIGEST.
965*4d7e907cSAndroid Build Coastguard Worker     *
966*4d7e907cSAndroid Build Coastguard Worker     * Private key operations (KeyPurpose::DECRYPT and KeyPurpose::SIGN) need authorization of
967*4d7e907cSAndroid Build Coastguard Worker     * digest and padding, which means that the key authorizations need to contain the specified
968*4d7e907cSAndroid Build Coastguard Worker     * values.  If not, begin() must return ErrorCode::INCOMPATIBLE_DIGEST or
969*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::INCOMPATIBLE_PADDING, as appropriate.  Public key operations (KeyPurpose::ENCRYPT
970*4d7e907cSAndroid Build Coastguard Worker     * and KeyPurpose::VERIFY) are permitted with unauthorized digest or padding modes.
971*4d7e907cSAndroid Build Coastguard Worker     *
972*4d7e907cSAndroid Build Coastguard Worker     * With the exception of PaddingMode::NONE, all RSA padding modes are applicable only to certain
973*4d7e907cSAndroid Build Coastguard Worker     * purposes.  Specifically, PaddingMode::RSA_PKCS1_1_5_SIGN and PaddingMode::RSA_PSS only
974*4d7e907cSAndroid Build Coastguard Worker     * support signing and verification, while PaddingMode::RSA_PKCS1_1_5_ENCRYPT and
975*4d7e907cSAndroid Build Coastguard Worker     * PaddingMode::RSA_OAEP only support encryption and decryption.  begin() must return
976*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::UNSUPPORTED_PADDING_MODE if the specified mode does not support the specified
977*4d7e907cSAndroid Build Coastguard Worker     * purpose.
978*4d7e907cSAndroid Build Coastguard Worker     *
979*4d7e907cSAndroid Build Coastguard Worker     * There are some important interactions between padding modes and digests:
980*4d7e907cSAndroid Build Coastguard Worker     *
981*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::NONE indicates that a "raw" RSA operation is performed.  If signing or
982*4d7e907cSAndroid Build Coastguard Worker     *   verifying, Digest::NONE is specified for the digest.  No digest is necessary for unpadded
983*4d7e907cSAndroid Build Coastguard Worker     *   encryption or decryption.
984*4d7e907cSAndroid Build Coastguard Worker     *
985*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::RSA_PKCS1_1_5_SIGN padding requires a digest.  The digest may be Digest::NONE,
986*4d7e907cSAndroid Build Coastguard Worker     *   in which case the Keymaster implementation cannot build a proper PKCS#1 v1.5 signature
987*4d7e907cSAndroid Build Coastguard Worker     *   structure, because it cannot add the DigestInfo structure.  Instead, the IKeymasterDevice
988*4d7e907cSAndroid Build Coastguard Worker     *   must construct 0x00 || 0x01 || PS || 0x00 || M, where M is the provided message and PS is a
989*4d7e907cSAndroid Build Coastguard Worker     *   random padding string at least eight bytes in length.  The size of the RSA key has to be at
990*4d7e907cSAndroid Build Coastguard Worker     *   least 11 bytes larger than the message, otherwise begin() must return
991*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::INVALID_INPUT_LENGTH.
992*4d7e907cSAndroid Build Coastguard Worker     *
993*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::RSA_PKCS1_1_1_5_ENCRYPT padding does not require a digest.
994*4d7e907cSAndroid Build Coastguard Worker     *
995*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::RSA_PSS padding requires a digest, which may not be Digest::NONE.  If
996*4d7e907cSAndroid Build Coastguard Worker     *   Digest::NONE is specified, the begin() must return ErrorCode::INCOMPATIBLE_DIGEST.  In
997*4d7e907cSAndroid Build Coastguard Worker     *   addition, the size of the RSA key must be at least 2 + D bytes larger than the output size
998*4d7e907cSAndroid Build Coastguard Worker     *   of the digest, where D is the size of the digest, in bytes.  Otherwise begin() must
999*4d7e907cSAndroid Build Coastguard Worker     *   return ErrorCode::INCOMPATIBLE_DIGEST.  The salt size must be D.
1000*4d7e907cSAndroid Build Coastguard Worker     *
1001*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::RSA_OAEP padding requires a digest, which may not be Digest::NONE.  If
1002*4d7e907cSAndroid Build Coastguard Worker     *   Digest::NONE is specified, begin() must return ErrorCode::INCOMPATIBLE_DIGEST.  The OAEP
1003*4d7e907cSAndroid Build Coastguard Worker     *   mask generation function must be MGF1 and the MGF1 digest must be SHA1, regardless of the
1004*4d7e907cSAndroid Build Coastguard Worker     *   OAEP digest specified.
1005*4d7e907cSAndroid Build Coastguard Worker     *
1006*4d7e907cSAndroid Build Coastguard Worker     * -- EC Keys --
1007*4d7e907cSAndroid Build Coastguard Worker     *
1008*4d7e907cSAndroid Build Coastguard Worker     * EC private key operations must specify exactly one digest in inParams.  If unspecified or
1009*4d7e907cSAndroid Build Coastguard Worker     * specified more than once, begin() must return ErrorCode::UNSUPPORTED_DIGEST.  For private key
1010*4d7e907cSAndroid Build Coastguard Worker     * operations, (KeyPurpose::SIGN), if the specified digest is not in the key's authorization
1011*4d7e907cSAndroid Build Coastguard Worker     * list, begin() must return ErrorCode::INCOMPATIBLE_DIGEST.  Public key operations
1012*4d7e907cSAndroid Build Coastguard Worker     * (KeyPurpose::VERIFY) are permitted with unauthorized digest.
1013*4d7e907cSAndroid Build Coastguard Worker     *
1014*4d7e907cSAndroid Build Coastguard Worker     * -- AES Keys --
1015*4d7e907cSAndroid Build Coastguard Worker     *
1016*4d7e907cSAndroid Build Coastguard Worker     * AES key operations must specify exactly one block mode (Tag::BLOCK_MODE) and one padding mode
1017*4d7e907cSAndroid Build Coastguard Worker     * (Tag::PADDING) in inParams.  If either value is unspecified or specified more than once,
1018*4d7e907cSAndroid Build Coastguard Worker     * begin() must return ErrorCode::UNSUPPORTED_BLOCK_MODE or
1019*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::UNSUPPORTED_PADDING_MODE.  The specified modes must be authorized by the key,
1020*4d7e907cSAndroid Build Coastguard Worker     * otherwise begin() must return ErrorCode::INCOMPATIBLE_BLOCK_MODE or
1021*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::INCOMPATIBLE_PADDING_MODE.
1022*4d7e907cSAndroid Build Coastguard Worker     *
1023*4d7e907cSAndroid Build Coastguard Worker     * If the block mode is BlockMode::GCM, inParams must specify Tag::MAC_LENGTH, and the specified
1024*4d7e907cSAndroid Build Coastguard Worker     * value must be a multiple of 8 that is not greater than 128 or less than the value of
1025*4d7e907cSAndroid Build Coastguard Worker     * Tag::MIN_MAC_LENGTH in the key authorizations.  For MAC lengths greater than 128 or
1026*4d7e907cSAndroid Build Coastguard Worker     * non-multiples of 8, begin() must return ErrorCode::UNSUPPORTED_MAC_LENGTH.  For values less
1027*4d7e907cSAndroid Build Coastguard Worker     * than the key's minimum length, begin() must return ErrorCode::INVALID_MAC_LENGTH.
1028*4d7e907cSAndroid Build Coastguard Worker     *
1029*4d7e907cSAndroid Build Coastguard Worker     * If the block mode is BlockMode::GCM or BlockMode::CTR, the specified padding mode must be
1030*4d7e907cSAndroid Build Coastguard Worker     * PaddingMode::NONE.  For BlockMode::ECB or BlockMode::CBC, the mode may be PaddingMode::NONE
1031*4d7e907cSAndroid Build Coastguard Worker     * or PaddingMode::PKCS7.  If the padding mode doesn't meet these conditions, begin() must
1032*4d7e907cSAndroid Build Coastguard Worker     * return ErrorCode::INCOMPATIBLE_PADDING_MODE.
1033*4d7e907cSAndroid Build Coastguard Worker     *
1034*4d7e907cSAndroid Build Coastguard Worker     * If the block mode is BlockMode::CBC, BlockMode::CTR, or BlockMode::GCM, an initialization
1035*4d7e907cSAndroid Build Coastguard Worker     * vector or nonce is required.  In most cases, callers shouldn't provide an IV or nonce and the
1036*4d7e907cSAndroid Build Coastguard Worker     * IKeymasterDevice implementation must generate a random IV or nonce and return it via
1037*4d7e907cSAndroid Build Coastguard Worker     * Tag::NONCE in outParams.  CBC and CTR IVs are 16 bytes.  GCM nonces are 12 bytes.  If the key
1038*4d7e907cSAndroid Build Coastguard Worker     * authorizations contain Tag::CALLER_NONCE, then the caller may provide an IV/nonce with
1039*4d7e907cSAndroid Build Coastguard Worker     * Tag::NONCE in inParams.  If a nonce is provided when Tag::CALLER_NONCE is not authorized,
1040*4d7e907cSAndroid Build Coastguard Worker     * begin() must return ErrorCode::CALLER_NONCE_PROHIBITED.  If a nonce is not provided when
1041*4d7e907cSAndroid Build Coastguard Worker     * Tag::CALLER_NONCE is authorized, IKeymasterDevice must generate a random IV/nonce.
1042*4d7e907cSAndroid Build Coastguard Worker     *
1043*4d7e907cSAndroid Build Coastguard Worker     * -- HMAC keys --
1044*4d7e907cSAndroid Build Coastguard Worker     *
1045*4d7e907cSAndroid Build Coastguard Worker     * HMAC key operations must specify Tag::MAC_LENGTH in inParams.  The specified value must be a
1046*4d7e907cSAndroid Build Coastguard Worker     * multiple of 8 that is not greater than the digest length or less than the value of
1047*4d7e907cSAndroid Build Coastguard Worker     * Tag::MIN_MAC_LENGTH in the key authorizations.  For MAC lengths greater than the digest
1048*4d7e907cSAndroid Build Coastguard Worker     * length or non-multiples of 8, begin() must return ErrorCode::UNSUPPORTED_MAC_LENGTH.  For
1049*4d7e907cSAndroid Build Coastguard Worker     * values less than the key's minimum length, begin() must return ErrorCode::INVALID_MAC_LENGTH.
1050*4d7e907cSAndroid Build Coastguard Worker     *
1051*4d7e907cSAndroid Build Coastguard Worker     * @param purpose The purpose of the operation, one of KeyPurpose::ENCRYPT, KeyPurpose::DECRYPT,
1052*4d7e907cSAndroid Build Coastguard Worker     *        KeyPurpose::SIGN or KeyPurpose::VERIFY.  Note that for AEAD modes, encryption and
1053*4d7e907cSAndroid Build Coastguard Worker     *        decryption imply signing and verification, respectively, but must be specified as
1054*4d7e907cSAndroid Build Coastguard Worker     *        KeyPurpose::ENCRYPT and KeyPurpose::DECRYPT.
1055*4d7e907cSAndroid Build Coastguard Worker     *
1056*4d7e907cSAndroid Build Coastguard Worker     * @param keyBlob The opaque key descriptor returned by generateKey() or importKey().  The key
1057*4d7e907cSAndroid Build Coastguard Worker     *        must have a purpose compatible with purpose and all of its usage requirements must be
1058*4d7e907cSAndroid Build Coastguard Worker     *        satisfied, or begin() must return an appropriate error code (see above).
1059*4d7e907cSAndroid Build Coastguard Worker     *
1060*4d7e907cSAndroid Build Coastguard Worker     * @param inParams Additional parameters for the operation.  If Tag::APPLICATION_ID or
1061*4d7e907cSAndroid Build Coastguard Worker     *        Tag::APPLICATION_DATA were provided during generation, they must be provided here, or
1062*4d7e907cSAndroid Build Coastguard Worker     *        the operation must fail with ErrorCode::INVALID_KEY_BLOB.  For operations that require
1063*4d7e907cSAndroid Build Coastguard Worker     *        a nonce or IV, on keys that were generated with Tag::CALLER_NONCE, inParams may
1064*4d7e907cSAndroid Build Coastguard Worker     *        contain a tag Tag::NONCE.  If Tag::NONCE is provided for a key without
1065*4d7e907cSAndroid Build Coastguard Worker     *        Tag:CALLER_NONCE, ErrorCode::CALLER_NONCE_PROHIBITED must be returned.
1066*4d7e907cSAndroid Build Coastguard Worker     *
1067*4d7e907cSAndroid Build Coastguard Worker     * @param authToken Authentication token.  Callers that provide no token must set all numeric
1068*4d7e907cSAndroid Build Coastguard Worker     *        fields to zero and the MAC must be an empty vector.
1069*4d7e907cSAndroid Build Coastguard Worker     *
1070*4d7e907cSAndroid Build Coastguard Worker     * @return outParams Output parameters.  Used to return additional data from the operation
1071*4d7e907cSAndroid Build Coastguard Worker     *         initialization, notably to return the IV or nonce from operations that generate an IV
1072*4d7e907cSAndroid Build Coastguard Worker     *         or nonce.
1073*4d7e907cSAndroid Build Coastguard Worker     *
1074*4d7e907cSAndroid Build Coastguard Worker     * @return operationHandle The newly-created operation handle which must be passed to update(),
1075*4d7e907cSAndroid Build Coastguard Worker     *         finish() or abort().
1076*4d7e907cSAndroid Build Coastguard Worker     */
1077*4d7e907cSAndroid Build Coastguard Worker    begin(KeyPurpose purpose, vec<uint8_t> keyBlob, vec<KeyParameter> inParams,
1078*4d7e907cSAndroid Build Coastguard Worker          HardwareAuthToken authToken)
1079*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<KeyParameter> outParams, OperationHandle operationHandle);
1080*4d7e907cSAndroid Build Coastguard Worker
1081*4d7e907cSAndroid Build Coastguard Worker    /**
1082*4d7e907cSAndroid Build Coastguard Worker     * Provides data to, and possibly receives output from, an ongoing cryptographic operation begun
1083*4d7e907cSAndroid Build Coastguard Worker     * with begin().  The operation is specified by the operationHandle parameter.
1084*4d7e907cSAndroid Build Coastguard Worker     *
1085*4d7e907cSAndroid Build Coastguard Worker     * If operationHandle is invalid, update() must return ErrorCode::INVALID_OPERATION_HANDLE.
1086*4d7e907cSAndroid Build Coastguard Worker     *
1087*4d7e907cSAndroid Build Coastguard Worker     * To provide more flexibility for buffer handling, implementations of this method have the
1088*4d7e907cSAndroid Build Coastguard Worker     * option of consuming less data than was provided.  The caller is responsible for looping to
1089*4d7e907cSAndroid Build Coastguard Worker     * feed the rest of the data in subsequent calls.  The amount of input consumed must be returned
1090*4d7e907cSAndroid Build Coastguard Worker     * in the inputConsumed parameter.  Implementations must always consume at least one byte, unless
1091*4d7e907cSAndroid Build Coastguard Worker     * the operation cannot accept any more; if more than zero bytes are provided and zero bytes are
1092*4d7e907cSAndroid Build Coastguard Worker     * consumed, callers must consider this an error and abort the operation.
1093*4d7e907cSAndroid Build Coastguard Worker     *
1094*4d7e907cSAndroid Build Coastguard Worker     * Implementations may also choose how much data to return, as a result of the update.  This is
1095*4d7e907cSAndroid Build Coastguard Worker     * only relevant for encryption and decryption operations, because signing and verification
1096*4d7e907cSAndroid Build Coastguard Worker     * return no data until finish.  It is recommended to return data as early as possible, rather
1097*4d7e907cSAndroid Build Coastguard Worker     * than buffer it.
1098*4d7e907cSAndroid Build Coastguard Worker     *
1099*4d7e907cSAndroid Build Coastguard Worker     * If this method returns an error code other than ErrorCode::OK, the operation is aborted and
1100*4d7e907cSAndroid Build Coastguard Worker     * the operation handle must be invalidated.  Any future use of the handle, with this method,
1101*4d7e907cSAndroid Build Coastguard Worker     * finish, or abort, must return ErrorCode::INVALID_OPERATION_HANDLE.
1102*4d7e907cSAndroid Build Coastguard Worker     *
1103*4d7e907cSAndroid Build Coastguard Worker     * == Authorization Enforcement ==
1104*4d7e907cSAndroid Build Coastguard Worker     *
1105*4d7e907cSAndroid Build Coastguard Worker     * Key authorization enforcement is performed primarily in begin().  The one exception is the
1106*4d7e907cSAndroid Build Coastguard Worker     * case where the key has:
1107*4d7e907cSAndroid Build Coastguard Worker
1108*4d7e907cSAndroid Build Coastguard Worker     * o One or more Tag::USER_SECURE_IDs, and
1109*4d7e907cSAndroid Build Coastguard Worker     *
1110*4d7e907cSAndroid Build Coastguard Worker     * o Does not have a Tag::AUTH_TIMEOUT
1111*4d7e907cSAndroid Build Coastguard Worker     *
1112*4d7e907cSAndroid Build Coastguard Worker     * In this case, the key requires an authorization per operation, and the update method must
1113*4d7e907cSAndroid Build Coastguard Worker     * receive a non-empty and valid HardwareAuthToken.  For the auth token to be valid, all of the
1114*4d7e907cSAndroid Build Coastguard Worker     * following has to be true:
1115*4d7e907cSAndroid Build Coastguard Worker     *
1116*4d7e907cSAndroid Build Coastguard Worker     *   o The HMAC field must validate correctly.
1117*4d7e907cSAndroid Build Coastguard Worker     *
1118*4d7e907cSAndroid Build Coastguard Worker     *   o At least one of the Tag::USER_SECURE_ID values from the key must match at least one of
1119*4d7e907cSAndroid Build Coastguard Worker     *     the secure ID values in the token.
1120*4d7e907cSAndroid Build Coastguard Worker     *
1121*4d7e907cSAndroid Build Coastguard Worker     *   o The key must have a Tag::USER_AUTH_TYPE that matches the auth type in the token.
1122*4d7e907cSAndroid Build Coastguard Worker     *
1123*4d7e907cSAndroid Build Coastguard Worker     *   o The challenge field in the auth token must contain the operationHandle
1124*4d7e907cSAndroid Build Coastguard Worker     *
1125*4d7e907cSAndroid Build Coastguard Worker     *   If any of these conditions are not met, update() must return
1126*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::KEY_USER_NOT_AUTHENTICATED.
1127*4d7e907cSAndroid Build Coastguard Worker     *
1128*4d7e907cSAndroid Build Coastguard Worker     * The caller must provide the auth token on every call to update() and finish().
1129*4d7e907cSAndroid Build Coastguard Worker     *
1130*4d7e907cSAndroid Build Coastguard Worker     * -- RSA keys --
1131*4d7e907cSAndroid Build Coastguard Worker     *
1132*4d7e907cSAndroid Build Coastguard Worker     * For signing and verification operations with Digest::NONE, this method must accept the entire
1133*4d7e907cSAndroid Build Coastguard Worker     * block to be signed or verified in a single update.  It may not consume only a portion of the
1134*4d7e907cSAndroid Build Coastguard Worker     * block in these cases.  However, the caller may choose to provide the data in multiple updates,
1135*4d7e907cSAndroid Build Coastguard Worker     * and update() must accept the data this way as well.  If the caller provides more data to sign
1136*4d7e907cSAndroid Build Coastguard Worker     * than can be used (length of data exceeds RSA key size), update() must return
1137*4d7e907cSAndroid Build Coastguard Worker     * ErrorCode::INVALID_INPUT_LENGTH.
1138*4d7e907cSAndroid Build Coastguard Worker     *
1139*4d7e907cSAndroid Build Coastguard Worker     * -- ECDSA keys --
1140*4d7e907cSAndroid Build Coastguard Worker     *
1141*4d7e907cSAndroid Build Coastguard Worker     * For signing and verification operations with Digest::NONE, this method must accept the entire
1142*4d7e907cSAndroid Build Coastguard Worker     * block to be signed or verified in a single update.  This method may not consume only a
1143*4d7e907cSAndroid Build Coastguard Worker     * portion of the block.  However, the caller may choose to provide the data in multiple updates
1144*4d7e907cSAndroid Build Coastguard Worker     * and update() must accept the data this way as well.  If the caller provides more data to sign
1145*4d7e907cSAndroid Build Coastguard Worker     * than can be used, the data is silently truncated.  (This differs from the handling of excess
1146*4d7e907cSAndroid Build Coastguard Worker     * data provided in similar RSA operations.  The reason for this is compatibility with legacy
1147*4d7e907cSAndroid Build Coastguard Worker     * clients.)
1148*4d7e907cSAndroid Build Coastguard Worker     *
1149*4d7e907cSAndroid Build Coastguard Worker     * -- AES keys --
1150*4d7e907cSAndroid Build Coastguard Worker     *
1151*4d7e907cSAndroid Build Coastguard Worker     * AES GCM mode supports "associated authentication data," provided via the Tag::ASSOCIATED_DATA
1152*4d7e907cSAndroid Build Coastguard Worker     * tag in the inParams argument.  The associated data may be provided in repeated calls
1153*4d7e907cSAndroid Build Coastguard Worker     * (important if the data is too large to send in a single block) but must always precede data
1154*4d7e907cSAndroid Build Coastguard Worker     * to be encrypted or decrypted.  An update call may receive both associated data and data to
1155*4d7e907cSAndroid Build Coastguard Worker     * encrypt/decrypt, but subsequent updates must not include associated data.  If the caller
1156*4d7e907cSAndroid Build Coastguard Worker     * provides associated data to an update call after a call that includes data to
1157*4d7e907cSAndroid Build Coastguard Worker     * encrypt/decrypt, update() must return ErrorCode::INVALID_TAG.
1158*4d7e907cSAndroid Build Coastguard Worker     *
1159*4d7e907cSAndroid Build Coastguard Worker     * For GCM encryption, the AEAD tag must be appended to the ciphertext by finish().  During
1160*4d7e907cSAndroid Build Coastguard Worker     * decryption, the last Tag::MAC_LENGTH bytes of the data provided to the last update call must
1161*4d7e907cSAndroid Build Coastguard Worker     * be the AEAD tag.  Since a given invocation of update cannot know if it's the last invocation,
1162*4d7e907cSAndroid Build Coastguard Worker     * it must process all but the tag length and buffer the possible tag data for processing during
1163*4d7e907cSAndroid Build Coastguard Worker     * finish().
1164*4d7e907cSAndroid Build Coastguard Worker     *
1165*4d7e907cSAndroid Build Coastguard Worker     * @param operationHandle The operation handle returned by begin().
1166*4d7e907cSAndroid Build Coastguard Worker     *
1167*4d7e907cSAndroid Build Coastguard Worker     * @param inParams Additional parameters for the operation.  For AEAD modes, this is used to
1168*4d7e907cSAndroid Build Coastguard Worker     *        specify Tag::ADDITIONAL_DATA.  Note that additional data may be provided in multiple
1169*4d7e907cSAndroid Build Coastguard Worker     *        calls to update(), but only until input data has been provided.
1170*4d7e907cSAndroid Build Coastguard Worker     *
1171*4d7e907cSAndroid Build Coastguard Worker     * @param input Data to be processed.  Note that update() may or may not consume all of the data
1172*4d7e907cSAndroid Build Coastguard Worker     *        provided.  See inputConsumed.
1173*4d7e907cSAndroid Build Coastguard Worker     *
1174*4d7e907cSAndroid Build Coastguard Worker     * @param authToken Authentication token.  Callers that provide no token must set all numeric
1175*4d7e907cSAndroid Build Coastguard Worker     *        fields to zero and the MAC must be an empty vector.
1176*4d7e907cSAndroid Build Coastguard Worker     *
1177*4d7e907cSAndroid Build Coastguard Worker     * @param verificationToken Verification token, used to prove that another IKeymasterDevice HAL
1178*4d7e907cSAndroid Build Coastguard Worker     *        has verified some parameters, and to deliver the other HAL's current timestamp, if
1179*4d7e907cSAndroid Build Coastguard Worker     *        needed.  If not provided, all fields must be initialized to zero and vectors must be
1180*4d7e907cSAndroid Build Coastguard Worker     *        empty.
1181*4d7e907cSAndroid Build Coastguard Worker     *
1182*4d7e907cSAndroid Build Coastguard Worker     * @return error See the ErrorCode enum in types.hal.
1183*4d7e907cSAndroid Build Coastguard Worker     *
1184*4d7e907cSAndroid Build Coastguard Worker     * @return inputConsumed Amount of data that was consumed by update().  If this is less than the
1185*4d7e907cSAndroid Build Coastguard Worker     *         amount provided, the caller may provide the remainder in a subsequent call to
1186*4d7e907cSAndroid Build Coastguard Worker     *         update() or finish().  Every call to update must consume at least one byte, unless
1187*4d7e907cSAndroid Build Coastguard Worker     *         the input is empty, and implementations should consume as much data as reasonably
1188*4d7e907cSAndroid Build Coastguard Worker     *         possible for each call.
1189*4d7e907cSAndroid Build Coastguard Worker     *
1190*4d7e907cSAndroid Build Coastguard Worker     * @return outParams Output parameters, used to return additional data from the operation.
1191*4d7e907cSAndroid Build Coastguard Worker     *
1192*4d7e907cSAndroid Build Coastguard Worker     * @return output The output data, if any.
1193*4d7e907cSAndroid Build Coastguard Worker     */
1194*4d7e907cSAndroid Build Coastguard Worker    update(OperationHandle operationHandle, vec<KeyParameter> inParams, vec<uint8_t> input,
1195*4d7e907cSAndroid Build Coastguard Worker           HardwareAuthToken authToken, VerificationToken verificationToken)
1196*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, uint32_t inputConsumed, vec<KeyParameter> outParams,
1197*4d7e907cSAndroid Build Coastguard Worker                   vec<uint8_t> output);
1198*4d7e907cSAndroid Build Coastguard Worker
1199*4d7e907cSAndroid Build Coastguard Worker    /**
1200*4d7e907cSAndroid Build Coastguard Worker     * Finalizes a cryptographic operation begun with begin() and invalidates operationHandle.
1201*4d7e907cSAndroid Build Coastguard Worker     *
1202*4d7e907cSAndroid Build Coastguard Worker     * This method is the last one called in an operation, so all processed data must be returned.
1203*4d7e907cSAndroid Build Coastguard Worker     *
1204*4d7e907cSAndroid Build Coastguard Worker     * Whether it completes successfully or returns an error, this method finalizes the operation
1205*4d7e907cSAndroid Build Coastguard Worker     * and therefore must invalidate the provided operation handle.  Any future use of the handle,
1206*4d7e907cSAndroid Build Coastguard Worker     * with finish(), update(), or abort(), must return ErrorCode::INVALID_OPERATION_HANDLE.
1207*4d7e907cSAndroid Build Coastguard Worker     *
1208*4d7e907cSAndroid Build Coastguard Worker     * Signing operations return the signature as the output.  Verification operations accept the
1209*4d7e907cSAndroid Build Coastguard Worker     * signature in the signature parameter, and return no output.
1210*4d7e907cSAndroid Build Coastguard Worker     *
1211*4d7e907cSAndroid Build Coastguard Worker     * == Authorization enforcement ==
1212*4d7e907cSAndroid Build Coastguard Worker     *
1213*4d7e907cSAndroid Build Coastguard Worker     * Key authorization enforcement is performed primarily in begin().  The exceptions are
1214*4d7e907cSAndroid Build Coastguard Worker     * authorization per operation keys and confirmation-required keys.
1215*4d7e907cSAndroid Build Coastguard Worker     *
1216*4d7e907cSAndroid Build Coastguard Worker     * Authorization per operation keys are the case where the key has one or more
1217*4d7e907cSAndroid Build Coastguard Worker     * Tag::USER_SECURE_IDs, and does not have a Tag::AUTH_TIMEOUT.  In this case, the key requires
1218*4d7e907cSAndroid Build Coastguard Worker     * an authorization per operation, and the finish method must receive a non-empty and valid
1219*4d7e907cSAndroid Build Coastguard Worker     * authToken.  For the auth token to be valid, all of the following has to be true:
1220*4d7e907cSAndroid Build Coastguard Worker     *
1221*4d7e907cSAndroid Build Coastguard Worker     *   o The HMAC field must validate correctly.
1222*4d7e907cSAndroid Build Coastguard Worker     *
1223*4d7e907cSAndroid Build Coastguard Worker     *   o At least one of the Tag::USER_SECURE_ID values from the key must match at least one of
1224*4d7e907cSAndroid Build Coastguard Worker     *     the secure ID values in the token.
1225*4d7e907cSAndroid Build Coastguard Worker     *
1226*4d7e907cSAndroid Build Coastguard Worker     *   o The key must have a Tag::USER_AUTH_TYPE that matches the auth type in the token.
1227*4d7e907cSAndroid Build Coastguard Worker     *
1228*4d7e907cSAndroid Build Coastguard Worker     *   o The challenge field in the auth token must contain the operationHandle
1229*4d7e907cSAndroid Build Coastguard Worker     *
1230*4d7e907cSAndroid Build Coastguard Worker     *   If any of these conditions are not met, update() must return
1231*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::KEY_USER_NOT_AUTHENTICATED.
1232*4d7e907cSAndroid Build Coastguard Worker     *
1233*4d7e907cSAndroid Build Coastguard Worker     * The caller must provide the auth token on every call to update() and finish().
1234*4d7e907cSAndroid Build Coastguard Worker     *
1235*4d7e907cSAndroid Build Coastguard Worker     * Confirmation-required keys are keys that were generated with
1236*4d7e907cSAndroid Build Coastguard Worker     * Tag::TRUSTED_CONFIRMATION_REQUIRED.  For these keys, when doing a signing operation the
1237*4d7e907cSAndroid Build Coastguard Worker     * caller must pass a KeyParameter Tag::CONFIRMATION_TOKEN to finish().  Implementations must
1238*4d7e907cSAndroid Build Coastguard Worker     * check the confirmation token by computing the 32-byte HMAC-SHA256 over all of the
1239*4d7e907cSAndroid Build Coastguard Worker     * to-be-signed data, prefixed with the 18-byte UTF-8 encoded string "confirmation token". If
1240*4d7e907cSAndroid Build Coastguard Worker     * the computed value does not match the Tag::CONFIRMATION_TOKEN parameter, finish() must not
1241*4d7e907cSAndroid Build Coastguard Worker     * produce a signature and must return ErrorCode::NO_USER_CONFIRMATION.
1242*4d7e907cSAndroid Build Coastguard Worker     *
1243*4d7e907cSAndroid Build Coastguard Worker     * -- RSA keys --
1244*4d7e907cSAndroid Build Coastguard Worker     *
1245*4d7e907cSAndroid Build Coastguard Worker     * Some additional requirements, depending on the padding mode:
1246*4d7e907cSAndroid Build Coastguard Worker     *
1247*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::NONE.  For unpadded signing and encryption operations, if the provided data is
1248*4d7e907cSAndroid Build Coastguard Worker     *   shorter than the key, the data must be zero-padded on the left before
1249*4d7e907cSAndroid Build Coastguard Worker     *   signing/encryption.  If the data is the same length as the key, but numerically larger,
1250*4d7e907cSAndroid Build Coastguard Worker     *   finish() must return ErrorCode::INVALID_ARGUMENT.  For verification and decryption
1251*4d7e907cSAndroid Build Coastguard Worker     *   operations, the data must be exactly as long as the key.  Otherwise, return
1252*4d7e907cSAndroid Build Coastguard Worker     *   ErrorCode::INVALID_INPUT_LENGTH.
1253*4d7e907cSAndroid Build Coastguard Worker     *
1254*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::RSA_PSS.  For PSS-padded signature operations, the PSS salt length must match
1255*4d7e907cSAndroid Build Coastguard Worker     *   the size of the PSS digest selected.  The digest specified with Tag::DIGEST in inputParams
1256*4d7e907cSAndroid Build Coastguard Worker     *   on begin() must be used as the PSS digest algorithm, MGF1 must be used as the mask
1257*4d7e907cSAndroid Build Coastguard Worker     *   generation function and the digest specified with Tag:DIGEST in inputParams must also be
1258*4d7e907cSAndroid Build Coastguard Worker     *   used as the MGF1 digest algorithm.
1259*4d7e907cSAndroid Build Coastguard Worker     *
1260*4d7e907cSAndroid Build Coastguard Worker     * o PaddingMode::RSA_OAEP.  The digest specified with Tag::DIGEST in inputParams on begin is
1261*4d7e907cSAndroid Build Coastguard Worker     *   used as the OAEP digest algorithm, MGF1 must be used as the mask generation function and
1262*4d7e907cSAndroid Build Coastguard Worker     *   and SHA1 must be used as the MGF1 digest algorithm.
1263*4d7e907cSAndroid Build Coastguard Worker     *
1264*4d7e907cSAndroid Build Coastguard Worker     * -- ECDSA keys --
1265*4d7e907cSAndroid Build Coastguard Worker     *
1266*4d7e907cSAndroid Build Coastguard Worker     * If the data provided for unpadded signing or verification is too long, truncate it.
1267*4d7e907cSAndroid Build Coastguard Worker     *
1268*4d7e907cSAndroid Build Coastguard Worker     * -- AES keys --
1269*4d7e907cSAndroid Build Coastguard Worker     *
1270*4d7e907cSAndroid Build Coastguard Worker     * Some additional conditions, depending on block mode:
1271*4d7e907cSAndroid Build Coastguard Worker     *
1272*4d7e907cSAndroid Build Coastguard Worker     * o BlockMode::ECB or BlockMode::CBC.  If padding is PaddingMode::NONE and the data length is
1273*4d7e907cSAndroid Build Coastguard Worker     *  not a multiple of the AES block size, finish() must return
1274*4d7e907cSAndroid Build Coastguard Worker     *  ErrorCode::INVALID_INPUT_LENGTH.  If padding is PaddingMode::PKCS7, pad the data per the
1275*4d7e907cSAndroid Build Coastguard Worker     *  PKCS#7 specification, including adding an additional padding block if the data is a multiple
1276*4d7e907cSAndroid Build Coastguard Worker     *  of the block length.
1277*4d7e907cSAndroid Build Coastguard Worker     *
1278*4d7e907cSAndroid Build Coastguard Worker     * o BlockMode::GCM.  During encryption, after processing all plaintext, compute the tag
1279*4d7e907cSAndroid Build Coastguard Worker     *   (Tag::MAC_LENGTH bytes) and append it to the returned ciphertext.  During decryption,
1280*4d7e907cSAndroid Build Coastguard Worker     *   process the last Tag::MAC_LENGTH bytes as the tag.  If tag verification fails, finish()
1281*4d7e907cSAndroid Build Coastguard Worker     *   must return ErrorCode::VERIFICATION_FAILED.
1282*4d7e907cSAndroid Build Coastguard Worker     *
1283*4d7e907cSAndroid Build Coastguard Worker     * @param operationHandle The operation handle returned by begin().  This handle must be invalid
1284*4d7e907cSAndroid Build Coastguard Worker     *        when finish() returns.
1285*4d7e907cSAndroid Build Coastguard Worker     *
1286*4d7e907cSAndroid Build Coastguard Worker     * @param inParams Additional parameters for the operation.  For AEAD modes, this is used to
1287*4d7e907cSAndroid Build Coastguard Worker     *        specify Tag::ADDITIONAL_DATA, but only if no input data was provided to update().
1288*4d7e907cSAndroid Build Coastguard Worker     *
1289*4d7e907cSAndroid Build Coastguard Worker     * @param input Data to be processed, per the parameters established in the call to begin().
1290*4d7e907cSAndroid Build Coastguard Worker     *        finish() must consume all provided data or return ErrorCode::INVALID_INPUT_LENGTH.
1291*4d7e907cSAndroid Build Coastguard Worker     *
1292*4d7e907cSAndroid Build Coastguard Worker     * @param signature The signature to be verified if the purpose specified in the begin() call
1293*4d7e907cSAndroid Build Coastguard Worker     *        was KeyPurpose::VERIFY.
1294*4d7e907cSAndroid Build Coastguard Worker     *
1295*4d7e907cSAndroid Build Coastguard Worker     * @param authToken Authentication token.  Callers that provide no token must set all numeric
1296*4d7e907cSAndroid Build Coastguard Worker     *        fields to zero and the MAC must be an empty vector.
1297*4d7e907cSAndroid Build Coastguard Worker     *
1298*4d7e907cSAndroid Build Coastguard Worker     * @param verificationToken Verification token, used to prove that another IKeymasterDevice HAL
1299*4d7e907cSAndroid Build Coastguard Worker     *        has verified some parameters, and to deliver the other HAL's current timestamp, if
1300*4d7e907cSAndroid Build Coastguard Worker     *        needed.  If not provided, all fields must be initialized to zero and vectors empty.
1301*4d7e907cSAndroid Build Coastguard Worker     *
1302*4d7e907cSAndroid Build Coastguard Worker     * @return outParams Any output parameters generated by finish().
1303*4d7e907cSAndroid Build Coastguard Worker     *
1304*4d7e907cSAndroid Build Coastguard Worker     * @return output The output data, if any.
1305*4d7e907cSAndroid Build Coastguard Worker     */
1306*4d7e907cSAndroid Build Coastguard Worker    finish(OperationHandle operationHandle, vec<KeyParameter> inParams, vec<uint8_t> input,
1307*4d7e907cSAndroid Build Coastguard Worker           vec<uint8_t> signature, HardwareAuthToken authToken, VerificationToken verificationToken)
1308*4d7e907cSAndroid Build Coastguard Worker        generates (ErrorCode error, vec<KeyParameter> outParams, vec<uint8_t> output);
1309*4d7e907cSAndroid Build Coastguard Worker
1310*4d7e907cSAndroid Build Coastguard Worker    /**
1311*4d7e907cSAndroid Build Coastguard Worker     * Aborts a cryptographic operation begun with begin(), freeing all internal resources and
1312*4d7e907cSAndroid Build Coastguard Worker     * invalidating operationHandle.
1313*4d7e907cSAndroid Build Coastguard Worker     *
1314*4d7e907cSAndroid Build Coastguard Worker     * @param operationHandle The operation handle returned by begin().  This handle must be
1315*4d7e907cSAndroid Build Coastguard Worker     *        invalid when abort() returns.
1316*4d7e907cSAndroid Build Coastguard Worker     *
1317*4d7e907cSAndroid Build Coastguard Worker     * @return error See the ErrorCode enum in types.hal.
1318*4d7e907cSAndroid Build Coastguard Worker     */
1319*4d7e907cSAndroid Build Coastguard Worker    abort(OperationHandle operationHandle) generates (ErrorCode error);
1320*4d7e907cSAndroid Build Coastguard Worker};
1321