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-2023 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 constexpr uint32_t MIN_RSA_KEY_SIZE = 2048;
46 constexpr uint32_t MAX_RSA_KEY_SIZE = 16384;
47 constexpr uint32_t DEFAULT_RSA_KEY_SIZE = 2048;
48 constexpr uint32_t EC_KEY_SIZES[] = {224, 256, 384, 521};
49 constexpr uint32_t DEFAULT_EC_KEY_SIZE = 256;
50 constexpr uint32_t AES_KEY_SIZES[] = {128, 192, 256};
51 constexpr uint32_t DEFAULT_AES_KEY_SIZE = 128;
52 constexpr uint32_t DEFAULT_HMAC_KEY_SIZE = 256;
53 
54 uint32_t
56 {
57  if (size < MIN_RSA_KEY_SIZE || size > MAX_RSA_KEY_SIZE)
58  NDN_THROW(KeyParams::Error("Unsupported RSA key size " + to_string(size)));
59  return size;
60 }
61 
62 uint32_t
64 {
65  return DEFAULT_RSA_KEY_SIZE;
66 }
67 
68 uint32_t
70 {
71  for (auto s : EC_KEY_SIZES) {
72  if (s == size)
73  return size;
74  }
75  NDN_THROW(KeyParams::Error("Unsupported EC key size " + to_string(size)));
76 }
77 
78 uint32_t
80 {
81  return DEFAULT_EC_KEY_SIZE;
82 }
83 
84 uint32_t
86 {
87  for (auto s : AES_KEY_SIZES) {
88  if (s == size)
89  return size;
90  }
91  NDN_THROW(KeyParams::Error("Unsupported AES key size " + to_string(size)));
92 }
93 
94 uint32_t
96 {
97  return DEFAULT_AES_KEY_SIZE;
98 }
99 
100 uint32_t
102 {
103  if (size == 0 || size % 8 != 0)
104  NDN_THROW(KeyParams::Error("Unsupported HMAC key size " + to_string(size)));
105  return size;
106 }
107 
108 uint32_t
110 {
111  return DEFAULT_HMAC_KEY_SIZE;
112 }
113 
114 } // namespace detail
115 } // namespace ndn
KeyParams(KeyType keyType, KeyIdType keyIdType)
Constructor.
Definition: key-params.cpp:26
virtual ~KeyParams()
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:85
static uint32_t getDefaultSize()
Definition: key-params.cpp:95
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:69
static uint32_t getDefaultSize()
Definition: key-params.cpp:79
static uint32_t getDefaultSize()
Definition: key-params.cpp:109
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:101
static uint32_t getDefaultSize()
Definition: key-params.cpp:63
static uint32_t checkKeySize(uint32_t size)
Check if size is valid and supported for this key type.
Definition: key-params.cpp:55
Represents a name component.
bool empty() const noexcept
#define NDN_THROW(e)
Definition: exception.hpp:56
constexpr uint32_t AES_KEY_SIZES[]
Definition: key-params.cpp:50
constexpr uint32_t MIN_RSA_KEY_SIZE
Definition: key-params.cpp:45
constexpr uint32_t DEFAULT_HMAC_KEY_SIZE
Definition: key-params.cpp:52
constexpr uint32_t DEFAULT_RSA_KEY_SIZE
Definition: key-params.cpp:47
constexpr uint32_t EC_KEY_SIZES[]
Definition: key-params.cpp:48
constexpr uint32_t DEFAULT_AES_KEY_SIZE
Definition: key-params.cpp:51
constexpr uint32_t DEFAULT_EC_KEY_SIZE
Definition: key-params.cpp:49
constexpr uint32_t MAX_RSA_KEY_SIZE
Definition: key-params.cpp:46
std::string to_string(const errinfo_stacktrace &x)
Definition: exception.cpp:30
Definition: data.cpp:25
KeyType
The type of a cryptographic key.
KeyIdType
The type of KeyId component in a key name.
@ USER_SPECIFIED
User-specified key id.