|
37 | 37 | #include <string.h> |
38 | 38 |
|
39 | 39 | #include <mbedtls/aes.h> |
| 40 | +#include <mbedtls/ccm.h> |
40 | 41 | #include <mbedtls/cmac.h> |
41 | 42 | #include <mbedtls/ctr_drbg.h> |
42 | 43 | #include <mbedtls/ecdsa.h> |
@@ -152,6 +153,59 @@ OT_TOOL_WEAK otError otPlatCryptoAesFree(otCryptoContext *aContext) |
152 | 153 | return error; |
153 | 154 | } |
154 | 155 |
|
| 156 | +#if OPENTHREAD_CONFIG_CRYPTO_PLATFORM_CCM_ONE_SHOT_ENABLE |
| 157 | + |
| 158 | +OT_TOOL_WEAK otError otPlatCryptoAesCcmProcessOneShot(bool aEncrypt, |
| 159 | + const otPlatCryptoAesCcmConfig *aConfig, |
| 160 | + const uint8_t *aHeader, |
| 161 | + uint8_t *aData) |
| 162 | +{ |
| 163 | + Error error = kErrorNone; |
| 164 | + mbedtls_ccm_context ctx; |
| 165 | + int ret; |
| 166 | + |
| 167 | + mbedtls_ccm_init(&ctx); |
| 168 | + |
| 169 | + VerifyOrExit(aConfig != nullptr && aConfig->mNonce != nullptr && aData != nullptr, error = kErrorInvalidArgs); |
| 170 | + |
| 171 | + { |
| 172 | + const LiteralKey key(*static_cast<const Key *>(&aConfig->mKey)); |
| 173 | + |
| 174 | + ret = mbedtls_ccm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key.GetBytes(), key.GetLength() * kBitsPerByte); |
| 175 | + VerifyOrExit(ret == 0, error = kErrorFailed); |
| 176 | + |
| 177 | + if (aEncrypt) |
| 178 | + { |
| 179 | + ret = mbedtls_ccm_encrypt_and_tag(&ctx, aConfig->mPlainTextLength, aConfig->mNonce, aConfig->mNonceLength, |
| 180 | + aHeader, aConfig->mHeaderLength, aData, aData, |
| 181 | + aData + aConfig->mPlainTextLength, aConfig->mTagLength); |
| 182 | + VerifyOrExit(ret == 0, error = kErrorFailed); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + // MBEDTLS_ERR_CCM_AUTH_FAILED is the expected return on tag mismatch; map to kErrorSecurity. |
| 187 | + ret = mbedtls_ccm_auth_decrypt(&ctx, aConfig->mPlainTextLength, aConfig->mNonce, aConfig->mNonceLength, |
| 188 | + aHeader, aConfig->mHeaderLength, aData, aData, |
| 189 | + aData + aConfig->mPlainTextLength, aConfig->mTagLength); |
| 190 | + |
| 191 | + if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) |
| 192 | + { |
| 193 | + error = kErrorSecurity; |
| 194 | + } |
| 195 | + else |
| 196 | + { |
| 197 | + VerifyOrExit(ret == 0, error = kErrorFailed); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +exit: |
| 203 | + mbedtls_ccm_free(&ctx); |
| 204 | + return error; |
| 205 | +} |
| 206 | + |
| 207 | +#endif // OPENTHREAD_CONFIG_CRYPTO_PLATFORM_CCM_ONE_SHOT_ENABLE |
| 208 | + |
155 | 209 | #if OPENTHREAD_FTD || OPENTHREAD_MTD |
156 | 210 |
|
157 | 211 | // HMAC implementations |
|
0 commit comments