Add base64 encoding with tests 37/307837/4
authorKrzysztof Malysa <k.malysa@samsung.com>
Wed, 13 Mar 2024 12:37:50 +0000 (13:37 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 15 Mar 2024 16:29:57 +0000 (17:29 +0100)
Change-Id: I6ff0c241c84a7fdc38a9bc35b1c2c2b10200a2b5

srcs/CMakeLists.txt
srcs/base64.cpp [new file with mode: 0644]
srcs/base64.h [new file with mode: 0644]
srcs/common.h
tests/CMakeLists.txt
tests/base64_tests.cpp [new file with mode: 0644]

index f5c60070508e56141f65f46b8e3571148c4091e2..ba72cfc76a806861cb1df3684f491dc34647f52c 100644 (file)
@@ -79,6 +79,7 @@ SET(WEBAUTHN_BLE_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/websockets.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/handshake.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/encrypted_tunnel.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/base64.cpp
 )
 SET(WEBAUTHN_BLE_SOURCES ${WEBAUTHN_BLE_SOURCES} PARENT_SCOPE)
 
diff --git a/srcs/base64.cpp b/srcs/base64.cpp
new file mode 100644 (file)
index 0000000..3b3caee
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *  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 "base64.h"
+
+#include <cassert>
+#include <openssl/evp.h>
+
+std::string Base64UrlEncode(BufferView buff)
+{
+    size_t len = (buff.size() + 2) / 3 * 4;
+    std::string res(len, '\0');
+    auto outLen =
+        EVP_EncodeBlock(reinterpret_cast<uint8_t *>(res.data()), buff.data(), buff.size());
+    assert(len == static_cast<size_t>(outLen));
+    // Remove padding
+    res.resize((buff.size() * 4 + 2) / 3);
+    return res;
+}
diff --git a/srcs/base64.h b/srcs/base64.h
new file mode 100644 (file)
index 0000000..df2660d
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ *  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 "common.h"
+
+#include <string>
+
+std::string Base64UrlEncode(BufferView buff);
index a54c2ddb2bea6ce75555715620f362cc7b0f846a..35bdfab677cc54fff85337f2364a4c552cc7273e 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <array>
 #include <cstddef>
+#include <string_view>
 #include <utility> // for std::move
 
 constexpr size_t QR_SECRET_LEN = 16;
@@ -35,3 +36,5 @@ public:
 private:
     T m_cleanupFn;
 };
+
+typedef std::basic_string_view<uint8_t> BufferView;
index 97df4b1a5cc9e750aa732b13d67957950510caf8..f97c79b49d6cc424c58cac8eae94dae67c7614e1 100644 (file)
@@ -41,6 +41,7 @@ SET(UNIT_TESTS_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/state_assisted_transaction_tests.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/handshake_tests.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/encrypted_tunnel_tests.cpp
+    ${CMAKE_CURRENT_SOURCE_DIR}/base64_tests.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/hkdf_unittest.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/hmac_unittest.cpp
     ${CMAKE_CURRENT_SOURCE_DIR}/crypto/sha2_unittest.cpp
diff --git a/tests/base64_tests.cpp b/tests/base64_tests.cpp
new file mode 100644 (file)
index 0000000..b3a4460
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ *  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 "base64.h"
+
+#include <gtest/gtest.h>
+
+TEST(base64, Base64UrlEncode)
+{
+    unsigned char data[] = "\x00\x01\x02\x03\x04\x05";
+    EXPECT_EQ(Base64UrlEncode({data, 0}), "");
+    EXPECT_EQ(Base64UrlEncode({data, 1}), "AA");
+    EXPECT_EQ(Base64UrlEncode({data, 2}), "AAE");
+    EXPECT_EQ(Base64UrlEncode({data, 3}), "AAEC");
+    EXPECT_EQ(Base64UrlEncode({data, 4}), "AAECAw");
+    EXPECT_EQ(Base64UrlEncode({data, 5}), "AAECAwQ");
+    EXPECT_EQ(Base64UrlEncode({data, 6}), "AAECAwQF");
+
+    unsigned char longData[256];
+    for (size_t i = 0; i < sizeof(longData); ++i)
+        longData[i] = i;
+
+    EXPECT_EQ(
+        Base64UrlEncode({longData, sizeof(longData)}),
+        "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+"
+        "P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+"
+        "AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/"
+        "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w");
+}