chiark / gitweb /
debian/.gitignore: Ignore `catacomb-data' directory.
[catacomb] / symm / xtea.c
1 /* -*-c-*-
2  *
3  * The Extended Tiny Encryption Algorithm
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/bits.h>
31
32 #include "blkc.h"
33 #include "gcipher.h"
34 #include "paranoia.h"
35 #include "xtea.h"
36
37 /*----- Global variables --------------------------------------------------*/
38
39 const octet xtea_keysz[] = { KSZ_RANGE, XTEA_KEYSZ, 0, 16, 1 };
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 /* --- @xtea_init@ --- *
44  *
45  * Arguments:   @xtea_ctx *k@ = pointer to key block
46  *              @const void *buf@ = pointer to key buffer
47  *              @size_t sz@ = size of key material
48  *
49  * Returns:     ---
50  *
51  * Use:         Initializes an XTEA key buffer.  The key buffer must be 16
52  *              bytes long.
53  */
54
55 void xtea_init(xtea_ctx *k, const void *buf, size_t sz)
56 {
57   octet kb[16];
58   const octet *p;
59
60   KSZ_ASSERT(xtea, sz);
61   if (sz >= sizeof(kb))
62     p = buf;
63   else {
64     memcpy(kb, buf, sz);
65     memset(kb + sz, 0, sizeof(kb) - sz);
66     p = kb;
67   }
68
69   k->k[0] = LOAD32(p +  0); k->k[1] = LOAD32(p +  4);
70   k->k[2] = LOAD32(p +  8); k->k[3] = LOAD32(p + 12);
71   k->r = 32;
72
73   if (p == kb)
74     BURN(kb);
75 }
76
77 /* --- @xtea_eblk@, @xtea_dblk@ --- *
78  *
79  * Arguments:   @const xtea_ctx *k@ = pointer to key block
80  *              @const uint32 s[2]@ = pointer to source block
81  *              @uint32 d[2]@ = pointer to xteatination block
82  *
83  * Returns:     ---
84  *
85  * Use:         Low-level block encryption and decryption.
86  */
87
88 #define DELTA 0x9e3779b9
89
90 void xtea_eblk(const xtea_ctx *k, const uint32 *s, uint32 *d)
91 {
92   uint32 y = s[0], z = s[1];
93   uint32 n = 0;
94   unsigned i;
95
96   for (i = 0; i < k->r; i++) {
97     y = U32(y + ((((z << 4) ^ (z >> 5)) + z) ^ (n + k->k[n & 3])));
98     n += DELTA;
99     z = U32(z + ((((y << 4) ^ (y >> 5)) + y) ^ (n + k->k[(n >> 11) & 3])));
100   }
101   d[0] = y; d[1] = z;
102 }
103
104 void xtea_dblk(const xtea_ctx *k, const uint32 *s, uint32 *d)
105 {
106   uint32 y = s[0], z = s[1];
107   uint32 n = DELTA * k->r;
108   unsigned i;
109
110   for (i = 0; i < k->r; i++) {
111     z = U32(z - ((((y << 4) ^ (y >> 5)) + y) ^ (n + k->k[(n >> 11) & 3])));
112     n -= DELTA;
113     y = U32(y - ((((z << 4) ^ (z >> 5)) + z) ^ (n + k->k[n & 3])));
114   }
115   d[0] = y; d[1] = z;
116 }
117
118 BLKC_TEST(XTEA, xtea)
119
120 /*----- That's all, folks -------------------------------------------------*/