blob: d9ec7f36d17a7d4fe5e1f190bb96a700f5f2f48f [file] [log] [blame]
Hasini Gunasinghe12486362020-07-24 18:40:20 +00001// Copyright 2020, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! KeyParameter is used to express different characteristics of a key requested by the user
16//! and enforced by the OEMs. This module implements the internal representation of KeyParameter
17//! and the methods to work with KeyParameter.
18
19use crate::keymint_definitions::{
20 Algorithm, BlockMode, Digest, EcCurve, HardwareAuthenticatorType, KeyBlobUsageRequirements,
21 KeyOrigin, KeyPurpose, PaddingMode, SecurityLevel, Tag,
22};
23
24/// KeyParameter wraps the KeyParameterValue and the security level at which it is enforced.
25pub struct KeyParameter {
26 key_parameter_value: KeyParameterValue,
27 security_level: SecurityLevel,
28}
29
30/// KeyParameterValue holds a value corresponding to one of the Tags defined in
31/// the AIDL spec at hardware/interfaces/keymint
32#[derive(PartialEq, Debug)]
33pub enum KeyParameterValue {
34 /// Associated with Tag:INVALID
35 Invalid,
36 /// Set of purposes for which the key may be used
37 KeyPurpose(KeyPurpose),
38 /// Cryptographic algorithm with which the key is used
39 Algorithm(Algorithm),
40 /// Size of the key , in bits
41 KeySize(u32),
42 /// Block cipher mode(s) with which the key may be used
43 BlockMode(BlockMode),
44 /// Digest algorithms that may be used with the key to perform signing and verification
45 Digest(Digest),
46 /// Padding modes that may be used with the key. Relevant to RSA, AES and 3DES keys.
47 PaddingMode(PaddingMode),
48 /// Can the caller provide a nonce for nonce-requiring operations
49 CallerNonce,
50 /// Minimum length of MAC for HMAC keys and AES keys that support GCM mode
51 MinMacLength(u32),
52 /// The elliptic curve
53 EcCurve(EcCurve),
54 /// Value of the public exponent for an RSA key pair
55 RSAPublicExponent(u64),
56 /// An attestation certificate for the generated key should contain an application-scoped
57 /// and time-bounded device-unique ID
58 IncludeUniqueID,
59 /// Necessary system environment conditions for the generated key to be used
60 KeyBlobUsageRequirements(KeyBlobUsageRequirements),
61 /// Only the boot loader can use the key
62 BootLoaderOnly,
63 /// When deleted, the key is guaranteed to be permanently deleted and unusable
64 RollbackResistance,
65 //TODO: HARDWARE_TYPE reserved for future use
66 /// The date and time at which the key becomes active
67 ActiveDateTime(u64),
68 /// The date and time at which the key expires for signing and encryption
69 OriginationExpireDateTime(u64),
70 /// The date and time at which the key expires for verification and decryption
71 UsageExpireDateTime(u64),
72 /// Minimum amount of time that elapses between allowed operations
73 MinSecondsBetweenOps(u32),
74 /// Maximum number of times that a key may be used between system reboots
75 MaxUsesPerBoot(u32),
76 /// ID of the Android user that is permitted to use the key
77 UserID(u32),
78 /// A key may only be used under a particular secure user authentication state
79 UserSecureID(u64),
80 /// No authentication is required to use this key
81 NoAuthRequired,
82 /// The types of user authenticators that may be used to authorize this key
83 HardwareAuthenticatorType(HardwareAuthenticatorType),
84 /// The time in seconds for which the key is authorized for use, after user authentication
85 AuthTimeout(u32),
86 /// The key may be used after authentication timeout if device is still on-body
87 AllowWhileOnBody,
88 /// The key must be unusable except when the user has provided proof of physical presence
89 TrustedUserPresenceRequired,
90 /// Applicable to keys with KeyPurpose SIGN, and specifies that this key must not be usable
91 /// unless the user provides confirmation of the data to be signed
92 TrustedConfirmationRequired,
93 /// The key may only be used when the device is unlocked
94 UnlockedDeviceRequired,
95 /// When provided to generateKey or importKey, this tag specifies data
96 /// that is necessary during all uses of the key
97 ApplicationID(Vec<u8>),
98 /// When provided to generateKey or importKey, this tag specifies data
99 /// that is necessary during all uses of the key
100 ApplicationData(Vec<u8>),
101 /// Specifies the date and time the key was created
102 CreationDateTime(u64),
103 /// Specifies where the key was created, if known
104 KeyOrigin(KeyOrigin),
105 /// The key used by verified boot to validate the operating system booted
106 RootOfTrust(Vec<u8>),
107 /// System OS version with which the key may be used
108 OSVersion(u32),
109 /// Specifies the system security patch level with which the key may be used
110 OSPatchLevel(u32),
111 /// Specifies a unique, time-based identifier
112 UniqueID(Vec<u8>),
113 /// Used to deliver a "challenge" value to the attestKey() method
114 AttestationChallenge(Vec<u8>),
115 /// The set of applications which may use a key, used only with attestKey()
116 AttestationApplicationID(Vec<u8>),
117 /// Provides the device's brand name, to attestKey()
118 AttestationIdBrand(Vec<u8>),
119 /// Provides the device's device name, to attestKey()
120 AttestationIdDevice(Vec<u8>),
121 /// Provides the device's product name, to attestKey()
122 AttestationIdProduct(Vec<u8>),
123 /// Provides the device's serial number, to attestKey()
124 AttestationIdSerial(Vec<u8>),
125 /// Provides the IMEIs for all radios on the device, to attestKey()
126 AttestationIdIMEI(Vec<u8>),
127 /// Provides the MEIDs for all radios on the device, to attestKey()
128 AttestationIdMEID(Vec<u8>),
129 /// Provides the device's manufacturer name, to attestKey()
130 AttestationIdManufacturer(Vec<u8>),
131 /// Provides the device's model name, to attestKey()
132 AttestationIdModel(Vec<u8>),
133 /// Specifies the vendor image security patch level with which the key may be used
134 VendorPatchLevel(u32),
135 /// Specifies the boot image (kernel) security patch level with which the key may be used
136 BootPatchLevel(u32),
137 /// Provides "associated data" for AES-GCM encryption or decryption
138 AssociatedData(Vec<u8>),
139 /// Provides or returns a nonce or Initialization Vector (IV) for AES-GCM,
140 /// AES-CBC, AES-CTR, or 3DES-CBC encryption or decryption
141 Nonce(Vec<u8>),
142 /// Provides the requested length of a MAC or GCM authentication tag, in bits
143 MacLength(u32),
144 /// Specifies whether the device has been factory reset since the
145 /// last unique ID rotation. Used for key attestation
146 ResetSinceIdRotation,
147 /// Used to deliver a cryptographic token proving that the user
148 /// confirmed a signing request
149 ConfirmationToken(Vec<u8>),
150}
151
152impl KeyParameter {
153 /// Create an instance of KeyParameter, given the value and the security level.
154 pub fn new(key_parameter_value: KeyParameterValue, security_level: SecurityLevel) -> Self {
155 KeyParameter { key_parameter_value, security_level }
156 }
157
158 /// Returns the tag given the KeyParameter instance.
159 pub fn get_tag(&self) -> Tag {
160 match self.key_parameter_value {
161 KeyParameterValue::Invalid => Tag::INVALID,
162 KeyParameterValue::KeyPurpose(_) => Tag::PURPOSE,
163 KeyParameterValue::Algorithm(_) => Tag::ALGORITHM,
164 KeyParameterValue::KeySize(_) => Tag::KEY_SIZE,
165 KeyParameterValue::BlockMode(_) => Tag::BLOCK_MODE,
166 KeyParameterValue::Digest(_) => Tag::DIGEST,
167 KeyParameterValue::PaddingMode(_) => Tag::PADDING,
168 KeyParameterValue::CallerNonce => Tag::CALLER_NONCE,
169 KeyParameterValue::MinMacLength(_) => Tag::MIN_MAC_LENGTH,
170 KeyParameterValue::EcCurve(_) => Tag::EC_CURVE,
171 KeyParameterValue::RSAPublicExponent(_) => Tag::RSA_PUBLIC_EXPONENT,
172 KeyParameterValue::IncludeUniqueID => Tag::INCLUDE_UNIQUE_ID,
173 KeyParameterValue::KeyBlobUsageRequirements(_) => Tag::BLOB_USAGE_REQUIREMENTS,
174 KeyParameterValue::BootLoaderOnly => Tag::BOOTLOADER_ONLY,
175 KeyParameterValue::RollbackResistance => Tag::ROLLBACK_RESISTANCE,
176 KeyParameterValue::ActiveDateTime(_) => Tag::ACTIVE_DATETIME,
177 KeyParameterValue::OriginationExpireDateTime(_) => Tag::ORIGINATION_EXPIRE_DATETIME,
178 KeyParameterValue::UsageExpireDateTime(_) => Tag::USAGE_EXPIRE_DATETIME,
179 KeyParameterValue::MinSecondsBetweenOps(_) => Tag::MIN_SECONDS_BETWEEN_OPS,
180 KeyParameterValue::MaxUsesPerBoot(_) => Tag::MAX_USES_PER_BOOT,
181 KeyParameterValue::UserID(_) => Tag::USER_ID,
182 KeyParameterValue::UserSecureID(_) => Tag::USER_SECURE_ID,
183 KeyParameterValue::NoAuthRequired => Tag::NO_AUTH_REQUIRED,
184 KeyParameterValue::HardwareAuthenticatorType(_) => Tag::USER_AUTH_TYPE,
185 KeyParameterValue::AuthTimeout(_) => Tag::AUTH_TIMEOUT,
186 KeyParameterValue::AllowWhileOnBody => Tag::ALLOW_WHILE_ON_BODY,
187 KeyParameterValue::TrustedUserPresenceRequired => Tag::TRUSTED_USER_PRESENCE_REQUIRED,
188 KeyParameterValue::TrustedConfirmationRequired => Tag::TRUSTED_CONFIRMATION_REQUIRED,
189 KeyParameterValue::UnlockedDeviceRequired => Tag::UNLOCKED_DEVICE_REQUIRED,
190 KeyParameterValue::ApplicationID(_) => Tag::APPLICATION_ID,
191 KeyParameterValue::ApplicationData(_) => Tag::APPLICATION_DATA,
192 KeyParameterValue::CreationDateTime(_) => Tag::CREATION_DATETIME,
193 KeyParameterValue::KeyOrigin(_) => Tag::ORIGIN,
194 KeyParameterValue::RootOfTrust(_) => Tag::ROOT_OF_TRUST,
195 KeyParameterValue::OSVersion(_) => Tag::OS_VERSION,
196 KeyParameterValue::OSPatchLevel(_) => Tag::OS_PATCHLEVEL,
197 KeyParameterValue::UniqueID(_) => Tag::UNIQUE_ID,
198 KeyParameterValue::AttestationChallenge(_) => Tag::ATTESTATION_CHALLENGE,
199 KeyParameterValue::AttestationApplicationID(_) => Tag::ATTESTATION_APPLICATION_ID,
200 KeyParameterValue::AttestationIdBrand(_) => Tag::ATTESTATION_ID_BRAND,
201 KeyParameterValue::AttestationIdDevice(_) => Tag::ATTESTATION_ID_DEVICE,
202 KeyParameterValue::AttestationIdProduct(_) => Tag::ATTESTATION_ID_PRODUCT,
203 KeyParameterValue::AttestationIdSerial(_) => Tag::ATTESTATION_ID_SERIAL,
204 KeyParameterValue::AttestationIdIMEI(_) => Tag::ATTESTATION_ID_IMEI,
205 KeyParameterValue::AttestationIdMEID(_) => Tag::ATTESTATION_ID_MEID,
206 KeyParameterValue::AttestationIdManufacturer(_) => Tag::ATTESTATION_ID_MANUFACTURER,
207 KeyParameterValue::AttestationIdModel(_) => Tag::ATTESTATION_ID_MODEL,
208 KeyParameterValue::VendorPatchLevel(_) => Tag::VENDOR_PATCHLEVEL,
209 KeyParameterValue::BootPatchLevel(_) => Tag::BOOT_PATCHLEVEL,
210 KeyParameterValue::AssociatedData(_) => Tag::ASSOCIATED_DATA,
211 KeyParameterValue::Nonce(_) => Tag::NONCE,
212 KeyParameterValue::MacLength(_) => Tag::MAC_LENGTH,
213 KeyParameterValue::ResetSinceIdRotation => Tag::RESET_SINCE_ID_ROTATION,
214 KeyParameterValue::ConfirmationToken(_) => Tag::CONFIRMATION_TOKEN,
215 }
216 }
217
218 /// Returns key parameter value.
219 pub fn key_parameter_value(&self) -> &KeyParameterValue {
220 &self.key_parameter_value
221 }
222
223 /// Returns the security level of a KeyParameter.
224 pub fn security_level(&self) -> &SecurityLevel {
225 &self.security_level
226 }
227}
228
229#[cfg(test)]
230mod basic_tests {
231 use crate::key_parameter::*;
232 use crate::keymint_definitions::{SecurityLevel, Tag};
233
234 // Test basic functionality of KeyParameter.
235 #[test]
236 fn test_key_parameter() {
237 let key_parameter = KeyParameter::new(
238 KeyParameterValue::Algorithm(Algorithm::RSA),
239 SecurityLevel::STRONGBOX,
240 );
241
242 assert_eq!(key_parameter.get_tag(), Tag::ALGORITHM);
243
244 assert_eq!(
245 *key_parameter.key_parameter_value(),
246 KeyParameterValue::Algorithm(Algorithm::RSA)
247 );
248
249 assert_eq!(*key_parameter.security_level(), SecurityLevel::STRONGBOX);
250 }
251}