ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
key-params.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2023 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_CXX_SECURITY_KEY_PARAMS_HPP
23#define NDN_CXX_SECURITY_KEY_PARAMS_HPP
24
27
28namespace ndn {
29
36{
37public:
38 class Error : public std::runtime_error
39 {
40 public:
41 using std::runtime_error::runtime_error;
42 };
43
44 virtual
46
48 getKeyType() const
49 {
50 return m_keyType;
51 }
52
55 {
56 return m_keyIdType;
57 }
58
59 const name::Component&
60 getKeyId() const
61 {
62 return m_keyId;
63 }
64
65 void
67 {
68 m_keyId = keyId;
69 }
70
71protected:
79 KeyParams(KeyType keyType, KeyIdType keyIdType);
80
89 KeyParams(KeyType keyType, const name::Component& keyId);
90
91private:
92 KeyType m_keyType;
93 KeyIdType m_keyIdType;
94 name::Component m_keyId;
95};
96
97
98namespace detail {
99
102{
103public:
104 static constexpr KeyType
106 {
107 return KeyType::RSA;
108 }
109
115 static uint32_t
116 checkKeySize(uint32_t size);
117
118 static uint32_t
120};
121
124{
125public:
126 static constexpr KeyType
128 {
129 return KeyType::EC;
130 }
131
137 static uint32_t
138 checkKeySize(uint32_t size);
139
140 static uint32_t
142};
143
144} // namespace detail
145
146
148template<typename KeyParamsInfo>
150{
151public:
153 explicit
155 uint32_t size = KeyParamsInfo::getDefaultSize())
156 : KeyParams(KeyParamsInfo::getType(), keyId)
157 {
158 setKeySize(size);
159 }
160
167 explicit
168 SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
169 KeyIdType keyIdType = KeyIdType::RANDOM)
170 : KeyParams(KeyParamsInfo::getType(), keyIdType)
171 {
172 setKeySize(size);
173 }
174
175 uint32_t
177 {
178 return m_size;
179 }
180
181private:
182 void
183 setKeySize(uint32_t size)
184 {
185 m_size = KeyParamsInfo::checkKeySize(size);
186 }
187
188 uint32_t
189 getDefaultKeySize() const
190 {
191 return KeyParamsInfo::getDefaultSize();
192 }
193
194private:
195 uint32_t m_size;
196};
197
200
203
204
205namespace detail {
206
208class AesKeyParamsInfo
209{
210public:
211 static constexpr KeyType
212 getType()
213 {
214 return KeyType::AES;
215 }
216
222 static uint32_t
223 checkKeySize(uint32_t size);
224
225 static uint32_t
226 getDefaultSize();
227};
228
230class HmacKeyParamsInfo
231{
232public:
233 static constexpr KeyType
234 getType()
235 {
236 return KeyType::HMAC;
237 }
238
244 static uint32_t
245 checkKeySize(uint32_t size);
246
247 static uint32_t
248 getDefaultSize();
249};
250
251} // namespace detail
252
253
255template<typename KeyParamsInfo>
257{
258public:
260 explicit
262 uint32_t size = KeyParamsInfo::getDefaultSize())
263 : KeyParams(KeyParamsInfo::getType(), keyId)
264 {
265 setKeySize(size);
266 }
267
274 explicit
275 SimpleSymmetricKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
276 KeyIdType keyIdType = KeyIdType::RANDOM)
277 : KeyParams(KeyParamsInfo::getType(), keyIdType)
278 {
279 setKeySize(size);
280 }
281
282 uint32_t
284 {
285 return m_size;
286 }
287
288private:
289 void
290 setKeySize(uint32_t size)
291 {
292 m_size = KeyParamsInfo::checkKeySize(size);
293 }
294
295 uint32_t
296 getDefaultKeySize() const
297 {
298 return KeyParamsInfo::getDefaultSize();
299 }
300
301private:
302 uint32_t m_size;
303};
304
307
310
311} // namespace ndn
312
313#endif // NDN_CXX_SECURITY_KEY_PARAMS_HPP
Base class for key parameters.
virtual ~KeyParams()
const name::Component & getKeyId() const
KeyIdType getKeyIdType() const
KeyType getKeyType() const
void setKeyId(const name::Component &keyId)
SimplePublicKeyParams is a template for public keys with only one parameter: size.
SimplePublicKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameters with user-specified key id.
SimplePublicKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameters with auto-generated key id.
uint32_t getKeySize() const
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
SimpleSymmetricKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameters with user-specified key id.
SimpleSymmetricKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameters with auto-generated key id.
EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
static uint32_t getDefaultSize()
static constexpr KeyType getType()
RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
static uint32_t getDefaultSize()
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
static constexpr KeyType getType()
Represents a name component.
Definition data.cpp:25
KeyType
The type of a cryptographic key.
@ EC
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
@ RSA
RSA key, supports sign/verify and encrypt/decrypt operations.
@ AES
AES key, supports encrypt/decrypt operations.
@ HMAC
HMAC key, supports sign/verify operations.
KeyIdType
The type of KeyId component in a key name.
@ RANDOM
Use a 64-bit random number as key id.