Add ECDH key derivation with unit tests 78/305778/11
authorDariusz Michaluk <d.michaluk@samsung.com>
Tue, 6 Feb 2024 15:14:03 +0000 (16:14 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 12 Feb 2024 13:40:44 +0000 (13:40 +0000)
Change-Id: I66bb3a0df70508c5b84517955c9849a762fe6630

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

index 98e934ca7efec6e37bf492a8c3ad1ab9795fd919..ecafd872ae2c0fd9c00ba86b8456e5acbd1ea6e8 100644 (file)
@@ -73,6 +73,7 @@ SET(WEBAUTHN_BLE_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/symmetric_key.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/encryptor.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/ec_key.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/crypto/ecdh.cpp
 )
 SET(WEBAUTHN_BLE_SOURCES ${WEBAUTHN_BLE_SOURCES} PARENT_SCOPE)
 
diff --git a/srcs/crypto/ecdh.cpp b/srcs/crypto/ecdh.cpp
new file mode 100644 (file)
index 0000000..71aae49
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ *  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/ec_key.h"
+#include "crypto/ecdh.h"
+#include "crypto/openssl_error.h"
+#include "log/log.h"
+
+#include <memory>
+#include <openssl/evp.h>
+
+namespace Crypto {
+
+CryptoBuffer deriveECDHSharedSecret(const X9_62_P_256_Key &myPrivateKey,
+                                    const X9_62_P_256_Key &peerPublicKey)
+{
+    CryptoBuffer secret;
+    size_t len = 0;
+
+    auto ctx = std::unique_ptr<EVP_PKEY_CTX, decltype(&EVP_PKEY_CTX_free)>{
+        EVP_PKEY_CTX_new(myPrivateKey.get(), nullptr), &EVP_PKEY_CTX_free};
+    if (!ctx) {
+        LogError("EVP_PKEY_CTX_new() error");
+        throw OpensslError{};
+    }
+    if (1 != EVP_PKEY_derive_init(ctx.get())) {
+        LogError("EVP_PKEY_derive_init() error");
+        throw OpensslError{};
+    }
+    if (1 != EVP_PKEY_derive_set_peer(ctx.get(), peerPublicKey.get())) {
+        LogError("EVP_PKEY_derive_set_peer() error");
+        throw OpensslError{};
+    }
+    if (1 != EVP_PKEY_derive(ctx.get(), nullptr, &len)) {
+        LogError("EVP_PKEY_derive() error");
+        throw OpensslError{};
+    }
+
+    secret.resize(len);
+    if (1 != EVP_PKEY_derive(ctx.get(), secret.data(), &len)) {
+        LogError("EVP_PKEY_derive() error");
+        throw OpensslError{};
+    }
+
+    return secret;
+}
+
+} // namespace Crypto
diff --git a/srcs/crypto/ecdh.h b/srcs/crypto/ecdh.h
new file mode 100644 (file)
index 0000000..dce14d8
--- /dev/null
@@ -0,0 +1,26 @@
+/*
+ *  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/ec_key.h"
+
+namespace Crypto {
+
+CryptoBuffer deriveECDHSharedSecret(const X9_62_P_256_Key &myPrivateKey,
+                                    const X9_62_P_256_Key &peerPublicKey);
+
+} // namespace Crypto
index da1494c48edfb9a975f9cb9772996a4733ecb1da..e67f556acc2771994081c814e1bb8617d37377cf 100644 (file)
@@ -42,6 +42,7 @@ SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/symmetric_key_unittest.cpp
     ${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
 )
 
 ADD_EXECUTABLE(${TARGET_WEBAUTHN_BLE_UNIT_TESTS} ${UNIT_TESTS_SOURCES})
diff --git a/tests/crypto/ecdh_unittest.cpp b/tests/crypto/ecdh_unittest.cpp
new file mode 100644 (file)
index 0000000..4999cdf
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ *  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/ec_key.h"
+#include "crypto/ecdh.h"
+
+#include <gtest/gtest.h>
+
+TEST(ECDHSharedSecret, ECDHSharedSecretTestCase1)
+{
+    auto my = Crypto::X9_62_P_256_Key::Create();
+    auto peer = Crypto::X9_62_P_256_Key::Create();
+
+    EXPECT_EQ(Crypto::deriveECDHSharedSecret(my, peer), Crypto::deriveECDHSharedSecret(peer, my));
+}
+
+TEST(ECDHSharedSecret, ECDHSharedSecretTestCase2)
+{
+    const CryptoBuffer myPrivateKey = {0x5c, 0x37, 0xa7, 0xac, 0x38, 0xc9, 0xa9, 0x37,
+                                       0xfe, 0xab, 0xfb, 0x65, 0xb5, 0xf0, 0xe2, 0xb0,
+                                       0x05, 0xcd, 0xa9, 0x10, 0x37, 0xa5, 0x7b, 0xc7,
+                                       0x3f, 0x0d, 0xcb, 0x11, 0x20, 0x2d, 0x25, 0x96};
+
+    const CryptoBuffer myPublicKey = {
+        0x04, 0x43, 0x02, 0x40, 0x38, 0x2c, 0x7d, 0x4b, 0x47, 0xb9, 0x0e, 0x82, 0x93,
+        0x5c, 0xa6, 0x15, 0x1b, 0xda, 0xdd, 0x1d, 0xfe, 0xc7, 0xea, 0x31, 0x18, 0xb9,
+        0x31, 0x47, 0x23, 0x43, 0x9f, 0x6f, 0x21, 0xb6, 0xb7, 0x47, 0x0e, 0x20, 0xa9,
+        0x8e, 0xa4, 0xd3, 0x20, 0xf5, 0x71, 0x18, 0x63, 0x70, 0xc2, 0x6f, 0x20, 0x80,
+        0x23, 0xed, 0xd5, 0x4f, 0xfb, 0xab, 0x47, 0x02, 0x25, 0x4a, 0x10, 0x02, 0x98};
+
+    const CryptoBuffer peerPrivateKey = {0x2a, 0xc4, 0x02, 0x67, 0x15, 0xfd, 0x0f, 0x0f,
+                                         0xb7, 0xa1, 0x2e, 0x2c, 0x66, 0x3b, 0xf5, 0x1e,
+                                         0x94, 0x64, 0xd0, 0xa3, 0x75, 0x7c, 0x3d, 0x0e,
+                                         0x46, 0xc9, 0xa3, 0xc5, 0x2a, 0xb7, 0xff, 0x55};
+
+    const CryptoBuffer peerPublicKey = {
+        0x04, 0xde, 0x20, 0xbd, 0x31, 0xd8, 0xe7, 0xfa, 0x5e, 0xb3, 0x8b, 0x2d, 0x59,
+        0xfb, 0x49, 0x0e, 0x56, 0xb5, 0xb4, 0x3b, 0xba, 0x0c, 0xcd, 0xa8, 0x9e, 0x70,
+        0xfc, 0xb3, 0xcb, 0xb1, 0xfa, 0x2d, 0x4b, 0x05, 0xf4, 0x8b, 0x00, 0xbc, 0xac,
+        0x05, 0xc1, 0x8e, 0xe4, 0x35, 0xea, 0x5d, 0x35, 0x04, 0x02, 0xcf, 0x0c, 0x7a,
+        0x23, 0x7b, 0x96, 0x34, 0x3b, 0xc2, 0x9a, 0xdb, 0xcb, 0xea, 0x32, 0xd1, 0xc9};
+
+    const CryptoBuffer expectedSecret = {0xf4, 0x54, 0xdc, 0x49, 0xe9, 0x9b, 0x39, 0x73,
+                                         0xaa, 0x90, 0x11, 0x86, 0x41, 0x0a, 0x4a, 0xc1,
+                                         0xad, 0x8e, 0xd1, 0xea, 0x38, 0xdc, 0xa7, 0xe8,
+                                         0x9e, 0xdb, 0xe0, 0xee, 0xed, 0x73, 0x0c, 0xac};
+
+    auto mySecret =
+        Crypto::deriveECDHSharedSecret(Crypto::X9_62_P_256_Key::ImportPrivateKey(myPrivateKey),
+                                       Crypto::X9_62_P_256_Key::ImportPublicKey(peerPublicKey));
+
+    auto peerSecret =
+        Crypto::deriveECDHSharedSecret(Crypto::X9_62_P_256_Key::ImportPrivateKey(peerPrivateKey),
+                                       Crypto::X9_62_P_256_Key::ImportPublicKey(myPublicKey));
+
+    EXPECT_EQ(mySecret, peerSecret);
+    EXPECT_EQ(mySecret, expectedSecret);
+}