Refactor BeRTOS to be in his own directory.
[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 /*#*
68  *#* $Log$
69  *#* Revision 1.2  2007/09/19 16:23:27  batt
70  *#* Fix doxygen warnings.
71  *#*
72  *#* Revision 1.1  2007/06/07 09:13:40  batt
73  *#* Add TEA enc/decryption algorithm.
74  *#*
75  *#* Revision 1.1  2007/01/10 17:30:10  batt
76  *#* Add cryptographic routines.
77  *#*
78  *#*/
79
80 #include "tea.h"
81 #include <mware/byteorder.h>
82
83 static uint32_t tea_func(uint32_t *in, uint32_t *sum, uint32_t *k)
84 {
85         return ((*in << 4) + cpu_to_le32(k[0])) ^ (*in + *sum) ^ ((*in >> 5) + cpu_to_le32(k[1]));
86 }
87
88 /**
89  * \brief TEA encryption function.
90  * This function encrypts <EM>v</EM> with <EM>k</EM> and returns the
91  * encrypted data in <EM>v</EM>.
92  * \param _v Array of two long values containing the data block.
93  * \param _k Array of four long values containing the key.
94  */
95 void tea_enc(void *_v, void *_k)
96 {
97         uint32_t y, z;
98         uint32_t sum = 0;
99         uint8_t n = ROUNDS;
100         uint32_t *v = (uint32_t *)_v;
101         uint32_t *k = (uint32_t *)_k;
102
103         y=cpu_to_le32(v[0]);
104         z=cpu_to_le32(v[1]);
105
106         while(n-- > 0)
107         {
108                 sum += DELTA;
109                 y += tea_func(&z, &sum, &(k[0]));
110                 z += tea_func(&y, &sum, &(k[2]));
111         }
112
113         v[0] = le32_to_cpu(y);
114         v[1] = le32_to_cpu(z);
115 }
116
117 /**
118  * \brief TEA decryption function.
119  * This function decrypts <EM>v</EM> with <EM>k</EM> and returns the
120  * decrypted data in <EM>v</EM>.
121  * \param _v Array of two long values containing the data block.
122  * \param _k Array of four long values containing the key.
123  */
124 void tea_dec(void *_v, void *_k)
125 {
126         uint32_t y, z;
127         uint32_t sum = DELTA * ROUNDS;
128         uint8_t n = ROUNDS;
129         uint32_t *v = (uint32_t *)_v;
130         uint32_t *k = (uint32_t *)_k;
131
132         y = cpu_to_le32(v[0]);
133         z = cpu_to_le32(v[1]);
134
135         while(n-- > 0)
136         {
137                 z -= tea_func(&y, &sum, &(k[2]));
138                 y -= tea_func(&z, &sum, &(k[0]));
139                 sum -= DELTA;
140         }
141
142         v[0] = le32_to_cpu(y);
143         v[1] = le32_to_cpu(z);
144 }
145