blob: 59bf26d2f53ecf8bed49c70222dbef8eda4951c9 [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc3dfc242017-09-14 20:18:48 -04002/*
Davide Pesavento3c7969f2018-09-08 15:31:35 -04003 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yu7036ce22014-06-19 18:53:37 -07004 *
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
24namespace ndn {
25
Davide Pesavento648ae9e2018-09-08 15:40:02 -040026static const uint32_t MIN_RSA_KEY_SIZE = 2048;
Yingdi Yuc08d7d62015-07-16 21:05:11 -070027static const uint32_t DEFAULT_RSA_KEY_SIZE = 2048;
Davide Pesavento3c7969f2018-09-08 15:31:35 -040028static const uint32_t EC_KEY_SIZES[] = {224, 256, 384, 521};
29static const uint32_t DEFAULT_EC_KEY_SIZE = 256;
Yingdi Yuc08d7d62015-07-16 21:05:11 -070030static const uint32_t AES_KEY_SIZES[] = {128, 192, 256};
Davide Pesavento3c7969f2018-09-08 15:31:35 -040031static const uint32_t DEFAULT_AES_KEY_SIZE = 128;
Yingdi Yuc08d7d62015-07-16 21:05:11 -070032
Yingdi Yuc08d7d62015-07-16 21:05:11 -070033KeyParams::KeyParams(KeyType keyType, KeyIdType keyIdType)
34 : m_keyType(keyType)
35 , m_keyIdType(keyIdType)
36{
37 BOOST_ASSERT(keyIdType != KeyIdType::USER_SPECIFIED);
38}
39
40KeyParams::KeyParams(KeyType keyType, const name::Component& keyId)
41 : m_keyType(keyType)
42 , m_keyIdType(KeyIdType::USER_SPECIFIED)
43 , m_keyId(keyId)
44{
45 BOOST_ASSERT(!keyId.empty());
46}
Yingdi Yu7036ce22014-06-19 18:53:37 -070047
Davide Pesaventoc3dfc242017-09-14 20:18:48 -040048KeyParams::~KeyParams() = default;
49
50namespace detail {
51
Yingdi Yu7036ce22014-06-19 18:53:37 -070052uint32_t
53RsaKeyParamsInfo::checkKeySize(uint32_t size)
54{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070055 if (size < MIN_RSA_KEY_SIZE)
Davide Pesavento3c7969f2018-09-08 15:31:35 -040056 BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported RSA key size " + to_string(size)));
Yingdi Yuc08d7d62015-07-16 21:05:11 -070057 return size;
Yingdi Yu7036ce22014-06-19 18:53:37 -070058}
59
60uint32_t
61RsaKeyParamsInfo::getDefaultSize()
62{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070063 return DEFAULT_RSA_KEY_SIZE;
Yingdi Yu7036ce22014-06-19 18:53:37 -070064}
65
66uint32_t
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070067EcKeyParamsInfo::checkKeySize(uint32_t size)
Yingdi Yu7036ce22014-06-19 18:53:37 -070068{
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070069 for (size_t i = 0; i < (sizeof(EC_KEY_SIZES) / sizeof(EC_KEY_SIZES[0])); i++) {
70 if (EC_KEY_SIZES[i] == size)
Yingdi Yuc08d7d62015-07-16 21:05:11 -070071 return size;
72 }
Davide Pesavento3c7969f2018-09-08 15:31:35 -040073 BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported EC key size " + to_string(size)));
Yingdi Yu7036ce22014-06-19 18:53:37 -070074}
75
76uint32_t
Spyridon Mastorakis1ece2e32015-08-27 18:52:21 -070077EcKeyParamsInfo::getDefaultSize()
Yingdi Yu7036ce22014-06-19 18:53:37 -070078{
Davide Pesavento3c7969f2018-09-08 15:31:35 -040079 return DEFAULT_EC_KEY_SIZE;
Yingdi Yu7036ce22014-06-19 18:53:37 -070080}
81
Yingdi Yu7036ce22014-06-19 18:53:37 -070082uint32_t
83AesKeyParamsInfo::checkKeySize(uint32_t size)
84{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070085 for (size_t i = 0; i < (sizeof(AES_KEY_SIZES) / sizeof(AES_KEY_SIZES[0])); i++) {
86 if (AES_KEY_SIZES[i] == size)
87 return size;
88 }
Davide Pesavento3c7969f2018-09-08 15:31:35 -040089 BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported AES key size " + to_string(size)));
Yingdi Yu7036ce22014-06-19 18:53:37 -070090}
91
92uint32_t
93AesKeyParamsInfo::getDefaultSize()
94{
Davide Pesavento3c7969f2018-09-08 15:31:35 -040095 return DEFAULT_AES_KEY_SIZE;
Yingdi Yu7036ce22014-06-19 18:53:37 -070096}
97
Davide Pesaventoc3dfc242017-09-14 20:18:48 -040098} // namespace detail
Yingdi Yu7036ce22014-06-19 18:53:37 -070099} // namespace ndn