Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LoRaMacCrypto.h Source File

LoRaMacCrypto.h

00001 /**
00002 \code
00003 
00004  / _____)             _              | |
00005 ( (____  _____ ____ _| |_ _____  ____| |__
00006  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00007  _____) ) ____| | | || |_| ____( (___| | | |
00008 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00009     (C)2013 Semtech
00010  ___ _____ _   ___ _  _____ ___  ___  ___ ___
00011 / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
00012 \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
00013 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
00014 embedded.connectivity.solutions===============
00015 
00016 \endcode
00017 
00018 Description: LoRa MAC Crypto implementation
00019 
00020 License: Revised BSD License, see LICENSE.TXT file include in the project
00021 
00022 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
00023 
00024 
00025 Copyright (c) 2017, Arm Limited and affiliates.
00026 
00027 SPDX-License-Identifier: BSD-3-Clause
00028 */
00029 
00030 #ifndef MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
00031 #define MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
00032 
00033 #include "mbedtls/aes.h"
00034 #include "mbedtls/cmac.h"
00035 
00036 
00037 class LoRaMacCrypto
00038 {
00039 public:
00040     /**
00041      * Constructor
00042      */
00043     LoRaMacCrypto();
00044 
00045     /**
00046      * Computes the LoRaMAC frame MIC field
00047      *
00048      * @param [in]  buffer          - Data buffer
00049      * @param [in]  size            - Data buffer size
00050      * @param [in]  key             - AES key to be used
00051      * @param [in]  key_length      - Length of the key (bits)
00052      * @param [in]  address         - Frame address
00053      * @param [in]  dir             - Frame direction [0: uplink, 1: downlink]
00054      * @param [in]  seq_counter     - Frame sequence counter
00055      * @param [out] mic             - Computed MIC field
00056      *
00057      * @return                        0 if successful, or a cipher specific error code
00058      */
00059     int compute_mic(const uint8_t *buffer, uint16_t size,
00060                     const uint8_t *key, uint32_t key_length,
00061                     uint32_t address, uint8_t dir, uint32_t seq_counter,
00062                     uint32_t *mic);
00063 
00064     /**
00065      * Performs payload encryption
00066      *
00067      * @param [in]  buffer          - Data buffer
00068      * @param [in]  size            - Data buffer size
00069      * @param [in]  key             - AES key to be used
00070      * @param [in]  key_length      - Length of the key (bits)
00071      * @param [in]  address         - Frame address
00072      * @param [in]  dir             - Frame direction [0: uplink, 1: downlink]
00073      * @param [in]  seq_counter     - Frame sequence counter
00074      * @param [out] enc_buffer      - Encrypted buffer
00075      *
00076      * @return                        0 if successful, or a cipher specific error code
00077      */
00078     int encrypt_payload(const uint8_t *buffer, uint16_t size,
00079                         const uint8_t *key, uint32_t key_length,
00080                         uint32_t address, uint8_t dir, uint32_t seq_counter,
00081                         uint8_t *enc_buffer);
00082 
00083     /**
00084      * Performs payload decryption
00085      *
00086      * @param [in]  buffer          - Data buffer
00087      * @param [in]  size            - Data buffer size
00088      * @param [in]  key             - AES key to be used
00089      * @param [in]  key_length      - Length of the key (bits)
00090      * @param [in]  address         - Frame address
00091      * @param [in]  dir             - Frame direction [0: uplink, 1: downlink]
00092      * @param [in]  seq_counter     - Frame sequence counter
00093      * @param [out] dec_buffer      - Decrypted buffer
00094      *
00095      * @return                        0 if successful, or a cipher specific error code
00096      */
00097     int decrypt_payload(const uint8_t *buffer, uint16_t size,
00098                         const uint8_t *key, uint32_t key_length,
00099                         uint32_t address, uint8_t dir, uint32_t seq_counter,
00100                         uint8_t *dec_buffer);
00101 
00102     /**
00103      * Computes the LoRaMAC Join Request frame MIC field
00104      *
00105      * @param [in]  buffer          - Data buffer
00106      * @param [in]  size            - Data buffer size
00107      * @param [in]  key             - AES key to be used
00108      * @param [in]  key_length      - Length of the key (bits)
00109      * @param [out] mic             - Computed MIC field
00110      *
00111      * @return                        0 if successful, or a cipher specific error code
00112      *
00113      */
00114     int compute_join_frame_mic(const uint8_t *buffer, uint16_t size,
00115                                const uint8_t *key, uint32_t key_length,
00116                                uint32_t *mic);
00117 
00118     /**
00119      * Computes the LoRaMAC join frame decryption
00120      *
00121      * @param [in]  buffer          - Data buffer
00122      * @param [in]  size            - Data buffer size
00123      * @param [in]  key             - AES key to be used
00124      * @param [in]  key_length      - Length of the key (bits)
00125      * @param [out] dec_buffer      - Decrypted buffer
00126      *
00127      * @return                        0 if successful, or a cipher specific error code
00128      */
00129     int decrypt_join_frame(const uint8_t *buffer, uint16_t size,
00130                            const uint8_t *key, uint32_t key_length,
00131                            uint8_t *dec_buffer);
00132 
00133     /**
00134      * Computes the LoRaMAC join frame decryption
00135      *
00136      * @param [in]  key              - AES key to be used
00137      * @param [in]  key_length       - Length of the key (bits)
00138      * @param [in]  app_nonce        - Application nonce
00139      * @param [in]  dev_nonce        - Device nonce
00140      * @param [out] nwk_skey         - Network session key
00141      * @param [out] app_skey         - Application session key
00142      *
00143      * @return                        0 if successful, or a cipher specific error code
00144      */
00145     int compute_skeys_for_join_frame(const uint8_t *key, uint32_t key_length,
00146                                      const uint8_t *app_nonce, uint16_t dev_nonce,
00147                                      uint8_t *nwk_skey, uint8_t *app_skey);
00148 
00149 private:
00150     /**
00151      * MIC field computation initial data
00152      */
00153     uint8_t mic_block_b0[16];
00154 
00155     /**
00156      * Contains the computed MIC field.
00157      *
00158      * \remark Only the 4 first bytes are used
00159      */
00160     uint8_t computed_mic[16];
00161 
00162     /**
00163      * Encryption aBlock and sBlock
00164      */
00165     uint8_t a_block[16];
00166     uint8_t s_block[16];
00167 
00168     /**
00169      * AES computation context variable
00170      */
00171     mbedtls_aes_context aes_ctx;
00172 
00173     /**
00174      * CMAC computation context variable
00175      */
00176     mbedtls_cipher_context_t aes_cmac_ctx[1];
00177 };
00178 
00179 #endif // MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__