security: add support for BLAKE2 hash function

Requires openssl >= 1.1.0

Change-Id: I680c948d23561cee8cb62eb8bf757e55bf70abca
diff --git a/src/security/detail/openssl-helper.cpp b/src/security/detail/openssl-helper.cpp
index 83e0ec9..92ec501 100644
--- a/src/security/detail/openssl-helper.cpp
+++ b/src/security/detail/openssl-helper.cpp
@@ -37,6 +37,12 @@
     return EVP_sha384();
   case DigestAlgorithm::SHA512:
     return EVP_sha512();
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL && !defined(OPENSSL_NO_BLAKE2)
+  case DigestAlgorithm::BLAKE2B_512:
+    return EVP_blake2b512();
+  case DigestAlgorithm::BLAKE2S_256:
+    return EVP_blake2s256();
+#endif
   default:
     return nullptr;
   }
@@ -50,7 +56,7 @@
     EVP_PKEY_type(key->type);
 #else
     EVP_PKEY_base_id(key);
-#endif // OPENSSL_VERSION_NUMBER < 0x1010000fL
+#endif
 }
 
 EvpMdCtx::EvpMdCtx()
diff --git a/src/security/security-common.cpp b/src/security/security-common.cpp
index 5cfe551..5e5fa31 100644
--- a/src/security/security-common.cpp
+++ b/src/security/security-common.cpp
@@ -85,6 +85,10 @@
       return os << "SHA384";
     case DigestAlgorithm::SHA512:
       return os << "SHA512";
+    case DigestAlgorithm::BLAKE2B_512:
+      return os << "BLAKE2b-512";
+    case DigestAlgorithm::BLAKE2S_256:
+      return os << "BLAKE2s-256";
   }
   return os << static_cast<int>(algorithm);
 }
diff --git a/src/security/security-common.hpp b/src/security/security-common.hpp
index 5df2c13..9685dc3 100644
--- a/src/security/security-common.hpp
+++ b/src/security/security-common.hpp
@@ -108,6 +108,8 @@
   SHA256 = 1,
   SHA384 = 3,
   SHA512 = 4,
+  BLAKE2B_512 = 10,
+  BLAKE2S_256 = 11,
 };
 
 std::ostream&
diff --git a/tests/unit-tests/security/transform/digest-filter.t.cpp b/tests/unit-tests/security/transform/digest-filter.t.cpp
index a3f0314..1a509eb 100644
--- a/tests/unit-tests/security/transform/digest-filter.t.cpp
+++ b/tests/unit-tests/security/transform/digest-filter.t.cpp
@@ -22,6 +22,7 @@
 #include "security/transform/digest-filter.hpp"
 
 #include "encoding/buffer-stream.hpp"
+#include "security/detail/openssl.hpp"
 #include "security/transform/buffer-source.hpp"
 #include "security/transform/step-source.hpp"
 #include "security/transform/stream-sink.hpp"
@@ -62,16 +63,14 @@
   0xf7, 0xfa, 0x7b, 0x2d, 0xd6, 0x8c, 0xd5, 0xa3
 };
 
-BOOST_AUTO_TEST_CASE(Basic)
+BOOST_AUTO_TEST_CASE(BufferInput)
 {
   OBufferStream os;
   bufferSource(in, sizeof(in)) >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
-
-  ConstBufferPtr buf = os.buf();
-  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
 }
 
-BOOST_AUTO_TEST_CASE(StepByStep)
+BOOST_AUTO_TEST_CASE(StepInput)
 {
   StepSource source;
   OBufferStream os;
@@ -83,34 +82,87 @@
   source.write(in + 38, 26);
   source.write(in + 64, 64);
   source.end();
-
-  ConstBufferPtr buf = os.buf();
-  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
 }
 
-BOOST_AUTO_TEST_CASE(EmptyInput)
-{
-  const uint8_t out[] = {
-    0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
-    0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
-    0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
-    0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
-  };
-
-  OBufferStream os;
-  StepSource source;
-  source >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
-  source.end();
-
-  ConstBufferPtr buf = os.buf();
-  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), buf->begin(), buf->end());
-}
-
-BOOST_AUTO_TEST_CASE(InvalidAlgorithm)
+BOOST_AUTO_TEST_CASE(AlgorithmNone)
 {
   BOOST_CHECK_THROW(DigestFilter{DigestAlgorithm::NONE}, Error);
 }
 
