_hmac_HMAC.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "_hmac_HMAC.h"
  2. #include "mbedtls/md.h"
  3. #include "string.h"
  4. enum {
  5. PIKA_HMAC_MD5 = 16,
  6. PIKA_HMAC_SHA1 = 20,
  7. PIKA_HMAC_SHA256 = 32
  8. } pika_hmac_t;
  9. static void hmac_to_hex(uint8_t* s, int l, uint8_t* d);
  10. static void init_buff(PikaObj* self, size_t h) {
  11. obj_setBytes(self, "_buff", NULL, h);
  12. obj_setBytes(self, "_hexbuff", NULL, (h * 2));
  13. memset(obj_getBytes(self, "_buff"), 0, h);
  14. memset(obj_getBytes(self, "_hexbuff"), 0, (h * 2));
  15. }
  16. void _hmac_HMAC_new(PikaObj* self, Arg* key, Arg* msg, char* digestmod) {
  17. ArgType t;
  18. t = arg_getType(key);
  19. if (ARG_TYPE_BYTES != t) {
  20. obj_setErrorCode(self, -2); // io error
  21. obj_setSysOut(self, "hmac.new() key type error");
  22. }
  23. t = arg_getType(msg);
  24. if (ARG_TYPE_NONE != t) {
  25. if (ARG_TYPE_BYTES != t) {
  26. obj_setErrorCode(self, -2); // io error
  27. obj_setSysOut(self, "hmac.new() msg type error");
  28. }
  29. }
  30. size_t key_len = arg_getBytesSize(key);
  31. uint8_t* key_data = arg_getBytes(key);
  32. size_t msg_len = arg_getBytesSize(msg);
  33. uint8_t* msg_data = arg_getBytes(msg);
  34. obj_setInt(self, "_digest_flags", 0); // flag
  35. mbedtls_md_context_t ctx;
  36. mbedtls_md_init(&ctx);
  37. if (strcmp(digestmod, "md5") == 0 || strcmp(digestmod, "MD5") == 0) {
  38. mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_MD5), 1);
  39. obj_setInt(self, "_mode", PIKA_HMAC_MD5);
  40. init_buff(self, PIKA_HMAC_MD5);
  41. } else if (strcmp(digestmod, "sha1") == 0 ||
  42. strcmp(digestmod, "SHA1") == 0) {
  43. mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1);
  44. obj_setInt(self, "_mode", PIKA_HMAC_SHA1);
  45. init_buff(self, PIKA_HMAC_SHA1);
  46. } else if (strcmp(digestmod, "sha256") == 0 ||
  47. strcmp(digestmod, "SHA256") == 0) {
  48. mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 1);
  49. obj_setInt(self, "_mode", PIKA_HMAC_SHA256);
  50. init_buff(self, PIKA_HMAC_SHA256);
  51. } else {
  52. obj_setErrorCode(self, -2); // io error
  53. obj_setSysOut(self, "hmac.new() not support mode");
  54. }
  55. mbedtls_md_hmac_starts(&ctx, key_data, key_len);
  56. if (msg_len > 0) {
  57. mbedtls_md_hmac_update(&ctx, msg_data, msg_len);
  58. }
  59. obj_setStruct(self, "_context", ctx);
  60. }
  61. void _hmac_HMAC_update(PikaObj* self, Arg* msg) {
  62. ArgType t = arg_getType(msg);
  63. if (ARG_TYPE_BYTES != t) {
  64. obj_setErrorCode(self, -2); // io error
  65. obj_setSysOut(self, "hmac.update() msg type error");
  66. }
  67. size_t msg_len = arg_getBytesSize(msg);
  68. uint8_t* msg_data = arg_getBytes(msg);
  69. mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
  70. mbedtls_md_hmac_update(ctx, msg_data, msg_len);
  71. }
  72. Arg* _hmac_HMAC_digest(PikaObj* self) {
  73. uint8_t* buff = obj_getBytes(self, "_buff");
  74. uint8_t flag = obj_getInt(self, "_digest_flags");
  75. if (flag & 0x01) // already digest
  76. {
  77. goto __exit;
  78. } else {
  79. mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
  80. mbedtls_md_hmac_finish(ctx, buff);
  81. obj_setInt(self, "_digest_flags", flag | 0x01);
  82. goto __exit;
  83. }
  84. __exit:
  85. return arg_newBytes(buff, obj_getInt(self, "_mode"));
  86. }
  87. char* _hmac_HMAC_hexdigest(PikaObj* self) {
  88. uint8_t* buff = obj_getBytes(self, "_buff");
  89. uint8_t* hexbuff = obj_getBytes(self, "_hexbuff");
  90. uint8_t flag = obj_getInt(self, "_digest_flags");
  91. if (flag & 0x01) { // already digest
  92. obj_setInt(self, "_digest_flags", flag | 0x02); // set hexdigest flag
  93. goto __exit;
  94. } else if (flag & 0x02) { // already hexdigest
  95. return (char*)hexbuff;
  96. } else {
  97. mbedtls_md_context_t* ctx = obj_getStruct(self, "_context");
  98. mbedtls_md_hmac_finish(ctx, buff);
  99. // set digest and hexdigest flags
  100. obj_setInt(self, "_digest_flags", flag | 0x03);
  101. goto __exit;
  102. }
  103. __exit:
  104. hmac_to_hex(buff, obj_getInt(self, "_mode"), hexbuff);
  105. return (char*)hexbuff;
  106. }
  107. void _hmac_HMAC_copy(PikaObj* self) {}
  108. static const uint8_t hmac_hex_table[] = {'0', '1', '2', '3', '4', '5',
  109. '6', '7', '8', '9', 'A', 'B',
  110. 'C', 'D', 'E', 'F'};
  111. static void hmac_to_hex(uint8_t* s, int l, uint8_t* d) {
  112. while (l--) {
  113. *(d++) = hmac_hex_table[*s >> 4];
  114. *(d++) = hmac_hex_table[*(s++) & 0x0f];
  115. }
  116. }