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