aiot_mqtt_sign.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (C) 2015-2019 Alibaba Group Holding Limited
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "sys.h"
  7. #define PRODUCTKEY_MAXLEN (20)
  8. #define DEVICENAME_MAXLEN (32)
  9. #define DEVICESECRET_MAXLEN (64)
  10. #define SIGN_SOURCE_MAXLEN (200)
  11. #define CLIENTID_MAXLEN (150)
  12. #define USERNAME_MAXLEN (64)
  13. #define PASSWORD_MAXLEN (65)
  14. #define TIMESTAMP_VALUE "2524608000000"
  15. #define MQTT_CLINETID_KV "|timestamp=2524608000000,_v=paho-c-1.0.0,securemode=3,signmethod=hmacsha256,lan=C|"
  16. static void utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32]);
  17. static void _hex2str(uint8_t *input, uint16_t input_len, char *output)
  18. {
  19. char *zEncode = "0123456789ABCDEF";
  20. int i = 0, j = 0;
  21. for (i = 0; i < input_len; i++) {
  22. output[j++] = zEncode[(input[i] >> 4) & 0xf];
  23. output[j++] = zEncode[(input[i]) & 0xf];
  24. }
  25. }
  26. int aiotMqttSign(const char *productKey, const char *deviceName, const char *deviceSecret,
  27. char clientId[150], char username[64], char password[65])
  28. {
  29. char deviceId[PRODUCTKEY_MAXLEN + DEVICENAME_MAXLEN + 2] = {0};
  30. char macSrc[SIGN_SOURCE_MAXLEN] = {0};
  31. uint8_t macRes[32] = {0};
  32. /* check parameters */
  33. if (productKey == NULL || deviceName == NULL || deviceSecret == NULL ||
  34. clientId == NULL || username == NULL || password == NULL) {
  35. return -1;
  36. }
  37. if ((strlen(productKey) > PRODUCTKEY_MAXLEN) || (strlen(deviceName) > DEVICENAME_MAXLEN) ||
  38. (strlen(deviceSecret) > DEVICESECRET_MAXLEN)) {
  39. return -1;
  40. }
  41. /* setup deviceId */
  42. memcpy(deviceId, deviceName, strlen(deviceName));
  43. memcpy(deviceId + strlen(deviceId), "&", strlen("&"));
  44. memcpy(deviceId + strlen(deviceId), productKey, strlen(productKey));
  45. /* setup clientid */
  46. memcpy(clientId, deviceId, strlen(deviceId));
  47. memcpy(clientId + strlen(deviceId), MQTT_CLINETID_KV, strlen(MQTT_CLINETID_KV));
  48. memset(clientId + strlen(deviceId) + strlen(MQTT_CLINETID_KV), 0, 1);
  49. /* setup username */
  50. memcpy(username, deviceId, strlen(deviceId));
  51. memset(username + strlen(deviceId), 0, 1);
  52. /* setup password */
  53. memcpy(macSrc, "clientId", strlen("clientId"));
  54. memcpy(macSrc + strlen(macSrc), deviceId, strlen(deviceId));
  55. memcpy(macSrc + strlen(macSrc), "deviceName", strlen("deviceName"));
  56. memcpy(macSrc + strlen(macSrc), deviceName, strlen(deviceName));
  57. memcpy(macSrc + strlen(macSrc), "productKey", strlen("productKey"));
  58. memcpy(macSrc + strlen(macSrc), productKey, strlen(productKey));
  59. memcpy(macSrc + strlen(macSrc), "timestamp", strlen("timestamp"));
  60. memcpy(macSrc + strlen(macSrc), TIMESTAMP_VALUE, strlen(TIMESTAMP_VALUE));
  61. utils_hmac_sha256((uint8_t *)macSrc, strlen(macSrc), (uint8_t *)deviceSecret,
  62. strlen(deviceSecret), macRes);
  63. memset(password, 0, PASSWORD_MAXLEN);
  64. _hex2str(macRes, sizeof(macRes), password);
  65. return 0;
  66. }
  67. /******************************
  68. * hmac-sha256 implement below
  69. ******************************/
  70. #define SHA256_KEY_IOPAD_SIZE (64)
  71. #define SHA256_DIGEST_SIZE (32)
  72. /**
  73. * \brief SHA-256 context structure
  74. */
  75. typedef struct {
  76. uint32_t total[2]; /*!< number of bytes processed */
  77. uint32_t state[8]; /*!< intermediate digest state */
  78. unsigned char buffer[64]; /*!< data block being processed */
  79. int is224; /*!< 0 => SHA-256, else SHA-224 */
  80. } iot_sha256_context;
  81. typedef union {
  82. char sptr[8];
  83. uint64_t lint;
  84. } u_retLen;
  85. /*
  86. * 32-bit integer manipulation macros (big endian)
  87. */
  88. #ifndef GET_UINT32_BE
  89. #define GET_UINT32_BE(n,b,i) \
  90. do { \
  91. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  92. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  93. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  94. | ( (uint32_t) (b)[(i) + 3] ); \
  95. } while( 0 )
  96. #endif
  97. #ifndef PUT_UINT32_BE
  98. #define PUT_UINT32_BE(n,b,i) \
  99. do { \
  100. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  101. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  102. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  103. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  104. } while( 0 )
  105. #endif
  106. static void utils_sha256_zeroize(void *v, uint32_t n)
  107. {
  108. volatile unsigned char *p = v;
  109. while (n--) {
  110. *p++ = 0;
  111. }
  112. }
  113. void utils_sha256_init(iot_sha256_context *ctx)
  114. {
  115. memset(ctx, 0, sizeof(iot_sha256_context));
  116. }
  117. void utils_sha256_free(iot_sha256_context *ctx)
  118. {
  119. if (NULL == ctx) {
  120. return;
  121. }
  122. utils_sha256_zeroize(ctx, sizeof(iot_sha256_context));
  123. }
  124. void utils_sha256_starts(iot_sha256_context *ctx)
  125. {
  126. int is224 = 0;
  127. ctx->total[0] = 0;
  128. ctx->total[1] = 0;
  129. if (is224 == 0) {
  130. /* SHA-256 */
  131. ctx->state[0] = 0x6A09E667;
  132. ctx->state[1] = 0xBB67AE85;
  133. ctx->state[2] = 0x3C6EF372;
  134. ctx->state[3] = 0xA54FF53A;
  135. ctx->state[4] = 0x510E527F;
  136. ctx->state[5] = 0x9B05688C;
  137. ctx->state[6] = 0x1F83D9AB;
  138. ctx->state[7] = 0x5BE0CD19;
  139. }
  140. ctx->is224 = is224;
  141. }
  142. static const uint32_t K[] = {
  143. 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
  144. 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
  145. 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
  146. 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
  147. 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
  148. 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
  149. 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
  150. 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
  151. 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
  152. 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
  153. 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
  154. 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
  155. 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
  156. 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
  157. 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
  158. 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2,
  159. };
  160. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  161. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  162. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  163. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  164. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  165. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  166. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  167. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  168. #define R(t) \
  169. ( \
  170. W[t] = S1(W[t - 2]) + W[t - 7] + \
  171. S0(W[t - 15]) + W[t - 16] \
  172. )
  173. #define P(a,b,c,d,e,f,g,h,x,K) \
  174. { \
  175. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  176. temp2 = S2(a) + F0(a,b,c); \
  177. d += temp1; h = temp1 + temp2; \
  178. }
  179. void utils_sha256_process(iot_sha256_context *ctx, const unsigned char data[64])
  180. {
  181. uint32_t temp1, temp2, W[64];
  182. uint32_t A[8];
  183. unsigned int i;
  184. for (i = 0; i < 8; i++) {
  185. A[i] = ctx->state[i];
  186. }
  187. for (i = 0; i < 64; i++) {
  188. if (i < 16) {
  189. GET_UINT32_BE(W[i], data, 4 * i);
  190. } else {
  191. R(i);
  192. }
  193. P(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], W[i], K[i]);
  194. temp1 = A[7];
  195. A[7] = A[6];
  196. A[6] = A[5];
  197. A[5] = A[4];
  198. A[4] = A[3];
  199. A[3] = A[2];
  200. A[2] = A[1];
  201. A[1] = A[0];
  202. A[0] = temp1;
  203. }
  204. for (i = 0; i < 8; i++) {
  205. ctx->state[i] += A[i];
  206. }
  207. }
  208. void utils_sha256_update(iot_sha256_context *ctx, const unsigned char *input, uint32_t ilen)
  209. {
  210. size_t fill;
  211. uint32_t left;
  212. if (ilen == 0) {
  213. return;
  214. }
  215. left = ctx->total[0] & 0x3F;
  216. fill = 64 - left;
  217. ctx->total[0] += (uint32_t) ilen;
  218. ctx->total[0] &= 0xFFFFFFFF;
  219. if (ctx->total[0] < (uint32_t) ilen) {
  220. ctx->total[1]++;
  221. }
  222. if (left && ilen >= fill) {
  223. memcpy((void *)(ctx->buffer + left), input, fill);
  224. utils_sha256_process(ctx, ctx->buffer);
  225. input += fill;
  226. ilen -= fill;
  227. left = 0;
  228. }
  229. while (ilen >= 64) {
  230. utils_sha256_process(ctx, input);
  231. input += 64;
  232. ilen -= 64;
  233. }
  234. if (ilen > 0) {
  235. memcpy((void *)(ctx->buffer + left), input, ilen);
  236. }
  237. }
  238. static const unsigned char sha256_padding[64] = {
  239. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  240. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  241. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  242. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  243. };
  244. void utils_sha256_finish(iot_sha256_context *ctx, uint8_t output[32])
  245. {
  246. uint32_t last, padn;
  247. uint32_t high, low;
  248. unsigned char msglen[8];
  249. high = (ctx->total[0] >> 29)
  250. | (ctx->total[1] << 3);
  251. low = (ctx->total[0] << 3);
  252. PUT_UINT32_BE(high, msglen, 0);
  253. PUT_UINT32_BE(low, msglen, 4);
  254. last = ctx->total[0] & 0x3F;
  255. padn = (last < 56) ? (56 - last) : (120 - last);
  256. utils_sha256_update(ctx, sha256_padding, padn);
  257. utils_sha256_update(ctx, msglen, 8);
  258. PUT_UINT32_BE(ctx->state[0], output, 0);
  259. PUT_UINT32_BE(ctx->state[1], output, 4);
  260. PUT_UINT32_BE(ctx->state[2], output, 8);
  261. PUT_UINT32_BE(ctx->state[3], output, 12);
  262. PUT_UINT32_BE(ctx->state[4], output, 16);
  263. PUT_UINT32_BE(ctx->state[5], output, 20);
  264. PUT_UINT32_BE(ctx->state[6], output, 24);
  265. if (ctx->is224 == 0) {
  266. PUT_UINT32_BE(ctx->state[7], output, 28);
  267. }
  268. }
  269. void utils_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32])
  270. {
  271. iot_sha256_context ctx;
  272. utils_sha256_init(&ctx);
  273. utils_sha256_starts(&ctx);
  274. utils_sha256_update(&ctx, input, ilen);
  275. utils_sha256_finish(&ctx, output);
  276. utils_sha256_free(&ctx);
  277. }
  278. static void utils_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32])
  279. {
  280. iot_sha256_context context;
  281. uint8_t k_ipad[SHA256_KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
  282. uint8_t k_opad[SHA256_KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
  283. int32_t i;
  284. if ((NULL == msg) || (NULL == key) || (NULL == output)) {
  285. return;
  286. }
  287. if (key_len > SHA256_KEY_IOPAD_SIZE) {
  288. return;
  289. }
  290. /* start out by storing key in pads */
  291. memset(k_ipad, 0, sizeof(k_ipad));
  292. memset(k_opad, 0, sizeof(k_opad));
  293. memcpy(k_ipad, key, key_len);
  294. memcpy(k_opad, key, key_len);
  295. /* XOR key with ipad and opad values */
  296. for (i = 0; i < SHA256_KEY_IOPAD_SIZE; i++) {
  297. k_ipad[i] ^= 0x36;
  298. k_opad[i] ^= 0x5c;
  299. }
  300. /* perform inner SHA */
  301. utils_sha256_init(&context); /* init context for 1st pass */
  302. utils_sha256_starts(&context); /* setup context for 1st pass */
  303. utils_sha256_update(&context, k_ipad, SHA256_KEY_IOPAD_SIZE); /* start with inner pad */
  304. utils_sha256_update(&context, msg, msg_len); /* then text of datagram */
  305. utils_sha256_finish(&context, output); /* finish up 1st pass */
  306. /* perform outer SHA */
  307. utils_sha256_init(&context); /* init context for 2nd pass */
  308. utils_sha256_starts(&context); /* setup context for 2nd pass */
  309. utils_sha256_update(&context, k_opad, SHA256_KEY_IOPAD_SIZE); /* start with outer pad */
  310. utils_sha256_update(&context, output, SHA256_DIGEST_SIZE); /* then results of 1st hash */
  311. utils_sha256_finish(&context, output); /* finish up 2nd pass */
  312. }