AES128.xs

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 42 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary tiny-AES-c/aes.c »
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 0 void make_aes_key(char *key, char *secret, size_t secret_size)
13 {
14 uint8_t i, m, n;
15 // make a 32 bytes key from secret.
16 0 ▶ if(secret_size >= 32) {
Branch: 1 branch
BranchTrueFalseTotal
if(secret_size >= 32) { 0 0 0
17 0 memcpy(key, secret, 32);
18 }
19 else {
20 0 m = 32 % secret_size;
21 0 n = (32 - m) / secret_size;
22 0 ▶ for(i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < n; i++) { 0 0 0
23 0 memcpy(key + secret_size * i, secret, secret_size);
24 }
25 0 memcpy(key + secret_size * i, secret, m);
26 }
27 0 }
28  
29 MODULE = AES128 PACKAGE = AES128
30  
31 TYPEMAP: <<END
32 const char * T_PV
33 const uint8_t * T_PV
34 uint8_t * T_PV
35 END
36  
37 SV *
38 AES128_CTR_encrypt(SV *sv_plain_text, SV *sv_secret)
39 CODE:
40 STRLEN text_size, secret_size;
41 uint8_t i;
42 struct AES_ctx ctx;
43 char *plain_text, *secret, *output;
44  
45 0 ▶ plain_text = (char *)SvPVbyte(sv_plain_text, text_size);
Branch: 1 branch
BranchTrueFalseTotal
plain_text = (char *)SvPVbyte(sv_plain_text, text_size); 0 0 0
46 0 secret = (char *)SvPVbyte(sv_secret, secret_size);
47 char key[32];
48 0 make_aes_key(key, secret, secret_size);
49  
50 0 uint8_t padding_len = 16 - text_size % 16;
51  
52 0 output = (char *)malloc(text_size + padding_len);
53 0 memcpy(output, plain_text, text_size);
54 0 ▶ for(i = 0; i < padding_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < padding_len; i++) 0 0 0
55 0 output[text_size + i] = padding_len;
56  
57 0 AES_init_ctx_iv(&ctx, key, key + 16);
58 0 AES_CTR_xcrypt_buffer(&ctx, output, text_size + padding_len);
59 0 RETVAL = newSVpv(output, text_size + padding_len);
60 0 free(output);
61 OUTPUT:
62 RETVAL
63  
64 SV *
65 AES128_CTR_decrypt(SV *sv_cipher_text, SV *sv_secret)
66 CODE:
67 STRLEN text_size, secret_size;
68 char *cipher_text, *secret;
69 struct AES_ctx ctx;
70 char key[32];
71  
72 0 ▶ cipher_text = (char *)SvPVbyte(sv_cipher_text, text_size);
Branch: 1 branch
BranchTrueFalseTotal
cipher_text = (char *)SvPVbyte(sv_cipher_text, text_size); 0 0 0
73 0 secret = (char *)SvPVbyte(sv_secret, secret_size);
74 0 ▶ if(text_size % 16 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if(text_size % 16 != 0) 0 0 0
75 0 croak("Corrupted cipher text!");
76  
77 0 make_aes_key(key, secret, secret_size);
78  
79 0 AES_init_ctx_iv(&ctx, key, key + 16);
80 0 AES_CTR_xcrypt_buffer(&ctx, cipher_text, text_size);
81 0 uint8_t padding_len = cipher_text[text_size -1];
82 0 RETVAL = newSVpv(cipher_text, text_size - padding_len);
83 OUTPUT:
84 RETVAL
↑ Summary tiny-AES-c/aes.c »