Add Noise AESGCM P-256 SHA256 implementation of KNpsk0 and NKpsk0 71/305871/5 accepted/tizen/unified/20240220.115630 accepted/tizen/unified/20240220.144633 accepted/tizen/unified/toolchain/20240311.064835 accepted/tizen/unified/x/20240221.033407
authorKrzysztof Malysa <k.malysa@samsung.com>
Mon, 12 Feb 2024 10:12:42 +0000 (11:12 +0100)
committerKrzysztof Malysa <k.malysa@samsung.com>
Mon, 19 Feb 2024 10:30:51 +0000 (11:30 +0100)
Change-Id: Ie2f6d3eebc105b84d157b6accb3347b1b5177bb8

srcs/CMakeLists.txt
srcs/crypto/noise/noise.cpp [new file with mode: 0644]
srcs/crypto/noise/noise.h [new file with mode: 0644]
tests/CMakeLists.txt
tests/crypto/noise/noise_unittest.cpp [new file with mode: 0644]

index ecafd872ae2c0fd9c00ba86b8456e5acbd1ea6e8..ec80221a48c556fe493c761685910d556cea2781 100644 (file)
@@ -74,6 +74,7 @@ SET(WEBAUTHN_BLE_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/encryptor.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/ec_key.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/ecdh.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/crypto/noise/noise.cpp
 )
 SET(WEBAUTHN_BLE_SOURCES ${WEBAUTHN_BLE_SOURCES} PARENT_SCOPE)
 
