Mistake on this page?
Report an issue in GitHub or email us
LoRaMacCrypto.h
1 /**
2 \code
3 
4  / _____) _ | |
5 ( (____ _____ ____ _| |_ _____ ____| |__
6  \____ \| ___ | (_ _) ___ |/ ___) _ \
7  _____) ) ____| | | || |_| ____( (___| | | |
8 (______/|_____)_|_|_| \__)_____)\____)_| |_|
9  (C)2013 Semtech
10  ___ _____ _ ___ _ _____ ___ ___ ___ ___
11 / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
12 \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
13 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
14 embedded.connectivity.solutions===============
15 
16 \endcode
17 
18 Description: LoRa MAC Crypto implementation
19 
20 License: Revised BSD License, see LICENSE.TXT file include in the project
21 
22 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
23 
24 
25 Copyright (c) 2017, Arm Limited and affiliates.
26 
27 SPDX-License-Identifier: BSD-3-Clause
28 */
29 
30 #ifndef MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
31 #define MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
32 
33 #include "mbedtls/config.h"
34 #include "mbedtls/aes.h"
35 #include "mbedtls/cmac.h"
36 
37 
39 public:
40  /**
41  * Constructor
42  */
43  LoRaMacCrypto();
44 
45  /**
46  * Destructor
47  */
49 
50  /**
51  * Computes the LoRaMAC frame MIC field
52  *
53  * @param [in] buffer - Data buffer
54  * @param [in] size - Data buffer size
55  * @param [in] key - AES key to be used
56  * @param [in] key_length - Length of the key (bits)
57  * @param [in] address - Frame address
58  * @param [in] dir - Frame direction [0: uplink, 1: downlink]
59  * @param [in] seq_counter - Frame sequence counter
60  * @param [out] mic - Computed MIC field
61  *
62  * @return 0 if successful, or a cipher specific error code
63  */
64  int compute_mic(const uint8_t *buffer, uint16_t size,
65  const uint8_t *key, uint32_t key_length,
66  uint32_t address, uint8_t dir, uint32_t seq_counter,
67  uint32_t *mic);
68 
69  /**
70  * Performs payload encryption
71  *
72  * @param [in] buffer - Data buffer
73  * @param [in] size - Data buffer size
74  * @param [in] key - AES key to be used
75  * @param [in] key_length - Length of the key (bits)
76  * @param [in] address - Frame address
77  * @param [in] dir - Frame direction [0: uplink, 1: downlink]
78  * @param [in] seq_counter - Frame sequence counter
79  * @param [out] enc_buffer - Encrypted buffer
80  *
81  * @return 0 if successful, or a cipher specific error code
82  */
83  int encrypt_payload(const uint8_t *buffer, uint16_t size,
84  const uint8_t *key, uint32_t key_length,
85  uint32_t address, uint8_t dir, uint32_t seq_counter,
86  uint8_t *enc_buffer);
87 
88  /**
89  * Performs payload decryption
90  *
91  * @param [in] buffer - Data buffer
92  * @param [in] size - Data buffer size
93  * @param [in] key - AES key to be used
94  * @param [in] key_length - Length of the key (bits)
95  * @param [in] address - Frame address
96  * @param [in] dir - Frame direction [0: uplink, 1: downlink]
97  * @param [in] seq_counter - Frame sequence counter
98  * @param [out] dec_buffer - Decrypted buffer
99  *
100  * @return 0 if successful, or a cipher specific error code
101  */
102  int decrypt_payload(const uint8_t *buffer, uint16_t size,
103  const uint8_t *key, uint32_t key_length,
104  uint32_t address, uint8_t dir, uint32_t seq_counter,
105  uint8_t *dec_buffer);
106 
107  /**
108  * Computes the LoRaMAC Join Request frame MIC field
109  *
110  * @param [in] buffer - Data buffer
111  * @param [in] size - Data buffer size
112  * @param [in] key - AES key to be used
113  * @param [in] key_length - Length of the key (bits)
114  * @param [out] mic - Computed MIC field
115  *
116  * @return 0 if successful, or a cipher specific error code
117  *
118  */
119  int compute_join_frame_mic(const uint8_t *buffer, uint16_t size,
120  const uint8_t *key, uint32_t key_length,
121  uint32_t *mic);
122 
123  /**
124  * Computes the LoRaMAC join frame decryption
125  *
126  * @param [in] buffer - Data buffer
127  * @param [in] size - Data buffer size
128  * @param [in] key - AES key to be used
129  * @param [in] key_length - Length of the key (bits)
130  * @param [out] dec_buffer - Decrypted buffer
131  *
132  * @return 0 if successful, or a cipher specific error code
133  */
134  int decrypt_join_frame(const uint8_t *buffer, uint16_t size,
135  const uint8_t *key, uint32_t key_length,
136  uint8_t *dec_buffer);
137 
138  /**
139  * Computes the LoRaMAC join frame decryption
140  *
141  * @param [in] key - AES key to be used
142  * @param [in] key_length - Length of the key (bits)
143  * @param [in] app_nonce - Application nonce
144  * @param [in] dev_nonce - Device nonce
145  * @param [out] nwk_skey - Network session key
146  * @param [out] app_skey - Application session key
147  *
148  * @return 0 if successful, or a cipher specific error code
149  */
150  int compute_skeys_for_join_frame(const uint8_t *key, uint32_t key_length,
151  const uint8_t *app_nonce, uint16_t dev_nonce,
152  uint8_t *nwk_skey, uint8_t *app_skey);
153 
154 private:
155  /**
156  * AES computation context variable
157  */
158  mbedtls_aes_context aes_ctx;
159 
160  /**
161  * CMAC computation context variable
162  */
163  mbedtls_cipher_context_t aes_cmac_ctx[1];
164 };
165 
166 #endif // MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
int compute_mic(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, uint32_t *mic)
Computes the LoRaMAC frame MIC field.
int decrypt_join_frame(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint8_t *dec_buffer)
Computes the LoRaMAC join frame decryption.
int compute_join_frame_mic(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t *mic)
Computes the LoRaMAC Join Request frame MIC field.
int encrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, uint8_t *enc_buffer)
Performs payload encryption.
int compute_skeys_for_join_frame(const uint8_t *key, uint32_t key_length, const uint8_t *app_nonce, uint16_t dev_nonce, uint8_t *nwk_skey, uint8_t *app_skey)
Computes the LoRaMAC join frame decryption.
int decrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, uint8_t *dec_buffer)
Performs payload decryption.
LoRaMacCrypto()
Constructor.
~LoRaMacCrypto()
Destructor.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.