aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Kurek <andrzej.kurek@arm.com>2020-08-07 11:34:21 -0400
committerAndrzej Kurek <andrzej.kurek@arm.com>2020-08-08 02:10:52 -0400
commit9df2b416b908b7ce206386a04980ac9f6c66b57c (patch)
tree84f7a662997967437e421546a6495fd3dd921335
parent0305753d7a132e907173a29e3986e8aa414499d2 (diff)
downloadmbed-tls-9df2b416b908b7ce206386a04980ac9f6c66b57c.tar.gz
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>
-rw-r--r--configs/baremetal.h1
-rw-r--r--include/mbedtls/config.h11
-rw-r--r--include/mbedtls/crc.h47
-rw-r--r--library/CMakeLists.txt1
-rw-r--r--library/Makefile3
-rw-r--r--library/crc.c55
-rw-r--r--library/version_features.c3
-rw-r--r--programs/ssl/query_config.c8
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/suites/test_suite_crc.data44
-rw-r--r--tests/suites/test_suite_crc.function26
-rw-r--r--visualc/VS2010/mbedTLS.vcxproj2
12 files changed, 201 insertions, 1 deletions
diff --git a/configs/baremetal.h b/configs/baremetal.h
index c93f53af9..24af9b670 100644
--- a/configs/baremetal.h
+++ b/configs/baremetal.h
@@ -137,6 +137,7 @@
#define MBEDTLS_OID_C
#define MBEDTLS_PLATFORM_C
+#define MBEDTLS_CRC_C
/* I/O buffer configuration */
#define MBEDTLS_SSL_MAX_CONTENT_LEN 2048
diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h
index 9b885973b..98df7c58c 100644
--- a/include/mbedtls/config.h
+++ b/include/mbedtls/config.h
@@ -2729,6 +2729,17 @@
#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
*
* Enable the Galois/Counter Mode (GCM) for AES.
diff --git a/include/mbedtls/crc.h b/include/mbedtls/crc.h
new file mode 100644
index 000000000..013166674
--- /dev/null
+++ b/include/mbedtls/crc.h
@@ -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 */
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index b59d318c8..9e0724f9b 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -20,6 +20,7 @@ set(src_crypto
cipher.c
cipher_wrap.c
cmac.c
+ crc.c
ctr_drbg.c
des.c
dhm.c
diff --git a/library/Makefile b/library/Makefile
index 96a9d6031..f11c4df2b 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -99,7 +99,8 @@ OBJS_CRYPTO= aes.o aesni.o arc4.o \
ripemd160.o rsa_internal.o rsa.o \
sha1.o sha256.o sha512.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
diff --git a/library/crc.c b/library/crc.c
new file mode 100644
index 000000000..9e21f2594
--- /dev/null
+++ b/library/crc.c
@@ -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 */
diff --git a/library/version_features.c b/library/version_features.c
index 38a7ceee2..7c5dae79c 100644
--- a/library/version_features.c
+++ b/library/version_features.c
@@ -684,6 +684,9 @@ static const char *features[] = {
#if defined(MBEDTLS_ERROR_C)
"MBEDTLS_ERROR_C",
#endif /* MBEDTLS_ERROR_C */
+#if defined(MBEDTLS_CRC_C)
+ "MBEDTLS_CRC_C",
+#endif /* MBEDTLS_CRC_C */
#if defined(MBEDTLS_GCM_C)
"MBEDTLS_GCM_C",
#endif /* MBEDTLS_GCM_C */
diff --git a/programs/ssl/query_config.c b/programs/ssl/query_config.c
index 8db6d22be..e83671c16 100644
--- a/programs/ssl/query_config.c
+++ b/programs/ssl/query_config.c
@@ -1866,6 +1866,14 @@ int query_config( const char *config )
}
#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( strcmp( "MBEDTLS_GCM_C", config ) == 0 )
{
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 2ea77e7e7..0a3415e34 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -94,6 +94,7 @@ add_test_suite(cipher cipher.misc)
add_test_suite(cipher cipher.null)
add_test_suite(cipher cipher.padding)
add_test_suite(cmac)
+add_test_suite(crc)
add_test_suite(ctr_drbg)
add_test_suite(debug)
add_test_suite(des)
diff --git a/tests/suites/test_suite_crc.data b/tests/suites/test_suite_crc.data
new file mode 100644
index 000000000..aa4c6861e
--- /dev/null
+++ b/tests/suites/test_suite_crc.data
@@ -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
diff --git a/tests/suites/test_suite_crc.function b/tests/suites/test_suite_crc.function
new file mode 100644
index 000000000..8d0995806
--- /dev/null
+++ b/tests/suites/test_suite_crc.function
@@ -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 */
diff --git a/visualc/VS2010/mbedTLS.vcxproj b/visualc/VS2010/mbedTLS.vcxproj
index 0e5788117..4139879b0 100644
--- a/visualc/VS2010/mbedTLS.vcxproj
+++ b/visualc/VS2010/mbedTLS.vcxproj
@@ -166,6 +166,7 @@
<ClInclude Include="..\..\include\mbedtls\cmac.h" />
<ClInclude Include="..\..\include\mbedtls\compat-1.3.h" />
<ClInclude Include="..\..\include\mbedtls\config.h" />
+ <ClInclude Include="..\..\include\mbedtls\crc.h" />
<ClInclude Include="..\..\include\mbedtls\ctr_drbg.h" />
<ClInclude Include="..\..\include\mbedtls\debug.h" />
<ClInclude Include="..\..\include\mbedtls\des.h" />
@@ -243,6 +244,7 @@
<ClCompile Include="..\..\library\cipher.c" />
<ClCompile Include="..\..\library\cipher_wrap.c" />
<ClCompile Include="..\..\library\cmac.c" />
+ <ClCompile Include="..\..\library\crc.c" />
<ClCompile Include="..\..\library\ctr_drbg.c" />
<ClCompile Include="..\..\library\debug.c" />
<ClCompile Include="..\..\library\des.c" />