lws-genhash.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * lws_genhash provides a hash / hmac abstraction api in lws that works the
  25. * same whether you are using openssl or mbedtls hash functions underneath.
  26. */
  27. #include "libwebsockets.h"
  28. #include <mbedtls/version.h>
  29. #if (MBEDTLS_VERSION_NUMBER >= 0x02070000)
  30. /*
  31. * We have the _ret variants available, check the return codes on everything
  32. */
  33. int
  34. lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type)
  35. {
  36. ctx->type = type;
  37. switch (ctx->type) {
  38. case LWS_GENHASH_TYPE_MD5:
  39. mbedtls_md5_init(&ctx->u.md5);
  40. if (mbedtls_md5_starts_ret(&ctx->u.md5))
  41. return 1;
  42. break;
  43. case LWS_GENHASH_TYPE_SHA1:
  44. mbedtls_sha1_init(&ctx->u.sha1);
  45. if (mbedtls_sha1_starts_ret(&ctx->u.sha1))
  46. return 1;
  47. break;
  48. case LWS_GENHASH_TYPE_SHA256:
  49. mbedtls_sha256_init(&ctx->u.sha256);
  50. if (mbedtls_sha256_starts_ret(&ctx->u.sha256, 0))
  51. return 1;
  52. break;
  53. case LWS_GENHASH_TYPE_SHA384:
  54. mbedtls_sha512_init(&ctx->u.sha512);
  55. if (mbedtls_sha512_starts_ret(&ctx->u.sha512, 1 /* is384 */))
  56. return 1;
  57. break;
  58. case LWS_GENHASH_TYPE_SHA512:
  59. mbedtls_sha512_init(&ctx->u.sha512);
  60. if (mbedtls_sha512_starts_ret(&ctx->u.sha512, 0))
  61. return 1;
  62. break;
  63. default:
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. int
  69. lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len)
  70. {
  71. if (!len)
  72. return 0;
  73. switch (ctx->type) {
  74. case LWS_GENHASH_TYPE_MD5:
  75. if (mbedtls_md5_update_ret(&ctx->u.md5, in, len))
  76. return 1;
  77. break;
  78. case LWS_GENHASH_TYPE_SHA1:
  79. if (mbedtls_sha1_update_ret(&ctx->u.sha1, in, len))
  80. return 1;
  81. break;
  82. case LWS_GENHASH_TYPE_SHA256:
  83. if (mbedtls_sha256_update_ret(&ctx->u.sha256, in, len))
  84. return 1;
  85. break;
  86. case LWS_GENHASH_TYPE_SHA384:
  87. if (mbedtls_sha512_update_ret(&ctx->u.sha512, in, len))
  88. return 1;
  89. break;
  90. case LWS_GENHASH_TYPE_SHA512:
  91. if (mbedtls_sha512_update_ret(&ctx->u.sha512, in, len))
  92. return 1;
  93. break;
  94. }
  95. return 0;
  96. }
  97. int
  98. lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result)
  99. {
  100. switch (ctx->type) {
  101. case LWS_GENHASH_TYPE_MD5:
  102. if (mbedtls_md5_finish_ret(&ctx->u.md5, result))
  103. return 1;
  104. mbedtls_md5_free(&ctx->u.md5);
  105. break;
  106. case LWS_GENHASH_TYPE_SHA1:
  107. if (mbedtls_sha1_finish_ret(&ctx->u.sha1, result))
  108. return 1;
  109. mbedtls_sha1_free(&ctx->u.sha1);
  110. break;
  111. case LWS_GENHASH_TYPE_SHA256:
  112. if (mbedtls_sha256_finish_ret(&ctx->u.sha256, result))
  113. return 1;
  114. mbedtls_sha256_free(&ctx->u.sha256);
  115. break;
  116. case LWS_GENHASH_TYPE_SHA384:
  117. if (mbedtls_sha512_finish_ret(&ctx->u.sha512, result))
  118. return 1;
  119. mbedtls_sha512_free(&ctx->u.sha512);
  120. break;
  121. case LWS_GENHASH_TYPE_SHA512:
  122. if (mbedtls_sha512_finish_ret(&ctx->u.sha512, result))
  123. return 1;
  124. mbedtls_sha512_free(&ctx->u.sha512);
  125. break;
  126. }
  127. return 0;
  128. }
  129. #else
  130. /*
  131. * mbedtls is too old to have the _ret variants
  132. */
  133. int
  134. lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type)
  135. {
  136. ctx->type = type;
  137. switch (ctx->type) {
  138. case LWS_GENHASH_TYPE_MD5:
  139. mbedtls_md5_init(&ctx->u.md5);
  140. mbedtls_md5_starts(&ctx->u.md5);
  141. break;
  142. case LWS_GENHASH_TYPE_SHA1:
  143. mbedtls_sha1_init(&ctx->u.sha1);
  144. mbedtls_sha1_starts(&ctx->u.sha1);
  145. break;
  146. case LWS_GENHASH_TYPE_SHA256:
  147. mbedtls_sha256_init(&ctx->u.sha256);
  148. mbedtls_sha256_starts(&ctx->u.sha256, 0);
  149. break;
  150. case LWS_GENHASH_TYPE_SHA384:
  151. mbedtls_sha512_init(&ctx->u.sha512);
  152. mbedtls_sha512_starts(&ctx->u.sha512, 1 /* is384 */);
  153. break;
  154. case LWS_GENHASH_TYPE_SHA512:
  155. mbedtls_sha512_init(&ctx->u.sha512);
  156. mbedtls_sha512_starts(&ctx->u.sha512, 0);
  157. break;
  158. default:
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. int
  164. lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len)
  165. {
  166. if (!len)
  167. return 0;
  168. switch (ctx->type) {
  169. case LWS_GENHASH_TYPE_MD5:
  170. mbedtls_md5_update(&ctx->u.md5, in, len);
  171. break;
  172. case LWS_GENHASH_TYPE_SHA1:
  173. mbedtls_sha1_update(&ctx->u.sha1, in, len);
  174. break;
  175. case LWS_GENHASH_TYPE_SHA256:
  176. mbedtls_sha256_update(&ctx->u.sha256, in, len);
  177. break;
  178. case LWS_GENHASH_TYPE_SHA384:
  179. mbedtls_sha512_update(&ctx->u.sha512, in, len);
  180. break;
  181. case LWS_GENHASH_TYPE_SHA512:
  182. mbedtls_sha512_update(&ctx->u.sha512, in, len);
  183. break;
  184. }
  185. return 0;
  186. }
  187. int
  188. lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result)
  189. {
  190. switch (ctx->type) {
  191. case LWS_GENHASH_TYPE_MD5:
  192. mbedtls_md5_finish(&ctx->u.md5, result);
  193. mbedtls_md5_free(&ctx->u.md5);
  194. break;
  195. case LWS_GENHASH_TYPE_SHA1:
  196. mbedtls_sha1_finish(&ctx->u.sha1, result);
  197. mbedtls_sha1_free(&ctx->u.sha1);
  198. break;
  199. case LWS_GENHASH_TYPE_SHA256:
  200. mbedtls_sha256_finish(&ctx->u.sha256, result);
  201. mbedtls_sha256_free(&ctx->u.sha256);
  202. break;
  203. case LWS_GENHASH_TYPE_SHA384:
  204. mbedtls_sha512_finish(&ctx->u.sha512, result);
  205. mbedtls_sha512_free(&ctx->u.sha512);
  206. break;
  207. case LWS_GENHASH_TYPE_SHA512:
  208. mbedtls_sha512_finish(&ctx->u.sha512, result);
  209. mbedtls_sha512_free(&ctx->u.sha512);
  210. break;
  211. }
  212. return 0;
  213. }
  214. #endif
  215. int
  216. lws_genhmac_init(struct lws_genhmac_ctx *ctx, enum lws_genhmac_types type,
  217. const uint8_t *key, size_t key_len)
  218. {
  219. int t;
  220. ctx->type = type;
  221. switch (type) {
  222. case LWS_GENHMAC_TYPE_SHA256:
  223. t = MBEDTLS_MD_SHA256;
  224. break;
  225. case LWS_GENHMAC_TYPE_SHA384:
  226. t = MBEDTLS_MD_SHA384;
  227. break;
  228. case LWS_GENHMAC_TYPE_SHA512:
  229. t = MBEDTLS_MD_SHA512;
  230. break;
  231. default:
  232. return -1;
  233. }
  234. ctx->hmac = mbedtls_md_info_from_type(t);
  235. if (!ctx->hmac)
  236. return -1;
  237. #if !defined(LWS_HAVE_mbedtls_md_setup)
  238. if (mbedtls_md_init_ctx(&ctx->ctx, ctx->hmac))
  239. return -1;
  240. #else
  241. if (mbedtls_md_setup(&ctx->ctx, ctx->hmac, 1))
  242. return -1;
  243. #endif
  244. if (mbedtls_md_hmac_starts(&ctx->ctx, key, key_len)) {
  245. mbedtls_md_free(&ctx->ctx);
  246. ctx->hmac = NULL;
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. int
  252. lws_genhmac_update(struct lws_genhmac_ctx *ctx, const void *in, size_t len)
  253. {
  254. if (!len)
  255. return 0;
  256. if (mbedtls_md_hmac_update(&ctx->ctx, in, len))
  257. return -1;
  258. return 0;
  259. }
  260. int
  261. lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result)
  262. {
  263. int n = 0;
  264. if (result)
  265. n = mbedtls_md_hmac_finish(&ctx->ctx, result);
  266. mbedtls_md_free(&ctx->ctx);
  267. ctx->hmac = NULL;
  268. if (n)
  269. return -1;
  270. return 0;
  271. }