blob: 792efe1ba049a7f9a0290020040cc5cf533931cb [file] [log] [blame]
Yingdi Yu7036ce22014-06-19 18:53:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yuc08d7d62015-07-16 21:05:11 -07003 * Copyright (c) 2013-2016 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
Yingdi Yuc08d7d62015-07-16 21:05:11 -070026static const uint32_t MIN_RSA_KEY_SIZE = 1024;
27static const uint32_t DEFAULT_RSA_KEY_SIZE = 2048;
Yingdi Yu7036ce22014-06-19 18:53:37 -070028static const uint32_t ECDSA_KEY_SIZES[] = {256, 384};
Yingdi Yuc08d7d62015-07-16 21:05:11 -070029static const uint32_t AES_KEY_SIZES[] = {128, 192, 256};
30
31KeyParams::~KeyParams() = default;
32
33KeyParams::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
48uint32_t
49RsaKeyParamsInfo::checkKeySize(uint32_t size)
50{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070051 if (size < MIN_RSA_KEY_SIZE)
52 BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported key size"));
53 return size;
Yingdi Yu7036ce22014-06-19 18:53:37 -070054}
55
56uint32_t
57RsaKeyParamsInfo::getDefaultSize()
58{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070059 return DEFAULT_RSA_KEY_SIZE;
Yingdi Yu7036ce22014-06-19 18:53:37 -070060}
61
62uint32_t
63EcdsaKeyParamsInfo::checkKeySize(uint32_t size)
64{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070065 for (size_t i = 0; i < (sizeof(ECDSA_KEY_SIZES) / sizeof(ECDSA_KEY_SIZES[0])); i++) {
66 if (ECDSA_KEY_SIZES[i] == size)
67 return size;
68 }
69 BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported key size"));
Yingdi Yu7036ce22014-06-19 18:53:37 -070070}
71
72uint32_t
73EcdsaKeyParamsInfo::getDefaultSize()
74{
75 return ECDSA_KEY_SIZES[0];
76}
77
78
79uint32_t
80AesKeyParamsInfo::checkKeySize(uint32_t size)
81{
Yingdi Yuc08d7d62015-07-16 21:05:11 -070082 for (size_t i = 0; i < (sizeof(AES_KEY_SIZES) / sizeof(AES_KEY_SIZES[0])); i++) {
83 if (AES_KEY_SIZES[i] == size)
84 return size;
85 }
86 BOOST_THROW_EXCEPTION(KeyParams::Error("Unsupported key size"));
Yingdi Yu7036ce22014-06-19 18:53:37 -070087}
88
89uint32_t
90AesKeyParamsInfo::getDefaultSize()
91{
92 return AES_KEY_SIZES[0];
93}
94
95} // namespace ndn