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