blob: d4840a22f466b87a152489422554dbfd588e8bc4 [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 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
Yingdi Yufe4a9182014-06-26 12:56:11 -070025#include "../common.hpp"
Yingdi Yu7036ce22014-06-19 18:53:37 -070026#include "security-common.hpp"
27
28namespace ndn {
29
30/**
31 * @brief Base class of key parameters.
32 *
33 * Its subclasses are used to store parameters for key generation.
34 */
35class KeyParams
36{
37public:
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 }
52
53 KeyType
54 getKeyType() const
55 {
56 return m_keyType;
57 }
58
59protected:
60 explicit
61 KeyParams(KeyType keyType)
62 : m_keyType(keyType)
63 {
64 }
65
66private:
67 KeyType m_keyType;
68};
69
70
71/// @brief RsaKeyParamInfo is used to initialize a SimplePublicKeyParams template for RSA key.
72class RsaKeyParamsInfo
73{
74public:
75 static KeyType
76 getType()
77 {
78 return KEY_TYPE_RSA;
79 }
80
81 /// @brief check if size is qualified, otherwise return the default key size.
82 static uint32_t
83 checkKeySize(uint32_t size);
84
85 static uint32_t
86 getDefaultSize();
87};
88
89/// @brief EcdsaKeyParamInfo is used to initialize a SimplePublicKeyParams template for ECDSA key.
90class EcdsaKeyParamsInfo
91{
92public:
93 static KeyType
94 getType()
95 {
96 return KEY_TYPE_ECDSA;
97 }
98
99 /// @brief check if size is qualified, otherwise return the default key size.
100 static uint32_t
101 checkKeySize(uint32_t size);
102
103 static uint32_t
104 getDefaultSize();
105};
106
107
108/// @brief SimplePublicKeyParams is a template for public keys with only one parameter: size.
109template<typename KeyParamsInfo>
110class SimplePublicKeyParams : public KeyParams
111{
112public:
113 explicit
114 SimplePublicKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize())
115 : KeyParams(KeyParamsInfo::getType())
116 {
117 setKeySize(size);
118 }
119
120 explicit
121 SimplePublicKeyParams(const SimplePublicKeyParams& params)
122 : KeyParams(params)
123 , m_size(params.m_size)
124 {
125 }
126
127 explicit
128 SimplePublicKeyParams(const KeyParams& params)
129 : KeyParams(params.getKeyType())
130 {
131 throw KeyParams::Error("Incorrect key parameters (incompatible key type)");
132 }
133
134 uint32_t
135 getKeySize() const
136 {
137 return m_size;
138 }
139
140private:
141 void
142 setKeySize(uint32_t size)
143 {
144 m_size = KeyParamsInfo::checkKeySize(size);
145 }
146
147 uint32_t
148 getDefaultKeySize() const
149 {
150 return KeyParamsInfo::getDefaultSize();
151 }
152
153private:
154 uint32_t m_size;
155};
156
157/// @brief RsaKeyParams carries parameters for RSA key.
158typedef SimplePublicKeyParams<RsaKeyParamsInfo> RsaKeyParams;
159
160/// @brief EcdsaKeyParams carries parameters for ECDSA key.
161typedef SimplePublicKeyParams<EcdsaKeyParamsInfo> EcdsaKeyParams;
162
163
164/// @brief AesKeyParamsInfo is used to initialize a SimpleSymmetricKeyParams template for AES key.
165class AesKeyParamsInfo
166{
167public:
168 static KeyType
169 getType()
170 {
171 return KEY_TYPE_AES;
172 }
173
174 /// @brief check if size is qualified, otherwise return the default key size.
175 static uint32_t
176 checkKeySize(uint32_t size);
177
178 static uint32_t
179 getDefaultSize();
180};
181
182
183/// @brief SimpleSymmetricKeyParams is a template for symmetric keys with only one parameter: size.
184template<typename KeyParamsInfo>
185class SimpleSymmetricKeyParams : public KeyParams
186{
187public:
188 explicit
189 SimpleSymmetricKeyParams(uint32_t size = KeyParamsInfo::getDefaultSize())
190 : KeyParams(KeyParamsInfo::getType())
191 {
192 setKeySize(size);
193 }
194
195 explicit
196 SimpleSymmetricKeyParams(const SimpleSymmetricKeyParams& params)
197 : KeyParams(params)
198 , m_size(params.m_size)
199 {
200 }
201
202 explicit
203 SimpleSymmetricKeyParams(const KeyParams& params)
204 {
205 throw KeyParams::Error("Incorrect key parameters (incompatible key type)");
206 }
207
208 uint32_t
209 getKeySize() const
210 {
211 return m_size;
212 }
213
214private:
215 void
216 setKeySize(uint32_t size)
217 {
218 m_size = KeyParamsInfo::checkKeySize(size);
219 }
220
221 uint32_t
222 getDefaultKeySize() const
223 {
224 return KeyParamsInfo::getDefaultSize();
225 }
226
227private:
228 uint32_t m_size;
229
230};
231
232typedef SimpleSymmetricKeyParams<AesKeyParamsInfo> AesKeyParams;
233
234} // namespace ndn
235
236#endif // NDN_SECURITY_KEY_PARAMS_HPP