px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_xtea.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2010 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_xtea.h : XTEA (eXtended Tiny Encryption Algorithm)
14 Author(s): Pieter Conradie
15 Creation Date: 2010-05-03
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20#include <string.h>
21
22/* _____PROJECT INCLUDES_____________________________________________________ */
23#include "px_xtea.h"
24
25/* _____LOCAL DEFINITIONS____________________________________________________ */
26#ifndef PX_XTEA_NUMBER_OF_ROUNDS
27#define PX_XTEA_NUMBER_OF_ROUNDS 16
28#endif
29
30#define PX_XTEA_DELTA 0x9E3779B9
31
32/* _____MACROS_______________________________________________________________ */
33
34/* _____GLOBAL VARIABLES_____________________________________________________ */
35
36/* _____LOCAL VARIABLES______________________________________________________ */
37static uint32_t px_xtea_key[4];
38
39/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
40
41/* _____LOCAL FUNCTIONS______________________________________________________ */
42
43/* _____GLOBAL FUNCTIONS_____________________________________________________ */
44void px_xtea_init(const uint32_t key[4] )
45{
46 uint8_t i;
47
48 for(i = 0; i < 4; i++)
49 {
50 px_xtea_key[i] = key[i];
51 }
52}
53
54void px_xtea_encrypt(uint32_t data[2] )
55{
56 uint8_t i;
57
58 uint32_t d0 = data[0];
59 uint32_t d1 = data[1];
60 uint32_t sum = 0;
61
62 for(i = PX_XTEA_NUMBER_OF_ROUNDS; i != 0; i--)
63 {
64 d0 += (((d1 << 4) ^ (d1 >> 5)) + d1) ^ (sum + px_xtea_key[sum & 3]);
65 sum += PX_XTEA_DELTA;
66 d1 += (((d0 << 4) ^ (d0 >> 5)) + d0) ^ (sum + px_xtea_key[(sum >> 11) & 3]);
67 }
68 data[0] = d0;
69 data[1] = d1;
70}
71
72size_t px_xtea_encrypt_data_ecb(const uint8_t * data_in, uint8_t * data_out, size_t nr_of_bytes)
73{
74 uint32_t buf[2];
75 size_t nr_of_bytes_out = 0;
76
77 while(nr_of_bytes != 0)
78 {
79 // Less than 8 bytes (64 bits)?
80 if(nr_of_bytes < 8)
81 {
82 // Set bytes to zero
83 buf[0] = 0;
84 buf[1] = 0;
85 // Copy remaining bytes
86 memcpy(buf, data_in, nr_of_bytes);
87 // Encrypt block
88 px_xtea_encrypt(buf);
89 memcpy(data_out, buf, 8);
90 // Next block
91 nr_of_bytes_out += 8;
92 // Done
93 break;
94 }
95 else
96 {
97 // Copy 8 bytes
98 memcpy(buf, data_in, 8);
99 // Encrypt block
100 px_xtea_encrypt(buf);
101 memcpy(data_out, buf, 8);
102 // Next block
103 nr_of_bytes -= 8;
104 nr_of_bytes_out += 8;
105 data_in += 8;
106 data_out += 8;
107 }
108 }
109 return nr_of_bytes_out;
110}
111
112void px_xtea_decrypt(uint32_t data[2] )
113{
114 uint8_t i;
115
116 uint32_t d0 = data[0];
117 uint32_t d1 = data[1];
118 uint32_t sum = (uint32_t)(PX_XTEA_DELTA * PX_XTEA_NUMBER_OF_ROUNDS);
119
120 for(i = PX_XTEA_NUMBER_OF_ROUNDS; i != 0; i--)
121 {
122 d1 -= (((d0 << 4) ^ (d0 >> 5)) + d0) ^ (sum + px_xtea_key[(sum >> 11) & 3]);
123 sum -= PX_XTEA_DELTA;
124 d0 -= (((d1 << 4) ^ (d1 >> 5)) + d1) ^ (sum + px_xtea_key[sum & 3]);
125 }
126 data[0] = d0;
127 data[1] = d1;
128}
129
130size_t px_xtea_decrypt_data_ecb(const uint8_t * data_in, uint8_t * data_out, size_t nr_of_bytes)
131{
132 uint32_t buf[2];
133 size_t nr_of_bytes_out = 0;
134
135 while(nr_of_bytes != 0)
136 {
137 // Less than 8 bytes (64 bits)?
138 if(nr_of_bytes < 8)
139 {
140 // Set bytes to zero
141 buf[0] = 0;
142 buf[1] = 0;
143 // Copy remaining bytes
144 memcpy(buf, data_in, nr_of_bytes);
145 // Decrypt block
146 px_xtea_decrypt(buf);
147 memcpy(data_out, buf, 8);
148 // Next block
149 nr_of_bytes_out += 8;
150 // Done
151 break;
152 }
153 else
154 {
155 // Copy 8 bytes
156 memcpy(buf, data_in, 8);
157 // Decrypt block
158 px_xtea_decrypt(buf);
159 memcpy(data_out, buf, 8);
160 // Next block
161 nr_of_bytes -= 8;
162 nr_of_bytes_out += 8;
163 data_in += 8;
164 data_out += 8;
165 }
166 }
167 return nr_of_bytes_out;
168}
void px_xtea_decrypt(uint32_t data[2])
Decrypt 64-bit data with 128-bit key.
Definition: px_xtea.c:112
void px_xtea_encrypt(uint32_t data[2])
Encrypt 64-bit data with 128-bit key.
Definition: px_xtea.c:54
size_t px_xtea_decrypt_data_ecb(const uint8_t *data_in, uint8_t *data_out, size_t nr_of_bytes)
Decrypt a data buffer using Electronic Codebook (ECB) block cipher operation.
Definition: px_xtea.c:130
size_t px_xtea_encrypt_data_ecb(const uint8_t *data_in, uint8_t *data_out, size_t nr_of_bytes)
Encrypt a data buffer using Electronic Codebook (ECB) block cipher operation.
Definition: px_xtea.c:72
void px_xtea_init(const uint32_t key[4])
Initialise 128-bit key for encryption/decryption.
Definition: px_xtea.c:44