Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/cryptography/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
# 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.

option(BUILD_CRYPTOGRAPHY_TESTS "Build cryptography test" OFF)
option(BUILD_CRYPTOGRAPHY_RPC_TESTS "Build cryptography rpc test" OFF)
option(BUILD_CRYPTOGRAPHY_TEST_KEY_GEN "Build test key generator" OFF)

if (BUILD_CRYPTOGRAPHY_TESTS)
add_subdirectory(cryptography_test)
Expand All @@ -24,3 +26,7 @@ endif()
if (BUILD_CRYPTOGRAPHY_RPC_TESTS)
add_subdirectory(rpc_cryptography_test)
endif()

if (BUILD_CRYPTOGRAPHY_TEST_KEY_GEN)
add_subdirectory(test_key_generator)
endif()
39 changes: 39 additions & 0 deletions Source/cryptography/tests/test_key_generator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# If not stated otherwise in this file or this component's LICENSE file the
# following copyright and licenses apply:
#
# Copyright 2023 Metrological
#
# 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.

find_package(${NAMESPACE}Core REQUIRED)
find_package(OpenSSL)

add_executable(cgkeygen
Module.cpp
main.cpp)

include_directories(${CMAKE_CURRENT_LIST_DIR}/../../../cryptography)
include_directories($<INSTALL_INTERFACE:include/${NAMESPACE}>)

set_target_properties(cgkeygen PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES)

target_link_libraries(cgkeygen
PRIVATE
${NAMESPACE}Cryptography
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always like consistency :-) and my brain got an error by this construction :-D
Why do we always have ${NAMESPACE}Core::${NAMESPACE}Core and here only once (${NAMESPACE}Cryptography) without the semicolon. I understood from @bramoosterhuis that the first was the preferred way.
If so lets stick to that and always use for components that we as metrological deliver the ${NAMESPACE}Cryptography::${NAMESPACE}Cryptography to avoid my brain errors :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Now need to educate myself on the difference. :)

${NAMESPACE}Core::${NAMESPACE}Core
ssl
crypto)

install(TARGETS cgkeygen DESTINATION bin)
22 changes: 22 additions & 0 deletions Source/cryptography/tests/test_key_generator/Module.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Metrological
*
* 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 "Module.h"

MODULE_NAME_DECLARATION(BUILD_REFERENCE)
29 changes: 29 additions & 0 deletions Source/cryptography/tests/test_key_generator/Module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Metrological
*
* 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

#ifndef MODULE_NAME
#define MODULE_NAME CryptographyTestKeyGenerator
#endif

#include <core/core.h>

#undef EXTERNAL
#define EXTERNAL
118 changes: 118 additions & 0 deletions Source/cryptography/tests/test_key_generator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Metrological
*
* 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 "Module.h"

#include <core/core.h>

#include <openssl/evp.h>
#include <openssl/rand.h>

#include <interfaces/ICryptography.h>
#include <interfaces/INetflixSecurity.h>

namespace {

using namespace WPEFramework;

static constexpr Exchange::CryptographyVault VAULT = Exchange::CRYPTOGRAPHY_VAULT_PLATFORM;

static bool GenerateAESKey(const std::string passphrase, const uint32_t iterations, const uint16_t sizeBits, const std::string name, const string connector = "")
{
bool result = false;

Exchange::ICryptography* crypto = Exchange::ICryptography::Instance(connector);

printf("Generating %d-bit AES key with PKBDF2 (HMAC-256, %d iterations)...\n", sizeBits, iterations);

if (crypto == nullptr) {
printf("Cryptography not available!\n");
}
else if ((iterations != 0) && (sizeBits % 8 == 0) && (sizeBits <= 512)) {

Exchange::IVault* vault = crypto->Vault(VAULT);

const uint16_t size = (sizeBits / 8);

if (vault != nullptr) {
uint8_t salt[16];
uint8_t* hash = new uint8_t[size];

RAND_bytes(salt, sizeof(salt));

if (PKCS5_PBKDF2_HMAC(passphrase.c_str(), passphrase.size(), salt, sizeof(salt), iterations, EVP_sha256(), size, hash)) {

const uint32_t keyId = vault->Import(size, hash);

::memset(hash, 0xFF, size);

uint8_t encryptedKey[64];
const uint16_t encryptedKeySize = vault->Get(keyId, sizeof(encryptedKey), encryptedKey);

Core::File keyFile(name);

if ((keyFile.Exists() == false) && (keyFile.Create() == true)) {
keyFile.Write(encryptedKey, encryptedKeySize);
keyFile.Close();

printf("Genenerated AES key %s\n", keyFile.Name().c_str());
result = true;
}

vault->Delete(keyId);
}

delete[] hash;

vault->Release();
}
}
else {
printf("Invalid parameters!\n");
}

if (crypto != nullptr) {
crypto->Release();
}

return (result);
}

}


int main(const int argc, const char* argv[])
{
int result = 0;

if ((argc == 5) || (argc == 4)) {
uint32_t iterations = (argc == 5? atoi(argv[4]) : 500000);

if (GenerateAESKey(argv[2], iterations, atoi(argv[3]), argv[1]) == false) {
printf("FAILED to generate a key!\n");
result = 1;
}
}
else {
printf("usage: %s <filename> <passphrase> <sizebits> [iterations]\n", argv[0]);
}

return (result);
}