lw_crypto.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #line __LINE__ "lw_crypto.c"
  2. /**************************************************************************
  3. Copyright (c) <2016> <Jiapeng Li>
  4. https://github.com/JiapengLi/lorawan-parser
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. *****************************************************************************/
  21. #include <stdint.h>
  22. #include <string.h> // memset
  23. #include "lw_crypto.h"
  24. #include "cmac.h"
  25. #define LW_KEY_LEN (16)
  26. #define LW_MIC_LEN (4)
  27. static void lw_write_dw(uint8_t *output, uint32_t input)
  28. {
  29. uint8_t* ptr = output;
  30. *(ptr++) = (uint8_t)(input), input >>= 8;
  31. *(ptr++) = (uint8_t)(input), input >>= 8;
  32. *(ptr++) = (uint8_t)(input), input >>= 8;
  33. *(ptr++) = (uint8_t)(input);
  34. }
  35. //static uint32_t lw_read_dw(uint8_t *buf)
  36. //{
  37. // uint32_t ret;
  38. //
  39. // ret = ( (uint32_t)buf[0] << 0 );
  40. // ret |= ( (uint32_t)buf[1] << 8 );
  41. // ret |= ( (uint32_t)buf[2] << 16 );
  42. // ret |= ( (uint32_t)buf[3] << 24 );
  43. //
  44. // return ret;
  45. //}
  46. void lw_msg_mic(lw_mic_t* mic, lw_key_t *key)
  47. {
  48. uint8_t b0[LW_KEY_LEN];
  49. memset(b0, 0 , LW_KEY_LEN);
  50. b0[0] = 0x49;
  51. // todo add LoRaWAN 1.1 support for b0[1..4]
  52. // LoRaWAN 1.1 spec, 4.4:
  53. // If the device is connected to a LoRaWAN1.1 Network Server and the ACK bit of the downlink frame is set,
  54. // meaning this frame is acknowledging an uplink “confirmed” frame,
  55. // then ConfFCnt is the frame counter value modulo 2^16 of the “confirmed” uplink frame that is being acknowledged.
  56. // In all other cases ConfFCnt = 0x0000.
  57. #if USE_LORAWAN_1_1 == 1
  58. #error "missing lorawan implementation!"
  59. #endif
  60. b0[5] = key->link;
  61. lw_write_dw(b0+6, key->devaddr.data);
  62. lw_write_dw(b0+10, key->fcnt32);
  63. b0[15] = (uint8_t)key->len;
  64. AES_CMAC_CTX cmacctx;
  65. AES_CMAC_Init(&cmacctx);
  66. AES_CMAC_SetKey(&cmacctx, key->aeskey);
  67. AES_CMAC_Update(&cmacctx, b0, LW_KEY_LEN);
  68. AES_CMAC_Update(&cmacctx, key->in, key->len);
  69. uint8_t temp[LW_KEY_LEN];
  70. AES_CMAC_Final(temp, &cmacctx);
  71. memcpy(mic->buf, temp, LW_MIC_LEN);
  72. }
  73. void lw_join_mic(lw_mic_t* mic, lw_key_t *key)
  74. {
  75. AES_CMAC_CTX cmacctx;
  76. AES_CMAC_Init(&cmacctx);
  77. AES_CMAC_SetKey(&cmacctx, key->aeskey);
  78. AES_CMAC_Update(&cmacctx, key->in, key->len);
  79. uint8_t temp[LW_KEY_LEN];
  80. AES_CMAC_Final(temp, &cmacctx);
  81. memcpy(mic->buf, temp, LW_MIC_LEN);
  82. }
  83. // Use to generate JoinAccept Payload
  84. int lw_join_encrypt(uint8_t *out, lw_key_t *key)
  85. {
  86. if((key->len == 0) || (key->len%LW_KEY_LEN != 0)){
  87. return -1;
  88. }
  89. aes_context aesContext;
  90. aes_set_key(key->aeskey, LW_KEY_LEN, &aesContext);
  91. // Check if optional CFList is included
  92. int i;
  93. for(i=0; i<key->len; i+=LW_KEY_LEN){
  94. aes_decrypt( key->in + i, out + i, &aesContext );
  95. }
  96. return key->len;
  97. }
  98. // Use to decrypt JoinAccept Payload
  99. int lw_join_decrypt(uint8_t *out, lw_key_t *key)
  100. {
  101. if((key->len == 0) || (key->len%LW_KEY_LEN != 0)){
  102. return -1;
  103. }
  104. aes_context aesContext;
  105. aes_set_key(key->aeskey, LW_KEY_LEN, &aesContext);
  106. // Check if optional CFList is included
  107. int i;
  108. for(i=0; i<key->len; i+=LW_KEY_LEN){
  109. aes_encrypt( key->in + i, out + i, &aesContext );
  110. }
  111. return key->len;
  112. }
  113. static void lw_block_xor(uint8_t const l[], uint8_t const r[], uint8_t out[], uint16_t bytes)
  114. {
  115. uint8_t const* lptr = l;
  116. uint8_t const* rptr = r;
  117. uint8_t* optr = out;
  118. uint8_t const* const end = out + bytes;
  119. for (;optr < end; lptr++, rptr++, optr++)
  120. *optr = *lptr ^ *rptr;
  121. }
  122. int lw_encrypt(uint8_t *out, lw_key_t *key)
  123. {
  124. if (key->len == 0)
  125. return -1;
  126. uint8_t A[LW_KEY_LEN];
  127. uint16_t const over_hang_bytes = key->len%LW_KEY_LEN;
  128. int blocks = key->len/LW_KEY_LEN + 1;
  129. memset(A, 0, LW_KEY_LEN);
  130. A[0] = 0x01; //encryption flags
  131. A[5] = key->link;
  132. lw_write_dw(A+6, key->devaddr.data);
  133. lw_write_dw(A+10, key->fcnt32);
  134. uint8_t const* blockInput = key->in;
  135. uint8_t* blockOutput = out;
  136. uint16_t i;
  137. for(i = 1; i <= blocks; i++, blockInput += LW_KEY_LEN, blockOutput += LW_KEY_LEN){
  138. A[15] = (uint8_t)(i);
  139. aes_context aesContext;
  140. aes_set_key(key->aeskey, LW_KEY_LEN, &aesContext);
  141. uint8_t S[LW_KEY_LEN];
  142. aes_encrypt(A, S, &aesContext);
  143. uint16_t bytes_to_encrypt;
  144. if ((i < blocks) || (over_hang_bytes == 0))
  145. bytes_to_encrypt = LW_KEY_LEN;
  146. else
  147. bytes_to_encrypt = over_hang_bytes;
  148. lw_block_xor(S, blockInput, blockOutput, bytes_to_encrypt);
  149. }
  150. return key->len;
  151. }
  152. void lw_get_skeys(uint8_t *nwkskey, uint8_t *appskey, lw_skey_seed_t *seed)
  153. {
  154. aes_context aesContext;
  155. uint8_t b[LW_KEY_LEN];
  156. memset(&aesContext, 0, sizeof(aesContext));
  157. memset(b, 0, LW_KEY_LEN);
  158. b[1] = (uint8_t)(seed->anonce.data>>0);
  159. b[2] = (uint8_t)(seed->anonce.data>>8);
  160. b[3] = (uint8_t)(seed->anonce.data>>16);
  161. b[4] = (uint8_t)(seed->netid.data>>0);
  162. b[5] = (uint8_t)(seed->netid.data>>8);
  163. b[6] = (uint8_t)(seed->netid.data>>16);
  164. b[7] = (uint8_t)(seed->dnonce.data>>0);
  165. b[8] = (uint8_t)(seed->dnonce.data>>8);
  166. b[0] = 0x01;
  167. aes_set_key(seed->aeskey, LW_KEY_LEN, &aesContext);
  168. aes_encrypt( b, nwkskey, &aesContext );
  169. b[0] = 0x02;
  170. aes_set_key(seed->aeskey, LW_KEY_LEN, &aesContext);
  171. aes_encrypt( b, appskey, &aesContext );
  172. }