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-2017 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 
25 #include "security-common.hpp"
26 #include "../name-component.hpp"
27 
28 namespace ndn {
29 
35 class KeyParams
36 {
37 public:
38  class Error : public std::runtime_error
39  {
40  public:
41  explicit
42  Error(const std::string& what)
43  : std::runtime_error(what)
44  {
45  }
46  };
47 
48  virtual
49  ~KeyParams();
50 
51  KeyType
52  getKeyType() const
53  {
54  return m_keyType;
55  }
56 
57  KeyIdType
58  getKeyIdType() const
59  {
60  return m_keyIdType;
61  }
62 
63  const name::Component&
64  getKeyId() const
65  {
66  return m_keyId;
67  }
68 
69  void
70  setKeyId(const name::Component& keyId)
71  {
72  m_keyId = keyId;
73  }
74 
75 protected:
83  KeyParams(KeyType keyType, KeyIdType keyIdType);
84 
93  KeyParams(KeyType keyType, const name::Component& keyId);
94 
95 private:
96  KeyType m_keyType;
97  KeyIdType m_keyIdType;
98  name::Component m_keyId;
99 };
100 
101 
102 namespace detail {
103 
106 {
107 public:
108  static constexpr KeyType
110  {
111  return KeyType::RSA;
112  }
113 
119  static uint32_t
120  checkKeySize(uint32_t size);
121 
122  static uint32_t
123  getDefaultSize();
124 };
125 
128 {
129 public:
130  static constexpr KeyType
132  {
133  return KeyType::EC;
134  }
135 
141  static uint32_t
142  checkKeySize(uint32_t size);
143 
144  static uint32_t
145  getDefaultSize();
146 };
147 
148 } // namespace detail
149 
150 
152 template<typename KeyParamsInfo>
154 {
155 public:
157  explicit
159  uint32_t size = KeyParamsInfo::getDefaultSize())
160  : KeyParams(KeyParamsInfo::getType(), keyId)
161  {
162  setKeySize(size);
163  }
164 
171  explicit
172  SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
173  KeyIdType keyIdType = KeyIdType::RANDOM)
174  : KeyParams(KeyParamsInfo::getType(), keyIdType)
175  {
176  setKeySize(size);
177  }
178 
179  uint32_t
180  getKeySize() const
181  {
182  return m_size;
183  }
184 
185 private:
186  void
187  setKeySize(uint32_t size)
188  {
189  m_size = KeyParamsInfo::checkKeySize(size);
190  }
191 
192  uint32_t
193  getDefaultKeySize() const
194  {
195  return KeyParamsInfo::getDefaultSize();
196  }
197 
198 private:
199  uint32_t m_size;
200 };
201 
204 
207 
208 
209 namespace detail {
210 
213 {
214 public:
215  static constexpr KeyType
217  {
218  return KeyType::AES;
219  }
220 
226  static uint32_t
227  checkKeySize(uint32_t size);
228 
229  static uint32_t
230  getDefaultSize();
231 };
232 
233 } // namespace detail
234 
235 
237 template<typename KeyParamsInfo>
239 {
240 public:
242  explicit
244  uint32_t size = KeyParamsInfo::getDefaultSize())
245  : KeyParams(KeyParamsInfo::getType(), keyId)
246  {
247  setKeySize(size);
248  }
249 
256  explicit
257  SimpleSymmetricKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
258  KeyIdType keyIdType = KeyIdType::RANDOM)
259  : KeyParams(KeyParamsInfo::getType(), keyIdType)
260  {
261  setKeySize(size);
262  }
263 
264  uint32_t
265  getKeySize() const
266  {
267  return m_size;
268  }
269 
270 private:
271  void
272  setKeySize(uint32_t size)
273  {
274  m_size = KeyParamsInfo::checkKeySize(size);
275  }
276 
277  uint32_t
278  getDefaultKeySize() const
279  {
280  return KeyParamsInfo::getDefaultSize();
281  }
282 
283 private:
284  uint32_t m_size;
285 };
286 
289 
290 } // namespace ndn
291 
292 #endif // NDN_SECURITY_KEY_PARAMS_HPP
static constexpr KeyType getType()
Definition: key-params.hpp:216
static constexpr KeyType getType()
Definition: key-params.hpp:109
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
static constexpr KeyType getType()
Definition: key-params.hpp:131
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size...
Definition: key-params.hpp:238
SimpleSymmetricKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameter with auto-created keyId.
Definition: key-params.hpp:257
SimpleSymmetricKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameter with user specified keyId.
Definition: key-params.hpp:243
Error(const std::string &what)
Definition: key-params.hpp:42
KeyIdType
The type of KeyId component in a key name.
void setKeyId(const name::Component &keyId)
Definition: key-params.hpp:70
KeyType getKeyType() const
Definition: key-params.hpp:52
virtual ~KeyParams()
uint32_t getKeySize() const
Definition: key-params.hpp:180
RSA key, supports sign/verify and encrypt/decrypt operations.
STL namespace.
RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
Definition: key-params.hpp:105
SimplePublicKeyParams< detail::RsaKeyParamsInfo > RsaKeyParams
RsaKeyParams carries parameters for RSA key.
Definition: key-params.hpp:203
KeyType
The type of a cryptographic key.
SimplePublicKeyParams< detail::EcKeyParamsInfo > EcKeyParams
EcKeyParams carries parameters for EC key.
Definition: key-params.hpp:206
uint32_t getKeySize() const
Definition: key-params.hpp:265
SimplePublicKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameter with user specified keyId.
Definition: key-params.hpp:158
Elliptic Curve key (e.g. for ECDSA), supports sign/verify operations.
SimpleSymmetricKeyParams< detail::AesKeyParamsInfo > AesKeyParams
AesKeyParams carries parameters for AES key.
Definition: key-params.hpp:288
KeyParams(KeyType keyType, KeyIdType keyIdType)
Create a key generation parameter.
Definition: key-params.cpp:31
KeyIdType getKeyIdType() const
Definition: key-params.hpp:58
EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
Definition: key-params.hpp:127
SimplePublicKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameter with auto-created keyId.
Definition: key-params.hpp:172
Component holds a read-only name component value.
Use a 64-bit random number as the key id.
Base class of key parameters.
Definition: key-params.hpp:35
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Definition: key-params.hpp:153
AES key, supports encrypt/decrypt operations.
const name::Component & getKeyId() const
Definition: key-params.hpp:64
AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys.
Definition: key-params.hpp:212