+BOOST_AUTO_TEST_CASE(AlgorithmSha224)
+{
+  const uint8_t out[] = {
+    0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
+    0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, 0x2f,
+  };
+  OBufferStream os;
+  bufferSource("") >> digestFilter(DigestAlgorithm::SHA224) >> streamSink(os);
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
+}
+
+BOOST_AUTO_TEST_CASE(AlgorithmSha256)
+{
+  const uint8_t out[] = {
+    0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
+    0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
+  };
+  OBufferStream os;
+  bufferSource("") >> digestFilter(DigestAlgorithm::SHA256) >> streamSink(os);
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
+}
+
+BOOST_AUTO_TEST_CASE(AlgorithmSha384)
+{
+  const uint8_t out[] = {
+    0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1, 0xe3, 0x6a,
+    0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda,
+    0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b,
+  };
+  OBufferStream os;
+  bufferSource("") >> digestFilter(DigestAlgorithm::SHA384) >> streamSink(os);
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
+}
+
+BOOST_AUTO_TEST_CASE(AlgorithmSha512)
+{
+  const uint8_t out[] = {
+    0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
+    0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
+    0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
+    0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e,
+  };
+  OBufferStream os;
+  bufferSource("") >> digestFilter(DigestAlgorithm::SHA512) >> streamSink(os);
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
+}
+
+#if OPENSSL_VERSION_NUMBER >= 0x1010000fL && !defined(OPENSSL_NO_BLAKE2)
+BOOST_AUTO_TEST_CASE(AlgorithmBlake2b_512)
+{
+  const uint8_t out[] = {
+    0x78, 0x6a, 0x02, 0xf7, 0x42, 0x01, 0x59, 0x03, 0xc6, 0xc6, 0xfd, 0x85, 0x25, 0x52, 0xd2, 0x72,
+    0x91, 0x2f, 0x47, 0x40, 0xe1, 0x58, 0x47, 0x61, 0x8a, 0x86, 0xe2, 0x17, 0xf7, 0x1f, 0x54, 0x19,
+    0xd2, 0x5e, 0x10, 0x31, 0xaf, 0xee, 0x58, 0x53, 0x13, 0x89, 0x64, 0x44, 0x93, 0x4e, 0xb0, 0x4b,
+    0x90, 0x3a, 0x68, 0x5b, 0x14, 0x48, 0xb7, 0x55, 0xd5, 0x6f, 0x70, 0x1a, 0xfe, 0x9b, 0xe2, 0xce,
+  };
+  OBufferStream os;
+  bufferSource("") >> digestFilter(DigestAlgorithm::BLAKE2B_512) >> streamSink(os);
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
+}
+
+BOOST_AUTO_TEST_CASE(AlgorithmBlake2s_256)
+{
+  const uint8_t out[] = {
+    0x69, 0x21, 0x7a, 0x30, 0x79, 0x90, 0x80, 0x94, 0xe1, 0x11, 0x21, 0xd0, 0x42, 0x35, 0x4a, 0x7c,
+    0x1f, 0x55, 0xb6, 0x48, 0x2c, 0xa1, 0xa5, 0x1e, 0x1b, 0x25, 0x0d, 0xfd, 0x1e, 0xd0, 0xee, 0xf9,
+  };
+  OBufferStream os;
+  bufferSource("") >> digestFilter(DigestAlgorithm::BLAKE2S_256) >> streamSink(os);
+  BOOST_CHECK_EQUAL_COLLECTIONS(out, out + sizeof(out), os.buf()->begin(), os.buf()->end());
+}
+#endif // OPENSSL_VERSION_NUMBER >= 0x1010000fL && !defined(OPENSSL_NO_BLAKE2)
+
 BOOST_AUTO_TEST_SUITE_END() // TestDigestFilter
 BOOST_AUTO_TEST_SUITE_END() // Transform
 BOOST_AUTO_TEST_SUITE_END() // Security
diff --git a/tests/unit-tests/security/transform/step-source.t.cpp b/tests/unit-tests/security/transform/step-source.t.cpp
index 321a3dc..5e9567a 100644
--- a/tests/unit-tests/security/transform/step-source.t.cpp
+++ b/tests/unit-tests/security/transform/step-source.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -24,6 +24,8 @@
 
 #include "boost-test.hpp"
 
+#include <sstream>
+
 namespace ndn {
 namespace security {
 namespace transform {
@@ -35,7 +37,7 @@
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
-  std::string input =
+  const std::string input =
     "0123456701234567012345670123456701234567012345670123456701234567"
     "0123456701234567012345670123456701234567012345670123456701234567"
     "0123456701234567012345670123456701234567012345670123456701234567"
@@ -58,7 +60,7 @@
     "0123456701234567012345670123456701234567012345670123456701234567";
   const uint8_t* buf = reinterpret_cast<const uint8_t*>(input.data());
 
-  std::stringstream os;
+  std::ostringstream os;
   StepSource ss;
   ss >> streamSink(os);
   BOOST_CHECK_EQUAL(ss.write(buf, 320), 320);
@@ -66,9 +68,17 @@
   BOOST_CHECK_EQUAL(ss.write(buf + 640, 320), 320);
   BOOST_CHECK_EQUAL(ss.write(buf + 960, 320), 320);
   ss.end();
-  BOOST_REQUIRE_THROW(ss.write(buf + 960, 320), transform::Error);
-  std::string output = os.str();
-  BOOST_CHECK_EQUAL(input, output);
+  BOOST_CHECK_THROW(ss.write(buf + 960, 320), transform::Error);
+  BOOST_CHECK_EQUAL(os.str(), input);
+}
+
+BOOST_AUTO_TEST_CASE(EmptyInput)
+{
+  std::ostringstream os;
+  StepSource ss;
+  ss >> streamSink(os);
+  ss.end();
+  BOOST_CHECK_EQUAL(os.str(), "");
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestStepSource