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 
28 namespace ndn {
29 
35 class KeyParams
36 {
37 public:
38  class Error : public std::runtime_error
39  {
40  public:
41  using std::runtime_error::runtime_error;
42  };
43 
44  virtual
46 
47  KeyType
48  getKeyType() const
49  {
50  return m_keyType;
51  }
52 
53  KeyIdType
54  getKeyIdType() const
55  {
56  return m_keyIdType;
57  }
58 
59  const name::Component&
60  getKeyId() const
61  {
62  return m_keyId;
63  }
64 
65  void
66  setKeyId(const name::Component& keyId)
67  {
68  m_keyId = keyId;
69  }
70 
71 protected:
79  KeyParams(KeyType keyType, KeyIdType keyIdType);
80 
89  KeyParams(KeyType keyType, const name::Component& keyId);
90 
91 private:
92  KeyType m_keyType;
93  KeyIdType m_keyIdType;
94  name::Component m_keyId;
95 };
96 
97 
98 namespace detail {
99 
102 {
103 public:
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
119  getDefaultSize();
120 };
121 
124 {
125 public:
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
141  getDefaultSize();
142 };
143 
144 } // namespace detail
145 
146 
148 template<typename KeyParamsInfo>
150 {
151 public:
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
176  getKeySize() const
177  {
178  return m_size;
179  }
180 
181 private:
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 
194 private:
195  uint32_t m_size;
196 };
197 
200 
203 
204 
205 namespace detail {
206 
209 {
210 public:
211  static constexpr KeyType
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 
231 {
232 public:
233  static constexpr KeyType
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 
255 template<typename KeyParamsInfo>
257 {
258 public:
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
283  getKeySize() const
284  {
285  return m_size;
286  }
287 
288 private:
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 
301 private:
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.
Definition: key-params.hpp:36
KeyParams(KeyType keyType, KeyIdType keyIdType)
Constructor.
Definition: key-params.cpp:26
virtual ~KeyParams()
KeyIdType getKeyIdType() const
Definition: key-params.hpp:54
KeyType getKeyType() const
Definition: key-params.hpp:48
const name::Component & getKeyId() const
Definition: key-params.hpp:60
void setKeyId(const name::Component &keyId)
Definition: key-params.hpp:66
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Definition: key-params.hpp:150
SimplePublicKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameters with user-specified key id.
Definition: key-params.hpp:154
SimplePublicKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameters with auto-generated key id.
Definition: key-params.hpp:168
uint32_t getKeySize() const
Definition: key-params.hpp:176
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
Definition: key-params.hpp:257
SimpleSymmetricKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameters with user-specified key id.
Definition: key-params.hpp:261
SimpleSymmetricKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameters with auto-generated key id.
Definition: key-params.hpp:275
AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys.
Definition: key-params.hpp:209
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:85
static constexpr KeyType getType()
Definition: key-params.hpp:212
static uint32_t getDefaultSize()
Definition: key-params.cpp:95
EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
Definition: key-params.hpp:124
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:69
static uint32_t getDefaultSize()
Definition: key-params.cpp:79
static constexpr KeyType getType()
Definition: key-params.hpp:127
HmacKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for HMAC keys.
Definition: key-params.hpp:231
static uint32_t getDefaultSize()
Definition: key-params.cpp:109
static constexpr KeyType getType()
Definition: key-params.hpp:234
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:101
RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
Definition: key-params.hpp:102
static uint32_t getDefaultSize()
Definition: key-params.cpp:63
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:55
static constexpr KeyType getType()
Definition: key-params.hpp:105
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.