digests.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2015, DiUS Computing Pty Ltd (jmattsson@dius.com.au)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holder nor the names of contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. */
  30. #include "digests.h"
  31. #include "user_config.h"
  32. #include "rom.h"
  33. #include "osapi.h"
  34. #include "mem.h"
  35. #include <string.h>
  36. #include <c_errno.h>
  37. #ifdef MD2_ENABLE
  38. #include "ssl/ssl_crypto.h"
  39. #endif
  40. #ifdef SHA2_ENABLE
  41. #include "sha2.h"
  42. #endif
  43. typedef char ensure_int_and_size_t_same[(sizeof(int)==sizeof(size_t)) ? 0 : -1];
  44. /* None of the functions match the prototype fully due to the void *, and in
  45. some cases also the int vs size_t len, so wrap declarations in a macro. */
  46. #define MECH(pfx, u, ds, bs) \
  47. { #pfx, \
  48. (create_ctx_fn)pfx ## u ## Init, \
  49. (update_ctx_fn)pfx ## u ## Update, \
  50. (finalize_ctx_fn)pfx ## u ## Final, \
  51. sizeof(pfx ## _CTX), \
  52. ds, \
  53. bs }
  54. static const digest_mech_info_t hash_mechs[] ICACHE_RODATA_ATTR =
  55. {
  56. #ifdef MD2_ENABLE
  57. MECH(MD2, _ , MD2_SIZE, 16),
  58. #endif
  59. MECH(MD5, , MD5_DIGEST_LENGTH, 64)
  60. ,MECH(SHA1, , SHA1_DIGEST_LENGTH, 64)
  61. #ifdef SHA2_ENABLE
  62. ,MECH(SHA256, _ , SHA256_DIGEST_LENGTH, SHA256_BLOCK_LENGTH)
  63. ,MECH(SHA384, _ , SHA384_DIGEST_LENGTH, SHA384_BLOCK_LENGTH)
  64. ,MECH(SHA512, _ , SHA512_DIGEST_LENGTH, SHA512_BLOCK_LENGTH)
  65. #endif
  66. };
  67. #undef MECH
  68. const digest_mech_info_t *ICACHE_FLASH_ATTR crypto_digest_mech (const char *mech)
  69. {
  70. if (!mech)
  71. return 0;
  72. size_t i;
  73. for (i = 0; i < (sizeof (hash_mechs) / sizeof (digest_mech_info_t)); ++i)
  74. {
  75. const digest_mech_info_t *mi = hash_mechs + i;
  76. if (strcasecmp (mech, mi->name) == 0)
  77. return mi;
  78. }
  79. return 0;
  80. }
  81. const char crypto_hexbytes[] = "0123456789abcdef";
  82. // note: supports in-place encoding
  83. void ICACHE_FLASH_ATTR crypto_encode_asciihex (const char *bin, size_t binlen, char *outbuf)
  84. {
  85. size_t aidx = binlen * 2 -1;
  86. int i;
  87. for (i = binlen -1; i >= 0; --i)
  88. {
  89. outbuf[aidx--] = crypto_hexbytes[bin[i] & 0xf];
  90. outbuf[aidx--] = crypto_hexbytes[bin[i] >> 4];
  91. }
  92. }
  93. int ICACHE_FLASH_ATTR crypto_hash (const digest_mech_info_t *mi,
  94. const char *data, size_t data_len,
  95. uint8_t *digest)
  96. {
  97. if (!mi)
  98. return EINVAL;
  99. void *ctx = (void *)os_malloc (mi->ctx_size);
  100. if (!ctx)
  101. return ENOMEM;
  102. mi->create (ctx);
  103. mi->update (ctx, data, data_len);
  104. mi->finalize (digest, ctx);
  105. os_free (ctx);
  106. return 0;
  107. }
  108. int ICACHE_FLASH_ATTR crypto_fhash (const digest_mech_info_t *mi,
  109. read_fn read, int readarg,
  110. uint8_t *digest)
  111. {
  112. if (!mi)
  113. return EINVAL;
  114. // Initialise
  115. void *ctx = (void *)os_malloc (mi->ctx_size);
  116. if (!ctx)
  117. return ENOMEM;
  118. mi->create (ctx);
  119. // Hash bytes from file in blocks
  120. uint8_t* buffer = (uint8_t*)os_malloc (mi->block_size);
  121. if (!buffer)
  122. return ENOMEM;
  123. int read_len = 0;
  124. do {
  125. read_len = read(readarg, buffer, mi->block_size);
  126. mi->update (ctx, buffer, read_len);
  127. } while (read_len == mi->block_size);
  128. // Finish up
  129. mi->finalize (digest, ctx);
  130. os_free (buffer);
  131. os_free (ctx);
  132. return 0;
  133. }
  134. int ICACHE_FLASH_ATTR crypto_hmac (const digest_mech_info_t *mi,
  135. const char *data, size_t data_len,
  136. const char *key, size_t key_len,
  137. uint8_t *digest)
  138. {
  139. if (!mi)
  140. return EINVAL;
  141. void *ctx = (void *)os_malloc (mi->ctx_size);
  142. if (!ctx)
  143. return ENOMEM;
  144. // If key too long, it needs to be hashed before use
  145. if (key_len > mi->block_size)
  146. {
  147. mi->create (ctx);
  148. mi->update (ctx, key, key_len);
  149. mi->finalize (digest, ctx);
  150. key = digest;
  151. key_len = mi->digest_size;
  152. }
  153. const size_t bs = mi->block_size;
  154. uint8_t k_ipad[bs];
  155. uint8_t k_opad[bs];
  156. os_memset (k_ipad, 0x36, bs);
  157. os_memset (k_opad, 0x5c, bs);
  158. size_t i;
  159. for (i = 0; i < key_len; ++i)
  160. {
  161. k_ipad[i] ^= key[i];
  162. k_opad[i] ^= key[i];
  163. }
  164. mi->create (ctx);
  165. mi->update (ctx, k_ipad, bs);
  166. mi->update (ctx, data, data_len);
  167. mi->finalize (digest, ctx);
  168. mi->create (ctx);
  169. mi->update (ctx, k_opad, bs);
  170. mi->update (ctx, digest, mi->digest_size);
  171. mi->finalize (digest, ctx);
  172. os_free (ctx);
  173. return 0;
  174. }