Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2022 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include <stdint.h>
19 :
20 : #include <credentials/FabricTable.h>
21 : #include <credentials/GroupDataProvider.h>
22 : #include <lib/core/CHIPError.h>
23 : #include <lib/support/BufferWriter.h>
24 : #include <lib/support/Span.h>
25 :
26 : #include "CASEDestinationId.h"
27 :
28 : namespace chip {
29 :
30 : using namespace chip::Crypto;
31 :
32 23 : CHIP_ERROR GenerateCaseDestinationId(const ByteSpan & ipk, const ByteSpan & initiatorRandom, const ByteSpan & rootPubKey,
33 : FabricId fabricId, NodeId nodeId, MutableByteSpan & outDestinationId)
34 : {
35 23 : VerifyOrReturnError(ipk.size() == kIPKSize, CHIP_ERROR_INVALID_ARGUMENT);
36 23 : VerifyOrReturnError(initiatorRandom.size() == kSigmaParamRandomNumberSize, CHIP_ERROR_INVALID_ARGUMENT);
37 23 : VerifyOrReturnError(rootPubKey.size() == kP256_PublicKey_Length, CHIP_ERROR_INVALID_ARGUMENT);
38 23 : VerifyOrReturnError(outDestinationId.size() >= kSHA256_Hash_Length, CHIP_ERROR_INVALID_ARGUMENT);
39 :
40 23 : constexpr size_t kDestinationMessageLen =
41 : kSigmaParamRandomNumberSize + kP256_PublicKey_Length + sizeof(FabricId) + sizeof(NodeId);
42 : uint8_t destinationMessage[kDestinationMessageLen];
43 :
44 23 : Encoding::LittleEndian::BufferWriter bbuf(destinationMessage, sizeof(destinationMessage));
45 23 : bbuf.Put(initiatorRandom.data(), initiatorRandom.size());
46 23 : bbuf.Put(rootPubKey.data(), rootPubKey.size());
47 23 : bbuf.Put64(fabricId);
48 23 : bbuf.Put64(nodeId);
49 :
50 23 : size_t written = 0;
51 23 : VerifyOrReturnError(bbuf.Fit(written), CHIP_ERROR_BUFFER_TOO_SMALL);
52 :
53 23 : HMAC_sha hmac;
54 : CHIP_ERROR err =
55 23 : hmac.HMAC_SHA256(ipk.data(), ipk.size(), bbuf.Buffer(), written, outDestinationId.data(), outDestinationId.size());
56 :
57 23 : if (err == CHIP_NO_ERROR)
58 : {
59 23 : outDestinationId.reduce_size(kSHA256_Hash_Length);
60 : }
61 :
62 23 : return err;
63 23 : }
64 :
65 : } // namespace chip
|