digests.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "vfs.h"
  31. #include "digests.h"
  32. #include "user_config.h"
  33. #include "rom.h"
  34. #include "osapi.h"
  35. #include "mem.h"
  36. #include <string.h>
  37. #include <c_errno.h>
  38. #ifdef MD2_ENABLE
  39. #include "ssl/ssl_crypto.h"
  40. #endif
  41. #ifdef SHA2_ENABLE
  42. #include "sha2.h"
  43. #endif
  44. typedef char ensure_int_and_size_t_same[(sizeof(int)==sizeof(size_t)) ? 0 : -1];
  45. /* None of the functions match the prototype fully due to the void *, and in
  46. some cases also the int vs size_t len, so wrap declarations in a macro. */
  47. #define MECH(pfx, u, ds, bs) \
  48. { #pfx, \
  49. (create_ctx_fn)pfx ## u ## Init, \
  50. (update_ctx_fn)pfx ## u ## Update, \
  51. (finalize_ctx_fn)pfx ## u ## Final, \
  52. sizeof(pfx ## _CTX), \
  53. ds, \
  54. bs }
  55. static const digest_mech_info_t hash_mechs[] ICACHE_RODATA_ATTR =
  56. {
  57. #ifdef MD2_ENABLE
  58. MECH(MD2, _ , MD2_SIZE, 16),
  59. #endif
  60. MECH(MD5, , MD5_DIGEST_LENGTH, 64)
  61. ,MECH(SHA1, , SHA1_DIGEST_LENGTH, 64)
  62. #ifdef SHA2_ENABLE
  63. ,MECH(SHA256, _ , SHA256_DIGEST_LENGTH, SHA256_BLOCK_LENGTH)
  64. ,MECH(SHA384, _ , SHA384_DIGEST_LENGTH, SHA384_BLOCK_LENGTH)
  65. ,MECH(SHA512, _ , SHA512_DIGEST_LENGTH, SHA512_BLOCK_LENGTH)
  66. #endif
  67. };
  68. #undef MECH
  69. const digest_mech_info_t *ICACHE_FLASH_ATTR crypto_digest_mech (const char *mech)
  70. {
  71. if (!mech)
  72. return 0;
  73. size_t i;
  74. for (i = 0; i < (sizeof (hash_mechs) / sizeof (digest_mech_info_t)); ++i)
  75. {
  76. const digest_mech_info_t *mi = hash_mechs + i;
  77. if (strcasecmp (mech, mi->name) == 0)
  78. return mi;
  79. }
  80. return 0;
  81. }
  82. const char crypto_hexbytes[] = "0123456789abcdef";
  83. // note: supports in-place encoding
  84. void ICACHE_FLASH_ATTR crypto_encode_asciihex (const char *bin, size_t binlen, char *outbuf)
  85. {
  86. size_t aidx = binlen * 2 -1;
  87. int i;
  88. for (i = binlen -1; i >= 0; --i)
  89. {
  90. outbuf[aidx--] = crypto_hexbytes[bin[i] & 0xf];
  91. outbuf[aidx--] = crypto_hexbytes[bin[i] >> 4];
  92. }
  93. }
  94. int ICACHE_FLASH_ATTR crypto_hash (const digest_mech_info_t *mi,
  95. const char *data, size_t data_len,
  96. uint8_t *digest)
  97. {
  98. if (!mi)
  99. return EINVAL;
  100. void *ctx = (void *)os_malloc (mi->ctx_size);
  101. if (!ctx)
  102. return ENOMEM;
  103. mi->create (ctx);
  104. mi->update (ctx, data, data_len);
  105. mi->finalize (digest, ctx);
  106. os_free (ctx);
  107. return 0;
  108. }
  109. int ICACHE_FLASH_ATTR crypto_fhash (const digest_mech_info_t *mi,
  110. read_fn read, int readarg,
  111. uint8_t *digest)
  112. {
  113. if (!mi)
  114. return EINVAL;
  115. // Initialise
  116. void *ctx = (void *)os_malloc (mi->ctx_size);
  117. if (!ctx)
  118. return ENOMEM;
  119. mi->create (ctx);
  120. // Hash bytes from file in blocks
  121. uint8_t* buffer = (uint8_t*)os_malloc (mi->block_size);
  122. if (!buffer)
  123. return ENOMEM;
  124. int read_len = 0;
  125. do {
  126. read_len = read(readarg, buffer, mi->block_size);
  127. mi->update (ctx, buffer, read_len);
  128. } while (read_len == mi->block_size);
  129. // Finish up
  130. mi->finalize (digest, ctx);
  131. os_free (buffer);
  132. os_free (ctx);
  133. return 0;
  134. }
  135. int ICACHE_FLASH_ATTR crypto_hmac (const digest_mech_info_t *mi,
  136. const char *data, size_t data_len,
  137. const char *key, size_t key_len,
  138. uint8_t *digest)
  139. {
  140. if (!mi)
  141. return EINVAL;
  142. void *ctx = (void *)os_malloc (mi->ctx_size);
  143. if (!ctx)
  144. return ENOMEM;
  145. // If key too long, it needs to be hashed before use
  146. if (key_len > mi->block_size)
  147. {
  148. mi->create (ctx);
  149. mi->update (ctx, key, key_len);
  150. mi->finalize (digest, ctx);
  151. key = digest;
  152. key_len = mi->digest_size;
  153. }
  154. const size_t bs = mi->block_size;
  155. uint8_t k_ipad[bs];
  156. uint8_t k_opad[bs];
  157. os_memset (k_ipad, 0x36, bs);
  158. os_memset (k_opad, 0x5c, bs);
  159. size_t i;
  160. for (i = 0; i < key_len; ++i)
  161. {
  162. k_ipad[i] ^= key[i];
  163. k_opad[i] ^= key[i];
  164. }
  165. mi->create (ctx);
  166. mi->update (ctx, k_ipad, bs);
  167. mi->update (ctx, data, data_len);
  168. mi->finalize (digest, ctx);
  169. mi->create (ctx);
  170. mi->update (ctx, k_opad, bs);
  171. mi->update (ctx, digest, mi->digest_size);
  172. mi->finalize (digest, ctx);
  173. os_free (ctx);
  174. return 0;
  175. }