3 * $Id: xtea.c,v 1.1 2000/07/15 13:44:31 mdw Exp $
5 * The Extended Tiny Encryption Algorithm
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.1 2000/07/15 13:44:31 mdw
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
47 /*----- Global variables --------------------------------------------------*/
49 const octet xtea_keysz[] = { KSZ_RANGE, XTEA_KEYSZ, 0, 16, 1 };
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @xtea_init@ --- *
55 * Arguments: @xtea_ctx *k@ = pointer to key block
56 * @const void *buf@ = pointer to key buffer
57 * @size_t sz@ = size of key material
61 * Use: Initializes an XTEA key buffer. The key buffer must be 16
65 void xtea_init(xtea_ctx *k, const void *buf, size_t sz)
75 memset(kb + sz, 0, sizeof(kb) - sz);
79 k->k[0] = LOAD32(p + 0); k->k[1] = LOAD32(p + 4);
80 k->k[2] = LOAD32(p + 8); k->k[3] = LOAD32(p + 12);
86 /* --- @xtea_eblk@, @xtea_dblk@ --- *
88 * Arguments: @const xtea_ctx *k@ = pointer to key block
89 * @const uint32 s[2]@ = pointer to source block
90 * @uint32 d[2]@ = pointer to xteatination block
94 * Use: Low-level block encryption and decryption.
97 #define DELTA 0x9e3779b9
99 void xtea_eblk(const xtea_ctx *k, const uint32 *s, uint32 *d)
101 uint32 y = s[0], z = s[1];
105 for (i = 0; i < 32; i++) {
106 y = U32(y + ((((z << 4) ^ (z >> 5)) + z) ^ (n + k->k[n & 3])));
108 z = U32(z + ((((y << 4) ^ (y >> 5)) + y) ^ (n + k->k[(n >> 11) & 3])));
113 void xtea_dblk(const xtea_ctx *k, const uint32 *s, uint32 *d)
115 uint32 y = s[0], z = s[1];
116 uint32 n = DELTA << 5;
119 for (i = 0; i < 32; i++) {
120 z = U32(z - ((((y << 4) ^ (y >> 5)) + y) ^ (n + k->k[(n >> 11) & 3])));
122 y = U32(y - ((((z << 4) ^ (z >> 5)) + z) ^ (n + k->k[n & 3])));
127 BLKC_TEST(XTEA, xtea)
129 /*----- That's all, folks -------------------------------------------------*/