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