2 * A 32-bit implementation of the XTEA algorithm
3 * Copyright (c) 2012 Samuel Pitoiset
5 * loosely based on the implementation of David Wheeler and Roger Needham
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * @brief XTEA 32-bit implementation
27 * @author Samuel Pitoiset
33 #include "intreadwrite.h"
37 #if !FF_API_CRYPTO_CONTEXT
43 AVXTEA *av_xtea_alloc(void)
45 return av_mallocz(sizeof(struct AVXTEA));
48 void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
52 for (i = 0; i < 4; i++)
53 ctx->key[i] = AV_RB32(key + (i << 2));
56 void av_xtea_le_init(AVXTEA *ctx, const uint8_t key[16])
60 for (i = 0; i < 4; i++)
61 ctx->key[i] = AV_RL32(key + (i << 2));
64 static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
65 int decrypt, uint8_t *iv)
71 v1 = AV_RB32(src + 4);
74 uint32_t delta = 0x9E3779B9, sum = delta * 32;
76 for (i = 0; i < 32; i++) {
77 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
79 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
83 v1 ^= AV_RB32(iv + 4);
87 uint32_t sum = 0, delta = 0x9E3779B9;
89 for (i = 0; i < 32; i++) {
90 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
92 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
100 static void xtea_le_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
101 int decrypt, uint8_t *iv)
107 v1 = AV_RL32(src + 4);
110 uint32_t delta = 0x9E3779B9, sum = delta * 32;
112 for (i = 0; i < 32; i++) {
113 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
115 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
119 v1 ^= AV_RL32(iv + 4);
123 uint32_t sum = 0, delta = 0x9E3779B9;
125 for (i = 0; i < 32; i++) {
126 v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
128 v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
133 AV_WL32(dst + 4, v1);
136 static void xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
137 uint8_t *iv, int decrypt,
138 void (*crypt)(AVXTEA *, uint8_t *, const uint8_t *, int, uint8_t *))
144 crypt(ctx, dst, src, decrypt, iv);
152 for (i = 0; i < 8; i++)
153 dst[i] = src[i] ^ iv[i];
154 crypt(ctx, dst, dst, decrypt, NULL);
157 crypt(ctx, dst, src, decrypt, NULL);
165 void av_xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
166 uint8_t *iv, int decrypt)
168 xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_crypt_ecb);
171 void av_xtea_le_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
172 uint8_t *iv, int decrypt)
174 xtea_crypt(ctx, dst, src, count, iv, decrypt, xtea_le_crypt_ecb);