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