line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
PROTOTYPES: DISABLE |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Crypt::AuthEnc::GCM |
6
|
|
|
|
|
|
|
new(Class, char * cipher_name, SV * key, SV * nonce = NULL) |
7
|
|
|
|
|
|
|
CODE: |
8
|
|
|
|
|
|
|
{ |
9
|
15
|
|
|
|
|
|
STRLEN k_len = 0, iv_len = 0; |
10
|
15
|
|
|
|
|
|
unsigned char *k = NULL, *iv = NULL; |
11
|
|
|
|
|
|
|
int id, rv; |
12
|
|
|
|
|
|
|
|
13
|
15
|
50
|
|
|
|
|
if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); |
14
|
15
|
50
|
|
|
|
|
k = (unsigned char *) SvPVbyte(key, k_len); |
15
|
15
|
50
|
|
|
|
|
if (nonce) { |
16
|
0
|
0
|
|
|
|
|
if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); |
17
|
0
|
0
|
|
|
|
|
iv = (unsigned char *)SvPVbyte(nonce, iv_len); |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
15
|
|
|
|
|
|
id = cryptx_internal_find_cipher(cipher_name); |
21
|
15
|
50
|
|
|
|
|
if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); |
22
|
|
|
|
|
|
|
|
23
|
15
|
|
|
|
|
|
Newz(0, RETVAL, 1, gcm_state); |
24
|
15
|
50
|
|
|
|
|
if (!RETVAL) croak("FATAL: Newz failed"); |
25
|
|
|
|
|
|
|
|
26
|
15
|
|
|
|
|
|
rv = gcm_init(RETVAL, id, k, (unsigned long)k_len); |
27
|
15
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
28
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
29
|
0
|
|
|
|
|
|
croak("FATAL: gcm_init failed: %s", error_to_string(rv)); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
15
|
50
|
|
|
|
|
if (iv && iv_len > 0) { |
|
|
0
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
rv = gcm_add_iv(RETVAL, iv, (unsigned long)iv_len); |
34
|
0
|
0
|
|
|
|
|
if (rv != CRYPT_OK) { |
35
|
0
|
|
|
|
|
|
Safefree(RETVAL); |
36
|
0
|
|
|
|
|
|
croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
OUTPUT: |
41
|
|
|
|
|
|
|
RETVAL |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
void |
44
|
|
|
|
|
|
|
DESTROY(Crypt::AuthEnc::GCM self) |
45
|
|
|
|
|
|
|
CODE: |
46
|
15
|
|
|
|
|
|
Safefree(self); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Crypt::AuthEnc::GCM |
49
|
|
|
|
|
|
|
clone(Crypt::AuthEnc::GCM self) |
50
|
|
|
|
|
|
|
CODE: |
51
|
0
|
|
|
|
|
|
Newz(0, RETVAL, 1, gcm_state); |
52
|
0
|
0
|
|
|
|
|
if (!RETVAL) croak("FATAL: Newz failed"); |
53
|
0
|
|
|
|
|
|
Copy(self, RETVAL, 1, gcm_state); |
54
|
|
|
|
|
|
|
OUTPUT: |
55
|
|
|
|
|
|
|
RETVAL |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
void |
58
|
|
|
|
|
|
|
reset(Crypt::AuthEnc::GCM self) |
59
|
|
|
|
|
|
|
PPCODE: |
60
|
|
|
|
|
|
|
{ |
61
|
|
|
|
|
|
|
int rv; |
62
|
0
|
|
|
|
|
|
rv = gcm_reset(self); |
63
|
0
|
0
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv)); |
64
|
0
|
0
|
|
|
|
|
XPUSHs(ST(0)); /* return self */ |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
SV * |
68
|
|
|
|
|
|
|
encrypt_add(Crypt::AuthEnc::GCM self, SV * data) |
69
|
|
|
|
|
|
|
CODE: |
70
|
|
|
|
|
|
|
{ |
71
|
|
|
|
|
|
|
int rv; |
72
|
|
|
|
|
|
|
STRLEN in_data_len; |
73
|
|
|
|
|
|
|
unsigned char *in_data, *out_data; |
74
|
|
|
|
|
|
|
|
75
|
8
|
50
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
76
|
8
|
50
|
|
|
|
|
if (in_data_len == 0) { |
77
|
0
|
|
|
|
|
|
RETVAL = newSVpvn("", 0); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else |
80
|
|
|
|
|
|
|
{ |
81
|
8
|
|
|
|
|
|
RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
82
|
8
|
|
|
|
|
|
SvPOK_only(RETVAL); |
83
|
8
|
|
|
|
|
|
SvCUR_set(RETVAL, in_data_len); |
84
|
8
|
|
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL); |
85
|
8
|
|
|
|
|
|
rv = gcm_process(self, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT); |
86
|
8
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
87
|
0
|
|
|
|
|
|
SvREFCNT_dec(RETVAL); |
88
|
0
|
|
|
|
|
|
croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
OUTPUT: |
93
|
|
|
|
|
|
|
RETVAL |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
void |
96
|
|
|
|
|
|
|
iv_add(Crypt::AuthEnc::GCM self, SV * data) |
97
|
|
|
|
|
|
|
PPCODE: |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
int rv; |
100
|
|
|
|
|
|
|
STRLEN in_data_len; |
101
|
|
|
|
|
|
|
unsigned char *in_data; |
102
|
|
|
|
|
|
|
|
103
|
15
|
50
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
104
|
15
|
|
|
|
|
|
rv = gcm_add_iv(self, in_data, (unsigned long)in_data_len); |
105
|
15
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); |
106
|
15
|
50
|
|
|
|
|
XPUSHs(ST(0)); /* return self */ |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
void |
110
|
|
|
|
|
|
|
adata_add(Crypt::AuthEnc::GCM self, SV * data) |
111
|
|
|
|
|
|
|
PPCODE: |
112
|
|
|
|
|
|
|
{ |
113
|
|
|
|
|
|
|
int rv; |
114
|
|
|
|
|
|
|
STRLEN in_data_len; |
115
|
|
|
|
|
|
|
unsigned char *in_data; |
116
|
|
|
|
|
|
|
|
117
|
15
|
50
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
118
|
15
|
|
|
|
|
|
rv = gcm_add_aad(self, in_data, (unsigned long)in_data_len); |
119
|
15
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv)); |
120
|
15
|
50
|
|
|
|
|
XPUSHs(ST(0)); /* return self */ |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
SV * |
124
|
|
|
|
|
|
|
decrypt_add(Crypt::AuthEnc::GCM self, SV * data) |
125
|
|
|
|
|
|
|
CODE: |
126
|
|
|
|
|
|
|
{ |
127
|
|
|
|
|
|
|
int rv; |
128
|
|
|
|
|
|
|
STRLEN in_data_len; |
129
|
|
|
|
|
|
|
unsigned char *in_data, *out_data; |
130
|
|
|
|
|
|
|
|
131
|
27
|
100
|
|
|
|
|
in_data = (unsigned char *)SvPVbyte(data, in_data_len); |
132
|
27
|
50
|
|
|
|
|
if (in_data_len == 0) { |
133
|
0
|
|
|
|
|
|
RETVAL = newSVpvn("", 0); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
else { |
136
|
27
|
|
|
|
|
|
RETVAL = NEWSV(0, in_data_len); /* avoid zero! */ |
137
|
27
|
|
|
|
|
|
SvPOK_only(RETVAL); |
138
|
27
|
|
|
|
|
|
SvCUR_set(RETVAL, in_data_len); |
139
|
27
|
|
|
|
|
|
out_data = (unsigned char *)SvPVX(RETVAL); |
140
|
27
|
|
|
|
|
|
rv = gcm_process(self, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT); |
141
|
27
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
142
|
0
|
|
|
|
|
|
SvREFCNT_dec(RETVAL); |
143
|
0
|
|
|
|
|
|
croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
OUTPUT: |
148
|
|
|
|
|
|
|
RETVAL |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
void |
152
|
|
|
|
|
|
|
encrypt_done(Crypt::AuthEnc::GCM self) |
153
|
|
|
|
|
|
|
PPCODE: |
154
|
|
|
|
|
|
|
{ |
155
|
|
|
|
|
|
|
int rv; |
156
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
157
|
7
|
|
|
|
|
|
unsigned long tag_len = sizeof(tag); |
158
|
|
|
|
|
|
|
|
159
|
7
|
|
|
|
|
|
rv = gcm_done(self, tag, &tag_len); |
160
|
7
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); |
161
|
7
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
void |
165
|
|
|
|
|
|
|
decrypt_done(Crypt::AuthEnc::GCM self, ...) |
166
|
|
|
|
|
|
|
PPCODE: |
167
|
|
|
|
|
|
|
{ |
168
|
|
|
|
|
|
|
int rv; |
169
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
170
|
8
|
|
|
|
|
|
unsigned long tag_len = sizeof(tag); |
171
|
|
|
|
|
|
|
STRLEN expected_tag_len; |
172
|
|
|
|
|
|
|
unsigned char *expected_tag; |
173
|
|
|
|
|
|
|
|
174
|
8
|
|
|
|
|
|
rv = gcm_done(self, tag, &tag_len); |
175
|
8
|
50
|
|
|
|
|
if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); |
176
|
8
|
50
|
|
|
|
|
if (items == 1) { |
177
|
8
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
else { |
180
|
0
|
0
|
|
|
|
|
if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); |
181
|
0
|
0
|
|
|
|
|
expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); |
182
|
0
|
0
|
|
|
|
|
if (expected_tag_len!=tag_len) { |
183
|
0
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
184
|
|
|
|
|
|
|
} |
185
|
0
|
0
|
|
|
|
|
else if (memNE(expected_tag, tag, tag_len)) { |
186
|
0
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newSViv(0))); /* false */ |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
else { |
189
|
0
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newSViv(1))); /* true */ |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
void |
195
|
|
|
|
|
|
|
gcm_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *header = NULL, SV *plaintext) |
196
|
|
|
|
|
|
|
PPCODE: |
197
|
|
|
|
|
|
|
{ |
198
|
8
|
|
|
|
|
|
STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0; |
199
|
8
|
|
|
|
|
|
unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL; |
200
|
|
|
|
|
|
|
int rv, id; |
201
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
202
|
8
|
|
|
|
|
|
unsigned long tag_len = sizeof(tag); |
203
|
|
|
|
|
|
|
SV *output; |
204
|
|
|
|
|
|
|
|
205
|
8
|
50
|
|
|
|
|
if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len); |
|
|
50
|
|
|
|
|
|
206
|
8
|
50
|
|
|
|
|
if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len); |
|
|
50
|
|
|
|
|
|
207
|
8
|
50
|
|
|
|
|
if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len); |
|
|
50
|
|
|
|
|
|
208
|
8
|
50
|
|
|
|
|
if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len); |
|
|
50
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
8
|
|
|
|
|
|
id = cryptx_internal_find_cipher(cipher_name); |
211
|
8
|
50
|
|
|
|
|
if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); |
212
|
8
|
50
|
|
|
|
|
output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */ |
213
|
8
|
|
|
|
|
|
SvPOK_only(output); |
214
|
8
|
|
|
|
|
|
SvCUR_set(output, pt_len); |
215
|
|
|
|
|
|
|
|
216
|
8
|
|
|
|
|
|
rv = gcm_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len, |
217
|
8
|
|
|
|
|
|
pt, (unsigned long)pt_len, (unsigned char *)SvPVX(output), tag, &tag_len, GCM_ENCRYPT); |
218
|
|
|
|
|
|
|
|
219
|
8
|
50
|
|
|
|
|
if (rv != CRYPT_OK) { |
220
|
0
|
|
|
|
|
|
SvREFCNT_dec(output); |
221
|
0
|
|
|
|
|
|
croak("FATAL: ccm_memory failed: %s", error_to_string(rv)); |
222
|
|
|
|
|
|
|
} |
223
|
8
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(output)); |
224
|
8
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
void |
228
|
|
|
|
|
|
|
gcm_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv) |
229
|
|
|
|
|
|
|
PPCODE: |
230
|
|
|
|
|
|
|
{ |
231
|
10
|
|
|
|
|
|
STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0; |
232
|
10
|
|
|
|
|
|
unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL; |
233
|
|
|
|
|
|
|
int rv, id; |
234
|
|
|
|
|
|
|
unsigned char tag[MAXBLOCKSIZE]; |
235
|
|
|
|
|
|
|
unsigned long tag_len; |
236
|
|
|
|
|
|
|
SV *output; |
237
|
|
|
|
|
|
|
|
238
|
10
|
50
|
|
|
|
|
if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len); |
|
|
50
|
|
|
|
|
|
239
|
10
|
50
|
|
|
|
|
if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len); |
|
|
50
|
|
|
|
|
|
240
|
10
|
50
|
|
|
|
|
if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len); |
|
|
50
|
|
|
|
|
|
241
|
10
|
50
|
|
|
|
|
if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len); |
|
|
50
|
|
|
|
|
|
242
|
10
|
50
|
|
|
|
|
if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len); |
|
|
50
|
|
|
|
|
|
243
|
|
|
|
|
|
|
|
244
|
10
|
|
|
|
|
|
id = cryptx_internal_find_cipher(cipher_name); |
245
|
10
|
50
|
|
|
|
|
if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); |
246
|
10
|
50
|
|
|
|
|
output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */ |
247
|
10
|
|
|
|
|
|
SvPOK_only(output); |
248
|
10
|
|
|
|
|
|
SvCUR_set(output, ct_len); |
249
|
10
|
|
|
|
|
|
tag_len = (unsigned long)t_len; |
250
|
10
|
|
|
|
|
|
Copy(t, tag, t_len, unsigned char); |
251
|
|
|
|
|
|
|
|
252
|
10
|
|
|
|
|
|
rv = gcm_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len, |
253
|
10
|
|
|
|
|
|
(unsigned char *)SvPVX(output), (unsigned long)ct_len, ct, tag, &tag_len, GCM_DECRYPT); |
254
|
|
|
|
|
|
|
|
255
|
10
|
100
|
|
|
|
|
if (rv != CRYPT_OK) { |
256
|
2
|
|
|
|
|
|
SvREFCNT_dec(output); |
257
|
2
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */ |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
else { |
260
|
8
|
50
|
|
|
|
|
XPUSHs(sv_2mortal(output)); |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
} |