Skip to content

Commit 0fbee98

Browse files
authored
[crypto] simplify AesCcm public APIs and introduce internal Engine (openthread#13215)
This commit refactors the `AesCcm` class to simplify its public interface, decoupling the high-level API from the underlying cryptographic execution. Key improvements: - Redesigned the API from a series of procedural method calls (`Init()`, `Header()`, `Payload()`, `Finalize()`) into a unified, stateful model. Callers now pre-configure the operation parameters using dedicated setters (`SetKey()`, `SetNonce()`, `SetAuthData()`, `SetTagLength()`) and execute the entire cryptographic operation in a single step via unified `Process()` methods. - Introduced a nested `Engine` class to encapsulate the low-level AES-CCM mathematical and cryptographic state. The `Engine` provides clean internal interfaces for both optimized one-shot (single-part) and multi-part operations. - This architectural separation allows the outer `AesCcm` class to focus on parameter validation, high-level buffer management, and complex `Message` chunk iterations, while the `Engine` remains focused purely on the cryptographic core. This also provides a clean extension point to easily route one-shot operations to platform-specific hardware acceleration APIs in the future. - Updated `Mac` and `Mle` modules to use the simplified APIs, reducing boilerplate code. - Retained a static `Perform()` wrapper to support the legacy public `otCrypto` API. - Updated unit tests to validate the new stateful interfaces, including robust in-place message chunk processing and separate-buffer validations.
1 parent a055c4b commit 0fbee98

7 files changed

Lines changed: 569 additions & 281 deletions

File tree

src/core/api/crypto_api.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,11 @@ void otCryptoAesCcm(const otCryptoKey *aKey,
6464
bool aEncrypt,
6565
void *aTag)
6666
{
67-
AesCcm aesCcm;
68-
6967
AssertPointerIsNotNull(aNonce);
7068
AssertPointerIsNotNull(aPlainText);
7169
AssertPointerIsNotNull(aCipherText);
7270
AssertPointerIsNotNull(aTag);
7371

74-
aesCcm.SetKey(AsCoreType(aKey));
75-
aesCcm.Init(aHeaderLength, aLength, aTagLength, aNonce, aNonceLength);
76-
77-
if (aHeaderLength != 0)
78-
{
79-
OT_ASSERT(aHeader != nullptr);
80-
aesCcm.Header(aHeader, aHeaderLength);
81-
}
82-
83-
aesCcm.Payload(aPlainText, aCipherText, aLength, aEncrypt ? AesCcm::kEncrypt : AesCcm::kDecrypt);
84-
aesCcm.Finalize(aTag);
72+
AesCcm::Perform(aEncrypt ? AesCcm::kEncrypt : AesCcm::kDecrypt, AsCoreType(aKey), aTagLength, aNonce, aNonceLength,
73+
aHeader, aHeaderLength, aPlainText, aCipherText, aLength, aTag);
8574
}

0 commit comments

Comments
 (0)