Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 1 | /* |
| 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 Casallas | 8881290 | 2022-02-02 20:21:35 -0500 | [diff] [blame] | 30 | #include <transport/CryptoContext.h> |
Zang MingJie | eca9bff | 2021-09-23 03:19:51 +0800 | [diff] [blame] | 31 | #include <transport/SecureSession.h> |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 32 | |
| 33 | namespace chip { |
| 34 | |
| 35 | namespace SecureMessageCodec { |
| 36 | |
| 37 | /** |
| 38 | * @brief |
| 39 | * Attach payload header to the message and encrypt the message buffer using |
Tennessee Carmel-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 40 | * key from the secure session. |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 41 | * |
Tennessee Carmel-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 42 | * @param session The secure session context with the peer node |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 43 | * @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-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 48 | * 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 Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 51 | */ |
Zang MingJie | 20f8b95 | 2022-03-25 02:06:03 +0800 | [diff] [blame] | 52 | CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader, |
| 53 | PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf); |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * @brief |
Tennessee Carmel-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 57 | * Decrypt the message, perform message integrity check, and decode the payload header, |
| 58 | * consuming the header from the packet in doing so. |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 59 | * |
Tennessee Carmel-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 60 | * @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 Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 62 | * @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-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 65 | * 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 Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 68 | */ |
Zang MingJie | 20f8b95 | 2022-03-25 02:06:03 +0800 | [diff] [blame] | 69 | CHIP_ERROR Decrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader, |
| 70 | const PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf); |
Tennessee Carmel-Veilleux | 8aca71c | 2022-01-14 15:59:49 -0500 | [diff] [blame] | 71 | |
Pankaj Garg | fad6d05 | 2021-01-20 12:26:34 -0800 | [diff] [blame] | 72 | } // namespace SecureMessageCodec |
| 73 | |
| 74 | } // namespace chip |