blob: b9304bcacb14b9bd02783cd7f75e6411c2690a1f [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#include "EicCbor.h"
18
19void eicCborInit(EicCbor* cbor, uint8_t* buffer, size_t bufferSize) {
David Zeuthen49f2d252020-10-16 11:27:24 -040020 eicMemSet(cbor, '\0', sizeof(EicCbor));
David Zeuthen630de2a2020-05-11 14:04:54 -040021 cbor->size = 0;
22 cbor->bufferSize = bufferSize;
23 cbor->buffer = buffer;
24 cbor->digestType = EIC_CBOR_DIGEST_TYPE_SHA256;
25 eicOpsSha256Init(&cbor->digester.sha256);
26}
27
28void eicCborInitHmacSha256(EicCbor* cbor, uint8_t* buffer, size_t bufferSize,
29 const uint8_t* hmacKey, size_t hmacKeySize) {
David Zeuthen49f2d252020-10-16 11:27:24 -040030 eicMemSet(cbor, '\0', sizeof(EicCbor));
David Zeuthen630de2a2020-05-11 14:04:54 -040031 cbor->size = 0;
32 cbor->bufferSize = bufferSize;
33 cbor->buffer = buffer;
34 cbor->digestType = EIC_CBOR_DIGEST_TYPE_HMAC_SHA256;
35 eicOpsHmacSha256Init(&cbor->digester.hmacSha256, hmacKey, hmacKeySize);
36}
37
David Zeuthen49f2d252020-10-16 11:27:24 -040038void eicCborEnableSecondaryDigesterSha256(EicCbor* cbor, EicSha256Ctx* sha256) {
39 cbor->secondaryDigesterSha256 = sha256;
40}
41
David Zeuthen630de2a2020-05-11 14:04:54 -040042void eicCborFinal(EicCbor* cbor, uint8_t digest[EIC_SHA256_DIGEST_SIZE]) {
43 switch (cbor->digestType) {
44 case EIC_CBOR_DIGEST_TYPE_SHA256:
45 eicOpsSha256Final(&cbor->digester.sha256, digest);
46 break;
47 case EIC_CBOR_DIGEST_TYPE_HMAC_SHA256:
48 eicOpsHmacSha256Final(&cbor->digester.hmacSha256, digest);
49 break;
50 }
51}
52
53void eicCborAppend(EicCbor* cbor, const uint8_t* data, size_t size) {
54 switch (cbor->digestType) {
55 case EIC_CBOR_DIGEST_TYPE_SHA256:
56 eicOpsSha256Update(&cbor->digester.sha256, data, size);
57 break;
58 case EIC_CBOR_DIGEST_TYPE_HMAC_SHA256:
59 eicOpsHmacSha256Update(&cbor->digester.hmacSha256, data, size);
60 break;
61 }
David Zeuthen49f2d252020-10-16 11:27:24 -040062 if (cbor->secondaryDigesterSha256 != NULL) {
63 eicOpsSha256Update(cbor->secondaryDigesterSha256, data, size);
64 }
David Zeuthen630de2a2020-05-11 14:04:54 -040065
66 if (cbor->size >= cbor->bufferSize) {
67 cbor->size += size;
68 return;
69 }
70
71 size_t numBytesLeft = cbor->bufferSize - cbor->size;
72 size_t numBytesToCopy = size;
73 if (numBytesToCopy > numBytesLeft) {
74 numBytesToCopy = numBytesLeft;
75 }
76 eicMemCpy(cbor->buffer + cbor->size, data, numBytesToCopy);
77
78 cbor->size += size;
79}
80
81size_t eicCborAdditionalLengthBytesFor(size_t size) {
82 if (size < 24) {
83 return 0;
84 } else if (size <= 0xff) {
85 return 1;
86 } else if (size <= 0xffff) {
87 return 2;
88 } else if (size <= 0xffffffff) {
89 return 4;
90 }
91 return 8;
92}
93
Joseph Jangdabb3c52021-09-01 16:50:09 +080094void eicCborBegin(EicCbor* cbor, int majorType, uint64_t size) {
David Zeuthen630de2a2020-05-11 14:04:54 -040095 uint8_t data[9];
96
97 if (size < 24) {
98 data[0] = (majorType << 5) | size;
99 eicCborAppend(cbor, data, 1);
100 } else if (size <= 0xff) {
101 data[0] = (majorType << 5) | 24;
102 data[1] = size;
103 eicCborAppend(cbor, data, 2);
104 } else if (size <= 0xffff) {
105 data[0] = (majorType << 5) | 25;
106 data[1] = size >> 8;
107 data[2] = size & 0xff;
108 eicCborAppend(cbor, data, 3);
109 } else if (size <= 0xffffffff) {
110 data[0] = (majorType << 5) | 26;
111 data[1] = (size >> 24) & 0xff;
112 data[2] = (size >> 16) & 0xff;
113 data[3] = (size >> 8) & 0xff;
114 data[4] = size & 0xff;
115 eicCborAppend(cbor, data, 5);
116 } else {
Andrew Scull29ba0642021-05-12 12:18:08 +0000117 data[0] = (majorType << 5) | 27;
David Zeuthen630de2a2020-05-11 14:04:54 -0400118 data[1] = (((uint64_t)size) >> 56) & 0xff;
119 data[2] = (((uint64_t)size) >> 48) & 0xff;
120 data[3] = (((uint64_t)size) >> 40) & 0xff;
121 data[4] = (((uint64_t)size) >> 32) & 0xff;
122 data[5] = (((uint64_t)size) >> 24) & 0xff;
123 data[6] = (((uint64_t)size) >> 16) & 0xff;
124 data[7] = (((uint64_t)size) >> 8) & 0xff;
125 data[8] = ((uint64_t)size) & 0xff;
126 eicCborAppend(cbor, data, 9);
127 }
128}
129
130void eicCborAppendByteString(EicCbor* cbor, const uint8_t* data, size_t dataSize) {
131 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_BYTE_STRING, dataSize);
132 eicCborAppend(cbor, data, dataSize);
133}
134
Joseph Jangdabb3c52021-09-01 16:50:09 +0800135void eicCborAppendString(EicCbor* cbor, const char* str, size_t strLength) {
136 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_STRING, strLength);
137 eicCborAppend(cbor, (const uint8_t*)str, strLength);
138}
139
140void eicCborAppendStringZ(EicCbor* cbor, const char* str) {
141 eicCborAppendString(cbor, str, eicStrLen(str));
David Zeuthen630de2a2020-05-11 14:04:54 -0400142}
143
144void eicCborAppendSimple(EicCbor* cbor, uint8_t simpleValue) {
145 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_SIMPLE, simpleValue);
146}
147
148void eicCborAppendBool(EicCbor* cbor, bool value) {
149 uint8_t simpleValue = value ? EIC_CBOR_SIMPLE_VALUE_TRUE : EIC_CBOR_SIMPLE_VALUE_FALSE;
150 eicCborAppendSimple(cbor, simpleValue);
151}
152
153void eicCborAppendSemantic(EicCbor* cbor, uint64_t value) {
154 size_t encoded = value;
155 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_SEMANTIC, encoded);
156}
157
158void eicCborAppendUnsigned(EicCbor* cbor, uint64_t value) {
Joseph Jangdabb3c52021-09-01 16:50:09 +0800159 uint64_t encoded = value;
David Zeuthen630de2a2020-05-11 14:04:54 -0400160 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_UNSIGNED, encoded);
161}
162
163void eicCborAppendNumber(EicCbor* cbor, int64_t value) {
164 if (value < 0) {
Joseph Jangdabb3c52021-09-01 16:50:09 +0800165 uint64_t encoded = -1 - value;
David Zeuthen630de2a2020-05-11 14:04:54 -0400166 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_NEGATIVE, encoded);
167 } else {
168 eicCborAppendUnsigned(cbor, value);
169 }
170}
171
172void eicCborAppendArray(EicCbor* cbor, size_t numElements) {
173 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_ARRAY, numElements);
174}
175
176void eicCborAppendMap(EicCbor* cbor, size_t numPairs) {
177 eicCborBegin(cbor, EIC_CBOR_MAJOR_TYPE_MAP, numPairs);
178}
179
180bool eicCborCalcAccessControl(EicCbor* cborBuilder, int id, const uint8_t* readerCertificate,
181 size_t readerCertificateSize, bool userAuthenticationRequired,
182 uint64_t timeoutMillis, uint64_t secureUserId) {
183 size_t numPairs = 1;
184 if (readerCertificateSize > 0) {
185 numPairs += 1;
186 }
187 if (userAuthenticationRequired) {
188 numPairs += 2;
189 if (secureUserId > 0) {
190 numPairs += 1;
191 }
192 }
193 eicCborAppendMap(cborBuilder, numPairs);
Joseph Jangdabb3c52021-09-01 16:50:09 +0800194 eicCborAppendStringZ(cborBuilder, "id");
David Zeuthen630de2a2020-05-11 14:04:54 -0400195 eicCborAppendUnsigned(cborBuilder, id);
196 if (readerCertificateSize > 0) {
Joseph Jangdabb3c52021-09-01 16:50:09 +0800197 eicCborAppendStringZ(cborBuilder, "readerCertificate");
David Zeuthen630de2a2020-05-11 14:04:54 -0400198 eicCborAppendByteString(cborBuilder, readerCertificate, readerCertificateSize);
199 }
200 if (userAuthenticationRequired) {
Joseph Jangdabb3c52021-09-01 16:50:09 +0800201 eicCborAppendStringZ(cborBuilder, "userAuthenticationRequired");
David Zeuthen630de2a2020-05-11 14:04:54 -0400202 eicCborAppendBool(cborBuilder, userAuthenticationRequired);
Joseph Jangdabb3c52021-09-01 16:50:09 +0800203 eicCborAppendStringZ(cborBuilder, "timeoutMillis");
David Zeuthen630de2a2020-05-11 14:04:54 -0400204 eicCborAppendUnsigned(cborBuilder, timeoutMillis);
205 if (secureUserId > 0) {
Joseph Jangdabb3c52021-09-01 16:50:09 +0800206 eicCborAppendStringZ(cborBuilder, "secureUserId");
David Zeuthen630de2a2020-05-11 14:04:54 -0400207 eicCborAppendUnsigned(cborBuilder, secureUserId);
208 }
209 }
210
211 if (cborBuilder->size > cborBuilder->bufferSize) {
212 eicDebug("Buffer for ACP CBOR is too small (%zd) - need %zd bytes", cborBuilder->bufferSize,
213 cborBuilder->size);
214 return false;
215 }
216
217 return true;
218}
219
Joseph Jangdabb3c52021-09-01 16:50:09 +0800220bool eicCborCalcEntryAdditionalData(const uint8_t* accessControlProfileIds,
David Zeuthen630de2a2020-05-11 14:04:54 -0400221 size_t numAccessControlProfileIds, const char* nameSpace,
Joseph Jangdabb3c52021-09-01 16:50:09 +0800222 size_t nameSpaceLength, const char* name,
223 size_t nameLength, uint8_t* cborBuffer,
224 size_t cborBufferSize, size_t* outAdditionalDataCborSize,
David Zeuthen630de2a2020-05-11 14:04:54 -0400225 uint8_t additionalDataSha256[EIC_SHA256_DIGEST_SIZE]) {
226 EicCbor cborBuilder;
227
228 eicCborInit(&cborBuilder, cborBuffer, cborBufferSize);
229 eicCborAppendMap(&cborBuilder, 3);
Joseph Jangdabb3c52021-09-01 16:50:09 +0800230 eicCborAppendStringZ(&cborBuilder, "Namespace");
231 eicCborAppendString(&cborBuilder, nameSpace, nameSpaceLength);
232 eicCborAppendStringZ(&cborBuilder, "Name");
233 eicCborAppendString(&cborBuilder, name, nameLength);
234 eicCborAppendStringZ(&cborBuilder, "AccessControlProfileIds");
David Zeuthen630de2a2020-05-11 14:04:54 -0400235 eicCborAppendArray(&cborBuilder, numAccessControlProfileIds);
236 for (size_t n = 0; n < numAccessControlProfileIds; n++) {
237 eicCborAppendNumber(&cborBuilder, accessControlProfileIds[n]);
238 }
239 if (cborBuilder.size > cborBufferSize) {
240 eicDebug("Not enough space for additionalData - buffer is only %zd bytes, content is %zd",
241 cborBufferSize, cborBuilder.size);
242 return false;
243 }
244 if (outAdditionalDataCborSize != NULL) {
245 *outAdditionalDataCborSize = cborBuilder.size;
246 }
247 eicCborFinal(&cborBuilder, additionalDataSha256);
248 return true;
249}