| 1 |
|
| MODULE = CryptX PACKAGE = Crypt::Mode::XTS |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| ### hand-written XS - unlike other Crypt::Mode::* this file is NOT generated from _generators/Mode.xs.inc.tt |
| 6 |
|
| |
| 7 |
|
| Crypt::Mode::XTS |
| 8 |
|
| new(Class, SV * cipher, SV * key, int rounds=0) |
| 9 |
|
| CODE: |
| 10 |
|
| { |
| 11 |
|
| int rv, id, half_ks; |
| 12 |
34 |
| STRLEN k_len = 0, half = 0; |
| 13 |
34 |
| unsigned char *k = NULL; |
| 14 |
|
| char *cipher_name; |
| 15 |
|
| |
| 16 |
34 |
▶ | if (!SvPOK_spec(cipher)) croak("FATAL: cipher name must be string scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(cipher)) croak("FATAL: cipher name must be string scalar"); |
34 |
0 |
34 |
| if (!SvPOK_spec(cipher)) croak("FATAL: cipher name must be string scalar"); |
0 |
34 |
34 |
| if (!SvPOK_spec(cipher)) croak("FATAL: cipher name must be string scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(cipher)) croak("FATAL: cipher name must be string scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(cipher)) croak("FATAL: cipher name must be string scalar"); |
0 |
0 |
0 |
|
| 17 |
34 |
| cipher_name = SvPVbyte_nolen(cipher); |
| 18 |
34 |
▶ | 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"); |
34 |
0 |
34 |
| if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); |
0 |
34 |
34 |
| 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 |
|
| 19 |
34 |
| k = (unsigned char *) SvPVbyte(key, k_len); |
| 20 |
|
| |
| 21 |
34 |
| id = cryptx_internal_find_cipher(cipher_name); |
| 22 |
34 |
▶ | if (id == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (id == -1) { |
2 |
32 |
34 |
|
| 23 |
2 |
▶ | if (k_len > 0 && k_len < 100 && memchr(k, 0, k_len) == NULL && cryptx_internal_find_cipher((const char *)k) != -1) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (k_len > 0 && k_len < 100 && memchr(k, 0, k_len) == NULL && cryptx_internal_find_cipher((const char *)k) != -1) { |
2 |
0 |
2 |
| if (k_len > 0 && k_len < 100 && memchr(k, 0, k_len) == NULL && cryptx_internal_find_cipher((const char *)k) != -1) { |
2 |
0 |
2 |
| if (k_len > 0 && k_len < 100 && memchr(k, 0, k_len) == NULL && cryptx_internal_find_cipher((const char *)k) != -1) { |
2 |
0 |
2 |
| if (k_len > 0 && k_len < 100 && memchr(k, 0, k_len) == NULL && cryptx_internal_find_cipher((const char *)k) != -1) { |
1 |
1 |
2 |
|
| 24 |
1 |
| croak("FATAL: find_cipher failed for '%s' (did you mean new(cipher, key)?)", cipher_name); |
| 25 |
|
| } |
| 26 |
1 |
| croak("FATAL: find_cipher failed for '%s'", cipher_name); |
| 27 |
|
| } |
| 28 |
32 |
▶ | if (cipher_descriptor[id].block_length != 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cipher_descriptor[id].block_length != 16) { |
1 |
31 |
32 |
|
| 29 |
1 |
| croak("FATAL: XTS requires a 128-bit block cipher"); |
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
31 |
▶ | if (k_len == 0 || k_len % 2 != 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (k_len == 0 || k_len % 2 != 0) { |
31 |
0 |
31 |
| if (k_len == 0 || k_len % 2 != 0) { |
1 |
30 |
31 |
|
| 33 |
1 |
| croak("FATAL: XTS key must be key1 || key2, two equal-length '%s' keys (got %d bytes)", cipher_name, (int)k_len); |
| 34 |
|
| } |
| 35 |
30 |
| half = k_len / 2; |
| 36 |
30 |
| half_ks = (int)half; |
| 37 |
30 |
▶ | if (cipher_descriptor[id].keysize(&half_ks) != CRYPT_OK || half_ks != (int)half) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (cipher_descriptor[id].keysize(&half_ks) != CRYPT_OK || half_ks != (int)half) { |
30 |
0 |
30 |
| if (cipher_descriptor[id].keysize(&half_ks) != CRYPT_OK || half_ks != (int)half) { |
1 |
29 |
30 |
|
| 38 |
1 |
| croak("FATAL: XTS key must be key1 || key2, two equal-length '%s' keys (%d is not a valid key size)", cipher_name, (int)half); |
| 39 |
|
| } |
| 40 |
29 |
▶ | if (mem_neq(k, k + half, half) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mem_neq(k, k + half, half) == 0) { |
1 |
28 |
29 |
|
| 41 |
1 |
| croak("FATAL: XTS requires key1 != key2 (FIPS 140 IG A.9)"); |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
28 |
| Newz(0, RETVAL, 1, struct xts_struct); |
| 45 |
28 |
▶ | if (!RETVAL) croak("FATAL: Newz failed"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!RETVAL) croak("FATAL: Newz failed"); |
0 |
28 |
28 |
|
| 46 |
28 |
| rv = xts_start(id, k, k + half, (unsigned long)half, rounds, &RETVAL->state); |
| 47 |
28 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
28 |
28 |
|
| 48 |
0 |
| Safefree(RETVAL); |
| 49 |
0 |
| croak("FATAL: xts_start failed: %s", error_to_string(rv)); |
| 50 |
|
| } |
| 51 |
|
| } |
| 52 |
|
| OUTPUT: |
| 53 |
|
| RETVAL |
| 54 |
|
| |
| 55 |
|
| void |
| 56 |
|
| DESTROY(Crypt::Mode::XTS self) |
| 57 |
|
| CODE: |
| 58 |
|
| { |
| 59 |
28 |
| xts_done(&self->state); |
| 60 |
28 |
| zeromem(&self->state, sizeof(symmetric_xts)); |
| 61 |
28 |
| Safefree(self); |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| SV * |
| 65 |
|
| encrypt(Crypt::Mode::XTS self, SV * data, SV * tweak) |
| 66 |
|
| ALIAS: |
| 67 |
|
| decrypt = 1 |
| 68 |
|
| CODE: |
| 69 |
|
| { |
| 70 |
|
| int rv; |
| 71 |
98 |
| STRLEN in_len = 0, t_len = 0; |
| 72 |
|
| unsigned char *in_data, *out_data, *t_raw; |
| 73 |
|
| unsigned char t[16]; |
| 74 |
|
| |
| 75 |
98 |
▶ | if (!SvPOK_spec(tweak)) croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(tweak)) croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
97 |
1 |
98 |
| if (!SvPOK_spec(tweak)) croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
0 |
97 |
97 |
| if (!SvPOK_spec(tweak)) croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
0 |
0 |
0 |
| if (!SvPOK_spec(tweak)) croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
0 |
0 |
0 |
| if (!SvPOK_spec(tweak)) croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
0 |
0 |
0 |
|
| 76 |
97 |
| t_raw = (unsigned char *) SvPVbyte(tweak, t_len); |
| 77 |
97 |
▶ | if (t_len == 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t_len == 16) { |
22 |
75 |
97 |
|
| 78 |
22 |
| Copy(t_raw, t, 16, unsigned char); |
| 79 |
|
| } |
| 80 |
|
| else { |
| 81 |
|
| /* data unit sequence number: 64-bit little-endian, zero-padded to 16 bytes */ |
| 82 |
75 |
| UV unit = 0; |
| 83 |
|
| int i, flags; |
| 84 |
75 |
| flags = grok_number((const char *)t_raw, t_len, &unit); |
| 85 |
75 |
▶ | if (!(flags & IS_NUMBER_IN_UV) || (flags & (IS_NUMBER_NEG|IS_NUMBER_NOT_INT))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(flags & IS_NUMBER_IN_UV) || (flags & (IS_NUMBER_NEG|IS_NUMBER_NOT_INT))) { |
72 |
3 |
75 |
| if (!(flags & IS_NUMBER_IN_UV) || (flags & (IS_NUMBER_NEG|IS_NUMBER_NOT_INT))) { |
1 |
71 |
72 |
|
| 86 |
4 |
| croak("FATAL: tweak must be a 16-byte string or a non-negative integer < 2^64"); |
| 87 |
|
| } |
| 88 |
1207 |
▶ | for (i = 0; i < 16; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) { |
1136 |
71 |
1207 |
|
| 89 |
1136 |
| t[i] = (unsigned char)(unit & 0xFF); |
| 90 |
1136 |
| unit >>= 8; |
| 91 |
|
| } |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
93 |
▶ | if (!SvPOK_spec(data)) croak("FATAL: data must be string/buffer scalar"); |
Branch: 5 branches
| Branch | True | False | Total |
| if (!SvPOK_spec(data)) croak("FATAL: data must be string/buffer scalar"); |
93 |
0 |
93 |
| if (!SvPOK_spec(data)) croak("FATAL: data must be string/buffer scalar"); |
0 |
93 |
93 |
| if (!SvPOK_spec(data)) croak("FATAL: data must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(data)) croak("FATAL: data must be string/buffer scalar"); |
0 |
0 |
0 |
| if (!SvPOK_spec(data)) croak("FATAL: data must be string/buffer scalar"); |
0 |
0 |
0 |
|
| 95 |
93 |
| in_data = (unsigned char *) SvPVbyte(data, in_len); |
| 96 |
93 |
▶ | if (in_len < 16) croak("FATAL: XTS data unit must be at least 16 bytes (one full block)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_len < 16) croak("FATAL: XTS data unit must be at least 16 bytes (one full block)"); |
1 |
92 |
93 |
|
| 97 |
92 |
▶ | if (in_len > 16777216) croak("FATAL: XTS data unit must not exceed 2^20 blocks / 16MiB (NIST SP 800-38E)"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_len > 16777216) croak("FATAL: XTS data unit must not exceed 2^20 blocks / 16MiB (NIST SP 800-38E)"); |
1 |
91 |
92 |
|
| 98 |
|
| |
| 99 |
91 |
| RETVAL = newSVpvn("", 0); |
| 100 |
91 |
▶ | out_data = (unsigned char *) SvGROW(RETVAL, in_len + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| out_data = (unsigned char *) SvGROW(RETVAL, in_len + 1); |
91 |
0 |
91 |
| out_data = (unsigned char *) SvGROW(RETVAL, in_len + 1); |
91 |
0 |
91 |
|
| 101 |
91 |
▶ | if (ix == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ix == 1) { |
37 |
54 |
91 |
|
| 102 |
37 |
| rv = xts_decrypt(in_data, (unsigned long)in_len, out_data, t, &self->state); |
| 103 |
|
| } |
| 104 |
|
| else { |
| 105 |
54 |
| rv = xts_encrypt(in_data, (unsigned long)in_len, out_data, t, &self->state); |
| 106 |
|
| } |
| 107 |
91 |
▶ | if (rv != CRYPT_OK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rv != CRYPT_OK) { |
0 |
91 |
91 |
|
| 108 |
0 |
| SvREFCNT_dec(RETVAL); |
| 109 |
0 |
▶ | croak("FATAL: xts_%scrypt failed: %s", ix == 1 ? "de" : "en", error_to_string(rv)); |
Branch: 1 branch
| Branch | True | False | Total |
| croak("FATAL: xts_%scrypt failed: %s", ix == 1 ? "de" : "en", error_to_string(rv)); |
0 |
0 |
0 |
|
| 110 |
|
| } |
| 111 |
91 |
| SvCUR_set(RETVAL, in_len); |
| 112 |
91 |
| *SvEND(RETVAL) = '\0'; |
| 113 |
|
| } |
| 114 |
|
| OUTPUT: |
| 115 |
|
| RETVAL |