Reformat.
[bertos.git] / bertos / algo / tea.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief TEA Tiny Encription Algorith functions (implementation).
34  *
35  * \version $Id$
36  * \author Francesco Sacchi <batt@develer.com>
37  *
38  * The Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham
39  * of the Cambridge Computer Laboratory
40  *
41  * Placed in the Public Domain by David Wheeler and Roger Needham.
42  *
43  * **** ANSI C VERSION ****
44  *
45  * Notes:
46  *
47  * TEA is a Feistel cipher with XOR and and addition as the non-linear
48  * mixing functions.
49  *
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].
52  *
53  * TEA can be operated in any of the modes of DES. Cipher Block Chaining is,
54  * for example, simple to implement.
55  *
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.
59  *
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.
62  *
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).
65  */
66
67 #include "tea.h"
68 #include <cpu/byteorder.h>
69
70 static uint32_t tea_func(uint32_t *in, uint32_t *sum, uint32_t *k)
71 {
72         return ((*in << 4) + cpu_to_le32(k[0])) ^ (*in + *sum) ^ ((*in >> 5) + cpu_to_le32(k[1]));
73 }
74
75 /**
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.
81  */
82 void tea_enc(void *_v, void *_k)
83 {
84         uint32_t y, z;
85         uint32_t sum = 0;
86         uint8_t n = ROUNDS;
87         uint32_t *v = (uint32_t *)_v;
88         uint32_t *k = (uint32_t *)_k;
89
90         y=cpu_to_le32(v[0]);
91         z=cpu_to_le32(v[1]);
92
93         while(n-- > 0)
94         {
95                 sum += DELTA;
96                 y += tea_func(&z, &sum, &(k[0]));
97                 z += tea_func(&y, &sum, &(k[2]));
98         }
99
100         v[0] = le32_to_cpu(y);
101         v[1] = le32_to_cpu(z);
102 }
103
104 /**
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.
110  */
111 void tea_dec(void *_v, void *_k)
112 {
113         uint32_t y, z;
114         uint32_t sum = DELTA * ROUNDS;
115         uint8_t n = ROUNDS;
116         uint32_t *v = (uint32_t *)_v;
117         uint32_t *k = (uint32_t *)_k;
118
119         y = cpu_to_le32(v[0]);
120         z = cpu_to_le32(v[1]);
121
122         while(n-- > 0)
123         {
124                 z -= tea_func(&y, &sum, &(k[2]));
125                 y -= tea_func(&z, &sum, &(k[0]));
126                 sum -= DELTA;
127         }
128
129         v[0] = le32_to_cpu(y);
130         v[1] = le32_to_cpu(z);
131 }
132