Alarm Sensor Module  0.1
⇒ back to Project Page
encryption.c
Go to the documentation of this file.
1 /****************************************************************************/
31 #include "compat_definitions.h"
32 #include <stdint.h>
33 #if defined(AVR)
34  #include "alarm_sensor.h"
35 #endif
36 #include "encryption.h"
37 #include "encryption_keys.h"
38 
39 
40 #if defined(STM32)
41 
42 const uint32_t * sym_key_tab = NULL;
43 
44 
45 /****************************************************************************/
64 /****************************************************************************/
65 
66 encrypt_return_values get_key_tab(uint32_t id) {
67 
68 
69  if (id == 1) {
70  sym_key_tab = SYM_KEY_TAB1;
71  return ENC_TABLE_FOUND;
72  }
73  if (id == 2) {
74  sym_key_tab = SYM_KEY_TAB2;
75  return ENC_TABLE_FOUND;
76  }
77  if (id == 3) {
78  sym_key_tab = SYM_KEY_TAB3;
79  return ENC_TABLE_FOUND;
80  }
81  if (id == 4) {
82  sym_key_tab = SYM_KEY_TAB4;
83  return ENC_TABLE_FOUND;
84  }
85  if (id == 5) {
86  sym_key_tab = SYM_KEY_TAB5;
87  return ENC_TABLE_FOUND;
88  }
89  if (id == 6) {
90  sym_key_tab = SYM_KEY_TAB6;
91  return ENC_TABLE_FOUND;
92  }
93  if (id == 7) {
94  sym_key_tab = SYM_KEY_TAB7;
95  return ENC_TABLE_FOUND;
96  }
97  if (id == 8) {
98  sym_key_tab = SYM_KEY_TAB8;
99  return ENC_TABLE_FOUND;
100  }
101  if (id == 9) {
102  sym_key_tab = SYM_KEY_TAB9;
103  return ENC_TABLE_FOUND;
104  }
105  if (id == 10) {
106  sym_key_tab = SYM_KEY_TAB10;
107  return ENC_TABLE_FOUND;
108  }
109  if (id == 11) {
110  sym_key_tab = SYM_KEY_TAB11;
111  return ENC_TABLE_FOUND;
112  }
113  if (id == 12) {
114  sym_key_tab = SYM_KEY_TAB12;
115  return ENC_TABLE_FOUND;
116  }
117  if (id == 13) {
118  sym_key_tab = SYM_KEY_TAB13;
119  return ENC_TABLE_FOUND;
120  }
121  if (id == 14) {
122  sym_key_tab = SYM_KEY_TAB14;
123  return ENC_TABLE_FOUND;
124  }
125  if (id == 15) {
126  sym_key_tab = SYM_KEY_TAB15;
127  return ENC_TABLE_FOUND;
128  }
129  if (id == 16) {
130  sym_key_tab = SYM_KEY_TAB16;
131  return ENC_TABLE_FOUND;
132  }
133  if (id == 17) {
134  sym_key_tab = SYM_KEY_TAB17;
135  return ENC_TABLE_FOUND;
136  }
137  if (id == 18) {
138  sym_key_tab = SYM_KEY_TAB18;
139  return ENC_TABLE_FOUND;
140  }
141  if (id == 19) {
142  sym_key_tab = SYM_KEY_TAB19;
143  return ENC_TABLE_FOUND;
144  }
145  if (id == 20) {
146  sym_key_tab = SYM_KEY_TAB20;
147  return ENC_TABLE_FOUND;
148  }
149  if (id == 21) {
150  sym_key_tab = SYM_KEY_TAB21;
151  return ENC_TABLE_FOUND;
152  }
153 
154 
155 
156  return ENC_NO_TABLE_FOUND;
157 
158 }
159 
160 
161 #endif
162 
163 
164 
165 /****************************************************************************/
193 /****************************************************************************/
194 
195 uint8_t sym_encrypt_buffer(uint32_t *input_buffer, uint32_t *output_buffer, uint8_t buffer_length) {
196 
197  uint8_t i, j;
198 
199  //for each buffer element apply the full number of keys
200  for (i = 0; i < buffer_length; i++) {
201 
202  output_buffer[i] = input_buffer[i];
203 
204  if (i == 0) {
205 #if defined(STM32)
206  output_buffer[i] = (output_buffer[i] ^ sym_key_tab[0]);
207 #else
208  output_buffer[i] = (output_buffer[i] ^ pgm_read_dword(&sym_key_tab[0]));
209 #endif
210  } else {
211  output_buffer[i] = (output_buffer[i] ^ output_buffer[i-1]);
212  }
213 
214  for (j = 1; j < NUMBER_OF_SYM_KEYS; j++) {
215 
216 #if defined(STM32)
217  output_buffer[i] = (output_buffer[i] ^ sym_key_tab[j]);
218 #else
219  output_buffer[i] = (output_buffer[i] ^ pgm_read_dword(&sym_key_tab[j]));
220 #endif
221  }
222  }
223 
224  return 0;
225 
226 }
227 
228 
229 /****************************************************************************/
256 /****************************************************************************/
257 
258 uint8_t sym_decrypt_buffer(uint32_t *input_buffer, uint32_t *output_buffer, uint8_t buffer_length) {
259 
260  int16_t i, j;
261 
262  //for each buffer element apply the full number of keys
263  for (i = 0; i < (int16_t) buffer_length; i++) {
264 
265  output_buffer[i] = input_buffer[i];
266 
267  for (j = (int16_t) (NUMBER_OF_SYM_KEYS-1); j > 0; j--) {
268 #if defined(STM32)
269  output_buffer[i] = (output_buffer[i] ^ sym_key_tab[j]);
270 #else
271  output_buffer[i] = (output_buffer[i] ^ pgm_read_dword(&sym_key_tab[j]));
272 #endif
273  }
274 
275  if (i == 0) {
276 #if defined(STM32)
277  output_buffer[i] = (output_buffer[i] ^ sym_key_tab[0]);
278 #else
279  output_buffer[i] = (output_buffer[i] ^ pgm_read_dword(&sym_key_tab[0]));
280 #endif
281  } else {
282  output_buffer[i] = (output_buffer[i] ^ input_buffer[i-1]);
283  }
284 
285  }
286 
287 
288 
289  return 0;
290 
291 }
uint8_t sym_decrypt_buffer(uint32_t *input_buffer, uint32_t *output_buffer, uint8_t buffer_length)
Definition: encryption.c:258
#define SYM_KEY_TAB18
#define SYM_KEY_TAB13
encrypt_return_values
Definition: encryption.h:44
#define SYM_KEY_TAB2
#define SYM_KEY_TAB21
#define SYM_KEY_TAB1
#define SYM_KEY_TAB10
uint8_t sym_encrypt_buffer(uint32_t *input_buffer, uint32_t *output_buffer, uint8_t buffer_length)
Definition: encryption.c:195
#define SYM_KEY_TAB6
#define SYM_KEY_TAB14
#define SYM_KEY_TAB11
#define SYM_KEY_TAB3
#define SYM_KEY_TAB19
#define SYM_KEY_TAB9
#define NUMBER_OF_SYM_KEYS
specifies the number of keys in this list
#define SYM_KEY_TAB7
#define SYM_KEY_TAB12
#define NULL
#define SYM_KEY_TAB5
#define SYM_KEY_TAB15
#define SYM_KEY_TAB20
#define SYM_KEY_TAB16
#define SYM_KEY_TAB4
#define SYM_KEY_TAB8
Contains the main definitions of the alarm sensor module.
#define SYM_KEY_TAB17