key-params.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_SECURITY_KEY_PARAMS_HPP
23 #define NDN_SECURITY_KEY_PARAMS_HPP
24 
25 #include "../common.hpp"
26 #include "../name-component.hpp"
27 #include "security-common.hpp"
28 
29 namespace ndn {
30 
36 class KeyParams
37 {
38 public:
39  class Error : public std::runtime_error
40  {
41  public:
42  explicit
43  Error(const std::string& what)
44  : std::runtime_error(what)
45  {
46  }
47  };
48 
49  virtual
50  ~KeyParams();
51 
52  KeyType
53  getKeyType() const
54  {
55  return m_keyType;
56  }
57 
58  KeyIdType
59  getKeyIdType() const
60  {
61  return m_keyIdType;
62  }
63 
64  void
65  setKeyId(const name::Component& keyId)
66  {
67  m_keyId = keyId;
68  }
69 
70  const name::Component&
71  getKeyId() const
72  {
73  return m_keyId;
74  }
75 
76 protected:
84  KeyParams(KeyType keyType, KeyIdType keyIdType);
85 
94  KeyParams(KeyType keyType, const name::Component& keyId);
95 
96 private:
97  KeyType m_keyType;
98  KeyIdType m_keyIdType;
99  name::Component m_keyId;
100 };
101 
102 
105 {
106 public:
107  static KeyType
109  {
110  return KeyType::RSA;
111  }
112 
118  static uint32_t
119  checkKeySize(uint32_t size);
120 
121  static uint32_t
122  getDefaultSize();
123 };
124 
127 {
128 public:
129  static KeyType
131  {
132  return KeyType::EC;
133  }
134 
140  static uint32_t
141  checkKeySize(uint32_t size);
142 
143  static uint32_t
144  getDefaultSize();
145 };
146 
147 
149 template<typename KeyParamsInfo>
151 {
152 public:
154  explicit
156  uint32_t size = KeyParamsInfo::getDefaultSize())
157  : KeyParams(KeyParamsInfo::getType(), keyId)
158  {
159  setKeySize(size);
160  }
161 
168  explicit
169  SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize(),
170  KeyIdType keyIdType = KeyIdType::RANDOM)
171  : KeyParams(KeyParamsInfo::getType(), keyIdType)
172  {
173  setKeySize(size);
174  }
175 
176  uint32_t
177  getKeySize() const
178  {
179  return m_size;
180  }
181 
182 private:
183  void
184  setKeySize(uint32_t size)
185  {
186  m_size = KeyParamsInfo::checkKeySize(size);
187  }
188 
189  uint32_t
190  getDefaultKeySize() const
191  {
192  return KeyParamsInfo::getDefaultSize();
193  }
194 
195 private:
196  uint32_t m_size;
197 };
198 
201 
204 
207 {
208 public:
209  static KeyType
211  {
212  return KeyType::AES;
213  }
214 
220  static uint32_t
221  checkKeySize(uint32_t size);
222 
223  static uint32_t
224  getDefaultSize();
225 };
226 
228 template<typename KeyParamsInfo>
230 {
231 public:
233  explicit
235  uint32_t size = KeyParamsInfo::getDefaultSize())
236  : KeyParams(KeyParamsInfo::getType(), keyId)
237  {
238  setKeySize(size);
239  }
240 
241  uint32_t
242  getKeySize() const
243  {
244  return m_size;
245  }
246 
247 private:
248  void
249  setKeySize(uint32_t size)
250  {
251  m_size = KeyParamsInfo::checkKeySize(size);
252  }
253 
254  uint32_t
255  getDefaultKeySize() const
256  {
257  return KeyParamsInfo::getDefaultSize();
258  }
259 
260 private:
261  uint32_t m_size;
262 };
263 
265 
266 } // namespace ndn
267 
268 #endif // NDN_SECURITY_KEY_PARAMS_HPP
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size...
Definition: key-params.hpp:229
SimplePublicKeyParams< RsaKeyParamsInfo > RsaKeyParams
RsaKeyParams carries parameters for RSA key.
Definition: key-params.hpp:200
SimpleSymmetricKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameter with user specified keyId.
Definition: key-params.hpp:234
AesKeyParamsInfo is used to initialize a SimpleSymmetricKeyParams template for AES key...
Definition: key-params.hpp:206
Error(const std::string &what)
Definition: key-params.hpp:43
KeyIdType
The type of KeyId component in a key name.
void setKeyId(const name::Component &keyId)
Definition: key-params.hpp:65
KeyType getKeyType() const
Definition: key-params.hpp:53
virtual ~KeyParams()
static uint32_t checkKeySize(uint32_t size)
check if size is qualified.
Definition: key-params.cpp:80
uint32_t getKeySize() const
Definition: key-params.hpp:177
static uint32_t getDefaultSize()
Definition: key-params.cpp:73
static KeyType getType()
Definition: key-params.hpp:210
STL namespace.
EcKeyParamInfo is used to initialize a SimplePublicKeyParams template for elliptic curve key...
Definition: key-params.hpp:126
static uint32_t checkKeySize(uint32_t size)
check if size is qualified.
Definition: key-params.cpp:49
uint32_t getKeySize() const
Definition: key-params.hpp:242
SimplePublicKeyParams(const name::Component &keyId, uint32_t size=KeyParamsInfo::getDefaultSize())
Create key parameter with user specified keyId.
Definition: key-params.hpp:155
static uint32_t getDefaultSize()
Definition: key-params.cpp:90
RsaKeyParamInfo is used to initialize a SimplePublicKeyParams template for RSA key.
Definition: key-params.hpp:104
static KeyType getType()
Definition: key-params.hpp:130
static uint32_t getDefaultSize()
Definition: key-params.cpp:57
static uint32_t checkKeySize(uint32_t size)
check if size is qualified.
Definition: key-params.cpp:63
KeyParams(KeyType keyType, KeyIdType keyIdType)
Create a key generation parameter.
Definition: key-params.cpp:33
SimplePublicKeyParams< EcKeyParamsInfo > EcKeyParams
EcKeyParams carries parameters for EC key.
Definition: key-params.hpp:203
KeyIdType getKeyIdType() const
Definition: key-params.hpp:59
SimplePublicKeyParams(uint32_t size=KeyParamsInfo::getDefaultSize(), KeyIdType keyIdType=KeyIdType::RANDOM)
Create key parameter with auto-created keyId.
Definition: key-params.hpp:169
Component holds a read-only name component value.
static KeyType getType()
Definition: key-params.hpp:108
SimpleSymmetricKeyParams< AesKeyParamsInfo > AesKeyParams
Definition: key-params.hpp:264
Use a 64-bit random number as the key id.
Base class of key parameters.
Definition: key-params.hpp:36
SimplePublicKeyParams is a template for public keys with only one parameter: size.
Definition: key-params.hpp:150
const name::Component & getKeyId() const
Definition: key-params.hpp:71