| 1 |
|
| MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCMSIV |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| SV * |
| 6 |
|
| gcm_siv_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *aad, SV *plaintext) |
| 7 |
|
| CODE: |
| 8 |
|
| { |
| 9 |
35 |
| STRLEN k_len = 0, n_len = 0, a_len = 0, pt_len = 0; |
| 10 |
35 |
| unsigned char *k, *n, *a = NULL, *pt, *ct; |
| 11 |
|
| unsigned long ct_len, tag_len; |
| 12 |
|
| int rv, id; |
| 13 |
|
| |
| 14 |
35 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 15 |
35 |
▶ | 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 |
35 |
35 |
|
| 16 |
|
| |
| 17 |
35 |
▶ | 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"); |
35 |
0 |
35 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
2 |
33 |
35 |
| 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 |
|
| 18 |
34 |
▶ | if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
34 |
0 |
34 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
33 |
34 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 19 |
34 |
▶ | 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"); |
34 |
0 |
34 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
1 |
33 |
34 |
| 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 |
|
| 20 |
34 |
▶ | k = (unsigned char *)SvPVbyte(key, k_len); |
Branch: 1 branch
| Branch | True | False | Total |
| k = (unsigned char *)SvPVbyte(key, k_len); |
33 |
1 |
34 |
|
| 21 |
34 |
▶ | n = (unsigned char *)SvPVbyte(nonce, n_len); |
Branch: 1 branch
| Branch | True | False | Total |
| n = (unsigned char *)SvPVbyte(nonce, n_len); |
33 |
1 |
34 |
|
| 22 |
34 |
| pt = (unsigned char *)SvPVbyte(plaintext, pt_len); |
| 23 |
34 |
▶ | if (SvOK(aad)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(aad)) { |
7 |
27 |
34 |
|
| 24 |
7 |
▶ | if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
7 |
0 |
7 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
1 |
6 |
7 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 25 |
7 |
| a = (unsigned char *)SvPVbyte(aad, a_len); |
| 26 |
|
| } |
| 27 |
|
| |
| 28 |
34 |
| ct_len = (unsigned long)pt_len + 16; |
| 29 |
34 |
| RETVAL = NEWSV(0, ct_len); |
| 30 |
34 |
| SvPOK_only(RETVAL); |
| 31 |
34 |
| SvCUR_set(RETVAL, ct_len); |
| 32 |
34 |
| *SvEND(RETVAL) = '\0'; |
| 33 |
34 |
| ct = (unsigned char *)SvPVX(RETVAL); |
| 34 |
34 |
| tag_len = 16; |
| 35 |
|
| |
| 36 |
34 |
| rv = gcm_siv_memory(id, |
| 37 |
|
| k, (unsigned long)k_len, |
| 38 |
|
| n, (unsigned long)n_len, |
| 39 |
|
| a, (unsigned long)a_len, |
| 40 |
|
| pt, (unsigned long)pt_len, |
| 41 |
|
| ct, /* ciphertext output */ |
| 42 |
|
| ct + pt_len, &tag_len, /* tag appended */ |
| 43 |
|
| LTC_ENCRYPT); |
| 44 |
34 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
2 |
32 |
34 |
|
| 45 |
2 |
| SvREFCNT_dec(RETVAL); |
| 46 |
2 |
| croak("FATAL: gcm_siv_memory(encrypt) failed: %s", error_to_string(rv)); |
| 47 |
|
| } |
| 48 |
|
| } |
| 49 |
|
| OUTPUT: |
| 50 |
|
| RETVAL |
| 51 |
|
| |
| 52 |
|
| void |
| 53 |
|
| gcm_siv_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *aad, SV *ciphertext) |
| 54 |
|
| PPCODE: |
| 55 |
|
| { |
| 56 |
20 |
| STRLEN k_len = 0, n_len = 0, a_len = 0, ct_len = 0; |
| 57 |
20 |
| unsigned char *k, *n, *a = NULL, *ct, *pt; |
| 58 |
|
| unsigned long pt_len, tag_len; |
| 59 |
|
| int rv, id; |
| 60 |
|
| SV *output; |
| 61 |
|
| unsigned char tag[16]; |
| 62 |
|
| |
| 63 |
20 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 64 |
20 |
▶ | 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 |
20 |
20 |
|
| 65 |
|
| |
| 66 |
20 |
▶ | 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"); |
20 |
0 |
20 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
1 |
19 |
20 |
| 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 |
|
| 67 |
20 |
▶ | if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
20 |
0 |
20 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
19 |
20 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 68 |
20 |
▶ | 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"); |
20 |
0 |
20 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
1 |
19 |
20 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 69 |
20 |
▶ | k = (unsigned char *)SvPVbyte(key, k_len); |
Branch: 1 branch
| Branch | True | False | Total |
| k = (unsigned char *)SvPVbyte(key, k_len); |
19 |
1 |
20 |
|
| 70 |
20 |
▶ | n = (unsigned char *)SvPVbyte(nonce, n_len); |
Branch: 1 branch
| Branch | True | False | Total |
| n = (unsigned char *)SvPVbyte(nonce, n_len); |
19 |
1 |
20 |
|
| 71 |
20 |
| ct = (unsigned char *)SvPVbyte(ciphertext, ct_len); |
| 72 |
20 |
▶ | 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"); |
1 |
19 |
20 |
|
| 73 |
19 |
▶ | if (SvOK(aad)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(aad)) { |
7 |
12 |
19 |
|
| 74 |
7 |
▶ | if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
7 |
0 |
7 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
1 |
6 |
7 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
1 |
0 |
1 |
| if (!SvPOK_spec(aad)) croak("FATAL: adata must be string/buffer scalar"); |
0 |
1 |
1 |
|
| 75 |
7 |
| a = (unsigned char *)SvPVbyte(aad, a_len); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
19 |
| pt_len = (unsigned long)ct_len - 16; |
| 79 |
19 |
▶ | 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); |
19 |
0 |
19 |
|
| 80 |
19 |
| SvPOK_only(output); |
| 81 |
19 |
| SvCUR_set(output, pt_len); |
| 82 |
19 |
| *SvEND(output) = '\0'; |
| 83 |
19 |
| pt = (unsigned char *)SvPVX(output); |
| 84 |
19 |
| memcpy(tag, ct + pt_len, 16); |
| 85 |
19 |
| tag_len = 16; |
| 86 |
|
| |
| 87 |
19 |
| rv = gcm_siv_memory(id, |
| 88 |
|
| k, (unsigned long)k_len, |
| 89 |
|
| n, (unsigned long)n_len, |
| 90 |
|
| a, (unsigned long)a_len, |
| 91 |
|
| ct, pt_len, /* ciphertext input */ |
| 92 |
|
| pt, /* plaintext output */ |
| 93 |
|
| tag, &tag_len, |
| 94 |
|
| LTC_DECRYPT); |
| 95 |
19 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
4 |
15 |
19 |
|
| 96 |
4 |
| SvREFCNT_dec(output); |
| 97 |
4 |
▶ | XPUSHs(sv_2mortal(newSVpvn(NULL, 0))); /* undef */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(NULL, 0))); /* undef */ |
0 |
4 |
4 |
|
| 98 |
|
| } else { |
| 99 |
15 |
▶ | XPUSHs(sv_2mortal(output)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(output)); |
0 |
15 |
15 |
|
| 100 |
|
| } |
| 101 |
|
| } |