tea.c
Go to the documentation of this file.
1 /**
2  * @file tea.c
3  * @brief TEA (Tiny Encryption Algorithm)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "cipher/tea.h"
37 
38 //TEA magic constant
39 #define DELTA 0x9E3779B9
40 
41 //Check crypto library configuration
42 #if (TEA_SUPPORT == ENABLED)
43 
44 
45 //Common interface for encryption algorithms
47 {
48  "TEA",
49  sizeof(TeaContext),
53  NULL,
54  NULL,
58 };
59 
60 
61 /**
62  * @brief Key expansion
63  * @param[in] context Pointer to the TEA context to initialize
64  * @param[in] key Pointer to the key
65  * @param[in] keyLen Length of the key
66  * @return Error code
67  **/
68 
69 error_t teaInit(TeaContext *context, const uint8_t *key, size_t keyLen)
70 {
71  //Check parameters
72  if(context == NULL || key == NULL)
74 
75  //Invalid key length?
76  if(keyLen != 16)
78 
79  //Copy the 128-bit key
80  context->k[0] = LOAD32BE(key + 0);
81  context->k[1] = LOAD32BE(key + 4);
82  context->k[2] = LOAD32BE(key + 8);
83  context->k[3] = LOAD32BE(key + 12);
84 
85  //No error to report
86  return NO_ERROR;
87 }
88 
89 
90 /**
91  * @brief Encrypt a 16-byte block using TEA algorithm
92  * @param[in] context Pointer to the TEA context
93  * @param[in] input Plaintext block to encrypt
94  * @param[out] output Ciphertext block resulting from encryption
95  **/
96 
97 void teaEncryptBlock(TeaContext *context, const uint8_t *input,
98  uint8_t *output)
99 {
100  uint_t i;
101  uint32_t y;
102  uint32_t z;
103  uint32_t sum;
104 
105  //Initialize variable
106  sum = 0;
107 
108  //The 8 bytes of plaintext are split into 2 words
109  y = LOAD32BE(input + 0);
110  z = LOAD32BE(input + 4);
111 
112  //Apply 32 rounds
113  for(i = 0; i < TEA_NB_ROUNDS; i++)
114  {
115  sum += DELTA;
116  y += ((z << 4) + context->k[0]) ^ (z + sum) ^ ((z >> 5) + context->k[1]);
117  z += ((y << 4) + context->k[2]) ^ (y + sum) ^ ((y >> 5) + context->k[3]);
118  }
119 
120  //The 2 words of ciphertext are then written as 8 bytes
121  STORE32BE(y, output + 0);
122  STORE32BE(z, output + 4);
123 }
124 
125 
126 /**
127  * @brief Decrypt a 16-byte block using TEA algorithm
128  * @param[in] context Pointer to the TEA context
129  * @param[in] input Ciphertext block to decrypt
130  * @param[out] output Plaintext block resulting from decryption
131  **/
132 
133 void teaDecryptBlock(TeaContext *context, const uint8_t *input,
134  uint8_t *output)
135 {
136  uint_t i;
137  uint32_t y;
138  uint32_t z;
139  uint32_t sum;
140 
141  //Initialize variable
142  sum = (DELTA & 0x07FFFFFF) << 5;
143 
144  //The 8 bytes of ciphertext are split into 2 words
145  y = LOAD32BE(input + 0);
146  z = LOAD32BE(input + 4);
147 
148  //Apply 32 rounds
149  for(i = 0; i < TEA_NB_ROUNDS; i++)
150  {
151  z -= ((y << 4) + context->k[2]) ^ (y + sum) ^ ((y >> 5) + context->k[3]);
152  y -= ((z << 4) + context->k[0]) ^ (z + sum) ^ ((z >> 5) + context->k[1]);
153  sum -= DELTA;
154  }
155 
156  //The 2 words of plaintext are then written as 8 bytes
157  STORE32BE(y, output + 0);
158  STORE32BE(z, output + 4);
159 }
160 
161 
162 /**
163  * @brief Release TEA context
164  * @param[in] context Pointer to the TEA context
165  **/
166 
167 void teaDeinit(TeaContext *context)
168 {
169  //Clear TEA context
170  osMemset(context, 0, sizeof(TeaContext));
171 }
172 
173 #endif
const CipherAlgo teaCipherAlgo
Definition: tea.c:46
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1009
#define LOAD32BE(p)
Definition: cpu_endian.h:210
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:953
#define TEA_NB_ROUNDS
Definition: tea.h:45
TEA (Tiny Encryption Algorithm)
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define DELTA
Definition: tea.c:39
error_t
Error codes.
Definition: error.h:43
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:1012
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:107
General definitions for cryptographic algorithms.
uint32_t k[4]
Definition: tea.h:61
#define TEA_BLOCK_SIZE
Definition: tea.h:43
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:1000
uint8_t z
Definition: dns_common.h:191
void teaDecryptBlock(TeaContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using TEA algorithm.
Definition: tea.c:133
void teaEncryptBlock(TeaContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using TEA algorithm.
Definition: tea.c:97
error_t teaInit(TeaContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
Definition: tea.c:69
void teaDeinit(TeaContext *context)
Release TEA context.
Definition: tea.c:167
Common interface for encryption algorithms.
Definition: crypto.h:1068
TEA algorithm context.
Definition: tea.h:60
unsigned int uint_t
Definition: compiler_port.h:50
#define osMemset(p, value, length)
Definition: os_port.h:135
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:1015
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
@ NO_ERROR
Success.
Definition: error.h:44