Add a CRC module to mbedtls and baremetal config

Add a new CRC module along with some tests for it.
The table and the CRC function body is generated using pycrc v0.9.2. 
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2020-08-07 11:34:21 -04:00
parent 0305753d7a
commit 9df2b416b9
No known key found for this signature in database
GPG Key ID: 89A90840DC388527
12 changed files with 201 additions and 1 deletions

View File

@ -137,6 +137,7 @@
#define MBEDTLS_OID_C #define MBEDTLS_OID_C
#define MBEDTLS_PLATFORM_C #define MBEDTLS_PLATFORM_C
#define MBEDTLS_CRC_C
/* I/O buffer configuration */ /* I/O buffer configuration */
#define MBEDTLS_SSL_MAX_CONTENT_LEN 2048 #define MBEDTLS_SSL_MAX_CONTENT_LEN 2048

View File

@ -2728,6 +2728,17 @@
*/ */
#define MBEDTLS_ERROR_C #define MBEDTLS_ERROR_C
/**
* \def MBEDTLS_CRC_C
*
* Enable the CRC calculating module
*
* Module: library/crc.c
*
* This module enables mbedtls_crc_update.
*/
//#define MBEDTLS_CRC_C
/** /**
* \def MBEDTLS_GCM_C * \def MBEDTLS_GCM_C
* *

47
include/mbedtls/crc.h Normal file
View File

@ -0,0 +1,47 @@
/*
* CRC-16/ARC implementation, generated using pycrc v0.9.2, https://pycrc.org.
*
* Used options: --model=crc-16 --algorithm=tbl --generate=h --std=C89 --table-idx-width 4
*
* Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
* 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.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#ifndef MBEDTLS_CRC_H
#define MBEDTLS_CRC_H
#include <stdlib.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Update the crc value with new data.
*
* \param[in] crc The current crc value.
* \param[in] data Pointer to a buffer of \a data_len bytes.
* \param[in] data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*/
uint16_t mbedtls_crc_update( uint16_t crc, const void *data, size_t data_len );
#ifdef __cplusplus
}
#endif
#endif /* MBEDTLS_CRC_H */

View File

