-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp_tomcrypt_crypt_lrw.h
66 lines (55 loc) · 1.73 KB
/
php_tomcrypt_crypt_lrw.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
65
#include <tomcrypt.h>
#include "php_tomcrypt_compat.h"
#include "php_tomcrypt_crypt.h"
static void php_tomcrypt_xcrypt_lrw(PLTC_CRYPT_PARAM)
{
#ifdef LTC_LRW_MODE
symmetric_LRW ctx;
char *output, *iv, *tweak;
pltc_size iv_len, tweak_len;
pltc_long num_rounds;
int err;
GET_OPT_STRING(options, "iv", iv, iv_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 (iv_len != cipher_descriptor[cipher].block_length) {
efree(output);
TOMCRYPT_G(last_error) = CRYPT_INVALID_ARG;
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid iv size (%d), expected %d", iv_len, cipher_descriptor[cipher].block_length);
RETURN_FALSE;
}
/* LRW 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 = lrw_start(cipher, iv, key, key_len, tweak, num_rounds, &ctx)) != CRYPT_OK) {
goto error;
}
if (direction == PLTC_ENCRYPT) {
err = lrw_encrypt(input, output, input_len, &ctx);
} else {
err = lrw_decrypt(input, output, input_len, &ctx);
}
if (err != CRYPT_OK) {
goto error;
}
if ((err = lrw_done(&ctx)) != CRYPT_OK) {
goto error;
}
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
}