NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
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-2019 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_SECURITY_KEY_PARAMS_HPP
23 #define NDN_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_SECURITY_KEY_PARAMS_HPP
ndn::KeyType
KeyType
The type of a cryptographic key.
Definition: security-common.hpp:85
ndn::detail::RsaKeyParamsInfo
RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
Definition: key-params.hpp:102
ndn::KeyParams
Base class for key parameters.
Definition: key-params.hpp:36
ndn::KeyIdType::RANDOM
@ RANDOM
Use a 64-bit random number as key id.
ndn::KeyIdType
KeyIdType
The type of KeyId component in a key name.
Definition: security-common.hpp:58
ndn::KeyParams::Error
Definition: key-params.hpp:39
security-common.hpp
ndn::detail::RsaKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:62
ndn::detail::EcKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:68
ndn::detail::AesKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:94
ndn::EcKeyParams
SimplePublicKeyParams< detail::EcKeyParamsInfo > EcKeyParams
EcKeyParams carries parameters for EC key.
Definition: key-params.hpp:202
ndn::KeyParams::getKeyId
const name::Component & getKeyId() const
Definition: key-params.hpp:60
ndn::SimplePublicKeyParams::SimplePublicKeyParams
SimplePublicKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameters with auto-generated key id.
Definition: key-params.hpp:168
ndn::detail::AesKeyParamsInfo
AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys.
Definition: key-params.hpp:209
ndn::RsaKeyParams
SimplePublicKeyParams< detail::RsaKeyParamsInfo > RsaKeyParams
RsaKeyParams carries parameters for RSA key.
Definition: key-params.hpp:199
ndn::KeyType::EC
@ EC
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
ndn::KeyParams::getKeyIdType
KeyIdType getKeyIdType() const
Definition: key-params.hpp:54
ndn::KeyParams::~KeyParams
virtual ~KeyParams()
ndn::SimplePublicKeyParams::getKeySize
uint32_t getKeySize() const
Definition: key-params.hpp:176
ndn::detail::AesKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:84
ndn::detail::HmacKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:100
ndn::detail::EcKeyParamsInfo
EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
Definition: key-params.hpp:124
ndn::KeyParams::getKeyType
KeyType getKeyType() const
Definition: key-params.hpp:48
ndn::SimpleSymmetricKeyParams::getKeySize
uint32_t getKeySize() const
Definition: key-params.hpp:283
ndn::KeyType::HMAC
@ HMAC
HMAC key, supports sign/verify operations.
ndn::KeyParams::setKeyId
void setKeyId(const name::Component &keyId)
Definition: key-params.hpp:66
ndn::detail::EcKeyParamsInfo::getType
static constexpr KeyType getType()
Definition: key-params.hpp:127
ndn::detail::RsaKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:54
ndn::HmacKeyParams
SimpleSymmetricKeyParams< detail::HmacKeyParamsInfo > HmacKeyParams
HmacKeyParams carries parameters for HMAC key.
Definition: key-params.hpp:309
ndn::AesKeyParams
SimpleSymmetricKeyParams< detail::AesKeyParamsInfo > AesKeyParams
AesKeyParams carries parameters for AES key.
Definition: key-params.hpp:306
ndn::SimplePublicKeyParams
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Definition: key-params.hpp:150
ndn::detail::HmacKeyParamsInfo::getType
static constexpr KeyType getType()
Definition: key-params.hpp:234
ndn::SimpleSymmetricKeyParams::SimpleSymmetricKeyParams
SimpleSymmetricKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameters with user-specified key id.
Definition: key-params.hpp:261
ndn::detail::AesKeyParamsInfo::getType
static constexpr KeyType getType()
Definition: key-params.hpp:212
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::SimpleSymmetricKeyParams::SimpleSymmetricKeyParams
SimpleSymmetricKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameters with auto-generated key id.
Definition: key-params.hpp:275
ndn::detail::HmacKeyParamsInfo
HmacKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for HMAC keys.
Definition: key-params.hpp:231
ndn::SimplePublicKeyParams::SimplePublicKeyParams
SimplePublicKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameters with user-specified key id.
Definition: key-params.hpp:154
ndn::SimpleSymmetricKeyParams
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
Definition: key-params.hpp:257
ndn::KeyParams::KeyParams
KeyParams(KeyType keyType, KeyIdType keyIdType)
Constructor.
Definition: key-params.cpp:26
ndn::detail::RsaKeyParamsInfo::getType
static constexpr KeyType getType()
Definition: key-params.hpp:105
name-component.hpp
ndn::KeyType::AES
@ AES
AES key, supports encrypt/decrypt operations.
ndn::KeyType::RSA
@ RSA
RSA key, supports sign/verify and encrypt/decrypt operations.
ndn::detail::HmacKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:108
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::EcKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:78