Add comments. Set static prog_flush function.
[bertos.git] / algos / tea.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief TEA Tiny Encription Algorith functions (implementation).
9  *
10  * \version $Id$
11  * \author Francesco Sacchi <batt@develer.com>
12  *
13  * The Tiny Encryption Algorithm (TEA) by David Wheeler and Roger Needham
14  * of the Cambridge Computer Laboratory
15  *
16  * Placed in the Public Domain by David Wheeler and Roger Needham.
17  *
18  * **** ANSI C VERSION ****
19  *
20  * Notes:
21  *
22  * TEA is a Feistel cipher with XOR and and addition as the non-linear
23  * mixing functions.
24  *
25  * Takes 64 bits of data in v[0] and v[1].  Returns 64 bits of data in w[0]
26  * and w[1].  Takes 128 bits of key in k[0] - k[3].
27  *
28  * TEA can be operated in any of the modes of DES. Cipher Block Chaining is,
29  * for example, simple to implement.
30  *
31  * n is the number of iterations. 32 is ample, 16 is sufficient, as few
32  * as eight may be OK.  The algorithm achieves good dispersion after six
33  * iterations. The iteration count can be made variable if required.
34  *
35  * Note this is optimised for 32-bit CPUs with fast shift capabilities. It
36  * can very easily be ported to assembly language on most CPUs.
37  *
38  * delta is chosen to be the real part of (the golden ratio Sqrt(5/4) -
39  * 1/2 ~ 0.618034 multiplied by 2^32).
40  */
41
42 /*#*
43  *#* $Log$
44  *#* Revision 1.1  2007/06/07 09:13:40  batt
45  *#* Add TEA enc/decryption algorithm.
46  *#*
47  *#* Revision 1.1  2007/01/10 17:30:10  batt
48  *#* Add cryptographic routines.
49  *#*
50  *#*/
51
52 #include "tea.h"
53 #include <mware/byteorder.h>
54
55 static uint32_t tea_func(uint32_t *in, uint32_t *sum, uint32_t *k)
56 {
57         return ((*in << 4) + cpu_to_le32(k[0])) ^ (*in + *sum) ^ ((*in >> 5) + cpu_to_le32(k[1]));
58 }
59
60 /**
61  * \brief TEA encryption function.
62  * This function encrypts <EM>v</EM> with <EM>k</EM> and returns the
63  * encrypted data in <EM>v</EM>.
64  * \param v Array of two long values containing the data block.
65  * \param k Array of four long values containing the key.
66  */
67 void tea_enc(void *_v, void *_k)
68 {
69         uint32_t y, z;
70         uint32_t sum = 0;
71         uint8_t n = ROUNDS;
72         uint32_t *v = (uint32_t *)_v;
73         uint32_t *k = (uint32_t *)_k;
74
75         y=cpu_to_le32(v[0]);
76         z=cpu_to_le32(v[1]);
77
78         while(n-- > 0)
79         {
80                 sum += DELTA;
81                 y += tea_func(&z, &sum, &(k[0]));
82                 z += tea_func(&y, &sum, &(k[2]));
83         }
84
85         v[0] = le32_to_cpu(y);
86         v[1] = le32_to_cpu(z);
87 }
88
89 /**
90  * \brief TEA decryption function.
91  * This function decrypts <EM>v</EM> with <EM>k</EM> and returns the
92  * decrypted data in <EM>v</EM>.
93  * \param v Array of two long values containing the data block.
94  *\param k Array of four long values containing the key.
95  */
96 void tea_dec(void *_v, void *_k)
97 {
98         uint32_t y, z;
99         uint32_t sum = DELTA * ROUNDS;
100         uint8_t n = ROUNDS;
101         uint32_t *v = (uint32_t *)_v;
102         uint32_t *k = (uint32_t *)_k;
103
104         y = cpu_to_le32(v[0]);
105         z = cpu_to_le32(v[1]);
106
107         while(n-- > 0)
108         {
109                 z -= tea_func(&y, &sum, &(k[2]));
110                 y -= tea_func(&z, &sum, &(k[0]));
111                 sum -= DELTA;
112         }
113
114         v[0] = le32_to_cpu(y);
115         v[1] = le32_to_cpu(z);
116 }
117