lw_crypto.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**************************************************************************
  2. Copyright (c) <2016> <Jiapeng Li>
  3. https://github.com/JiapengLi/lorawan-parser
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. *****************************************************************************/
  20. #ifndef DRV_LOBAWAN_LW_CRYPTO_H_
  21. #define DRV_LOBAWAN_LW_CRYPTO_H_
  22. typedef union{
  23. uint32_t data;
  24. uint8_t buf[4];
  25. struct{
  26. #ifdef ENABLE_BIG_ENDIAN
  27. uint32_t nwkaddr : 25;
  28. uint32_t nwkid : 7;
  29. #else
  30. uint32_t nwkid : 7;
  31. uint32_t nwkaddr : 25;
  32. #endif
  33. }bits;
  34. }__attribute__((packed)) lw_devaddr_t;
  35. typedef union{
  36. uint32_t data;
  37. uint8_t buf[4];
  38. }__attribute__((packed)) lw_mic_t;
  39. typedef enum{
  40. LW_UPLINK = 0,
  41. LW_DOWNLINK = 1,
  42. }lw_link_t;
  43. typedef union{
  44. uint32_t data;
  45. }__attribute__((packed)) lw_anonce_t;
  46. typedef union{
  47. uint16_t data;
  48. }__attribute__((packed)) lw_dnonce_t;
  49. typedef lw_anonce_t lw_netid_t;
  50. typedef struct{
  51. uint8_t *aeskey;
  52. lw_anonce_t anonce;
  53. lw_netid_t netid;
  54. lw_dnonce_t dnonce;
  55. }lw_skey_seed_t;
  56. typedef struct{
  57. uint8_t *aeskey;
  58. uint8_t *in;
  59. uint16_t len;
  60. lw_devaddr_t devaddr;
  61. lw_link_t link;
  62. uint32_t fcnt32;
  63. }lw_key_t;
  64. void lw_msg_mic(lw_mic_t* mic, lw_key_t *key);
  65. void lw_join_mic(lw_mic_t* mic, lw_key_t *key);
  66. int lw_encrypt(uint8_t *out, lw_key_t *key);
  67. int lw_join_decrypt(uint8_t *out, lw_key_t *key);
  68. int lw_join_encrypt(uint8_t *out, lw_key_t *key);
  69. void lw_get_skeys(uint8_t *nwkskey, uint8_t *appskey, lw_skey_seed_t *seed);
  70. #endif /* DRV_LOBAWAN_LW_CRYPTO_H_ */