blob: d4fcf0e1bb5ffff62f254c0f613db88052493247 [file] [log] [blame]
David Zeuthen630de2a2020-05-11 14:04:54 -04001/*
2 * Copyright 2020, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#if !defined(EIC_INSIDE_LIBEIC_H) && !defined(EIC_COMPILATION)
18#error "Never include this file directly, include libeic.h instead."
19#endif
20
21#ifndef ANDROID_HARDWARE_IDENTITY_EIC_OPS_H
22#define ANDROID_HARDWARE_IDENTITY_EIC_OPS_H
23
24#include <stdarg.h>
25#include <stdbool.h>
26#include <stddef.h>
27#include <stdlib.h>
28
29// Uncomment or define if debug messages are needed.
30//
31//#define EIC_DEBUG
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37// The following defines must be set to something appropriate
38//
39// EIC_SHA256_CONTEXT_SIZE - the size of EicSha256Ctx
40// EIC_HMAC_SHA256_CONTEXT_SIZE - the size of EicHmacSha256Ctx
41//
42// For example, if EicSha256Ctx is implemented using BoringSSL this would be defined
43// as sizeof(SHA256_CTX).
44//
45// We expect the implementation to provide a header file with the name
46// EicOpsImpl.h to do all this.
47//
48#include "EicOpsImpl.h"
49
50#define EIC_SHA256_DIGEST_SIZE 32
51
52// The size of a P-256 private key.
53//
54#define EIC_P256_PRIV_KEY_SIZE 32
55
56// The size of a P-256 public key in uncompressed form.
57//
58// The public key is stored in uncompressed form, first the X coordinate, then
59// the Y coordinate.
60//
61#define EIC_P256_PUB_KEY_SIZE 64
62
63// Size of one of the coordinates in a curve-point.
64//
65#define EIC_P256_COORDINATE_SIZE 32
66
67// The size of an ECSDA signature using P-256.
68//
69// The R and S values are stored here, first R then S.
70//
71#define EIC_ECDSA_P256_SIGNATURE_SIZE 64
72
73#define EIC_AES_128_KEY_SIZE 16
74
75// The following are definitions of implementation functions the
76// underlying platform must provide.
77//
78
79struct EicSha256Ctx {
80 uint8_t reserved[EIC_SHA256_CONTEXT_SIZE];
81};
82typedef struct EicSha256Ctx EicSha256Ctx;
83
84struct EicHmacSha256Ctx {
85 uint8_t reserved[EIC_HMAC_SHA256_CONTEXT_SIZE];
86};
87typedef struct EicHmacSha256Ctx EicHmacSha256Ctx;
88
89#ifdef EIC_DEBUG
90// Debug macro. Don't include a new-line in message.
91//
92#define eicDebug(...) \
93 do { \
94 eicPrint("%s:%d: ", __FILE__, __LINE__); \
95 eicPrint(__VA_ARGS__); \
96 eicPrint("\n"); \
97 } while (0)
98#else
99#define eicDebug(...) \
100 do { \
101 } while (0)
102#endif
103
104// Prints message which should include new-line character. Can be no-op.
105//
106// Don't use this from code, use eicDebug() instead.
107//
108#ifdef EIC_DEBUG
109void eicPrint(const char* format, ...);
110#else
111inline void eicPrint(const char*, ...) {}
112#endif
113
114// Dumps data as pretty-printed hex. Can be no-op.
115//
116#ifdef EIC_DEBUG
117void eicHexdump(const char* message, const uint8_t* data, size_t dataSize);
118#else
119inline void eicHexdump(const char*, const uint8_t*, size_t) {}
120#endif
121
122// Pretty-prints encoded CBOR. Can be no-op.
123//
124// If a byte-string is larger than |maxBStrSize| its contents will not be
125// printed, instead the value of the form "<bstr size=1099016
126// sha1=ef549cca331f73dfae2090e6a37c04c23f84b07b>" will be printed. Pass zero
127// for |maxBStrSize| to disable this.
128//
129#ifdef EIC_DEBUG
130void eicCborPrettyPrint(const uint8_t* cborData, size_t cborDataSize, size_t maxBStrSize);
131#else
132inline void eicCborPrettyPrint(const uint8_t*, size_t, size_t) {}
133#endif
134
135// Memory setting, see memset(3).
136void* eicMemSet(void* s, int c, size_t n);
137
138// Memory copying, see memcpy(3).
139void* eicMemCpy(void* dest, const void* src, size_t n);
140
141// String length, see strlen(3).
142size_t eicStrLen(const char* s);
143
144// Memory compare, see CRYPTO_memcmp(3SSL)
145//
146// It takes an amount of time dependent on len, but independent of the contents of the
147// memory regions pointed to by s1 and s2.
148//
149int eicCryptoMemCmp(const void* s1, const void* s2, size_t n);
150
151// Random number generation.
152bool eicOpsRandom(uint8_t* buf, size_t numBytes);
153
154// If |testCredential| is true, returns the 128-bit AES Hardware-Bound Key (16 bytes).
155//
156// Otherwise returns all zeroes (16 bytes).
157//
158const uint8_t* eicOpsGetHardwareBoundKey(bool testCredential);
159
160// Encrypts |data| with |key| and |additionalAuthenticatedData| using |nonce|,
161// returns the resulting (nonce || ciphertext || tag) in |encryptedData| which
162// must be of size |dataSize| + 28.
163bool eicOpsEncryptAes128Gcm(
164 const uint8_t* key, // Must be 16 bytes
165 const uint8_t* nonce, // Must be 12 bytes
166 const uint8_t* data, // May be NULL if size is 0
167 size_t dataSize,
168 const uint8_t* additionalAuthenticationData, // May be NULL if size is 0
169 size_t additionalAuthenticationDataSize, uint8_t* encryptedData);
170
171// Decrypts |encryptedData| using |key| and |additionalAuthenticatedData|,
172// returns resulting plaintext in |data| must be of size |encryptedDataSize| - 28.
173//
174// The format of |encryptedData| must be as specified in the
175// encryptAes128Gcm() function.
176bool eicOpsDecryptAes128Gcm(const uint8_t* key, // Must be 16 bytes
177 const uint8_t* encryptedData, size_t encryptedDataSize,
178 const uint8_t* additionalAuthenticationData,
179 size_t additionalAuthenticationDataSize, uint8_t* data);
180
181// Creates an EC key using the P-256 curve. The private key is written to
182// |privateKey|. The public key is written to |publicKey|.
183//
184bool eicOpsCreateEcKey(uint8_t privateKey[EIC_P256_PRIV_KEY_SIZE],
185 uint8_t publicKey[EIC_P256_PUB_KEY_SIZE]);
186
187// Generates CredentialKey plus an attestation certificate.
188//
189// The attestation certificate will be signed by the attestation keys the secure
190// area has been provisioned with. The given |challenge| and |applicationId|
191// will be used as will |testCredential|.
192//
193// The generated certificate will be in X.509 format and returned in |cert|
194// and |certSize| must be set to the size of this array and this function will
195// set it to the size of the certification chain on successfully return.
196//
197// This may return either a single certificate or an entire certificate
198// chain. If it returns only a single certificate, the implementation of
199// SecureHardwareProvisioningProxy::createCredentialKey() should amend the
200// remainder of the certificate chain on the HAL side.
201//
202bool eicOpsCreateCredentialKey(uint8_t privateKey[EIC_P256_PRIV_KEY_SIZE], const uint8_t* challenge,
203 size_t challengeSize, const uint8_t* applicationId,
204 size_t applicationIdSize, bool testCredential, uint8_t* cert,
205 size_t* certSize); // inout
206
207// Generate an X.509 certificate for the key identified by |publicKey| which
208// must be of the form returned by eicOpsCreateEcKey().
209//
David Zeuthen49f2d252020-10-16 11:27:24 -0400210// If proofOfBinding is not NULL, it will be included as an OCTET_STRING
211// X.509 extension at OID 1.3.6.1.4.1.11129.2.1.26.
212//
David Zeuthen630de2a2020-05-11 14:04:54 -0400213// The certificate will be signed by the key identified by |signingKey| which
214// must be of the form returned by eicOpsCreateEcKey().
215//
216bool eicOpsSignEcKey(const uint8_t publicKey[EIC_P256_PUB_KEY_SIZE],
217 const uint8_t signingKey[EIC_P256_PRIV_KEY_SIZE], unsigned int serial,
218 const char* issuerName, const char* subjectName, time_t validityNotBefore,
David Zeuthen49f2d252020-10-16 11:27:24 -0400219 time_t validityNotAfter, const uint8_t* proofOfBinding,
220 size_t proofOfBindingSize, uint8_t* cert, size_t* certSize); // inout
David Zeuthen630de2a2020-05-11 14:04:54 -0400221
222// Uses |privateKey| to create an ECDSA signature of some data (the SHA-256 must
223// be given by |digestOfData|). Returns the signature in |signature|.
224//
225bool eicOpsEcDsa(const uint8_t privateKey[EIC_P256_PRIV_KEY_SIZE],
226 const uint8_t digestOfData[EIC_SHA256_DIGEST_SIZE],
227 uint8_t signature[EIC_ECDSA_P256_SIGNATURE_SIZE]);
228
229// Performs Elliptic Curve Diffie-Helman.
230//
231bool eicOpsEcdh(const uint8_t publicKey[EIC_P256_PUB_KEY_SIZE],
232 const uint8_t privateKey[EIC_P256_PRIV_KEY_SIZE],
233 uint8_t sharedSecret[EIC_P256_COORDINATE_SIZE]);
234
235// Performs HKDF.
236//
237bool eicOpsHkdf(const uint8_t* sharedSecret, size_t sharedSecretSize, const uint8_t* salt,
238 size_t saltSize, const uint8_t* info, size_t infoSize, uint8_t* output,
239 size_t outputSize);
240
241// SHA-256 functions.
242void eicOpsSha256Init(EicSha256Ctx* ctx);
243void eicOpsSha256Update(EicSha256Ctx* ctx, const uint8_t* data, size_t len);
244void eicOpsSha256Final(EicSha256Ctx* ctx, uint8_t digest[EIC_SHA256_DIGEST_SIZE]);
245
246// HMAC SHA-256 functions.
247void eicOpsHmacSha256Init(EicHmacSha256Ctx* ctx, const uint8_t* key, size_t keySize);
248void eicOpsHmacSha256Update(EicHmacSha256Ctx* ctx, const uint8_t* data, size_t len);
249void eicOpsHmacSha256Final(EicHmacSha256Ctx* ctx, uint8_t digest[EIC_SHA256_DIGEST_SIZE]);
250
251// Extracts the public key in the given X.509 certificate.
252//
253// If the key is not an EC key, this function fails.
254//
255// Otherwise the public key is stored in uncompressed form in |publicKey| which
256// size should be set in |publicKeySize|. On successful return |publicKeySize|
257// is set to the length of the key. If there is not enough space, the function
258// fails.
259//
260// (The public key returned is not necessarily a P-256 key, even if it is note
261// that its size is not EIC_P256_PUBLIC_KEY_SIZE because of the leading 0x04.)
262//
263bool eicOpsX509GetPublicKey(const uint8_t* x509Cert, size_t x509CertSize, uint8_t* publicKey,
264 size_t* publicKeySize);
265
266// Checks that the X.509 certificate given by |x509Cert| is signed by the public
267// key given by |publicKey| which must be an EC key in uncompressed form (e.g.
268// same formatt as returned by eicOpsX509GetPublicKey()).
269//
270bool eicOpsX509CertSignedByPublicKey(const uint8_t* x509Cert, size_t x509CertSize,
271 const uint8_t* publicKey, size_t publicKeySize);
272
273// Checks that |signature| is a signature of some data (given by |digest|),
274// signed by the public key given by |publicKey|.
275//
276// The key must be an EC key in uncompressed form (e.g. same format as returned
277// by eicOpsX509GetPublicKey()).
278//
279// The format of the signature is the same encoding as the 'signature' field of
280// COSE_Sign1 - that is, it's the R and S integers both with the same length as
281// the key-size.
282//
283// The size of digest must match the size of the key.
284//
285bool eicOpsEcDsaVerifyWithPublicKey(const uint8_t* digest, size_t digestSize,
286 const uint8_t* signature, size_t signatureSize,
287 const uint8_t* publicKey, size_t publicKeySize);
288
289// Validates that the passed in data constitutes a valid auth- and verification tokens.
290//
291bool eicOpsValidateAuthToken(uint64_t challenge, uint64_t secureUserId, uint64_t authenticatorId,
292 int hardwareAuthenticatorType, uint64_t timeStamp, const uint8_t* mac,
293 size_t macSize, uint64_t verificationTokenChallenge,
294 uint64_t verificationTokenTimeStamp,
295 int verificationTokenSecurityLevel,
296 const uint8_t* verificationTokenMac, size_t verificationTokenMacSize);
297
298#ifdef __cplusplus
299}
300#endif
301
302#endif // ANDROID_HARDWARE_IDENTITY_EIC_OPS_H