digests.c 5.7 KB

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