inc/CryptX_Cipher.xs.inc

filter: Statementstmt 91.2% 83 / 91 Branchbran 47.9% 69 / 144 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 64.6% 152 / 235 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« inc/CryptX_PK_DH.xs.inc ↑ Summary inc/CryptX_Mode_CFB.xs.inc »
1 MODULE = CryptX PACKAGE = Crypt::Cipher
2  
3 PROTOTYPES: DISABLE
4  
5 Crypt::Cipher
6 new(char * class, ...)
7 CODE:
8 {
9 STRLEN name_len;
10 STRLEN key_len;
11 3728 unsigned char *key_data = NULL;
12 SV *key;
13 SV *name_sv;
14 char *cipher_name;
15 3728 int rv, id, rounds = 0, idx;
16  
17 /* we need to handle:
18 Crypt::Cipher->new('AES');
19 Crypt::Cipher::AES->new();
20 */
21 3728 idx = strcmp("Crypt::Cipher", class) == 0 ? 1 : 0;
22 3728 ▶ if (idx + 1 > items) croak("FATAL: missing argument");
Branch: 1 branch
BranchTrueFalseTotal
if (idx + 1 > items) croak("FATAL: missing argument"); 0 3728 3728
23 3728 name_sv = ST(idx);
24 3728 ▶ if (!SvPOK_spec(name_sv)) croak("FATAL: invalid cipher name");
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(name_sv)) croak("FATAL: invalid cipher name"); 3728 0 3728
if (!SvPOK_spec(name_sv)) croak("FATAL: invalid cipher name"); 3 3725 3728
if (!SvPOK_spec(name_sv)) croak("FATAL: invalid cipher name"); 3 0 3
if (!SvPOK_spec(name_sv)) croak("FATAL: invalid cipher name"); 1 2 3
if (!SvPOK_spec(name_sv)) croak("FATAL: invalid cipher name"); 0 1 1
25 3726 cipher_name = SvPVbyte(name_sv, name_len);
26 3726 ▶ if (idx + 2 > items) croak("FATAL: missing key argument");
Branch: 1 branch
BranchTrueFalseTotal
if (idx + 2 > items) croak("FATAL: missing key argument"); 0 3726 3726
27 3726 key = ST(idx + 1);
28 3726 ▶ if (idx + 3 <= items) rounds = (int)SvIV(ST(idx + 2));
Branch: 1 branch
BranchTrueFalseTotal
if (idx + 3 <= items) rounds = (int)SvIV(ST(idx + 2)); 1134 2592 3726
29  
30 3726 ▶ if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar");
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 3726 0 3726
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 1 3725 3726
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 1 0 1
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 1 0 1
if (!SvPOK_spec(key)) croak("FATAL: key must be string/buffer scalar"); 0 1 1
31 3726 key_data = (unsigned char *)SvPVbyte(key, key_len);
32  
33 3726 id = cryptx_internal_find_cipher(cipher_name);
34 3726 ▶ if (id == -1) croak("FATAL: find_cipher failed for '%s'", cipher_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_cipher failed for '%s'", cipher_name); 1 3725 3726
35  
36 3725 Newz(0, RETVAL, 1, struct cipher_struct);
37 3725 ▶ if (!RETVAL) croak("FATAL: Newz failed");
Branch: 1 branch
BranchTrueFalseTotal
if (!RETVAL) croak("FATAL: Newz failed"); 0 3725 3725
38  
39 3725 RETVAL->desc = &cipher_descriptor[id];
40 3725 rv = RETVAL->desc->setup(key_data, (int)key_len, rounds, &RETVAL->skey);
41 3725 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 3725 3725
42 0 Safefree(RETVAL);
43 0 croak("FATAL: cipher setup failed: %s", error_to_string(rv));
44 }
45 }
46 OUTPUT:
47 RETVAL
48  
49 void
50 DESTROY(Crypt::Cipher self)
51 CODE:
52 {
53 3725 zeromem(&self->skey, sizeof(symmetric_key));
54 3725 Safefree(self);
55 }
56  
57 SV *
58 encrypt(Crypt::Cipher self, SV * data)
59 CODE:
60 {
61 int rv;
62 STRLEN len;
63 unsigned char *plaintext;
64  
65 122727 ▶ if (!SvPOK_spec(data)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 122727 0 122727
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 122727 122727
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 0 0
66 122727 plaintext = (unsigned char *)SvPVbyte(data, len);
67  
68 122727 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 1 122726 122727
69 1 RETVAL = newSVpvn("", 0);
70 }
71 122726 ▶ else if (len == (STRLEN)self->desc->block_length) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len == (STRLEN)self->desc->block_length) { 122726 0 122726
72 122726 RETVAL = NEWSV(0, len); /* avoid zero! */
73 122726 SvPOK_only(RETVAL);
74 122726 SvCUR_set(RETVAL, len);
75 122726 *SvEND(RETVAL) = '\0';
76 122726 rv = self->desc->ecb_encrypt(plaintext, (unsigned char *)SvPVX(RETVAL), &self->skey);
77 122726 ▶ if (rv!=CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv!=CRYPT_OK) { 0 122726 122726
78 0 SvREFCNT_dec(RETVAL);
79 0 croak("FATAL: encrypt failed: %s", error_to_string(rv));
80 }
81 }
82 else {
83 0 croak ("FATAL: input size not equal to blocksize (%d)", self->desc->block_length);
84 }
85 }
86 OUTPUT:
87 RETVAL
88  
89 SV *
90 decrypt(Crypt::Cipher self, SV * data)
91 CODE:
92 {
93 int rv;
94 STRLEN len;
95 unsigned char *ciphertext;
96  
97 703 ▶ if (!SvPOK_spec(data)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 703 0 703
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 703 703
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(data)) XSRETURN_UNDEF; 0 0 0
98 703 ciphertext = (unsigned char *)SvPVbyte(data, len);
99  
100 703 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 1 702 703
101 1 RETVAL = newSVpvn("", 0);
102 }
103 702 ▶ else if (len == (STRLEN)self->desc->block_length) {
Branch: 1 branch
BranchTrueFalseTotal
else if (len == (STRLEN)self->desc->block_length) { 702 0 702
104 702 RETVAL = NEWSV(0, len); /* avoid zero! */
105 702 SvPOK_only(RETVAL);
106 702 SvCUR_set(RETVAL, len);
107 702 *SvEND(RETVAL) = '\0';
108 702 rv = self->desc->ecb_decrypt(ciphertext, (unsigned char *)SvPVX(RETVAL), &self->skey);
109 702 ▶ if (rv!=CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv!=CRYPT_OK) { 0 702 702
110 0 SvREFCNT_dec(RETVAL);
111 0 croak("FATAL: decrypt failed: %s", error_to_string(rv));
112 }
113 }
114 else {
115 0 croak ("FATAL: input size not equal to blocksize (%d)", self->desc->block_length);
116 }
117 }
118 OUTPUT:
119 RETVAL
120  
121 int
122 blocksize(SV * param, char * extra = NULL)
123 CODE:
124 {
125 233 ▶ if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 28 205 233
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 28 0 28
126 28 IV tmp = SvIV((SV*)SvRV(param));
127 28 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
128 28 RETVAL = obj->desc->block_length;
129 }
130 else {
131 STRLEN plen;
132 548 ▶ char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra;
Branch: 6 branches
BranchTrueFalseTotal
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 205 0 205
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 205 205
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 138 67 205
133 205 int rv, id = cryptx_internal_find_cipher(name);
134 205 ▶ if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_cipher failed for '%s'", name); 0 205 205
135 205 rv = cipher_descriptor[id].block_length;
136 205 ▶ if (!rv) croak("FATAL: invalid block_length for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!rv) croak("FATAL: invalid block_length for '%s'", name); 0 205 205
137 205 RETVAL = rv;
138 }
139 }
140 OUTPUT:
141 RETVAL
142  
143 int
144 max_keysize(SV * param, char * extra = NULL)
145 CODE:
146 {
147 335 ▶ if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 54 281 335
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 54 0 54
148 54 IV tmp = SvIV((SV*)SvRV(param));
149 54 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
150 54 RETVAL = obj->desc->max_key_length;
151 }
152 else {
153 STRLEN plen;
154 789 ▶ char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra;
Branch: 6 branches
BranchTrueFalseTotal
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 281 0 281
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 281 281
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 227 54 281
155 281 int rv, id = cryptx_internal_find_cipher(name);
156 281 ▶ if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_cipher failed for '%s'", name); 0 281 281
157 281 rv = cipher_descriptor[id].max_key_length;
158 281 ▶ if (!rv) croak("FATAL: invalid max_key_length for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!rv) croak("FATAL: invalid max_key_length for '%s'", name); 0 281 281
159 281 RETVAL = rv;
160 }
161 }
162 OUTPUT:
163 RETVAL
164  
165 int
166 min_keysize(SV * param, char * extra = NULL)
167 CODE:
168 {
169 165 ▶ if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 27 138 165
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 27 0 27
170 27 IV tmp = SvIV((SV*)SvRV(param));
171 27 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
172 27 RETVAL = obj->desc->min_key_length;
173 }
174 else {
175 STRLEN plen;
176 387 ▶ char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra;
Branch: 6 branches
BranchTrueFalseTotal
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 138 0 138
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 138 138
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 111 27 138
177 138 int rv, id = cryptx_internal_find_cipher(name);
178 138 ▶ if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_cipher failed for '%s'", name); 0 138 138
179 138 rv = cipher_descriptor[id].min_key_length;
180 138 ▶ if (!rv) croak("FATAL: invalid min_key_length for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!rv) croak("FATAL: invalid min_key_length for '%s'", name); 0 138 138
181 138 RETVAL = rv;
182 }
183 }
184 OUTPUT:
185 RETVAL
186  
187 int
188 default_rounds(SV * param, char * extra = NULL)
189 CODE:
190 {
191 162 ▶ if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
Branch: 2 branches
BranchTrueFalseTotal
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 27 135 162
if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) { 27 0 27
192 27 IV tmp = SvIV((SV*)SvRV(param));
193 27 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
194 27 RETVAL = obj->desc->default_rounds;
195 }
196 else {
197 STRLEN plen;
198 378 ▶ char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra;
Branch: 6 branches
BranchTrueFalseTotal
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 135 0 135
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 135 135
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 0 0 0
char *name = SvPOK_spec(param) && strcmp(SvPVbyte(param, plen), "Crypt::Cipher") ? SvPVbyte(param, plen) : extra; 108 27 135
199 135 int rv, id = cryptx_internal_find_cipher(name);
200 135 ▶ if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_cipher failed for '%s'", name); 0 135 135
201 135 rv = cipher_descriptor[id].default_rounds;
202 135 ▶ if (!rv) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!rv) XSRETURN_UNDEF; 0 135 135
203 135 RETVAL = rv;
204 }
205 }
206 OUTPUT:
207 RETVAL
« inc/CryptX_PK_DH.xs.inc ↑ Summary inc/CryptX_Mode_CFB.xs.inc »