include/cjws/cjws_digest.h

filter: Statementstmt 87.8% 29 / 33 Branchbran 50.0% 9 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.5% 38 / 51 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/cjws/cjws_compat.h ↑ Summary xs/key.xs »
1 #ifndef CJWS_DIGEST_H
2 #define CJWS_DIGEST_H
3  
4 #include <openssl/evp.h>
5  
6 /* One-shot SHA-2 digests and HMAC. HMAC runs through the same
7 * EVP_DigestSign machinery the asymmetric algorithms use
8 * (EVP_PKEY_new_raw_private_key exists on 1.1 and 3.x alike), so the
9 * whole JWS algorithm table is one code path. */
10  
11 110 static const EVP_MD *cjws_md_by_bits(int bits) {
12 110 switch (bits) {
13 /* 160 is SHA-1, which no JWS algorithm reaches - cjws_alg_parse
14 * still admits only 256/384/512. It exists for the raw digest and
15 * HMAC surface, where RFC 6238 makes HMAC-SHA1 the interop
16 * default every authenticator app assumes. */
17 16 case 160: return EVP_sha1();
18 70 case 256: return EVP_sha256();
19 10 case 384: return EVP_sha384();
20 14 case 512: return EVP_sha512();
21 }
22 0 return NULL;
23 }
24  
25 /* out must hold EVP_MAX_MD_SIZE. Returns digest length or 0 on error. */
26 29 static unsigned cjws_digest(int bits, const unsigned char *in, STRLEN n,
27 unsigned char *out) {
28 29 const EVP_MD *md = cjws_md_by_bits(bits);
29 29 unsigned outlen = 0;
30 29 ▶ if (!md) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!md) return 0; 0 29 29
31 29 ▶ if (EVP_Digest(in, n, out, &outlen, md, NULL) != 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (EVP_Digest(in, n, out, &outlen, md, NULL) != 1) return 0; 0 29 29
32 29 return outlen;
33 }
34  
35 /* HMAC-SHA-2. out must hold EVP_MAX_MD_SIZE. Returns MAC length or 0. */
36 34 static STRLEN cjws_hmac(int bits, const unsigned char *key, STRLEN keylen,
37 const unsigned char *in, STRLEN n,
38 unsigned char *out) {
39 34 const EVP_MD *md = cjws_md_by_bits(bits);
40 34 EVP_PKEY *pk = NULL;
41 34 EVP_MD_CTX *ctx = NULL;
42 34 size_t maclen = 0;
43 34 ▶ if (!md) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!md) return 0; 0 34 34
44 34 pk = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, key, keylen);
45 34 ▶ if (!pk) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!pk) return 0; 0 34 34
46 34 ctx = EVP_MD_CTX_new();
47 34 ▶ if (!ctx) { EVP_PKEY_free(pk); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) { EVP_PKEY_free(pk); return 0; } 0 34 34
48 34 ▶ if (EVP_DigestSignInit(ctx, NULL, md, NULL, pk) == 1
Branch: 1 branch
BranchTrueFalseTotal
if (EVP_DigestSignInit(ctx, NULL, md, NULL, pk) == 1 34 0 34
49 34 ▶ && EVP_DigestSign(ctx, NULL, &maclen, in, n) == 1
Branch: 1 branch
BranchTrueFalseTotal
&& EVP_DigestSign(ctx, NULL, &maclen, in, n) == 1 34 0 34
50 34 ▶ && maclen <= EVP_MAX_MD_SIZE
Branch: 1 branch
BranchTrueFalseTotal
&& maclen <= EVP_MAX_MD_SIZE 34 0 34
51 34 ▶ && EVP_DigestSign(ctx, out, &maclen, in, n) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
&& EVP_DigestSign(ctx, out, &maclen, in, n) == 1) { 34 0 34
52 34 EVP_MD_CTX_free(ctx);
53 34 EVP_PKEY_free(pk);
54 34 return (STRLEN)maclen;
55 }
56 0 EVP_MD_CTX_free(ctx);
57 0 EVP_PKEY_free(pk);
58 0 return 0;
59 }
60  
61 #endif
« include/cjws/cjws_compat.h ↑ Summary xs/key.xs »