blob: f074e792b59d217a6231b94fc80b2e241b11e5fe [file] [log] [blame]
Pankaj Gargfad6d052021-01-20 12:26:34 -08001/*
2 *
3 * Copyright (c) 2021 Project CHIP Authors
4 * All rights reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/**
20 * @file
21 * This file defines functions for encoding and decoding CHIP messages.
22 * The encoded messages contain CHIP packet header, encrypted payload
23 * header, encrypted payload and message authentication code, as per
24 * CHIP specifications.
25 *
26 */
27
28#pragma once
29
Ricardo Casallas88812902022-02-02 20:21:35 -050030#include <transport/CryptoContext.h>
Zang MingJieeca9bff2021-09-23 03:19:51 +080031#include <transport/SecureSession.h>
Pankaj Gargfad6d052021-01-20 12:26:34 -080032
33namespace chip {
34
35namespace SecureMessageCodec {
36
37/**
38 * @brief
39 * Attach payload header to the message and encrypt the message buffer using
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050040 * key from the secure session.
Pankaj Gargfad6d052021-01-20 12:26:34 -080041 *
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050042 * @param session The secure session context with the peer node
Pankaj Gargfad6d052021-01-20 12:26:34 -080043 * @param payloadHeader Reference to the payload header that should be inserted in
44 * the message
45 * @param packetHeader Reference to the packet header that contains unencrypted
46 * portion of the message header
47 * @param msgBuf The message buffer that contains the unencrypted message. If
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050048 * the operation is successful, this buffer will be mutated to contain
49 * the encrypted message.
50 * @return A CHIP_ERROR value consistent with the result of the encryption operation
Pankaj Gargfad6d052021-01-20 12:26:34 -080051 */
Zang MingJie20f8b952022-03-25 02:06:03 +080052CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
53 PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf);
Pankaj Gargfad6d052021-01-20 12:26:34 -080054
55/**
56 * @brief
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050057 * Decrypt the message, perform message integrity check, and decode the payload header,
58 * consuming the header from the packet in doing so.
Pankaj Gargfad6d052021-01-20 12:26:34 -080059 *
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050060 * @param session The secure session context with the peer node
61 * @param payloadHeader Reference to the payload header that will be recovered from the message
Pankaj Gargfad6d052021-01-20 12:26:34 -080062 * @param packetHeader Reference to the packet header that contains unencrypted
63 * portion of the message header
64 * @param msgBuf The message buffer that contains the encrypted message. If
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050065 * the operation is successful, this buffer will be mutated to contain
66 * the decrypted message.
67 * @return A CHIP_ERROR value consistent with the result of the decryption operation
Pankaj Gargfad6d052021-01-20 12:26:34 -080068 */
Zang MingJie20f8b952022-03-25 02:06:03 +080069CHIP_ERROR Decrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
70 const PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf);
Tennessee Carmel-Veilleux8aca71c2022-01-14 15:59:49 -050071
Pankaj Gargfad6d052021-01-20 12:26:34 -080072} // namespace SecureMessageCodec
73
74} // namespace chip