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