Force subkeys to be sorted in structured keys.
[u/mdw/catacomb] / tea.c
CommitLineData
ea937a0e 1/* -*-c-*-
2 *
d36cd240 3 * $Id: tea.c,v 1.2 2000/07/29 09:56:47 mdw Exp $
ea937a0e 4 *
5 * The Tiny Encryption Algorithm
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: tea.c,v $
d36cd240 33 * Revision 1.2 2000/07/29 09:56:47 mdw
34 * Allow the number of rounds to be configured. This isn't exported
35 * through the gcipher interface, but it may be useful anyway.
36 *
ea937a0e 37 * Revision 1.1 2000/07/15 13:44:31 mdw
38 * New ciphers.
39 *
40 */
41
42/*----- Header files ------------------------------------------------------*/
43
44#include <mLib/bits.h>
45
46#include "blkc.h"
47#include "gcipher.h"
48#include "paranoia.h"
49#include "tea.h"
50
51/*----- Global variables --------------------------------------------------*/
52
53const octet tea_keysz[] = { KSZ_RANGE, TEA_KEYSZ, 0, 16, 1 };
54
55/*----- Main code ---------------------------------------------------------*/
56
57/* --- @tea_init@ --- *
58 *
59 * Arguments: @tea_ctx *k@ = pointer to key block
60 * @const void *buf@ = pointer to key buffer
61 * @size_t sz@ = size of key material
62 *
63 * Returns: ---
64 *
65 * Use: Initializes a TEA key buffer. The key buffer must be 16
66 * bytes long.
67 */
68
69void tea_init(tea_ctx *k, const void *buf, size_t sz)
70{
71 octet kb[16];
72 const octet *p;
73
74 KSZ_ASSERT(tea, sz);
75 if (sz >= sizeof(kb))
76 p = buf;
77 else {
78 memcpy(kb, buf, sz);
79 memset(kb + sz, 0, sizeof(kb) - sz);
80 p = kb;
81 }
82
d36cd240 83 k->ka = LOAD32(p + 0); k->kb = LOAD32(p + 4);
84 k->kc = LOAD32(p + 8); k->kd = LOAD32(p + 12);
85 k->r = 32;
ea937a0e 86
87 if (p == kb)
88 BURN(kb);
89}
90
91/* --- @tea_eblk@, @tea_dblk@ --- *
92 *
93 * Arguments: @const tea_ctx *k@ = pointer to key block
94 * @const uint32 s[2]@ = pointer to source block
95 * @uint32 d[2]@ = pointer to teatination block
96 *
97 * Returns: ---
98 *
99 * Use: Low-level block encryption and decryption.
100 */
101
102#define DELTA 0x9e3779b9
103
104void tea_eblk(const tea_ctx *k, const uint32 *s, uint32 *d)
105{
106 uint32 y = s[0], z = s[1];
107 uint32 ka = k->ka, kb = k->kb, kc = k->kc, kd = k->kd;
108 uint32 n = 0;
109 unsigned i;
110
d36cd240 111 for (i = 0; i < k->r; i++) {
ea937a0e 112 n += DELTA;
113 y = U32(y + (((z << 4) + ka) ^ (z + n) ^ ((z >> 5) + kb)));
114 z = U32(z + (((y << 4) + kc) ^ (y + n) ^ ((y >> 5) + kd)));
115 }
116 d[0] = y; d[1] = z;
117}
118
119void tea_dblk(const tea_ctx *k, const uint32 *s, uint32 *d)
120{
121 uint32 y = s[0], z = s[1];
122 uint32 ka = k->ka, kb = k->kb, kc = k->kc, kd = k->kd;
d36cd240 123 uint32 n = DELTA * k->r;
ea937a0e 124 unsigned i;
125
d36cd240 126 for (i = 0; i < k->r; i++) {
ea937a0e 127 z = U32(z - (((y << 4) + kc) ^ (y + n) ^ ((y >> 5) + kd)));
128 y = U32(y - (((z << 4) + ka) ^ (z + n) ^ ((z >> 5) + kb)));
129 n -= DELTA;
130 }
131 d[0] = y; d[1] = z;
132}
133
134BLKC_TEST(TEA, tea)
135
136/*----- That's all, folks -------------------------------------------------*/