NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
key-params.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
23 
24 namespace ndn {
25 
27  : m_keyType(keyType)
28  , m_keyIdType(keyIdType)
29 {
30  BOOST_ASSERT(keyIdType != KeyIdType::USER_SPECIFIED);
31 }
32 
34  : m_keyType(keyType)
35  , m_keyIdType(KeyIdType::USER_SPECIFIED)
36  , m_keyId(keyId)
37 {
38  BOOST_ASSERT(!keyId.empty());
39 }
40 
41 KeyParams::~KeyParams() = default;
42 
43 namespace detail {
44 
45 const uint32_t MIN_RSA_KEY_SIZE = 2048;
46 const uint32_t DEFAULT_RSA_KEY_SIZE = 2048;
47 const uint32_t EC_KEY_SIZES[] = {224, 256, 384, 521};
48 const uint32_t DEFAULT_EC_KEY_SIZE = 256;
49 const uint32_t AES_KEY_SIZES[] = {128, 192, 256};
50 const uint32_t DEFAULT_AES_KEY_SIZE = 128;
51 const uint32_t DEFAULT_HMAC_KEY_SIZE = 256;
52 
53 uint32_t
55 {
56  if (size < MIN_RSA_KEY_SIZE)
57  NDN_THROW(KeyParams::Error("Unsupported RSA key size " + to_string(size)));
58  return size;
59 }
60 
61 uint32_t
63 {
64  return DEFAULT_RSA_KEY_SIZE;
65 }
66 
67 uint32_t
69 {
70  for (size_t i = 0; i < (sizeof(EC_KEY_SIZES) / sizeof(EC_KEY_SIZES[0])); i++) {
71  if (EC_KEY_SIZES[i] == size)
72  return size;
73  }
74  NDN_THROW(KeyParams::Error("Unsupported EC key size " + to_string(size)));
75 }
76 
77 uint32_t
79 {
80  return DEFAULT_EC_KEY_SIZE;
81 }
82 
83 uint32_t
85 {
86  for (size_t i = 0; i < (sizeof(AES_KEY_SIZES) / sizeof(AES_KEY_SIZES[0])); i++) {
87  if (AES_KEY_SIZES[i] == size)
88  return size;
89  }
90  NDN_THROW(KeyParams::Error("Unsupported AES key size " + to_string(size)));
91 }
92 
93 uint32_t
95 {
96  return DEFAULT_AES_KEY_SIZE;
97 }
98 
99 uint32_t
101 {
102  if (size == 0 || size % 8 != 0)
103  NDN_THROW(KeyParams::Error("Unsupported HMAC key size " + to_string(size)));
104  return size;
105 }
106 
107 uint32_t
109 {
110  return DEFAULT_HMAC_KEY_SIZE;
111 }
112 
113 } // namespace detail
114 } // namespace ndn
ndn::KeyType
KeyType
The type of a cryptographic key.
Definition: security-common.hpp:85
ndn::KeyIdType
KeyIdType
The type of KeyId component in a key name.
Definition: security-common.hpp:58
ndn::KeyParams::Error
Definition: key-params.hpp:39
ndn::detail::DEFAULT_AES_KEY_SIZE
const uint32_t DEFAULT_AES_KEY_SIZE
Definition: key-params.cpp:50
ndn::detail::RsaKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:62
ndn::detail::EcKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:68
ndn::detail::EC_KEY_SIZES
const uint32_t EC_KEY_SIZES[]
Definition: key-params.cpp:47
ndn::detail::AesKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:94
ndn::detail::AES_KEY_SIZES
const uint32_t AES_KEY_SIZES[]
Definition: key-params.cpp:49
ndn::detail::DEFAULT_EC_KEY_SIZE
const uint32_t DEFAULT_EC_KEY_SIZE
Definition: key-params.cpp:48
ndn::detail::DEFAULT_HMAC_KEY_SIZE
const uint32_t DEFAULT_HMAC_KEY_SIZE
Definition: key-params.cpp:51
ndn::KeyParams::~KeyParams
virtual ~KeyParams()
ndn::detail::AesKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:84
ndn::detail::HmacKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:100
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::detail::MIN_RSA_KEY_SIZE
const uint32_t MIN_RSA_KEY_SIZE
Definition: key-params.cpp:45
ndn::detail::RsaKeyParamsInfo::checkKeySize
static uint32_t checkKeySize(uint32_t size)
check if size is valid and supported for this key type.
Definition: key-params.cpp:54
ndn::name::Component::empty
NDN_CXX_NODISCARD bool empty() const
Definition: name-component.hpp:536
ndn::detail::DEFAULT_RSA_KEY_SIZE
const uint32_t DEFAULT_RSA_KEY_SIZE
Definition: key-params.cpp:46
ndn::name::Component
Represents a name component.
Definition: name-component.hpp:94
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::KeyIdType::USER_SPECIFIED
@ USER_SPECIFIED
User-specified key id.
ndn::KeyParams::KeyParams
KeyParams(KeyType keyType, KeyIdType keyIdType)
Constructor.
Definition: key-params.cpp:26
key-params.hpp
ndn::detail::HmacKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:108
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::detail::EcKeyParamsInfo::getDefaultSize
static uint32_t getDefaultSize()
Definition: key-params.cpp:78