digests.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (read_len > 0) {
  128. mi->update (ctx, buffer, read_len);
  129. }
  130. } while (read_len == mi->block_size);
  131. // Finish up
  132. mi->finalize (digest, ctx);
  133. os_free (buffer);
  134. os_free (ctx);
  135. return 0;
  136. }
  137. void crypto_hmac_begin (void *ctx, const digest_mech_info_t *mi,
  138. const char *key, size_t key_len, uint8_t *k_opad)
  139. {
  140. // If key too long, it needs to be hashed before use
  141. char tmp[mi->digest_size];
  142. if (key_len > mi->block_size)
  143. {
  144. mi->update (ctx, key, key_len);
  145. mi->finalize (tmp, ctx);
  146. key = tmp;
  147. key_len = mi->digest_size;
  148. mi->create (ctx); // refresh
  149. }
  150. const size_t bs = mi->block_size;
  151. uint8_t k_ipad[bs];
  152. os_memset (k_ipad, 0x36, bs);
  153. os_memset (k_opad, 0x5c, bs);
  154. size_t i;
  155. for (i = 0; i < key_len; ++i)
  156. {
  157. k_ipad[i] ^= key[i];
  158. k_opad[i] ^= key[i];
  159. }
  160. mi->update (ctx, k_ipad, bs);
  161. }
  162. void crypto_hmac_finalize (void *ctx, const digest_mech_info_t *mi,
  163. const uint8_t *k_opad, uint8_t *digest)
  164. {
  165. mi->finalize (digest, ctx);
  166. mi->create (ctx);
  167. mi->update (ctx, k_opad, mi->block_size);
  168. mi->update (ctx, digest, mi->digest_size);
  169. mi->finalize (digest, ctx);
  170. }
  171. int crypto_hmac (const digest_mech_info_t *mi,
  172. const char *data, size_t data_len,
  173. const char *key, size_t key_len,
  174. uint8_t *digest)
  175. {
  176. if (!mi)
  177. return EINVAL;
  178. struct {
  179. uint8_t ctx[mi->ctx_size];
  180. uint8_t k_opad[mi->block_size];
  181. } *tmp = os_malloc (sizeof (*tmp));
  182. if (!tmp)
  183. return ENOMEM;
  184. mi->create (tmp->ctx);
  185. crypto_hmac_begin (tmp->ctx, mi, key, key_len, tmp->k_opad);
  186. mi->update (tmp->ctx, data, data_len);
  187. crypto_hmac_finalize (tmp->ctx, mi, tmp->k_opad, digest);
  188. os_free (tmp);
  189. return 0;
  190. }