digests.c 5.8 KB

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