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/aes.h"
34 #include "mbedtls/cmac.h"
35 
36 
38 public:
39  /**
40  * Constructor
41  */
42  LoRaMacCrypto();
43 
44  /**
45  * Destructor
46  */
48 
49  /**
50  * Computes the LoRaMAC frame MIC field
51  *
52  * @param [in] buffer - Data buffer
53  * @param [in] size - Data buffer size
54  * @param [in] key - AES key to be used
55  * @param [in] key_length - Length of the key (bits)
56  * @param [in] address - Frame address
57  * @param [in] dir - Frame direction [0: uplink, 1: downlink]
58  * @param [in] seq_counter - Frame sequence counter
59  * @param [out] mic - Computed MIC field
60  *
61  * @return 0 if successful, or a cipher specific error code
62  */
63  int compute_mic(const uint8_t *buffer, uint16_t size,
64  const uint8_t *key, uint32_t key_length,
65  uint32_t address, uint8_t dir, uint32_t seq_counter,
66  uint32_t *mic);
67 
68  /**
69  * Performs payload encryption
70  *
71  * @param [in] buffer - Data buffer
72  * @param [in] size - Data buffer size
73  * @param [in] key - AES key to be used
74  * @param [in] key_length - Length of the key (bits)
75  * @param [in] address - Frame address
76  * @param [in] dir - Frame direction [0: uplink, 1: downlink]
77  * @param [in] seq_counter - Frame sequence counter
78  * @param [out] enc_buffer - Encrypted buffer
79  *
80  * @return 0 if successful, or a cipher specific error code
81  */
82  int encrypt_payload(const uint8_t *buffer, uint16_t size,
83  const uint8_t *key, uint32_t key_length,
84  uint32_t address, uint8_t dir, uint32_t seq_counter,
85  uint8_t *enc_buffer);
86 
87  /**
88  * Performs payload decryption
89  *
90  * @param [in] buffer - Data buffer
91  * @param [in] size - Data buffer size
92  * @param [in] key - AES key to be used
93  * @param [in] key_length - Length of the key (bits)
94  * @param [in] address - Frame address
95  * @param [in] dir - Frame direction [0: uplink, 1: downlink]
96  * @param [in] seq_counter - Frame sequence counter
97  * @param [out] dec_buffer - Decrypted buffer
98  *
99  * @return 0 if successful, or a cipher specific error code
100  */
101  int decrypt_payload(const uint8_t *buffer, uint16_t size,
102  const uint8_t *key, uint32_t key_length,
103  uint32_t address, uint8_t dir, uint32_t seq_counter,
104  uint8_t *dec_buffer);
105 
106  /**
107  * Computes the LoRaMAC Join Request frame MIC field
108  *
109  * @param [in] buffer - Data buffer
110  * @param [in] size - Data buffer size
111  * @param [in] key - AES key to be used
112  * @param [in] key_length - Length of the key (bits)
113  * @param [out] mic - Computed MIC field
114  *
115  * @return 0 if successful, or a cipher specific error code
116  *
117  */
118  int compute_join_frame_mic(const uint8_t *buffer, uint16_t size,
119  const uint8_t *key, uint32_t key_length,
120  uint32_t *mic);
121 
122  /**
123  * Computes the LoRaMAC join frame decryption
124  *
125  * @param [in] buffer - Data buffer
126  * @param [in] size - Data buffer size
127  * @param [in] key - AES key to be used
128  * @param [in] key_length - Length of the key (bits)
129  * @param [out] dec_buffer - Decrypted buffer
130  *
131  * @return 0 if successful, or a cipher specific error code
132  */
133  int decrypt_join_frame(const uint8_t *buffer, uint16_t size,
134  const uint8_t *key, uint32_t key_length,
135  uint8_t *dec_buffer);
136 
137  /**
138  * Computes the LoRaMAC join frame decryption
139  *
140  * @param [in] key - AES key to be used
141  * @param [in] key_length - Length of the key (bits)
142  * @param [in] app_nonce - Application nonce
143  * @param [in] dev_nonce - Device nonce
144  * @param [out] nwk_skey - Network session key
145  * @param [out] app_skey - Application session key
146  *
147  * @return 0 if successful, or a cipher specific error code
148  */
149  int compute_skeys_for_join_frame(const uint8_t *key, uint32_t key_length,
150  const uint8_t *app_nonce, uint16_t dev_nonce,
151  uint8_t *nwk_skey, uint8_t *app_skey);
152 
153 private:
154  /**
155  * AES computation context variable
156  */
157  mbedtls_aes_context aes_ctx;
158 
159  /**
160  * CMAC computation context variable
161  */
162  mbedtls_cipher_context_t aes_cmac_ctx[1];
163 };
164 
165 #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.