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