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).
35 * \author Francesco Sacchi <batt@develer.com>
37 * The Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham
38 * of the Cambridge Computer Laboratory
40 * Placed in the Public Domain by David Wheeler and Roger Needham.
42 * **** ANSI C VERSION ****
46 * TEA is a Feistel cipher with XOR and and addition as the non-linear
49 * Takes 64 bits of data in v[0] and v[1]. Returns 64 bits of data in w[0]
50 * and w[1]. Takes 128 bits of key in k[0] - k[3].
52 * TEA can be operated in any of the modes of DES. Cipher Block Chaining is,
53 * for example, simple to implement.
55 * n is the number of iterations. 32 is ample, 16 is sufficient, as few
56 * as eight may be OK. The algorithm achieves good dispersion after six
57 * iterations. The iteration count can be made variable if required.
59 * Note this is optimised for 32-bit CPUs with fast shift capabilities. It
60 * can very easily be ported to assembly language on most CPUs.
62 * delta is chosen to be the real part of (the golden ratio Sqrt(5/4) -
63 * 1/2 ~ 0.618034 multiplied by 2^32).
67 #include <cpu/byteorder.h>
69 static uint32_t tea_func(uint32_t *in, uint32_t *sum, uint32_t *k)
71 return ((*in << 4) + cpu_to_le32(k[0])) ^ (*in + *sum) ^ ((*in >> 5) + cpu_to_le32(k[1]));
75 * \brief TEA encryption function.
76 * This function encrypts <EM>v</EM> with <EM>k</EM> and returns the
77 * encrypted data in <EM>v</EM>.
78 * \param _v Array of two long values containing the data block.
79 * \param _k Array of four long values containing the key.
81 void tea_enc(void *_v, void *_k)
86 uint32_t *v = (uint32_t *)_v;
87 uint32_t *k = (uint32_t *)_k;
95 y += tea_func(&z, &sum, &(k[0]));
96 z += tea_func(&y, &sum, &(k[2]));
99 v[0] = le32_to_cpu(y);
100 v[1] = le32_to_cpu(z);
104 * \brief TEA decryption function.
105 * This function decrypts <EM>v</EM> with <EM>k</EM> and returns the
106 * decrypted data in <EM>v</EM>.
107 * \param _v Array of two long values containing the data block.
108 * \param _k Array of four long values containing the key.
110 void tea_dec(void *_v, void *_k)
113 uint32_t sum = DELTA * ROUNDS;
115 uint32_t *v = (uint32_t *)_v;
116 uint32_t *k = (uint32_t *)_k;
118 y = cpu_to_le32(v[0]);
119 z = cpu_to_le32(v[1]);
123 z -= tea_func(&y, &sum, &(k[2]));
124 y -= tea_func(&z, &sum, &(k[0]));
128 v[0] = le32_to_cpu(y);
129 v[1] = le32_to_cpu(z);