blob: d94f8f18c20e644fd869d914195210f5cb32d766 [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 Moorebe321132022-01-25 22:44:24 +000080 size_t applicationIdSize, uint8_t* publicKeyCert,
David Zeuthen630de2a2020-05-11 14:04:54 -040081 size_t* publicKeyCertSize);
82
83bool eicProvisioningStartPersonalization(EicProvisioning* ctx, int accessControlProfileCount,
84 const int* entryCounts, size_t numEntryCounts,
Joseph Jangdabb3c52021-09-01 16:50:09 +080085 const char* docType, size_t docTypeLength,
David Zeuthen630de2a2020-05-11 14:04:54 -040086 size_t expectedProofOfProvisioningingSize);
87
David Zeuthen630de2a2020-05-11 14:04:54 -040088// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
89// avoid allocating stack space.
90//
Joseph Jangdabb3c52021-09-01 16:50:09 +080091bool eicProvisioningAddAccessControlProfile(EicProvisioning* ctx, int id,
92 const uint8_t* readerCertificate,
93 size_t readerCertificateSize,
94 bool userAuthenticationRequired,
95 uint64_t timeoutMillis, uint64_t secureUserId,
96 uint8_t outMac[28], uint8_t* scratchSpace,
97 size_t scratchSpaceSize);
98
99// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
100// avoid allocating stack space.
101//
102bool eicProvisioningBeginAddEntry(EicProvisioning* ctx, const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -0400103 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800104 size_t nameSpaceLength, const char* name, size_t nameLength,
105 uint64_t entrySize, uint8_t* scratchSpace,
David Zeuthen630de2a2020-05-11 14:04:54 -0400106 size_t scratchSpaceSize);
107
108// The outEncryptedContent array must be contentSize + 28 bytes long.
109//
110// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
111// avoid allocating stack space.
112//
Joseph Jangdabb3c52021-09-01 16:50:09 +0800113bool eicProvisioningAddEntryValue(EicProvisioning* ctx, const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -0400114 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800115 size_t nameSpaceLength, const char* name, size_t nameLength,
116 const uint8_t* content, size_t contentSize,
David Zeuthen630de2a2020-05-11 14:04:54 -0400117 uint8_t* outEncryptedContent, uint8_t* scratchSpace,
118 size_t scratchSpaceSize);
119
120// The data returned in |signatureOfToBeSigned| contains the ECDSA signature of
121// the ToBeSigned CBOR from RFC 8051 "4.4. Signing and Verification Process"
122// where content is set to the ProofOfProvisioninging CBOR.
123//
124bool eicProvisioningFinishAddingEntries(
125 EicProvisioning* ctx, uint8_t signatureOfToBeSigned[EIC_ECDSA_P256_SIGNATURE_SIZE]);
126
127//
128//
129// The |encryptedCredentialKeys| array is set to AES-GCM-ENC(HBK, R, CredentialKeys, docType)
130// where
131//
132// CredentialKeys = [
133// bstr, ; storageKey, a 128-bit AES key
134// bstr ; credentialPrivKey, the private key for credentialKey
David Zeuthen49f2d252020-10-16 11:27:24 -0400135// bstr ; SHA-256(ProofOfProvisioning)
David Zeuthen630de2a2020-05-11 14:04:54 -0400136// ]
137//
David Zeuthen49f2d252020-10-16 11:27:24 -0400138// for feature version 202101. For feature version 202009 the third field was not present.
139//
David Zeuthen630de2a2020-05-11 14:04:54 -0400140// Since |storageKey| is 16 bytes and |credentialPrivKey| is 32 bytes, the
David Zeuthen49f2d252020-10-16 11:27:24 -0400141// encoded CBOR for CredentialKeys is 86 bytes and consequently
142// |encryptedCredentialKeys| will be no longer than 86 + 28 = 114 bytes.
David Zeuthen630de2a2020-05-11 14:04:54 -0400143//
144bool eicProvisioningFinishGetCredentialData(EicProvisioning* ctx, const char* docType,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800145 size_t docTypeLength,
David Zeuthen49f2d252020-10-16 11:27:24 -0400146 uint8_t* encryptedCredentialKeys,
147 size_t* encryptedCredentialKeysSize);
David Zeuthen630de2a2020-05-11 14:04:54 -0400148
149#ifdef __cplusplus
150}
151#endif
152
153#endif // ANDROID_HARDWARE_IDENTITY_EIC_PROVISIONING_H