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