mavlink_sha256.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. /*
  3. sha-256 implementation for MAVLink based on Heimdal sources, with
  4. modifications to suit mavlink headers
  5. */
  6. /*
  7. * Copyright (c) 1995 - 2001 Kungliga Tekniska Högskolan
  8. * (Royal Institute of Technology, Stockholm, Sweden).
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. *
  22. * 3. Neither the name of the Institute nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. */
  38. /*
  39. allow implementation to provide their own sha256 with the same API
  40. */
  41. #ifndef HAVE_MAVLINK_SHA256
  42. #ifdef MAVLINK_USE_CXX_NAMESPACE
  43. namespace mavlink {
  44. #endif
  45. #ifndef MAVLINK_HELPER
  46. #define MAVLINK_HELPER
  47. #endif
  48. typedef struct {
  49. uint32_t sz[2];
  50. uint32_t counter[8];
  51. union {
  52. unsigned char save_bytes[64];
  53. uint32_t save_u32[16];
  54. } u;
  55. } mavlink_sha256_ctx;
  56. #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  57. #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  58. #define ROTR(x,n) (((x)>>(n)) | ((x) << (32 - (n))))
  59. #define Sigma0(x) (ROTR(x,2) ^ ROTR(x,13) ^ ROTR(x,22))
  60. #define Sigma1(x) (ROTR(x,6) ^ ROTR(x,11) ^ ROTR(x,25))
  61. #define sigma0(x) (ROTR(x,7) ^ ROTR(x,18) ^ ((x)>>3))
  62. #define sigma1(x) (ROTR(x,17) ^ ROTR(x,19) ^ ((x)>>10))
  63. static const uint32_t mavlink_sha256_constant_256[64] = {
  64. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  65. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  66. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  67. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  68. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  69. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  70. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  71. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  72. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  73. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  74. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  75. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  76. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  77. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  78. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  79. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  80. };
  81. MAVLINK_HELPER void mavlink_sha256_init(mavlink_sha256_ctx *m)
  82. {
  83. m->sz[0] = 0;
  84. m->sz[1] = 0;
  85. m->counter[0] = 0x6a09e667;
  86. m->counter[1] = 0xbb67ae85;
  87. m->counter[2] = 0x3c6ef372;
  88. m->counter[3] = 0xa54ff53a;
  89. m->counter[4] = 0x510e527f;
  90. m->counter[5] = 0x9b05688c;
  91. m->counter[6] = 0x1f83d9ab;
  92. m->counter[7] = 0x5be0cd19;
  93. }
  94. static inline void mavlink_sha256_calc(mavlink_sha256_ctx *m, uint32_t *in)
  95. {
  96. uint32_t AA, BB, CC, DD, EE, FF, GG, HH;
  97. uint32_t data[64];
  98. int i;
  99. AA = m->counter[0];
  100. BB = m->counter[1];
  101. CC = m->counter[2];
  102. DD = m->counter[3];
  103. EE = m->counter[4];
  104. FF = m->counter[5];
  105. GG = m->counter[6];
  106. HH = m->counter[7];
  107. for (i = 0; i < 16; ++i)
  108. data[i] = in[i];
  109. for (i = 16; i < 64; ++i)
  110. data[i] = sigma1(data[i-2]) + data[i-7] +
  111. sigma0(data[i-15]) + data[i - 16];
  112. for (i = 0; i < 64; i++) {
  113. uint32_t T1, T2;
  114. T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + mavlink_sha256_constant_256[i] + data[i];
  115. T2 = Sigma0(AA) + Maj(AA,BB,CC);
  116. HH = GG;
  117. GG = FF;
  118. FF = EE;
  119. EE = DD + T1;
  120. DD = CC;
  121. CC = BB;
  122. BB = AA;
  123. AA = T1 + T2;
  124. }
  125. m->counter[0] += AA;
  126. m->counter[1] += BB;
  127. m->counter[2] += CC;
  128. m->counter[3] += DD;
  129. m->counter[4] += EE;
  130. m->counter[5] += FF;
  131. m->counter[6] += GG;
  132. m->counter[7] += HH;
  133. }
  134. MAVLINK_HELPER void mavlink_sha256_update(mavlink_sha256_ctx *m, const void *v, uint32_t len)
  135. {
  136. const unsigned char *p = (const unsigned char *)v;
  137. uint32_t old_sz = m->sz[0];
  138. uint32_t offset;
  139. m->sz[0] += len * 8;
  140. if (m->sz[0] < old_sz)
  141. ++m->sz[1];
  142. offset = (old_sz / 8) % 64;
  143. while(len > 0){
  144. uint32_t l = 64 - offset;
  145. if (len < l) {
  146. l = len;
  147. }
  148. memcpy(m->u.save_bytes + offset, p, l);
  149. offset += l;
  150. p += l;
  151. len -= l;
  152. if(offset == 64){
  153. int i;
  154. uint32_t current[16];
  155. const uint32_t *u = m->u.save_u32;
  156. for (i = 0; i < 16; i++){
  157. const uint8_t *p1 = (const uint8_t *)&u[i];
  158. uint8_t *p2 = (uint8_t *)&current[i];
  159. p2[0] = p1[3];
  160. p2[1] = p1[2];
  161. p2[2] = p1[1];
  162. p2[3] = p1[0];
  163. }
  164. mavlink_sha256_calc(m, current);
  165. offset = 0;
  166. }
  167. }
  168. }
  169. /*
  170. get first 48 bits of final sha256 hash
  171. */
  172. MAVLINK_HELPER void mavlink_sha256_final_48(mavlink_sha256_ctx *m, uint8_t result[6])
  173. {
  174. unsigned char zeros[72];
  175. unsigned offset = (m->sz[0] / 8) % 64;
  176. unsigned int dstart = (120 - offset - 1) % 64 + 1;
  177. uint8_t *p = (uint8_t *)&m->counter[0];
  178. *zeros = 0x80;
  179. memset (zeros + 1, 0, sizeof(zeros) - 1);
  180. zeros[dstart+7] = (m->sz[0] >> 0) & 0xff;
  181. zeros[dstart+6] = (m->sz[0] >> 8) & 0xff;
  182. zeros[dstart+5] = (m->sz[0] >> 16) & 0xff;
  183. zeros[dstart+4] = (m->sz[0] >> 24) & 0xff;
  184. zeros[dstart+3] = (m->sz[1] >> 0) & 0xff;
  185. zeros[dstart+2] = (m->sz[1] >> 8) & 0xff;
  186. zeros[dstart+1] = (m->sz[1] >> 16) & 0xff;
  187. zeros[dstart+0] = (m->sz[1] >> 24) & 0xff;
  188. mavlink_sha256_update(m, zeros, dstart + 8);
  189. // this ordering makes the result consistent with taking the first
  190. // 6 bytes of more conventional sha256 functions. It assumes
  191. // little-endian ordering of m->counter
  192. result[0] = p[3];
  193. result[1] = p[2];
  194. result[2] = p[1];
  195. result[3] = p[0];
  196. result[4] = p[7];
  197. result[5] = p[6];
  198. }
  199. // prevent conflicts with users of the header
  200. #undef Ch
  201. #undef ROTR
  202. #undef Sigma0
  203. #undef Sigma1
  204. #undef sigma0
  205. #undef sigma1
  206. #ifdef MAVLINK_USE_CXX_NAMESPACE
  207. } // namespace mavlink
  208. #endif
  209. #endif // HAVE_MAVLINK_SHA256