Move unpack lwip ip address macro to macros module.
[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  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * The Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham
38  * of the Cambridge Computer Laboratory
39  *
40  * Placed in the Public Domain by David Wheeler and Roger Needham.
41  *
42  * **** ANSI C VERSION ****
43  *
44  * Notes:
45  *
46  * TEA is a Feistel cipher with XOR and and addition as the non-linear
47  * mixing functions.
48  *
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].
51  *
52  * TEA can be operated in any of the modes of DES. Cipher Block Chaining is,
53  * for example, simple to implement.
54  *
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.
58  *
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.
61  *
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).
64  */
65
66 #include "tea.h"
67 #include <cpu/byteorder.h>
68
69 static uint32_t tea_func(uint32_t *in, uint32_t *sum, uint32_t *k)
70 {
71         return ((*in << 4) + cpu_to_le32(k[0])) ^ (*in + *sum) ^ ((*in >> 5) + cpu_to_le32(k[1]));
72 }
73
74 /**
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.
80  */
81 void tea_enc(void *_v, void *_k)
82 {
83         uint32_t y, z;
84         uint32_t sum = 0;
85         uint8_t n = ROUNDS;
86         uint32_t *v = (uint32_t *)_v;
87         uint32_t *k = (uint32_t *)_k;
88
89         y=cpu_to_le32(v[0]);
90         z=cpu_to_le32(v[1]);
91
92         while(n-- > 0)
93         {
94                 sum += DELTA;
95                 y += tea_func(&z, &sum, &(k[0]));
96                 z += tea_func(&y, &sum, &(k[2]));
97         }
98
99         v[0] = le32_to_cpu(y);
100         v[1] = le32_to_cpu(z);
101 }
102
103 /**
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.
109  */
110 void tea_dec(void *_v, void *_k)
111 {
112         uint32_t y, z;
113         uint32_t sum = DELTA * ROUNDS;
114         uint8_t n = ROUNDS;
115         uint32_t *v = (uint32_t *)_v;
116         uint32_t *k = (uint32_t *)_k;
117
118         y = cpu_to_le32(v[0]);
119         z = cpu_to_le32(v[1]);
120
121         while(n-- > 0)
122         {
123                 z -= tea_func(&y, &sum, &(k[2]));
124                 y -= tea_func(&z, &sum, &(k[0]));
125                 sum -= DELTA;
126         }
127
128         v[0] = le32_to_cpu(y);
129         v[1] = le32_to_cpu(z);
130 }
131