security: convert more functions to span
Update span-lite to commit 2962cb925c2323b936e4d5f366d2cddfb1b8dc68
and enable the initializer_list constructor.
Change-Id: Ie679315fe2dd69a8b9760c3b7dd47e97fcdc04c2
diff --git a/tests/unit/security/transform/block-cipher.t.cpp b/tests/unit/security/transform/block-cipher.t.cpp
index 60944ad..d8b3c3b 100644
--- a/tests/unit/security/transform/block-cipher.t.cpp
+++ b/tests/unit/security/transform/block-cipher.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -67,9 +67,9 @@
// encrypt
OBufferStream os;
- bufferSource(plainText) >>
- blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT,
- key, sizeof(key), iv, sizeof(iv)) >> streamSink(os);
+ bufferSource(plainText)
+ >> blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, key, iv)
+ >> streamSink(os);
auto buf = os.buf();
BOOST_CHECK_EQUAL_COLLECTIONS(cipherText, cipherText + sizeof(cipherText),
@@ -77,9 +77,9 @@
// decrypt
OBufferStream os2;
- bufferSource(cipherText) >>
- blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::DECRYPT,
- key, sizeof(key), iv, sizeof(iv)) >> streamSink(os2);
+ bufferSource(cipherText)
+ >> blockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::DECRYPT, key, iv)
+ >> streamSink(os2);
auto buf2 = os2.buf();
BOOST_CHECK_EQUAL_COLLECTIONS(plainText, plainText + sizeof(plainText),
@@ -87,19 +87,19 @@
// invalid key length
const uint8_t badKey[] = {0x00, 0x01, 0x02, 0x03};
- BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT,
- badKey, sizeof(badKey), iv, sizeof(iv)), Error);
+ BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, badKey, iv),
+ Error);
// wrong iv length
const uint8_t badIv[] = {0x00, 0x01, 0x02, 0x03};
- BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT,
- key, sizeof(key), badIv, sizeof(badIv)), Error);
+ BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::AES_CBC, CipherOperator::ENCRYPT, key, badIv),
+ Error);
}
BOOST_AUTO_TEST_CASE(InvalidAlgorithm)
{
- BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::NONE, CipherOperator::ENCRYPT,
- nullptr, 0, nullptr, 0), Error);
+ BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::NONE, CipherOperator::DECRYPT, {}, {}), Error);
+ BOOST_CHECK_THROW(BlockCipher(BlockCipherAlgorithm::NONE, CipherOperator::ENCRYPT, {}, {}), Error);
}
BOOST_AUTO_TEST_SUITE_END() // TestBlockCipher
diff --git a/tests/unit/security/transform/buffer-source.t.cpp b/tests/unit/security/transform/buffer-source.t.cpp
index 4c56591..f957282 100644
--- a/tests/unit/security/transform/buffer-source.t.cpp
+++ b/tests/unit/security/transform/buffer-source.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -76,7 +76,7 @@
std::string out3 = os3.str();
BOOST_CHECK_EQUAL_COLLECTIONS(out3.begin(), out3.end(), in3.begin(), in3.end());
- InputBuffers in4{make_span(in), {reinterpret_cast<const uint8_t*>(in2.data()), in2.size()}};
+ InputBuffers in4{{in}, {reinterpret_cast<const uint8_t*>(in2.data()), in2.size()}};
std::ostringstream os4;
bufferSource(in4) >> streamSink(os4);
std::string out4 = os4.str();
diff --git a/tests/unit/security/transform/private-key.t.cpp b/tests/unit/security/transform/private-key.t.cpp
index 87d39fe..4be078b 100644
--- a/tests/unit/security/transform/private-key.t.cpp
+++ b/tests/unit/security/transform/private-key.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -681,7 +681,7 @@
PublicKey pKey;
pKey.loadPkcs8(*pKeyBits);
BOOST_CHECK_NO_THROW(bufferSource(data) >>
- verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >>
+ verifierFilter(DigestAlgorithm::SHA256, pKey, *sig) >>
boolSink(result));
}
else {
@@ -689,7 +689,7 @@
BOOST_CHECK_THROW(sKey->derivePublicKey(), PrivateKey::Error);
#endif
BOOST_CHECK_NO_THROW(bufferSource(data) >>
- verifierFilter(DigestAlgorithm::SHA256, *sKey, sig->data(), sig->size()) >>
+ verifierFilter(DigestAlgorithm::SHA256, *sKey, *sig) >>
boolSink(result));
}
BOOST_CHECK(result);
diff --git a/tests/unit/security/transform/public-key.t.cpp b/tests/unit/security/transform/public-key.t.cpp
index d572ecc..c888482 100644
--- a/tests/unit/security/transform/public-key.t.cpp
+++ b/tests/unit/security/transform/public-key.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -71,14 +71,14 @@
{
T dataSet;
- auto pKeyPkcs8Base64 = reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.data());
- size_t pKeyPkcs8Base64Len = dataSet.publicKeyPkcs8.size();
+ auto pKeyPkcs8Base64 = make_span(reinterpret_cast<const uint8_t*>(dataSet.publicKeyPkcs8.data()),
+ dataSet.publicKeyPkcs8.size());
OBufferStream os;
- bufferSource(make_span(pKeyPkcs8Base64, pKeyPkcs8Base64Len)) >> base64Decode() >> streamSink(os);
+ bufferSource(pKeyPkcs8Base64) >> base64Decode() >> streamSink(os);
auto pKeyPkcs8 = os.buf();
PublicKey pKey1;
- BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64({pKeyPkcs8Base64, pKeyPkcs8Base64Len}));
+ BOOST_CHECK_NO_THROW(pKey1.loadPkcs8Base64(pKeyPkcs8Base64));
std::stringstream ss2(dataSet.publicKeyPkcs8);
PublicKey pKey2;
@@ -94,7 +94,7 @@
OBufferStream os5;
BOOST_REQUIRE_NO_THROW(pKey1.savePkcs8Base64(os5));
- BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8Base64, pKeyPkcs8Base64 + pKeyPkcs8Base64Len,
+ BOOST_CHECK_EQUAL_COLLECTIONS(pKeyPkcs8Base64.begin(), pKeyPkcs8Base64.end(),
os5.buf()->begin(), os5.buf()->end());
OBufferStream os6;
diff --git a/tests/unit/security/transform/signer-filter.t.cpp b/tests/unit/security/transform/signer-filter.t.cpp
index 063e3fc..cad6f04 100644
--- a/tests/unit/security/transform/signer-filter.t.cpp
+++ b/tests/unit/security/transform/signer-filter.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -92,7 +92,7 @@
bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2);
auto sig = os2.buf();
- BOOST_TEST(verifySignature({{data, sizeof(data)}}, sig->data(), sig->size(), pubKey->data(), pubKey->size()));
+ BOOST_TEST(verifySignature({data}, *sig, *pubKey));
}
BOOST_AUTO_TEST_CASE(Ecdsa)
@@ -130,7 +130,7 @@
bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2);
auto sig = os2.buf();
- BOOST_TEST(verifySignature({{data, sizeof(data)}}, sig->data(), sig->size(), pubKey->data(), pubKey->size()));
+ BOOST_TEST(verifySignature({data}, *sig, *pubKey));
}
BOOST_AUTO_TEST_SUITE(Hmac)
@@ -192,7 +192,7 @@
BOOST_AUTO_TEST_CASE(Rfc4231Test2)
{
// Test case 2 (HMAC-SHA-256 only)
- const char rawKey[] = "Jefe";
+ const uint8_t rawKey[] = {'J', 'e', 'f', 'e'};
const std::string data("what do ya want for nothing?");
const uint8_t hmacSha256[] = {0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04,
0x24, 0x26, 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08,
@@ -200,7 +200,7 @@
0x38, 0x43};
PrivateKey key;
- key.loadRaw(KeyType::HMAC, {reinterpret_cast<const uint8_t*>(rawKey), std::strlen(rawKey)});
+ key.loadRaw(KeyType::HMAC, rawKey);
OBufferStream os256;
bufferSource(data) >> signerFilter(DigestAlgorithm::SHA256, key) >> streamSink(os256);
diff --git a/tests/unit/security/transform/verifier-filter.t.cpp b/tests/unit/security/transform/verifier-filter.t.cpp
index 8feabfc..882890f 100644
--- a/tests/unit/security/transform/verifier-filter.t.cpp
+++ b/tests/unit/security/transform/verifier-filter.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2021 Regents of the University of California.
+ * Copyright (c) 2013-2022 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -98,12 +98,12 @@
bufferSource(DATA) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2);
auto sig = os2.buf();
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, sig->data(), sig->size()), Error);
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, sKey, sig->data(), sig->size()), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, *sig), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, sKey, *sig), Error);
bool result = false;
bufferSource(DATA) >>
- verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >>
+ verifierFilter(DigestAlgorithm::SHA256, pKey, *sig) >>
boolSink(result);
BOOST_CHECK_EQUAL(result, true);
@@ -144,12 +144,12 @@
bufferSource(DATA) >> signerFilter(DigestAlgorithm::SHA256, sKey) >> streamSink(os2);
auto sig = os2.buf();
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, sig->data(), sig->size()), Error);
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, sKey, sig->data(), sig->size()), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, pKey, *sig), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, sKey, *sig), Error);
bool result = false;
bufferSource(DATA) >>
- verifierFilter(DigestAlgorithm::SHA256, pKey, sig->data(), sig->size()) >>
+ verifierFilter(DigestAlgorithm::SHA256, pKey, *sig) >>
boolSink(result);
BOOST_CHECK_EQUAL(result, true);
@@ -163,12 +163,12 @@
bufferSource(DATA) >> signerFilter(DigestAlgorithm::SHA256, *sKey) >> streamSink(os);
auto sig = os.buf();
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, *sKey, sig->data(), sig->size()), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::NONE, *sKey, *sig), Error);
#if OPENSSL_VERSION_NUMBER < 0x30000000L // FIXME #5154
bool result = false;
bufferSource(DATA) >>
- verifierFilter(DigestAlgorithm::SHA256, *sKey, sig->data(), sig->size()) >>
+ verifierFilter(DigestAlgorithm::SHA256, *sKey, *sig) >>
boolSink(result);
BOOST_CHECK_EQUAL(result, true);
@@ -178,9 +178,9 @@
BOOST_AUTO_TEST_CASE(InvalidKey)
{
PublicKey pubKey;
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, pubKey, nullptr, 0), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, pubKey, {}), Error);
PrivateKey privKey;
- BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, privKey, nullptr, 0), Error);
+ BOOST_CHECK_THROW(VerifierFilter(DigestAlgorithm::SHA256, privKey, {}), Error);
}
BOOST_AUTO_TEST_SUITE_END() // TestVerifierFilter