forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecureMessageCodec.cpp
99 lines (77 loc) · 3.37 KB
/
SecureMessageCodec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* This file defines functions for encoding and decoding CHIP messages.
* The encoded messages contain CHIP packet header, encrypted payload
* header, encrypted payload and message authentication code, as per
* CHIP specifications.
*
*/
#include <lib/support/CodeUtils.h>
#include <lib/support/SafeInt.h>
#include <transport/SecureMessageCodec.h>
namespace chip {
using System::PacketBuffer;
using System::PacketBufferHandle;
namespace SecureMessageCodec {
CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf)
{
VerifyOrReturnError(!msgBuf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(!msgBuf->HasChainedBuffer(), CHIP_ERROR_INVALID_MESSAGE_LENGTH);
ReturnErrorOnFailure(payloadHeader.EncodeBeforeData(msgBuf));
uint8_t * data = msgBuf->Start();
size_t totalLen = msgBuf->TotalLength();
MessageAuthenticationCode mac;
ReturnErrorOnFailure(context.Encrypt(data, totalLen, data, nonce, packetHeader, mac));
uint16_t taglen = 0;
ReturnErrorOnFailure(mac.Encode(packetHeader, &data[totalLen], msgBuf->AvailableDataLength(), &taglen));
msgBuf->SetDataLength(totalLen + taglen);
return CHIP_NO_ERROR;
}
CHIP_ERROR Decrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
const PacketHeader & packetHeader, System::PacketBufferHandle & msg)
{
ReturnErrorCodeIf(msg.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
uint8_t * data = msg->Start();
size_t len = msg->DataLength();
PacketBufferHandle origMsg;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
/* This is a workaround for the case where PacketBuffer payload is not
allocated as an inline buffer to PacketBuffer structure */
origMsg = std::move(msg);
msg = PacketBufferHandle::New(len);
VerifyOrReturnError(!msg.IsNull(), CHIP_ERROR_NO_MEMORY);
msg->SetDataLength(len);
#endif
uint16_t footerLen = packetHeader.MICTagLength();
VerifyOrReturnError(footerLen <= len, CHIP_ERROR_INVALID_MESSAGE_LENGTH);
uint16_t taglen = 0;
MessageAuthenticationCode mac;
ReturnErrorOnFailure(mac.Decode(packetHeader, &data[len - footerLen], footerLen, &taglen));
VerifyOrReturnError(taglen == footerLen, CHIP_ERROR_INTERNAL);
len = len - taglen;
msg->SetDataLength(len);
uint8_t * plainText = msg->Start();
ReturnErrorOnFailure(context.Decrypt(data, len, plainText, nonce, packetHeader, mac));
ReturnErrorOnFailure(payloadHeader.DecodeAndConsume(msg));
return CHIP_NO_ERROR;
}
} // namespace SecureMessageCodec
} // namespace chip