-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_tomcrypt_crypt_xts.h
65 lines (54 loc) · 1.76 KB
/
php_tomcrypt_crypt_xts.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <tomcrypt.h>
#include "php_tomcrypt_compat.h"
#include "php_tomcrypt_crypt.h"
static void php_tomcrypt_xcrypt_xts(PLTC_CRYPT_PARAM)
{
#ifdef LTC_XTS_MODE
symmetric_xts ctx;
char *output, *key2, *tweak;
int err;
pltc_size key2_len, tweak_len;
pltc_long num_rounds;
GET_OPT_STRING(options, "key2", key2, key2_len, NULL);
GET_OPT_LONG(options, "rounds", num_rounds, 0);
GET_OPT_STRING(options, "tweak", tweak, tweak_len, NULL);
output = emalloc(input_len + 1);
output[input_len] = '\0';
if (key2_len != key_len) {
efree(output);
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid length for key2 (%d), expected %d", key2_len, key_len);
RETURN_FALSE;
}
/* XTS uses a fixed-length tweak value. */
if (tweak_len != 16) {
efree(output);
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid tweak size (%d), expected %d", tweak_len, 16);
RETURN_FALSE;
}
if ((err = xts_start(cipher, key, key2, key_len, num_rounds, &ctx)) != CRYPT_OK) {
goto error;
}
/* API inconsistency: input_len & output are swapped compared to other XXX_encrypt() functions. */
if (direction == PLTC_ENCRYPT) {
err = xts_encrypt(input, input_len, output, tweak, &ctx);
} else {
err = xts_decrypt(input, input_len, output, tweak, &ctx);
}
if (err != CRYPT_OK) {
goto error;
}
xts_done(&ctx);
PLTC_RETURN_STRINGL(output, input_len, 0);
error:
efree(output);
TOMCRYPT_G(last_error) = err;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", error_to_string(err));
RETURN_FALSE;
#else
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported mode");
RETURN_FALSE;
#endif
}