SX126x-Arduino  2.0.1
aes.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
4 
5  LICENSE TERMS
6 
7  The redistribution and use of this software (with or without changes)
8  is allowed without the payment of fees or royalties provided that:
9 
10  1. source code distributions include the above copyright notice, this
11  list of conditions and the following disclaimer;
12 
13  2. binary distributions include the above copyright notice, this list
14  of conditions and the following disclaimer in their documentation;
15 
16  3. the name of the copyright holder is not used to endorse products
17  built using this software without specific written permission.
18 
19  DISCLAIMER
20 
21  This software is provided 'as is' with no explicit or implied warranties
22  in respect of its properties, including, but not limited to, correctness
23  and/or fitness for purpose.
24  ---------------------------------------------------------------------------
25  Issue 09/09/2006
26 
27  This is an AES implementation that uses only 8-bit byte operations on the
28  cipher state.
29  */
30 
31 #ifndef AES_H
32 #define AES_H
33 
34 extern "C"
35 {
36 #if 1
37 #define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */
38 #endif
39 #if 0
40 #define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
41 #endif
42 #if 0
43 #define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
44 #endif
45 #if 0
46 #define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
47 #endif
48 #if 0
49 #define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
50 #endif
51 #if 0
52 #define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
53 #endif
54 
55 #define N_ROW 4
56 #define N_COL 4
57 #define N_BLOCK (N_ROW * N_COL)
58 #define N_MAX_ROUNDS 14
59 
60  typedef uint8_t return_type;
61 
62  /* Warning: The key length for 256 bit keys overflows a byte
63  (see comment below)
64 */
65 
66  typedef uint8_t length_type;
67 
68  typedef struct
69  {
70  uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
71  uint8_t rnd;
73 
74  /* The following calls are for a precomputed key schedule
75 
76  NOTE: If the length_type used for the key length is an
77  unsigned 8-bit character, a key length of 256 bits must
78  be entered as a length in bytes (valid inputs are hence
79  128, 192, 16, 24 and 32).
80 */
81 
82 #if defined(AES_ENC_PREKEYED) || defined(AES_DEC_PREKEYED)
83 
84  return_type lora_aes_set_key(const uint8_t key[],
85  length_type keylen,
86  lora_aes_context ctx[1]);
87 #endif
88 
89 #if defined(AES_ENC_PREKEYED)
90 
91  return_type lora_aes_encrypt(const uint8_t in[N_BLOCK],
92  uint8_t out[N_BLOCK],
93  const lora_aes_context ctx[1]);
94 
95  return_type lora_aes_cbc_encrypt(const uint8_t *in,
96  uint8_t *out,
97  int32_t n_block,
98  uint8_t iv[N_BLOCK],
99  const lora_aes_context ctx[1]);
100 #endif
101 
102 #if defined(AES_DEC_PREKEYED)
103 
104  return_type lora_aes_decrypt(const uint8_t in[N_BLOCK],
105  uint8_t out[N_BLOCK],
106  const lora_aes_context ctx[1]);
107 
108  return_type lora_aes_cbc_decrypt(const uint8_t *in,
109  uint8_t *out,
110  int32_t n_block,
111  uint8_t iv[N_BLOCK],
112  const lora_aes_context ctx[1]);
113 #endif
114 
115  /* The following calls are for 'on the fly' keying. In this case the
116  encryption and decryption keys are different.
117 
118  The encryption subroutines take a key in an array of bytes in
119  key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
120  192, and 256 bits respectively. They then encrypts the input
121  data, in[] with this key and put the reult in the output array
122  out[]. In addition, the second key array, o_key[L], is used
123  to output the key that is needed by the decryption subroutine
124  to reverse the encryption operation. The two key arrays can
125  be the same array but in this case the original key will be
126  overwritten.
127 
128  In the same way, the decryption subroutines output keys that
129  can be used to reverse their effect when used for encryption.
130 
131  Only 128 and 256 bit keys are supported in these 'on the fly'
132  modes.
133 */
134 
135 #if defined(AES_ENC_128_OTFK)
136  void lora_aes_encrypt_128(const uint8_t in[N_BLOCK],
137  uint8_t out[N_BLOCK],
138  const uint8_t key[N_BLOCK],
139  uint8_t o_key[N_BLOCK]);
140 #endif
141 
142 #if defined(AES_DEC_128_OTFK)
143  void lora_aes_decrypt_128(const uint8_t in[N_BLOCK],
144  uint8_t out[N_BLOCK],
145  const uint8_t key[N_BLOCK],
146  uint8_t o_key[N_BLOCK]);
147 #endif
148 
149 #if defined(AES_ENC_256_OTFK)
150  void lora_aes_encrypt_256(const uint8_t in[N_BLOCK],
151  uint8_t out[N_BLOCK],
152  const uint8_t key[2 * N_BLOCK],
153  uint8_t o_key[2 * N_BLOCK]);
154 #endif
155 
156 #if defined(AES_DEC_256_OTFK)
157  void lora_aes_decrypt_256(const uint8_t in[N_BLOCK],
158  uint8_t out[N_BLOCK],
159  const uint8_t key[2 * N_BLOCK],
160  uint8_t o_key[2 * N_BLOCK]);
161 #endif
162 };
163 
164 #endif
lora_aes_cbc_encrypt
return_type lora_aes_cbc_encrypt(const uint8_t *in, uint8_t *out, int32_t n_block, uint8_t iv[N_BLOCK], const lora_aes_context ctx[1])
Definition: aes.cpp:633
lora_aes_context::rnd
uint8_t rnd
Definition: aes.h:71
lora_aes_set_key
return_type lora_aes_set_key(const uint8_t key[], length_type keylen, lora_aes_context ctx[1])
Definition: aes.cpp:547
length_type
uint8_t length_type
Definition: aes.h:66
N_MAX_ROUNDS
#define N_MAX_ROUNDS
Definition: aes.h:58
N_BLOCK
#define N_BLOCK
Definition: aes.h:57
lora_aes_context
Definition: aes.h:69
return_type
uint8_t return_type
Definition: aes.h:60
lora_aes_encrypt
return_type lora_aes_encrypt(const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK], const lora_aes_context ctx[1])
Definition: aes.cpp:603