utils_hmac.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. * Licensed under the MIT License (the "License"); you may not use this file
  6. except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://opensource.org/licenses/MIT
  9. * Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is
  11. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND,
  13. * either express or implied. See the License for the specific language
  14. governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "utils_hmac.h"
  19. #include <string.h>
  20. #include "qcloud_iot_export_log.h"
  21. #include "utils_md5.h"
  22. #include "utils_sha1.h"
  23. #define KEY_IOPAD_SIZE 64
  24. #define MD5_DIGEST_SIZE 16
  25. #define SHA1_DIGEST_SIZE 20
  26. void utils_hmac_md5(const char *msg, int msg_len, char *digest, const char *key, int key_len)
  27. {
  28. if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
  29. Log_e("parameter is Null,failed!");
  30. return;
  31. }
  32. if (key_len > KEY_IOPAD_SIZE) {
  33. Log_e("key_len > size(%d) of array", KEY_IOPAD_SIZE);
  34. return;
  35. }
  36. iot_md5_context context;
  37. unsigned char k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
  38. unsigned char k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
  39. unsigned char out[MD5_DIGEST_SIZE];
  40. int i;
  41. /* start out by storing key in pads */
  42. memset(k_ipad, 0, sizeof(k_ipad));
  43. memset(k_opad, 0, sizeof(k_opad));
  44. memcpy(k_ipad, key, key_len);
  45. memcpy(k_opad, key, key_len);
  46. /* XOR key with ipad and opad values */
  47. for (i = 0; i < KEY_IOPAD_SIZE; i++) {
  48. k_ipad[i] ^= 0x36;
  49. k_opad[i] ^= 0x5c;
  50. }
  51. /* perform inner MD5 */
  52. utils_md5_init(&context); /* init context for 1st pass */
  53. utils_md5_starts(&context); /* setup context for 1st pass */
  54. utils_md5_update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
  55. utils_md5_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
  56. utils_md5_finish(&context, out); /* finish up 1st pass */
  57. /* perform outer MD5 */
  58. utils_md5_init(&context); /* init context for 2nd pass */
  59. utils_md5_starts(&context); /* setup context for 2nd pass */
  60. utils_md5_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
  61. utils_md5_update(&context, out, MD5_DIGEST_SIZE); /* then results of 1st hash */
  62. utils_md5_finish(&context, out); /* finish up 2nd pass */
  63. for (i = 0; i < MD5_DIGEST_SIZE; ++i) {
  64. digest[i * 2] = utils_hb2hex(out[i] >> 4);
  65. digest[i * 2 + 1] = utils_hb2hex(out[i]);
  66. }
  67. }
  68. void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len)
  69. {
  70. if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
  71. Log_e("parameter is Null,failed!");
  72. return;
  73. }
  74. if (key_len > KEY_IOPAD_SIZE) {
  75. Log_e("key_len > size(%d) of array", KEY_IOPAD_SIZE);
  76. return;
  77. }
  78. iot_sha1_context context;
  79. unsigned char k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
  80. unsigned char k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
  81. unsigned char out[SHA1_DIGEST_SIZE];
  82. int i;
  83. /* start out by storing key in pads */
  84. memset(k_ipad, 0, sizeof(k_ipad));
  85. memset(k_opad, 0, sizeof(k_opad));
  86. memcpy(k_ipad, key, key_len);
  87. memcpy(k_opad, key, key_len);
  88. /* XOR key with ipad and opad values */
  89. for (i = 0; i < KEY_IOPAD_SIZE; i++) {
  90. k_ipad[i] ^= 0x36;
  91. k_opad[i] ^= 0x5c;
  92. }
  93. /* perform inner SHA */
  94. utils_sha1_init(&context); /* init context for 1st pass */
  95. utils_sha1_starts(&context); /* setup context for 1st pass */
  96. utils_sha1_update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
  97. utils_sha1_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
  98. utils_sha1_finish(&context, out); /* finish up 1st pass */
  99. /* perform outer SHA */
  100. utils_sha1_init(&context); /* init context for 2nd pass */
  101. utils_sha1_starts(&context); /* setup context for 2nd pass */
  102. utils_sha1_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
  103. utils_sha1_update(&context, out, SHA1_DIGEST_SIZE); /* then results of 1st hash */
  104. utils_sha1_finish(&context, out); /* finish up 2nd pass */
  105. for (i = 0; i < SHA1_DIGEST_SIZE; ++i) {
  106. digest[i * 2] = utils_hb2hex(out[i] >> 4);
  107. digest[i * 2 + 1] = utils_hb2hex(out[i]);
  108. }
  109. }
  110. int utils_hmac_sha1_hex(const char *msg, int msg_len, char *digest, const char *key, int key_len)
  111. {
  112. if ((NULL == msg) || (NULL == digest) || (NULL == key)) {
  113. Log_e("parameter is Null,failed!");
  114. return 0;
  115. }
  116. if (key_len > KEY_IOPAD_SIZE) {
  117. Log_e("key_len > size(%d) of array", KEY_IOPAD_SIZE);
  118. return 0;
  119. }
  120. iot_sha1_context context;
  121. unsigned char k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
  122. unsigned char k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
  123. unsigned char out[SHA1_DIGEST_SIZE];
  124. int i;
  125. /* start out by storing key in pads */
  126. memset(k_ipad, 0, sizeof(k_ipad));
  127. memset(k_opad, 0, sizeof(k_opad));
  128. memcpy(k_ipad, key, key_len);
  129. memcpy(k_opad, key, key_len);
  130. /* XOR key with ipad and opad values */
  131. for (i = 0; i < KEY_IOPAD_SIZE; i++) {
  132. k_ipad[i] ^= 0x36;
  133. k_opad[i] ^= 0x5c;
  134. }
  135. /* perform inner SHA */
  136. utils_sha1_init(&context); /* init context for 1st pass */
  137. utils_sha1_starts(&context); /* setup context for 1st pass */
  138. utils_sha1_update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
  139. utils_sha1_update(&context, (unsigned char *)msg, msg_len); /* then text of datagram */
  140. utils_sha1_finish(&context, out); /* finish up 1st pass */
  141. /* perform outer SHA */
  142. utils_sha1_init(&context); /* init context for 2nd pass */
  143. utils_sha1_starts(&context); /* setup context for 2nd pass */
  144. utils_sha1_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
  145. utils_sha1_update(&context, out, SHA1_DIGEST_SIZE); /* then results of 1st hash */
  146. utils_sha1_finish(&context, out); /* finish up 2nd pass */
  147. memcpy(digest, out, SHA1_DIGEST_SIZE);
  148. return SHA1_DIGEST_SIZE;
  149. }