libwebsockets
Lightweight C library for HTML5 websockets
lws-genhash.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
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 
35 
36 enum lws_genhash_types {
37  LWS_GENHASH_TYPE_UNKNOWN,
38  LWS_GENHASH_TYPE_MD5,
39  LWS_GENHASH_TYPE_SHA1,
40  LWS_GENHASH_TYPE_SHA256,
41  LWS_GENHASH_TYPE_SHA384,
42  LWS_GENHASH_TYPE_SHA512,
43 };
44 
45 enum lws_genhmac_types {
46  LWS_GENHMAC_TYPE_UNKNOWN,
47  LWS_GENHMAC_TYPE_SHA256,
48  LWS_GENHMAC_TYPE_SHA384,
49  LWS_GENHMAC_TYPE_SHA512,
50 };
51 
52 #define LWS_GENHASH_LARGEST 64
53 
55  uint8_t type;
56 #if defined(LWS_WITH_MBEDTLS)
57  union {
58  mbedtls_md5_context md5;
59  mbedtls_sha1_context sha1;
60  mbedtls_sha256_context sha256;
61  mbedtls_sha512_context sha512; /* 384 also uses this */
62  const mbedtls_md_info_t *hmac;
63  } u;
64 #else
65  const EVP_MD *evp_type;
66  EVP_MD_CTX *mdctx;
67 #endif
68 };
69 
71  uint8_t type;
72 #if defined(LWS_WITH_MBEDTLS)
73  const mbedtls_md_info_t *hmac;
74  mbedtls_md_context_t ctx;
75 #else
76  const EVP_MD *evp_type;
77 
78 #if defined(LWS_HAVE_EVP_PKEY_new_raw_private_key)
79  EVP_MD_CTX *ctx;
80  EVP_PKEY *key;
81 #else
82 #if defined(LWS_HAVE_HMAC_CTX_new)
83  HMAC_CTX *ctx;
84 #else
85  HMAC_CTX ctx;
86 #endif
87 #endif
88 
89 #endif
90 };
91 
98 LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT
99 lws_genhash_size(enum lws_genhash_types type);
100 
107 LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT
108 lws_genhmac_size(enum lws_genhmac_types type);
109 
117 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
118 lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type);
119 
128 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
129 lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len);
130 
142 LWS_VISIBLE LWS_EXTERN int
143 lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result);
144 
157 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
158 lws_genhmac_init(struct lws_genhmac_ctx *ctx, enum lws_genhmac_types type,
159  const uint8_t *key, size_t key_len);
160 
171 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
172 lws_genhmac_update(struct lws_genhmac_ctx *ctx, const void *in, size_t len);
173 
185 LWS_VISIBLE LWS_EXTERN int
186 lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result);
LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT lws_genhash_size(enum lws_genhash_types type)
LWS_VISIBLE LWS_EXTERN int lws_genhmac_destroy(struct lws_genhmac_ctx *ctx, void *result)
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_genhash_update(struct lws_genhash_ctx *ctx, const void *in, size_t len)
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_genhash_init(struct lws_genhash_ctx *ctx, enum lws_genhash_types type)
LWS_VISIBLE LWS_EXTERN int lws_genhash_destroy(struct lws_genhash_ctx *ctx, void *result)
LWS_VISIBLE LWS_EXTERN size_t LWS_WARN_UNUSED_RESULT lws_genhmac_size(enum lws_genhmac_types type)
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_genhmac_init(struct lws_genhmac_ctx *ctx, enum lws_genhmac_types type, const uint8_t *key, size_t key_len)
LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT lws_genhmac_update(struct lws_genhmac_ctx *ctx, const void *in, size_t len)
Definition: lws-genhash.h:54
Definition: lws-genhash.h:70