|
1 | 1 | /* |
2 | 2 | * Copyright (C) 2018 Linaro Ltd |
| 3 | + * Copyright (C) 2024 BayLibre SAS |
3 | 4 | * |
4 | 5 | * SPDX-License-Identifier: Apache-2.0 |
5 | 6 | */ |
|
11 | 12 | #include <zephyr/data/jwt.h> |
12 | 13 | #include <zephyr/data/json.h> |
13 | 14 |
|
14 | | -#ifdef CONFIG_JWT_SIGN_RSA |
15 | | -#include <mbedtls/pk.h> |
16 | | -#include <mbedtls/rsa.h> |
17 | | -#include <mbedtls/sha256.h> |
18 | | -#include <zephyr/random/random.h> |
19 | | -#endif |
| 15 | +#include "jwt.h" |
20 | 16 |
|
21 | | -#ifdef CONFIG_JWT_SIGN_ECDSA |
22 | | -#include <tinycrypt/ctr_prng.h> |
23 | | -#include <tinycrypt/sha256.h> |
24 | | -#include <tinycrypt/ecc_dsa.h> |
25 | | -#include <tinycrypt/constants.h> |
26 | | - |
27 | | -#include <zephyr/random/random.h> |
| 17 | +#if defined(CONFIG_JWT_SIGN_RSA) |
| 18 | +#define JWT_SIGNATURE_LEN 256 |
| 19 | +#else /* CONFIG_JWT_SIGN_ECDSA */ |
| 20 | +#define JWT_SIGNATURE_LEN 64 |
28 | 21 | #endif |
29 | 22 |
|
30 | 23 | /* |
@@ -153,8 +146,7 @@ static int jwt_add_header(struct jwt_builder *builder) |
153 | 146 | #ifdef CONFIG_JWT_SIGN_RSA |
154 | 147 | /* {"alg":"RS256","typ":"JWT"} */ |
155 | 148 | "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9"; |
156 | | -#endif |
157 | | -#ifdef CONFIG_JWT_SIGN_ECDSA |
| 149 | +#else /* CONFIG_JWT_SIGN_ECDSA */ |
158 | 150 | /* {"alg":"ES256","typ":"JWT"} */ |
159 | 151 | "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9"; |
160 | 152 | #endif |
@@ -190,120 +182,24 @@ int jwt_add_payload(struct jwt_builder *builder, |
190 | 182 | return res; |
191 | 183 | } |
192 | 184 |
|
193 | | -#ifdef CONFIG_JWT_SIGN_RSA |
194 | | - |
195 | | -static int csprng_wrapper(void *ctx, unsigned char *dest, size_t size) |
196 | | -{ |
197 | | - ARG_UNUSED(ctx); |
198 | | - |
199 | | - return sys_csrand_get((void *)dest, size); |
200 | | -} |
201 | | - |
202 | | -int jwt_sign(struct jwt_builder *builder, |
203 | | - const char *der_key, |
204 | | - size_t der_key_len) |
205 | | -{ |
206 | | - int res; |
207 | | - mbedtls_pk_context ctx; |
208 | | - |
209 | | - mbedtls_pk_init(&ctx); |
210 | | - |
211 | | - res = mbedtls_pk_parse_key(&ctx, der_key, der_key_len, |
212 | | - NULL, 0, csprng_wrapper, NULL); |
213 | | - if (res != 0) { |
214 | | - return res; |
215 | | - } |
216 | | - |
217 | | - uint8_t hash[32], sig[256]; |
218 | | - size_t sig_len = sizeof(sig); |
219 | | - |
220 | | - /* |
221 | | - * The '0' indicates to mbedtls to do a SHA256, instead of |
222 | | - * 224. |
223 | | - */ |
224 | | - mbedtls_sha256(builder->base, builder->buf - builder->base, |
225 | | - hash, 0); |
226 | | - |
227 | | - res = mbedtls_pk_sign(&ctx, MBEDTLS_MD_SHA256, |
228 | | - hash, sizeof(hash), |
229 | | - sig, sig_len, &sig_len, |
230 | | - csprng_wrapper, NULL); |
231 | | - if (res != 0) { |
232 | | - return res; |
233 | | - } |
234 | | - |
235 | | - base64_outch(builder, '.'); |
236 | | - base64_append_bytes(sig, sig_len, builder); |
237 | | - base64_flush(builder); |
238 | | - |
239 | | - return builder->overflowed ? -ENOMEM : 0; |
240 | | -} |
241 | | -#endif |
242 | | - |
243 | | -#ifdef CONFIG_JWT_SIGN_ECDSA |
244 | | -static TCCtrPrng_t prng_state; |
245 | | -static bool prng_init; |
246 | | - |
247 | | -static const char personalize[] = "zephyr:drivers/jwt/jwt.c"; |
248 | | - |
249 | | -static int setup_prng(void) |
250 | | -{ |
251 | | - if (prng_init) { |
252 | | - return 0; |
253 | | - } |
254 | | - prng_init = true; |
255 | | - |
256 | | - uint8_t entropy[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE]; |
257 | | - |
258 | | - sys_rand_get(entropy, sizeof(entropy)); |
259 | | - |
260 | | - int res = tc_ctr_prng_init(&prng_state, |
261 | | - (const uint8_t *) &entropy, sizeof(entropy), |
262 | | - personalize, |
263 | | - sizeof(personalize)); |
264 | | - |
265 | | - return res == TC_CRYPTO_SUCCESS ? 0 : -EINVAL; |
266 | | -} |
267 | | - |
268 | | -int default_CSPRNG(uint8_t *dest, unsigned int size) |
269 | | -{ |
270 | | - int res = tc_ctr_prng_generate(&prng_state, NULL, 0, dest, size); |
271 | | - return res; |
272 | | -} |
273 | | - |
274 | 185 | int jwt_sign(struct jwt_builder *builder, |
275 | 186 | const char *der_key, |
276 | 187 | size_t der_key_len) |
277 | 188 | { |
278 | | - struct tc_sha256_state_struct ctx; |
279 | | - uint8_t hash[32], sig[64]; |
280 | | - int res; |
281 | | - |
282 | | - tc_sha256_init(&ctx); |
283 | | - tc_sha256_update(&ctx, builder->base, builder->buf - builder->base); |
284 | | - tc_sha256_final(hash, &ctx); |
| 189 | + int ret; |
| 190 | + unsigned char sig[JWT_SIGNATURE_LEN]; |
285 | 191 |
|
286 | | - res = setup_prng(); |
287 | | - |
288 | | - if (res != 0) { |
289 | | - return res; |
290 | | - } |
291 | | - uECC_set_rng(&default_CSPRNG); |
292 | | - |
293 | | - /* Note that tinycrypt only supports P-256. */ |
294 | | - res = uECC_sign(der_key, hash, sizeof(hash), |
295 | | - sig, &curve_secp256r1); |
296 | | - if (res != TC_CRYPTO_SUCCESS) { |
297 | | - return -EINVAL; |
| 192 | + ret = jwt_sign_impl(builder, der_key, der_key_len, sig, sizeof(sig)); |
| 193 | + if (ret < 0) { |
| 194 | + return ret; |
298 | 195 | } |
299 | 196 |
|
300 | 197 | base64_outch(builder, '.'); |
301 | 198 | base64_append_bytes(sig, sizeof(sig), builder); |
302 | 199 | base64_flush(builder); |
303 | 200 |
|
304 | | - return 0; |
| 201 | + return builder->overflowed ? -ENOMEM : 0; |
305 | 202 | } |
306 | | -#endif |
307 | 203 |
|
308 | 204 | int jwt_init_builder(struct jwt_builder *builder, |
309 | 205 | char *buffer, |
|
0 commit comments