3 * The Tiny Encryption Algorithm
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/bits.h>
37 /*----- Global variables --------------------------------------------------*/
39 const octet tea_keysz
[] = { KSZ_RANGE
, TEA_KEYSZ
, 0, 16, 1 };
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- @tea_init@ --- *
45 * Arguments: @tea_ctx *k@ = pointer to key block
46 * @const void *buf@ = pointer to key buffer
47 * @size_t sz@ = size of key material
51 * Use: Initializes a TEA key buffer. The key buffer must be 16
55 void tea_init(tea_ctx
*k
, const void *buf
, size_t sz
)
65 memset(kb
+ sz
, 0, sizeof(kb
) - sz
);
69 k
->ka
= LOAD32(p
+ 0); k
->kb
= LOAD32(p
+ 4);
70 k
->kc
= LOAD32(p
+ 8); k
->kd
= LOAD32(p
+ 12);
77 /* --- @tea_eblk@, @tea_dblk@ --- *
79 * Arguments: @const tea_ctx *k@ = pointer to key block
80 * @const uint32 s[2]@ = pointer to source block
81 * @uint32 d[2]@ = pointer to teatination block
85 * Use: Low-level block encryption and decryption.
88 #define DELTA 0x9e3779b9
90 void tea_eblk(const tea_ctx
*k
, const uint32
*s
, uint32
*d
)
92 uint32 y
= s
[0], z
= s
[1];
93 uint32 ka
= k
->ka
, kb
= k
->kb
, kc
= k
->kc
, kd
= k
->kd
;
97 for (i
= 0; i
< k
->r
; i
++) {
99 y
= U32(y
+ (((z
<< 4) + ka
) ^ (z
+ n
) ^ ((z
>> 5) + kb
)));
100 z
= U32(z
+ (((y
<< 4) + kc
) ^ (y
+ n
) ^ ((y
>> 5) + kd
)));
105 void tea_dblk(const tea_ctx
*k
, const uint32
*s
, uint32
*d
)
107 uint32 y
= s
[0], z
= s
[1];
108 uint32 ka
= k
->ka
, kb
= k
->kb
, kc
= k
->kc
, kd
= k
->kd
;
109 uint32 n
= DELTA
* k
->r
;
112 for (i
= 0; i
< k
->r
; i
++) {
113 z
= U32(z
- (((y
<< 4) + kc
) ^ (y
+ n
) ^ ((y
>> 5) + kd
)));
114 y
= U32(y
- (((z
<< 4) + ka
) ^ (z
+ n
) ^ ((z
>> 5) + kb
)));
122 /*----- That's all, folks -------------------------------------------------*/