}
static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
- int decrypt)
+ int decrypt, uint8_t *iv)
{
uint32_t v0, v1;
int i;
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
}
+ if (iv) {
+ v0 ^= AV_RB32(iv);
+ v1 ^= AV_RB32(iv + 4);
+ memcpy(iv, src, 8);
+ }
} else {
uint32_t sum = 0, delta = 0x9E3779B9;
int i;
if (decrypt) {
- while (count > 0) {
- xtea_crypt_ecb(ctx, dst, src, decrypt);
-
- if (iv) {
- for (i = 0; i < 8; i++)
- dst[i] = dst[i] ^ iv[i];
- memcpy(iv, src, 8);
- }
+ while (count--) {
+ xtea_crypt_ecb(ctx, dst, src, decrypt, iv);
src += 8;
dst += 8;
- count -= 8;
}
} else {
- while (count > 0) {
+ while (count--) {
if (iv) {
for (i = 0; i < 8; i++)
dst[i] = src[i] ^ iv[i];
- xtea_crypt_ecb(ctx, dst, dst, decrypt);
+ xtea_crypt_ecb(ctx, dst, dst, decrypt, NULL);
memcpy(iv, dst, 8);
} else {
- xtea_crypt_ecb(ctx, dst, src, decrypt);
+ xtea_crypt_ecb(ctx, dst, src, decrypt, NULL);
}
src += 8;
dst += 8;
- count -= 8;
}
}
}
{ 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 }
};
+#undef exit
+static void test_xtea(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
+ const uint8_t *ref, int len, uint8_t *iv, int dir,
+ const char *test)
+{
+ av_xtea_crypt(ctx, dst, src, len, iv, dir);
+ if (memcmp(dst, ref, 8*len)) {
+ int i;
+ printf("%s failed\ngot ", test);
+ for (i = 0; i < 8*len; i++)
+ printf("%02x ", dst[i]);
+ printf("\nexpected ");
+ for (i = 0; i < 8*len; i++)
+ printf("%02x ", ref[i]);
+ printf("\n");
+ exit(1);
+ }
+}
+
int main(void)
{
AVXTEA ctx;
- uint8_t buf[8];
+ uint8_t buf[8], iv[8];
int i;
+ const uint8_t src[32] = "HelloWorldHelloWorldHelloWorld";
+ uint8_t ct[32];
+ uint8_t pl[32];
for (i = 0; i < XTEA_NUM_TESTS; i++) {
av_xtea_init(&ctx, xtea_test_key[i]);
- av_xtea_crypt(&ctx, buf, xtea_test_pt[i], 1, NULL, 0);
- if (memcmp(buf, xtea_test_ct[i], 8)) {
- printf("Test encryption failed.\n");
- return 1;
- }
+ test_xtea(&ctx, buf, xtea_test_pt[i], xtea_test_ct[i], 1, NULL, 0, "encryption");
+ test_xtea(&ctx, buf, xtea_test_ct[i], xtea_test_pt[i], 1, NULL, 1, "decryption");
- av_xtea_crypt(&ctx, buf, xtea_test_ct[i], 1, NULL, 1);
- if (memcmp(buf, xtea_test_pt[i], 8)) {
- printf("Test decryption failed.\n");
- return 1;
- }
+ /* encrypt */
+ memcpy(iv, "HALLO123", 8);
+ av_xtea_crypt(&ctx, ct, src, 4, iv, 0);
+
+ /* decrypt into pl */
+ memcpy(iv, "HALLO123", 8);
+ test_xtea(&ctx, pl, ct, src, 4, iv, 1, "CBC decryption");
+
+ memcpy(iv, "HALLO123", 8);
+ test_xtea(&ctx, ct, ct, src, 4, iv, 1, "CBC inplace decryption");
}
printf("Test encryption/decryption success.\n");