• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-api-test-gencrypto - lws-genec
3  *
4  * Written in 2010-2018 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  */
9 
10 #include <libwebsockets.h>
11 
12 static const uint8_t
13 	*jwk_ec1 = (uint8_t *)
14 		"{\"kty\":\"EC\","
15 		  "\"crv\":\"P-256\","
16 		  "\"x\":\"MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4\","
17 		  "\"y\":\"4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM\","
18 		  "\"d\":\"870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE\","
19 		  "\"use\":\"enc\","
20 		  "\"kid\":\"rfc7517-A.2-example private key\"}"
21 ;
22 
23 static int
test_genec1(struct lws_context * context)24 test_genec1(struct lws_context *context)
25 {
26 	struct lws_genec_ctx ctx;
27 	struct lws_jwk jwk;
28 	struct lws_gencrypto_keyelem el[LWS_GENCRYPTO_EC_KEYEL_COUNT];
29 	//uint8_t res[32], res1[32];
30 	int n;
31 
32 	memset(el, 0, sizeof(el));
33 
34 	if (lws_genecdh_create(&ctx, context, NULL))
35 		return 1;
36 
37 	/* let's create a new key */
38 
39 	if (lws_genecdh_new_keypair(&ctx, LDHS_OURS, "P-256", el)) {
40 		lwsl_err("%s: lws_genec_new_keypair failed\n", __func__);
41 		return 1;
42 	}
43 
44 	lws_genec_dump(el);
45 	lws_genec_destroy_elements(el);
46 
47 	lws_genec_destroy(&ctx);
48 
49 	if (lws_jwk_import(&jwk, NULL, NULL, (char *)jwk_ec1,
50 			   strlen((char *)jwk_ec1)) < 0) {
51 		lwsl_notice("Failed to decode JWK test key\n");
52 		return 1;
53 	}
54 
55 	lws_jwk_dump(&jwk);
56 
57 	if (jwk.kty != LWS_GENCRYPTO_KTY_EC) {
58 		lws_jwk_destroy(&jwk);
59 		lwsl_err("%s: jwk is not an EC key\n", __func__);
60 		return 1;
61 	}
62 
63 	if (lws_genecdh_create(&ctx, context, NULL))
64 		return 1;
65 
66 	n = lws_genecdh_set_key(&ctx, jwk.e, LDHS_OURS);
67 	if (n) {
68 		lws_jwk_destroy(&jwk);
69 		lwsl_err("%s: lws_genec_create failed: %d\n", __func__, n);
70 		return 1;
71 	}
72 #if 0
73 	if (lws_genec_crypt(&ctx, cbc256, 16, res, (uint8_t *)cbc256_iv,
74 			     NULL, NULL)) {
75 		lwsl_err("%s: lws_genec_crypt failed\n", __func__);
76 		goto bail;
77 	}
78 
79 	if (lws_timingsafe_bcmp(cbc256_enc, res, 16)) {
80 		lwsl_err("%s: lws_genec_crypt encoding mismatch\n", __func__);
81 		lwsl_hexdump_notice(res, 16);
82 		goto bail;
83 	}
84 
85 	lws_genec_destroy(&ctx);
86 
87 	if (lws_genec_create(&ctx, LWS_GAESO_DEC, LWS_GAESM_CBC, &e, NULL)) {
88 		lwsl_err("%s: lws_genec_create dec failed\n", __func__);
89 		return -1;
90 	}
91 
92 	if (lws_genec_crypt(&ctx, res, 16, res1, (uint8_t *)cbc256_iv,
93 			     NULL, NULL)) {
94 		lwsl_err("%s: lws_genec_crypt dec failed\n", __func__);
95 		goto bail;
96 	}
97 
98 	if (lws_timingsafe_bcmp(cbc256, res1, 16)) {
99 		lwsl_err("%s: lws_genec_crypt decoding mismatch\n", __func__);
100 		lwsl_hexdump_notice(res, 16);
101 		goto bail;
102 	}
103 #endif
104 	lws_genec_destroy(&ctx);
105 
106 	lws_jwk_destroy(&jwk);
107 
108 	return 0;
109 
110 //bail:
111 //	lws_genec_destroy(&ctx);
112 
113 //	return -1;
114 }
115 
116 int
test_genec(struct lws_context * context)117 test_genec(struct lws_context *context)
118 {
119 	if (test_genec1(context))
120 		goto bail;
121 
122 	/* end */
123 
124 	lwsl_notice("%s: selftest OK\n", __func__);
125 
126 	return 0;
127 
128 bail:
129 	lwsl_err("%s: selftest failed ++++++++++++++++++++\n", __func__);
130 
131 	return 1;
132 }
133