xtea.c
Go to the documentation of this file.
1 /**
2  * @file xtea.c
3  * @brief XTEA (eXtended TEA)
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.0
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/xtea.h"
37 
38 //XTEA magic constant
39 #define DELTA 0x9E3779B9
40 
41 //Check crypto library configuration
42 #if (XTEA_SUPPORT == ENABLED)
43 
44 
45 //Common interface for encryption algorithms
47 {
48  "XTEA",
49  sizeof(XteaContext),
53  NULL,
54  NULL,
58 };
59 
60 
61 /**
62  * @brief Key expansion
63  * @param[in] context Pointer to the XTEA 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 __weak_func error_t xteaInit(XteaContext *context, const uint8_t *key,
70  size_t keyLen)
71 {
72  //Check parameters
73  if(context == NULL || key == NULL)
75 
76  //Invalid key length?
77  if(keyLen != 16)
79 
80  //Copy the 128-bit key
81  context->k[0] = LOAD32BE(key + 0);
82  context->k[1] = LOAD32BE(key + 4);
83  context->k[2] = LOAD32BE(key + 8);
84  context->k[3] = LOAD32BE(key + 12);
85 
86  //No error to report
87  return NO_ERROR;
88 }
89 
90 
91 /**
92  * @brief Encrypt a 16-byte block using XTEA algorithm
93  * @param[in] context Pointer to the XTEA context
94  * @param[in] input Plaintext block to encrypt
95  * @param[out] output Ciphertext block resulting from encryption
96  **/
97 
98 __weak_func void xteaEncryptBlock(XteaContext *context, const uint8_t *input,
99  uint8_t *output)
100 {
101  uint_t i;
102  uint32_t y;
103  uint32_t z;
104  uint32_t sum;
105 
106  //Initialize variable
107  sum = 0;
108 
109  //The 8 bytes of plaintext are split into 2 words
110  y = LOAD32BE(input + 0);
111  z = LOAD32BE(input + 4);
112 
113  //Apply 32 rounds
114  for(i = 0; i < XTEA_NB_ROUNDS; i++)
115  {
116  y += (((z << 4) ^ (z >> 5)) + z) ^ (sum + context->k[sum & 0x03]);
117  sum += DELTA;
118  z += (((y << 4) ^ (y >> 5)) + y) ^ (sum + context->k[(sum >> 11) & 0x03]);
119  }
120 
121  //The 2 words of ciphertext are then written as 8 bytes
122  STORE32BE(y, output + 0);
123  STORE32BE(z, output + 4);
124 }
125 
126 
127 /**
128  * @brief Decrypt a 16-byte block using XTEA algorithm
129  * @param[in] context Pointer to the XTEA context
130  * @param[in] input Ciphertext block to decrypt
131  * @param[out] output Plaintext block resulting from decryption
132  **/
133 
134 __weak_func void xteaDecryptBlock(XteaContext *context, const uint8_t *input,
135  uint8_t *output)
136 {
137  uint_t i;
138  uint32_t y;
139  uint32_t z;
140  uint32_t sum;
141 
142  //Initialize variable
143  sum = (DELTA & 0x07FFFFFF) << 5;
144 
145  //The 8 bytes of ciphertext are split into 2 words
146  y = LOAD32BE(input + 0);
147  z = LOAD32BE(input + 4);
148 
149  //Apply 32 rounds
150  for(i = 0; i < XTEA_NB_ROUNDS; i++)
151  {
152  z -= (((y << 4) ^ (y >> 5)) + y) ^ (sum + context->k[(sum >> 11) & 0x03]);
153  sum -= DELTA;
154  y -= (((z << 4) ^ (z >> 5)) + z) ^ (sum + context->k[sum & 0x03]);
155  }
156 
157  //The 2 words of plaintext are then written as 8 bytes
158  STORE32BE(y, output + 0);
159  STORE32BE(z, output + 4);
160 }
161 
162 
163 /**
164  * @brief Release XTEA context
165  * @param[in] context Pointer to the XTEA context
166  **/
167 
168 __weak_func void xteaDeinit(XteaContext *context)
169 {
170  //Clear XTEA context
171  osMemset(context, 0, sizeof(XteaContext));
172 }
173 
174 #endif
unsigned int uint_t
Definition: compiler_port.h:50
#define LOAD32BE(p)
Definition: cpu_endian.h:210
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
General definitions for cryptographic algorithms.
void(* CipherAlgoDeinit)(void *context)
Definition: crypto.h:983
void(* CipherAlgoDecryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:980
error_t(* CipherAlgoInit)(void *context, const uint8_t *key, size_t keyLen)
Definition: crypto.h:968
void(* CipherAlgoEncryptBlock)(void *context, const uint8_t *input, uint8_t *output)
Definition: crypto.h:977
@ CIPHER_ALGO_TYPE_BLOCK
Definition: crypto.h:932
uint8_t z
Definition: dns_common.h:191
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_KEY_LENGTH
Definition: error.h:107
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemset(p, value, length)
Definition: os_port.h:135
Common interface for encryption algorithms.
Definition: crypto.h:1036
XTEA algorithm context.
Definition: xtea.h:60
uint32_t k[4]
Definition: xtea.h:61
const CipherAlgo xteaCipherAlgo
Definition: xtea.c:46
__weak_func void xteaDecryptBlock(XteaContext *context, const uint8_t *input, uint8_t *output)
Decrypt a 16-byte block using XTEA algorithm.
Definition: xtea.c:134
#define DELTA
Definition: xtea.c:39
__weak_func void xteaDeinit(XteaContext *context)
Release XTEA context.
Definition: xtea.c:168
__weak_func void xteaEncryptBlock(XteaContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 16-byte block using XTEA algorithm.
Definition: xtea.c:98
__weak_func error_t xteaInit(XteaContext *context, const uint8_t *key, size_t keyLen)
Key expansion.
Definition: xtea.c:69
XTEA (eXtended TEA)
#define XTEA_BLOCK_SIZE
Definition: xtea.h:43
#define XTEA_NB_ROUNDS
Definition: xtea.h:45