Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame^] | 2 | /* |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 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 | #include "key-params.hpp" |
| 23 | |
| 24 | namespace ndn { |
| 25 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 26 | static const uint32_t MIN_RSA_KEY_SIZE = 1024; |
| 27 | static const uint32_t DEFAULT_RSA_KEY_SIZE = 2048; |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 28 | static const uint32_t EC_KEY_SIZES[] = {256, 384}; |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 29 | static const uint32_t AES_KEY_SIZES[] = {128, 192, 256}; |
| 30 | |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 31 | KeyParams::KeyParams(KeyType keyType, KeyIdType keyIdType) |
| 32 | : m_keyType(keyType) |
| 33 | , m_keyIdType(keyIdType) |
| 34 | { |
| 35 | BOOST_ASSERT(keyIdType != KeyIdType::USER_SPECIFIED); |
| 36 | } |
| 37 | |
| 38 | KeyParams::KeyParams(KeyType keyType, const name::Component& keyId) |
| 39 | : m_keyType(keyType) |
| 40 | , m_keyIdType(KeyIdType::USER_SPECIFIED) |
| 41 | , m_keyId(keyId) |
| 42 | { |
| 43 | BOOST_ASSERT(!keyId.empty()); |
| 44 | } |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 45 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame^] | 46 | KeyParams::~KeyParams() = default; |
| 47 | |
| 48 | namespace detail { |
| 49 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 50 | uint32_t |
| 51 | RsaKeyParamsInfo::checkKeySize(uint32_t size) |
| 52 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 53 | if (size < MIN_RSA_KEY_SIZE) |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame^] | 54 | BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported RSA key size")); |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 55 | return size; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | uint32_t |
| 59 | RsaKeyParamsInfo::getDefaultSize() |
| 60 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 61 | return DEFAULT_RSA_KEY_SIZE; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | uint32_t |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 65 | EcKeyParamsInfo::checkKeySize(uint32_t size) |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 66 | { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 67 | for (size_t i = 0; i < (sizeof(EC_KEY_SIZES) / sizeof(EC_KEY_SIZES[0])); i++) { |
| 68 | if (EC_KEY_SIZES[i] == size) |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 69 | return size; |
| 70 | } |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame^] | 71 | BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported EC key size")); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | uint32_t |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 75 | EcKeyParamsInfo::getDefaultSize() |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 76 | { |
Spyridon Mastorakis | 1ece2e3 | 2015-08-27 18:52:21 -0700 | [diff] [blame] | 77 | return EC_KEY_SIZES[0]; |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 80 | uint32_t |
| 81 | AesKeyParamsInfo::checkKeySize(uint32_t size) |
| 82 | { |
Yingdi Yu | c08d7d6 | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 83 | for (size_t i = 0; i < (sizeof(AES_KEY_SIZES) / sizeof(AES_KEY_SIZES[0])); i++) { |
| 84 | if (AES_KEY_SIZES[i] == size) |
| 85 | return size; |
| 86 | } |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame^] | 87 | BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported AES key size")); |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | uint32_t |
| 91 | AesKeyParamsInfo::getDefaultSize() |
| 92 | { |
| 93 | return AES_KEY_SIZES[0]; |
| 94 | } |
| 95 | |
Davide Pesavento | c3dfc24 | 2017-09-14 20:18:48 -0400 | [diff] [blame^] | 96 | } // namespace detail |
Yingdi Yu | 7036ce2 | 2014-06-19 18:53:37 -0700 | [diff] [blame] | 97 | } // namespace ndn |