mirror of https://github.com/ARMmbed/mbed-os.git
LoRaMacCrypto unittested
parent
f3d402f70e
commit
e07940d5e7
|
@ -18,12 +18,23 @@
|
|||
#include "gtest/gtest.h"
|
||||
#include "LoRaMacCrypto.h"
|
||||
|
||||
#include "cipher_stub.h"
|
||||
#include "cmac_stub.h"
|
||||
#include "aes_stub.h"
|
||||
|
||||
class Test_LoRaMacCrypto : public testing::Test {
|
||||
protected:
|
||||
LoRaMacCrypto *object;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
cipher_stub.info_value = NULL;
|
||||
cipher_stub.int_zero_counter = 0;
|
||||
cipher_stub.int_value = 0;
|
||||
cmac_stub.int_zero_counter = 0;
|
||||
cmac_stub.int_value = 0;
|
||||
aes_stub.int_zero_counter = 0;
|
||||
aes_stub.int_value = 0;
|
||||
object = new LoRaMacCrypto();
|
||||
}
|
||||
|
||||
|
@ -36,5 +47,132 @@ protected:
|
|||
TEST_F(Test_LoRaMacCrypto, constructor)
|
||||
{
|
||||
EXPECT_TRUE(object);
|
||||
LoRaMacCrypto obj;
|
||||
}
|
||||
|
||||
TEST_F(Test_LoRaMacCrypto, compute_mic)
|
||||
{
|
||||
EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
mbedtls_cipher_info_t info;
|
||||
cipher_stub.info_value = &info;
|
||||
cipher_stub.int_zero_counter = 0;
|
||||
cipher_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
cipher_stub.int_value = 0;
|
||||
cmac_stub.int_zero_counter = 0;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
cmac_stub.int_zero_counter = 1;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
cmac_stub.int_zero_counter = 2;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
cmac_stub.int_zero_counter = 3;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
uint32_t mic[16];
|
||||
cmac_stub.int_zero_counter = 3;
|
||||
cmac_stub.int_value = 0;
|
||||
EXPECT_TRUE(0 == object->compute_mic(NULL, 0, NULL, 0, 0, 0, 0, mic));
|
||||
|
||||
}
|
||||
|
||||
TEST_F(Test_LoRaMacCrypto, encrypt_payload)
|
||||
{
|
||||
aes_stub.int_zero_counter = 0;
|
||||
aes_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
|
||||
aes_stub.int_zero_counter = 1;
|
||||
aes_stub.int_value = -2;
|
||||
uint8_t buf[60];
|
||||
uint8_t enc[60];
|
||||
EXPECT_TRUE(-2 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
|
||||
|
||||
aes_stub.int_zero_counter = 2;
|
||||
aes_stub.int_value = -3;
|
||||
EXPECT_TRUE(-3 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
|
||||
|
||||
aes_stub.int_value = 0;
|
||||
EXPECT_TRUE(0 == object->encrypt_payload(buf, 20, NULL, 0, 0, 0, 0, enc));
|
||||
|
||||
EXPECT_TRUE(0 == object->encrypt_payload(buf, 60, NULL, 0, 0, 0, 0, enc));
|
||||
|
||||
aes_stub.int_zero_counter = 0;
|
||||
EXPECT_TRUE(0 == object->encrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
}
|
||||
|
||||
TEST_F(Test_LoRaMacCrypto, decrypt_payload)
|
||||
{
|
||||
EXPECT_TRUE(0 == object->decrypt_payload(NULL, 0, NULL, 0, 0, 0, 0, NULL));
|
||||
}
|
||||
|
||||
TEST_F(Test_LoRaMacCrypto, compute_join_frame_mic)
|
||||
{
|
||||
uint32_t mic[16];
|
||||
EXPECT_TRUE(MBEDTLS_ERR_CIPHER_ALLOC_FAILED == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
|
||||
mbedtls_cipher_info_t info;
|
||||
cipher_stub.info_value = &info;
|
||||
cipher_stub.int_zero_counter = 0;
|
||||
cipher_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
|
||||
|
||||
cipher_stub.int_value = 0;
|
||||
cmac_stub.int_zero_counter = 0;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
|
||||
|
||||
cmac_stub.int_zero_counter = 1;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
|
||||
|
||||
cmac_stub.int_zero_counter = 2;
|
||||
cmac_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_join_frame_mic(NULL, 0, NULL, 0, NULL));
|
||||
|
||||
cmac_stub.int_zero_counter = 3;
|
||||
cmac_stub.int_value = 0;
|
||||
EXPECT_TRUE(0 == object->compute_join_frame_mic(NULL, 0, NULL, 0, mic));
|
||||
}
|
||||
|
||||
TEST_F(Test_LoRaMacCrypto, decrypt_join_frame)
|
||||
{
|
||||
aes_stub.int_zero_counter = 0;
|
||||
aes_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL));
|
||||
|
||||
aes_stub.int_zero_counter = 1;
|
||||
aes_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->decrypt_join_frame(NULL, 0, NULL, 0, NULL));
|
||||
|
||||
aes_stub.int_value = 0;
|
||||
uint8_t buf[60];
|
||||
uint8_t enc[60];
|
||||
EXPECT_TRUE(0 == object->decrypt_join_frame(buf, 60, NULL, 0, enc));
|
||||
}
|
||||
|
||||
TEST_F(Test_LoRaMacCrypto, compute_skeys_for_join_frame)
|
||||
{
|
||||
uint8_t nwk_key[16];
|
||||
uint8_t app_key[16];
|
||||
uint8_t nonce[16];
|
||||
|
||||
aes_stub.int_zero_counter = 0;
|
||||
aes_stub.int_value = -1;
|
||||
EXPECT_TRUE(-1 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
|
||||
|
||||
aes_stub.int_zero_counter = 1;
|
||||
aes_stub.int_value = -2;
|
||||
EXPECT_TRUE(-2 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
|
||||
|
||||
aes_stub.int_zero_counter = 0;
|
||||
aes_stub.int_value = 0;
|
||||
EXPECT_TRUE(0 == object->compute_skeys_for_join_frame(NULL, 0, nonce, 0, nwk_key, app_key));
|
||||
}
|
||||
|
|
|
@ -26,6 +26,9 @@
|
|||
|
||||
#include "mbedtls/aes.h"
|
||||
|
||||
#include "aes_stub.h"
|
||||
aes_stub_def aes_stub;
|
||||
|
||||
|
||||
void mbedtls_aes_init( mbedtls_aes_context *ctx )
|
||||
{
|
||||
|
@ -49,7 +52,11 @@ void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx )
|
|||
int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -57,7 +64,11 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
|
|||
int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits )
|
||||
{
|
||||
return 0;
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -66,14 +77,22 @@ int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
|
|||
const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
return 0;
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
return 0;
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -81,7 +100,11 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
|
|||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
|
||||
|
@ -94,7 +117,11 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
|
|||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
|
||||
|
@ -108,7 +135,11 @@ int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
|
|||
const unsigned char input[16],
|
||||
unsigned char output[16] )
|
||||
{
|
||||
return 0;
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
|
||||
|
@ -118,7 +149,11 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
|
|||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
|
||||
|
@ -130,7 +165,11 @@ int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
|
|||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -142,7 +181,11 @@ int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
|
|||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
|
||||
|
@ -152,7 +195,11 @@ int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
|
|||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
|
||||
|
@ -162,7 +209,11 @@ int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
|
|||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
|
||||
|
@ -173,5 +224,9 @@ int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
|
|||
const unsigned char *input,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (aes_stub.int_zero_counter) {
|
||||
aes_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return aes_stub.int_value;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) , Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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 <inttypes.h>
|
||||
|
||||
typedef struct {
|
||||
int int_value;
|
||||
uint8_t int_zero_counter;
|
||||
} aes_stub_def;
|
||||
|
||||
extern aes_stub_def aes_stub;
|
|
@ -40,27 +40,30 @@
|
|||
#include "mbedtls/cmac.h"
|
||||
#endif
|
||||
|
||||
#include "cipher_stub.h"
|
||||
|
||||
cipher_stub_def cipher_stub;
|
||||
|
||||
const int *mbedtls_cipher_list( void )
|
||||
{
|
||||
return NULL;
|
||||
return cipher_stub.int_ptr;
|
||||
}
|
||||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
|
||||
{
|
||||
return( NULL );
|
||||
return cipher_stub.info_value;
|
||||
}
|
||||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
|
||||
{
|
||||
return( NULL );
|
||||
return cipher_stub.info_value;
|
||||
}
|
||||
|
||||
const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
|
||||
int key_bitlen,
|
||||
const mbedtls_cipher_mode_t mode )
|
||||
{
|
||||
return( NULL );
|
||||
return cipher_stub.info_value;
|
||||
}
|
||||
|
||||
void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
|
||||
|
@ -73,59 +76,99 @@ void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
|
|||
|
||||
int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
|
||||
int key_bitlen, const mbedtls_operation_t operation )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *iv, size_t iv_len )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *ad, size_t ad_len )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
|
||||
size_t ilen, unsigned char *output, size_t *olen )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *output, size_t *olen )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *tag, size_t tag_len )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *tag, size_t tag_len )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
|
||||
|
@ -133,7 +176,11 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
|
|||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output, size_t *olen )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
|
||||
|
@ -143,7 +190,11 @@ int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
|
|||
unsigned char *output, size_t *olen,
|
||||
unsigned char *tag, size_t tag_len )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
|
||||
|
@ -153,6 +204,10 @@ int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
|
|||
unsigned char *output, size_t *olen,
|
||||
const unsigned char *tag, size_t tag_len )
|
||||
{
|
||||
return( 0 );
|
||||
if (cipher_stub.int_zero_counter) {
|
||||
cipher_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cipher_stub.int_value;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) , Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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 <inttypes.h>
|
||||
#include "mbedtls/cipher.h"
|
||||
|
||||
typedef struct {
|
||||
mbedtls_cipher_info_t *info_value;
|
||||
int int_value;
|
||||
int *int_ptr;
|
||||
uint8_t int_zero_counter;
|
||||
} cipher_stub_def;
|
||||
|
||||
extern cipher_stub_def cipher_stub;
|
|
@ -26,28 +26,47 @@
|
|||
#include "mbedtls/platform_util.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "cmac_stub.h"
|
||||
|
||||
cmac_stub_def cmac_stub;
|
||||
|
||||
int mbedtls_cipher_cmac_starts( mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *key, size_t keybits )
|
||||
{
|
||||
return 0;
|
||||
if (cmac_stub.int_zero_counter) {
|
||||
cmac_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cmac_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_cmac_update( mbedtls_cipher_context_t *ctx,
|
||||
const unsigned char *input, size_t ilen )
|
||||
{
|
||||
return( 0 );
|
||||
if (cmac_stub.int_zero_counter) {
|
||||
cmac_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cmac_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_cmac_finish( mbedtls_cipher_context_t *ctx,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (cmac_stub.int_zero_counter) {
|
||||
cmac_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cmac_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_cmac_reset( mbedtls_cipher_context_t *ctx )
|
||||
{
|
||||
return( 0 );
|
||||
if (cmac_stub.int_zero_counter) {
|
||||
cmac_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cmac_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
|
||||
|
@ -55,13 +74,21 @@ int mbedtls_cipher_cmac( const mbedtls_cipher_info_t *cipher_info,
|
|||
const unsigned char *input, size_t ilen,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (cmac_stub.int_zero_counter) {
|
||||
cmac_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cmac_stub.int_value;
|
||||
}
|
||||
|
||||
int mbedtls_aes_cmac_prf_128( const unsigned char *key, size_t key_length,
|
||||
const unsigned char *input, size_t in_len,
|
||||
unsigned char *output )
|
||||
{
|
||||
return( 0 );
|
||||
if (cmac_stub.int_zero_counter) {
|
||||
cmac_stub.int_zero_counter--;
|
||||
return 0;
|
||||
}
|
||||
return cmac_stub.int_value;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) , Arm Limited and affiliates.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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 <inttypes.h>
|
||||
|
||||
typedef struct {
|
||||
int int_value;
|
||||
uint8_t int_zero_counter;
|
||||
} cmac_stub_def;
|
||||
|
||||
extern cmac_stub_def cmac_stub;
|
|
@ -33,13 +33,7 @@
|
|||
#if defined(MBEDTLS_CMAC_C) && defined(MBEDTLS_AES_C) && defined(MBEDTLS_CIPHER_C)
|
||||
|
||||
LoRaMacCrypto::LoRaMacCrypto()
|
||||
: mic_block_b0(),
|
||||
computed_mic(),
|
||||
a_block(),
|
||||
s_block()
|
||||
{
|
||||
mic_block_b0[0] = 0x49;
|
||||
a_block[0] = 0x01;
|
||||
}
|
||||
|
||||
int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size,
|
||||
|
@ -47,8 +41,12 @@ int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size,
|
|||
uint32_t address, uint8_t dir, uint32_t seq_counter,
|
||||
uint32_t *mic)
|
||||
{
|
||||
uint8_t computed_mic[16] = {};
|
||||
uint8_t mic_block_b0[16] = {};
|
||||
int ret = 0;
|
||||
|
||||
mic_block_b0[0] = 0x49;
|
||||
|
||||
mic_block_b0[5] = dir;
|
||||
|
||||
mic_block_b0[6] = (address) & 0xFF;
|
||||
|
@ -95,7 +93,8 @@ int LoRaMacCrypto::compute_mic(const uint8_t *buffer, uint16_t size,
|
|||
ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
exit: mbedtls_cipher_free(aes_cmac_ctx);
|
||||
exit:
|
||||
mbedtls_cipher_free(aes_cmac_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -108,12 +107,15 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
|
|||
uint8_t bufferIndex = 0;
|
||||
uint16_t ctr = 1;
|
||||
int ret = 0;
|
||||
uint8_t a_block[16] = {};
|
||||
uint8_t s_block[16] = {};
|
||||
|
||||
mbedtls_aes_init(&aes_ctx);
|
||||
ret = mbedtls_aes_setkey_enc(&aes_ctx, key, key_length);
|
||||
if (0 != ret)
|
||||
goto exit;
|
||||
|
||||
a_block[0] = 0x01;
|
||||
a_block[5] = dir;
|
||||
|
||||
a_block[6] = (address) & 0xFF;
|
||||
|
@ -153,7 +155,8 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
|
|||
}
|
||||
}
|
||||
|
||||
exit: mbedtls_aes_free(&aes_ctx);
|
||||
exit:
|
||||
mbedtls_aes_free(&aes_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -170,6 +173,7 @@ int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size,
|
|||
const uint8_t *key, uint32_t key_length,
|
||||
uint32_t *mic)
|
||||
{
|
||||
uint8_t computed_mic[16] = {};
|
||||
int ret = 0;
|
||||
|
||||
mbedtls_cipher_init(aes_cmac_ctx);
|
||||
|
@ -199,7 +203,8 @@ int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size,
|
|||
ret = MBEDTLS_ERR_CIPHER_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
exit: mbedtls_cipher_free(aes_cmac_ctx);
|
||||
exit:
|
||||
mbedtls_cipher_free(aes_cmac_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -226,7 +231,8 @@ int LoRaMacCrypto::decrypt_join_frame(const uint8_t *buffer, uint16_t size,
|
|||
dec_buffer + 16);
|
||||
}
|
||||
|
||||
exit: mbedtls_aes_free(&aes_ctx);
|
||||
exit:
|
||||
mbedtls_aes_free(&aes_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -258,7 +264,8 @@ int LoRaMacCrypto::compute_skeys_for_join_frame(const uint8_t *key, uint32_t key
|
|||
memcpy(nonce + 7, p_dev_nonce, 2);
|
||||
ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, nonce, app_skey);
|
||||
|
||||
exit: mbedtls_aes_free(&aes_ctx);
|
||||
exit:
|
||||
mbedtls_aes_free(&aes_ctx);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
|
|
|
@ -147,24 +147,6 @@ public:
|
|||
uint8_t *nwk_skey, uint8_t *app_skey);
|
||||
|
||||
private:
|
||||
/**
|
||||
* MIC field computation initial data
|
||||
*/
|
||||
uint8_t mic_block_b0[16];
|
||||
|
||||
/**
|
||||
* Contains the computed MIC field.
|
||||
*
|
||||
* \remark Only the 4 first bytes are used
|
||||
*/
|
||||
uint8_t computed_mic[16];
|
||||
|
||||
/**
|
||||
* Encryption aBlock and sBlock
|
||||
*/
|
||||
uint8_t a_block[16];
|
||||
uint8_t s_block[16];
|
||||
|
||||
/**
|
||||
* AES computation context variable
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue