blob: 2619bfc45ed32936709e9b2eac2648af6245cda3 [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_PROVISIONING_H
22#define ANDROID_HARDWARE_IDENTITY_EIC_PROVISIONING_H
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#include "EicCbor.h"
29
30#define EIC_MAX_NUM_NAMESPACES 32
31#define EIC_MAX_NUM_ACCESS_CONTROL_PROFILE_IDS 32
32
33typedef struct {
David Zeuthen1eb12b22021-09-11 13:59:43 -040034 // A non-zero number unique for this EicProvisioning instance
35 uint32_t id;
36
David Zeuthen49f2d252020-10-16 11:27:24 -040037 // Set by eicCreateCredentialKey() OR eicProvisioningInitForUpdate()
David Zeuthen630de2a2020-05-11 14:04:54 -040038 uint8_t credentialPrivateKey[EIC_P256_PRIV_KEY_SIZE];
39
40 int numEntryCounts;
41 uint8_t entryCounts[EIC_MAX_NUM_NAMESPACES];
42
43 int curNamespace;
44 int curNamespaceNumProcessed;
45
46 size_t curEntrySize;
47 size_t curEntryNumBytesReceived;
48
David Zeuthen49f2d252020-10-16 11:27:24 -040049 // Set by eicProvisioningInit() OR eicProvisioningInitForUpdate()
David Zeuthen630de2a2020-05-11 14:04:54 -040050 uint8_t storageKey[EIC_AES_128_KEY_SIZE];
51
52 size_t expectedCborSizeAtEnd;
53
54 // SHA-256 for AdditionalData, updated for each entry.
55 uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE];
56
David Zeuthen49f2d252020-10-16 11:27:24 -040057 // Digester just for ProofOfProvisioning (without Sig_structure).
58 EicSha256Ctx proofOfProvisioningDigester;
59
David Zeuthen630de2a2020-05-11 14:04:54 -040060 EicCbor cbor;
61
62 bool testCredential;
David Zeuthen49f2d252020-10-16 11:27:24 -040063
64 // Set to true if this is an update.
65 bool isUpdate;
David Zeuthen630de2a2020-05-11 14:04:54 -040066} EicProvisioning;
67
68bool eicProvisioningInit(EicProvisioning* ctx, bool testCredential);
69
David Zeuthen49f2d252020-10-16 11:27:24 -040070bool eicProvisioningInitForUpdate(EicProvisioning* ctx, bool testCredential, const char* docType,
Joseph Jangdabb3c52021-09-01 16:50:09 +080071 size_t docTypeLength, const uint8_t* encryptedCredentialKeys,
David Zeuthen49f2d252020-10-16 11:27:24 -040072 size_t encryptedCredentialKeysSize);
73
David Zeuthen1eb12b22021-09-11 13:59:43 -040074bool eicProvisioningShutdown(EicProvisioning* ctx);
75
76bool eicProvisioningGetId(EicProvisioning* ctx, uint32_t* outId);
77
David Zeuthen630de2a2020-05-11 14:04:54 -040078bool eicProvisioningCreateCredentialKey(EicProvisioning* ctx, const uint8_t* challenge,
79 size_t challengeSize, const uint8_t* applicationId,
Seth Moore1bf823c2022-01-25 23:04:37 +000080 size_t applicationIdSize, const uint8_t* attestationKeyBlob,
81 size_t attestationKeyBlobSize,
82 const uint8_t* attestationKeyCert,
83 size_t attestationKeyCertSize, uint8_t* publicKeyCert,
David Zeuthen630de2a2020-05-11 14:04:54 -040084 size_t* publicKeyCertSize);
85
86bool eicProvisioningStartPersonalization(EicProvisioning* ctx, int accessControlProfileCount,
87 const int* entryCounts, size_t numEntryCounts,
Joseph Jangdabb3c52021-09-01 16:50:09 +080088 const char* docType, size_t docTypeLength,
David Zeuthen630de2a2020-05-11 14:04:54 -040089 size_t expectedProofOfProvisioningingSize);
90
David Zeuthen630de2a2020-05-11 14:04:54 -040091// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
92// avoid allocating stack space.
93//
Joseph Jangdabb3c52021-09-01 16:50:09 +080094bool eicProvisioningAddAccessControlProfile(EicProvisioning* ctx, int id,
95 const uint8_t* readerCertificate,
96 size_t readerCertificateSize,
97 bool userAuthenticationRequired,
98 uint64_t timeoutMillis, uint64_t secureUserId,
99 uint8_t outMac[28], uint8_t* scratchSpace,
100 size_t scratchSpaceSize);
101
102// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
103// avoid allocating stack space.
104//
105bool eicProvisioningBeginAddEntry(EicProvisioning* ctx, const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -0400106 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800107 size_t nameSpaceLength, const char* name, size_t nameLength,
108 uint64_t entrySize, uint8_t* scratchSpace,
David Zeuthen630de2a2020-05-11 14:04:54 -0400109 size_t scratchSpaceSize);
110
111// The outEncryptedContent array must be contentSize + 28 bytes long.
112//
113// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
114// avoid allocating stack space.
115//
Joseph Jangdabb3c52021-09-01 16:50:09 +0800116bool eicProvisioningAddEntryValue(EicProvisioning* ctx, const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -0400117 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800118 size_t nameSpaceLength, const char* name, size_t nameLength,
119 const uint8_t* content, size_t contentSize,
David Zeuthen630de2a2020-05-11 14:04:54 -0400120 uint8_t* outEncryptedContent, uint8_t* scratchSpace,
121 size_t scratchSpaceSize);
122
123// The data returned in |signatureOfToBeSigned| contains the ECDSA signature of
124// the ToBeSigned CBOR from RFC 8051 "4.4. Signing and Verification Process"
125// where content is set to the ProofOfProvisioninging CBOR.
126//
127bool eicProvisioningFinishAddingEntries(
128 EicProvisioning* ctx, uint8_t signatureOfToBeSigned[EIC_ECDSA_P256_SIGNATURE_SIZE]);
129
130//
131//
132// The |encryptedCredentialKeys| array is set to AES-GCM-ENC(HBK, R, CredentialKeys, docType)
133// where
134//
135// CredentialKeys = [
136// bstr, ; storageKey, a 128-bit AES key
137// bstr ; credentialPrivKey, the private key for credentialKey
David Zeuthen49f2d252020-10-16 11:27:24 -0400138// bstr ; SHA-256(ProofOfProvisioning)
David Zeuthen630de2a2020-05-11 14:04:54 -0400139// ]
140//
David Zeuthen49f2d252020-10-16 11:27:24 -0400141// for feature version 202101. For feature version 202009 the third field was not present.
142//
David Zeuthen630de2a2020-05-11 14:04:54 -0400143// Since |storageKey| is 16 bytes and |credentialPrivKey| is 32 bytes, the
David Zeuthen49f2d252020-10-16 11:27:24 -0400144// encoded CBOR for CredentialKeys is 86 bytes and consequently
145// |encryptedCredentialKeys| will be no longer than 86 + 28 = 114 bytes.
David Zeuthen630de2a2020-05-11 14:04:54 -0400146//
147bool eicProvisioningFinishGetCredentialData(EicProvisioning* ctx, const char* docType,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800148 size_t docTypeLength,
David Zeuthen49f2d252020-10-16 11:27:24 -0400149 uint8_t* encryptedCredentialKeys,
150 size_t* encryptedCredentialKeysSize);
David Zeuthen630de2a2020-05-11 14:04:54 -0400151
152#ifdef __cplusplus
153}
154#endif
155
156#endif // ANDROID_HARDWARE_IDENTITY_EIC_PROVISIONING_H