| 1 |
|
| =for documentation |
| 2 |
|
| |
| 3 |
|
| libsodium aead provides only algorithm-specific functions. |
| 4 |
|
| |
| 5 |
|
| =cut |
| 6 |
|
| |
| 7 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::aead |
| 8 |
|
| |
| 9 |
|
| void |
| 10 |
|
| _define_constants() |
| 11 |
|
| PREINIT: |
| 12 |
1 |
| HV *stash = gv_stashpv("Crypt::Sodium::XS::aead", 0); |
| 13 |
|
| |
| 14 |
|
| PPCODE: |
| 15 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_ABYTES", |
| 16 |
|
| newSVuv(crypto_aead_chacha20poly1305_ABYTES)); |
| 17 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_ietf_ABYTES", |
| 18 |
|
| newSVuv(crypto_aead_chacha20poly1305_ietf_ABYTES)); |
| 19 |
1 |
| newCONSTSUB(stash, "aead_aes256gcm_ABYTES", |
| 20 |
|
| newSVuv(crypto_aead_aes256gcm_ABYTES)); |
| 21 |
1 |
| newCONSTSUB(stash, "aead_xchacha20poly1305_ietf_ABYTES", |
| 22 |
|
| newSVuv(crypto_aead_xchacha20poly1305_ietf_ABYTES)); |
| 23 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_KEYBYTES", |
| 24 |
|
| newSVuv(crypto_aead_chacha20poly1305_KEYBYTES)); |
| 25 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_ietf_KEYBYTES", |
| 26 |
|
| newSVuv(crypto_aead_chacha20poly1305_ietf_KEYBYTES)); |
| 27 |
1 |
| newCONSTSUB(stash, "aead_aes256gcm_KEYBYTES", |
| 28 |
|
| newSVuv(crypto_aead_aes256gcm_KEYBYTES)); |
| 29 |
1 |
| newCONSTSUB(stash, "aead_xchacha20poly1305_ietf_KEYBYTES", |
| 30 |
|
| newSVuv(crypto_aead_xchacha20poly1305_ietf_KEYBYTES)); |
| 31 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_MESSAGEBYTES_MAX", |
| 32 |
|
| newSVuv(crypto_aead_chacha20poly1305_KEYBYTES)); |
| 33 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX", |
| 34 |
|
| newSVuv(crypto_aead_chacha20poly1305_ietf_MESSAGEBYTES_MAX)); |
| 35 |
1 |
| newCONSTSUB(stash, "aead_aes256gcm_MESSAGEBYTES_MAX", |
| 36 |
|
| newSVuv(crypto_aead_aes256gcm_MESSAGEBYTES_MAX)); |
| 37 |
1 |
| newCONSTSUB(stash, "aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX", |
| 38 |
|
| newSVuv(crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX)); |
| 39 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_NPUBBYTES", |
| 40 |
|
| newSVuv(crypto_aead_chacha20poly1305_NPUBBYTES)); |
| 41 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_ietf_NPUBBYTES", |
| 42 |
|
| newSVuv(crypto_aead_chacha20poly1305_ietf_NPUBBYTES)); |
| 43 |
1 |
| newCONSTSUB(stash, "aead_aes256gcm_NPUBBYTES", |
| 44 |
|
| newSVuv(crypto_aead_aes256gcm_NPUBBYTES)); |
| 45 |
1 |
| newCONSTSUB(stash, "aead_xchacha20poly1305_ietf_NPUBBYTES", |
| 46 |
|
| newSVuv(crypto_aead_xchacha20poly1305_ietf_NPUBBYTES)); |
| 47 |
|
| #ifdef SODIUM_HAS_AEGIS |
| 48 |
1 |
| newCONSTSUB(stash, "aead_aegis128l_ABYTES", |
| 49 |
|
| newSVuv(crypto_aead_aegis128l_ABYTES)); |
| 50 |
1 |
| newCONSTSUB(stash, "aead_aegis256_ABYTES", |
| 51 |
|
| newSVuv(crypto_aead_aegis256_ABYTES)); |
| 52 |
1 |
| newCONSTSUB(stash, "aead_aegis128l_KEYBYTES", |
| 53 |
|
| newSVuv(crypto_aead_aegis128l_KEYBYTES)); |
| 54 |
1 |
| newCONSTSUB(stash, "aead_aegis256_KEYBYTES", |
| 55 |
|
| newSVuv(crypto_aead_aegis256_KEYBYTES)); |
| 56 |
1 |
| newCONSTSUB(stash, "aead_aegis128l_MESSAGEBYTES_MAX", |
| 57 |
|
| newSVuv(crypto_aead_aegis128l_MESSAGEBYTES_MAX)); |
| 58 |
1 |
| newCONSTSUB(stash, "aead_aegis256_MESSAGEBYTES_MAX", |
| 59 |
|
| newSVuv(crypto_aead_aegis256_MESSAGEBYTES_MAX)); |
| 60 |
1 |
| newCONSTSUB(stash, "aead_aegis128l_NPUBBYTES", |
| 61 |
|
| newSVuv(crypto_aead_aegis128l_NPUBBYTES)); |
| 62 |
1 |
| newCONSTSUB(stash, "aead_aegis256_NPUBBYTES", |
| 63 |
|
| newSVuv(crypto_aead_aegis256_NPUBBYTES)); |
| 64 |
|
| #endif |
| 65 |
1 |
| =for notes |
| 66 |
1 |
| nsec is not used by libsodium aead algorithms. provided for completeness. |
| 67 |
1 |
| =cut |
| 68 |
|
| newCONSTSUB(stash, "aead_chacha20poly1305_NSECBYTES", newSVuv(0)); |
| 69 |
1 |
| newCONSTSUB(stash, "aead_chacha20poly1305_ietf_NSECBYTES", newSVuv(0)); |
| 70 |
1 |
| newCONSTSUB(stash, "aead_aes256gcm_NSECBYTES", newSVuv(0)); |
| 71 |
|
| #ifdef SODIUM_HAS_AEGIS |
| 72 |
1 |
| newCONSTSUB(stash, "aead_aegis128l_NSECBYTES", newSVuv(0)); |
| 73 |
1 |
▶ | newCONSTSUB(stash, "aead_aegis256_NSECBYTES", newSVuv(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| newCONSTSUB(stash, "aead_aegis256_NSECBYTES", newSVuv(0)); |
1 |
0 |
1 |
|
| 74 |
|
| #endif |
| 75 |
|
| newCONSTSUB(stash, "aead_xchacha20poly1305_NSECBYTES", newSVuv(0)); |
| 76 |
1 |
| newCONSTSUB(stash, "aead_aes256gcm_available", |
| 77 |
|
| has_aes256gcm ? &PL_sv_yes : &PL_sv_no); |
| 78 |
|
| #ifdef SODIUM_HAS_AEGIS |
| 79 |
|
| newCONSTSUB(stash, "aead_aegis_available", &PL_sv_yes); |
| 80 |
|
| #else |
| 81 |
|
| newCONSTSUB(stash, "aead_aegis_available", &PL_sv_no); |
| 82 |
|
| #endif |
| 83 |
|
| |
| 84 |
|
| SV * aead_aes256gcm_beforenm(SV * key, SV * flags = &PL_sv_undef) |
| 85 |
|
| |
| 86 |
|
| PREINIT: |
| 87 |
2 |
| protmem *precalc_pm, *key_pm = NULL; |
| 88 |
|
| unsigned char *key_buf; |
| 89 |
|
| STRLEN key_len; |
| 90 |
2 |
▶ | unsigned int precalc_pm_flags = g_protmem_default_flags_key; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned int precalc_pm_flags = g_protmem_default_flags_key; |
2 |
0 |
2 |
|
| 91 |
|
| |
| 92 |
|
| CODE: |
| 93 |
2 |
▶ | if (!has_aes256gcm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_aes256gcm) |
0 |
2 |
2 |
|
| 94 |
0 |
| croak("aead_beforenm: AES256GCM is not supported on this cpu"); |
| 95 |
|
| |
| 96 |
2 |
| SvGETMAGIC(flags); |
| 97 |
2 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
2 |
2 |
|
| 98 |
0 |
| precalc_pm_flags = SvUV_nomg(flags); |
| 99 |
|
| |
| 100 |
2 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
2 |
0 |
2 |
|
| 101 |
2 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 102 |
2 |
| key_buf = key_pm->pm_ptr; |
| 103 |
2 |
| key_len = key_pm->size; |
| 104 |
|
| } |
| 105 |
|
| else |
| 106 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 107 |
2 |
▶ | if (key_len != crypto_aead_aes256gcm_KEYBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != crypto_aead_aes256gcm_KEYBYTES) |
0 |
2 |
2 |
|
| 108 |
0 |
| croak("aead_beforenm: Invalid key length %lu", key_len); |
| 109 |
|
| |
| 110 |
2 |
| precalc_pm = protmem_init(aTHX_ sizeof(crypto_aead_aes256gcm_state), precalc_pm_flags); |
| 111 |
2 |
▶ | if (precalc_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (precalc_pm == NULL) |
0 |
2 |
2 |
|
| 112 |
0 |
| croak("aead_beforenm: Failed to allocate protmem"); |
| 113 |
|
| |
| 114 |
2 |
▶ | 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) { |
2 |
0 |
2 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 115 |
0 |
| protmem_free(aTHX_ precalc_pm); |
| 116 |
0 |
| croak("aead_beforenm: Failed to grant key protmem RO"); |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
2 |
| crypto_aead_aes256gcm_beforenm(precalc_pm->pm_ptr, key_buf); |
| 120 |
|
| |
| 121 |
2 |
▶ | 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) { |
2 |
0 |
2 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
2 |
2 |
|
| 122 |
0 |
| protmem_free(aTHX_ precalc_pm); |
| 123 |
0 |
| croak("aead_beforenm: Failed to release key protmem RO"); |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
2 |
▶ | if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
2 |
2 |
|
| 127 |
0 |
| protmem_free(aTHX_ precalc_pm); |
| 128 |
0 |
| croak("aead_beforenm: Failed to release protmem RW"); |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
2 |
| RETVAL = protmem_to_sv(aTHX_ precalc_pm, "Crypt::Sodium::XS::aead::precalc"); |
| 132 |
|
| |
| 133 |
|
| OUTPUT: |
| 134 |
|
| RETVAL |
| 135 |
|
| |
| 136 |
|
| SV * aead_chacha20poly1305_decrypt( \ |
| 137 |
|
| SV * ciphertext, \ |
| 138 |
|
| SV * nonce, \ |
| 139 |
|
| SV * key, \ |
| 140 |
|
| SV * adata = &PL_sv_undef, \ |
| 141 |
|
| SV * flags = &PL_sv_undef \ |
| 142 |
|
| ) |
| 143 |
|
| |
| 144 |
|
| ALIAS: |
| 145 |
|
| aead_chacha20poly1305_ietf_decrypt = 1 |
| 146 |
|
| aead_aes256gcm_decrypt = 2 |
| 147 |
|
| aead_xchacha20poly1305_ietf_decrypt = 3 |
| 148 |
|
| aead_aegis128l_decrypt = 4 |
| 149 |
|
| aead_aegis256_decrypt = 5 |
| 150 |
|
| |
| 151 |
|
| PREINIT: |
| 152 |
72 |
| protmem *key_pm = NULL, *msg_pm; |
| 153 |
72 |
| unsigned char *ct_buf, *adata_buf = NULL, *nonce_buf, *key_buf; |
| 154 |
72 |
| STRLEN ct_len, adata_len = 0, nonce_len, key_len; |
| 155 |
|
| STRLEN adata_req_len, nonce_req_len, key_req_len; |
| 156 |
72 |
▶ | 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; |
0 |
72 |
72 |
|
| 157 |
|
| int ret; |
| 158 |
|
| int (*func)(unsigned char *, unsigned long long *, unsigned char *, |
| 159 |
|
| const unsigned char *, unsigned long long, const unsigned char *, |
| 160 |
|
| unsigned long long, const unsigned char *, const unsigned char *); |
| 161 |
|
| |
| 162 |
|
| CODE: |
| 163 |
72 |
| switch(ix) { |
| 164 |
12 |
| case 1: |
| 165 |
12 |
| adata_req_len = crypto_aead_chacha20poly1305_ietf_ABYTES; |
| 166 |
12 |
| nonce_req_len = crypto_aead_chacha20poly1305_ietf_NPUBBYTES; |
| 167 |
12 |
| key_req_len = crypto_aead_chacha20poly1305_ietf_KEYBYTES; |
| 168 |
12 |
| func = crypto_aead_chacha20poly1305_ietf_decrypt; |
| 169 |
12 |
| break; |
| 170 |
12 |
| case 2: |
| 171 |
12 |
▶ | if (!has_aes256gcm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_aes256gcm) |
0 |
12 |
12 |
|
| 172 |
0 |
| croak("aead_decrypt: AES256GCM is not supported by this CPU"); |
| 173 |
12 |
| adata_req_len = crypto_aead_aes256gcm_ABYTES; |
| 174 |
12 |
| nonce_req_len = crypto_aead_aes256gcm_NPUBBYTES; |
| 175 |
12 |
| key_req_len = crypto_aead_aes256gcm_KEYBYTES; |
| 176 |
12 |
| func = crypto_aead_aes256gcm_decrypt; |
| 177 |
12 |
| break; |
| 178 |
12 |
| case 3: |
| 179 |
12 |
| adata_req_len = crypto_aead_xchacha20poly1305_ietf_ABYTES; |
| 180 |
12 |
| nonce_req_len = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES; |
| 181 |
12 |
| key_req_len = crypto_aead_xchacha20poly1305_ietf_KEYBYTES; |
| 182 |
12 |
| func = crypto_aead_xchacha20poly1305_ietf_decrypt; |
| 183 |
12 |
| break; |
| 184 |
12 |
| case 4: |
| 185 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 186 |
|
| croak("aead_decrypt: AEGIS not supported by this version of libsodium"); |
| 187 |
|
| #else |
| 188 |
12 |
| adata_req_len = crypto_aead_aegis128l_ABYTES; |
| 189 |
12 |
| nonce_req_len = crypto_aead_aegis128l_NPUBBYTES; |
| 190 |
12 |
| key_req_len = crypto_aead_aegis128l_KEYBYTES; |
| 191 |
12 |
| func = crypto_aead_aegis128l_decrypt; |
| 192 |
|
| #endif |
| 193 |
12 |
| break; |
| 194 |
12 |
| case 5: |
| 195 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 196 |
|
| croak("aead_decrypt: AEGIS not supported by this version of libsodium"); |
| 197 |
|
| #else |
| 198 |
12 |
| adata_req_len = crypto_aead_aegis256_ABYTES; |
| 199 |
12 |
| nonce_req_len = crypto_aead_aegis256_NPUBBYTES; |
| 200 |
12 |
| key_req_len = crypto_aead_aegis256_KEYBYTES; |
| 201 |
12 |
| func = crypto_aead_aegis256_decrypt; |
| 202 |
|
| #endif |
| 203 |
12 |
| break; |
| 204 |
12 |
| default: |
| 205 |
12 |
| adata_req_len = crypto_aead_chacha20poly1305_ABYTES; |
| 206 |
12 |
| nonce_req_len = crypto_aead_chacha20poly1305_NPUBBYTES; |
| 207 |
12 |
| key_req_len = crypto_aead_chacha20poly1305_KEYBYTES; |
| 208 |
12 |
| func = crypto_aead_chacha20poly1305_decrypt; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
72 |
| SvGETMAGIC(flags); |
| 212 |
72 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
72 |
72 |
|
| 213 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 214 |
|
| |
| 215 |
72 |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
| 216 |
72 |
▶ | if (ct_len < adata_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_len < adata_req_len) |
0 |
72 |
72 |
|
| 217 |
0 |
| croak("aead_decrypt: Invalid ciphertext length (too short)"); |
| 218 |
|
| |
| 219 |
72 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 220 |
72 |
▶ | if (nonce_len != nonce_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != nonce_req_len) |
0 |
72 |
72 |
|
| 221 |
0 |
| croak("aead_decrypt: Invalid nonce length %lu", nonce_len); |
| 222 |
|
| |
| 223 |
72 |
| SvGETMAGIC(adata); |
| 224 |
72 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
72 |
0 |
72 |
|
| 225 |
72 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 226 |
|
| |
| 227 |
72 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
60 |
12 |
72 |
|
| 228 |
60 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 229 |
60 |
| key_buf = key_pm->pm_ptr; |
| 230 |
60 |
| key_len = key_pm->size; |
| 231 |
|
| } |
| 232 |
|
| else |
| 233 |
12 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 234 |
72 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
72 |
72 |
|
| 235 |
0 |
| croak("aead_decrypt: Invalid key length %lu:", key_len); |
| 236 |
|
| |
| 237 |
72 |
| msg_pm = protmem_init(aTHX_ ct_len - adata_req_len, msg_flags); |
| 238 |
72 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
72 |
72 |
|
| 239 |
0 |
| croak("aead_decrypt: Failed to allocate protmem"); |
| 240 |
|
| |
| 241 |
72 |
▶ | 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) { |
60 |
12 |
72 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
60 |
60 |
|
| 242 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 243 |
0 |
| croak("aead_decrypt: Failed to grant key protmem RO"); |
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
72 |
| ret = func(msg_pm->pm_ptr, NULL, NULL, ct_buf, ct_len, |
| 247 |
|
| adata_buf, adata_len, nonce_buf, key_buf); |
| 248 |
|
| |
| 249 |
72 |
▶ | 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) { |
60 |
12 |
72 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
60 |
60 |
|
| 250 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 251 |
0 |
| croak("aead_decrypt: Failed to release key protmem RO"); |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
72 |
▶ | 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 |
72 |
72 |
|
| 255 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 256 |
0 |
| croak("aead_decrypt: Failed to release msg protmem RW"); |
| 257 |
|
| } |
| 258 |
|
| |
| 259 |
72 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
24 |
48 |
72 |
|
| 260 |
24 |
| RETVAL = protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS); |
| 261 |
|
| else { |
| 262 |
48 |
| protmem_free(aTHX_ msg_pm); |
| 263 |
48 |
| croak("aead_decrypt: Message forged"); |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
|
| OUTPUT: |
| 267 |
|
| RETVAL |
| 268 |
|
| |
| 269 |
|
| SV * aead_chacha20poly1305_decrypt_detached( \ |
| 270 |
|
| SV * ciphertext, \ |
| 271 |
|
| SV * mac, \ |
| 272 |
|
| SV * nonce, \ |
| 273 |
|
| SV * key, \ |
| 274 |
|
| SV * adata = &PL_sv_undef, \ |
| 275 |
|
| SV * flags = &PL_sv_undef \ |
| 276 |
|
| ) |
| 277 |
|
| |
| 278 |
|
| ALIAS: |
| 279 |
|
| aead_chacha20poly1305_ietf_decrypt_detached = 1 |
| 280 |
|
| aead_aes256gcm_decrypt_detached = 2 |
| 281 |
|
| aead_xchacha20poly1305_ietf_decrypt_detached = 3 |
| 282 |
|
| aead_aegis128l_decrypt_detached = 4 |
| 283 |
|
| aead_aegis256_decrypt_detached = 5 |
| 284 |
|
| |
| 285 |
|
| PREINIT: |
| 286 |
12 |
| protmem *key_pm = NULL, *msg_pm; |
| 287 |
12 |
| unsigned char *ct_buf, *mac_buf, *adata_buf = NULL, *nonce_buf, *key_buf; |
| 288 |
12 |
| STRLEN ct_len, mac_len, adata_len = 0, nonce_len, key_len; |
| 289 |
|
| STRLEN adata_req_len, nonce_req_len, key_req_len; |
| 290 |
12 |
▶ | 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; |
0 |
12 |
12 |
|
| 291 |
|
| int ret; |
| 292 |
|
| int (*func)(unsigned char *, unsigned char *, const unsigned char *, |
| 293 |
|
| unsigned long long, const unsigned char *, const unsigned char *, |
| 294 |
|
| unsigned long long, const unsigned char *, const unsigned char *); |
| 295 |
|
| |
| 296 |
|
| CODE: |
| 297 |
12 |
| switch(ix) { |
| 298 |
2 |
| case 1: |
| 299 |
2 |
| adata_req_len = crypto_aead_chacha20poly1305_ietf_ABYTES; |
| 300 |
2 |
| nonce_req_len = crypto_aead_chacha20poly1305_ietf_NPUBBYTES; |
| 301 |
2 |
| key_req_len = crypto_aead_chacha20poly1305_ietf_KEYBYTES; |
| 302 |
2 |
| func = crypto_aead_chacha20poly1305_ietf_decrypt_detached; |
| 303 |
2 |
| break; |
| 304 |
2 |
| case 2: |
| 305 |
2 |
| adata_req_len = crypto_aead_aes256gcm_ABYTES; |
| 306 |
2 |
| nonce_req_len = crypto_aead_aes256gcm_NPUBBYTES; |
| 307 |
2 |
| key_req_len = crypto_aead_aes256gcm_KEYBYTES; |
| 308 |
2 |
| func = crypto_aead_aes256gcm_decrypt_detached; |
| 309 |
2 |
| break; |
| 310 |
2 |
| case 3: |
| 311 |
2 |
| adata_req_len = crypto_aead_xchacha20poly1305_ietf_ABYTES; |
| 312 |
2 |
| nonce_req_len = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES; |
| 313 |
2 |
| key_req_len = crypto_aead_xchacha20poly1305_ietf_KEYBYTES; |
| 314 |
2 |
| func = crypto_aead_xchacha20poly1305_ietf_decrypt_detached; |
| 315 |
2 |
| break; |
| 316 |
2 |
| case 4: |
| 317 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 318 |
|
| croak("AEGIS not supported by this version of libsodium"); |
| 319 |
|
| #else |
| 320 |
2 |
| adata_req_len = crypto_aead_aegis128l_ABYTES; |
| 321 |
2 |
| nonce_req_len = crypto_aead_aegis128l_NPUBBYTES; |
| 322 |
2 |
| key_req_len = crypto_aead_aegis128l_KEYBYTES; |
| 323 |
2 |
| func = crypto_aead_aegis128l_decrypt_detached; |
| 324 |
|
| #endif |
| 325 |
2 |
| break; |
| 326 |
2 |
| case 5: |
| 327 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 328 |
|
| croak("AEGIS not supported by this version of libsodium"); |
| 329 |
|
| #else |
| 330 |
2 |
| adata_req_len = crypto_aead_aegis256_ABYTES; |
| 331 |
2 |
| nonce_req_len = crypto_aead_aegis256_NPUBBYTES; |
| 332 |
2 |
| key_req_len = crypto_aead_aegis256_KEYBYTES; |
| 333 |
2 |
| func = crypto_aead_aegis256_decrypt_detached; |
| 334 |
|
| #endif |
| 335 |
2 |
| break; |
| 336 |
2 |
| default: |
| 337 |
2 |
| adata_req_len = crypto_aead_chacha20poly1305_ABYTES; |
| 338 |
2 |
| nonce_req_len = crypto_aead_chacha20poly1305_NPUBBYTES; |
| 339 |
2 |
| key_req_len = crypto_aead_chacha20poly1305_KEYBYTES; |
| 340 |
2 |
| func = crypto_aead_chacha20poly1305_decrypt_detached; |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
12 |
▶ | ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
Branch: 1 branch
| Branch | True | False | Total |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
12 |
0 |
12 |
|
| 344 |
|
| |
| 345 |
12 |
| mac_buf = (unsigned char *)SvPVbyte(mac, mac_len); |
| 346 |
12 |
▶ | if (mac_len != adata_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mac_len != adata_req_len) |
0 |
12 |
12 |
|
| 347 |
0 |
| croak("aead_decrypt_detached: Invalid mac length %lu", mac_len); |
| 348 |
|
| |
| 349 |
12 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 350 |
12 |
▶ | if (nonce_len != nonce_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != nonce_req_len) |
0 |
12 |
12 |
|
| 351 |
0 |
| croak("aead_decrypt_detached: Invalid nonce length %lu", nonce_len); |
| 352 |
|
| |
| 353 |
12 |
| SvGETMAGIC(adata); |
| 354 |
12 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
12 |
0 |
12 |
|
| 355 |
12 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 356 |
|
| |
| 357 |
12 |
| SvGETMAGIC(flags); |
| 358 |
12 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
12 |
12 |
|
| 359 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 360 |
|
| |
| 361 |
12 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
12 |
0 |
12 |
|
| 362 |
12 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 363 |
12 |
| key_buf = key_pm->pm_ptr; |
| 364 |
12 |
| key_len = key_pm->size; |
| 365 |
|
| } |
| 366 |
|
| else |
| 367 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 368 |
12 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
12 |
12 |
|
| 369 |
0 |
| croak("aead_decrypt_detached: Invalid key length %lu", key_len); |
| 370 |
|
| |
| 371 |
12 |
| msg_pm = protmem_init(aTHX_ ct_len, msg_flags); |
| 372 |
12 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
12 |
12 |
|
| 373 |
0 |
| croak("aead_decrypt_detached: Failed to allocate protmem"); |
| 374 |
|
| |
| 375 |
12 |
▶ | 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) { |
12 |
0 |
12 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
12 |
12 |
|
| 376 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 377 |
0 |
| croak("aead_decrypt_detached: Failed to grant key protmem RO"); |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
12 |
| ret = func(msg_pm->pm_ptr, NULL, ct_buf, ct_len, mac_buf, |
| 381 |
|
| adata_buf, adata_len, nonce_buf, key_buf); |
| 382 |
|
| |
| 383 |
12 |
▶ | 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) { |
12 |
0 |
12 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
12 |
12 |
|
| 384 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 385 |
0 |
| croak("aead_decrypt_detached: Failed to release key protmem RO"); |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
12 |
▶ | 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 |
12 |
12 |
|
| 389 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 390 |
0 |
| croak("aead_decrypt_detached: Failed to release msg protmem RW"); |
| 391 |
|
| } |
| 392 |
|
| |
| 393 |
12 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
12 |
0 |
12 |
|
| 394 |
12 |
| RETVAL = protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS); |
| 395 |
|
| else { |
| 396 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 397 |
0 |
| croak("aead_decrypt_detached: Message forged"); |
| 398 |
|
| } |
| 399 |
|
| |
| 400 |
|
| OUTPUT: |
| 401 |
|
| RETVAL |
| 402 |
|
| |
| 403 |
|
| void aead_chacha20poly1305_encrypt( \ |
| 404 |
|
| SV * msg, \ |
| 405 |
|
| SV * nonce, \ |
| 406 |
|
| SV * key, \ |
| 407 |
|
| SV * adata = &PL_sv_undef \ |
| 408 |
|
| ) |
| 409 |
|
| |
| 410 |
|
| ALIAS: |
| 411 |
|
| aead_chacha20poly1305_encrypt_detached = 1 |
| 412 |
|
| aead_chacha20poly1305_ietf_encrypt = 2 |
| 413 |
|
| aead_chacha20poly1305_ietf_encrypt_detached = 3 |
| 414 |
|
| aead_aes256gcm_encrypt = 4 |
| 415 |
|
| aead_aes256gcm_encrypt_detached = 5 |
| 416 |
|
| aead_xchacha20poly1305_ietf_encrypt = 6 |
| 417 |
|
| aead_xchacha20poly1305_ietf_encrypt_detached = 7 |
| 418 |
|
| aead_aegis128l_encrypt = 8 |
| 419 |
|
| aead_aegis128l_encrypt_detached = 9 |
| 420 |
|
| aead_aegis256_encrypt = 10 |
| 421 |
|
| aead_aegis256_encrypt_detached = 11 |
| 422 |
|
| |
| 423 |
|
| PREINIT: |
| 424 |
36 |
| protmem *msg_pm = NULL, *key_pm = NULL; |
| 425 |
36 |
| SV *ct, *adata_out = NULL; |
| 426 |
36 |
| unsigned char *msg_buf, *adata_buf = NULL, *nonce_buf, *key_buf, *ct_buf, *adata_out_buf; |
| 427 |
36 |
▶ | STRLEN msg_len, adata_len = 0, nonce_len, key_len; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN msg_len, adata_len = 0, nonce_len, key_len; |
0 |
36 |
36 |
|
| 428 |
|
| STRLEN adata_req_len, nonce_req_len, key_req_len, out_len; |
| 429 |
|
| int (*comb_func)(unsigned char *, unsigned long long *, const unsigned char *, |
| 430 |
|
| unsigned long long, const unsigned char *, unsigned long long, |
| 431 |
|
| const unsigned char *, const unsigned char *, const unsigned char *); |
| 432 |
|
| int (*detached_func)(unsigned char *, unsigned char *, unsigned long long *, |
| 433 |
|
| const unsigned char *, unsigned long long, const unsigned char *, |
| 434 |
|
| unsigned long long, const unsigned char *, const unsigned char *, |
| 435 |
|
| const unsigned char *); |
| 436 |
|
| |
| 437 |
|
| PPCODE: |
| 438 |
36 |
| switch(ix) { |
| 439 |
6 |
| case 2: /* fallthrough */ |
| 440 |
|
| case 3: |
| 441 |
6 |
| adata_req_len = crypto_aead_chacha20poly1305_ietf_ABYTES; |
| 442 |
6 |
| nonce_req_len = crypto_aead_chacha20poly1305_ietf_NPUBBYTES; |
| 443 |
6 |
| key_req_len = crypto_aead_chacha20poly1305_ietf_KEYBYTES; |
| 444 |
6 |
| comb_func = crypto_aead_chacha20poly1305_ietf_encrypt; |
| 445 |
6 |
| detached_func = crypto_aead_chacha20poly1305_ietf_encrypt_detached; |
| 446 |
6 |
| break; |
| 447 |
6 |
| case 4: /* fallthrough */ |
| 448 |
|
| case 5: |
| 449 |
6 |
▶ | if (!has_aes256gcm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_aes256gcm) |
0 |
6 |
6 |
|
| 450 |
0 |
| croak("aead_encrypt: AES256GCM is not supported by this CPU"); |
| 451 |
6 |
| adata_req_len = crypto_aead_aes256gcm_ABYTES; |
| 452 |
6 |
| nonce_req_len = crypto_aead_aes256gcm_NPUBBYTES; |
| 453 |
6 |
| key_req_len = crypto_aead_aes256gcm_KEYBYTES; |
| 454 |
6 |
| comb_func = crypto_aead_aes256gcm_encrypt; |
| 455 |
6 |
| detached_func = crypto_aead_aes256gcm_encrypt_detached; |
| 456 |
6 |
| break; |
| 457 |
6 |
| case 6: /* fallthrough */ |
| 458 |
|
| case 7: |
| 459 |
6 |
| adata_req_len = crypto_aead_xchacha20poly1305_ietf_ABYTES; |
| 460 |
6 |
| nonce_req_len = crypto_aead_xchacha20poly1305_ietf_NPUBBYTES; |
| 461 |
6 |
| key_req_len = crypto_aead_xchacha20poly1305_ietf_KEYBYTES; |
| 462 |
6 |
| comb_func = crypto_aead_xchacha20poly1305_ietf_encrypt; |
| 463 |
6 |
| detached_func = crypto_aead_xchacha20poly1305_ietf_encrypt_detached; |
| 464 |
6 |
| break; |
| 465 |
6 |
| case 8: /* fallthrough */ |
| 466 |
|
| case 9: |
| 467 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 468 |
|
| croak("aead_encrypt: AEGIS not supported by this version of libsodium"); |
| 469 |
|
| #else |
| 470 |
6 |
| adata_req_len = crypto_aead_aegis128l_ABYTES; |
| 471 |
6 |
| nonce_req_len = crypto_aead_aegis128l_NPUBBYTES; |
| 472 |
6 |
| key_req_len = crypto_aead_aegis128l_KEYBYTES; |
| 473 |
6 |
| comb_func = crypto_aead_aegis128l_encrypt; |
| 474 |
6 |
| detached_func = crypto_aead_aegis128l_encrypt_detached; |
| 475 |
|
| #endif |
| 476 |
6 |
| break; |
| 477 |
6 |
| case 10: /* fallthrough */ |
| 478 |
|
| case 11: |
| 479 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 480 |
|
| croak("aead_encrypt: AEGIS not supported by this version of libsodium"); |
| 481 |
|
| #else |
| 482 |
6 |
| adata_req_len = crypto_aead_aegis256_ABYTES; |
| 483 |
6 |
| nonce_req_len = crypto_aead_aegis256_NPUBBYTES; |
| 484 |
6 |
| key_req_len = crypto_aead_aegis256_KEYBYTES; |
| 485 |
6 |
| comb_func = crypto_aead_aegis256_encrypt; |
| 486 |
6 |
| detached_func = crypto_aead_aegis256_encrypt_detached; |
| 487 |
|
| #endif |
| 488 |
6 |
| break; |
| 489 |
6 |
| case 1: /* fallthrough */ |
| 490 |
|
| default: |
| 491 |
6 |
| adata_req_len = crypto_aead_chacha20poly1305_ABYTES; |
| 492 |
6 |
| nonce_req_len = crypto_aead_chacha20poly1305_NPUBBYTES; |
| 493 |
6 |
| key_req_len = crypto_aead_chacha20poly1305_KEYBYTES; |
| 494 |
6 |
| comb_func = crypto_aead_chacha20poly1305_encrypt; |
| 495 |
6 |
| detached_func = crypto_aead_chacha20poly1305_encrypt_detached; |
| 496 |
|
| } |
| 497 |
|
| |
| 498 |
36 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 499 |
36 |
▶ | if (nonce_len != nonce_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != nonce_req_len) |
0 |
36 |
36 |
|
| 500 |
0 |
| croak("aead_encrypt: Invalid nonce length %lu", nonce_len); |
| 501 |
|
| |
| 502 |
36 |
| SvGETMAGIC(adata); |
| 503 |
36 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
36 |
0 |
36 |
|
| 504 |
36 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 505 |
|
| |
| 506 |
36 |
▶ | if (sv_derived_from(msg, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(msg, MEMVAULT_CLASS)) { |
0 |
36 |
36 |
|
| 507 |
0 |
| msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS); |
| 508 |
0 |
| msg_buf = msg_pm->pm_ptr; |
| 509 |
0 |
| msg_len = msg_pm->size; |
| 510 |
|
| } |
| 511 |
|
| else |
| 512 |
36 |
| msg_buf = (unsigned char *)SvPVbyte(msg, msg_len); |
| 513 |
|
| |
| 514 |
36 |
▶ | if (sv_derived_from(key, MEMVAULT_CLASS)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(key, MEMVAULT_CLASS)) { |
36 |
0 |
36 |
|
| 515 |
36 |
| key_pm = protmem_get(aTHX_ key, MEMVAULT_CLASS); |
| 516 |
36 |
| key_buf = key_pm->pm_ptr; |
| 517 |
36 |
| key_len = key_pm->size; |
| 518 |
|
| } |
| 519 |
|
| else |
| 520 |
0 |
| key_buf = (unsigned char *)SvPVbyte(key, key_len); |
| 521 |
36 |
▶ | if (key_len != key_req_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != key_req_len) |
0 |
36 |
36 |
|
| 522 |
0 |
| croak("aead_encrypt: Invalid key length %lu", key_len); |
| 523 |
|
| |
| 524 |
36 |
▶ | 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 |
36 |
36 |
| if (msg_pm && protmem_grant(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
0 |
0 |
|
| 525 |
0 |
| croak("aead_encrypt: Failed to grant msg protmem RO"); |
| 526 |
|
| |
| 527 |
36 |
▶ | 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) { |
36 |
0 |
36 |
| if (key_pm && protmem_grant(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
36 |
36 |
|
| 528 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 529 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 530 |
0 |
| croak("aead_encrypt: Failed to grant key protmem RO"); |
| 531 |
|
| } |
| 532 |
|
| |
| 533 |
|
| /* NB: odd aliases are _detached, even ones are combined */ |
| 534 |
36 |
▶ | if (ix & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix & 1) { |
12 |
24 |
36 |
|
| 535 |
12 |
| Newx(ct_buf, msg_len + 1, unsigned char); |
| 536 |
12 |
▶ | if (ct_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) { |
0 |
12 |
12 |
|
| 537 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 538 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 539 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 540 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 541 |
0 |
| croak("aead_encrypt: Failed to allocate memory"); |
| 542 |
|
| } |
| 543 |
12 |
| ct_buf[msg_len] = '\0'; |
| 544 |
|
| |
| 545 |
12 |
| Newx(adata_out_buf, adata_req_len + 1, unsigned char); |
| 546 |
12 |
▶ | if (adata_out_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (adata_out_buf == NULL) { |
0 |
12 |
12 |
|
| 547 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 548 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 549 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 550 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 551 |
0 |
| croak("aead_encrypt: Failed to allocate memory"); |
| 552 |
|
| } |
| 553 |
12 |
| adata_out_buf[adata_req_len] = '\0'; |
| 554 |
|
| |
| 555 |
12 |
| detached_func(ct_buf, adata_out_buf, NULL, msg_buf, msg_len, |
| 556 |
|
| adata_buf, adata_len, NULL, nonce_buf, key_buf); |
| 557 |
|
| |
| 558 |
12 |
| ct = newSV(0); |
| 559 |
12 |
| sv_usepvn_flags(ct, (char *)ct_buf, msg_len, SV_HAS_TRAILING_NUL); |
| 560 |
12 |
| adata_out = newSV(0); |
| 561 |
12 |
| sv_usepvn_flags(adata_out, (char *)adata_out_buf, adata_req_len, |
| 562 |
|
| SV_HAS_TRAILING_NUL); |
| 563 |
|
| } |
| 564 |
|
| else { |
| 565 |
24 |
▶ | if (SIZE_MAX - adata_req_len < msg_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SIZE_MAX - adata_req_len < msg_len) |
0 |
24 |
24 |
|
| 566 |
0 |
| croak("Message too large"); |
| 567 |
24 |
| out_len = adata_req_len + msg_len; |
| 568 |
|
| |
| 569 |
24 |
| Newx(ct_buf, out_len + 1, unsigned char); |
| 570 |
24 |
▶ | if (ct_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) { |
0 |
24 |
24 |
|
| 571 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 572 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 573 |
0 |
▶ | if (key_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_pm) |
0 |
0 |
0 |
|
| 574 |
0 |
| protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 575 |
0 |
| croak("aead_encrypt: Failed to allocate memory"); |
| 576 |
|
| } |
| 577 |
24 |
| ct_buf[out_len] = '\0'; |
| 578 |
|
| |
| 579 |
24 |
| comb_func(ct_buf, NULL, msg_buf, msg_len, |
| 580 |
|
| adata_buf, adata_len, NULL, nonce_buf, key_buf); |
| 581 |
|
| |
| 582 |
24 |
| ct = newSV(0); |
| 583 |
24 |
| sv_usepvn_flags(ct, (char *)ct_buf, out_len, SV_HAS_TRAILING_NUL); |
| 584 |
|
| } |
| 585 |
|
| |
| 586 |
36 |
▶ | 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) { |
36 |
0 |
36 |
| if (key_pm && protmem_release(aTHX_ key_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
36 |
36 |
|
| 587 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 588 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 589 |
0 |
| croak("aead_encrypt: Failed to release key protmem RO"); |
| 590 |
|
| } |
| 591 |
|
| |
| 592 |
36 |
▶ | 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 |
36 |
36 |
| if (msg_pm && protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) |
0 |
0 |
0 |
|
| 593 |
0 |
| croak("aead_encrypt: Failed to release msg protmem RO"); |
| 594 |
|
| |
| 595 |
36 |
▶ | mXPUSHs(ct); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(ct); |
0 |
36 |
36 |
|
| 596 |
|
| |
| 597 |
36 |
▶ | if (ix & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix & 1) { |
12 |
24 |
36 |
|
| 598 |
12 |
▶ | mXPUSHs(adata_out); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(adata_out); |
0 |
12 |
12 |
|
| 599 |
12 |
| XSRETURN(2); |
| 600 |
|
| } |
| 601 |
|
| |
| 602 |
24 |
| XSRETURN(1); |
| 603 |
|
| |
| 604 |
|
| SV * aead_chacha20poly1305_keygen(SV * flags = &PL_sv_undef) |
| 605 |
|
| |
| 606 |
|
| ALIAS: |
| 607 |
|
| aead_chacha20poly1305_ietf_keygen = 1 |
| 608 |
|
| aead_aes256gcm_keygen = 2 |
| 609 |
|
| aead_xchacha20poly1305_ietf_keygen = 3 |
| 610 |
|
| aead_aegis128l_keygen = 4 |
| 611 |
|
| aead_aegis256_keygen = 5 |
| 612 |
|
| |
| 613 |
|
| CODE: |
| 614 |
12 |
| switch(ix) { |
| 615 |
2 |
| case 1: |
| 616 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_aead_chacha20poly1305_ietf_KEYBYTES, flags); |
| 617 |
2 |
| break; |
| 618 |
2 |
| case 2: |
| 619 |
2 |
▶ | if (!has_aes256gcm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_aes256gcm) |
0 |
2 |
2 |
|
| 620 |
0 |
| croak("aead_keygen: AES256GCM is not supported by this CPU"); |
| 621 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_aead_aes256gcm_KEYBYTES, flags); |
| 622 |
2 |
| break; |
| 623 |
2 |
| case 3: |
| 624 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_aead_xchacha20poly1305_ietf_KEYBYTES, flags); |
| 625 |
2 |
| break; |
| 626 |
2 |
| case 4: |
| 627 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 628 |
|
| croak("aead_keygen: AEGIS not supported by this version of libsodium"); |
| 629 |
|
| #else |
| 630 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_aead_aegis128l_KEYBYTES, flags); |
| 631 |
|
| #endif |
| 632 |
2 |
| break; |
| 633 |
2 |
| case 5: |
| 634 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 635 |
|
| croak("aead_keygen: AEGIS not supported by this version of libsodium"); |
| 636 |
|
| #else |
| 637 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_aead_aegis256_KEYBYTES, flags); |
| 638 |
|
| #endif |
| 639 |
2 |
| break; |
| 640 |
2 |
| default: |
| 641 |
2 |
| RETVAL = sv_keygen(aTHX_ crypto_aead_chacha20poly1305_KEYBYTES, flags); |
| 642 |
|
| } |
| 643 |
|
| |
| 644 |
|
| OUTPUT: |
| 645 |
|
| RETVAL |
| 646 |
|
| |
| 647 |
|
| SV * aead_chacha20poly1305_nonce(SV * base = &PL_sv_undef) |
| 648 |
|
| |
| 649 |
|
| ALIAS: |
| 650 |
|
| aead_chacha20poly1305_ietf_nonce = 1 |
| 651 |
|
| aead_aes256gcm_nonce = 2 |
| 652 |
|
| aead_xchacha20poly1305_ietf_nonce = 3 |
| 653 |
|
| aead_aegis128l_nonce = 4 |
| 654 |
|
| aead_aegis256_nonce = 5 |
| 655 |
|
| |
| 656 |
|
| CODE: |
| 657 |
12 |
| switch(ix) { |
| 658 |
2 |
| case 1: |
| 659 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_aead_chacha20poly1305_ietf_NPUBBYTES, base); |
| 660 |
2 |
| break; |
| 661 |
2 |
| case 2: |
| 662 |
2 |
▶ | if (!has_aes256gcm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_aes256gcm) |
0 |
2 |
2 |
|
| 663 |
0 |
| croak("aead_nonce: AES256GCM is not supported by this CPU"); |
| 664 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_aead_aes256gcm_NPUBBYTES, base); |
| 665 |
2 |
| break; |
| 666 |
2 |
| case 3: |
| 667 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_aead_xchacha20poly1305_ietf_NPUBBYTES, base); |
| 668 |
2 |
| break; |
| 669 |
2 |
| case 4: |
| 670 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 671 |
|
| croak("aead_nonce: AEGIS not supported by this version of libsodium"); |
| 672 |
|
| #else |
| 673 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_aead_aegis128l_NPUBBYTES, base); |
| 674 |
|
| #endif |
| 675 |
2 |
| break; |
| 676 |
2 |
| case 5: |
| 677 |
|
| #ifndef SODIUM_HAS_AEGIS |
| 678 |
|
| croak("aead_nonce: AEGIS not supported by this version of libsodium"); |
| 679 |
|
| #else |
| 680 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_aead_aegis256_NPUBBYTES, base); |
| 681 |
|
| #endif |
| 682 |
2 |
| break; |
| 683 |
2 |
| default: |
| 684 |
2 |
| RETVAL = nonce_generate(aTHX_ crypto_aead_chacha20poly1305_NPUBBYTES, base); |
| 685 |
|
| } |
| 686 |
|
| |
| 687 |
|
| OUTPUT: |
| 688 |
|
| RETVAL |
| 689 |
|
| |
| 690 |
|
| MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::aead::precalc |
| 691 |
|
| |
| 692 |
|
| void DESTROY(SV * self) |
| 693 |
|
| |
| 694 |
|
| PREINIT: |
| 695 |
|
| protmem *precalc_pm; |
| 696 |
|
| |
| 697 |
|
| PPCODE: |
| 698 |
2 |
| precalc_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::aead::precalc"); |
| 699 |
2 |
| protmem_free(aTHX_ precalc_pm); |
| 700 |
|
| |
| 701 |
|
| SV * decrypt( \ |
| 702 |
|
| SV * self, \ |
| 703 |
|
| SV * ciphertext, \ |
| 704 |
|
| SV * nonce, \ |
| 705 |
|
| SV * adata = &PL_sv_undef, \ |
| 706 |
|
| SV * flags = &PL_sv_undef \ |
| 707 |
|
| ) |
| 708 |
|
| |
| 709 |
|
| PREINIT: |
| 710 |
|
| protmem *precalc_pm, *msg_pm; |
| 711 |
12 |
| unsigned char *ct_buf, *adata_buf = NULL, *nonce_buf; |
| 712 |
12 |
| STRLEN ct_len, adata_len = 0, nonce_len; |
| 713 |
12 |
▶ | 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; |
0 |
12 |
12 |
|
| 714 |
|
| int ret; |
| 715 |
|
| |
| 716 |
|
| CODE: |
| 717 |
12 |
▶ | if (!has_aes256gcm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!has_aes256gcm) |
0 |
12 |
12 |
|
| 718 |
0 |
| croak("decrypt: AES256GCM is not supported by this CPU"); |
| 719 |
|
| |
| 720 |
12 |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
| 721 |
12 |
▶ | if (ct_len < crypto_aead_aes256gcm_ABYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_len < crypto_aead_aes256gcm_ABYTES) |
0 |
12 |
12 |
|
| 722 |
0 |
| croak("decrypt: Invalid ciphertext length (too short)"); |
| 723 |
|
| |
| 724 |
12 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 725 |
12 |
▶ | if (nonce_len != crypto_aead_aes256gcm_NPUBBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != crypto_aead_aes256gcm_NPUBBYTES) |
0 |
12 |
12 |
|
| 726 |
0 |
| croak("decrypt: Invalid nonce length %lu", nonce_len); |
| 727 |
|
| |
| 728 |
12 |
| SvGETMAGIC(adata); |
| 729 |
12 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
12 |
0 |
12 |
|
| 730 |
12 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 731 |
|
| |
| 732 |
12 |
| SvGETMAGIC(flags); |
| 733 |
12 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
12 |
12 |
|
| 734 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 735 |
|
| |
| 736 |
12 |
| msg_pm = protmem_init(aTHX_ ct_len - crypto_aead_aes256gcm_ABYTES, msg_flags); |
| 737 |
12 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
12 |
12 |
|
| 738 |
0 |
| croak("decrypt: Failed to allocate protmem"); |
| 739 |
|
| |
| 740 |
12 |
| precalc_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::aead::precalc"); |
| 741 |
12 |
▶ | if (protmem_grant(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_grant(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
12 |
12 |
|
| 742 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 743 |
0 |
| croak("decrypt: Failed to grant protmem RO"); |
| 744 |
|
| } |
| 745 |
|
| |
| 746 |
12 |
| ret = crypto_aead_aes256gcm_decrypt_afternm(msg_pm->pm_ptr, NULL, NULL, |
| 747 |
|
| ct_buf, ct_len, adata_buf, adata_len, nonce_buf, |
| 748 |
12 |
| (crypto_aead_aes256gcm_state *)precalc_pm->pm_ptr); |
| 749 |
|
| |
| 750 |
12 |
▶ | if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
12 |
12 |
|
| 751 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 752 |
0 |
| croak("decrypt: Failed to release procmem RO"); |
| 753 |
|
| } |
| 754 |
|
| |
| 755 |
12 |
▶ | 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 |
12 |
12 |
|
| 756 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 757 |
0 |
| croak("decrypt: Failed to release msg protmem RW"); |
| 758 |
|
| } |
| 759 |
|
| |
| 760 |
12 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
6 |
6 |
12 |
|
| 761 |
6 |
| RETVAL = protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS); |
| 762 |
|
| else { |
| 763 |
6 |
| protmem_free(aTHX_ msg_pm); |
| 764 |
6 |
| croak("decrypt: Message forged"); |
| 765 |
|
| } |
| 766 |
|
| |
| 767 |
|
| OUTPUT: |
| 768 |
|
| RETVAL |
| 769 |
|
| |
| 770 |
|
| SV * decrypt_detached( \ |
| 771 |
|
| SV * self, \ |
| 772 |
|
| SV * ciphertext, \ |
| 773 |
|
| SV * mac, \ |
| 774 |
|
| SV * nonce, \ |
| 775 |
|
| SV * adata = &PL_sv_undef, \ |
| 776 |
|
| SV * flags = &PL_sv_undef \ |
| 777 |
|
| ) |
| 778 |
|
| |
| 779 |
|
| PREINIT: |
| 780 |
|
| protmem *precalc_pm, *msg_pm; |
| 781 |
6 |
| unsigned char *ct_buf, *mac_buf, *adata_buf = NULL, *nonce_buf; |
| 782 |
6 |
| STRLEN ct_len, mac_len, adata_len = 0, nonce_len; |
| 783 |
6 |
▶ | 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; |
0 |
6 |
6 |
|
| 784 |
|
| int ret; |
| 785 |
|
| |
| 786 |
|
| CODE: |
| 787 |
6 |
▶ | ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
Branch: 1 branch
| Branch | True | False | Total |
| ct_buf = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
6 |
0 |
6 |
|
| 788 |
|
| |
| 789 |
6 |
| mac_buf = (unsigned char *)SvPVbyte(mac, mac_len); |
| 790 |
6 |
▶ | if (mac_len != crypto_aead_aes256gcm_ABYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mac_len != crypto_aead_aes256gcm_ABYTES) |
0 |
6 |
6 |
|
| 791 |
0 |
| croak("decrypt_detached: Invalid mac length %lu", mac_len); |
| 792 |
|
| |
| 793 |
6 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 794 |
6 |
▶ | if (nonce_len != crypto_aead_aes256gcm_NPUBBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != crypto_aead_aes256gcm_NPUBBYTES) |
0 |
6 |
6 |
|
| 795 |
0 |
| croak("decrypt_detached: Invalid nonce length %lu", nonce_len); |
| 796 |
|
| |
| 797 |
6 |
| SvGETMAGIC(adata); |
| 798 |
6 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
6 |
0 |
6 |
|
| 799 |
6 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 800 |
|
| |
| 801 |
6 |
| SvGETMAGIC(flags); |
| 802 |
6 |
▶ | if (SvOK(flags)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(flags)) |
0 |
6 |
6 |
|
| 803 |
0 |
| msg_flags = SvUV_nomg(flags); |
| 804 |
|
| |
| 805 |
6 |
| msg_pm = protmem_init(aTHX_ ct_len, msg_flags); |
| 806 |
6 |
▶ | if (msg_pm == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm == NULL) |
0 |
6 |
6 |
|
| 807 |
0 |
| croak("decrypt_detached: Failed to allocate protmem"); |
| 808 |
|
| |
| 809 |
6 |
| precalc_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::aead::precalc"); |
| 810 |
6 |
▶ | if (protmem_grant(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_grant(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
6 |
6 |
|
| 811 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 812 |
0 |
| croak("decrypt_detached: Failed to grant protmem RO"); |
| 813 |
|
| } |
| 814 |
|
| |
| 815 |
6 |
| ret = crypto_aead_aes256gcm_decrypt_detached_afternm( |
| 816 |
6 |
| msg_pm->pm_ptr, NULL, ct_buf, ct_len, mac_buf, adata_buf, adata_len, |
| 817 |
6 |
| nonce_buf, (crypto_aead_aes256gcm_state *)precalc_pm->pm_ptr |
| 818 |
|
| ); |
| 819 |
|
| |
| 820 |
6 |
▶ | if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { |
0 |
6 |
6 |
|
| 821 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 822 |
0 |
| croak("decrypt_detached: Failed to release protmem RO"); |
| 823 |
|
| } |
| 824 |
|
| |
| 825 |
6 |
▶ | 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 |
6 |
6 |
|
| 826 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 827 |
0 |
| croak("decrypt_detached: Failed to release msg protmem RW"); |
| 828 |
|
| } |
| 829 |
|
| |
| 830 |
6 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
6 |
0 |
6 |
|
| 831 |
6 |
| RETVAL = protmem_to_sv(aTHX_ msg_pm, MEMVAULT_CLASS); |
| 832 |
|
| else { |
| 833 |
0 |
| protmem_free(aTHX_ msg_pm); |
| 834 |
0 |
| croak("decrypt_detached: Message forged"); |
| 835 |
|
| } |
| 836 |
|
| |
| 837 |
|
| OUTPUT: |
| 838 |
|
| RETVAL |
| 839 |
|
| |
| 840 |
|
| void encrypt(SV * self, SV * msg, SV * nonce, SV * adata = &PL_sv_undef) |
| 841 |
|
| |
| 842 |
|
| ALIAS: |
| 843 |
|
| encrypt_detached = 1 |
| 844 |
|
| |
| 845 |
|
| PREINIT: |
| 846 |
12 |
| protmem *precalc_pm, *msg_pm = NULL; |
| 847 |
12 |
| SV *ct, *adata_out = NULL; |
| 848 |
12 |
| unsigned char *msg_buf, *adata_buf = NULL, *nonce_buf, *ct_buf, *adata_out_buf; |
| 849 |
12 |
▶ | STRLEN msg_len, adata_len = 0, nonce_len, out_len; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN msg_len, adata_len = 0, nonce_len, out_len; |
0 |
12 |
12 |
|
| 850 |
|
| |
| 851 |
|
| PPCODE: |
| 852 |
12 |
| nonce_buf = (unsigned char *)SvPVbyte(nonce, nonce_len); |
| 853 |
12 |
▶ | if (nonce_len != crypto_aead_aes256gcm_NPUBBYTES) |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce_len != crypto_aead_aes256gcm_NPUBBYTES) |
0 |
12 |
12 |
|
| 854 |
0 |
| croak("encrypt: Invalid nonce length %lu", nonce_len); |
| 855 |
|
| |
| 856 |
12 |
| SvGETMAGIC(adata); |
| 857 |
12 |
▶ | if (SvOK(adata)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(adata)) |
12 |
0 |
12 |
|
| 858 |
12 |
| adata_buf = (unsigned char *)SvPVbyte_nomg(adata, adata_len); |
| 859 |
|
| |
| 860 |
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 |
|
| 861 |
0 |
| msg_pm = protmem_get(aTHX_ msg, MEMVAULT_CLASS); |
| 862 |
0 |
| msg_buf = msg_pm->pm_ptr; |
| 863 |
0 |
| msg_len = msg_pm->size; |
| 864 |
|
| } |
| 865 |
|
| else |
| 866 |
12 |
| msg_buf = (unsigned char *)SvPVbyte(msg, msg_len); |
| 867 |
|
| |
| 868 |
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 |
|
| 869 |
0 |
| croak("encrypt: Failed to grant msg protmem RO"); |
| 870 |
|
| |
| 871 |
12 |
| precalc_pm = protmem_get(aTHX_ self, "Crypt::Sodium::XS::aead::precalc"); |
| 872 |
12 |
▶ | if (protmem_grant(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_grant(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
12 |
12 |
|
| 873 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 874 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 875 |
0 |
| croak("encrypt: Failed to grant protmem RW"); |
| 876 |
|
| } |
| 877 |
|
| |
| 878 |
12 |
▶ | if (ix == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix == 1) { |
6 |
6 |
12 |
|
| 879 |
6 |
| Newx(ct_buf, msg_len + 1, unsigned char); |
| 880 |
6 |
▶ | if (ct_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) { |
0 |
6 |
6 |
|
| 881 |
0 |
| protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW); |
| 882 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 883 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 884 |
0 |
| croak("encrypt: Failed to allocate memory"); |
| 885 |
|
| } |
| 886 |
6 |
| ct_buf[msg_len] = '\0'; |
| 887 |
|
| |
| 888 |
6 |
| Newx(adata_out_buf, crypto_aead_aes256gcm_ABYTES + 1, unsigned char); |
| 889 |
6 |
▶ | if (adata_out_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (adata_out_buf == NULL) { |
0 |
6 |
6 |
|
| 890 |
0 |
| protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW); |
| 891 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 892 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 893 |
0 |
| croak("encrypt: Failed to allocate memory"); |
| 894 |
|
| } |
| 895 |
6 |
| adata_out_buf[crypto_aead_aes256gcm_ABYTES] = '\0'; |
| 896 |
|
| |
| 897 |
6 |
| crypto_aead_aes256gcm_encrypt_detached_afternm( |
| 898 |
|
| ct_buf, adata_out_buf, NULL, msg_buf, msg_len, adata_buf, adata_len, |
| 899 |
6 |
| NULL, nonce_buf, (crypto_aead_aes256gcm_state *)precalc_pm->pm_ptr |
| 900 |
|
| ); |
| 901 |
|
| |
| 902 |
6 |
| ct = newSV(0); |
| 903 |
6 |
| sv_usepvn_flags(ct, (char *)ct_buf, msg_len, SV_HAS_TRAILING_NUL); |
| 904 |
6 |
| adata_out = newSV(0); |
| 905 |
6 |
| sv_usepvn_flags(adata_out, (char *)adata_out_buf, |
| 906 |
|
| crypto_aead_aes256gcm_ABYTES, SV_HAS_TRAILING_NUL); |
| 907 |
|
| } |
| 908 |
|
| else { |
| 909 |
6 |
▶ | if (SIZE_MAX - crypto_aead_aes256gcm_ABYTES < msg_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SIZE_MAX - crypto_aead_aes256gcm_ABYTES < msg_len) |
0 |
6 |
6 |
|
| 910 |
0 |
| croak("Message too large"); |
| 911 |
6 |
| out_len = crypto_aead_aes256gcm_ABYTES + msg_len; |
| 912 |
|
| |
| 913 |
6 |
| Newx(ct_buf, out_len + 1, unsigned char); |
| 914 |
6 |
▶ | if (ct_buf == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_buf == NULL) { |
0 |
6 |
6 |
|
| 915 |
0 |
| protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW); |
| 916 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 917 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 918 |
0 |
| croak("encrypt: Failed to allocate memory"); |
| 919 |
|
| } |
| 920 |
6 |
| ct_buf[out_len] = '\0'; |
| 921 |
|
| |
| 922 |
6 |
| crypto_aead_aes256gcm_encrypt_afternm( |
| 923 |
|
| ct_buf, NULL, msg_buf, msg_len, adata_buf, adata_len, |
| 924 |
6 |
| NULL, nonce_buf, (crypto_aead_aes256gcm_state *)precalc_pm->pm_ptr); |
| 925 |
|
| |
| 926 |
6 |
| ct = newSV(0); |
| 927 |
6 |
| sv_usepvn_flags(ct, (char *)ct_buf, out_len, SV_HAS_TRAILING_NUL); |
| 928 |
|
| } |
| 929 |
|
| |
| 930 |
12 |
▶ | if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (protmem_release(aTHX_ precalc_pm, PROTMEM_FLAG_MPROTECT_RW) != 0) { |
0 |
12 |
12 |
|
| 931 |
0 |
▶ | if (msg_pm) |
Branch: 1 branch
| Branch | True | False | Total |
| if (msg_pm) |
0 |
0 |
0 |
|
| 932 |
0 |
| protmem_release(aTHX_ msg_pm, PROTMEM_FLAG_MPROTECT_RO); |
| 933 |
0 |
| croak("encrypt: Failed to release protmem RW"); |
| 934 |
|
| } |
| 935 |
|
| |
| 936 |
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 |
|
| 937 |
0 |
| croak("encrypt: Failed to release msg protmem RO"); |
| 938 |
|
| |
| 939 |
12 |
▶ | mXPUSHs(ct); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(ct); |
0 |
12 |
12 |
|
| 940 |
|
| |
| 941 |
12 |
▶ | if (ix & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix & 1) { |
6 |
6 |
12 |
|
| 942 |
6 |
▶ | mXPUSHs(adata_out); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(adata_out); |
0 |
6 |
6 |
|
| 943 |
6 |
| XSRETURN(2); |
| 944 |
|
| } |
| 945 |
|
| |
| 946 |
6 |
| XSRETURN(1); |