RMW desert 5.0.0
Loading...
Searching...
No Matches
SecurityLayer.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (C) 2026 Davide Costa *
3 * *
4 * This file is part of RMW desert. *
5 * *
6 * RMW desert is free software: you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or any *
9 * later version. *
10 * *
11 * RMW desert is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with RMW desert. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************/
19
32
33#ifndef SECURITY_LAYER_H
34#define SECURITY_LAYER_H
35
37
38#include <memory>
39#include <string>
40#include <vector>
41
43
44#include <cose_defines.h>
45#include <cose/key.h>
46
47#include "AeadParams.h"
48
49#define DEFAULT_MASTER_SALT_LEN 16
50#define COSE_SERIALIZATION_MAX_OVERHEAD 12
51#define COSE_INTERNAL_BUF_SIZE 512
52
53namespace security
54{
55
56enum SecurityResult
57{
58 OK = 0,
59 PARAM_ERROR = -1,
60 KEY_SIZE_ERROR = -2,
61 KEY_ENV_ERROR = -3,
62 NONCE_ENV_ERROR = -4,
63 NONCE_SIZE_ERROR = -5,
64 BUFFER_ERROR = -6,
65 CONTEXT_DERIVE_ERROR = -7,
66 COMP_ERROR = -8,
67 WRAP_ERROR = -100,
68 UNWRAP_ERROR = -200,
69 INTERNAL_ERROR = -999
70};
71
73{
74 public:
82
96 SecurityLayer(const AeadParams &aead_params, KdfAlgorithms kdf, const std::vector<uint8_t> &master_key,
97 const std::vector<uint8_t> &master_salt, size_t piv_size, size_t sender_seq_number,
98 const std::vector<uint8_t> &sender_id, const std::vector<uint8_t> &receiver_id);
99
119 SecurityResult wrap(uint8_t* data, size_t data_len, size_t data_max_len, uint8_t** cose_ptr, size_t* cose_len);
120
136 SecurityResult unwrap(uint8_t* data, size_t data_len, size_t data_max_len, size_t* new_data_len);
137
138 private:
139 SecurityResult get_master_key_env(std::vector<uint8_t>& master_key) const;
140 SecurityResult generate_nonce();
141 SecurityResult build_kdf_info(const std::vector<uint8_t>& id, cose_algo_t alg, const std::string& type, size_t len, uint8_t** out, size_t* out_len);
142 SecurityResult derive_key(const std::vector<uint8_t>& ikm, const std::vector<uint8_t>& salt, const std::vector<uint8_t>& id, cose_algo_t algo, std::vector<uint8_t>& key_out, cose_key_t* cose_k_out);
143 SecurityResult derive_iv(const std::vector<uint8_t>& ikm, const std::vector<uint8_t>& salt, cose_algo_t alg, std::vector<uint8_t>& iv_out);
144 SecurityResult derive_context(const std::vector<uint8_t>& master_key, const std::vector<uint8_t>& master_salt);
145 SecurityResult cose_stateless_compress(uint8_t* cose, size_t cose_len, uint8_t** out, size_t* out_len) const;
146 SecurityResult cose_stateless_decompress(uint8_t* data, size_t data_len, size_t data_max_len, size_t* out_len) const;
147
148 /* Algorithms to use */
149 AeadParams aead_params_;
150 KdfAlgorithms kdf_;
151 /* ***************** */
152
153 /* Nonce-related fields */
154 std::vector<uint8_t> context_iv_{};
155 size_t piv_size_;
156 std::vector<uint8_t> piv_bytes_{};
157 size_t sender_seq_number_;
158 /* ******************** */
159
160 /* Key-related fields */
161 std::vector<uint8_t> sender_id_{};
162 std::vector<uint8_t> sender_key_bytes_{};
163 cose_key_t sender_cose_key_{};
164 std::vector<uint8_t> receiver_id_{};
165 std::vector<uint8_t> receiver_key_bytes_{};
166 cose_key_t receiver_cose_key_{};
167 /* ****************** */
168
169 uint8_t internal_buf_[COSE_INTERNAL_BUF_SIZE]{};
170};
171
172} // namespace security
173
174#endif
Class containing the parameters of the selected cryptography.
Definition AeadParams.h:68
SecurityLayer()
Creates a security layer using compile-time configuration values.
Definition SecurityLayer.cpp:101
SecurityResult wrap(uint8_t *data, size_t data_len, size_t data_max_len, uint8_t **cose_ptr, size_t *cose_len)
Encrypts and serializes an application payload as a COSE message.
Definition SecurityLayer.cpp:411
SecurityResult unwrap(uint8_t *data, size_t data_len, size_t data_max_len, size_t *new_data_len)
Decodes, authenticates, and decrypts a COSE message.
Definition SecurityLayer.cpp:456