]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS-Labs/Source/mbedtls/library/xtea.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Source / mbedtls / library / xtea.c
diff --git a/FreeRTOS-Labs/Source/mbedtls/library/xtea.c b/FreeRTOS-Labs/Source/mbedtls/library/xtea.c
new file mode 100644 (file)
index 0000000..3eb7035
--- /dev/null
@@ -0,0 +1,277 @@
+/*\r
+ *  An 32-bit implementation of the XTEA algorithm\r
+ *\r
+ *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
+ *  SPDX-License-Identifier: Apache-2.0\r
+ *\r
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may\r
+ *  not use this file except in compliance with the License.\r
+ *  You may obtain a copy of the License at\r
+ *\r
+ *  http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *\r
+ *  This file is part of mbed TLS (https://tls.mbed.org)\r
+ */\r
+\r
+#if !defined(MBEDTLS_CONFIG_FILE)\r
+#include "mbedtls/config.h"\r
+#else\r
+#include MBEDTLS_CONFIG_FILE\r
+#endif\r
+\r
+#if defined(MBEDTLS_XTEA_C)\r
+\r
+#include "mbedtls/xtea.h"\r
+#include "mbedtls/platform_util.h"\r
+\r
+#include <string.h>\r
+\r
+#if defined(MBEDTLS_SELF_TEST)\r
+#if defined(MBEDTLS_PLATFORM_C)\r
+#include "mbedtls/platform.h"\r
+#else\r
+#include <stdio.h>\r
+#define mbedtls_printf printf\r
+#endif /* MBEDTLS_PLATFORM_C */\r
+#endif /* MBEDTLS_SELF_TEST */\r
+\r
+#if !defined(MBEDTLS_XTEA_ALT)\r
+\r
+/*\r
+ * 32-bit integer manipulation macros (big endian)\r
+ */\r
+#ifndef GET_UINT32_BE\r
+#define GET_UINT32_BE(n,b,i)                            \\r
+{                                                       \\r
+    (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \\r
+        | ( (uint32_t) (b)[(i) + 1] << 16 )             \\r
+        | ( (uint32_t) (b)[(i) + 2] <<  8 )             \\r
+        | ( (uint32_t) (b)[(i) + 3]       );            \\r
+}\r
+#endif\r
+\r
+#ifndef PUT_UINT32_BE\r
+#define PUT_UINT32_BE(n,b,i)                            \\r
+{                                                       \\r
+    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \\r
+    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \\r
+    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \\r
+    (b)[(i) + 3] = (unsigned char) ( (n)       );       \\r
+}\r
+#endif\r
+\r
+void mbedtls_xtea_init( mbedtls_xtea_context *ctx )\r
+{\r
+    memset( ctx, 0, sizeof( mbedtls_xtea_context ) );\r
+}\r
+\r
+void mbedtls_xtea_free( mbedtls_xtea_context *ctx )\r
+{\r
+    if( ctx == NULL )\r
+        return;\r
+\r
+    mbedtls_platform_zeroize( ctx, sizeof( mbedtls_xtea_context ) );\r
+}\r
+\r
+/*\r
+ * XTEA key schedule\r
+ */\r
+void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] )\r
+{\r
+    int i;\r
+\r
+    memset( ctx, 0, sizeof(mbedtls_xtea_context) );\r
+\r
+    for( i = 0; i < 4; i++ )\r
+    {\r
+        GET_UINT32_BE( ctx->k[i], key, i << 2 );\r
+    }\r
+}\r
+\r
+/*\r
+ * XTEA encrypt function\r
+ */\r
+int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode,\r
+                    const unsigned char input[8], unsigned char output[8])\r
+{\r
+    uint32_t *k, v0, v1, i;\r
+\r
+    k = ctx->k;\r
+\r
+    GET_UINT32_BE( v0, input, 0 );\r
+    GET_UINT32_BE( v1, input, 4 );\r
+\r
+    if( mode == MBEDTLS_XTEA_ENCRYPT )\r
+    {\r
+        uint32_t sum = 0, delta = 0x9E3779B9;\r
+\r
+        for( i = 0; i < 32; i++ )\r
+        {\r
+            v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);\r
+            sum += delta;\r
+            v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);\r
+        }\r
+    }\r
+    else /* MBEDTLS_XTEA_DECRYPT */\r
+    {\r
+        uint32_t delta = 0x9E3779B9, sum = delta * 32;\r
+\r
+        for( i = 0; i < 32; i++ )\r
+        {\r
+            v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);\r
+            sum -= delta;\r
+            v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);\r
+        }\r
+    }\r
+\r
+    PUT_UINT32_BE( v0, output, 0 );\r
+    PUT_UINT32_BE( v1, output, 4 );\r
+\r
+    return( 0 );\r
+}\r
+\r
+#if defined(MBEDTLS_CIPHER_MODE_CBC)\r
+/*\r
+ * XTEA-CBC buffer encryption/decryption\r
+ */\r
+int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length,\r
+                    unsigned char iv[8], const unsigned char *input,\r
+                    unsigned char *output)\r
+{\r
+    int i;\r
+    unsigned char temp[8];\r
+\r
+    if( length % 8 )\r
+        return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH );\r
+\r
+    if( mode == MBEDTLS_XTEA_DECRYPT )\r
+    {\r
+        while( length > 0 )\r
+        {\r
+            memcpy( temp, input, 8 );\r
+            mbedtls_xtea_crypt_ecb( ctx, mode, input, output );\r
+\r
+            for( i = 0; i < 8; i++ )\r
+                output[i] = (unsigned char)( output[i] ^ iv[i] );\r
+\r
+            memcpy( iv, temp, 8 );\r
+\r
+            input  += 8;\r
+            output += 8;\r
+            length -= 8;\r
+        }\r
+    }\r
+    else\r
+    {\r
+        while( length > 0 )\r
+        {\r
+            for( i = 0; i < 8; i++ )\r
+                output[i] = (unsigned char)( input[i] ^ iv[i] );\r
+\r
+            mbedtls_xtea_crypt_ecb( ctx, mode, output, output );\r
+            memcpy( iv, output, 8 );\r
+\r
+            input  += 8;\r
+            output += 8;\r
+            length -= 8;\r
+        }\r
+    }\r
+\r
+    return( 0 );\r
+}\r
+#endif /* MBEDTLS_CIPHER_MODE_CBC */\r
+#endif /* !MBEDTLS_XTEA_ALT */\r
+\r
+#if defined(MBEDTLS_SELF_TEST)\r
+\r
+/*\r
+ * XTEA tests vectors (non-official)\r
+ */\r
+\r
+static const unsigned char xtea_test_key[6][16] =\r
+{\r
+   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,\r
+     0x0c, 0x0d, 0x0e, 0x0f },\r
+   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,\r
+     0x0c, 0x0d, 0x0e, 0x0f },\r
+   { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,\r
+     0x0c, 0x0d, 0x0e, 0x0f },\r
+   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
+     0x00, 0x00, 0x00, 0x00 },\r
+   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
+     0x00, 0x00, 0x00, 0x00 },\r
+   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\r
+     0x00, 0x00, 0x00, 0x00 }\r
+};\r
+\r
+static const unsigned char xtea_test_pt[6][8] =\r
+{\r
+    { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },\r
+    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },\r
+    { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },\r
+    { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },\r
+    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },\r
+    { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }\r
+};\r
+\r
+static const unsigned char xtea_test_ct[6][8] =\r
+{\r
+    { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },\r
+    { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },\r
+    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },\r
+    { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },\r
+    { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },\r
+    { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }\r
+};\r
+\r
+/*\r
+ * Checkup routine\r
+ */\r
+int mbedtls_xtea_self_test( int verbose )\r
+{\r
+    int i, ret = 0;\r
+    unsigned char buf[8];\r
+    mbedtls_xtea_context ctx;\r
+\r
+    mbedtls_xtea_init( &ctx );\r
+    for( i = 0; i < 6; i++ )\r
+    {\r
+        if( verbose != 0 )\r
+            mbedtls_printf( "  XTEA test #%d: ", i + 1 );\r
+\r
+        memcpy( buf, xtea_test_pt[i], 8 );\r
+\r
+        mbedtls_xtea_setup( &ctx, xtea_test_key[i] );\r
+        mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf );\r
+\r
+        if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 )\r
+        {\r
+            if( verbose != 0 )\r
+                mbedtls_printf( "failed\n" );\r
+\r
+            ret = 1;\r
+            goto exit;\r
+        }\r
+\r
+        if( verbose != 0 )\r
+            mbedtls_printf( "passed\n" );\r
+    }\r
+\r
+    if( verbose != 0 )\r
+        mbedtls_printf( "\n" );\r
+\r
+exit:\r
+    mbedtls_xtea_free( &ctx );\r
+\r
+    return( ret );\r
+}\r
+\r
+#endif /* MBEDTLS_SELF_TEST */\r
+\r
+#endif /* MBEDTLS_XTEA_C */\r