4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
33 * \brief TEA Tiny Encription Algorith functions (implementation).
36 * \author Francesco Sacchi <batt@develer.com>
38 * The Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham
39 * of the Cambridge Computer Laboratory
41 * Placed in the Public Domain by David Wheeler and Roger Needham.
43 * **** ANSI C VERSION ****
47 * TEA is a Feistel cipher with XOR and and addition as the non-linear
50 * Takes 64 bits of data in v[0] and v[1]. Returns 64 bits of data in w[0]
51 * and w[1]. Takes 128 bits of key in k[0] - k[3].
53 * TEA can be operated in any of the modes of DES. Cipher Block Chaining is,
54 * for example, simple to implement.
56 * n is the number of iterations. 32 is ample, 16 is sufficient, as few
57 * as eight may be OK. The algorithm achieves good dispersion after six
58 * iterations. The iteration count can be made variable if required.
60 * Note this is optimised for 32-bit CPUs with fast shift capabilities. It
61 * can very easily be ported to assembly language on most CPUs.
63 * delta is chosen to be the real part of (the golden ratio Sqrt(5/4) -
64 * 1/2 ~ 0.618034 multiplied by 2^32).
68 #include <cpu/byteorder.h>
70 static uint32_t tea_func(uint32_t *in, uint32_t *sum, uint32_t *k)
72 return ((*in << 4) + cpu_to_le32(k[0])) ^ (*in + *sum) ^ ((*in >> 5) + cpu_to_le32(k[1]));
76 * \brief TEA encryption function.
77 * This function encrypts <EM>v</EM> with <EM>k</EM> and returns the
78 * encrypted data in <EM>v</EM>.
79 * \param _v Array of two long values containing the data block.
80 * \param _k Array of four long values containing the key.
82 void tea_enc(void *_v, void *_k)
87 uint32_t *v = (uint32_t *)_v;
88 uint32_t *k = (uint32_t *)_k;
96 y += tea_func(&z, &sum, &(k[0]));
97 z += tea_func(&y, &sum, &(k[2]));
100 v[0] = le32_to_cpu(y);
101 v[1] = le32_to_cpu(z);
105 * \brief TEA decryption function.
106 * This function decrypts <EM>v</EM> with <EM>k</EM> and returns the
107 * decrypted data in <EM>v</EM>.
108 * \param _v Array of two long values containing the data block.
109 * \param _k Array of four long values containing the key.
111 void tea_dec(void *_v, void *_k)
114 uint32_t sum = DELTA * ROUNDS;
116 uint32_t *v = (uint32_t *)_v;
117 uint32_t *k = (uint32_t *)_k;
119 y = cpu_to_le32(v[0]);
120 z = cpu_to_le32(v[1]);
124 z -= tea_func(&y, &sum, &(k[2]));
125 y -= tea_func(&z, &sum, &(k[0]));
129 v[0] = le32_to_cpu(y);
130 v[1] = le32_to_cpu(z);