inc/CryptX_Mac_KMAC.xs.inc

filter: Statementstmt 92.5% 100 / 108 Branchbran 53.6% 73 / 136 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.9% 173 / 244 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« inc/CryptX_Mode_CTR.xs.inc ↑ Summary CryptX.xs »
1 MODULE = CryptX PACKAGE = Crypt::Mac::KMAC
2  
3 PROTOTYPES: DISABLE
4  
5 #define CRYPTX_MAC_KMAC_MAX_OUTPUT_LEN 1000000000UL
6  
7 Crypt::Mac::KMAC
8 new(Class, const char * variant, SV * key, SV * cust = NULL)
9 CODE:
10 {
11 6 STRLEN k_len=0, c_len=0;
12 6 unsigned char *k=NULL, *c=NULL;
13 int rv;
14 6 int var = -1;
15  
16 6 ▶ if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar");
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 6 0 6
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 6 6
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 0 0
17 6 k = (unsigned char *) SvPVbyte(key, k_len);
18 6 ▶ if (cust && SvOK(cust)) {
Branch: 2 branches
BranchTrueFalseTotal
if (cust && SvOK(cust)) { 2 4 6
if (cust && SvOK(cust)) { 2 0 2
19 2 ▶ if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar");
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 2 0 2
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 2 2
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 0 0
20 2 c = (unsigned char *) SvPVbyte(cust, c_len);
21 }
22  
23 6 ▶ if (strcmp(variant, "KMAC128") == 0) var = LTC_KMAC128;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMAC128") == 0) var = LTC_KMAC128; 4 2 6
24 6 ▶ if (strcmp(variant, "KMAC256") == 0) var = LTC_KMAC256;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMAC256") == 0) var = LTC_KMAC256; 1 5 6
25 6 ▶ if (strcmp(variant, "KMACXOF128") == 0) var = LTC_KMAC128_XOF;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMACXOF128") == 0) var = LTC_KMAC128_XOF; 0 6 6
26 6 ▶ if (strcmp(variant, "KMACXOF256") == 0) var = LTC_KMAC256_XOF;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMACXOF256") == 0) var = LTC_KMAC256_XOF; 0 6 6
27 6 ▶ if (var == -1) croak("FATAL: unknown KMAC variant '%s'", variant);
Branch: 1 branch
BranchTrueFalseTotal
if (var == -1) croak("FATAL: unknown KMAC variant '%s'", variant); 1 5 6
28  
29 5 Newz(0, RETVAL, 1, struct cryptx_mac_kmac_struct);
30 5 ▶ if (!RETVAL) croak("FATAL: Newz failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) croak("FATAL: Newz failed"); 0 5 5
31  
32 5 rv = kmac_init(&RETVAL->state, var, k, (unsigned long)k_len, c, (unsigned long)c_len);
33 5 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 5 5
34 0 Safefree(RETVAL);
35 0 croak("FATAL: kmac_init failed: %s", error_to_string(rv));
36 }
37 5 RETVAL->finalized = 0;
38 5 RETVAL->variant = var;
39 }
40 OUTPUT:
41 RETVAL
42  
43 void
44 DESTROY(Crypt::Mac::KMAC self)
45 CODE:
46 {
47 6 zeromem(&self->state, sizeof(kmac_state));
48 6 Safefree(self);
49 }
50  
51 Crypt::Mac::KMAC
52 clone(Crypt::Mac::KMAC self)
53 CODE:
54 1 Newz(0, RETVAL, 1, struct cryptx_mac_kmac_struct);
55 1 ▶ if (!RETVAL) croak("FATAL: Newz failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) croak("FATAL: Newz failed"); 0 1 1
56 1 Copy(self, RETVAL, 1, struct cryptx_mac_kmac_struct);
57 OUTPUT:
58 RETVAL
59  
60 void
61 add(Crypt::Mac::KMAC self, ...)
62 PPCODE:
63 {
64 int rv, i;
65 STRLEN in_data_len;
66 unsigned char *in_data;
67  
68 9 ▶ if (self->finalized) croak("FATAL: MAC object already finalized");
Branch: 1 branch
BranchTrueFalseTotal
if (self->finalized) croak("FATAL: MAC object already finalized"); 1 8 9
69 16 ▶ for(i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i < items; i++) { 8 8 16
70 8 in_data = (unsigned char *)SvPVbyte(ST(i), in_data_len);
71 8 ▶ if (in_data_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_data_len > 0) { 8 0 8
72 8 rv = kmac_process(&self->state, in_data, (unsigned long)in_data_len);
73 8 ▶ if (rv != CRYPT_OK) croak("FATAL: kmac_process failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: kmac_process failed: %s", error_to_string(rv)); 0 8 8
74 }
75 }
76 8 ▶ XPUSHs(ST(0)); /* return self */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(0)); /* return self */ 0 8 8
77 }
78  
79 SV *
80 mac(Crypt::Mac::KMAC self, unsigned long out_len)
81 ALIAS:
82 hexmac = 1
83 b64mac = 2
84 b64umac = 3
85 CODE:
86 {
87 unsigned char *mac_buf;
88 unsigned long maclen, outlen;
89 int rv;
90 char *out;
91  
92 7 ▶ if (self->finalized) croak("FATAL: MAC object already finalized");
Branch: 1 branch
BranchTrueFalseTotal
if (self->finalized) croak("FATAL: MAC object already finalized"); 1 6 7
93 6 ▶ if (out_len == 0) croak("FATAL: invalid output length");
Branch: 1 branch
BranchTrueFalseTotal
if (out_len == 0) croak("FATAL: invalid output length"); 0 6 6
94 6 ▶ if (out_len > CRYPTX_MAC_KMAC_MAX_OUTPUT_LEN) croak("FATAL: output length too large");
Branch: 1 branch
BranchTrueFalseTotal
if (out_len > CRYPTX_MAC_KMAC_MAX_OUTPUT_LEN) croak("FATAL: output length too large"); 0 6 6
95  
96 6 Newz(0, mac_buf, out_len, unsigned char);
97 6 ▶ if (!mac_buf) croak("FATAL: Newz failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!mac_buf) croak("FATAL: Newz failed"); 0 6 6
98 6 maclen = out_len;
99 6 rv = kmac_done(&self->state, mac_buf, &maclen);
100 6 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 6 6
101 0 Safefree(mac_buf);
102 0 croak("FATAL: kmac_done failed: %s", error_to_string(rv));
103 }
104 6 self->finalized = 1;
105  
106 6 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 1 5 6
107 1 RETVAL = newSVpvn((char *)mac_buf, maclen);
108 }
109 else {
110 /* worst case base16 -> 2x; base64 -> ~4/3x; allocate 2x + 4 to be safe */
111 5 unsigned long buflen = maclen * 2 + 8;
112 5 Newz(0, out, buflen, char);
113 5 ▶ if (!out) { Safefree(mac_buf); croak("FATAL: Newz failed"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { Safefree(mac_buf); croak("FATAL: Newz failed"); } 0 5 5
114 5 outlen = buflen;
115 5 ▶ if (ix == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) { 0 5 5
116 0 rv = base64url_encode(mac_buf, maclen, out, &outlen);
117 0 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); } 0 0 0
118 }
119 5 ▶ else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) { 0 5 5
120 0 rv = base64_encode(mac_buf, maclen, out, &outlen);
121 0 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64_encode failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64_encode failed: %s", error_to_string(rv)); } 0 0 0
122 }
123 else { /* ix == 1 */
124 5 rv = base16_encode(mac_buf, maclen, out, &outlen, 0);
125 5 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base16_encode failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base16_encode failed: %s", error_to_string(rv)); } 0 5 5
126 }
127 5 RETVAL = newSVpvn(out, outlen);
128 5 Safefree(out);
129 }
130 6 zeromem(mac_buf, out_len);
131 6 Safefree(mac_buf);
132 }
133 OUTPUT:
134 RETVAL
135  
136 SV *
137 kmac(const char * variant, unsigned long out_len, SV * key, SV * cust, ...)
138 ALIAS:
139 kmac_hex = 1
140 kmac_b64 = 2
141 kmac_b64u = 3
142 CODE:
143 {
144 24 STRLEN inlen, klen, clen=0;
145 24 unsigned char *in, *k, *c=NULL;
146 int rv, i;
147 24 int var = -1;
148 unsigned char *mac_buf;
149 unsigned long maclen, outlen;
150 char *out;
151 kmac_state st;
152  
153 24 ▶ if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar");
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 24 0 24
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 24 24
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 0 0
154 24 k = (unsigned char *)SvPVbyte(key, klen);
155 24 ▶ if (SvOK(cust)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(cust)) { 24 0 24
156 24 ▶ if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar");
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 24 0 24
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 24 24
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 0 0
if (!SvPOK_spec(cust)) croak("FATAL: cust must be string/buffer scalar"); 0 0 0
157 24 c = (unsigned char *)SvPVbyte(cust, clen);
158 }
159  
160 24 ▶ if (strcmp(variant, "KMAC128") == 0) var = LTC_KMAC128;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMAC128") == 0) var = LTC_KMAC128; 11 13 24
161 24 ▶ if (strcmp(variant, "KMAC256") == 0) var = LTC_KMAC256;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMAC256") == 0) var = LTC_KMAC256; 2 22 24
162 24 ▶ if (strcmp(variant, "KMACXOF128") == 0) var = LTC_KMAC128_XOF;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMACXOF128") == 0) var = LTC_KMAC128_XOF; 5 19 24
163 24 ▶ if (strcmp(variant, "KMACXOF256") == 0) var = LTC_KMAC256_XOF;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(variant, "KMACXOF256") == 0) var = LTC_KMAC256_XOF; 2 22 24
164 24 ▶ if (var == -1) croak("FATAL: unknown KMAC variant '%s'", variant);
Branch: 1 branch
BranchTrueFalseTotal
if (var == -1) croak("FATAL: unknown KMAC variant '%s'", variant); 4 20 24
165 20 ▶ if (out_len == 0) croak("FATAL: invalid output length");
Branch: 1 branch
BranchTrueFalseTotal
if (out_len == 0) croak("FATAL: invalid output length"); 1 19 20
166 19 ▶ if (out_len > CRYPTX_MAC_KMAC_MAX_OUTPUT_LEN) croak("FATAL: output length too large");
Branch: 1 branch
BranchTrueFalseTotal
if (out_len > CRYPTX_MAC_KMAC_MAX_OUTPUT_LEN) croak("FATAL: output length too large"); 0 19 19
167  
168 19 rv = kmac_init(&st, var, k, (unsigned long)klen, c, (unsigned long)clen);
169 19 ▶ if (rv != CRYPT_OK) croak("FATAL: kmac_init failed: %s", error_to_string(rv));
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) croak("FATAL: kmac_init failed: %s", error_to_string(rv)); 0 19 19
170 38 ▶ for (i = 4; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 4; i < items; i++) { 19 19 38
171 19 in = (unsigned char *)SvPVbyte(ST(i), inlen);
172 19 ▶ if (inlen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (inlen > 0) { 18 1 19
173 18 rv = kmac_process(&st, in, (unsigned long)inlen);
174 18 ▶ if (rv != CRYPT_OK) { zeromem(&st, sizeof(st)); croak("FATAL: kmac_process failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { zeromem(&st, sizeof(st)); croak("FATAL: kmac_process failed: %s", error_to_string(rv)); } 0 18 18
175 }
176 }
177 19 Newz(0, mac_buf, out_len, unsigned char);
178 19 ▶ if (!mac_buf) { zeromem(&st, sizeof(st)); croak("FATAL: Newz failed"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!mac_buf) { zeromem(&st, sizeof(st)); croak("FATAL: Newz failed"); } 0 19 19
179 19 maclen = out_len;
180 19 rv = kmac_done(&st, mac_buf, &maclen);
181 19 zeromem(&st, sizeof(st));
182 19 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); croak("FATAL: kmac_done failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); croak("FATAL: kmac_done failed: %s", error_to_string(rv)); } 0 19 19
183  
184 19 ▶ if (ix == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 0) { 7 12 19
185 7 RETVAL = newSVpvn((char *)mac_buf, maclen);
186 }
187 else {
188 12 unsigned long buflen = maclen * 2 + 8;
189 12 Newz(0, out, buflen, char);
190 12 ▶ if (!out) { Safefree(mac_buf); croak("FATAL: Newz failed"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { Safefree(mac_buf); croak("FATAL: Newz failed"); } 0 12 12
191 12 outlen = buflen;
192 12 ▶ if (ix == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) { 1 11 12
193 1 rv = base64url_encode(mac_buf, maclen, out, &outlen);
194 1 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); } 0 1 1
195 }
196 11 ▶ else if (ix == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (ix == 2) { 1 10 11
197 1 rv = base64_encode(mac_buf, maclen, out, &outlen);
198 1 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64_encode failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base64_encode failed: %s", error_to_string(rv)); } 0 1 1
199 }
200 else { /* ix == 1 */
201 10 rv = base16_encode(mac_buf, maclen, out, &outlen, 0);
202 10 ▶ if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base16_encode failed: %s", error_to_string(rv)); }
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { Safefree(mac_buf); Safefree(out); croak("FATAL: base16_encode failed: %s", error_to_string(rv)); } 0 10 10
203 }
204 12 RETVAL = newSVpvn(out, outlen);
205 12 Safefree(out);
206 }
207 19 zeromem(mac_buf, out_len);
208 19 Safefree(mac_buf);
209 }
210 OUTPUT:
211 RETVAL
« inc/CryptX_Mode_CTR.xs.inc ↑ Summary CryptX.xs »