| 1 |
|
| MODULE = CryptX PACKAGE = Crypt::AuthEnc::SIV |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| SV * |
| 6 |
|
| siv_encrypt_authenticate(char *cipher_name, SV *key, SV *plaintext, SV *aad = &PL_sv_undef) |
| 7 |
|
| CODE: |
| 8 |
|
| { |
| 9 |
59 |
| STRLEN k_len = 0, pt_len = 0; |
| 10 |
|
| unsigned char *k, *pt, *ct; |
| 11 |
|
| unsigned long ct_len; |
| 12 |
|
| int rv, id; |
| 13 |
|
| const unsigned char *ad_ptrs[126]; |
| 14 |
|
| unsigned long ad_lens[126]; |
| 15 |
59 |
| unsigned long n_ad = 0; |
| 16 |
|
| STRLEN tmp; |
| 17 |
|
| |
| 18 |
59 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 19 |
59 |
▶ | if (id == -1) croak("FATAL: find_cipher failed for '%s'", cipher_name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (id == -1) croak("FATAL: find_cipher failed for '%s'", cipher_name); |
0 |
59 |
59 |
|
| 20 |
|
| |
| 21 |
59 |
▶ | if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
59 |
0 |
59 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
2 |
57 |
59 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
2 |
0 |
2 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
1 |
1 |
2 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 22 |
58 |
| k = (unsigned char *)SvPVbyte(key, k_len); |
| 23 |
58 |
▶ | if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
58 |
0 |
58 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
1 |
57 |
58 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 24 |
58 |
| pt = (unsigned char *)SvPVbyte(plaintext, pt_len); |
| 25 |
|
| |
| 26 |
58 |
▶ | if (SvOK(aad)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(aad)) { |
48 |
10 |
58 |
|
| 27 |
65 |
▶ | if (SvROK(aad) && SvTYPE(SvRV(aad)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(aad) && SvTYPE(SvRV(aad)) == SVt_PVAV) { |
20 |
28 |
48 |
| if (SvROK(aad) && SvTYPE(SvRV(aad)) == SVt_PVAV) { |
19 |
1 |
20 |
|
| 28 |
19 |
| AV *av = (AV *)SvRV(aad); |
| 29 |
19 |
| SSize_t i, alen = av_len(av); |
| 30 |
159 |
▶ | for (i = 0; i <= alen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= alen; i++) { |
142 |
17 |
159 |
|
| 31 |
142 |
| SV **svp = av_fetch(av, i, 0); |
| 32 |
142 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
142 |
0 |
142 |
| if (svp && SvOK(*svp)) { |
137 |
5 |
142 |
|
| 33 |
137 |
▶ | if (n_ad >= 126) croak("FATAL: too many associated data components (max 126)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n_ad >= 126) croak("FATAL: too many associated data components (max 126)"); |
1 |
136 |
137 |
|
| 34 |
136 |
▶ | if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
136 |
0 |
136 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
2 |
134 |
136 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
2 |
0 |
2 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
1 |
1 |
2 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 35 |
135 |
| ad_ptrs[n_ad] = (const unsigned char *)SvPVbyte(*svp, tmp); |
| 36 |
135 |
| ad_lens[n_ad] = (unsigned long)tmp; |
| 37 |
135 |
| n_ad++; |
| 38 |
|
| } |
| 39 |
|
| } |
| 40 |
|
| } else { |
| 41 |
29 |
▶ | if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
29 |
0 |
29 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
1 |
28 |
29 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
0 |
1 |
1 |
|
| 42 |
29 |
| ad_ptrs[0] = (const unsigned char *)SvPVbyte(aad, tmp); |
| 43 |
29 |
| ad_lens[0] = (unsigned long)tmp; |
| 44 |
29 |
| n_ad = 1; |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
56 |
| ct_len = (unsigned long)pt_len + 16; |
| 48 |
56 |
▶ | RETVAL = NEWSV(0, ct_len > 0 ? ct_len : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = NEWSV(0, ct_len > 0 ? ct_len : 1); |
56 |
0 |
56 |
|
| 49 |
56 |
| SvPOK_only(RETVAL); |
| 50 |
56 |
| SvCUR_set(RETVAL, ct_len); |
| 51 |
56 |
| ct = (unsigned char *)SvPVX(RETVAL); |
| 52 |
|
| |
| 53 |
56 |
| rv = siv_encrypt_memory(id, k, (unsigned long)k_len, |
| 54 |
|
| n_ad, |
| 55 |
|
| ad_ptrs, ad_lens, |
| 56 |
|
| pt, (unsigned long)pt_len, |
| 57 |
|
| ct, &ct_len); |
| 58 |
56 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
56 |
56 |
|
| 59 |
0 |
| SvREFCNT_dec(RETVAL); |
| 60 |
0 |
| croak("FATAL: siv_encrypt_memory failed: %s", error_to_string(rv)); |
| 61 |
|
| } |
| 62 |
56 |
| SvCUR_set(RETVAL, ct_len); |
| 63 |
56 |
| *SvEND(RETVAL) = '\0'; |
| 64 |
|
| } |
| 65 |
|
| OUTPUT: |
| 66 |
|
| RETVAL |
| 67 |
|
| |
| 68 |
|
| void |
| 69 |
|
| siv_decrypt_verify(char *cipher_name, SV *key, SV *ciphertext, SV *aad = &PL_sv_undef) |
| 70 |
|
| PPCODE: |
| 71 |
|
| { |
| 72 |
36 |
| STRLEN k_len = 0, ct_len = 0; |
| 73 |
|
| unsigned char *k, *ct, *pt; |
| 74 |
|
| unsigned long pt_len; |
| 75 |
|
| int rv, id; |
| 76 |
|
| SV *output; |
| 77 |
|
| const unsigned char *ad_ptrs[126]; |
| 78 |
|
| unsigned long ad_lens[126]; |
| 79 |
36 |
| unsigned long n_ad = 0; |
| 80 |
|
| STRLEN tmp; |
| 81 |
|
| |
| 82 |
36 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 83 |
36 |
▶ | if (id == -1) croak("FATAL: find_cipher failed for '%s'", cipher_name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (id == -1) croak("FATAL: find_cipher failed for '%s'", cipher_name); |
0 |
36 |
36 |
|
| 84 |
|
| |
| 85 |
36 |
▶ | if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
36 |
0 |
36 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
1 |
35 |
36 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 86 |
36 |
| k = (unsigned char *)SvPVbyte(key, k_len); |
| 87 |
36 |
▶ | if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
36 |
0 |
36 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
2 |
34 |
36 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
2 |
0 |
2 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
1 |
1 |
2 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 88 |
35 |
| ct = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
| 89 |
35 |
▶ | if (ct_len < 16) croak("FATAL: ciphertext too short"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ct_len < 16) croak("FATAL: ciphertext too short"); |
0 |
35 |
35 |
|
| 90 |
|
| |
| 91 |
35 |
▶ | if (SvOK(aad)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(aad)) { |
25 |
10 |
35 |
|
| 92 |
26 |
▶ | if (SvROK(aad) && SvTYPE(SvRV(aad)) == SVt_PVAV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(aad) && SvTYPE(SvRV(aad)) == SVt_PVAV) { |
3 |
22 |
25 |
| if (SvROK(aad) && SvTYPE(SvRV(aad)) == SVt_PVAV) { |
2 |
1 |
3 |
|
| 93 |
2 |
| AV *av = (AV *)SvRV(aad); |
| 94 |
2 |
| SSize_t i, alen = av_len(av); |
| 95 |
131 |
▶ | for (i = 0; i <= alen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= alen; i++) { |
130 |
1 |
131 |
|
| 96 |
130 |
| SV **svp = av_fetch(av, i, 0); |
| 97 |
130 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
130 |
0 |
130 |
| if (svp && SvOK(*svp)) { |
130 |
0 |
130 |
|
| 98 |
130 |
▶ | if (n_ad >= 126) croak("FATAL: too many associated data components (max 126)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n_ad >= 126) croak("FATAL: too many associated data components (max 126)"); |
1 |
129 |
130 |
|
| 99 |
129 |
▶ | if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
129 |
0 |
129 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
0 |
129 |
129 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(*svp)) croak("FATAL: associated data component must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 100 |
129 |
| ad_ptrs[n_ad] = (const unsigned char *)SvPVbyte(*svp, tmp); |
| 101 |
129 |
| ad_lens[n_ad] = (unsigned long)tmp; |
| 102 |
129 |
| n_ad++; |
| 103 |
|
| } |
| 104 |
|
| } |
| 105 |
|
| } else { |
| 106 |
23 |
▶ | if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
23 |
0 |
23 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
1 |
22 |
23 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: associated data must be string/buffer scalar or arrayref"); |
0 |
1 |
1 |
|
| 107 |
23 |
| ad_ptrs[0] = (const unsigned char *)SvPVbyte(aad, tmp); |
| 108 |
23 |
| ad_lens[0] = (unsigned long)tmp; |
| 109 |
23 |
| n_ad = 1; |
| 110 |
|
| } |
| 111 |
|
| } |
| 112 |
34 |
| pt_len = (unsigned long)ct_len - 16; |
| 113 |
34 |
▶ | output = NEWSV(0, pt_len > 0 ? pt_len : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| output = NEWSV(0, pt_len > 0 ? pt_len : 1); |
30 |
4 |
34 |
|
| 114 |
34 |
| SvPOK_only(output); |
| 115 |
34 |
| SvCUR_set(output, pt_len); |
| 116 |
34 |
| pt = (unsigned char *)SvPVX(output); |
| 117 |
|
| |
| 118 |
34 |
| rv = siv_decrypt_memory(id, k, (unsigned long)k_len, |
| 119 |
|
| n_ad, |
| 120 |
|
| ad_ptrs, ad_lens, |
| 121 |
|
| ct, (unsigned long)ct_len, |
| 122 |
|
| pt, &pt_len); |
| 123 |
34 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
11 |
23 |
34 |
|
| 124 |
11 |
| SvREFCNT_dec(output); |
| 125 |
11 |
▶ | XPUSHs(sv_2mortal(newSVpvn(NULL, 0))); /* undef */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(NULL, 0))); /* undef */ |
0 |
11 |
11 |
|
| 126 |
|
| } else { |
| 127 |
23 |
| SvCUR_set(output, pt_len); |
| 128 |
23 |
| *SvEND(output) = '\0'; |
| 129 |
23 |
▶ | XPUSHs(sv_2mortal(output)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(output)); |
0 |
23 |
23 |
|
| 130 |
|
| } |
| 131 |
|
| } |