David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 1 | /* |
| 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 |
| 25 | extern "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 | |
| 33 | typedef struct { |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 34 | // Set by eicCreateCredentialKey() OR eicProvisioningInitForUpdate() |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 35 | 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 Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 46 | // Set by eicProvisioningInit() OR eicProvisioningInitForUpdate() |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 47 | 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 Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 54 | // Digester just for ProofOfProvisioning (without Sig_structure). |
| 55 | EicSha256Ctx proofOfProvisioningDigester; |
| 56 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 57 | EicCbor cbor; |
| 58 | |
| 59 | bool testCredential; |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 60 | |
| 61 | // Set to true if this is an update. |
| 62 | bool isUpdate; |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 63 | } EicProvisioning; |
| 64 | |
| 65 | bool eicProvisioningInit(EicProvisioning* ctx, bool testCredential); |
| 66 | |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 67 | bool eicProvisioningInitForUpdate(EicProvisioning* ctx, bool testCredential, const char* docType, |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 68 | size_t docTypeLength, const uint8_t* encryptedCredentialKeys, |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 69 | size_t encryptedCredentialKeysSize); |
| 70 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 71 | bool 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 | |
| 76 | bool eicProvisioningStartPersonalization(EicProvisioning* ctx, int accessControlProfileCount, |
| 77 | const int* entryCounts, size_t numEntryCounts, |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 78 | const char* docType, size_t docTypeLength, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 79 | size_t expectedProofOfProvisioningingSize); |
| 80 | |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 81 | // 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 Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 84 | bool 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 | // |
| 95 | bool eicProvisioningBeginAddEntry(EicProvisioning* ctx, const uint8_t* accessControlProfileIds, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 96 | size_t numAccessControlProfileIds, const char* nameSpace, |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 97 | size_t nameSpaceLength, const char* name, size_t nameLength, |
| 98 | uint64_t entrySize, uint8_t* scratchSpace, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 99 | 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 Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 106 | bool eicProvisioningAddEntryValue(EicProvisioning* ctx, const uint8_t* accessControlProfileIds, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 107 | size_t numAccessControlProfileIds, const char* nameSpace, |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 108 | size_t nameSpaceLength, const char* name, size_t nameLength, |
| 109 | const uint8_t* content, size_t contentSize, |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 110 | 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 | // |
| 117 | bool 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 Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 128 | // bstr ; SHA-256(ProofOfProvisioning) |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 129 | // ] |
| 130 | // |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 131 | // for feature version 202101. For feature version 202009 the third field was not present. |
| 132 | // |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 133 | // Since |storageKey| is 16 bytes and |credentialPrivKey| is 32 bytes, the |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 134 | // encoded CBOR for CredentialKeys is 86 bytes and consequently |
| 135 | // |encryptedCredentialKeys| will be no longer than 86 + 28 = 114 bytes. |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 136 | // |
| 137 | bool eicProvisioningFinishGetCredentialData(EicProvisioning* ctx, const char* docType, |
Joseph Jang | dabb3c5 | 2021-09-01 16:50:09 +0800 | [diff] [blame^] | 138 | size_t docTypeLength, |
David Zeuthen | 49f2d25 | 2020-10-16 11:27:24 -0400 | [diff] [blame] | 139 | uint8_t* encryptedCredentialKeys, |
| 140 | size_t* encryptedCredentialKeysSize); |
David Zeuthen | 630de2a | 2020-05-11 14:04:54 -0400 | [diff] [blame] | 141 | |
| 142 | #ifdef __cplusplus |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | #endif // ANDROID_HARDWARE_IDENTITY_EIC_PROVISIONING_H |