2 * libwebsockets - generic hash and HMAC api hiding the backend
4 * Copyright (C) 2017 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * lws_genhash provides a hash / hmac abstraction api in lws that works the
22 * same whether you are using openssl or mbedtls hash functions underneath.
24 #include "libwebsockets.h"
25 #include <openssl/obj_mac.h>
27 * Care: many openssl apis return 1 for success. These are translated to the
28 * lws convention of 0 for success.
32 lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type)
35 ctx->mdctx = EVP_MD_CTX_create();
40 case LWS_GENHASH_TYPE_MD5:
41 ctx->evp_type = EVP_md5();
43 case LWS_GENHASH_TYPE_SHA1:
44 ctx->evp_type = EVP_sha1();
46 case LWS_GENHASH_TYPE_SHA256:
47 ctx->evp_type = EVP_sha256();
49 case LWS_GENHASH_TYPE_SHA384:
50 ctx->evp_type = EVP_sha384();
52 case LWS_GENHASH_TYPE_SHA512:
53 ctx->evp_type = EVP_sha512();
59 if (EVP_DigestInit_ex(ctx->mdctx, ctx->evp_type, NULL) != 1) {
60 EVP_MD_CTX_destroy(ctx->mdctx);
69 lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len)
74 return EVP_DigestUpdate(ctx->mdctx, in, len) != 1;
78 lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result)
84 ret = EVP_DigestFinal_ex(ctx->mdctx, result, &len) != 1;
88 EVP_MD_CTX_destroy(ctx->mdctx);
95 lws_genhmac_init(struct lws_genhmac_ctx *ctx, enum lws_genhmac_types type,
96 const uint8_t *key, size_t key_len)
98 #if defined(LWS_HAVE_HMAC_CTX_new)
99 ctx->ctx = HMAC_CTX_new();
103 HMAC_CTX_init(&ctx->ctx);
107 case LWS_GENHMAC_TYPE_SHA256:
108 ctx->evp_type = EVP_sha256();
110 case LWS_GENHMAC_TYPE_SHA384:
111 ctx->evp_type = EVP_sha384();
113 case LWS_GENHMAC_TYPE_SHA512:
114 ctx->evp_type = EVP_sha512();
117 lwsl_err("%s: unknown HMAC type %d\n", __func__, type);
121 #if defined(LWS_HAVE_HMAC_CTX_new)
122 if (HMAC_Init_ex(ctx->ctx, key, key_len, ctx->evp_type, NULL) != 1)
124 if (HMAC_Init_ex(&ctx->ctx, key, key_len, ctx->evp_type, NULL) != 1)
131 #if defined(LWS_HAVE_HMAC_CTX_new)
132 HMAC_CTX_free(ctx->ctx);
139 lws_genhmac_update(struct lws_genhmac_ctx *ctx, const void *in, size_t len)
141 #if defined(LWS_HAVE_HMAC_CTX_new)
142 if (HMAC_Update(ctx->ctx, in, len) != 1)
144 if (HMAC_Update(&ctx->ctx, in, len) != 1)
152 lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result)
154 unsigned int size = lws_genhmac_size(ctx->type);
155 #if defined(LWS_HAVE_HMAC_CTX_new)
156 int n = HMAC_Final(ctx->ctx, result, &size);
158 HMAC_CTX_free(ctx->ctx);
160 int n = HMAC_Final(&ctx->ctx, result, &size);