forked from matthijskooijman/arduino-dsmr
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Remove dynamic memory allocations. Add support for TF-PSA encryption. Add logging. Add extra sensors. #51
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
Merged
Merged
Changes from all commits
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
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
This file was deleted.
Oops, something went wrong.
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,41 @@ | ||
| #pragma once | ||
| #include "../util.h" | ||
| #include <charconv> | ||
| #include <optional> | ||
| #include <span> | ||
|
|
||
| namespace dsmr_parser { | ||
|
|
||
| class Aes128GcmDecryptionKey final { | ||
| std::array<uint8_t, 16> key{}; | ||
| explicit Aes128GcmDecryptionKey(const std::array<uint8_t, 16> k) : key(k) {} | ||
|
|
||
| public: | ||
| // hex is a string like "00112233445566778899AABBCCDDEEFF" | ||
| static std::optional<Aes128GcmDecryptionKey> from_hex(const std::string_view hex) { | ||
| if (hex.size() != 32) | ||
| return std::nullopt; | ||
| std::array<uint8_t, 16> arr{}; | ||
| for (size_t i = 0; i < 16; ++i) { | ||
| auto [ptr, ec] = std::from_chars(hex.data() + i * 2, hex.data() + i * 2 + 2, arr[i], 16); | ||
| if (ec != std::errc{}) | ||
| return std::nullopt; | ||
| } | ||
| return Aes128GcmDecryptionKey(arr); | ||
| } | ||
|
|
||
| const uint8_t* data() const { return key.data(); } | ||
| }; | ||
|
|
||
| class Aes128GcmDecryptor { | ||
| public: | ||
| virtual void set_encryption_key(const Aes128GcmDecryptionKey& key) = 0; | ||
| virtual bool decrypt_inplace(std::span<const uint8_t, 17> aad, std::span<const uint8_t, 12> nonce, std::span<uint8_t> ciphertext, | ||
| std::span<const uint8_t, 12> tag) = 0; | ||
|
|
||
| protected: | ||
| virtual ~Aes128GcmDecryptor() = default; | ||
| std::optional<Aes128GcmDecryptionKey> decryption_key_; | ||
| }; | ||
|
|
||
| } | ||
20 changes: 14 additions & 6 deletions
20
src/dsmr_parser/aes128gcm_bearssl.h → ...smr_parser/decryption/aes128gcm_bearssl.h
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
10 changes: 5 additions & 5 deletions
10
src/dsmr_parser/aes128gcm_mbedtls.h → ...smr_parser/decryption/aes128gcm_mbedtls.h
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
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,98 @@ | ||
| #pragma once | ||
|
|
||
| #include "../util.h" | ||
| #include "aes128gcm.h" | ||
| #include <psa/crypto.h> | ||
| #include <span> | ||
|
|
||
|
PolarGoose marked this conversation as resolved.
|
||
| namespace dsmr_parser { | ||
|
|
||
| class Aes128GcmTfPsa final : public Aes128GcmDecryptor, NonCopyableAndNonMovable { | ||
| psa_key_id_t key_id = 0; | ||
| bool initialized = false; | ||
|
|
||
| public: | ||
| Aes128GcmTfPsa() { initialized = (psa_crypto_init() == PSA_SUCCESS); } | ||
|
|
||
| void set_encryption_key(const Aes128GcmDecryptionKey& key) override { | ||
| if (!initialized) { | ||
| return; | ||
| } | ||
|
|
||
| if (key_id != 0) { | ||
| psa_destroy_key(key_id); | ||
| key_id = 0; | ||
| } | ||
|
|
||
| psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | ||
| psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); | ||
| psa_set_key_bits(&attributes, 128); | ||
| psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); | ||
| psa_set_key_algorithm(&attributes, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 12)); | ||
|
|
||
| const psa_status_t status = psa_import_key(&attributes, key.data(), 16, &key_id); | ||
|
|
||
| psa_reset_key_attributes(&attributes); | ||
|
|
||
| if (status != PSA_SUCCESS) { | ||
| key_id = 0; | ||
| } | ||
| } | ||
|
|
||
| bool decrypt_inplace(std::span<const uint8_t, 17> aad, std::span<const uint8_t, 12> nonce, std::span<uint8_t> ciphertext, | ||
| std::span<const uint8_t, 12> tag) override { | ||
| if (!initialized || key_id == 0) { | ||
| return false; | ||
| } | ||
|
|
||
| psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT; | ||
| size_t out_len = 0; | ||
| size_t tail_len = 0; | ||
|
|
||
| psa_status_t status = psa_aead_decrypt_setup(&op, key_id, PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, tag.size())); | ||
| if (status != PSA_SUCCESS) { | ||
| psa_aead_abort(&op); | ||
| return false; | ||
| } | ||
|
|
||
| status = psa_aead_set_nonce(&op, nonce.data(), nonce.size()); | ||
| if (status != PSA_SUCCESS) { | ||
| psa_aead_abort(&op); | ||
| return false; | ||
| } | ||
|
|
||
| status = psa_aead_update_ad(&op, aad.data(), aad.size()); | ||
| if (status != PSA_SUCCESS) { | ||
| psa_aead_abort(&op); | ||
| return false; | ||
| } | ||
|
|
||
| status = psa_aead_update(&op, ciphertext.data(), ciphertext.size(), ciphertext.data(), ciphertext.size(), &out_len); | ||
| if (status != PSA_SUCCESS || out_len != ciphertext.size()) { | ||
| psa_aead_abort(&op); | ||
| return false; | ||
| } | ||
|
|
||
| status = psa_aead_verify(&op, nullptr, 0, &tail_len, tag.data(), tag.size()); | ||
|
|
||
| if (status != PSA_SUCCESS) { | ||
| psa_aead_abort(&op); | ||
| return false; | ||
| } | ||
|
|
||
| if (tail_len != 0) { | ||
| psa_aead_abort(&op); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| ~Aes128GcmTfPsa() { | ||
| if (key_id != 0) { | ||
| psa_destroy_key(key_id); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace dsmr_parser | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.