Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 1 | /* |
Tom Cosgrove | 5205c97 | 2022-07-28 06:12:08 +0100 | [diff] [blame] | 2 | * A 32-bit implementation of the XTEA algorithm |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 8 | #include "common.h" |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_XTEA_C) |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/xtea.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 13 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 14 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 17 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | #if !defined(MBEDTLS_XTEA_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 20 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 21 | void mbedtls_xtea_init(mbedtls_xtea_context *ctx) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 22 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 23 | memset(ctx, 0, sizeof(mbedtls_xtea_context)); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 24 | } |
| 25 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 26 | void mbedtls_xtea_free(mbedtls_xtea_context *ctx) |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 27 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 28 | if (ctx == NULL) { |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 29 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 30 | } |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 31 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 32 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_xtea_context)); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 35 | /* |
| 36 | * XTEA key schedule |
| 37 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 38 | void mbedtls_xtea_setup(mbedtls_xtea_context *ctx, const unsigned char key[16]) |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 39 | { |
| 40 | int i; |
| 41 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 42 | memset(ctx, 0, sizeof(mbedtls_xtea_context)); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 43 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | for (i = 0; i < 4; i++) { |
| 45 | ctx->k[i] = MBEDTLS_GET_UINT32_BE(key, i << 2); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * XTEA encrypt function |
| 51 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | int mbedtls_xtea_crypt_ecb(mbedtls_xtea_context *ctx, int mode, |
| 53 | const unsigned char input[8], unsigned char output[8]) |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 54 | { |
Paul Bakker | 0fdf3ca | 2009-05-03 12:54:07 +0000 | [diff] [blame] | 55 | uint32_t *k, v0, v1, i; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 56 | |
| 57 | k = ctx->k; |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 58 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 59 | v0 = MBEDTLS_GET_UINT32_BE(input, 0); |
| 60 | v1 = MBEDTLS_GET_UINT32_BE(input, 4); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 61 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 62 | if (mode == MBEDTLS_XTEA_ENCRYPT) { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 63 | uint32_t sum = 0, delta = 0x9E3779B9; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 64 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | for (i = 0; i < 32; i++) { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 66 | v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
| 67 | sum += delta; |
| 68 | v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); |
| 69 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | } else { /* MBEDTLS_XTEA_DECRYPT */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 71 | uint32_t delta = 0x9E3779B9, sum = delta * 32; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 72 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | for (i = 0; i < 32; i++) { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 74 | v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]); |
| 75 | sum -= delta; |
| 76 | v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); |
| 77 | } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | MBEDTLS_PUT_UINT32_BE(v0, output, 0); |
| 81 | MBEDTLS_PUT_UINT32_BE(v1, output, 4); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 82 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | return 0; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 87 | /* |
| 88 | * XTEA-CBC buffer encryption/decryption |
| 89 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | int mbedtls_xtea_crypt_cbc(mbedtls_xtea_context *ctx, int mode, size_t length, |
| 91 | unsigned char iv[8], const unsigned char *input, |
| 92 | unsigned char *output) |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 93 | { |
| 94 | int i; |
| 95 | unsigned char temp[8]; |
| 96 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | if (length % 8) { |
| 98 | return MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH; |
| 99 | } |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 101 | if (mode == MBEDTLS_XTEA_DECRYPT) { |
| 102 | while (length > 0) { |
| 103 | memcpy(temp, input, 8); |
| 104 | mbedtls_xtea_crypt_ecb(ctx, mode, input, output); |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 105 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | for (i = 0; i < 8; i++) { |
| 107 | output[i] = (unsigned char) (output[i] ^ iv[i]); |
| 108 | } |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 109 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 110 | memcpy(iv, temp, 8); |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 111 | |
| 112 | input += 8; |
| 113 | output += 8; |
| 114 | length -= 8; |
| 115 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | } else { |
| 117 | while (length > 0) { |
| 118 | for (i = 0; i < 8; i++) { |
| 119 | output[i] = (unsigned char) (input[i] ^ iv[i]); |
| 120 | } |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | mbedtls_xtea_crypt_ecb(ctx, mode, output, output); |
| 123 | memcpy(iv, output, 8); |
Paul Bakker | b6c5d2e | 2013-06-25 16:25:17 +0200 | [diff] [blame] | 124 | |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 125 | input += 8; |
| 126 | output += 8; |
| 127 | length -= 8; |
| 128 | } |
| 129 | } |
| 130 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | return 0; |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 132 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 134 | #endif /* !MBEDTLS_XTEA_ALT */ |
Paul Bakker | b6ecaf5 | 2011-04-19 14:29:23 +0000 | [diff] [blame] | 135 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 136 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 137 | |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 138 | /* |
| 139 | * XTEA tests vectors (non-official) |
| 140 | */ |
| 141 | |
| 142 | static const unsigned char xtea_test_key[6][16] = |
| 143 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 145 | 0x0c, 0x0d, 0x0e, 0x0f }, |
| 146 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 147 | 0x0c, 0x0d, 0x0e, 0x0f }, |
| 148 | { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, |
| 149 | 0x0c, 0x0d, 0x0e, 0x0f }, |
| 150 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 151 | 0x00, 0x00, 0x00, 0x00 }, |
| 152 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 153 | 0x00, 0x00, 0x00, 0x00 }, |
| 154 | { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 155 | 0x00, 0x00, 0x00, 0x00 } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | static const unsigned char xtea_test_pt[6][8] = |
| 159 | { |
| 160 | { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, |
| 161 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
| 162 | { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f }, |
| 163 | { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 }, |
| 164 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
| 165 | { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 } |
| 166 | }; |
| 167 | |
| 168 | static const unsigned char xtea_test_ct[6][8] = |
| 169 | { |
| 170 | { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 }, |
| 171 | { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 }, |
| 172 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }, |
| 173 | { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 }, |
| 174 | { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d }, |
| 175 | { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 } |
| 176 | }; |
| 177 | |
| 178 | /* |
| 179 | * Checkup routine |
| 180 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | int mbedtls_xtea_self_test(int verbose) |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 182 | { |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 183 | int i, ret = 0; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 184 | unsigned char buf[8]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | mbedtls_xtea_context ctx; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | mbedtls_xtea_init(&ctx); |
| 188 | for (i = 0; i < 6; i++) { |
| 189 | if (verbose != 0) { |
| 190 | mbedtls_printf(" XTEA test #%d: ", i + 1); |
| 191 | } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | memcpy(buf, xtea_test_pt[i], 8); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 194 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 195 | mbedtls_xtea_setup(&ctx, xtea_test_key[i]); |
| 196 | mbedtls_xtea_crypt_ecb(&ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf); |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 197 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | if (memcmp(buf, xtea_test_ct[i], 8) != 0) { |
| 199 | if (verbose != 0) { |
| 200 | mbedtls_printf("failed\n"); |
| 201 | } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 202 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 203 | ret = 1; |
| 204 | goto exit; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | if (verbose != 0) { |
| 208 | mbedtls_printf("passed\n"); |
| 209 | } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | if (verbose != 0) { |
| 213 | mbedtls_printf("\n"); |
| 214 | } |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 215 | |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 216 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | mbedtls_xtea_free(&ctx); |
Paul Bakker | c7ea99a | 2014-06-18 11:12:03 +0200 | [diff] [blame] | 218 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 219 | return ret; |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 222 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | #endif /* MBEDTLS_XTEA_C */ |