-
Notifications
You must be signed in to change notification settings - Fork 52
[cryptography] Add test key generator #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sebaszm
wants to merge
3
commits into
master
Choose a base branch
from
development/cg-test-keygen
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
Source/cryptography/tests/test_key_generator/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ${NAMESPACE}Core::${NAMESPACE}Core | ||
| ssl | ||
| crypto) | ||
|
|
||
| install(TARGETS cgkeygen DESTINATION bin) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
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. :)