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-2018 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
45  ~KeyParams();
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 
229 } // namespace detail
230 
231 
233 template<typename KeyParamsInfo>
235 {
236 public:
238  explicit
240  uint32_t size = KeyParamsInfo::getDefaultSize())
241  : KeyParams(KeyParamsInfo::getType(), keyId)
242  {
243  setKeySize(size);
244  }
245 
252  explicit
253  SimpleSymmetricKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
254  KeyIdType keyIdType = KeyIdType::RANDOM)
255  : KeyParams(KeyParamsInfo::getType(), keyIdType)
256  {
257  setKeySize(size);
258  }
259 
260  uint32_t
261  getKeySize() const
262  {
263  return m_size;
264  }
265 
266 private:
267  void
268  setKeySize(uint32_t size)
269  {
270  m_size = KeyParamsInfo::checkKeySize(size);
271  }
272 
273  uint32_t
274  getDefaultKeySize() const
275  {
276  return KeyParamsInfo::getDefaultSize();
277  }
278 
279 private:
280  uint32_t m_size;
281 };
282 
285 
286 } // namespace ndn
287 
288 #endif // NDN_SECURITY_KEY_PARAMS_HPP
static constexpr KeyType getType()
Definition: key-params.hpp:212
static constexpr KeyType getType()
Definition: key-params.hpp:105
Definition: data.cpp:26
static constexpr KeyType getType()
Definition: key-params.hpp:127
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size...
Definition: key-params.hpp:234
SimpleSymmetricKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameter with auto-created keyId.
Definition: key-params.hpp:253
SimpleSymmetricKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameter with user specified keyId.
Definition: key-params.hpp:239
KeyIdType
The type of KeyId component in a key name.
void setKeyId(const name::Component &keyId)
Definition: key-params.hpp:66
KeyType getKeyType() const
Definition: key-params.hpp:48
virtual ~KeyParams()
uint32_t getKeySize() const
Definition: key-params.hpp:176
RSA key, supports sign/verify and encrypt/decrypt operations.
RsaKeyParamInfo is used to instantiate SimplePublicKeyParams for RSA keys.
Definition: key-params.hpp:101
SimplePublicKeyParams< detail::RsaKeyParamsInfo > RsaKeyParams
RsaKeyParams carries parameters for RSA key.
Definition: key-params.hpp:199
KeyType
The type of a cryptographic key.
SimplePublicKeyParams< detail::EcKeyParamsInfo > EcKeyParams
EcKeyParams carries parameters for EC key.
Definition: key-params.hpp:202
uint32_t getKeySize() const
Definition: key-params.hpp:261
SimplePublicKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameter with user specified keyId.
Definition: key-params.hpp:154
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:284
KeyParams(KeyType keyType, KeyIdType keyIdType)
Create a key generation parameter.
Definition: key-params.cpp:33
KeyIdType getKeyIdType() const
Definition: key-params.hpp:54
EcKeyParamInfo is used to instantiate SimplePublicKeyParams for elliptic curve keys.
Definition: key-params.hpp:123
SimplePublicKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameter with auto-created keyId.
Definition: key-params.hpp:168
Represents a name component.
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:149
AES key, supports encrypt/decrypt operations.
const name::Component & getKeyId() const
Definition: key-params.hpp:60
AesKeyParamsInfo is used to instantiate SimpleSymmetricKeyParams for AES keys.
Definition: key-params.hpp:208