NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
openssl-helper.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 namespace security {
26 namespace detail {
27 
28 const EVP_MD*
30 {
31  switch (algo) {
33  return EVP_sha224();
35  return EVP_sha256();
37  return EVP_sha384();
39  return EVP_sha512();
40 #ifndef OPENSSL_NO_BLAKE2
42  return EVP_blake2b512();
44  return EVP_blake2s256();
45 #endif
47  return EVP_sha3_224();
49  return EVP_sha3_256();
51  return EVP_sha3_384();
53  return EVP_sha3_512();
54  default:
55  return nullptr;
56  }
57 }
58 
59 int
60 getEvpPkeyType(const EVP_PKEY* key)
61 {
62  return EVP_PKEY_base_id(key);
63 }
64 
66  : m_ctx(EVP_MD_CTX_new())
67 {
68  if (m_ctx == nullptr)
69  NDN_THROW(std::runtime_error("EVP_MD_CTX creation failed"));
70 }
71 
73 {
74  EVP_MD_CTX_free(m_ctx);
75 }
76 
77 EvpPkeyCtx::EvpPkeyCtx(EVP_PKEY* key)
78  : m_ctx(EVP_PKEY_CTX_new(key, nullptr))
79 {
80  if (m_ctx == nullptr)
81  NDN_THROW(std::runtime_error("EVP_PKEY_CTX creation failed"));
82 }
83 
85  : m_ctx(EVP_PKEY_CTX_new_id(id, nullptr))
86 {
87  if (m_ctx == nullptr)
88  NDN_THROW(std::runtime_error("EVP_PKEY_CTX creation failed"));
89 }
90 
92 {
93  EVP_PKEY_CTX_free(m_ctx);
94 }
95 
96 Bio::Bio(const BIO_METHOD* method)
97  : m_bio(BIO_new(method))
98 {
99  if (m_bio == nullptr)
100  NDN_THROW(std::runtime_error("BIO creation failed"));
101 }
102 
104 {
105  BIO_free_all(m_bio);
106 }
107 
108 bool
109 Bio::read(span<uint8_t> buf) const noexcept
110 {
111  BOOST_ASSERT(buf.size() <= std::numeric_limits<int>::max());
112  int n = BIO_read(m_bio, buf.data(), static_cast<int>(buf.size()));
113  return n >= 0 && static_cast<size_t>(n) == buf.size();
114 }
115 
116 bool
117 Bio::write(span<const uint8_t> buf) noexcept
118 {
119  BOOST_ASSERT(buf.size() <= std::numeric_limits<int>::max());
120  int n = BIO_write(m_bio, buf.data(), static_cast<int>(buf.size()));
121  return n >= 0 && static_cast<size_t>(n) == buf.size();
122 }
123 
124 } // namespace detail
125 } // namespace security
126 } // namespace ndn
NDN_CXX_NODISCARD bool read(span< uint8_t > buf) const noexcept
Copyright (c) 2011-2015 Regents of the University of California.
const EVP_MD * digestAlgorithmToEvpMd(DigestAlgorithm algo)
int getEvpPkeyType(const EVP_PKEY *key)
#define NDN_THROW(e)
Definition: exception.hpp:61
NDN_CXX_NODISCARD bool write(span< const uint8_t > buf) noexcept
Use the SHA-256 hash of the public key as key id.
Bio(const BIO_METHOD *method)