utils_sha2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #if 0
  16. #define UNROLL_LOOPS /* Enable loops unrolling */
  17. #endif
  18. #define INFRA_SHA256_SMALLER
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "utils_sha2.h"
  22. #define SHA256_KEY_IOPAD_SIZE (64)
  23. #define SHA256_DIGEST_SIZE (32)
  24. /*
  25. * 32-bit integer manipulation macros (big endian)
  26. */
  27. #ifndef GET_UINT32_BE
  28. #define GET_UINT32_BE(n,b,i) \
  29. do { \
  30. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  31. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  32. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  33. | ( (uint32_t) (b)[(i) + 3] ); \
  34. } while( 0 )
  35. #endif
  36. #ifndef PUT_UINT32_BE
  37. #define PUT_UINT32_BE(n,b,i) \
  38. do { \
  39. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  40. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  41. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  42. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  43. } while( 0 )
  44. #endif
  45. static void utils_sha256_zeroize(void *v, uint32_t n)
  46. {
  47. volatile unsigned char *p = v;
  48. while (n--) {
  49. *p++ = 0;
  50. }
  51. }
  52. void utils_sha256_init(iot_sha256_context *ctx)
  53. {
  54. memset(ctx, 0, sizeof(iot_sha256_context));
  55. }
  56. void utils_sha256_free(iot_sha256_context *ctx)
  57. {
  58. if (NULL == ctx) {
  59. return;
  60. }
  61. utils_sha256_zeroize(ctx, sizeof(iot_sha256_context));
  62. }
  63. void utils_sha256_starts(iot_sha256_context *ctx)
  64. {
  65. int is224 = 0;
  66. ctx->total[0] = 0;
  67. ctx->total[1] = 0;
  68. if (is224 == 0) {
  69. /* SHA-256 */
  70. ctx->state[0] = 0x6A09E667;
  71. ctx->state[1] = 0xBB67AE85;
  72. ctx->state[2] = 0x3C6EF372;
  73. ctx->state[3] = 0xA54FF53A;
  74. ctx->state[4] = 0x510E527F;
  75. ctx->state[5] = 0x9B05688C;
  76. ctx->state[6] = 0x1F83D9AB;
  77. ctx->state[7] = 0x5BE0CD19;
  78. }
  79. ctx->is224 = is224;
  80. }
  81. static const uint32_t K[] = {
  82. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  83. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  84. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  85. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  86. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  87. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  88. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  89. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  90. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  91. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  92. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  93. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  94. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  95. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  96. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  97. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  98. };
  99. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  100. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  101. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  102. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  103. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  104. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  105. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  106. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  107. #define R(t) \
  108. ( \
  109. W[t] = S1(W[t - 2]) + W[t - 7] + \
  110. S0(W[t - 15]) + W[t - 16] \
  111. )
  112. #define P(a,b,c,d,e,f,g,h,x,K) \
  113. { \
  114. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  115. temp2 = S2(a) + F0(a,b,c); \
  116. d += temp1; h = temp1 + temp2; \
  117. }
  118. void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64])
  119. {
  120. uint32_t temp1, temp2, W[64];
  121. uint32_t A[8];
  122. unsigned int i;
  123. for (i = 0; i < 8; i++) {
  124. A[i] = ctx->state[i];
  125. }
  126. #if defined(INFRA_SHA256_SMALLER)
  127. for (i = 0; i < 64; i++) {
  128. if (i < 16) {
  129. GET_UINT32_BE(W[i], data, 4 * i);
  130. } else {
  131. R(i);
  132. }
  133. P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i]);
  134. temp1 = A[7];
  135. A[7] = A[6];
  136. A[6] = A[5];
  137. A[5] = A[4];
  138. A[4] = A[3];
  139. A[3] = A[2];
  140. A[2] = A[1];
  141. A[1] = A[0];
  142. A[0] = temp1;
  143. }
  144. #else /* INFRA_SHA256_SMALLER */
  145. for (i = 0; i < 16; i++) {
  146. GET_UINT32_BE(W[i], data, 4 * i);
  147. }
  148. for (i = 0; i < 16; i += 8) {
  149. P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i + 0], K[i + 0]);
  150. P(A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], W[i + 1], K[i + 1]);
  151. P(A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], W[i + 2], K[i + 2]);
  152. P(A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], W[i + 3], K[i + 3]);
  153. P(A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], W[i + 4], K[i + 4]);
  154. P(A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], W[i + 5], K[i + 5]);
  155. P(A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], W[i + 6], K[i + 6]);
  156. P(A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], W[i + 7], K[i + 7]);
  157. }
  158. for (i = 16; i < 64; i += 8) {
  159. P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], R(i + 0), K[i + 0]);
  160. P(A[7], A[0], A[1], A[2], A[3], A[4], A[5], A[6], R(i + 1), K[i + 1]);
  161. P(A[6], A[7], A[0], A[1], A[2], A[3], A[4], A[5], R(i + 2), K[i + 2]);
  162. P(A[5], A[6], A[7], A[0], A[1], A[2], A[3], A[4], R(i + 3), K[i + 3]);
  163. P(A[4], A[5], A[6], A[7], A[0], A[1], A[2], A[3], R(i + 4), K[i + 4]);
  164. P(A[3], A[4], A[5], A[6], A[7], A[0], A[1], A[2], R(i + 5), K[i + 5]);
  165. P(A[2], A[3], A[4], A[5], A[6], A[7], A[0], A[1], R(i + 6), K[i + 6]);
  166. P(A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[0], R(i + 7), K[i + 7]);
  167. }
  168. #endif /* INFRA_SHA256_SMALLER */
  169. for (i = 0; i < 8; i++) {
  170. ctx->state[i] += A[i];
  171. }
  172. }
  173. void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen)
  174. {
  175. size_t fill;
  176. uint32_t left;
  177. if (ilen == 0) {
  178. return;
  179. }
  180. left = ctx->total[0] & 0x3F;
  181. fill = 64 - left;
  182. ctx->total[0] += (uint32_t) ilen;
  183. ctx->total[0] &= 0xFFFFFFFF;
  184. if (ctx->total[0] < (uint32_t) ilen) {
  185. ctx->total[1]++;
  186. }
  187. if (left && ilen >= fill) {
  188. memcpy((void *)(ctx->buffer + left), input, fill);
  189. utils_sha256_process(ctx, ctx->buffer);
  190. input += fill;
  191. ilen -= fill;
  192. left = 0;
  193. }
  194. while (ilen >= 64) {
  195. utils_sha256_process(ctx, input);
  196. input += 64;
  197. ilen -= 64;
  198. }
  199. if (ilen > 0) {
  200. memcpy((void *)(ctx->buffer + left), input, ilen);
  201. }
  202. }
  203. static const unsigned char sha256_padding[64] = {
  204. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  206. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  207. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  208. };
  209. void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32])
  210. {
  211. uint32_t last, padn;
  212. uint32_t high, low;
  213. unsigned char msglen[8];
  214. high = (ctx->total[0] >> 29)
  215. | (ctx->total[1] << 3);
  216. low = (ctx->total[0] << 3);
  217. PUT_UINT32_BE(high, msglen, 0);
  218. PUT_UINT32_BE(low, msglen, 4);
  219. last = ctx->total[0] & 0x3F;
  220. padn = (last < 56) ? (56 - last) : (120 - last);
  221. utils_sha256_update(ctx, sha256_padding, padn);
  222. utils_sha256_update(ctx, msglen, 8);
  223. PUT_UINT32_BE(ctx->state[0], output, 0);
  224. PUT_UINT32_BE(ctx->state[1], output, 4);
  225. PUT_UINT32_BE(ctx->state[2], output, 8);
  226. PUT_UINT32_BE(ctx->state[3], output, 12);
  227. PUT_UINT32_BE(ctx->state[4], output, 16);
  228. PUT_UINT32_BE(ctx->state[5], output, 20);
  229. PUT_UINT32_BE(ctx->state[6], output, 24);
  230. if (ctx->is224 == 0) {
  231. PUT_UINT32_BE(ctx->state[7], output, 28);
  232. }
  233. }
  234. void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32])
  235. {
  236. iot_sha256_context ctx;
  237. utils_sha256_init(&ctx);
  238. utils_sha256_starts(&ctx);
  239. utils_sha256_update(&ctx, input, ilen);
  240. utils_sha256_finish(&ctx, output);
  241. utils_sha256_free(&ctx);
  242. }
  243. void utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32])
  244. {
  245. iot_sha256_context context;
  246. uint8_t k_ipad[SHA256_KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
  247. uint8_t k_opad[SHA256_KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
  248. int32_t i;
  249. if ((NULL == msg) || (NULL == key) || (NULL == output)) {
  250. return;
  251. }
  252. if (key_len > SHA256_KEY_IOPAD_SIZE) {
  253. return;
  254. }
  255. /* start out by storing key in pads */
  256. memset(k_ipad, 0, sizeof(k_ipad));
  257. memset(k_opad, 0, sizeof(k_opad));
  258. memcpy(k_ipad, key, key_len);
  259. memcpy(k_opad, key, key_len);
  260. /* XOR key with ipad and opad values */
  261. for (i = 0; i < SHA256_KEY_IOPAD_SIZE; i++) {
  262. k_ipad[i] ^= 0x36;
  263. k_opad[i] ^= 0x5c;
  264. }
  265. /* perform inner SHA */
  266. utils_sha256_init(&context); /* init context for 1st pass */
  267. utils_sha256_starts(&context); /* setup context for 1st pass */
  268. utils_sha256_update(&context, k_ipad, SHA256_KEY_IOPAD_SIZE); /* start with inner pad */
  269. utils_sha256_update(&context, msg, msg_len); /* then text of datagram */
  270. utils_sha256_finish(&context, output); /* finish up 1st pass */
  271. /* perform outer SHA */
  272. utils_sha256_init(&context); /* init context for 2nd pass */
  273. utils_sha256_starts(&context); /* setup context for 2nd pass */
  274. utils_sha256_update(&context, k_opad, SHA256_KEY_IOPAD_SIZE); /* start with outer pad */
  275. utils_sha256_update(&context, output, SHA256_DIGEST_SIZE); /* then results of 1st hash */
  276. utils_sha256_finish(&context, output); /* finish up 2nd pass */
  277. }