Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/features/mbedtls/src/xtea.c@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* |
marcozecchini | 0:9fca2b23d0ba | 2 | * An 32-bit implementation of the XTEA algorithm |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
marcozecchini | 0:9fca2b23d0ba | 5 | * SPDX-License-Identifier: Apache-2.0 |
marcozecchini | 0:9fca2b23d0ba | 6 | * |
marcozecchini | 0:9fca2b23d0ba | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
marcozecchini | 0:9fca2b23d0ba | 8 | * not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 9 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 10 | * |
marcozecchini | 0:9fca2b23d0ba | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 12 | * |
marcozecchini | 0:9fca2b23d0ba | 13 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
marcozecchini | 0:9fca2b23d0ba | 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 16 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 17 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 18 | * |
marcozecchini | 0:9fca2b23d0ba | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
marcozecchini | 0:9fca2b23d0ba | 20 | */ |
marcozecchini | 0:9fca2b23d0ba | 21 | |
marcozecchini | 0:9fca2b23d0ba | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
marcozecchini | 0:9fca2b23d0ba | 23 | #include "mbedtls/config.h" |
marcozecchini | 0:9fca2b23d0ba | 24 | #else |
marcozecchini | 0:9fca2b23d0ba | 25 | #include MBEDTLS_CONFIG_FILE |
marcozecchini | 0:9fca2b23d0ba | 26 | #endif |
marcozecchini | 0:9fca2b23d0ba | 27 | |
marcozecchini | 0:9fca2b23d0ba | 28 | #if defined(MBEDTLS_XTEA_C) |
marcozecchini | 0:9fca2b23d0ba | 29 | |
marcozecchini | 0:9fca2b23d0ba | 30 | #include "mbedtls/xtea.h" |
marcozecchini | 0:9fca2b23d0ba | 31 | |
marcozecchini | 0:9fca2b23d0ba | 32 | #include <string.h> |
marcozecchini | 0:9fca2b23d0ba | 33 | |
marcozecchini | 0:9fca2b23d0ba | 34 | #if defined(MBEDTLS_SELF_TEST) |
marcozecchini | 0:9fca2b23d0ba | 35 | #if defined(MBEDTLS_PLATFORM_C) |
marcozecchini | 0:9fca2b23d0ba | 36 | #include "mbedtls/platform.h" |
marcozecchini | 0:9fca2b23d0ba | 37 | #else |
marcozecchini | 0:9fca2b23d0ba | 38 | #include <stdio.h> |
marcozecchini | 0:9fca2b23d0ba | 39 | #define mbedtls_printf printf |
marcozecchini | 0:9fca2b23d0ba | 40 | #endif /* MBEDTLS_PLATFORM_C */ |
marcozecchini | 0:9fca2b23d0ba | 41 | #endif /* MBEDTLS_SELF_TEST */ |
marcozecchini | 0:9fca2b23d0ba | 42 | |
marcozecchini | 0:9fca2b23d0ba | 43 | #if !defined(MBEDTLS_XTEA_ALT) |
marcozecchini | 0:9fca2b23d0ba | 44 | |
marcozecchini | 0:9fca2b23d0ba | 45 | /* Implementation that should never be optimized out by the compiler */ |
marcozecchini | 0:9fca2b23d0ba | 46 | static void mbedtls_zeroize( void *v, size_t n ) { |
marcozecchini | 0:9fca2b23d0ba | 47 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; |
marcozecchini | 0:9fca2b23d0ba | 48 | } |
marcozecchini | 0:9fca2b23d0ba | 49 | |
marcozecchini | 0:9fca2b23d0ba | 50 | /* |
marcozecchini | 0:9fca2b23d0ba | 51 | * 32-bit integer manipulation macros (big endian) |
marcozecchini | 0:9fca2b23d0ba | 52 | */ |
marcozecchini | 0:9fca2b23d0ba | 53 | #ifndef GET_UINT32_BE |
marcozecchini | 0:9fca2b23d0ba | 54 | #define GET_UINT32_BE(n,b,i) \ |
marcozecchini | 0:9fca2b23d0ba | 55 | { \ |
marcozecchini | 0:9fca2b23d0ba | 56 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ |
marcozecchini | 0:9fca2b23d0ba | 57 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ |
marcozecchini | 0:9fca2b23d0ba | 58 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ |
marcozecchini | 0:9fca2b23d0ba | 59 | | ( (uint32_t) (b)[(i) + 3] ); \ |
marcozecchini | 0:9fca2b23d0ba | 60 | } |
marcozecchini | 0:9fca2b23d0ba | 61 | #endif |
marcozecchini | 0:9fca2b23d0ba | 62 | |
marcozecchini | 0:9fca2b23d0ba | 63 | #ifndef PUT_UINT32_BE |
marcozecchini | 0:9fca2b23d0ba | 64 | #define PUT_UINT32_BE(n,b,i) \ |
marcozecchini | 0:9fca2b23d0ba | 65 | { \ |
marcozecchini | 0:9fca2b23d0ba | 66 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ |
marcozecchini | 0:9fca2b23d0ba | 67 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ |
marcozecchini | 0:9fca2b23d0ba | 68 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ |
marcozecchini | 0:9fca2b23d0ba | 69 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ |
marcozecchini | 0:9fca2b23d0ba | 70 | } |
marcozecchini | 0:9fca2b23d0ba | 71 | #endif |
marcozecchini | 0:9fca2b23d0ba | 72 | |
marcozecchini | 0:9fca2b23d0ba | 73 | void mbedtls_xtea_init( mbedtls_xtea_context *ctx ) |
marcozecchini | 0:9fca2b23d0ba | 74 | { |
marcozecchini | 0:9fca2b23d0ba | 75 | memset( ctx, 0, sizeof( mbedtls_xtea_context ) ); |
marcozecchini | 0:9fca2b23d0ba | 76 | } |
marcozecchini | 0:9fca2b23d0ba | 77 | |
marcozecchini | 0:9fca2b23d0ba | 78 | void mbedtls_xtea_free( mbedtls_xtea_context *ctx ) |
marcozecchini | 0:9fca2b23d0ba | 79 | { |
marcozecchini | 0:9fca2b23d0ba | 80 | if( ctx == NULL ) |
marcozecchini | 0:9fca2b23d0ba | 81 | return; |
marcozecchini | 0:9fca2b23d0ba | 82 | |
marcozecchini | 0:9fca2b23d0ba | 83 | mbedtls_zeroize( ctx, sizeof( mbedtls_xtea_context ) ); |
marcozecchini | 0:9fca2b23d0ba | 84 | } |
marcozecchini | 0:9fca2b23d0ba | 85 | |
marcozecchini | 0:9fca2b23d0ba | 86 | /* |
marcozecchini | 0:9fca2b23d0ba | 87 | * XTEA key schedule |
marcozecchini | 0:9fca2b23d0ba | 88 | */ |
marcozecchini | 0:9fca2b23d0ba | 89 | void mbedtls_xtea_setup( mbedtls_xtea_context *ctx, const unsigned char key[16] ) |
marcozecchini | 0:9fca2b23d0ba | 90 | { |
marcozecchini | 0:9fca2b23d0ba | 91 | int i; |
marcozecchini | 0:9fca2b23d0ba | 92 | |
marcozecchini | 0:9fca2b23d0ba | 93 | memset( ctx, 0, sizeof(mbedtls_xtea_context) ); |
marcozecchini | 0:9fca2b23d0ba | 94 | |
marcozecchini | 0:9fca2b23d0ba | 95 | for( i = 0; i < 4; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 96 | { |
marcozecchini | 0:9fca2b23d0ba | 97 | GET_UINT32_BE( ctx->k[i], key, i << 2 ); |
marcozecchini | 0:9fca2b23d0ba | 98 | } |
marcozecchini | 0:9fca2b23d0ba | 99 | } |
marcozecchini | 0:9fca2b23d0ba | 100 | |
marcozecchini | 0:9fca2b23d0ba | 101 | /* |
marcozecchini | 0:9fca2b23d0ba | 102 | * XTEA encrypt function |
marcozecchini | 0:9fca2b23d0ba | 103 | */ |
marcozecchini | 0:9fca2b23d0ba | 104 | int mbedtls_xtea_crypt_ecb( mbedtls_xtea_context *ctx, int mode, |
marcozecchini | 0:9fca2b23d0ba | 105 | const unsigned char input[8], unsigned char output[8]) |
marcozecchini | 0:9fca2b23d0ba | 106 | { |
marcozecchini | 0:9fca2b23d0ba | 107 | uint32_t *k, v0, v1, i; |
marcozecchini | 0:9fca2b23d0ba | 108 | |
marcozecchini | 0:9fca2b23d0ba | 109 | k = ctx->k; |
marcozecchini | 0:9fca2b23d0ba | 110 | |
marcozecchini | 0:9fca2b23d0ba | 111 | GET_UINT32_BE( v0, input, 0 ); |
marcozecchini | 0:9fca2b23d0ba | 112 | GET_UINT32_BE( v1, input, 4 ); |
marcozecchini | 0:9fca2b23d0ba | 113 | |
marcozecchini | 0:9fca2b23d0ba | 114 | if( mode == MBEDTLS_XTEA_ENCRYPT ) |
marcozecchini | 0:9fca2b23d0ba | 115 | { |
marcozecchini | 0:9fca2b23d0ba | 116 | uint32_t sum = 0, delta = 0x9E3779B9; |
marcozecchini | 0:9fca2b23d0ba | 117 | |
marcozecchini | 0:9fca2b23d0ba | 118 | for( i = 0; i < 32; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 119 | { |
marcozecchini | 0:9fca2b23d0ba | 120 | v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
marcozecchini | 0:9fca2b23d0ba | 121 | sum += delta; |
marcozecchini | 0:9fca2b23d0ba | 122 | v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); |
marcozecchini | 0:9fca2b23d0ba | 123 | } |
marcozecchini | 0:9fca2b23d0ba | 124 | } |
marcozecchini | 0:9fca2b23d0ba | 125 | else /* MBEDTLS_XTEA_DECRYPT */ |
marcozecchini | 0:9fca2b23d0ba | 126 | { |
marcozecchini | 0:9fca2b23d0ba | 127 | uint32_t delta = 0x9E3779B9, sum = delta * 32; |
marcozecchini | 0:9fca2b23d0ba | 128 | |
marcozecchini | 0:9fca2b23d0ba | 129 | for( i = 0; i < 32; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 130 | { |
marcozecchini | 0:9fca2b23d0ba | 131 | v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); |
marcozecchini | 0:9fca2b23d0ba | 132 | sum -= delta; |
marcozecchini | 0:9fca2b23d0ba | 133 | v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
marcozecchini | 0:9fca2b23d0ba | 134 | } |
marcozecchini | 0:9fca2b23d0ba | 135 | } |
marcozecchini | 0:9fca2b23d0ba | 136 | |
marcozecchini | 0:9fca2b23d0ba | 137 | PUT_UINT32_BE( v0, output, 0 ); |
marcozecchini | 0:9fca2b23d0ba | 138 | PUT_UINT32_BE( v1, output, 4 ); |
marcozecchini | 0:9fca2b23d0ba | 139 | |
marcozecchini | 0:9fca2b23d0ba | 140 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 141 | } |
marcozecchini | 0:9fca2b23d0ba | 142 | |
marcozecchini | 0:9fca2b23d0ba | 143 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
marcozecchini | 0:9fca2b23d0ba | 144 | /* |
marcozecchini | 0:9fca2b23d0ba | 145 | * XTEA-CBC buffer encryption/decryption |
marcozecchini | 0:9fca2b23d0ba | 146 | */ |
marcozecchini | 0:9fca2b23d0ba | 147 | int mbedtls_xtea_crypt_cbc( mbedtls_xtea_context *ctx, int mode, size_t length, |
marcozecchini | 0:9fca2b23d0ba | 148 | unsigned char iv[8], const unsigned char *input, |
marcozecchini | 0:9fca2b23d0ba | 149 | unsigned char *output) |
marcozecchini | 0:9fca2b23d0ba | 150 | { |
marcozecchini | 0:9fca2b23d0ba | 151 | int i; |
marcozecchini | 0:9fca2b23d0ba | 152 | unsigned char temp[8]; |
marcozecchini | 0:9fca2b23d0ba | 153 | |
marcozecchini | 0:9fca2b23d0ba | 154 | if( length % 8 ) |
marcozecchini | 0:9fca2b23d0ba | 155 | return( MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH ); |
marcozecchini | 0:9fca2b23d0ba | 156 | |
marcozecchini | 0:9fca2b23d0ba | 157 | if( mode == MBEDTLS_XTEA_DECRYPT ) |
marcozecchini | 0:9fca2b23d0ba | 158 | { |
marcozecchini | 0:9fca2b23d0ba | 159 | while( length > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 160 | { |
marcozecchini | 0:9fca2b23d0ba | 161 | memcpy( temp, input, 8 ); |
marcozecchini | 0:9fca2b23d0ba | 162 | mbedtls_xtea_crypt_ecb( ctx, mode, input, output ); |
marcozecchini | 0:9fca2b23d0ba | 163 | |
marcozecchini | 0:9fca2b23d0ba | 164 | for( i = 0; i < 8; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 165 | output[i] = (unsigned char)( output[i] ^ iv[i] ); |
marcozecchini | 0:9fca2b23d0ba | 166 | |
marcozecchini | 0:9fca2b23d0ba | 167 | memcpy( iv, temp, 8 ); |
marcozecchini | 0:9fca2b23d0ba | 168 | |
marcozecchini | 0:9fca2b23d0ba | 169 | input += 8; |
marcozecchini | 0:9fca2b23d0ba | 170 | output += 8; |
marcozecchini | 0:9fca2b23d0ba | 171 | length -= 8; |
marcozecchini | 0:9fca2b23d0ba | 172 | } |
marcozecchini | 0:9fca2b23d0ba | 173 | } |
marcozecchini | 0:9fca2b23d0ba | 174 | else |
marcozecchini | 0:9fca2b23d0ba | 175 | { |
marcozecchini | 0:9fca2b23d0ba | 176 | while( length > 0 ) |
marcozecchini | 0:9fca2b23d0ba | 177 | { |
marcozecchini | 0:9fca2b23d0ba | 178 | for( i = 0; i < 8; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 179 | output[i] = (unsigned char)( input[i] ^ iv[i] ); |
marcozecchini | 0:9fca2b23d0ba | 180 | |
marcozecchini | 0:9fca2b23d0ba | 181 | mbedtls_xtea_crypt_ecb( ctx, mode, output, output ); |
marcozecchini | 0:9fca2b23d0ba | 182 | memcpy( iv, output, 8 ); |
marcozecchini | 0:9fca2b23d0ba | 183 | |
marcozecchini | 0:9fca2b23d0ba | 184 | input += 8; |
marcozecchini | 0:9fca2b23d0ba | 185 | output += 8; |
marcozecchini | 0:9fca2b23d0ba | 186 | length -= 8; |
marcozecchini | 0:9fca2b23d0ba | 187 | } |
marcozecchini | 0:9fca2b23d0ba | 188 | } |
marcozecchini | 0:9fca2b23d0ba | 189 | |
marcozecchini | 0:9fca2b23d0ba | 190 | return( 0 ); |
marcozecchini | 0:9fca2b23d0ba | 191 | } |
marcozecchini | 0:9fca2b23d0ba | 192 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
marcozecchini | 0:9fca2b23d0ba | 193 | #endif /* !MBEDTLS_XTEA_ALT */ |
marcozecchini | 0:9fca2b23d0ba | 194 | |
marcozecchini | 0:9fca2b23d0ba | 195 | #if defined(MBEDTLS_SELF_TEST) |
marcozecchini | 0:9fca2b23d0ba | 196 | |
marcozecchini | 0:9fca2b23d0ba | 197 | /* |
marcozecchini | 0:9fca2b23d0ba | 198 | * XTEA tests vectors (non-official) |
marcozecchini | 0:9fca2b23d0ba | 199 | */ |
marcozecchini | 0:9fca2b23d0ba | 200 | |
marcozecchini | 0:9fca2b23d0ba | 201 | static const unsigned char xtea_test_key[6][16] = |
marcozecchini | 0:9fca2b23d0ba | 202 | { |
marcozecchini | 0:9fca2b23d0ba | 203 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
marcozecchini | 0:9fca2b23d0ba | 204 | 0x0c, 0x0d, 0x0e, 0x0f }, |
marcozecchini | 0:9fca2b23d0ba | 205 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
marcozecchini | 0:9fca2b23d0ba | 206 | 0x0c, 0x0d, 0x0e, 0x0f }, |
marcozecchini | 0:9fca2b23d0ba | 207 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
marcozecchini | 0:9fca2b23d0ba | 208 | 0x0c, 0x0d, 0x0e, 0x0f }, |
marcozecchini | 0:9fca2b23d0ba | 209 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
marcozecchini | 0:9fca2b23d0ba | 210 | 0x00, 0x00, 0x00, 0x00 }, |
marcozecchini | 0:9fca2b23d0ba | 211 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
marcozecchini | 0:9fca2b23d0ba | 212 | 0x00, 0x00, 0x00, 0x00 }, |
marcozecchini | 0:9fca2b23d0ba | 213 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
marcozecchini | 0:9fca2b23d0ba | 214 | 0x00, 0x00, 0x00, 0x00 } |
marcozecchini | 0:9fca2b23d0ba | 215 | }; |
marcozecchini | 0:9fca2b23d0ba | 216 | |
marcozecchini | 0:9fca2b23d0ba | 217 | static const unsigned char xtea_test_pt[6][8] = |
marcozecchini | 0:9fca2b23d0ba | 218 | { |
marcozecchini | 0:9fca2b23d0ba | 219 | { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, |
marcozecchini | 0:9fca2b23d0ba | 220 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
marcozecchini | 0:9fca2b23d0ba | 221 | { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, |
marcozecchini | 0:9fca2b23d0ba | 222 | { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, |
marcozecchini | 0:9fca2b23d0ba | 223 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
marcozecchini | 0:9fca2b23d0ba | 224 | { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } |
marcozecchini | 0:9fca2b23d0ba | 225 | }; |
marcozecchini | 0:9fca2b23d0ba | 226 | |
marcozecchini | 0:9fca2b23d0ba | 227 | static const unsigned char xtea_test_ct[6][8] = |
marcozecchini | 0:9fca2b23d0ba | 228 | { |
marcozecchini | 0:9fca2b23d0ba | 229 | { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, |
marcozecchini | 0:9fca2b23d0ba | 230 | { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, |
marcozecchini | 0:9fca2b23d0ba | 231 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
marcozecchini | 0:9fca2b23d0ba | 232 | { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, |
marcozecchini | 0:9fca2b23d0ba | 233 | { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, |
marcozecchini | 0:9fca2b23d0ba | 234 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } |
marcozecchini | 0:9fca2b23d0ba | 235 | }; |
marcozecchini | 0:9fca2b23d0ba | 236 | |
marcozecchini | 0:9fca2b23d0ba | 237 | /* |
marcozecchini | 0:9fca2b23d0ba | 238 | * Checkup routine |
marcozecchini | 0:9fca2b23d0ba | 239 | */ |
marcozecchini | 0:9fca2b23d0ba | 240 | int mbedtls_xtea_self_test( int verbose ) |
marcozecchini | 0:9fca2b23d0ba | 241 | { |
marcozecchini | 0:9fca2b23d0ba | 242 | int i, ret = 0; |
marcozecchini | 0:9fca2b23d0ba | 243 | unsigned char buf[8]; |
marcozecchini | 0:9fca2b23d0ba | 244 | mbedtls_xtea_context ctx; |
marcozecchini | 0:9fca2b23d0ba | 245 | |
marcozecchini | 0:9fca2b23d0ba | 246 | mbedtls_xtea_init( &ctx ); |
marcozecchini | 0:9fca2b23d0ba | 247 | for( i = 0; i < 6; i++ ) |
marcozecchini | 0:9fca2b23d0ba | 248 | { |
marcozecchini | 0:9fca2b23d0ba | 249 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 250 | mbedtls_printf( " XTEA test #%d: ", i + 1 ); |
marcozecchini | 0:9fca2b23d0ba | 251 | |
marcozecchini | 0:9fca2b23d0ba | 252 | memcpy( buf, xtea_test_pt[i], 8 ); |
marcozecchini | 0:9fca2b23d0ba | 253 | |
marcozecchini | 0:9fca2b23d0ba | 254 | mbedtls_xtea_setup( &ctx, xtea_test_key[i] ); |
marcozecchini | 0:9fca2b23d0ba | 255 | mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf ); |
marcozecchini | 0:9fca2b23d0ba | 256 | |
marcozecchini | 0:9fca2b23d0ba | 257 | if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 258 | { |
marcozecchini | 0:9fca2b23d0ba | 259 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 260 | mbedtls_printf( "failed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 261 | |
marcozecchini | 0:9fca2b23d0ba | 262 | ret = 1; |
marcozecchini | 0:9fca2b23d0ba | 263 | goto exit; |
marcozecchini | 0:9fca2b23d0ba | 264 | } |
marcozecchini | 0:9fca2b23d0ba | 265 | |
marcozecchini | 0:9fca2b23d0ba | 266 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 267 | mbedtls_printf( "passed\n" ); |
marcozecchini | 0:9fca2b23d0ba | 268 | } |
marcozecchini | 0:9fca2b23d0ba | 269 | |
marcozecchini | 0:9fca2b23d0ba | 270 | if( verbose != 0 ) |
marcozecchini | 0:9fca2b23d0ba | 271 | mbedtls_printf( "\n" ); |
marcozecchini | 0:9fca2b23d0ba | 272 | |
marcozecchini | 0:9fca2b23d0ba | 273 | exit: |
marcozecchini | 0:9fca2b23d0ba | 274 | mbedtls_xtea_free( &ctx ); |
marcozecchini | 0:9fca2b23d0ba | 275 | |
marcozecchini | 0:9fca2b23d0ba | 276 | return( ret ); |
marcozecchini | 0:9fca2b23d0ba | 277 | } |
marcozecchini | 0:9fca2b23d0ba | 278 | |
marcozecchini | 0:9fca2b23d0ba | 279 | #endif /* MBEDTLS_SELF_TEST */ |
marcozecchini | 0:9fca2b23d0ba | 280 | |
marcozecchini | 0:9fca2b23d0ba | 281 | #endif /* MBEDTLS_XTEA_C */ |