| 1 |
|
| =for documentation |
| 2 |
|
| |
| 3 |
|
| similar to the situation with kx, libsodium does not have separate secretbox |
| 4 |
|
| functions for xsalsa20poly1305 _easy, _detached, _open_easy, or _open_detached. |
| 5 |
|
| it is the default algorithm for them. aliases are still provided, and just fall |
| 6 |
|
| through to the defaults. |
| 7 |
|
| |
| 8 |
|
| =cut |
| 9 |
|
| |
| 10 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::secretbox |
| 11 |
|
| |
| 12 |
|
| void _define_constants() |
| 13 |
|
| PREINIT: |
| 14 |
3 |
| HV *stash = gv_stashpv("Crypt::Sodium::XS::secretbox", 0); |
| 15 |
|
| |
| 16 |
|
| PPCODE: |
| 17 |
3 |
| newCONSTSUB(stash, "secretbox_KEYBYTES", newSVuv(crypto_secretbox_KEYBYTES)); |
| 18 |
3 |
| newCONSTSUB(stash, "secretbox_xchacha20poly1305_KEYBYTES", |
| 19 |
|
| newSVuv(crypto_secretbox_xchacha20poly1305_KEYBYTES)); |
| 20 |
3 |
| newCONSTSUB(stash, "secretbox_xsalsa20poly1305_KEYBYTES", |
| 21 |
|
| newSVuv(crypto_secretbox_xsalsa20poly1305_KEYBYTES)); |
| 22 |
3 |
| newCONSTSUB(stash, "secretbox_MACBYTES", newSVuv(crypto_secretbox_MACBYTES)); |
| 23 |
3 |
| newCONSTSUB(stash, "secretbox_xchacha20poly1305_MACBYTES", |
| 24 |
|
| newSVuv(crypto_secretbox_xchacha20poly1305_MACBYTES)); |
| 25 |
3 |
| newCONSTSUB(stash, "secretbox_xsalsa20poly1305_MACBYTES", |
| 26 |
|
| newSVuv(crypto_secretbox_xsalsa20poly1305_MACBYTES)); |
| 27 |
3 |
| newCONSTSUB(stash, "secretbox_NONCEBYTES", |
| 28 |
|
| newSVuv(crypto_secretbox_NONCEBYTES)); |
| 29 |
3 |
| newCONSTSUB(stash, "secretbox_xchacha20poly1305_NONCEBYTES", |
| 30 |
|
| newSVuv(crypto_secretbox_xchacha20poly1305_NONCEBYTES)); |
| 31 |
3 |
| newCONSTSUB(stash, "secretbox_xsalsa20poly1305_NONCEBYTES", |
| 32 |
|
| newSVuv(crypto_secretbox_xsalsa20poly1305_NONCEBYTES)); |
| 33 |
3 |
| newCONSTSUB(stash, "secretbox_PRIMITIVE", newSVpvs(crypto_secretbox_PRIMITIVE)); |
| 34 |
|
| |
| 35 |
|
| SV * secretbox_decrypt( \ |
| 36 |
|
| SV * ciphertext, \ |
| 37 |
|
| SV * nonce, \ |
| 38 |
|
| SV * key, \ |
| 39 |
|
| SV * flags = &PL_sv_undef \ |
| 40 |
|
| ) |
| 41 |
|
| |
| 42 |
|
| ALIAS: |
| 43 |
|
| secretbox_xchacha20poly1305_decrypt = 1 |
| 44 |
|
| secretbox_xsalsa20poly1305_decrypt = 2 |
| 45 |
|
| |
| 46 |
|
| PREINIT: |
| 47 |
30 |
| protmem *msg_pm, *key_pm = NULL; |
| 48 |
|
| unsigned char *ct_buf, *nonce_buf, *key_buf; |
| 49 |
|
| STRLEN ct_len, nonce_len, key_len, nonce_req_len, key_req_len, mac_len; |
| 50 |
30 |
▶ | unsigned int msg_flags = g_protmem_default_flags_decrypt; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int msg_flags = g_protmem_default_flags_decrypt; |
30 |
0 |
30 |
|
| 51 |
|
| int ret; |
| 52 |
|
| int (*func)(unsigned char *, const unsigned char *, |
| 53 |
|
| unsigned long long, const unsigned char *, |
| 54 |
|
| const unsigned char *); |
| 55 |
|
| |
| 56 |
|
| CODE: |
| 57 |
30 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
6 |
24 |
30 |
|
| 58 |
6 |
| case 1: |
| 59 |
6 |
| nonce_req_len = crypto_secretbox_xchacha20poly1305_NONCEBYTES; |
| 60 |
6 |
| key_req_len = crypto_secretbox_xchacha20poly1305_KEYBYTES; |
| 61 |
6 |
| mac_len = crypto_secretbox_xchacha20poly1305_MACBYTES; |
| 62 |
6 |
| func = crypto_secretbox_xchacha20poly1305_open_easy; |
| 63 |
6 |
| break; |
| 64 |
24 |
| case 2: /* fallthrough */ |
| 65 |
|
| default: |
| 66 |
24 |
| nonce_req_len = crypto_secretbox_NONCEBYTES; |
| 67 |
24 |
| key_req_len = crypto_secretbox_KEYBYTES; |
| 68 |
24 |
| mac_len = crypto_secretbox_MACBYTES; |
| 69 |
24 |
| func = crypto_secretbox_open_easy; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
30 |
| SvGETMAGIC(flags); |
| 73 |
30 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
30 |
30 |
|
| 74 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 75 |
|
| |
| 76 |
30 |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
| 77 |
30 |
▶ | if (ct_len < mac_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_len < mac_len) |
0 |
30 |
30 |
|
| 78 |
0 |
| croak("secretbox_decrypt: Invalid ciphertext length (too short)"); |
| 79 |
|
| |
| 80 |
30 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 81 |
30 |
▶ | if (nonce_len != nonce_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != nonce_req_len) |
0 |
30 |
30 |
|
| 82 |
0 |
| croak("secretbox_decrypt: Invalid nonce length %lu", nonce_len); |
| 83 |
|
| |
| 84 |
30 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
30 |
0 |
30 |
|
| 85 |
30 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 86 |
30 |
| key_buf = key_pm->pm_ptr; |
| 87 |
30 |
| key_len = key_pm->size; |
| 88 |
|
| } |
| 89 |
|
| else |
| 90 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 91 |
30 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
30 |
30 |
|
| 92 |
0 |
| croak("secretbox_decrypt: Invalid key length %lu", key_len); |
| 93 |
|
| |
| 94 |
30 |
| msg_pm = protmem_init(aTHX_ ct_len - mac_len, msg_flags); |
| 95 |
30 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
30 |
30 |
|
| 96 |
0 |
| croak("secretbox_decrypt: Failed to allocate protmem"); |
| 97 |
|
| |
| 98 |
30 |
▶ | if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
30 |
0 |
30 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
30 |
30 |
|
| 99 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 100 |
0 |
| croak("secretbox_decrypt: Failed to grant key protmem RO"); |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
30 |
| ret = func(msg_pm->pm_ptr, ct_buf, ct_len, nonce_buf, key_buf); |
| 104 |
|
| |
| 105 |
30 |
▶ | if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
30 |
0 |
30 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
30 |
30 |
|
| 106 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 107 |
0 |
| croak("secretbox_decrypt: Failed to release key protmem RO"); |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
30 |
▶ | if (protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
30 |
30 |
|
| 111 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 112 |
0 |
| croak("secretbox_decrypt: Failed to release msg protmem RW"); |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
30 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
18 |
12 |
30 |
|
| 116 |
18 |
| RETVAL = protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS); |
| 117 |
|
| else { |
| 118 |
12 |
| protmem_free(aTHX_ msg_pm); |
| 119 |
12 |
| croak("secretbox_decrypt: Message forged"); |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| OUTPUT: |
| 123 |
|
| RETVAL |
| 124 |
|
| |
| 125 |
|
| SV * secretbox_decrypt_detached( \ |
| 126 |
|
| SV * ciphertext, \ |
| 127 |
|
| SV * mac, \ |
| 128 |
|
| SV * nonce, \ |
| 129 |
|
| SV * key, \ |
| 130 |
|
| SV * flags = &PL_sv_undef \ |
| 131 |
|
| ) |
| 132 |
|
| |
| 133 |
|
| ALIAS: |
| 134 |
|
| secretbox_xchacha20poly1305_decrypt_detached = 1 |
| 135 |
|
| secretbox_xsalsa20poly1305_decrypt_detached = 2 |
| 136 |
|
| |
| 137 |
|
| PREINIT: |
| 138 |
24 |
| protmem *msg_pm, *key_pm = NULL; |
| 139 |
|
| unsigned char *ct_buf, *mac_buf, *nonce_buf, *key_buf; |
| 140 |
|
| STRLEN ct_len, mac_len, nonce_len, key_len; |
| 141 |
|
| STRLEN mac_req_len, nonce_req_len, key_req_len; |
| 142 |
24 |
▶ | unsigned int msg_flags = g_protmem_default_flags_decrypt; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int msg_flags = g_protmem_default_flags_decrypt; |
24 |
0 |
24 |
|
| 143 |
|
| int ret; |
| 144 |
|
| int (*func)(unsigned char *, const unsigned char *, |
| 145 |
|
| const unsigned char *, unsigned long long, |
| 146 |
|
| const unsigned char *, const unsigned char *); |
| 147 |
|
| |
| 148 |
|
| CODE: |
| 149 |
24 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
8 |
16 |
24 |
|
| 150 |
8 |
| case 1: |
| 151 |
8 |
| nonce_req_len = crypto_secretbox_xchacha20poly1305_NONCEBYTES; |
| 152 |
8 |
| key_req_len = crypto_secretbox_xchacha20poly1305_KEYBYTES; |
| 153 |
8 |
| mac_req_len = crypto_secretbox_xchacha20poly1305_MACBYTES; |
| 154 |
8 |
| func = crypto_secretbox_xchacha20poly1305_open_detached; |
| 155 |
8 |
| break; |
| 156 |
16 |
| case 2: /* fallthrough */ |
| 157 |
|
| default: |
| 158 |
16 |
| nonce_req_len = crypto_secretbox_NONCEBYTES; |
| 159 |
16 |
| key_req_len = crypto_secretbox_KEYBYTES; |
| 160 |
16 |
| mac_req_len = crypto_secretbox_MACBYTES; |
| 161 |
16 |
| func = crypto_secretbox_open_detached; |
| 162 |
|
| } |
| 163 |
|
| |
| 164 |
24 |
| SvGETMAGIC(flags); |
| 165 |
24 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
24 |
24 |
|
| 166 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 167 |
|
| |
| 168 |
24 |
▶ | ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
Branch: 1 branch
| Branch | True | False | Total |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
24 |
0 |
24 |
|
| 169 |
|
| |
| 170 |
24 |
| mac_buf = (unsigned char *)SvPVbyte(mac, mac_len); |
| 171 |
24 |
▶ | if (mac_len != mac_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mac_len != mac_req_len) |
0 |
24 |
24 |
|
| 172 |
0 |
| croak("secretbox_decrypt_detached: Invalid mac length: %lu", mac_len); |
| 173 |
|
| |
| 174 |
24 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 175 |
24 |
▶ | if (nonce_len != nonce_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != nonce_req_len) |
0 |
24 |
24 |
|
| 176 |
0 |
| croak("secretbox_decrypt_detached: Invalid nonce length: %lu", nonce_len); |
| 177 |
|
| |
| 178 |
24 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
24 |
0 |
24 |
|
| 179 |
24 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 180 |
24 |
| key_buf = key_pm->pm_ptr; |
| 181 |
24 |
| key_len = key_pm->size; |
| 182 |
|
| } |
| 183 |
|
| else |
| 184 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 185 |
24 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
24 |
24 |
|
| 186 |
0 |
| croak("secretbox_decrypt_detached: Invalid key length: %lu", key_len); |
| 187 |
|
| |
| 188 |
24 |
| msg_pm = protmem_init(aTHX_ ct_len, msg_flags); |
| 189 |
24 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
24 |
24 |
|
| 190 |
0 |
| croak("secretbox_decrypt_detached: Failed to allocate protmem"); |
| 191 |
|
| |
| 192 |
24 |
▶ | if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
24 |
0 |
24 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
24 |
24 |
|
| 193 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 194 |
0 |
| croak("secretbox_decrypt_detached: Failed to grant key protmem RO"); |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
24 |
| ret = func(msg_pm->pm_ptr, ct_buf, mac_buf, ct_len, nonce_buf, key_buf); |
| 198 |
|
| |
| 199 |
24 |
▶ | if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
24 |
0 |
24 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
24 |
24 |
|
| 200 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 201 |
0 |
| croak("secretbox_decrypt_detached: Failed to release key protmem RO"); |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
24 |
▶ | if (protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
24 |
24 |
|
| 205 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 206 |
0 |
| croak("secretbox_decrypt_detached: Failed to release msg protmem RO"); |
| 207 |
|
| } |
| 208 |
|
| |
| 209 |
24 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
6 |
18 |
24 |
|
| 210 |
6 |
| RETVAL = protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS); |
| 211 |
|
| else { |
| 212 |
18 |
| protmem_free(aTHX_ msg_pm); |
| 213 |
18 |
| croak("secretbox_decrypt_detached: Message forged"); |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
|
| OUTPUT: |
| 217 |
|
| RETVAL |
| 218 |
|
| |
| 219 |
|
| void secretbox_encrypt(SV * msg, SV * nonce, SV * key) |
| 220 |
|
| |
| 221 |
|
| ALIAS: |
| 222 |
|
| secretbox_encrypt_detached = 1 |
| 223 |
|
| secretbox_xchacha20poly1305_encrypt = 2 |
| 224 |
|
| secretbox_xchacha20poly1305_encrypt_detached = 3 |
| 225 |
|
| secretbox_xsalsa20poly1305_encrypt = 4 |
| 226 |
|
| secretbox_xsalsa20poly1305_encrypt_detached = 5 |
| 227 |
|
| |
| 228 |
|
| PREINIT: |
| 229 |
30 |
| protmem *key_pm = NULL, *msg_mv = NULL; |
| 230 |
30 |
▶ | SV *ct, *mac = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV *ct, *mac = NULL; |
6 |
24 |
30 |
|
| 231 |
|
| unsigned char *msg_buf, *nonce_buf, *key_buf, *ct_buf, *mac_buf; |
| 232 |
|
| STRLEN msg_len, nonce_len, key_len, nonce_req_len, key_req_len, mac_len; |
| 233 |
|
| int (*detached_func)(unsigned char *, unsigned char *, |
| 234 |
|
| const unsigned char *, unsigned long long, |
| 235 |
|
| const unsigned char *, const unsigned char *); |
| 236 |
|
| int (*easy_func)(unsigned char *, const unsigned char *, |
| 237 |
|
| unsigned long long, const unsigned char *, |
| 238 |
|
| const unsigned char *); |
| 239 |
|
| |
| 240 |
|
| PPCODE: |
| 241 |
30 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
6 |
24 |
30 |
|
| 242 |
6 |
| case 2: |
| 243 |
|
| case 3: /* fallthrough */ |
| 244 |
6 |
| nonce_req_len = crypto_secretbox_xchacha20poly1305_NONCEBYTES; |
| 245 |
6 |
| key_req_len = crypto_secretbox_xchacha20poly1305_KEYBYTES; |
| 246 |
6 |
| mac_len = crypto_secretbox_xchacha20poly1305_MACBYTES; |
| 247 |
6 |
| detached_func = crypto_secretbox_xchacha20poly1305_detached; |
| 248 |
6 |
| easy_func = crypto_secretbox_xchacha20poly1305_easy; |
| 249 |
6 |
| break; |
| 250 |
24 |
| case 4: /* fallthrough */ |
| 251 |
|
| case 5: /* fallthrough */ |
| 252 |
|
| case 1: /* fallthrough */ |
| 253 |
|
| default: |
| 254 |
24 |
| nonce_req_len = crypto_secretbox_NONCEBYTES; |
| 255 |
24 |
| key_req_len = crypto_secretbox_KEYBYTES; |
| 256 |
24 |
| mac_len = crypto_secretbox_MACBYTES; |
| 257 |
24 |
| detached_func = crypto_secretbox_detached; |
| 258 |
24 |
| easy_func = crypto_secretbox_easy; |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
30 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 262 |
30 |
▶ | if (nonce_len != nonce_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != nonce_req_len) |
0 |
30 |
30 |
|
| 263 |
0 |
| croak("secretbox_encrypt: Invalid nonce length %lu", nonce_len); |
| 264 |
|
| |
| 265 |
30 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
30 |
0 |
30 |
|
| 266 |
30 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 267 |
30 |
| key_buf = key_pm->pm_ptr; |
| 268 |
30 |
| key_len = key_pm->size; |
| 269 |
|
| } |
| 270 |
|
| else |
| 271 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 272 |
30 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
30 |
30 |
|
| 273 |
0 |
| croak("secretbox_encrypt: Invalid key length %lu", key_len); |
| 274 |
|
| |
| 275 |
30 |
▶ | if (sv_derived_from(msg, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(msg, MEMVAULT_CLASS)) { |
0 |
30 |
30 |
|
| 276 |
0 |
| msg_mv = protmem_get(aTHX_ msg, MEMVAULT_CLASS); |
| 277 |
0 |
| msg_buf = msg_mv->pm_ptr; |
| 278 |
0 |
| msg_len = msg_mv->size; |
| 279 |
|
| } |
| 280 |
|
| else |
| 281 |
30 |
| msg_buf = (unsigned char *)SvPVbyte(msg, msg_len); |
| 282 |
|
| |
| 283 |
30 |
▶ | if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
30 |
0 |
30 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
30 |
30 |
|
| 284 |
0 |
| croak("secretbox_encrypt: Failed to grant key protmem RO"); |
| 285 |
|
| |
| 286 |
30 |
▶ | if (msg_mv && protmem_grant(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (msg_mv && protmem_grant(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
30 |
30 |
| if (msg_mv && protmem_grant(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 287 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 288 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 289 |
0 |
| croak("secretbox_encrypt: Failed to grant msg protmem RO"); |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
|
| /* NB: odd aliases are _detached, even are combined */ |
| 293 |
30 |
▶ | if (ix & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix & 1) { |
6 |
24 |
30 |
|
| 294 |
|
| /* detached mode */ |
| 295 |
6 |
| Newx(mac_buf, mac_len + 1, unsigned char); |
| 296 |
6 |
▶ | if (mac_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mac_buf == NULL) { |
0 |
6 |
6 |
|
| 297 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 298 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 299 |
0 |
▶ | if (msg_mv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_mv) |
0 |
0 |
0 |
|
| 300 |
0 |
| protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO); |
| 301 |
0 |
| croak("secretbox_encrypt: Failed to allocate memory"); |
| 302 |
|
| } |
| 303 |
6 |
| mac_buf[mac_len] = '\0'; |
| 304 |
6 |
| Newx(ct_buf, msg_len + 1, unsigned char); |
| 305 |
6 |
▶ | if (ct_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) { |
0 |
6 |
6 |
|
| 306 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 307 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 308 |
0 |
▶ | if (msg_mv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_mv) |
0 |
0 |
0 |
|
| 309 |
0 |
| protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO); |
| 310 |
0 |
| Safefree(mac_buf); |
| 311 |
0 |
| croak("secretbox_encrypt: Failed to allocate memory"); |
| 312 |
|
| } |
| 313 |
6 |
| ct_buf[msg_len] = '\0'; |
| 314 |
|
| |
| 315 |
6 |
| detached_func(ct_buf, mac_buf, msg_buf, msg_len, nonce_buf, key_buf); |
| 316 |
|
| |
| 317 |
6 |
| mac = newSV(0); |
| 318 |
6 |
| sv_usepvn_flags(mac, (char *)mac_buf, mac_len, SV_HAS_TRAILING_NUL); |
| 319 |
6 |
| ct = newSV(0); |
| 320 |
6 |
| sv_usepvn_flags(ct, (char *)ct_buf, msg_len, SV_HAS_TRAILING_NUL); |
| 321 |
|
| } |
| 322 |
|
| else { |
| 323 |
|
| /* combined mode */ |
| 324 |
24 |
| STRLEN ct_len = mac_len + msg_len; |
| 325 |
|
| |
| 326 |
24 |
| Newx(ct_buf, ct_len + 1, unsigned char); |
| 327 |
24 |
▶ | if (ct_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) { |
0 |
24 |
24 |
|
| 328 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 329 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 330 |
0 |
▶ | if (msg_mv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_mv) |
0 |
0 |
0 |
|
| 331 |
0 |
| protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO); |
| 332 |
0 |
| croak("secretbox_encrypt: Failed to allocate memory"); |
| 333 |
|
| } |
| 334 |
24 |
| ct_buf[ct_len] = '\0'; |
| 335 |
|
| |
| 336 |
24 |
| easy_func(ct_buf, msg_buf, msg_len, nonce_buf, key_buf); |
| 337 |
|
| |
| 338 |
24 |
| ct = newSV(0); |
| 339 |
24 |
| sv_usepvn_flags(ct, (char *)ct_buf, ct_len, SV_HAS_TRAILING_NUL); |
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
30 |
▶ | if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
30 |
0 |
30 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
30 |
30 |
|
| 343 |
0 |
▶ | if (msg_mv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_mv) |
0 |
0 |
0 |
|
| 344 |
0 |
| protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO); |
| 345 |
0 |
| croak("secretbox_encrypt: Failed to release key protmem RO"); |
| 346 |
|
| } |
| 347 |
30 |
▶ | if (msg_mv && protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (msg_mv && protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
30 |
30 |
| if (msg_mv && protmem_release(aTHX_ msg_mv, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
0 |
0 |
|
| 348 |
0 |
| croak("secretbox_encrypt: Failed to release msg protmem RO"); |
| 349 |
|
| |
| 350 |
30 |
▶ | mXPUSHs(ct); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(ct); |
0 |
30 |
30 |
|
| 351 |
|
| |
| 352 |
30 |
▶ | if (ix & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix & 1) { |
6 |
24 |
30 |
|
| 353 |
6 |
▶ | mXPUSHs(mac); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(mac); |
0 |
6 |
6 |
|
| 354 |
6 |
| XSRETURN(2); |
| 355 |
|
| } |
| 356 |
|
| |
| 357 |
24 |
| XSRETURN(1); |
| 358 |
|
| |
| 359 |
|
| SV * secretbox_keygen(SV * flags = &PL_sv_undef) |
| 360 |
|
| |
| 361 |
|
| ALIAS: |
| 362 |
|
| secretbox_xchacha20poly1305_keygen = 1 |
| 363 |
|
| secretbox_xsalsa20poly1305_keygen = 2 |
| 364 |
|
| |
| 365 |
|
| CODE: |
| 366 |
12 |
| switch(ix) { |
| 367 |
4 |
| case 1: |
| 368 |
4 |
| RETVAL = sv_keygen(aTHX_ crypto_secretbox_xchacha20poly1305_KEYBYTES, flags); |
| 369 |
4 |
| break; |
| 370 |
4 |
| case 2: |
| 371 |
4 |
| RETVAL = sv_keygen(aTHX_ crypto_secretbox_xsalsa20poly1305_KEYBYTES, flags); |
| 372 |
4 |
| break; |
| 373 |
4 |
| default: |
| 374 |
4 |
| RETVAL = sv_keygen(aTHX_ crypto_secretbox_KEYBYTES, flags); |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
|
| OUTPUT: |
| 378 |
|
| RETVAL |
| 379 |
|
| |
| 380 |
|
| SV * secretbox_nonce(SV * base = &PL_sv_undef) |
| 381 |
|
| |
| 382 |
|
| ALIAS: |
| 383 |
|
| secretbox_xchacha20poly1305_nonce = 1 |
| 384 |
|
| secretbox_xsalsa20poly1305_nonce = 2 |
| 385 |
|
| |
| 386 |
|
| CODE: |
| 387 |
12 |
| switch(ix) { |
| 388 |
2 |
| case 1: |
| 389 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_secretbox_xchacha20poly1305_NONCEBYTES, base); |
| 390 |
2 |
| break; |
| 391 |
2 |
| case 2: |
| 392 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_secretbox_xsalsa20poly1305_NONCEBYTES, base); |
| 393 |
2 |
| break; |
| 394 |
8 |
| default: |
| 395 |
8 |
| RETVAL = nonce_generate(aTHX_ crypto_secretbox_NONCEBYTES, base); |
| 396 |
|
| } |
| 397 |
|
| |
| 398 |
|
| OUTPUT: |
| 399 |
|
| RETVAL |