| 1 |
|
| MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| Crypt::AuthEnc::GCM |
| 6 |
|
| new(Class, char * cipher_name, SV * key, SV * nonce = NULL) |
| 7 |
|
| CODE: |
| 8 |
|
| { |
| 9 |
36 |
| STRLEN k_len = 0, iv_len = 0; |
| 10 |
36 |
| unsigned char *k = NULL, *iv = NULL; |
| 11 |
|
| int id, rv; |
| 12 |
|
| |
| 13 |
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"); |
0 |
36 |
36 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 14 |
36 |
| k = (unsigned char *) SvPVbyte(key, k_len); |
| 15 |
36 |
▶ | if (nonce) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nonce) { |
21 |
15 |
36 |
|
| 16 |
21 |
▶ | 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"); |
21 |
0 |
21 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
21 |
21 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 17 |
21 |
| iv = (unsigned char *)SvPVbyte(nonce, iv_len); |
| 18 |
|
| } |
| 19 |
|
| |
| 20 |
36 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 21 |
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 |
|
| 22 |
|
| |
| 23 |
36 |
| Newz(0, RETVAL, 1, struct cryptx_authenc_gcm_struct); |
| 24 |
36 |
▶ | if (!RETVAL) croak("FATAL: Newz failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) croak("FATAL: Newz failed"); |
0 |
36 |
36 |
|
| 25 |
|
| |
| 26 |
36 |
| rv = gcm_init(&RETVAL->state, id, k, (unsigned long)k_len); |
| 27 |
36 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
36 |
36 |
|
| 28 |
0 |
| zeromem(RETVAL, sizeof(*RETVAL)); |
| 29 |
0 |
| Safefree(RETVAL); |
| 30 |
0 |
| croak("FATAL: gcm_init failed: %s", error_to_string(rv)); |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
36 |
▶ | if (iv && iv_len > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (iv && iv_len > 0) { |
21 |
15 |
36 |
| if (iv && iv_len > 0) { |
21 |
0 |
21 |
|
| 34 |
21 |
| rv = gcm_add_iv(&RETVAL->state, iv, (unsigned long)iv_len); |
| 35 |
21 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
21 |
21 |
|
| 36 |
0 |
| zeromem(RETVAL, sizeof(*RETVAL)); |
| 37 |
0 |
| Safefree(RETVAL); |
| 38 |
0 |
| croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); |
| 39 |
|
| } |
| 40 |
|
| } |
| 41 |
|
| } |
| 42 |
|
| OUTPUT: |
| 43 |
|
| RETVAL |
| 44 |
|
| |
| 45 |
|
| void |
| 46 |
|
| DESTROY(Crypt::AuthEnc::GCM self) |
| 47 |
|
| CODE: |
| 48 |
37 |
▶ | if (self) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self) { |
37 |
0 |
37 |
|
| 49 |
37 |
| zeromem(self, sizeof(*self)); |
| 50 |
37 |
| Safefree(self); |
| 51 |
|
| } |
| 52 |
|
| |
| 53 |
|
| Crypt::AuthEnc::GCM |
| 54 |
|
| clone(Crypt::AuthEnc::GCM self) |
| 55 |
|
| CODE: |
| 56 |
1 |
| Newz(0, RETVAL, 1, struct cryptx_authenc_gcm_struct); |
| 57 |
1 |
▶ | if (!RETVAL) croak("FATAL: Newz failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) croak("FATAL: Newz failed"); |
0 |
1 |
1 |
|
| 58 |
1 |
| Copy(self, RETVAL, 1, struct cryptx_authenc_gcm_struct); |
| 59 |
|
| OUTPUT: |
| 60 |
|
| RETVAL |
| 61 |
|
| |
| 62 |
|
| void |
| 63 |
|
| reset(Crypt::AuthEnc::GCM self) |
| 64 |
|
| PPCODE: |
| 65 |
|
| { |
| 66 |
|
| int rv; |
| 67 |
1 |
| rv = gcm_reset(&self->state); |
| 68 |
1 |
▶ | if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv)); |
0 |
1 |
1 |
|
| 69 |
1 |
| self->finalized = 0; |
| 70 |
1 |
▶ | XPUSHs(ST(0)); /* return self */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(ST(0)); /* return self */ |
0 |
1 |
1 |
|
| 71 |
|
| } |
| 72 |
|
| |
| 73 |
|
| SV * |
| 74 |
|
| encrypt_add(Crypt::AuthEnc::GCM self, SV * data) |
| 75 |
|
| CODE: |
| 76 |
|
| { |
| 77 |
|
| int rv; |
| 78 |
|
| STRLEN in_data_len; |
| 79 |
|
| unsigned char *in_data, *out_data; |
| 80 |
|
| |
| 81 |
21 |
▶ | if (self->finalized) croak("FATAL: AEAD object already finalized"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->finalized) croak("FATAL: AEAD object already finalized"); |
1 |
20 |
21 |
|
| 82 |
20 |
| in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
| 83 |
20 |
▶ | if (in_data_len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_data_len == 0) { |
0 |
20 |
20 |
|
| 84 |
0 |
| RETVAL = newSVpvn("", 0); |
| 85 |
|
| } |
| 86 |
|
| else |
| 87 |
|
| { |
| 88 |
20 |
| RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
| 89 |
20 |
| SvPOK_only(RETVAL); |
| 90 |
20 |
| SvCUR_set(RETVAL, in_data_len); |
| 91 |
20 |
| *SvEND(RETVAL) = '\0'; |
| 92 |
20 |
| out_data = (unsigned char *)SvPVX(RETVAL); |
| 93 |
20 |
| rv = gcm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT); |
| 94 |
20 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
20 |
20 |
|
| 95 |
0 |
| SvREFCNT_dec(RETVAL); |
| 96 |
0 |
| croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); |
| 97 |
|
| } |
| 98 |
|
| } |
| 99 |
|
| } |
| 100 |
|
| OUTPUT: |
| 101 |
|
| RETVAL |
| 102 |
|
| |
| 103 |
|
| void |
| 104 |
|
| iv_add(Crypt::AuthEnc::GCM self, SV * data) |
| 105 |
|
| PPCODE: |
| 106 |
|
| { |
| 107 |
|
| int rv; |
| 108 |
|
| STRLEN in_data_len; |
| 109 |
|
| unsigned char *in_data; |
| 110 |
|
| |
| 111 |
16 |
▶ | if (self->finalized) croak("FATAL: AEAD object already finalized"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->finalized) croak("FATAL: AEAD object already finalized"); |
0 |
16 |
16 |
|
| 112 |
16 |
| in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
| 113 |
16 |
| rv = gcm_add_iv(&self->state, in_data, (unsigned long)in_data_len); |
| 114 |
16 |
▶ | if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); |
0 |
16 |
16 |
|
| 115 |
16 |
▶ | XPUSHs(ST(0)); /* return self */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(ST(0)); /* return self */ |
0 |
16 |
16 |
|
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| void |
| 119 |
|
| adata_add(Crypt::AuthEnc::GCM self, SV * data) |
| 120 |
|
| PPCODE: |
| 121 |
|
| { |
| 122 |
|
| int rv; |
| 123 |
|
| STRLEN in_data_len; |
| 124 |
|
| unsigned char *in_data; |
| 125 |
|
| |
| 126 |
17 |
▶ | if (self->finalized) croak("FATAL: AEAD object already finalized"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->finalized) croak("FATAL: AEAD object already finalized"); |
0 |
17 |
17 |
|
| 127 |
17 |
| in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
| 128 |
17 |
| rv = gcm_add_aad(&self->state, in_data, (unsigned long)in_data_len); |
| 129 |
17 |
▶ | if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv)); |
0 |
17 |
17 |
|
| 130 |
17 |
▶ | XPUSHs(ST(0)); /* return self */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(ST(0)); /* return self */ |
0 |
17 |
17 |
|
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| SV * |
| 134 |
|
| decrypt_add(Crypt::AuthEnc::GCM self, SV * data) |
| 135 |
|
| CODE: |
| 136 |
|
| { |
| 137 |
|
| int rv; |
| 138 |
|
| STRLEN in_data_len; |
| 139 |
|
| unsigned char *in_data, *out_data; |
| 140 |
|
| |
| 141 |
37 |
▶ | if (self->finalized) croak("FATAL: AEAD object already finalized"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->finalized) croak("FATAL: AEAD object already finalized"); |
0 |
37 |
37 |
|
| 142 |
37 |
| in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
| 143 |
37 |
▶ | if (in_data_len == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_data_len == 0) { |
0 |
37 |
37 |
|
| 144 |
0 |
| RETVAL = newSVpvn("", 0); |
| 145 |
|
| } |
| 146 |
|
| else { |
| 147 |
37 |
| RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
| 148 |
37 |
| SvPOK_only(RETVAL); |
| 149 |
37 |
| SvCUR_set(RETVAL, in_data_len); |
| 150 |
37 |
| *SvEND(RETVAL) = '\0'; |
| 151 |
37 |
| out_data = (unsigned char *)SvPVX(RETVAL); |
| 152 |
37 |
| rv = gcm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT); |
| 153 |
37 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
37 |
37 |
|
| 154 |
0 |
| SvREFCNT_dec(RETVAL); |
| 155 |
0 |
| croak("FATAL: decrypt_add/gcm_process failed: %s", error_to_string(rv)); |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
|
| } |
| 159 |
|
| OUTPUT: |
| 160 |
|
| RETVAL |
| 161 |
|
| |
| 162 |
|
| |
| 163 |
|
| void |
| 164 |
|
| encrypt_done(Crypt::AuthEnc::GCM self) |
| 165 |
|
| PPCODE: |
| 166 |
|
| { |
| 167 |
|
| int rv; |
| 168 |
|
| unsigned char tag[MAXBLOCKSIZE]; |
| 169 |
11 |
| unsigned long tag_len = sizeof(tag); |
| 170 |
|
| |
| 171 |
11 |
▶ | if (self->finalized) croak("FATAL: AEAD object already finalized"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->finalized) croak("FATAL: AEAD object already finalized"); |
1 |
10 |
11 |
|
| 172 |
10 |
| rv = gcm_done(&self->state, tag, &tag_len); |
| 173 |
10 |
▶ | if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); |
0 |
10 |
10 |
|
| 174 |
10 |
| self->finalized = 1; |
| 175 |
10 |
▶ | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
0 |
10 |
10 |
|
| 176 |
10 |
| zeromem(tag, sizeof(tag)); |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
|
| void |
| 180 |
|
| decrypt_done(Crypt::AuthEnc::GCM self, ...) |
| 181 |
|
| PPCODE: |
| 182 |
|
| { |
| 183 |
|
| int rv; |
| 184 |
|
| unsigned char tag[MAXBLOCKSIZE]; |
| 185 |
8 |
| unsigned long tag_len = sizeof(tag); |
| 186 |
|
| STRLEN expected_tag_len; |
| 187 |
|
| unsigned char *expected_tag; |
| 188 |
|
| |
| 189 |
8 |
▶ | if (self->finalized) croak("FATAL: AEAD object already finalized"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->finalized) croak("FATAL: AEAD object already finalized"); |
0 |
8 |
8 |
|
| 190 |
8 |
| rv = gcm_done(&self->state, tag, &tag_len); |
| 191 |
8 |
▶ | if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); |
0 |
8 |
8 |
|
| 192 |
8 |
| self->finalized = 1; |
| 193 |
8 |
▶ | if (items == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 1) { |
8 |
0 |
8 |
|
| 194 |
8 |
▶ | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
0 |
8 |
8 |
|
| 195 |
8 |
| zeromem(tag, sizeof(tag)); |
| 196 |
|
| } |
| 197 |
|
| else { |
| 198 |
0 |
▶ | if (!SvPOK_spec(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 199 |
0 |
| expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); |
| 200 |
0 |
▶ | if (expected_tag_len!=tag_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (expected_tag_len!=tag_len) { |
0 |
0 |
0 |
|
| 201 |
0 |
▶ | XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
0 |
0 |
0 |
|
| 202 |
|
| } |
| 203 |
0 |
▶ | else if (mem_neq(expected_tag, tag, tag_len)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (mem_neq(expected_tag, tag, tag_len)) { |
0 |
0 |
0 |
|
| 204 |
0 |
▶ | XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
0 |
0 |
0 |
|
| 205 |
|
| } |
| 206 |
|
| else { |
| 207 |
0 |
▶ | XPUSHs(sv_2mortal(newSViv(1))); /* true */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(1))); /* true */ |
0 |
0 |
0 |
|
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
|
| void |
| 213 |
|
| gcm_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *header = NULL, SV *plaintext) |
| 214 |
|
| PPCODE: |
| 215 |
|
| { |
| 216 |
33 |
| STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0; |
| 217 |
33 |
| unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL; |
| 218 |
|
| int rv, id; |
| 219 |
|
| unsigned char tag[MAXBLOCKSIZE]; |
| 220 |
33 |
| unsigned long tag_len = sizeof(tag); |
| 221 |
|
| SV *output; |
| 222 |
|
| |
| 223 |
33 |
▶ | 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"); |
33 |
0 |
33 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
1 |
32 |
33 |
| 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 |
|
| 224 |
33 |
| k = (unsigned char *) SvPVbyte(key, k_len); |
| 225 |
33 |
▶ | 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"); |
33 |
0 |
33 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
1 |
32 |
33 |
| 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 |
|
| 226 |
33 |
| n = (unsigned char *) SvPVbyte(nonce, n_len); |
| 227 |
33 |
▶ | 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"); |
33 |
0 |
33 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
0 |
33 |
33 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 228 |
33 |
| pt = (unsigned char *) SvPVbyte(plaintext, pt_len); |
| 229 |
33 |
▶ | if (header && SvOK(header)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (header && SvOK(header)) { |
33 |
0 |
33 |
| if (header && SvOK(header)) { |
33 |
0 |
33 |
|
| 230 |
33 |
▶ | if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
33 |
0 |
33 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
33 |
33 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 231 |
33 |
| h = (unsigned char *) SvPVbyte(header, h_len); |
| 232 |
33 |
| zeromem(tag, sizeof(tag)); |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
33 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 236 |
33 |
▶ | 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 |
33 |
33 |
|
| 237 |
33 |
▶ | output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */ |
Branch: 1 branch
| Branch | True | False | Total |
| output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */ |
33 |
0 |
33 |
|
| 238 |
33 |
| SvPOK_only(output); |
| 239 |
33 |
| SvCUR_set(output, pt_len); |
| 240 |
33 |
| *SvEND(output) = '\0'; |
| 241 |
|
| |
| 242 |
33 |
| rv = gcm_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len, |
| 243 |
33 |
| pt, (unsigned long)pt_len, (unsigned char *)SvPVX(output), tag, &tag_len, GCM_ENCRYPT); |
| 244 |
|
| |
| 245 |
33 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
1 |
32 |
33 |
|
| 246 |
1 |
| SvREFCNT_dec(output); |
| 247 |
1 |
| croak("FATAL: gcm_memory failed: %s", error_to_string(rv)); |
| 248 |
|
| } |
| 249 |
32 |
▶ | XPUSHs(sv_2mortal(output)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(output)); |
0 |
32 |
32 |
|
| 250 |
32 |
▶ | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
0 |
32 |
32 |
|
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
|
| void |
| 254 |
|
| gcm_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv) |
| 255 |
|
| PPCODE: |
| 256 |
|
| { |
| 257 |
24 |
| STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0; |
| 258 |
24 |
| unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL; |
| 259 |
|
| int rv, id; |
| 260 |
|
| unsigned char tag[MAXBLOCKSIZE]; |
| 261 |
|
| unsigned long tag_len; |
| 262 |
|
| SV *output; |
| 263 |
|
| |
| 264 |
24 |
▶ | 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"); |
24 |
0 |
24 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
24 |
24 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 265 |
24 |
| k = (unsigned char *) SvPVbyte(key, k_len); |
| 266 |
24 |
▶ | 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"); |
24 |
0 |
24 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
24 |
24 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 267 |
24 |
| n = (unsigned char *) SvPVbyte(nonce, n_len); |
| 268 |
24 |
▶ | 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"); |
24 |
0 |
24 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
0 |
24 |
24 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 269 |
24 |
| ct = (unsigned char *) SvPVbyte(ciphertext, ct_len); |
| 270 |
24 |
▶ | if (!SvPOK_spec(tagsv)) croak("FATAL: tag must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(tagsv)) croak("FATAL: tag must be string/buffer scalar"); |
24 |
0 |
24 |
| if (!SvPOK_spec(tagsv)) croak("FATAL: tag must be string/buffer scalar"); |
0 |
24 |
24 |
| if (!SvPOK_spec(tagsv)) croak("FATAL: tag must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(tagsv)) croak("FATAL: tag must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(tagsv)) croak("FATAL: tag must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 271 |
24 |
| t = (unsigned char *) SvPVbyte(tagsv, t_len); |
| 272 |
24 |
▶ | if (header && SvOK(header)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (header && SvOK(header)) { |
24 |
0 |
24 |
| if (header && SvOK(header)) { |
24 |
0 |
24 |
|
| 273 |
24 |
▶ | if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
24 |
0 |
24 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
24 |
24 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(header)) croak("FATAL: header must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 274 |
24 |
| h = (unsigned char *) SvPVbyte(header, h_len); |
| 275 |
24 |
| zeromem(tag, sizeof(tag)); |
| 276 |
|
| } |
| 277 |
|
| |
| 278 |
24 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 279 |
24 |
▶ | 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 |
24 |
24 |
|
| 280 |
24 |
▶ | output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */ |
Branch: 1 branch
| Branch | True | False | Total |
| output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */ |
24 |
0 |
24 |
|
| 281 |
24 |
| SvPOK_only(output); |
| 282 |
24 |
| SvCUR_set(output, ct_len); |
| 283 |
24 |
| *SvEND(output) = '\0'; |
| 284 |
|
| /* Clamp malformed tags to the stack buffer size before copying. */ |
| 285 |
24 |
| tag_len = (unsigned long)(t_len > sizeof(tag) ? sizeof(tag) : t_len); |
| 286 |
24 |
▶ | if (tag_len > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag_len > 0) { |
23 |
1 |
24 |
|
| 287 |
23 |
| Copy(t, tag, tag_len, unsigned char); |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
24 |
| rv = gcm_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len, |
| 291 |
24 |
| (unsigned char *)SvPVX(output), (unsigned long)ct_len, ct, tag, &tag_len, GCM_DECRYPT); |
| 292 |
|
| |
| 293 |
24 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
5 |
19 |
24 |
|
| 294 |
5 |
| SvREFCNT_dec(output); |
| 295 |
5 |
▶ | XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */ |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */ |
0 |
5 |
5 |
|
| 296 |
|
| } |
| 297 |
|
| else { |
| 298 |
19 |
▶ | XPUSHs(sv_2mortal(output)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(output)); |
0 |
19 |
19 |
|
| 299 |
|
| } |
| 300 |
|
| } |