key-data.c (key_struct{set,steal}): Assert no other references.
[u/mdw/catacomb] / tea.c
CommitLineData
ea937a0e 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: tea.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
ea937a0e 4 *
5 * The Tiny Encryption Algorithm
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
ea937a0e 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.
45c0fd36 18 *
ea937a0e 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.
45c0fd36 23 *
ea937a0e 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
ea937a0e 30/*----- Header files ------------------------------------------------------*/
31
32#include <mLib/bits.h>
33
34#include "blkc.h"
35#include "gcipher.h"
36#include "paranoia.h"
37#include "tea.h"
38
39/*----- Global variables --------------------------------------------------*/
40
41const octet tea_keysz[] = { KSZ_RANGE, TEA_KEYSZ, 0, 16, 1 };
42
43/*----- Main code ---------------------------------------------------------*/
44
45/* --- @tea_init@ --- *
46 *
45c0fd36
MW
47 * Arguments: @tea_ctx *k@ = pointer to key block
48 * @const void *buf@ = pointer to key buffer
49 * @size_t sz@ = size of key material
ea937a0e 50 *
45c0fd36 51 * Returns: ---
ea937a0e 52 *
45c0fd36 53 * Use: Initializes a TEA key buffer. The key buffer must be 16
ea937a0e 54 * bytes long.
55 */
56
57void tea_init(tea_ctx *k, const void *buf, size_t sz)
58{
59 octet kb[16];
60 const octet *p;
61
62 KSZ_ASSERT(tea, sz);
63 if (sz >= sizeof(kb))
64 p = buf;
65 else {
66 memcpy(kb, buf, sz);
67 memset(kb + sz, 0, sizeof(kb) - sz);
68 p = kb;
69 }
70
d36cd240 71 k->ka = LOAD32(p + 0); k->kb = LOAD32(p + 4);
72 k->kc = LOAD32(p + 8); k->kd = LOAD32(p + 12);
73 k->r = 32;
ea937a0e 74
75 if (p == kb)
76 BURN(kb);
77}
78
79/* --- @tea_eblk@, @tea_dblk@ --- *
80 *
45c0fd36
MW
81 * Arguments: @const tea_ctx *k@ = pointer to key block
82 * @const uint32 s[2]@ = pointer to source block
83 * @uint32 d[2]@ = pointer to teatination block
ea937a0e 84 *
45c0fd36 85 * Returns: ---
ea937a0e 86 *
45c0fd36 87 * Use: Low-level block encryption and decryption.
ea937a0e 88 */
89
90#define DELTA 0x9e3779b9
91
92void tea_eblk(const tea_ctx *k, const uint32 *s, uint32 *d)
93{
94 uint32 y = s[0], z = s[1];
95 uint32 ka = k->ka, kb = k->kb, kc = k->kc, kd = k->kd;
96 uint32 n = 0;
97 unsigned i;
98
d36cd240 99 for (i = 0; i < k->r; i++) {
ea937a0e 100 n += DELTA;
101 y = U32(y + (((z << 4) + ka) ^ (z + n) ^ ((z >> 5) + kb)));
102 z = U32(z + (((y << 4) + kc) ^ (y + n) ^ ((y >> 5) + kd)));
103 }
104 d[0] = y; d[1] = z;
105}
106
107void tea_dblk(const tea_ctx *k, const uint32 *s, uint32 *d)
108{
109 uint32 y = s[0], z = s[1];
110 uint32 ka = k->ka, kb = k->kb, kc = k->kc, kd = k->kd;
d36cd240 111 uint32 n = DELTA * k->r;
ea937a0e 112 unsigned i;
113
d36cd240 114 for (i = 0; i < k->r; i++) {
ea937a0e 115 z = U32(z - (((y << 4) + kc) ^ (y + n) ^ ((y >> 5) + kd)));
116 y = U32(y - (((z << 4) + ka) ^ (z + n) ^ ((z >> 5) + kb)));
117 n -= DELTA;
118 }
119 d[0] = y; d[1] = z;
120}
121
122BLKC_TEST(TEA, tea)
123
124/*----- That's all, folks -------------------------------------------------*/