| 1 |
|
| =for documentation |
| 2 |
|
| |
| 3 |
|
| libsodium secretstream includes only xchacha20poly1305-specific functions. |
| 4 |
|
| |
| 5 |
|
| =cut |
| 6 |
|
| |
| 7 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::secretstream |
| 8 |
|
| |
| 9 |
|
| void _define_constants() |
| 10 |
|
| PREINIT: |
| 11 |
2 |
| HV *stash = gv_stashpv("Crypt::Sodium::XS::secretstream", 0); |
| 12 |
|
| |
| 13 |
|
| PPCODE: |
| 14 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_ABYTES", |
| 15 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_ABYTES)); |
| 16 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_HEADERBYTES", |
| 17 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_HEADERBYTES)); |
| 18 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_KEYBYTES", |
| 19 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_KEYBYTES)); |
| 20 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_MESSAGEBYTES_MAX", |
| 21 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX)); |
| 22 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_TAG_MESSAGE", |
| 23 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_TAG_MESSAGE)); |
| 24 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_TAG_PUSH", |
| 25 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_TAG_PUSH)); |
| 26 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_TAG_REKEY", |
| 27 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_TAG_REKEY)); |
| 28 |
2 |
| newCONSTSUB(stash, "secretstream_xchacha20poly1305_TAG_FINAL", |
| 29 |
|
| newSVuv(crypto_secretstream_xchacha20poly1305_TAG_FINAL)); |
| 30 |
|
| |
| 31 |
|
| SV * secretstream_xchacha20poly1305_init_decrypt( \ |
| 32 |
|
| SV * header, \ |
| 33 |
|
| SV * key, \ |
| 34 |
|
| SV * flags = &PL_sv_undef \ |
| 35 |
|
| ) |
| 36 |
|
| |
| 37 |
|
| ALIAS: |
| 38 |
|
| secretstream_xchacha20poly1305_init_pull = 1 |
| 39 |
|
| |
| 40 |
|
| PREINIT: |
| 41 |
|
| PERL_UNUSED_VAR(ix); |
| 42 |
8 |
| protmem *state_pm, *key_pm = NULL; |
| 43 |
|
| unsigned char *header_buf, *key_buf; |
| 44 |
|
| STRLEN header_len, key_len; |
| 45 |
8 |
▶ | unsigned int state_flags = g_protmem_default_flags_key; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int state_flags = g_protmem_default_flags_key; |
8 |
0 |
8 |
|
| 46 |
|
| |
| 47 |
|
| CODE: |
| 48 |
8 |
| SvGETMAGIC(flags); |
| 49 |
8 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
8 |
8 |
|
| 50 |
0 |
| state_flags = SvUV_nomg(flags); |
| 51 |
|
| |
| 52 |
8 |
| header_buf = (unsigned char *)SvPVbyte(header, header_len); |
| 53 |
8 |
▶ | if (header_len != crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (header_len != crypto_secretstream_xchacha20poly1305_HEADERBYTES) |
0 |
8 |
8 |
|
| 54 |
0 |
| croak("secretstream_init_decrypt: Invalid header length %lu", header_len); |
| 55 |
|
| |
| 56 |
8 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
8 |
0 |
8 |
|
| 57 |
8 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 58 |
8 |
| key_buf = key_pm->pm_ptr; |
| 59 |
8 |
| key_len = key_pm->size; |
| 60 |
|
| } |
| 61 |
|
| else |
| 62 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 63 |
8 |
▶ | if (key_len != crypto_secretstream_xchacha20poly1305_KEYBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != crypto_secretstream_xchacha20poly1305_KEYBYTES) |
0 |
8 |
8 |
|
| 64 |
0 |
| croak("secretstream_init_decrypt: Invalid key length %lu", key_len); |
| 65 |
|
| |
| 66 |
8 |
| state_pm = protmem_init(aTHX_ sizeof(crypto_secretstream_xchacha20poly1305_state), state_flags); |
| 67 |
8 |
▶ | if (state_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (state_pm == NULL) |
0 |
8 |
8 |
|
| 68 |
0 |
| croak("secretstream_init_decrypt: Failed to allocate state protmem"); |
| 69 |
|
| |
| 70 |
8 |
▶ | 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) { |
8 |
0 |
8 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
8 |
8 |
|
| 71 |
0 |
| protmem_free(aTHX_ state_pm); |
| 72 |
0 |
| croak("secretstream_init_decrypt: Failed to grant key protmem RO"); |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
8 |
| crypto_secretstream_xchacha20poly1305_init_pull(state_pm->pm_ptr, header_buf, key_buf); |
| 76 |
|
| |
| 77 |
8 |
▶ | 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) { |
8 |
0 |
8 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
8 |
8 |
|
| 78 |
0 |
| protmem_free(aTHX_ state_pm); |
| 79 |
0 |
| croak("secretstream_init_decrypt: Failed to release key protmem RO"); |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
8 |
▶ | if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
8 |
8 |
|
| 83 |
0 |
| protmem_free(aTHX_ state_pm); |
| 84 |
0 |
| croak("secretstream_init_decrypt: Failed to release state protmem RW"); |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
8 |
| RETVAL = protmem_to_sv(aTHX_ state_pm, |
| 88 |
|
| "Crypt::Sodium::XS::secretstream::xchacha20poly1305_decrypt"); |
| 89 |
|
| |
| 90 |
|
| OUTPUT: |
| 91 |
|
| RETVAL |
| 92 |
|
| |
| 93 |
|
| void secretstream_xchacha20poly1305_init_encrypt(SV * key, SV * flags = &PL_sv_undef) |
| 94 |
|
| |
| 95 |
|
| ALIAS: |
| 96 |
|
| secretstream_xchacha20poly1305_init_push = 1 |
| 97 |
|
| |
| 98 |
|
| PREINIT: |
| 99 |
|
| PERL_UNUSED_VAR(ix); |
| 100 |
4 |
| protmem *state_pm, *key_pm = NULL; |
| 101 |
|
| SV * header; |
| 102 |
|
| unsigned char *key_buf, *header_buf; |
| 103 |
|
| STRLEN key_len; |
| 104 |
4 |
▶ | unsigned int state_flags = g_protmem_default_flags_key; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int state_flags = g_protmem_default_flags_key; |
4 |
0 |
4 |
|
| 105 |
|
| |
| 106 |
|
| PPCODE: |
| 107 |
4 |
| SvGETMAGIC(flags); |
| 108 |
4 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
4 |
4 |
|
| 109 |
0 |
| state_flags = SvUV_nomg(flags); |
| 110 |
|
| |
| 111 |
4 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
4 |
0 |
4 |
|
| 112 |
4 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 113 |
4 |
| key_buf = key_pm->pm_ptr; |
| 114 |
4 |
| key_len = key_pm->size; |
| 115 |
|
| } |
| 116 |
|
| else |
| 117 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 118 |
4 |
▶ | if (key_len != crypto_secretstream_xchacha20poly1305_KEYBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != crypto_secretstream_xchacha20poly1305_KEYBYTES) |
0 |
4 |
4 |
|
| 119 |
0 |
| croak("secretstream_init_encrypt: Invalid key length %lu", key_len); |
| 120 |
|
| |
| 121 |
4 |
| state_pm = protmem_init(aTHX_ sizeof(crypto_secretstream_xchacha20poly1305_state), state_flags); |
| 122 |
4 |
▶ | if (state_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (state_pm == NULL) |
0 |
4 |
4 |
|
| 123 |
0 |
| croak("secretstream_init_encrypt: Failed to allocate state protmem"); |
| 124 |
|
| |
| 125 |
4 |
| Newx(header_buf, |
| 126 |
|
| crypto_secretstream_xchacha20poly1305_HEADERBYTES + 1, unsigned char); |
| 127 |
4 |
▶ | if (header_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header_buf == NULL) { |
0 |
4 |
4 |
|
| 128 |
0 |
| protmem_free(aTHX_ state_pm); |
| 129 |
0 |
| croak("secretstream_init_encrypt: Failed to allocate memory"); |
| 130 |
|
| } |
| 131 |
4 |
| header_buf[crypto_secretstream_xchacha20poly1305_HEADERBYTES] = '\0'; |
| 132 |
|
| |
| 133 |
4 |
▶ | 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) { |
4 |
0 |
4 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
4 |
4 |
|
| 134 |
0 |
| protmem_free(aTHX_ state_pm); |
| 135 |
0 |
| Safefree(header_buf); |
| 136 |
0 |
| croak("secretstream_init_encrypt: Failed to grant key protmem RO"); |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
4 |
| crypto_secretstream_xchacha20poly1305_init_push(state_pm->pm_ptr, header_buf, key_buf); |
| 140 |
|
| |
| 141 |
4 |
▶ | 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) { |
4 |
0 |
4 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
4 |
4 |
|
| 142 |
0 |
| protmem_free(aTHX_ state_pm); |
| 143 |
0 |
| croak("secretstream_init_encrypt: Failed to release key protmem RO"); |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
4 |
▶ | if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
4 |
4 |
|
| 147 |
0 |
| protmem_free(aTHX_ state_pm); |
| 148 |
0 |
| Safefree(header_buf); |
| 149 |
0 |
| croak("secretstream_init_encrypt: Failed to release state protmem RW"); |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
4 |
| header = newSV(0); |
| 153 |
4 |
| sv_usepvn_flags(header, (char *)header_buf, |
| 154 |
|
| crypto_secretstream_xchacha20poly1305_HEADERBYTES, |
| 155 |
|
| SV_HAS_TRAILING_NUL); |
| 156 |
4 |
▶ | mXPUSHs(header); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(header); |
0 |
4 |
4 |
|
| 157 |
4 |
▶ | mXPUSHs(protmem_to_sv(aTHX_ state_pm, "Crypt::Sodium::XS::secretstream::xchacha20poly1305_encrypt")); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(protmem_to_sv(aTHX_ state_pm, "Crypt::Sodium::XS::secretstream::xchacha20poly1305_encrypt")); |
0 |
4 |
4 |
|
| 158 |
|
| |
| 159 |
4 |
| XSRETURN(2); |
| 160 |
|
| |
| 161 |
|
| SV * secretstream_xchacha20poly1305_keygen(SV * flags = &PL_sv_undef) |
| 162 |
|
| |
| 163 |
|
| CODE: |
| 164 |
4 |
| RETVAL = sv_keygen(aTHX_ crypto_secretstream_xchacha20poly1305_KEYBYTES, flags); |
| 165 |
|
| |
| 166 |
|
| OUTPUT: |
| 167 |
|
| RETVAL |
| 168 |
|
| |
| 169 |
|
| |
| 170 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::secretstream::xchacha20poly1305_decrypt |
| 171 |
|
| |
| 172 |
|
| void DESTROY(SV * self) |
| 173 |
|
| |
| 174 |
|
| ALIAS: |
| 175 |
|
| Crypt::Sodium::XS::secretstream::xchachapoly1305_encrypt = 1 |
| 176 |
|
| |
| 177 |
|
| PREINIT: |
| 178 |
|
| protmem *state_pm; |
| 179 |
|
| |
| 180 |
|
| PPCODE: |
| 181 |
8 |
▶ | switch(ix) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch(ix) { |
0 |
8 |
8 |
|
| 182 |
0 |
| case 1: |
| 183 |
0 |
| state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::secretstream::xchacha20poly1305_encrypt"); |
| 184 |
0 |
| break; |
| 185 |
8 |
| default: |
| 186 |
8 |
| state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::secretstream::xchacha20poly1305_decrypt"); |
| 187 |
|
| } |
| 188 |
8 |
| protmem_free(aTHX_ state_pm); |
| 189 |
|
| |
| 190 |
|
| void decrypt( \ |
| 191 |
|
| SV * self, \ |
| 192 |
|
| SV * ciphertext, \ |
| 193 |
|
| SV * adata = &PL_sv_undef, \ |
| 194 |
|
| SV * flags = &PL_sv_undef \ |
| 195 |
|
| ) |
| 196 |
|
| |
| 197 |
|
| ALIAS: |
| 198 |
|
| pull = 1 |
| 199 |
|
| |
| 200 |
|
| PREINIT: |
| 201 |
|
| PERL_UNUSED_VAR(ix); |
| 202 |
26 |
| protmem *state_pm, *ct_pm = NULL, *msg_pm; |
| 203 |
26 |
| unsigned char *ct_buf, *adata_buf = NULL, tag; |
| 204 |
26 |
| STRLEN ct_len, adata_len = 0; |
| 205 |
26 |
▶ | 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; |
20 |
6 |
26 |
|
| 206 |
|
| int ret; |
| 207 |
|
| |
| 208 |
|
| PPCODE: |
| 209 |
26 |
| SvGETMAGIC(flags); |
| 210 |
26 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
26 |
26 |
|
| 211 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 212 |
|
| |
| 213 |
26 |
▶ | if (sv_derived_from(ciphertext, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(ciphertext, MEMVAULT_CLASS)) { |
0 |
26 |
26 |
|
| 214 |
0 |
| ct_pm = protmem_get(aTHX_ ciphertext, MEMVAULT_CLASS); |
| 215 |
0 |
| ct_buf = ct_pm->pm_ptr; |
| 216 |
0 |
| ct_len = ct_pm->size; |
| 217 |
|
| } |
| 218 |
|
| else |
| 219 |
26 |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
| 220 |
26 |
▶ | if (ct_len < crypto_secretstream_xchacha20poly1305_ABYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_len < crypto_secretstream_xchacha20poly1305_ABYTES) |
0 |
26 |
26 |
|
| 221 |
0 |
| croak("decrypt: Invalid ciphertext (too short): %lu", ct_len); |
| 222 |
|
| |
| 223 |
26 |
| SvGETMAGIC(adata); |
| 224 |
26 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
6 |
20 |
26 |
|
| 225 |
6 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 226 |
|
| |
| 227 |
26 |
| msg_pm = protmem_init(aTHX_ |
| 228 |
|
| ct_len - crypto_secretstream_xchacha20poly1305_ABYTES, msg_flags); |
| 229 |
26 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
26 |
26 |
|
| 230 |
0 |
| croak("decrypt: Failed to allocate protmem"); |
| 231 |
|
| |
| 232 |
26 |
▶ | if (ct_pm && protmem_grant(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ct_pm && protmem_grant(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
26 |
26 |
| if (ct_pm && protmem_grant(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 233 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 234 |
0 |
| croak("decrypt: Failed to grant ciphertext protmem RO"); |
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
26 |
| state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::secretstream::xchacha20poly1305_decrypt"); |
| 238 |
26 |
▶ | if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
26 |
26 |
|
| 239 |
0 |
▶ | if (ct_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_pm) |
0 |
0 |
0 |
|
| 240 |
0 |
| protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 241 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 242 |
0 |
| croak("decrypt: Failed to grant state protmem RW"); |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
26 |
| ret = crypto_secretstream_xchacha20poly1305_pull(state_pm->pm_ptr, |
| 246 |
26 |
| msg_pm->pm_ptr, NULL, &tag, ct_buf, ct_len, adata_buf, adata_len); |
| 247 |
|
| |
| 248 |
26 |
▶ | if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
26 |
26 |
|
| 249 |
0 |
▶ | if (ct_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_pm) |
0 |
0 |
0 |
|
| 250 |
0 |
| protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 251 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 252 |
0 |
| croak("decrypt: Failed to release state protmem RW"); |
| 253 |
|
| } |
| 254 |
|
| |
| 255 |
26 |
▶ | if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
26 |
26 |
| if (ct_pm && protmem_release(aTHX_ ct_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 256 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 257 |
0 |
| croak("decrypt: Failed to release ciphertext protmem RO"); |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
26 |
▶ | 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 |
26 |
26 |
|
| 261 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 262 |
0 |
| croak("Failed to protect memvault"); |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
26 |
▶ | if (ret != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret != 0) { |
14 |
12 |
26 |
|
| 266 |
14 |
| protmem_free(aTHX_ msg_pm); |
| 267 |
14 |
| croak("decrypt: Message forged"); |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
12 |
▶ | mXPUSHs(protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS)); |
0 |
12 |
12 |
|
| 271 |
12 |
▶ | if (GIMME_V == G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_ARRAY) { |
2 |
10 |
12 |
|
| 272 |
2 |
▶ | mXPUSHs(newSViv(tag)); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(newSViv(tag)); |
0 |
2 |
2 |
|
| 273 |
2 |
| XSRETURN(2); |
| 274 |
|
| } |
| 275 |
10 |
| XSRETURN(1); |
| 276 |
|
| |
| 277 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::secretstream::xchacha20poly1305_encrypt |
| 278 |
|
| |
| 279 |
|
| SV * encrypt( \ |
| 280 |
|
| SV * self, \ |
| 281 |
|
| SV * msg, \ |
| 282 |
|
| unsigned char tag = crypto_secretstream_xchacha20poly1305_TAG_MESSAGE, \ |
| 283 |
|
| SV * adata = &PL_sv_undef \ |
| 284 |
|
| ) |
| 285 |
|
| |
| 286 |
|
| ALIAS: |
| 287 |
|
| push = 1 |
| 288 |
|
| |
| 289 |
|
| PREINIT: |
| 290 |
|
| PERL_UNUSED_VAR(ix); |
| 291 |
12 |
| protmem *state_pm, *msg_pm = NULL; |
| 292 |
12 |
| unsigned char *msg_buf, *adata_buf = NULL, *ct_buf; |
| 293 |
12 |
▶ | STRLEN msg_len, ct_len, adata_len = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN msg_len, ct_len, adata_len = 0; |
6 |
6 |
12 |
|
| 294 |
|
| |
| 295 |
|
| CODE: |
| 296 |
12 |
▶ | if (sv_derived_from(msg, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(msg, MEMVAULT_CLASS)) { |
0 |
12 |
12 |
|
| 297 |
0 |
| msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS); |
| 298 |
0 |
| msg_buf = msg_pm->pm_ptr; |
| 299 |
0 |
| msg_len = msg_pm->size; |
| 300 |
|
| } |
| 301 |
|
| else |
| 302 |
12 |
| msg_buf = (unsigned char *)SvPVbyte(msg, msg_len); |
| 303 |
12 |
| ct_len = msg_len + crypto_secretstream_xchacha20poly1305_ABYTES; |
| 304 |
|
| |
| 305 |
12 |
| SvGETMAGIC(adata); |
| 306 |
12 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
4 |
8 |
12 |
|
| 307 |
4 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 308 |
|
| |
| 309 |
12 |
| Newx(ct_buf, ct_len + 1, unsigned char); |
| 310 |
12 |
▶ | if (ct_buf == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) |
0 |
12 |
12 |
|
| 311 |
0 |
| croak("encrypt: Failed to allocate memory"); |
| 312 |
12 |
| ct_buf[ct_len] = '\0'; |
| 313 |
|
| |
| 314 |
12 |
▶ | if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
12 |
12 |
| if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 315 |
0 |
| Safefree(ct_buf); |
| 316 |
0 |
| croak("encrypt: Failed to grant msg protmem RO"); |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
12 |
| state_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::secretstream::xchacha20poly1305_encrypt"); |
| 320 |
12 |
▶ | if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_grant(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
12 |
12 |
|
| 321 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 322 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 323 |
0 |
| Safefree(ct_buf); |
| 324 |
0 |
| croak("encrypt: Failed to grant state protmem RW"); |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
12 |
| crypto_secretstream_xchacha20poly1305_push(state_pm->pm_ptr, ct_buf, NULL, |
| 328 |
|
| msg_buf, msg_len, |
| 329 |
|
| adata_buf, adata_len, tag); |
| 330 |
|
| |
| 331 |
12 |
▶ | if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ state_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
12 |
12 |
|
| 332 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 333 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 334 |
0 |
| Safefree(ct_buf); |
| 335 |
0 |
| croak("encrypt: Failed to release state protmem RW"); |
| 336 |
|
| } |
| 337 |
|
| |
| 338 |
12 |
▶ | if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
12 |
12 |
| if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
0 |
0 |
|
| 339 |
0 |
| Safefree(ct_buf); |
| 340 |
0 |
| croak("encrypt: Failed to release msg protmem RO"); |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
12 |
| RETVAL = newSV(0); |
| 344 |
12 |
| sv_usepvn_flags(RETVAL, (char *)ct_buf, ct_len, SV_HAS_TRAILING_NUL); |
| 345 |
|
| |
| 346 |
|
| OUTPUT: |
| 347 |
|
| RETVAL |