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