blob: 27651cc0e53939179359899d393b848c631375c0 [file] [log] [blame]
Paul Bakker7a7c78f2009-01-04 18:15:48 +00001/*
Tom Cosgrove5205c972022-07-28 06:12:08 +01002 * A 32-bit implementation of the XTEA algorithm
Paul Bakker7a7c78f2009-01-04 18:15:48 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7a7c78f2009-01-04 18:15:48 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_XTEA_C)
Paul Bakker7a7c78f2009-01-04 18:15:48 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/xtea.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050025#include "mbedtls/platform_util.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
28
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_XTEA_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020032
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033void mbedtls_xtea_init(mbedtls_xtea_context *ctx)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020034{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 memset(ctx, 0, sizeof(mbedtls_xtea_context));
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020036}
37
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038void mbedtls_xtea_free(mbedtls_xtea_context *ctx)
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020039{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010040 if (ctx == NULL) {
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020041 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042 }
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_xtea_context));
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020045}
46
Paul Bakker7a7c78f2009-01-04 18:15:48 +000047/*
48 * XTEA key schedule
49 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050void mbedtls_xtea_setup(mbedtls_xtea_context *ctx, const unsigned char key[16])
Paul Bakker7a7c78f2009-01-04 18:15:48 +000051{
52 int i;
53
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 memset(ctx, 0, sizeof(mbedtls_xtea_context));
Paul Bakker7a7c78f2009-01-04 18:15:48 +000055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 for (i = 0; i < 4; i++) {
57 ctx->k[i] = MBEDTLS_GET_UINT32_BE(key, i << 2);
Paul Bakker7a7c78f2009-01-04 18:15:48 +000058 }
59}
60
61/*
62 * XTEA encrypt function
63 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064int mbedtls_xtea_crypt_ecb(mbedtls_xtea_context *ctx, int mode,
65 const unsigned char input[8], unsigned char output[8])
Paul Bakker7a7c78f2009-01-04 18:15:48 +000066{
Paul Bakker0fdf3ca2009-05-03 12:54:07 +000067 uint32_t *k, v0, v1, i;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000068
69 k = ctx->k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +020070
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 v0 = MBEDTLS_GET_UINT32_BE(input, 0);
72 v1 = MBEDTLS_GET_UINT32_BE(input, 4);
Paul Bakker7a7c78f2009-01-04 18:15:48 +000073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 if (mode == MBEDTLS_XTEA_ENCRYPT) {
Paul Bakker23986e52011-04-24 08:57:21 +000075 uint32_t sum = 0, delta = 0x9E3779B9;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 for (i = 0; i < 32; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +000078 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
79 sum += delta;
80 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
81 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 } else { /* MBEDTLS_XTEA_DECRYPT */
Paul Bakker23986e52011-04-24 08:57:21 +000083 uint32_t delta = 0x9E3779B9, sum = delta * 32;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 for (i = 0; i < 32; i++) {
Paul Bakker23986e52011-04-24 08:57:21 +000086 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
87 sum -= delta;
88 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
89 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +000090 }
91
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 MBEDTLS_PUT_UINT32_BE(v0, output, 0);
93 MBEDTLS_PUT_UINT32_BE(v1, output, 4);
Paul Bakkerf3ccc682010-03-18 21:21:02 +000094
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 return 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +000096}
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +000099/*
100 * XTEA-CBC buffer encryption/decryption
101 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102int mbedtls_xtea_crypt_cbc(mbedtls_xtea_context *ctx, int mode, size_t length,
103 unsigned char iv[8], const unsigned char *input,
104 unsigned char *output)
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000105{
106 int i;
107 unsigned char temp[8];
108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 if (length % 8) {
110 return MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH;
111 }
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if (mode == MBEDTLS_XTEA_DECRYPT) {
114 while (length > 0) {
115 memcpy(temp, input, 8);
116 mbedtls_xtea_crypt_ecb(ctx, mode, input, output);
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 for (i = 0; i < 8; i++) {
119 output[i] = (unsigned char) (output[i] ^ iv[i]);
120 }
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 memcpy(iv, temp, 8);
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000123
124 input += 8;
125 output += 8;
126 length -= 8;
127 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 } else {
129 while (length > 0) {
130 for (i = 0; i < 8; i++) {
131 output[i] = (unsigned char) (input[i] ^ iv[i]);
132 }
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 mbedtls_xtea_crypt_ecb(ctx, mode, output, output);
135 memcpy(iv, output, 8);
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200136
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000137 input += 8;
138 output += 8;
139 length -= 8;
140 }
141 }
142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 return 0;
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#endif /* MBEDTLS_CIPHER_MODE_CBC */
146#endif /* !MBEDTLS_XTEA_ALT */
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_SELF_TEST)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000149
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000150/*
151 * XTEA tests vectors (non-official)
152 */
153
154static const unsigned char xtea_test_key[6][16] =
155{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
157 0x0c, 0x0d, 0x0e, 0x0f },
158 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
159 0x0c, 0x0d, 0x0e, 0x0f },
160 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
161 0x0c, 0x0d, 0x0e, 0x0f },
162 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163 0x00, 0x00, 0x00, 0x00 },
164 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165 0x00, 0x00, 0x00, 0x00 },
166 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167 0x00, 0x00, 0x00, 0x00 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000168};
169
170static const unsigned char xtea_test_pt[6][8] =
171{
172 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
173 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
174 { 0x5a, 0x5b, 0x6e, 0x27, 0x89, 0x48, 0xd7, 0x7f },
175 { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48 },
176 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
177 { 0x70, 0xe1, 0x22, 0x5d, 0x6e, 0x4e, 0x76, 0x55 }
178};
179
180static const unsigned char xtea_test_ct[6][8] =
181{
182 { 0x49, 0x7d, 0xf3, 0xd0, 0x72, 0x61, 0x2c, 0xb5 },
183 { 0xe7, 0x8f, 0x2d, 0x13, 0x74, 0x43, 0x41, 0xd8 },
184 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 },
185 { 0xa0, 0x39, 0x05, 0x89, 0xf8, 0xb8, 0xef, 0xa5 },
186 { 0xed, 0x23, 0x37, 0x5a, 0x82, 0x1a, 0x8c, 0x2d },
187 { 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
188};
189
190/*
191 * Checkup routine
192 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193int mbedtls_xtea_self_test(int verbose)
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000194{
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200195 int i, ret = 0;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000196 unsigned char buf[8];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_xtea_context ctx;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 mbedtls_xtea_init(&ctx);
200 for (i = 0; i < 6; i++) {
201 if (verbose != 0) {
202 mbedtls_printf(" XTEA test #%d: ", i + 1);
203 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 memcpy(buf, xtea_test_pt[i], 8);
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 mbedtls_xtea_setup(&ctx, xtea_test_key[i]);
208 mbedtls_xtea_crypt_ecb(&ctx, MBEDTLS_XTEA_ENCRYPT, buf, buf);
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (memcmp(buf, xtea_test_ct[i], 8) != 0) {
211 if (verbose != 0) {
212 mbedtls_printf("failed\n");
213 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000214
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200215 ret = 1;
216 goto exit;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000217 }
218
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 if (verbose != 0) {
220 mbedtls_printf("passed\n");
221 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000222 }
223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 if (verbose != 0) {
225 mbedtls_printf("\n");
226 }
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000227
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200228exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 mbedtls_xtea_free(&ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return ret;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#endif /* MBEDTLS_SELF_TEST */
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_XTEA_C */