line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#define PERL_NO_GET_CONTEXT |
2
|
|
|
|
|
|
|
#include "EXTERN.h" |
3
|
|
|
|
|
|
|
#include "perl.h" |
4
|
|
|
|
|
|
|
#include "XSUB.h" |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
#include "ppport.h" |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "aes.h" |
9
|
|
|
|
|
|
|
#include "aes.c" |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
MODULE = AES128 PACKAGE = AES128 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
TYPEMAP: <
|
15
|
|
|
|
|
|
|
const char * T_PV |
16
|
|
|
|
|
|
|
const uint8_t * T_PV |
17
|
|
|
|
|
|
|
uint8_t * T_PV |
18
|
|
|
|
|
|
|
END |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
SV * |
21
|
|
|
|
|
|
|
AES128_CTR_encrypt(SV *sv_plain_text, SV *sv_secret) |
22
|
|
|
|
|
|
|
CODE: |
23
|
|
|
|
|
|
|
STRLEN text_size, secret_size; |
24
|
|
|
|
|
|
|
uint8_t i; |
25
|
|
|
|
|
|
|
struct AES_ctx ctx; |
26
|
|
|
|
|
|
|
char *plain_text, *secret, *output; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
plain_text = (char *)SvPVbyte(sv_plain_text, text_size); |
29
|
0
|
0
|
|
|
|
|
secret = (char *)SvPVbyte(sv_secret, secret_size); |
30
|
0
|
0
|
|
|
|
|
if(secret_size != 32) |
31
|
0
|
|
|
|
|
|
croak("Secret size MUST be 32 bytes long!"); |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
uint8_t padding_len = 16 - text_size % 16; |
34
|
|
|
|
|
|
|
|
35
|
0
|
|
|
|
|
|
output = (char *)malloc(text_size + padding_len); |
36
|
0
|
|
|
|
|
|
memcpy(output, plain_text, text_size); |
37
|
0
|
0
|
|
|
|
|
for(i = 0; i < padding_len; i++) |
38
|
0
|
|
|
|
|
|
output[text_size + i] = padding_len; |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
AES_init_ctx_iv(&ctx, secret, secret + 16); |
41
|
0
|
|
|
|
|
|
AES_CTR_xcrypt_buffer(&ctx, output, text_size + padding_len); |
42
|
0
|
|
|
|
|
|
RETVAL = newSVpv(output, text_size + padding_len); |
43
|
0
|
|
|
|
|
|
free(output); |
44
|
|
|
|
|
|
|
OUTPUT: |
45
|
|
|
|
|
|
|
RETVAL |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
SV * |
48
|
|
|
|
|
|
|
AES128_CTR_decrypt(SV *sv_cipher_text, SV *sv_secret) |
49
|
|
|
|
|
|
|
CODE: |
50
|
|
|
|
|
|
|
STRLEN text_size, secret_size; |
51
|
|
|
|
|
|
|
char *cipher_text, *secret; |
52
|
|
|
|
|
|
|
struct AES_ctx ctx; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
cipher_text = (char *)SvPVbyte(sv_cipher_text, text_size); |
55
|
0
|
0
|
|
|
|
|
secret = (char *)SvPVbyte(sv_secret, secret_size); |
56
|
0
|
0
|
|
|
|
|
if(text_size % 16 != 0 || secret_size != 32) |
|
|
0
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
croak("Invalid cipher text or secret size"); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
AES_init_ctx_iv(&ctx, secret, secret + 16); |
60
|
0
|
|
|
|
|
|
AES_CTR_xcrypt_buffer(&ctx, cipher_text, text_size); |
61
|
0
|
|
|
|
|
|
uint8_t padding_len = cipher_text[text_size -1]; |
62
|
0
|
|
|
|
|
|
RETVAL = newSVpv(cipher_text, text_size - padding_len); |
63
|
|
|
|
|
|
|
OUTPUT: |
64
|
|
|
|
|
|
|
RETVAL |