libwebsockets
Lightweight C library for HTML5 websockets
lws-genec.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 
25 enum enum_genec_alg {
26  LEGENEC_UNKNOWN,
27 
28  LEGENEC_ECDH,
29  LEGENEC_ECDSA
30 };
31 
32 struct lws_genec_ctx {
33 #if defined(LWS_WITH_MBEDTLS)
34  union {
35  mbedtls_ecdh_context *ctx_ecdh;
36  mbedtls_ecdsa_context *ctx_ecdsa;
37  } u;
38 #else
39  EVP_PKEY_CTX *ctx[2];
40 #endif
41  struct lws_context *context;
42  const struct lws_ec_curves *curve_table;
43  enum enum_genec_alg genec_alg;
44 
45  char has_private;
46 };
47 
48 #if defined(LWS_WITH_MBEDTLS)
49 enum enum_lws_dh_side {
50  LDHS_OURS = MBEDTLS_ECDH_OURS,
51  LDHS_THEIRS = MBEDTLS_ECDH_THEIRS
52 };
53 #else
54 enum enum_lws_dh_side {
55  LDHS_OURS,
56  LDHS_THEIRS
57 };
58 #endif
59 
60 struct lws_ec_curves {
61  const char *name;
62  int tls_lib_nid;
63  uint16_t key_bytes;
64 };
65 
66 
67 /* ECDH-specific apis */
68 
79 LWS_VISIBLE int
80 lws_genecdh_create(struct lws_genec_ctx *ctx, struct lws_context *context,
81  const struct lws_ec_curves *curve_table);
82 
91 LWS_VISIBLE LWS_EXTERN int
92 lws_genecdh_set_key(struct lws_genec_ctx *ctx, struct lws_gencrypto_keyelem *el,
93  enum enum_lws_dh_side side);
94 
104 LWS_VISIBLE LWS_EXTERN int
105 lws_genecdh_new_keypair(struct lws_genec_ctx *ctx, enum enum_lws_dh_side side,
106  const char *curve_name, struct lws_gencrypto_keyelem *el);
107 
108 LWS_VISIBLE LWS_EXTERN int
109 lws_genecdh_compute_shared_secret(struct lws_genec_ctx *ctx, uint8_t *ss,
110  int *ss_len);
111 
112 
113 /* ECDSA-specific apis */
114 
125 LWS_VISIBLE int
126 lws_genecdsa_create(struct lws_genec_ctx *ctx, struct lws_context *context,
127  const struct lws_ec_curves *curve_table);
128 
137 LWS_VISIBLE LWS_EXTERN int
138 lws_genecdsa_new_keypair(struct lws_genec_ctx *ctx, const char *curve_name,
139  struct lws_gencrypto_keyelem *el);
140 
148 LWS_VISIBLE LWS_EXTERN int
149 lws_genecdsa_set_key(struct lws_genec_ctx *ctx,
150  const struct lws_gencrypto_keyelem *el);
151 
173 LWS_VISIBLE LWS_EXTERN int
174 lws_genecdsa_hash_sig_verify_jws(struct lws_genec_ctx *ctx, const uint8_t *in,
175  enum lws_genhash_types hash_type, int keybits,
176  const uint8_t *sig, size_t sig_len);
177 
196 LWS_VISIBLE LWS_EXTERN int
197 lws_genecdsa_hash_sign_jws(struct lws_genec_ctx *ctx, const uint8_t *in,
198  enum lws_genhash_types hash_type, int keybits,
199  uint8_t *sig, size_t sig_len);
200 
201 
202 /* Apis that apply to both ECDH and ECDSA */
203 
204 LWS_VISIBLE LWS_EXTERN void
205 lws_genec_destroy(struct lws_genec_ctx *ctx);
206 
207 LWS_VISIBLE LWS_EXTERN void
208 lws_genec_destroy_elements(struct lws_gencrypto_keyelem *el);
209 
210 LWS_VISIBLE LWS_EXTERN int
211 lws_genec_dump(struct lws_gencrypto_keyelem *el);
Definition: lws-genec.h:60
Definition: lws-gencrypto.h:99
Definition: lws-genec.h:32