diff --git a/srcs/crypto/noise/noise.cpp b/srcs/crypto/noise/noise.cpp
new file mode 100644 (file)
index 0000000..02d1f6a
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ *  Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include "crypto/encryptor.h"
+#include "crypto/hkdf.h"
+#include "crypto/noise/noise.h"
+#include "crypto/openssl_error.h"
+#include "crypto/sha2.h"
+#include "log/log.h"
+
+#include <array>
+#include <cassert>
+#include <cstring>
+#include <endian.h>
+#include <stdexcept>
+
+namespace {
+
+constexpr size_t KEY_AND_HASH_LEN = 32;
+
+CryptoBuffer NonceToInitialVector(uint64_t nonce) noexcept
+{
+    CryptoBuffer res(12);
+    std::memset(res.data(), 0, 4);
+    nonce = htobe64(nonce);
+    std::memcpy(res.data() + 4, &nonce, sizeof(nonce));
+    return res;
+}
+
+} // namespace
+
+namespace Crypto::Noise {
+
+SymmetricState::SymmetricState(HandshakeKind handshakeKind)
+{
+    m_chainingKey.resize(KEY_AND_HASH_LEN);
+    switch (handshakeKind) {
+    case HandshakeKind::KNpsk0_P256_AESGCM_SHA256: {
+        constexpr char protocolName[] = "Noise_KNpsk0_P256_AESGCM_SHA256";
+        static_assert(sizeof(protocolName) == KEY_AND_HASH_LEN);
+        std::memcpy(m_chainingKey.data(), protocolName, sizeof(protocolName));
+    } break;
+    case HandshakeKind::NKpsk0_P256_AESGCM_SHA256: {
+        constexpr char protocolName[] = "Noise_NKpsk0_P256_AESGCM_SHA256";
+        static_assert(sizeof(protocolName) == KEY_AND_HASH_LEN);
+        std::memcpy(m_chainingKey.data(), protocolName, sizeof(protocolName));
+    } break;
+    default: throw std::runtime_error("invalid noise handshakeKind");
+    }
+
+    m_hash = m_chainingKey;
+}
+
+void SymmetricState::MixHash(const CryptoBuffer &data)
+{
+    CryptoBuffer in = m_hash;
+    in.insert(in.end(), data.begin(), data.end());
+    m_hash = Sha256Hash(in);
+}
+
+void SymmetricState::MixKey(const CryptoBuffer &data)
+{
+    auto derived = HkdfSha256(data, m_chainingKey, {}, KEY_AND_HASH_LEN * 2);
+
+    assert(m_chainingKey.size() == KEY_AND_HASH_LEN);
+    std::memcpy(m_chainingKey.data(), derived.data(), KEY_AND_HASH_LEN);
+
+    m_symmetricState.InitializeKey(derived.data() + KEY_AND_HASH_LEN, KEY_AND_HASH_LEN);
+}
+
+void SymmetricState::MixKeyAndHash(const CryptoBuffer &data)
+{
+    auto derived = HkdfSha256(data, m_chainingKey, {}, KEY_AND_HASH_LEN * 3);
+
+    assert(m_chainingKey.size() == KEY_AND_HASH_LEN);
+    std::memcpy(m_chainingKey.data(), derived.data(), KEY_AND_HASH_LEN);
+
+    MixHash(CryptoBuffer{derived.data() + KEY_AND_HASH_LEN, derived.data() + KEY_AND_HASH_LEN * 2});
+
+    m_symmetricState.InitializeKey(derived.data() + KEY_AND_HASH_LEN * 2, KEY_AND_HASH_LEN);
+}
+
+CryptoBuffer SymmetricState::EncryptAndHash(const CryptoBuffer &plaintext)
+{
+    auto ciphertext = m_symmetricState.EncryptWithAd(plaintext, m_hash);
+    MixHash(ciphertext);
+    return ciphertext;
+}
+
+CryptoBuffer SymmetricState::DecryptAndHash(const CryptoBuffer &ciphertext)
+{
+    auto plaintext = m_symmetricState.DecryptWithAd(ciphertext, m_hash);
+    MixHash(ciphertext);
+    return plaintext;
+}
+
+CryptoBuffer SymmetricState::GetHandshakeHash() const { return m_hash; }
+
+SymmetricState::CipherState::CipherState(CryptoBuffer key) noexcept : m_key(std::move(key)) {}
+
+CryptoBuffer SymmetricState::CipherState::EncryptWithAd(const CryptoBuffer &plaintext,
+                                                        const CryptoBuffer &ad)
+{
+    assert(m_key.size() == KEY_AND_HASH_LEN);
+    auto ciphertext = EncryptAes256GCM(m_key, NonceToInitialVector(m_nonce), ad, plaintext);
+    ++m_nonce;
+    return ciphertext;
+}
+
+CryptoBuffer SymmetricState::CipherState::DecryptWithAd(const CryptoBuffer &ciphertext,
+                                                        const CryptoBuffer &ad)
+{
+    assert(m_key.size() == KEY_AND_HASH_LEN);
+    auto plaintext = DecryptAes256GCM(m_key, NonceToInitialVector(m_nonce), ad, ciphertext);
+    ++m_nonce;
+    return plaintext;
+}
+
+void SymmetricState::CipherState::InitializeKey(const uint8_t *key, size_t len)
+{
+    assert(len == KEY_AND_HASH_LEN);
+    m_key.assign(key, key + len);
+    m_nonce = 0;
+}
+
+SymmetricState::SplitRes SymmetricState::Split() const
+{
+    auto derived = HkdfSha256({}, m_chainingKey, {}, KEY_AND_HASH_LEN * 2);
+    return SplitRes{
+        CipherState{CryptoBuffer{derived.data(), derived.data() + KEY_AND_HASH_LEN}},
+        CipherState{
+            CryptoBuffer{derived.data() + KEY_AND_HASH_LEN, derived.data() + KEY_AND_HASH_LEN * 2}},
+    };
+}
+
+} // namespace Crypto::Noise
diff --git a/srcs/crypto/noise/noise.h b/srcs/crypto/noise/noise.h
new file mode 100644 (file)
index 0000000..9114e26
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ *  Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#pragma once
+
+#include "crypto/common.h"
+
+#include <cstddef>
+#include <cstdint>
+
+namespace Crypto::Noise {
+
+class SymmetricState {
+public:
+    enum HandshakeKind {
+        KNpsk0_P256_AESGCM_SHA256,
+        NKpsk0_P256_AESGCM_SHA256,
+    };
+
+    explicit SymmetricState(HandshakeKind handshakeKind);
+
+    void MixHash(const CryptoBuffer &data);
+
+    void MixKey(const CryptoBuffer &data);
+
+    void MixKeyAndHash(const CryptoBuffer &data);
+
+    CryptoBuffer EncryptAndHash(const CryptoBuffer &plaintext);
+
+    CryptoBuffer DecryptAndHash(const CryptoBuffer &ciphertext);
+
+    CryptoBuffer GetHandshakeHash() const;
+
+    class CipherState {
+    public:
+        explicit CipherState(CryptoBuffer key) noexcept;
+
+        CryptoBuffer EncryptWithAd(const CryptoBuffer &plaintext, const CryptoBuffer &ad);
+
+        CryptoBuffer DecryptWithAd(const CryptoBuffer &ciphertext, const CryptoBuffer &ad);
+
+        const CryptoBuffer &Key() const { return m_key; }
+
+    private:
+        friend class SymmetricState;
+
+        void InitializeKey(const uint8_t *key, size_t len);
+
+        CryptoBuffer m_key;
+        uint64_t m_nonce = 0;
+    };
+
+    struct SplitRes {
+        CipherState first;
+        CipherState second;
+    };
+
+    SplitRes Split() const;
+
+private:
+    CryptoBuffer m_chainingKey;
+    CryptoBuffer m_hash;
+
+    CipherState m_symmetricState{CryptoBuffer{}};
+};
+
+} // namespace Crypto::Noise
index e67f556acc2771994081c814e1bb8617d37377cf..86d469a3ae0583dcc571cf4c6f17c5b297896524 100644 (file)
@@ -43,6 +43,7 @@ SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/encryptor_unittest.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/ec_key_unittest.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/ecdh_unittest.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/crypto/noise/noise_unittest.cpp
 )
 
 ADD_EXECUTABLE(${TARGET_WEBAUTHN_BLE_UNIT_TESTS} ${UNIT_TESTS_SOURCES})
