xs/kx.xs

filter: Statementstmt 62.7% 101 / 161 Branchbran 54.6% 59 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 59.4% 160 / 269 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/stream.xs ↑ Summary xs/onetimeauth.xs »
1 =for documentation
2  
3 Note that for kx, aliases are provided for the current default primitive
4 (x25519blake2b) even though libsodium does not provide algorithm-specific
5 functions.
6  
7 =cut
8  
9 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::kx
10  
11 void _define_constants()
12 PREINIT:
13 1 HV *stash = gv_stashpv("Crypt::Sodium::XS::kx", 0);
14  
15 PPCODE:
16 1 newCONSTSUB(stash, "kx_PUBLICKEYBYTES", newSVuv(crypto_kx_PUBLICKEYBYTES));
17 1 newCONSTSUB(stash, "kx_x25519blake2b_PUBLICKEYBYTES",
18 newSVuv(crypto_kx_PUBLICKEYBYTES));
19 1 newCONSTSUB(stash, "kx_SECRETKEYBYTES", newSVuv(crypto_kx_SECRETKEYBYTES));
20 1 newCONSTSUB(stash, "kx_x25519blake2b_SECRETKEYBYTES",
21 newSVuv(crypto_kx_SECRETKEYBYTES));
22 1 newCONSTSUB(stash, "kx_SEEDBYTES", newSVuv(crypto_kx_SEEDBYTES));
23 1 newCONSTSUB(stash, "kx_x25519blake2b_SEEDBYTES",
24 newSVuv(crypto_kx_SEEDBYTES));
25 1 newCONSTSUB(stash, "kx_SESSIONKEYBYTES", newSVuv(crypto_kx_SESSIONKEYBYTES));
26 1 newCONSTSUB(stash, "kx_x25519blake2b_SESSIONKEYBYTES",
27 newSVuv(crypto_kx_SESSIONKEYBYTES));
28 1 newCONSTSUB(stash, "kx_PRIMITIVE", newSVpvs(crypto_kx_PRIMITIVE));
29  
30 void kx_keypair(SV * seed = &PL_sv_undef, SV * flags = &PL_sv_undef)
31  
32 ALIAS:
33 kx_x25519blake2b_keypair = 1
34  
35 PREINIT:
36 protmem *sk_pm;
37 SV *pk_sv;
38 unsigned char *pk_buf;
39 12 ▶ unsigned int sk_flags = g_protmem_default_flags_key;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int sk_flags = g_protmem_default_flags_key; 8 4 12
40  
41 PPCODE:
42 PERL_UNUSED_VAR(ix);
43  
44 12 SvGETMAGIC(flags);
45 12 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 12 12
46 0 sk_flags = SvUV_nomg(flags);
47  
48 12 Newx(pk_buf, crypto_kx_PUBLICKEYBYTES + 1, unsigned char);
49 12 ▶ if (pk_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pk_buf == NULL) 0 12 12
50 0 croak("kx_keypair: Failed to allocate memory");
51 12 pk_buf[crypto_kx_PUBLICKEYBYTES] = '\0';
52  
53 12 SvGETMAGIC(seed);
54 12 ▶ if (!SvOK(seed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(seed)) { 8 4 12
55 8 sk_pm = protmem_init(aTHX_ crypto_kx_SECRETKEYBYTES, sk_flags);
56 8 ▶ if (sk_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sk_pm == NULL) { 0 8 8
57 0 Safefree(pk_buf);
58 0 croak("kx_keypair: Failed to allocate protmem");
59 }
60  
61 8 crypto_kx_keypair(pk_buf, sk_pm->pm_ptr);
62  
63 8 ▶ if (protmem_release(aTHX_ sk_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ sk_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 8 8
64 0 protmem_free(aTHX_ sk_pm);
65 0 Safefree(pk_buf);
66 0 croak("kx_keypair: Failed to release sk protmem RW");
67 }
68 }
69 else {
70 /* from seed */
71 4 protmem *seed_pm = NULL;
72 unsigned char *seed_buf;
73 STRLEN seed_len;
74  
75 4 ▶ if (sv_derived_from(seed, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(seed, MEMVAULT_CLASS)) { 2 2 4
76 2 seed_pm = protmem_get(aTHX_ seed, MEMVAULT_CLASS);
77 2 seed_buf = seed_pm->pm_ptr;
78 2 seed_len = seed_pm->size;
79 }
80 else
81 2 seed_buf = (unsigned char *)SvPVbyte_nomg(seed, seed_len);
82  
83 4 ▶ if (seed_len != crypto_kx_SEEDBYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (seed_len != crypto_kx_SEEDBYTES) { 0 4 4
84 0 Safefree(pk_buf);
85 0 croak("kx_keypair: Invalid seed length: %lu", seed_len);
86 }
87  
88 4 sk_pm = protmem_init(aTHX_ crypto_kx_SECRETKEYBYTES, sk_flags);
89 4 ▶ if (sk_pm == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (sk_pm == NULL) { 0 4 4
90 0 Safefree(pk_buf);
91 0 croak("kx_keypair: Failed to allocate protmem");
92 }
93 /* use flags from seed_pm (if there was one)? */
94  
95 4 ▶ if (seed_pm && protmem_grant(aTHX_ seed_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (seed_pm && protmem_grant(aTHX_ seed_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 2 2 4
if (seed_pm && protmem_grant(aTHX_ seed_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
96 0 protmem_free(aTHX_ sk_pm);
97 0 Safefree(pk_buf);
98 0 croak("kx_keypair: Failed to grant seed protmem RO");
99 }
100  
101 4 crypto_kx_seed_keypair(pk_buf, sk_pm->pm_ptr, seed_buf);
102  
103 4 ▶ if (seed_pm && protmem_release(aTHX_ seed_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (seed_pm && protmem_release(aTHX_ seed_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 2 2 4
if (seed_pm && protmem_release(aTHX_ seed_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 2 2
104 0 protmem_free(aTHX_ sk_pm);
105 0 Safefree(pk_buf);
106 0 croak("kx_keypair: Failed to release seed protmem RO");
107 }
108  
109 4 ▶ if (protmem_release(aTHX_ sk_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ sk_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 4 4
110 0 protmem_free(aTHX_ sk_pm);
111 0 Safefree(pk_buf);
112 0 croak("kx_keypair: Failed to release sk protmem RW");
113 }
114 }
115  
116 12 pk_sv = newSV(0);
117 12 sv_usepvn_flags(pk_sv, (char *)pk_buf, crypto_kx_PUBLICKEYBYTES, SV_HAS_TRAILING_NUL);
118 12 ▶ mXPUSHs(pk_sv);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(pk_sv); 0 12 12
119 12 ▶ mXPUSHs(protmem_to_sv(aTHX_ sk_pm, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ sk_pm, MEMVAULT_CLASS)); 0 12 12
120 12 XSRETURN(2);
121  
122 void kx_client_session_keys(SV * cpk, SV * csk, SV * spk, SV * flags = &PL_sv_undef)
123  
124 ALIAS:
125 kx_x25519blake2b_client_session_keys = 1
126  
127 PREINIT:
128 6 protmem *rx, *tx, *csk_pm = NULL;
129 unsigned char * cpk_buf, * csk_buf, * spk_buf;
130 STRLEN cpk_len, csk_len, spk_len;
131 6 ▶ unsigned int key_flags = g_protmem_default_flags_key;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int key_flags = g_protmem_default_flags_key; 6 0 6
132 int ret;
133  
134 PPCODE:
135 PERL_UNUSED_VAR(ix);
136  
137 6 SvGETMAGIC(flags);
138 6 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 6 6
139 0 key_flags = SvUV_nomg(flags);
140  
141 6 cpk_buf = (unsigned char *)SvPVbyte(cpk, cpk_len);
142 6 ▶ if (cpk_len != crypto_kx_PUBLICKEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (cpk_len != crypto_kx_PUBLICKEYBYTES) 0 6 6
143 0 croak("kx_client_session_keys: Invalid public key length %lu", cpk_len);
144  
145 6 ▶ if (sv_derived_from(csk, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(csk, MEMVAULT_CLASS)) { 6 0 6
146 6 csk_pm = protmem_get(aTHX_ csk, MEMVAULT_CLASS);
147 6 csk_buf = csk_pm->pm_ptr;
148 6 csk_len = csk_pm->size;
149 }
150 else
151 0 csk_buf = (unsigned char *)SvPVbyte(csk, csk_len);
152 6 ▶ if (csk_len != crypto_kx_SECRETKEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (csk_len != crypto_kx_SECRETKEYBYTES) 0 6 6
153 0 croak("kx_client_session_keys: Invalid secret key length %lu", csk_len);
154  
155 6 spk_buf = (unsigned char *)SvPVbyte(spk, spk_len);
156 6 ▶ if (spk_len != crypto_kx_PUBLICKEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (spk_len != crypto_kx_PUBLICKEYBYTES) 0 6 6
157 0 croak("kx_client_session_keys: Invalid public key length %lu", spk_len);
158  
159 6 rx = protmem_init(aTHX_ crypto_kx_SESSIONKEYBYTES, key_flags);
160 6 ▶ if (rx == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (rx == NULL) 0 6 6
161 0 croak("kx_client_session_keys: Failed to allocate rx protmem");
162 6 ▶ if (csk_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (csk_pm) 6 0 6
163 6 rx->flags = csk_pm->flags;
164  
165 6 tx = protmem_init(aTHX_ crypto_kx_SESSIONKEYBYTES, key_flags);
166 6 ▶ if (tx == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tx == NULL) { 0 6 6
167 0 protmem_free(aTHX_ rx);
168 0 croak("kx_client_session_keys: Failed to allocate tx protmem");
169 }
170 6 ▶ if (csk_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (csk_pm) 6 0 6
171 6 tx->flags = csk_pm->flags;
172  
173 6 ▶ if (csk_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (csk_pm) 6 0 6
174 6 ▶ if (protmem_grant(aTHX_ csk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_grant(aTHX_ csk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
175 0 protmem_free(aTHX_ rx);
176 0 protmem_free(aTHX_ tx);
177 0 croak("kx_client_session_keys: Failed to grant csk protmem RO");
178 }
179  
180 6 ret = crypto_kx_client_session_keys(rx->pm_ptr, tx->pm_ptr,
181 cpk_buf, csk_buf, spk_buf);
182  
183 6 ▶ if (csk_pm && protmem_release(aTHX_ csk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (csk_pm && protmem_release(aTHX_ csk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 6 0 6
if (csk_pm && protmem_release(aTHX_ csk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
184 0 protmem_free(aTHX_ rx);
185 0 protmem_free(aTHX_ tx);
186 0 croak("kx_client_session_keys: Failed to release csk protmem RO");
187 }
188  
189 6 ▶ if (protmem_release(aTHX_ rx, PROTMEM_FLAG_MPROTECT_RW) != 0
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ rx, PROTMEM_FLAG_MPROTECT_RW) != 0 6 0 6
190 6 ▶ || protmem_release(aTHX_ tx, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
|| protmem_release(aTHX_ tx, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 6 6
191 0 protmem_free(aTHX_ rx);
192 0 protmem_free(aTHX_ tx);
193 0 croak("kx_client_session_keys: Failed to release rx/tx protmem RW");
194 }
195  
196 6 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 6 6
197 0 protmem_free(aTHX_ rx);
198 0 protmem_free(aTHX_ tx);
199 0 croak("kx_client_session_keys: Failed to generate session keys (invalid server pubkey?)");
200 }
201  
202 6 ▶ mXPUSHs(protmem_to_sv(aTHX_ rx, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ rx, MEMVAULT_CLASS)); 0 6 6
203 6 ▶ mXPUSHs(protmem_to_sv(aTHX_ tx, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ tx, MEMVAULT_CLASS)); 0 6 6
204 6 XSRETURN(2);
205  
206 void kx_server_session_keys(SV * spk, SV * ssk, SV * cpk, SV * flags = &PL_sv_undef)
207  
208 ALIAS:
209 kx_x25519blake2b_server_session_keys = 1
210  
211 PREINIT:
212 6 protmem *rx, *tx, *ssk_pm = NULL;
213 unsigned char * spk_buf, * ssk_buf, * cpk_buf;
214 STRLEN spk_len, ssk_len, cpk_len;
215 6 ▶ unsigned int key_flags = g_protmem_default_flags_key;
Branch: 1 branch
BranchTrueFalseTotal
unsigned int key_flags = g_protmem_default_flags_key; 6 0 6
216 int ret;
217  
218 PPCODE:
219 PERL_UNUSED_VAR(ix);
220  
221 6 SvGETMAGIC(flags);
222 6 ▶ if (SvOK(flags))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(flags)) 0 6 6
223 0 key_flags = SvUV_nomg(flags);
224  
225 6 spk_buf = (unsigned char *)SvPVbyte(spk, spk_len);
226 6 ▶ if (spk_len != crypto_kx_PUBLICKEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (spk_len != crypto_kx_PUBLICKEYBYTES) 0 6 6
227 0 croak("kx_server_session_keys: Invalid public key length %lu", spk_len);
228  
229 6 ▶ if (sv_derived_from(ssk, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(ssk, MEMVAULT_CLASS)) { 6 0 6
230 6 ssk_pm = protmem_get(aTHX_ ssk, MEMVAULT_CLASS);
231 6 ssk_buf = ssk_pm->pm_ptr;
232 6 ssk_len = ssk_pm->size;
233 }
234 else
235 0 ssk_buf = (unsigned char *)SvPVbyte(ssk, ssk_len);
236 6 ▶ if (ssk_len != crypto_kx_SECRETKEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (ssk_len != crypto_kx_SECRETKEYBYTES) 0 6 6
237 0 croak("kx_server_session_keys: Invalid secret key length %lu", ssk_len);
238  
239 6 cpk_buf = (unsigned char *)SvPVbyte(cpk, cpk_len);
240 6 ▶ if (cpk_len != crypto_kx_PUBLICKEYBYTES)
Branch: 1 branch
BranchTrueFalseTotal
if (cpk_len != crypto_kx_PUBLICKEYBYTES) 0 6 6
241 0 croak("kx_server_session_keys: Invalid public key length %lu", cpk_len);
242  
243 6 rx = protmem_init(aTHX_ crypto_kx_SESSIONKEYBYTES, key_flags);
244 6 ▶ if (rx == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (rx == NULL) 0 6 6
245 0 croak("kx_server_session_keys: Failed to allocate rx protmem");
246 6 ▶ if (ssk_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ssk_pm) 6 0 6
247 6 rx->flags = ssk_pm->flags;
248  
249 6 tx = protmem_init(aTHX_ crypto_kx_SESSIONKEYBYTES, key_flags);
250 6 ▶ if (tx == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tx == NULL) { 0 6 6
251 0 protmem_free(aTHX_ rx);
252 0 croak("kx_server_session_keys: Failed to allocate tx protmem");
253 }
254 6 ▶ if (ssk_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (ssk_pm) 6 0 6
255 6 tx->flags = ssk_pm->flags;
256  
257 6 ▶ if (ssk_pm && protmem_grant(aTHX_ ssk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ssk_pm && protmem_grant(aTHX_ ssk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 6 0 6
if (ssk_pm && protmem_grant(aTHX_ ssk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
258 0 protmem_free(aTHX_ rx);
259 0 protmem_free(aTHX_ tx);
260 0 croak("kx_server_session_keys: Failed to grant ssk protmem RO");
261 }
262  
263 6 ret = crypto_kx_server_session_keys(rx->pm_ptr, tx->pm_ptr,
264 spk_buf, ssk_buf, cpk_buf);
265  
266 6 ▶ if (ssk_pm && protmem_release(aTHX_ ssk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ssk_pm && protmem_release(aTHX_ ssk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 6 0 6
if (ssk_pm && protmem_release(aTHX_ ssk_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 6 6
267 0 protmem_free(aTHX_ rx);
268 0 protmem_free(aTHX_ tx);
269 0 croak("kx_server_session_keys: Failed to release ssk protmem RO");
270 }
271  
272 6 ▶ if (protmem_release(aTHX_ rx, PROTMEM_FLAG_MPROTECT_RW) != 0
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ rx, PROTMEM_FLAG_MPROTECT_RW) != 0 6 0 6
273 6 ▶ || protmem_release(aTHX_ tx, PROTMEM_FLAG_MPROTECT_RW) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
|| protmem_release(aTHX_ tx, PROTMEM_FLAG_MPROTECT_RW) != 0) { 0 6 6
274 0 protmem_free(aTHX_ rx);
275 0 protmem_free(aTHX_ tx);
276 0 croak("kx_server_session_keys: Failed to release rx/tx protmem RW");
277 }
278  
279 6 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 6 6
280 0 protmem_free(aTHX_ rx);
281 0 protmem_free(aTHX_ tx);
282 0 croak("kx_server_session_keys: Failed to generate session keys (invalid client pubkey?)");
283 }
284  
285 6 ▶ mXPUSHs(protmem_to_sv(aTHX_ rx, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ rx, MEMVAULT_CLASS)); 0 6 6
286 6 ▶ mXPUSHs(protmem_to_sv(aTHX_ tx, MEMVAULT_CLASS));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(protmem_to_sv(aTHX_ tx, MEMVAULT_CLASS)); 0 6 6
287 6 XSRETURN(2);
« xs/stream.xs ↑ Summary xs/onetimeauth.xs »