@ -20,6 +20,7 @@ set(src_crypto
cipher.c cipher.c
cipher_wrap.c cipher_wrap.c
cmac.c cmac.c
crc.c
ctr_drbg.c ctr_drbg.c
des.c des.c
dhm.c dhm.c

View File

@ -99,7 +99,8 @@ OBJS_CRYPTO= aes.o aesni.o arc4.o \
ripemd160.o rsa_internal.o rsa.o \ ripemd160.o rsa_internal.o rsa.o \
sha1.o sha256.o sha512.o \ sha1.o sha256.o sha512.o \
threading.o timing.o version.o \ threading.o timing.o version.o \
version_features.o xtea.o version_features.o xtea.o \
crc.o
OBJS_X509= certs.o pkcs11.o x509.o OBJS_X509= certs.o pkcs11.o x509.o

55
library/crc.c Normal file
View File

@ -0,0 +1,55 @@
/*
* CRC-16/ARC implementation, generated using pycrc v0.9.2, https://pycrc.org,
* with further FI countermeasures added manually.
*
* Used options: --model=crc-16 --algorithm=tbl --generate=c --std=C89 --table-idx-width 4
*
* Copyright (C) 2006-2020, ARM Limited, All Rights Reserved
* 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.
*
* This file is part of mbed TLS (https://tls.mbed.org)
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_CRC_C)
#include "mbedtls/crc.h"
static const uint32_t crc_table[16] = {
0x0000, 0xcc01, 0xd801, 0x1400, 0xf001, 0x3c00, 0x2800, 0xe401,
0xa001, 0x6c00, 0x7800, 0xb401, 0x5000, 0x9c01, 0x8801, 0x4400
};
uint16_t mbedtls_crc_update( uint16_t crc, const void *data, size_t data_len )
{
const unsigned char *d = (const unsigned char *)data;
unsigned int tbl_idx;
while ( data_len -- ) {
tbl_idx = crc ^ *d;
crc = crc_table[tbl_idx & 0x0f] ^ ( crc >> 4 );
tbl_idx = crc ^ ( *d >> 4 );
crc = crc_table[tbl_idx & 0x0f] ^ ( crc >> 4 );
d ++;
}
return crc;
}
#endif /* MBEDTLS_CRC_C */

View File

@ -684,6 +684,9 @@ static const char *features[] = {
#if defined(MBEDTLS_ERROR_C) #if defined(MBEDTLS_ERROR_C)
"MBEDTLS_ERROR_C", "MBEDTLS_ERROR_C",
#endif /* MBEDTLS_ERROR_C */ #endif /* MBEDTLS_ERROR_C */
#if defined(MBEDTLS_CRC_C)
"MBEDTLS_CRC_C",
#endif /* MBEDTLS_CRC_C */
#if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_GCM_C)
"MBEDTLS_GCM_C", "MBEDTLS_GCM_C",
#endif /* MBEDTLS_GCM_C */ #endif /* MBEDTLS_GCM_C */

View File

@ -1866,6 +1866,14 @@ int query_config( const char *config )
} }
#endif /* MBEDTLS_ERROR_C */ #endif /* MBEDTLS_ERROR_C */
#if defined(MBEDTLS_CRC_C)
if( strcmp( "MBEDTLS_CRC_C", config ) == 0 )
{
MACRO_EXPANSION_TO_STR( MBEDTLS_CRC_C );
return( 0 );
}
#endif /* MBEDTLS_CRC_C */
#if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_GCM_C)
if( strcmp( "MBEDTLS_GCM_C", config ) == 0 ) if( strcmp( "MBEDTLS_GCM_C", config ) == 0 )
{ {

View File

@ -94,6 +94,7 @@ add_test_suite(cipher cipher.misc)
add_test_suite(cipher cipher.null) add_test_suite(cipher cipher.null)
add_test_suite(cipher cipher.padding) add_test_suite(cipher cipher.padding)
add_test_suite(cmac) add_test_suite(cmac)
add_test_suite(crc)
add_test_suite(ctr_drbg) add_test_suite(ctr_drbg)
add_test_suite(debug) add_test_suite(debug)
add_test_suite(des) add_test_suite(des)

View File

@ -0,0 +1,44 @@
CRC-16 1 byte of 0x00
compute_crc:"00":0
CRC-16 8 bytes of 0x00
compute_crc:"0000000000000000":0
CRC-16 16 bytes of 0x00
compute_crc:"00000000000000000000000000000000":0
CRC-16 32 bytes of 0x00
compute_crc:"0000000000000000000000000000000000000000000000000000000000000000":0
CRC-16 1 byte of 0xFF
compute_crc:"FF":16448
CRC-16 8 bytes of 0xFF
compute_crc:"FFFFFFFFFFFFFFFF":33857
CRC-16 16 bytes of 0xFF
compute_crc:"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF":28736
CRC-16 32 bytes of 0xFF
compute_crc:"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF":41985
CRC-16 1 byte of 0x01
compute_crc:"01":49345
CRC-16 8 bytes incrementing
compute_crc:"0123456789abcdef":62374
CRC-16 16 bytes incrementing
compute_crc:"0123456789abcdef0123456789abcdef":44783
CRC-16 32 bytes incrementing
compute_crc:"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef":44749
CRC-16 64 bytes incrementing
compute_crc:"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef":53063
CRC-16 ascii 1 to 9 incrementing
compute_crc:"313233343536373839":47933
CRC-16 512 bytes of random data
compute_crc:"66870a93e17d4a5dd6ef84476dff6e2aa7d2ebd391cf4c54affff479a98a81360909f32eafbea98f4a3e4737de4c588d11c356860333ad7f4c334fb7dfce77cb04fafb50991f9b2e7957312a1b9dbcbebaf03f4eb9443938279f9b6c01e2b8c6022ee58f5840c7e86962830ca088174dc1b9912b64bde42877343c0b979b8ea376e4bf994a7ff6c629d5ba936958cc9f55db1c98151b16f7d918ff84f85b45e3ee49e7d166baac4dec81a174b3e496446a92c00d0859c2402f0110964effbdae9a6a3243530996029f4a428f1626837e55d32660cf6a2d4263c9fe23841d01b9410a9530bf9b1561fa83f6c42447d310bc991352ee9863b83b890b5aa0ea0bbf":49505

View File

@ -0,0 +1,26 @@
/* BEGIN_HEADER */
#include "mbedtls/crc.h"
/* END_HEADER */
/* BEGIN_DEPENDENCIES
* depends_on:MBEDTLS_CRC_C
* END_DEPENDENCIES
*/
/* BEGIN_CASE */
void compute_crc( data_t *input, unsigned int crc )
{
uint16_t result = mbedtls_crc_update( 0, input->x, input->len );
uint32_t len = input->len;
TEST_ASSERT( crc == result );
result = 0;
while( len > 0 )
{
uint8_t cur_len = ( len > 8 ? 8 : len );
result = mbedtls_crc_update( result, &input->x[ input->len - len ], cur_len );
len -= cur_len;
}
TEST_ASSERT( crc == result );
}
/* END_CASE */

View File

@ -166,6 +166,7 @@
<ClInclude Include="..\..\include\mbedtls\cmac.h" /> <ClInclude Include="..\..\include\mbedtls\cmac.h" />
<ClInclude Include="..\..\include\mbedtls\compat-1.3.h" /> <ClInclude Include="..\..\include\mbedtls\compat-1.3.h" />
<ClInclude Include="..\..\include\mbedtls\config.h" /> <ClInclude Include="..\..\include\mbedtls\config.h" />
<ClInclude Include="..\..\include\mbedtls\crc.h" />
<ClInclude Include="..\..\include\mbedtls\ctr_drbg.h" /> <ClInclude Include="..\..\include\mbedtls\ctr_drbg.h" />
<ClInclude Include="..\..\include\mbedtls\debug.h" /> <ClInclude Include="..\..\include\mbedtls\debug.h" />
<ClInclude Include="..\..\include\mbedtls\des.h" /> <ClInclude Include="..\..\include\mbedtls\des.h" />
@ -243,6 +244,7 @@
<ClCompile Include="..\..\library\cipher.c" /> <ClCompile Include="..\..\library\cipher.c" />
<ClCompile Include="..\..\library\cipher_wrap.c" /> <ClCompile Include="..\..\library\cipher_wrap.c" />
<ClCompile Include="..\..\library\cmac.c" /> <ClCompile Include="..\..\library\cmac.c" />
<ClCompile Include="..\..\library\crc.c" />
<ClCompile Include="..\..\library\ctr_drbg.c" /> <ClCompile Include="..\..\library\ctr_drbg.c" />
<ClCompile Include="..\..\library\debug.c" /> <ClCompile Include="..\..\library\debug.c" />
<ClCompile Include="..\..\library\des.c" /> <ClCompile Include="..\..\library\des.c" />