blob: 92f1e4a2a00cfdb8ab064985aad6f30e331bcd16 [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 Zeuthen49f2d252020-10-16 11:27:24 -040034 // Set by eicCreateCredentialKey() OR eicProvisioningInitForUpdate()
David Zeuthen630de2a2020-05-11 14:04:54 -040035 uint8_t credentialPrivateKey[EIC_P256_PRIV_KEY_SIZE];
36
37 int numEntryCounts;
38 uint8_t entryCounts[EIC_MAX_NUM_NAMESPACES];
39
40 int curNamespace;
41 int curNamespaceNumProcessed;
42
43 size_t curEntrySize;
44 size_t curEntryNumBytesReceived;
45
David Zeuthen49f2d252020-10-16 11:27:24 -040046 // Set by eicProvisioningInit() OR eicProvisioningInitForUpdate()
David Zeuthen630de2a2020-05-11 14:04:54 -040047 uint8_t storageKey[EIC_AES_128_KEY_SIZE];
48
49 size_t expectedCborSizeAtEnd;
50
51 // SHA-256 for AdditionalData, updated for each entry.
52 uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE];
53
David Zeuthen49f2d252020-10-16 11:27:24 -040054 // Digester just for ProofOfProvisioning (without Sig_structure).
55 EicSha256Ctx proofOfProvisioningDigester;
56
David Zeuthen630de2a2020-05-11 14:04:54 -040057 EicCbor cbor;
58
59 bool testCredential;
David Zeuthen49f2d252020-10-16 11:27:24 -040060
61 // Set to true if this is an update.
62 bool isUpdate;
David Zeuthen630de2a2020-05-11 14:04:54 -040063} EicProvisioning;
64
65bool eicProvisioningInit(EicProvisioning* ctx, bool testCredential);
66
David Zeuthen49f2d252020-10-16 11:27:24 -040067bool eicProvisioningInitForUpdate(EicProvisioning* ctx, bool testCredential, const char* docType,
Joseph Jangdabb3c52021-09-01 16:50:09 +080068 size_t docTypeLength, const uint8_t* encryptedCredentialKeys,
David Zeuthen49f2d252020-10-16 11:27:24 -040069 size_t encryptedCredentialKeysSize);
70
David Zeuthen630de2a2020-05-11 14:04:54 -040071bool eicProvisioningCreateCredentialKey(EicProvisioning* ctx, const uint8_t* challenge,
72 size_t challengeSize, const uint8_t* applicationId,
73 size_t applicationIdSize, uint8_t* publicKeyCert,
74 size_t* publicKeyCertSize);
75
76bool eicProvisioningStartPersonalization(EicProvisioning* ctx, int accessControlProfileCount,
77 const int* entryCounts, size_t numEntryCounts,
Joseph Jangdabb3c52021-09-01 16:50:09 +080078 const char* docType, size_t docTypeLength,
David Zeuthen630de2a2020-05-11 14:04:54 -040079 size_t expectedProofOfProvisioningingSize);
80
David Zeuthen630de2a2020-05-11 14:04:54 -040081// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
82// avoid allocating stack space.
83//
Joseph Jangdabb3c52021-09-01 16:50:09 +080084bool eicProvisioningAddAccessControlProfile(EicProvisioning* ctx, int id,
85 const uint8_t* readerCertificate,
86 size_t readerCertificateSize,
87 bool userAuthenticationRequired,
88 uint64_t timeoutMillis, uint64_t secureUserId,
89 uint8_t outMac[28], uint8_t* scratchSpace,
90 size_t scratchSpaceSize);
91
92// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
93// avoid allocating stack space.
94//
95bool eicProvisioningBeginAddEntry(EicProvisioning* ctx, const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -040096 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +080097 size_t nameSpaceLength, const char* name, size_t nameLength,
98 uint64_t entrySize, uint8_t* scratchSpace,
David Zeuthen630de2a2020-05-11 14:04:54 -040099 size_t scratchSpaceSize);
100
101// The outEncryptedContent array must be contentSize + 28 bytes long.
102//
103// The scratchSpace should be set to a buffer at least 512 bytes. It's done this way to
104// avoid allocating stack space.
105//
Joseph Jangdabb3c52021-09-01 16:50:09 +0800106bool eicProvisioningAddEntryValue(EicProvisioning* ctx, const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -0400107 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800108 size_t nameSpaceLength, const char* name, size_t nameLength,
109 const uint8_t* content, size_t contentSize,
David Zeuthen630de2a2020-05-11 14:04:54 -0400110 uint8_t* outEncryptedContent, uint8_t* scratchSpace,
111 size_t scratchSpaceSize);
112
113// The data returned in |signatureOfToBeSigned| contains the ECDSA signature of
114// the ToBeSigned CBOR from RFC 8051 "4.4. Signing and Verification Process"
115// where content is set to the ProofOfProvisioninging CBOR.
116//
117bool eicProvisioningFinishAddingEntries(
118 EicProvisioning* ctx, uint8_t signatureOfToBeSigned[EIC_ECDSA_P256_SIGNATURE_SIZE]);
119
120//
121//
122// The |encryptedCredentialKeys| array is set to AES-GCM-ENC(HBK, R, CredentialKeys, docType)
123// where
124//
125// CredentialKeys = [
126// bstr, ; storageKey, a 128-bit AES key
127// bstr ; credentialPrivKey, the private key for credentialKey
David Zeuthen49f2d252020-10-16 11:27:24 -0400128// bstr ; SHA-256(ProofOfProvisioning)
David Zeuthen630de2a2020-05-11 14:04:54 -0400129// ]
130//
David Zeuthen49f2d252020-10-16 11:27:24 -0400131// for feature version 202101. For feature version 202009 the third field was not present.
132//
David Zeuthen630de2a2020-05-11 14:04:54 -0400133// Since |storageKey| is 16 bytes and |credentialPrivKey| is 32 bytes, the
David Zeuthen49f2d252020-10-16 11:27:24 -0400134// encoded CBOR for CredentialKeys is 86 bytes and consequently
135// |encryptedCredentialKeys| will be no longer than 86 + 28 = 114 bytes.
David Zeuthen630de2a2020-05-11 14:04:54 -0400136//
137bool eicProvisioningFinishGetCredentialData(EicProvisioning* ctx, const char* docType,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800138 size_t docTypeLength,
David Zeuthen49f2d252020-10-16 11:27:24 -0400139 uint8_t* encryptedCredentialKeys,
140 size_t* encryptedCredentialKeysSize);
David Zeuthen630de2a2020-05-11 14:04:54 -0400141
142#ifdef __cplusplus
143}
144#endif
145
146#endif // ANDROID_HARDWARE_IDENTITY_EIC_PROVISIONING_H