diff --git a/tests/crypto/noise/noise_unittest.cpp b/tests/crypto/noise/noise_unittest.cpp
new file mode 100644 (file)
index 0000000..f9a18fd
--- /dev/null
@@ -0,0 +1,279 @@
+/*
+ *  Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+#include "crypto/noise/noise.h"
+
+#include <gtest/gtest.h>
+
+using namespace Crypto::Noise;
+
+TEST(NoiseTest, KNpsk0_P256_AESGCM_SHA256_initial_state)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::KNpsk0_P256_AESGCM_SHA256};
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x4e, 0x6f, 0x69, 0x73, 0x65, 0x5f, 0x4b, 0x4e, 0x70, 0x73, 0x6b,
+                            0x30, 0x5f, 0x50, 0x32, 0x35, 0x36, 0x5f, 0x41, 0x45, 0x53, 0x47,
+                            0x43, 0x4d, 0x5f, 0x53, 0x48, 0x41, 0x32, 0x35, 0x36, 0x00}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xa4, 0xdb, 0x02, 0x9f, 0x61, 0x2c, 0x32, 0x49, 0x75, 0x2f, 0x90,
+                            0xb9, 0x2d, 0xee, 0x29, 0xce, 0x0e, 0xa6, 0xe4, 0x97, 0xbd, 0xf9,
+                            0x84, 0xeb, 0x3e, 0x77, 0x35, 0x38, 0x9d, 0x44, 0xe8, 0xdc}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0x7f, 0x5d, 0x64, 0xf2, 0x95, 0x89, 0xd6, 0x1b, 0x4e, 0xbf, 0x57,
+                            0x74, 0x7b, 0x70, 0x87, 0x0f, 0x20, 0x9a, 0xb8, 0xd4, 0x83, 0x36,
+                            0x5f, 0xde, 0x3f, 0x46, 0x25, 0x31, 0xbd, 0x14, 0x26, 0x90}));
+}
+
+TEST(NoiseTest, NKpsk0_P256_AESGCM_SHA256_initial_state)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::NKpsk0_P256_AESGCM_SHA256};
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x4e, 0x6f, 0x69, 0x73, 0x65, 0x5f, 0x4e, 0x4b, 0x70, 0x73, 0x6b,
+                            0x30, 0x5f, 0x50, 0x32, 0x35, 0x36, 0x5f, 0x41, 0x45, 0x53, 0x47,
+                            0x43, 0x4d, 0x5f, 0x53, 0x48, 0x41, 0x32, 0x35, 0x36, 0x00}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xd8, 0xf4, 0x83, 0x2e, 0x80, 0x4c, 0x5e, 0x6b, 0x5b, 0x5f, 0xd9,
+                            0xd7, 0x70, 0x88, 0x78, 0xef, 0xbb, 0x72, 0xfb, 0x0d, 0x76, 0x79,
+                            0xa1, 0xf4, 0x53, 0x8d, 0xdd, 0x21, 0x95, 0xde, 0x4d, 0x22}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xb8, 0x50, 0x2d, 0xec, 0x3d, 0xa3, 0x80, 0xdd, 0x4f, 0xf9, 0x08,
+                            0x5d, 0xc3, 0x92, 0x7f, 0x82, 0xaa, 0xed, 0xf5, 0x22, 0xb8, 0x33,
+                            0xfd, 0x5e, 0x60, 0x6b, 0x2f, 0xe2, 0x6e, 0xfc, 0x18, 0xda}));
+}
+
+TEST(NoiseTest, KNpsk0_P256_AESGCM_SHA256_MixHash)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::KNpsk0_P256_AESGCM_SHA256};
+    noise.MixHash({'a', 'b', 'c'});
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0xe0, 0x92, 0xb5, 0x62, 0x6c, 0x14, 0xf8, 0x3f, 0x8d, 0x28, 0x9f,
+                            0x2c, 0x37, 0x61, 0xba, 0x15, 0xf9, 0x68, 0xb0, 0x7e, 0x40, 0x80,
+                            0x51, 0x9d, 0x76, 0x3d, 0xc8, 0xb1, 0x65, 0xf4, 0xbb, 0x20}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xa4, 0xdb, 0x02, 0x9f, 0x61, 0x2c, 0x32, 0x49, 0x75, 0x2f, 0x90,
+                            0xb9, 0x2d, 0xee, 0x29, 0xce, 0x0e, 0xa6, 0xe4, 0x97, 0xbd, 0xf9,
+                            0x84, 0xeb, 0x3e, 0x77, 0x35, 0x38, 0x9d, 0x44, 0xe8, 0xdc}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0x7f, 0x5d, 0x64, 0xf2, 0x95, 0x89, 0xd6, 0x1b, 0x4e, 0xbf, 0x57,
+                            0x74, 0x7b, 0x70, 0x87, 0x0f, 0x20, 0x9a, 0xb8, 0xd4, 0x83, 0x36,
+                            0x5f, 0xde, 0x3f, 0x46, 0x25, 0x31, 0xbd, 0x14, 0x26, 0x90}));
+}
+
+TEST(NoiseTest, NKpsk0_P256_AESGCM_SHA256_MixHash)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::NKpsk0_P256_AESGCM_SHA256};
+    noise.MixHash({'a', 'b', 'c'});
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0xc3, 0xec, 0x18, 0xe6, 0x5f, 0x80, 0xcc, 0xe1, 0x43, 0x86, 0xab,
+                            0x76, 0x5d, 0xbc, 0x2f, 0xfc, 0x82, 0xc5, 0x94, 0x68, 0x9a, 0x45,
+                            0xe3, 0xb6, 0xc0, 0xa7, 0x8d, 0x8d, 0x24, 0x14, 0x52, 0x59}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xd8, 0xf4, 0x83, 0x2e, 0x80, 0x4c, 0x5e, 0x6b, 0x5b, 0x5f, 0xd9,
+                            0xd7, 0x70, 0x88, 0x78, 0xef, 0xbb, 0x72, 0xfb, 0x0d, 0x76, 0x79,
+                            0xa1, 0xf4, 0x53, 0x8d, 0xdd, 0x21, 0x95, 0xde, 0x4d, 0x22}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xb8, 0x50, 0x2d, 0xec, 0x3d, 0xa3, 0x80, 0xdd, 0x4f, 0xf9, 0x08,
+                            0x5d, 0xc3, 0x92, 0x7f, 0x82, 0xaa, 0xed, 0xf5, 0x22, 0xb8, 0x33,
+                            0xfd, 0x5e, 0x60, 0x6b, 0x2f, 0xe2, 0x6e, 0xfc, 0x18, 0xda}));
+}
+
+TEST(NoiseTest, KNpsk0_P256_AESGCM_SHA256_MixKey)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::KNpsk0_P256_AESGCM_SHA256};
+    noise.MixKey({'a', 'b', 'c'});
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x4e, 0x6f, 0x69, 0x73, 0x65, 0x5f, 0x4b, 0x4e, 0x70, 0x73, 0x6b,
+                            0x30, 0x5f, 0x50, 0x32, 0x35, 0x36, 0x5f, 0x41, 0x45, 0x53, 0x47,
+                            0x43, 0x4d, 0x5f, 0x53, 0x48, 0x41, 0x32, 0x35, 0x36, 0x00}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xfa, 0xc0, 0x19, 0x0d, 0x02, 0xa2, 0xd8, 0xa3, 0x4b, 0xb1, 0x15,
+                            0xae, 0xd1, 0x52, 0x38, 0x9d, 0xfd, 0x99, 0x35, 0x29, 0x89, 0x8d,
+                            0xf0, 0x89, 0xc7, 0xa5, 0x9e, 0x98, 0x1d, 0xad, 0x4a, 0xf9}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xec, 0x2a, 0x88, 0xf4, 0xb0, 0x60, 0x4c, 0x69, 0x12, 0xd4, 0x99,
+                            0x16, 0xd7, 0xae, 0x0b, 0x9d, 0x2b, 0x02, 0xf5, 0x3f, 0x10, 0x32,
+                            0xea, 0x72, 0x92, 0x9c, 0x19, 0xbf, 0x22, 0xe4, 0xe3, 0xd8}));
+}
+
+TEST(NoiseTest, NKpsk0_P256_AESGCM_SHA256_MixKey)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::NKpsk0_P256_AESGCM_SHA256};
+    noise.MixKey({'a', 'b', 'c'});
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x4e, 0x6f, 0x69, 0x73, 0x65, 0x5f, 0x4e, 0x4b, 0x70, 0x73, 0x6b,
+                            0x30, 0x5f, 0x50, 0x32, 0x35, 0x36, 0x5f, 0x41, 0x45, 0x53, 0x47,
+                            0x43, 0x4d, 0x5f, 0x53, 0x48, 0x41, 0x32, 0x35, 0x36, 0x00}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0x0c, 0x7e, 0x21, 0xf2, 0x77, 0x6a, 0x8d, 0x81, 0x35, 0x72, 0xbc,
+                            0x2e, 0x64, 0xf6, 0xa4, 0xbf, 0x41, 0xad, 0x79, 0xca, 0x6c, 0xa9,
+                            0xc9, 0x38, 0x44, 0x75, 0xf8, 0xa0, 0x34, 0xa0, 0x3e, 0xc7}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xd6, 0x1a, 0xc7, 0x4b, 0xfc, 0x82, 0x0a, 0x76, 0x8f, 0x1e, 0x98,
+                            0x3a, 0xf6, 0xb3, 0xd2, 0xe9, 0x58, 0x21, 0xea, 0xbf, 0xef, 0x96,
+                            0xd2, 0xf7, 0x51, 0xfb, 0xce, 0xc3, 0xba, 0xa7, 0x88, 0x98}));
+}
+
+TEST(NoiseTest, KNpsk0_P256_AESGCM_SHA256_MixKeyAndHash)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::KNpsk0_P256_AESGCM_SHA256};
+    noise.MixKeyAndHash({'a', 'b', 'c'});
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x3a, 0xf7, 0x39, 0xd6, 0xa2, 0xf4, 0x87, 0x05, 0x3c, 0xf7, 0x20,
+                            0x30, 0x91, 0x02, 0x85, 0xdf, 0x70, 0x33, 0x67, 0x53, 0xcf, 0xf1,
+                            0x22, 0xfc, 0xc6, 0x99, 0xa2, 0x9a, 0x77, 0x36, 0x11, 0xab}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xfa, 0xc0, 0x19, 0x0d, 0x02, 0xa2, 0xd8, 0xa3, 0x4b, 0xb1, 0x15,
+                            0xae, 0xd1, 0x52, 0x38, 0x9d, 0xfd, 0x99, 0x35, 0x29, 0x89, 0x8d,
+                            0xf0, 0x89, 0xc7, 0xa5, 0x9e, 0x98, 0x1d, 0xad, 0x4a, 0xf9}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xec, 0x2a, 0x88, 0xf4, 0xb0, 0x60, 0x4c, 0x69, 0x12, 0xd4, 0x99,
+                            0x16, 0xd7, 0xae, 0x0b, 0x9d, 0x2b, 0x02, 0xf5, 0x3f, 0x10, 0x32,
+                            0xea, 0x72, 0x92, 0x9c, 0x19, 0xbf, 0x22, 0xe4, 0xe3, 0xd8}));
+}
+
+TEST(NoiseTest, NKpsk0_P256_AESGCM_SHA256_MixKeyAndHash)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::NKpsk0_P256_AESGCM_SHA256};
+    noise.MixKeyAndHash({'a', 'b', 'c'});
+    // Values come from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x3f, 0x46, 0xd8, 0x5d, 0x19, 0x65, 0x9a, 0x23, 0x62, 0x16, 0x48,
+                            0xde, 0x8d, 0x47, 0x38, 0x06, 0x8b, 0x54, 0x6a, 0x7b, 0x99, 0x30,
+                            0x52, 0x1e, 0x4b, 0xf0, 0x0d, 0x6d, 0xa0, 0xc3, 0xe8, 0xda}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0x0c, 0x7e, 0x21, 0xf2, 0x77, 0x6a, 0x8d, 0x81, 0x35, 0x72, 0xbc,
+                            0x2e, 0x64, 0xf6, 0xa4, 0xbf, 0x41, 0xad, 0x79, 0xca, 0x6c, 0xa9,
+                            0xc9, 0x38, 0x44, 0x75, 0xf8, 0xa0, 0x34, 0xa0, 0x3e, 0xc7}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xd6, 0x1a, 0xc7, 0x4b, 0xfc, 0x82, 0x0a, 0x76, 0x8f, 0x1e, 0x98,
+                            0x3a, 0xf6, 0xb3, 0xd2, 0xe9, 0x58, 0x21, 0xea, 0xbf, 0xef, 0x96,
+                            0xd2, 0xf7, 0x51, 0xfb, 0xce, 0xc3, 0xba, 0xa7, 0x88, 0x98}));
+}
+
+TEST(NoiseTest, KNpsk0_P256_AESGCM_SHA256_Encrypt_Decrypt)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::KNpsk0_P256_AESGCM_SHA256};
+    noise.MixKeyAndHash({'a', 'b', 'c'});
+
+    CryptoBuffer msg1 = {'x', 'y', 'z'};
+    CryptoBuffer msg2(77, 0x0b);
+
+    auto noise2 = noise;
+    auto ciphertext1 = noise.EncryptAndHash(msg1);
+    auto plaintext1 = noise2.DecryptAndHash(ciphertext1);
+    EXPECT_EQ(msg1, plaintext1);
+    // clang-format off
+    // Value comes from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(ciphertext1,
+              (CryptoBuffer{
+                  0x73, 0xa5, 0x70, 0x0a, 0x9f, 0x2b, 0x63, 0x8c, 0xa9, 0x68,
+                  0xc9, 0xe7, 0x05, 0xd9, 0x9a, 0xca, 0xf2, 0x2b, 0xf1}));
+    // clang-format on
+
+    auto ciphertext2 = noise.EncryptAndHash(msg2);
+    auto plaintext2 = noise2.DecryptAndHash(ciphertext2);
+    EXPECT_EQ(msg2, plaintext2);
+    // Values come from this implementation after verifying the Noise handshake works with
+    // webauthn.io
+    EXPECT_EQ(ciphertext2,
+              (CryptoBuffer{0xac, 0xfa, 0xcf, 0x33, 0x04, 0x6a, 0x9b, 0xe4, 0x35, 0x5e, 0xc3, 0xee,
+                            0x34, 0xcb, 0xf5, 0x92, 0x5e, 0x08, 0x08, 0x51, 0x73, 0xad, 0x0f, 0xd3,
+                            0xe9, 0x19, 0x52, 0x2a, 0x46, 0x41, 0x5c, 0x81, 0x7a, 0x89, 0x50, 0x98,
+                            0xc3, 0xa8, 0x59, 0x9e, 0x43, 0x75, 0x4a, 0x43, 0x33, 0x4a, 0xb3, 0x59,
+                            0x5f, 0x19, 0xa4, 0xe4, 0x34, 0x70, 0xf9, 0x34, 0x11, 0x44, 0x03, 0xa4,
+                            0x0a, 0x1e, 0x9a, 0x3a, 0xb2, 0xf4, 0x0b, 0xef, 0x6d, 0xbe, 0x64, 0x0b,
+                            0xba, 0x75, 0x95, 0xe2, 0x0c, 0x59, 0xe7, 0x44, 0x6f, 0x36, 0x25, 0x01,
+                            0x3b, 0xa3, 0xf9, 0xf0, 0x8e, 0x01, 0xce, 0x5b, 0x25}));
+
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x87, 0xc6, 0x55, 0x89, 0x86, 0x7a, 0xbe, 0x17, 0x88, 0xf1, 0x2b,
+                            0xb6, 0xf0, 0x0b, 0x6e, 0xd1, 0x8a, 0xb8, 0x68, 0xee, 0x57, 0x67,
+                            0x58, 0xfa, 0xbf, 0xb2, 0x65, 0x17, 0xed, 0xd3, 0x3b, 0x0c}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0xfa, 0xc0, 0x19, 0x0d, 0x02, 0xa2, 0xd8, 0xa3, 0x4b, 0xb1, 0x15,
+                            0xae, 0xd1, 0x52, 0x38, 0x9d, 0xfd, 0x99, 0x35, 0x29, 0x89, 0x8d,
+                            0xf0, 0x89, 0xc7, 0xa5, 0x9e, 0x98, 0x1d, 0xad, 0x4a, 0xf9}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xec, 0x2a, 0x88, 0xf4, 0xb0, 0x60, 0x4c, 0x69, 0x12, 0xd4, 0x99,
+                            0x16, 0xd7, 0xae, 0x0b, 0x9d, 0x2b, 0x02, 0xf5, 0x3f, 0x10, 0x32,
+                            0xea, 0x72, 0x92, 0x9c, 0x19, 0xbf, 0x22, 0xe4, 0xe3, 0xd8}));
+}
+
+TEST(NoiseTest, NKpsk0_P256_AESGCM_SHA256_Encrypt_Decrypt)
+{
+    SymmetricState noise{SymmetricState::HandshakeKind::NKpsk0_P256_AESGCM_SHA256};
+    noise.MixKeyAndHash({'a', 'b', 'c'});
+
+    CryptoBuffer msg1 = {'x', 'y', 'z'};
+    CryptoBuffer msg2(77, 0x0b);
+
+    auto noise2 = noise;
+    auto ciphertext1 = noise.EncryptAndHash(msg1);
+    auto plaintext1 = noise2.DecryptAndHash(ciphertext1);
+    EXPECT_EQ(msg1, plaintext1);
+    // clang-format off
+    // Value comes from an alternative implemantation of Dong Sun Lee
+    EXPECT_EQ(ciphertext1,
+              (CryptoBuffer{
+                  0x6e, 0x5f, 0x52, 0xf4, 0xf8, 0x53, 0xa3, 0x6c, 0xb0, 0x27,
+                  0x43, 0xa9, 0x4c, 0x66, 0xb0, 0x64, 0xb2, 0xd2, 0x1b}));
+    // clang-format on
+
+    auto ciphertext2 = noise.EncryptAndHash(msg2);
+    auto plaintext2 = noise2.DecryptAndHash(ciphertext2);
+    EXPECT_EQ(msg2, plaintext2);
+    // Values come from this implementation after verifying the Noise handshake works with
+    // webauthn.io
+    EXPECT_EQ(ciphertext2,
+              (CryptoBuffer{0x19, 0x87, 0x80, 0x66, 0x99, 0xa6, 0x07, 0x28, 0x5d, 0xf0, 0x7e, 0x68,
+                            0x91, 0xa8, 0x16, 0xc1, 0xce, 0x79, 0x52, 0xfd, 0x5e, 0xe5, 0x57, 0xb0,
+                            0xca, 0xf2, 0xeb, 0xd3, 0x12, 0x14, 0x92, 0x2f, 0x0f, 0x05, 0x8b, 0x4f,
+                            0xf4, 0x93, 0x1d, 0x08, 0x16, 0xee, 0x82, 0x87, 0x77, 0xcf, 0x30, 0x6d,
+                            0x82, 0x3e, 0xe4, 0x3b, 0x85, 0xf4, 0xc3, 0xab, 0xcb, 0x8f, 0xcd, 0x79,
+                            0x4c, 0x04, 0xd0, 0x4d, 0x95, 0x1a, 0xf7, 0xfc, 0xa0, 0xd7, 0x77, 0x69,
+                            0x87, 0xa6, 0xa8, 0x59, 0x38, 0xcb, 0x29, 0x6d, 0xbc, 0x65, 0x6a, 0xfb,
+                            0xae, 0x99, 0x6a, 0x57, 0xb9, 0x0a, 0x59, 0x1d, 0xaf}));
+
+    EXPECT_EQ(noise.GetHandshakeHash(),
+              (CryptoBuffer{0x07, 0x84, 0xd9, 0x43, 0x5d, 0xfd, 0xd4, 0xb6, 0x98, 0x28, 0xf7,
+                            0xc1, 0x92, 0xfd, 0xff, 0x20, 0xe3, 0x93, 0x80, 0xba, 0xc6, 0xe5,
+                            0x6a, 0x7c, 0xfa, 0x8e, 0x7a, 0x5c, 0x6a, 0x5e, 0x97, 0x04}));
+    auto splitRes = noise.Split();
+    EXPECT_EQ(splitRes.first.Key(),
+              (CryptoBuffer{0x0c, 0x7e, 0x21, 0xf2, 0x77, 0x6a, 0x8d, 0x81, 0x35, 0x72, 0xbc,
+                            0x2e, 0x64, 0xf6, 0xa4, 0xbf, 0x41, 0xad, 0x79, 0xca, 0x6c, 0xa9,
+                            0xc9, 0x38, 0x44, 0x75, 0xf8, 0xa0, 0x34, 0xa0, 0x3e, 0xc7}));
+    EXPECT_EQ(splitRes.second.Key(),
+              (CryptoBuffer{0xd6, 0x1a, 0xc7, 0x4b, 0xfc, 0x82, 0x0a, 0x76, 0x8f, 0x1e, 0x98,
+                            0x3a, 0xf6, 0xb3, 0xd2, 0xe9, 0x58, 0x21, 0xea, 0xbf, 0xef, 0x96,
+                            0xd2, 0xf7, 0x51, 0xfb, 0xce, 0xc3, 0xba, 0xa7, 0x88, 0x98}));
+}