Package list libcryptx-perl / fa5f52b
New upstream version 0.059 gregor herrmann 3 years ago
334 changed file(s) with 11496 addition(s) and 7956 deletion(s). Raw diff Collapse all Expand all
00 Changes for CryptX
1
2 0.059 2018-03-25
3 - new Crypt::Digest::Keccak(224|256|384|512)
4 - new methods sign_hash_rfc7518 + verify_hash_rfc7518 (Crypt::PK::ECC)
5 - improved import of pkcs#8 private keys (Crypt::PK::ECC)
6 - improved export allowing "compressed" variants (Crypt::PK::ECC)
7 - fix #28 Apple's APNS pkcs8 auth key import fails (Crypt::PK::ECC)
8 - fix cpantesters failure (5.8.1 related)
9
10 0.058 2018-02-27
11 - fix: decode_b58b + invalid input
12
13 0.057 2018-01-31
14 - significant speed-up (more stuff handled on XS level)
15 - Crypt::Checksum is deprecated in favour of Crypt::Checksum::Adler32|CRC32
116
217 0.056 2017-12-22
318 - new Crypt::Stream::Rabbit
0 #define PERL_NO_GET_CONTEXT /* we want efficiency */
01 #include "EXTERN.h"
12 #include "perl.h"
23 #include "XSUB.h"
67 #define NEED_newRV_noinc_GLOBAL
78 #include "ppport.h"
89
10 /* assert_not_ROK is broken in 5.8.1 */
11 #if PERL_VERSION == 8 && PERL_SUBVERSION == 1
12 # undef assert_not_ROK
13 # if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
14 # define assert_not_ROK(sv) ({assert(!SvROK(sv) || !SvRV(sv));}),
15 # else
16 # define assert_not_ROK(sv)
17 # endif
18 #endif
19
920 #undef LTC_SOURCE
1021 #include "tomcrypt.h"
1122 #include "tommath.h"
1223
13 typedef adler32_state *Crypt__Checksum__Adler32;
14 typedef crc32_state *Crypt__Checksum__CRC32;
24 typedef adler32_state *Crypt__Checksum__Adler32;
25 typedef crc32_state *Crypt__Checksum__CRC32;
26
27 typedef ccm_state *Crypt__AuthEnc__CCM;
28 typedef eax_state *Crypt__AuthEnc__EAX;
29 typedef gcm_state *Crypt__AuthEnc__GCM;
30 typedef chacha20poly1305_state *Crypt__AuthEnc__ChaCha20Poly1305;
31 typedef ocb3_state *Crypt__AuthEnc__OCB;
32
33 typedef chacha_state *Crypt__Stream__ChaCha;
34 typedef salsa20_state *Crypt__Stream__Salsa20;
35 typedef sosemanuk_state *Crypt__Stream__Sosemanuk;
36 typedef rabbit_state *Crypt__Stream__Rabbit;
37 typedef rc4_state *Crypt__Stream__RC4;
38 typedef sober128_state *Crypt__Stream__Sober128;
39
40 typedef f9_state *Crypt__Mac__F9;
41 typedef hmac_state *Crypt__Mac__HMAC;
42 typedef omac_state *Crypt__Mac__OMAC;
43 typedef pelican_state *Crypt__Mac__Pelican;
44 typedef pmac_state *Crypt__Mac__PMAC;
45 typedef xcbc_state *Crypt__Mac__XCBC;
46 typedef poly1305_state *Crypt__Mac__Poly1305;
47 typedef blake2smac_state *Crypt__Mac__BLAKE2s;
48 typedef blake2bmac_state *Crypt__Mac__BLAKE2b;
1549
1650 typedef struct cipher_struct { /* used by Crypt::Cipher */
1751 symmetric_key skey;
18 int id;
1952 struct ltc_cipher_descriptor *desc;
2053 } *Crypt__Cipher;
2154
2255 typedef struct digest_struct { /* used by Crypt::Digest */
2356 hash_state state;
24 int id;
2557 struct ltc_hash_descriptor *desc;
2658 } *Crypt__Digest;
2759
2961 hash_state state;
3062 int num;
3163 } *Crypt__Digest__SHAKE;
32
33 typedef struct ccm_struct { /* used by Crypt::AuthEnc::CCM */
34 ccm_state state;
35 int direction;
36 int tag_len;
37 unsigned long pt_len;
38 int id;
39 } *Crypt__AuthEnc__CCM;
40
41 typedef struct eax_struct { /* used by Crypt::AuthEnc::EAX */
42 eax_state state;
43 int id;
44 } *Crypt__AuthEnc__EAX;
45
46 typedef struct gcm_struct { /* used by Crypt::AuthEnc::GCM */
47 gcm_state state;
48 int id;
49 } *Crypt__AuthEnc__GCM;
50
51 typedef struct chacha20poly1305_struct {/* used by Crypt::AuthEnc::ChaCha20Poly1305 */
52 chacha20poly1305_state state;
53 int id;
54 } *Crypt__AuthEnc__ChaCha20Poly1305;
55
56 typedef struct ocb_struct { /* used by Crypt::AuthEnc::OCB */
57 ocb3_state state;
58 int id;
59 } *Crypt__AuthEnc__OCB;
60
61 typedef struct chacha_struct { /* used by Crypt::Stream::ChaCha */
62 chacha_state state;
63 int id;
64 } *Crypt__Stream__ChaCha;
65
66 typedef struct salsa20_struct { /* used by Crypt::Stream::Salsa20 */
67 salsa20_state state;
68 int id;
69 } *Crypt__Stream__Salsa20;
70
71 typedef struct sosemanuk_struct { /* used by Crypt::Stream::Sosemanuk */
72 sosemanuk_state state;
73 int id;
74 } *Crypt__Stream__Sosemanuk;
75
76 typedef struct rabbit_struct { /* used by Crypt::Stream::Rabbit */
77 rabbit_state state;
78 int id;
79 } *Crypt__Stream__Rabbit;
80
81 typedef struct rc4_struct { /* used by Crypt::Stream::RC4 */
82 rc4_state state;
83 int id;
84 } *Crypt__Stream__RC4;
85
86 typedef struct sober128_struct { /* used by Crypt::Stream::Sober128 */
87 sober128_state state;
88 int id;
89 } *Crypt__Stream__Sober128;
90
91 typedef struct f9_struct { /* used by Crypt::Mac::F9 */
92 f9_state state;
93 int id;
94 } *Crypt__Mac__F9;
95
96 typedef struct hmac_struct { /* used by Crypt::Mac::HMAC */
97 hmac_state state;
98 int id;
99 } *Crypt__Mac__HMAC;
100
101 typedef struct omac_struct { /* used by Crypt::Mac::OMAC */
102 omac_state state;
103 int id;
104 } *Crypt__Mac__OMAC;
105
106 typedef struct pelican_struct { /* used by Crypt::Mac::Pelican */
107 pelican_state state;
108 int id;
109 } *Crypt__Mac__Pelican;
110
111 typedef struct pmac_struct { /* used by Crypt::Mac::PMAC */
112 pmac_state state;
113 int id;
114 } *Crypt__Mac__PMAC;
115
116 typedef struct xcbc_struct { /* used by Crypt::Mac::XCBC */
117 xcbc_state state;
118 int id;
119 } *Crypt__Mac__XCBC;
120
121 typedef struct poly1305_struct { /* used by Crypt::Mac::Poly1305 */
122 poly1305_state state;
123 int id;
124 } *Crypt__Mac__Poly1305;
125
126 typedef struct blake2s_struct { /* used by Crypt::Mac::BLAKE2s */
127 blake2smac_state state;
128 int id;
129 } *Crypt__Mac__BLAKE2s;
130
131 typedef struct blake2b_struct { /* used by Crypt::Mac::BLAKE2b */
132 blake2bmac_state state;
133 int id;
134 } *Crypt__Mac__BLAKE2b;
13564
13665 typedef struct cbc_struct { /* used by Crypt::Mode::CBC */
13766 int cipher_id, cipher_rounds;
14069 int padlen;
14170 int padding_mode;
14271 int direction;
143 int id;
14472 } *Crypt__Mode__CBC;
14573
14674 typedef struct ecb_struct { /* used by Crypt::Mode::ECB */
15078 int padlen;
15179 int padding_mode;
15280 int direction;
153 int id;
15481 } *Crypt__Mode__ECB;
15582
15683 typedef struct cfb_struct { /* used by Crypt::Mode::CFB */
15784 int cipher_id, cipher_rounds;
15885 symmetric_CFB state;
15986 int direction;
160 int id;
16187 } *Crypt__Mode__CFB;
16288
16389 typedef struct ctr_struct { /* used by Crypt::Mode::CTR */
16591 int ctr_mode_param;
16692 symmetric_CTR state;
16793 int direction;
168 int id;
16994 } *Crypt__Mode__CTR;
17095
17196 typedef struct f8_struct { /* used by Crypt::Mode::F8 */
17297 int cipher_id, cipher_rounds;
17398 symmetric_F8 state;
17499 int direction;
175 int id;
176100 } *Crypt__Mode__F8;
177101
178102 typedef struct lrw_struct { /* used by Crypt::Mode::LRW */
179103 int cipher_id, cipher_rounds;
180104 symmetric_LRW state;
181105 int direction;
182 int id;
183106 } *Crypt__Mode__LRW;
184107
185108 typedef struct ofb_struct { /* used by Crypt::Mode::OFB */
186109 int cipher_id, cipher_rounds;
187110 symmetric_OFB state;
188111 int direction;
189 int id;
190112 } *Crypt__Mode__OFB;
191113
192114 typedef struct xts_struct { /* used by Crypt::Mode::XTS */
193115 int cipher_id, cipher_rounds;
194116 symmetric_xts state;
195117 int direction;
196 int id;
197118 } *Crypt__Mode__XTS;
198119
199120 typedef struct prng_struct { /* used by Crypt::PRNG */
200121 prng_state state;
201122 struct ltc_prng_descriptor *desc;
202123 IV last_pid;
203 int id;
204124 } *Crypt__PRNG;
205125
206126 typedef struct rsa_struct { /* used by Crypt::PK::RSA */
207127 prng_state pstate;
208128 int pindex;
209129 rsa_key key;
210 int id;
211130 } *Crypt__PK__RSA;
212131
213132 typedef struct dsa_struct { /* used by Crypt::PK::DSA */
214133 prng_state pstate;
215134 int pindex;
216135 dsa_key key;
217 int id;
218136 } *Crypt__PK__DSA;
219137
220138 typedef struct dh_struct { /* used by Crypt::PK::DH */
221139 prng_state pstate;
222140 int pindex;
223141 dh_key key;
224 int id;
225142 } *Crypt__PK__DH;
226143
227144 typedef struct ecc_struct { /* used by Crypt::PK::ECC */
228145 prng_state pstate;
229146 int pindex;
230147 ecc_key key;
231 ltc_ecc_set_type dp;
232 int id;
233148 } *Crypt__PK__ECC;
234149
235 int str_add_leading_zero(char *str, int maxlen, int minlen) {
236 int len;
150 int mp_tohex_with_leading_zero(mp_int * a, char *str, int maxlen, int minlen) {
151 int len, rv;
152
153 if (mp_isneg(a) == MP_YES) {
154 *str = '\0';
155 return MP_VAL;
156 }
157
158 rv = mp_toradix_n(a, str, 16, maxlen);
159 if (rv != MP_OKAY) {
160 *str = '\0';
161 return rv;
162 }
163
237164 len = (int)strlen(str);
238165 if (len > 0 && len % 2 && len < maxlen-2) {
239166 memmove(str+1, str, len+1); /* incl. NUL byte */
240167 *str = '0'; /* add leading zero */
241168 }
169
242170 len = (int)strlen(str);
243171 if (len < minlen && minlen < maxlen-1) {
244172 memmove(str+(minlen-len), str, len+1); /* incl. NUL byte */
245173 memset(str, '0', minlen-len); /* add leading zero */
246174 }
175
247176 return MP_OKAY;
248177 }
249178
250 int mp_tohex_with_leading_zero(mp_int * a, char *str, int maxlen, int minlen) {
251 int rv;
252 if (mp_isneg(a) == MP_YES) {
253 *str = '\0';
254 return MP_VAL;
255 }
256 rv = mp_toradix_n(a, str, 16, maxlen);
257 if (rv != MP_OKAY) {
258 *str = '\0';
259 return rv;
260 }
261 return str_add_leading_zero(str, maxlen, minlen);
179 int _base16_encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
180 {
181 unsigned long i;
182 const char alphabet[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
183
184 if (*outlen < inlen * 2) {
185 *outlen = inlen * 2;
186 return CRYPT_BUFFER_OVERFLOW;
187 }
188
189 for (i = 0; i < inlen; i++) {
190 out[i*2] = (unsigned char)alphabet[in[i] >> 4];
191 out[i*2+1] = (unsigned char)alphabet[in[i] & 0xF];
192 }
193
194 *outlen = inlen * 2;
195 return CRYPT_OK;
196 }
197
198 size_t _find_start(const char *name, char *ltcname, size_t ltclen)
199 {
200 size_t i, start = 0;
201 if (name == NULL || strlen(name) + 1 > ltclen) croak("FATAL: invalid name") ;
202 /* normalize */
203 for (i = 0; i < ltclen && name[i] > 0; i++) {
204 if (name[i] >= 'A' && name[i] <= 'Z') {
205 ltcname[i] = name[i] + 32; /* lowecase */
206 }
207 else if (name[i] == '_') {
208 ltcname[i] = '-';
209 }
210 else {
211 ltcname[i] = name[i];
212 }
213 if (name[i] == ':') start = i + 1;
214 }
215 return start;
216 }
217
218 int _find_hash(const char *name)
219 {
220 char ltcname[100] = { 0 };
221 size_t start = _find_start(name, ltcname, sizeof(ltcname) - 1);
222 /* special cases */
223 if (strcmp(ltcname + start, "ripemd128") == 0) return find_hash("rmd128");
224 if (strcmp(ltcname + start, "ripemd160") == 0) return find_hash("rmd160");
225 if (strcmp(ltcname + start, "ripemd256") == 0) return find_hash("rmd256");
226 if (strcmp(ltcname + start, "ripemd320") == 0) return find_hash("rmd320");
227 if (strcmp(ltcname + start, "tiger192") == 0) return find_hash("tiger");
228 if (strcmp(ltcname + start, "chaes") == 0) return find_hash("chc_hash");
229 if (strcmp(ltcname + start, "chc-hash") == 0) return find_hash("chc_hash");
230 return find_hash(ltcname + start);
231 }
232
233 int _find_cipher(const char *name)
234 {
235 char ltcname[100] = { 0 };
236 size_t start = _find_start(name, ltcname, sizeof(ltcname) - 1);
237 /* special cases */
238 if (strcmp(ltcname + start, "des-ede") == 0) return find_cipher("3des");
239 if (strcmp(ltcname + start, "saferp") == 0) return find_cipher("safer+");
240 return find_cipher(ltcname + start);
241 }
242
243 int _find_prng(const char *name)
244 {
245 char ltcname[100] = { 0 };
246 size_t start = _find_start(name, ltcname, sizeof(ltcname) - 1);
247 return find_prng(ltcname + start);
262248 }
263249
264250 /* Math::BigInt::LTM related */
265251 typedef mp_int * Math__BigInt__LTM;
266252 STATIC SV * sv_from_mpi(mp_int *mpi) {
253 dTHX; /* fetch context */
267254 SV *obj = newSV(0);
268255 sv_setref_pv(obj, "Math::BigInt::LTM", (void*)mpi);
269256 return obj;
270257 }
271258
272 ltc_ecc_set_type* _ecc_set_dp_from_SV(ltc_ecc_set_type *dp, SV *curve)
259 void _ecc_oid_lookup(ecc_key *key)
273260 {
274 HV *h;
275 SV *param, **pref;
276 SV **sv_cofactor, **sv_prime, **sv_A, **sv_B, **sv_order, **sv_Gx, **sv_Gy;
261 int err;
262 unsigned i;
263 void *tmp;
264 const ltc_ecc_set_type *set;
265
266 key->dp.oidlen = 0;
267 if ((err = ltc_mp.init(&tmp)) != CRYPT_OK) return;
268 for (set = ltc_ecc_sets; set->name != NULL; set++) {
269 if ((err = mp_read_radix(tmp, set->prime, 16)) != CRYPT_OK) continue;
270 if ((mp_cmp(tmp, key->dp.prime) != LTC_MP_EQ)) continue;
271 if ((err = mp_read_radix(tmp, set->order, 16)) != CRYPT_OK) continue;
272 if ((mp_cmp(tmp, key->dp.order) != LTC_MP_EQ)) continue;
273 if ((err = mp_read_radix(tmp, set->A, 16)) != CRYPT_OK) continue;
274 if ((mp_cmp(tmp, key->dp.A) != LTC_MP_EQ)) continue;
275 if ((err = mp_read_radix(tmp, set->B, 16)) != CRYPT_OK) continue;
276 if ((mp_cmp(tmp, key->dp.B) != LTC_MP_EQ)) continue;
277 if ((err = mp_read_radix(tmp, set->Gx, 16)) != CRYPT_OK) continue;
278 if ((mp_cmp(tmp, key->dp.base.x) != LTC_MP_EQ)) continue;
279 if ((err = mp_read_radix(tmp, set->Gy, 16)) != CRYPT_OK) continue;
280 if ((mp_cmp(tmp, key->dp.base.y) != LTC_MP_EQ)) continue;
281 if (key->dp.cofactor != set->cofactor) continue;
282 break; /* found */
283 }
284 ltc_mp.deinit(tmp);
285 if (set->name != NULL) {
286 key->dp.oidlen = set->oidlen;
287 for(i = 0; i < set->oidlen; i++) key->dp.oid[i] = set->oid[i];
288 }
289 }
290
291 int _ecc_set_dp_from_SV(ecc_key *key, SV *curve)
292 {
293 dTHX; /* fetch context */
294 HV *hc, *hl, *h;
295 SV *sv_crv, **pref;
296 SV **sv_cofactor, **sv_prime, **sv_A, **sv_B, **sv_order, **sv_Gx, **sv_Gy, **sv_oid;
297 char *ch_name;
298 STRLEN l_name, i, j;
277299 int err;
278 char *ch_name;
279 STRLEN l_name;
300
301 if (!SvOK(curve)) croak("FATAL: undefined curve");
280302
281303 if (SvPOK(curve)) {
304 /* string */
282305 ch_name = SvPV(curve, l_name);
283 if ((h = get_hv("Crypt::PK::ECC::curve", 0)) == NULL) croak("FATAL: generate_key_ex: no curve register");
284 if ((pref = hv_fetch(h, ch_name, (U32)l_name, 0)) == NULL) croak("FATAL: generate_key_ex: unknown curve/1 '%s'", ch_name);
285 if (!SvOK(*pref)) croak("FATAL: generate_key_ex: unknown curve/2 '%s'", ch_name);
286 param = *pref;
306 if ((hl = get_hv("Crypt::PK::ECC::curve2ltc", 0)) == NULL) croak("FATAL: no curve2ltc register");
307 pref = hv_fetch(hl, ch_name, (U32)l_name, 0);
308 if (pref && SvOK(*pref)) {
309 sv_crv = *pref; /* found in %cutve2ltc */
310 }
311 else {
312 if ((hc = get_hv("Crypt::PK::ECC::curve", 0)) == NULL) croak("FATAL: no curve register");
313 pref = hv_fetch(hc, ch_name, (U32)l_name, 0);
314 if (pref && SvOK(*pref)) {
315 sv_crv = *pref; /* found in %curve */
316 }
317 else {
318 sv_crv = curve;
319 }
320 }
287321 }
288322 else if (SvROK(curve)) {
289 param = curve;
323 /* hashref */
324 sv_crv = curve;
290325 }
291326 else {
292327 croak("FATAL: curve has to be a string or a hashref");
293328 }
294329
295 if ((h = (HV*)(SvRV(param))) == NULL) croak("FATAL: ecparams: param is not valid hashref");
296
297 if ((sv_prime = hv_fetchs(h, "prime", 0)) == NULL) croak("FATAL: ecparams: missing param prime");
298 if ((sv_A = hv_fetchs(h, "A", 0)) == NULL) croak("FATAL: ecparams: missing param A");
299 if ((sv_B = hv_fetchs(h, "B", 0)) == NULL) croak("FATAL: ecparams: missing param B");
300 if ((sv_order = hv_fetchs(h, "order", 0)) == NULL) croak("FATAL: ecparams: missing param order");
301 if ((sv_Gx = hv_fetchs(h, "Gx", 0)) == NULL) croak("FATAL: ecparams: missing param Gx");
302 if ((sv_Gy = hv_fetchs(h, "Gy", 0)) == NULL) croak("FATAL: ecparams: missing param Gy");
303 if ((sv_cofactor = hv_fetchs(h, "cofactor", 0)) == NULL) croak("FATAL: ecparams: missing param cofactor");
304
305 if (!SvOK(*sv_prime )) croak("FATAL: ecparams: undefined param prime");
306 if (!SvOK(*sv_A )) croak("FATAL: ecparams: undefined param A");
307 if (!SvOK(*sv_B )) croak("FATAL: ecparams: undefined param B");
308 if (!SvOK(*sv_order )) croak("FATAL: ecparams: undefined param order");
309 if (!SvOK(*sv_Gx )) croak("FATAL: ecparams: undefined param Gx");
310 if (!SvOK(*sv_Gy )) croak("FATAL: ecparams: undefined param Gy");
311 if (!SvOK(*sv_cofactor)) croak("FATAL: ecparams: undefined param cofactor");
312
313 err = ecc_dp_set( dp,
314 SvPV_nolen(*sv_prime),
315 SvPV_nolen(*sv_A),
316 SvPV_nolen(*sv_B),
317 SvPV_nolen(*sv_order),
318 SvPV_nolen(*sv_Gx),
319 SvPV_nolen(*sv_Gy),
320 (unsigned long)SvUV(*sv_cofactor),
321 NULL, /* we intentionally don't allow setting custom names */
322 NULL /* we intentionally don't allow setting custom OIDs */
323 );
324 return err == CRYPT_OK ? dp : NULL;
325 }
326
327 void _ecc_free_key(ecc_key *key, ltc_ecc_set_type *dp)
328 {
329 if(dp) {
330 ecc_dp_clear(dp);
331 }
332 if (key->type != -1) {
333 ecc_free(key);
334 key->type = -1;
335 key->dp = NULL;
330 if (SvPOK(sv_crv)) {
331 /* string - curve name */
332 const ltc_ecc_set_type *dp;
333 ch_name = SvPV(sv_crv, l_name);
334 if (ecc_get_set_by_name(ch_name, &dp) != CRYPT_OK) croak("FATAL: ecparams: unknown curve '%s'", ch_name);
335 return ecc_set_dp(dp, key);
336 }
337 else {
338 /* hashref */
339 ltc_ecc_set_type set = { 0 };
340
341 if ((h = (HV*)(SvRV(sv_crv))) == NULL) croak("FATAL: ecparams: param is not valid hashref");
342
343 if ((sv_prime = hv_fetchs(h, "prime", 0)) == NULL) croak("FATAL: ecparams: missing param prime");
344 if ((sv_A = hv_fetchs(h, "A", 0)) == NULL) croak("FATAL: ecparams: missing param A");
345 if ((sv_B = hv_fetchs(h, "B", 0)) == NULL) croak("FATAL: ecparams: missing param B");
346 if ((sv_order = hv_fetchs(h, "order", 0)) == NULL) croak("FATAL: ecparams: missing param order");
347 if ((sv_Gx = hv_fetchs(h, "Gx", 0)) == NULL) croak("FATAL: ecparams: missing param Gx");
348 if ((sv_Gy = hv_fetchs(h, "Gy", 0)) == NULL) croak("FATAL: ecparams: missing param Gy");
349 if ((sv_cofactor = hv_fetchs(h, "cofactor", 0)) == NULL) croak("FATAL: ecparams: missing param cofactor");
350
351 if (!SvOK(*sv_prime )) croak("FATAL: ecparams: undefined param prime");
352 if (!SvOK(*sv_A )) croak("FATAL: ecparams: undefined param A");
353 if (!SvOK(*sv_B )) croak("FATAL: ecparams: undefined param B");
354 if (!SvOK(*sv_order )) croak("FATAL: ecparams: undefined param order");
355 if (!SvOK(*sv_Gx )) croak("FATAL: ecparams: undefined param Gx");
356 if (!SvOK(*sv_Gy )) croak("FATAL: ecparams: undefined param Gy");
357 if (!SvOK(*sv_cofactor)) croak("FATAL: ecparams: undefined param cofactor");
358
359 set.prime = SvPV_nolen(*sv_prime);
360 set.A = SvPV_nolen(*sv_A);
361 set.B = SvPV_nolen(*sv_B);
362 set.order = SvPV_nolen(*sv_order);
363 set.Gx = SvPV_nolen(*sv_Gx);
364 set.Gy = SvPV_nolen(*sv_Gy);
365 set.cofactor = (unsigned long)SvUV(*sv_cofactor),
366 set.name = NULL;
367 set.oidlen = 0;
368
369 sv_oid = hv_fetchs(h, "oid", 0);
370 if (sv_oid && SvPOK(*sv_oid)) {
371 ch_name = SvPV(*sv_oid, l_name);
372 for (i = 0, j = 0; i < l_name; i++) {
373 if (ch_name[i] == '.') {
374 if (++j >= 16) return CRYPT_ERROR;
375 }
376 else if(ch_name[i] >= '0' && ch_name[i] <= '9') {
377 set.oid[j] = set.oid[j] * 10 + (ch_name[i] - '0');
378 }
379 else {
380 return CRYPT_ERROR;
381 }
382 }
383 if (j == 0) return CRYPT_ERROR;
384 set.oidlen = j + 1;
385 }
386
387 if ((err = ecc_set_dp(&set, key)) != CRYPT_OK) return err;
388 if (key->dp.oidlen == 0) _ecc_oid_lookup(key);
389 return CRYPT_OK;
336390 }
337391 }
338392
347401 if(crypt_mp_init("ltm") != CRYPT_OK) { croak("FATAL: crypt_mp_init failed"); }
348402
349403 SV *
350 CryptX__encode_base64url(SV * in)
351 CODE:
352 {
353 STRLEN in_len;
354 unsigned long out_len;
355 unsigned char *out_data, *in_data;
356
357 if (!SvPOK(in)) XSRETURN_UNDEF;
358 in_data = (unsigned char *) SvPVbyte(in, in_len);
359 if (in_len == 0) {
360 RETVAL = newSVpvn("", 0);
361 }
362 else {
363 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
364 RETVAL = NEWSV(0, out_len);
365 SvPOK_only(RETVAL);
366 out_data = (unsigned char *)SvPVX(RETVAL);
367 if (base64url_encode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
368 SvREFCNT_dec(RETVAL);
369 XSRETURN_UNDEF;
370 }
371 SvCUR_set(RETVAL, out_len);
372 }
373 }
374 OUTPUT:
375 RETVAL
376
377 SV *
378 CryptX__decode_base64url(SV * in)
379 CODE:
380 {
381 STRLEN in_len;
382 unsigned long out_len;
383 unsigned char *out_data, *in_data;
384
385 if (!SvPOK(in)) XSRETURN_UNDEF;
386 in_data = (unsigned char *) SvPVbyte(in, in_len);
387 if (in_len == 0) {
388 RETVAL = newSVpvn("", 0);
389 }
390 else {
391 out_len = (unsigned long)in_len;
392 RETVAL = NEWSV(0, out_len);
393 SvPOK_only(RETVAL);
394 out_data = (unsigned char *)SvPVX(RETVAL);
395 if (base64url_decode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
396 SvREFCNT_dec(RETVAL);
397 XSRETURN_UNDEF;
398 }
399 SvCUR_set(RETVAL, out_len);
400 }
401 }
402 OUTPUT:
403 RETVAL
404
405 SV *
406 CryptX__encode_base64(SV * in)
407 CODE:
408 {
409 STRLEN in_len;
410 unsigned long out_len;
411 unsigned char *out_data, *in_data;
412
413 if (!SvPOK(in)) XSRETURN_UNDEF;
414 in_data = (unsigned char *) SvPVbyte(in, in_len);
415 if (in_len == 0) {
416 RETVAL = newSVpvn("", 0);
417 }
418 else {
419 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
420 RETVAL = NEWSV(0, out_len);
421 SvPOK_only(RETVAL);
422 out_data = (unsigned char *)SvPVX(RETVAL);
423 if (base64_encode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
424 SvREFCNT_dec(RETVAL);
425 XSRETURN_UNDEF;
426 }
427 SvCUR_set(RETVAL, out_len);
428 }
429 }
430 OUTPUT:
431 RETVAL
432
433 SV *
434 CryptX__decode_base64(SV * in)
435 CODE:
436 {
437 STRLEN in_len;
438 unsigned long out_len;
439 unsigned char *out_data, *in_data;
440
441 if (!SvPOK(in)) XSRETURN_UNDEF;
442 in_data = (unsigned char *)SvPVbyte(in, in_len);
443 if (in_len == 0) {
444 RETVAL = newSVpvn("", 0);
445 }
446 else {
447 out_len = (unsigned long)in_len;
448 RETVAL = NEWSV(0, out_len);
449 SvPOK_only(RETVAL);
450 out_data = (unsigned char *)SvPVX(RETVAL);
451 if (base64_decode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
452 SvREFCNT_dec(RETVAL);
453 XSRETURN_UNDEF;
454 }
455 SvCUR_set(RETVAL, out_len);
456 }
457 }
458 OUTPUT:
459 RETVAL
460
461 SV *
462 CryptX__encode_b32(SV *in, unsigned idx)
463 CODE:
464 {
465 STRLEN in_len;
466 unsigned long out_len;
467 unsigned char *out_data, *in_data;
468 int id = -1;
469
470 if (!SvPOK(in)) XSRETURN_UNDEF;
471 if (idx == 0) id = BASE32_RFC4648;
472 if (idx == 1) id = BASE32_BASE32HEX;
473 if (idx == 2) id = BASE32_ZBASE32;
474 if (idx == 3) id = BASE32_CROCKFORD;
475 if (id == -1) XSRETURN_UNDEF;
476 in_data = (unsigned char *) SvPVbyte(in, in_len);
477 if (in_len == 0) {
478 RETVAL = newSVpvn("", 0);
479 }
480 else {
481 out_len = (unsigned long)((8 * in_len + 4) / 5);
482 RETVAL = NEWSV(0, out_len);
483 SvPOK_only(RETVAL);
484 out_data = (unsigned char *)SvPVX(RETVAL);
485 if (base32_encode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
486 SvREFCNT_dec(RETVAL);
487 XSRETURN_UNDEF;
488 }
489 SvCUR_set(RETVAL, out_len);
490 }
491 }
492 OUTPUT:
493 RETVAL
494
495 SV *
496 CryptX__decode_b32(SV *in, unsigned idx)
497 CODE:
498 {
499 STRLEN in_len;
500 unsigned long out_len;
501 unsigned char *out_data, *in_data;
502 int id = -1;
503
504 if (!SvPOK(in)) XSRETURN_UNDEF;
505 if (idx == 0) id = BASE32_RFC4648;
506 if (idx == 1) id = BASE32_BASE32HEX;
507 if (idx == 2) id = BASE32_ZBASE32;
508 if (idx == 3) id = BASE32_CROCKFORD;
509 if (id == -1) XSRETURN_UNDEF;
510 in_data = (unsigned char *)SvPVbyte(in, in_len);
511 if (in_len == 0) {
512 RETVAL = newSVpvn("", 0);
513 }
514 else {
515 out_len = (unsigned long)in_len;
516 RETVAL = NEWSV(0, out_len);
517 SvPOK_only(RETVAL);
518 out_data = (unsigned char *)SvPVX(RETVAL);
519 if (base32_decode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
520 SvREFCNT_dec(RETVAL);
521 XSRETURN_UNDEF;
522 }
523 SvCUR_set(RETVAL, out_len);
524 }
525 }
526 OUTPUT:
527 RETVAL
528
529 SV *
530 CryptX__increment_octets_le(SV * in)
531 CODE:
532 {
533 STRLEN len, i = 0;
534 unsigned char *out_data, *in_data;
535
536 if (!SvPOK(in)) XSRETURN_UNDEF;
537 in_data = (unsigned char *)SvPVbyte(in, len);
538 if (len == 0) XSRETURN_UNDEF;
539
540 RETVAL = NEWSV(0, len);
541 SvPOK_only(RETVAL);
542 SvCUR_set(RETVAL, len);
543 out_data = (unsigned char *)SvPVX(RETVAL);
544 Copy(in_data, out_data, len, unsigned char);
545 while (i < len) {
546 out_data[i]++;
547 if (0 != out_data[i]) break;
548 i++;
549 }
550 if (i == len) {
551 SvREFCNT_dec(RETVAL);
552 croak("FATAL: increment_octets_le overflow");
553 }
554 }
555 OUTPUT:
556 RETVAL
557
558 SV *
559 CryptX__increment_octets_be(SV * in)
560 CODE:
561 {
562 STRLEN len, i = 0;
563 unsigned char *out_data, *in_data;
564
565 if (!SvPOK(in)) XSRETURN_UNDEF;
566 in_data = (unsigned char *)SvPVbyte(in, len);
567 if (len == 0) XSRETURN_UNDEF;
568
569 RETVAL = NEWSV(0, len);
570 SvPOK_only(RETVAL);
571 SvCUR_set(RETVAL, len);
572 out_data = (unsigned char *)SvPVX(RETVAL);
573 Copy(in_data, out_data, len, unsigned char);
574 while (i < len) {
575 out_data[len - 1 - i]++;
576 if (0 != out_data[len - 1 - i]) break;
577 i++;
578 }
579 if (i == len) {
580 SvREFCNT_dec(RETVAL);
581 croak("FATAL: increment_octets_be overflow");
582 }
583 }
584 OUTPUT:
585 RETVAL
586
587 SV *
588 CryptX__radix_to_bin(char *in, int radix)
404 CryptX__ltc_build_settings()
405 CODE:
406 RETVAL = newSVpv(crypt_build_settings, 0);
407 OUTPUT:
408 RETVAL
409
410 SV *
411 CryptX__ltc_mp_name()
412 CODE:
413 RETVAL = newSVpv(ltc_mp.name, 0);
414 OUTPUT:
415 RETVAL
416
417 int
418 CryptX__ltc_mp_bits_per_digit()
419 CODE:
420 RETVAL = ltc_mp.bits_per_digit;
421 OUTPUT:
422 RETVAL
423
424 MODULE = CryptX PACKAGE = Crypt::Misc
425
426 PROTOTYPES: DISABLE
427
428 SV *
429 _radix_to_bin(char *in, int radix)
589430 CODE:
590431 {
591432 STRLEN len;
592433 unsigned char *out_data;
593434 mp_int mpi;
594435
595 if (in == NULL || strlen(in) == 0) XSRETURN_UNDEF;
596 if (mp_init(&mpi) != CRYPT_OK) XSRETURN_UNDEF;
597
598 if (mp_read_radix(&mpi, in, radix) == CRYPT_OK) {
436 if (in == NULL) XSRETURN_UNDEF;
437 if (mp_init(&mpi) != CRYPT_OK) XSRETURN_UNDEF;
438 if (strlen(in) == 0) {
439 RETVAL = newSVpvn("", 0);
440 }
441 else if (mp_read_radix(&mpi, in, radix) == CRYPT_OK) {
599442 len = mp_unsigned_bin_size(&mpi);
600 RETVAL = NEWSV(0, len);
601 SvPOK_only(RETVAL);
602 SvCUR_set(RETVAL, len);
603 out_data = (unsigned char *)SvPVX(RETVAL);
604 mp_to_unsigned_bin(&mpi, out_data);
605 mp_clear(&mpi);
606 }
607 else {
608 XSRETURN_UNDEF;
609 }
610 }
611 OUTPUT:
612 RETVAL
613
614 SV *
615 CryptX__bin_to_radix(SV *in, int radix)
443 if (len == 0) {
444 RETVAL = newSVpvn("", 0);
445 }
446 else {
447 RETVAL = NEWSV(0, len); /* avoid zero! */
448 SvPOK_only(RETVAL);
449 SvCUR_set(RETVAL, len);
450 out_data = (unsigned char *)SvPVX(RETVAL);
451 mp_to_unsigned_bin(&mpi, out_data);
452 }
453 }
454 else {
455 RETVAL = newSVpvn(NULL, 0); /* undef */
456 }
457 mp_clear(&mpi);
458 }
459 OUTPUT:
460 RETVAL
461
462 SV *
463 _bin_to_radix(SV *in, int radix)
616464 CODE:
617465 {
618466 STRLEN len;
624472
625473 if (!SvPOK(in) || radix < 2 || radix > 64) XSRETURN_UNDEF;
626474 in_data = (unsigned char *) SvPVbyte(in, len);
627 if (len == 0) XSRETURN_UNDEF;
628
629 mp_init(&mpi);
630 if (mp_read_unsigned_bin(&mpi, in_data, (unsigned long)len) == CRYPT_OK) {
631 mp_init_copy(&tmp, &mpi);
632 while (mp_iszero(&tmp) == MP_NO) {
633 mp_div_d(&tmp, (mp_digit)radix, &tmp, &d);
634 digits++;
635 }
636 mp_clear(&tmp);
637
638 if (digits == 0) {
639 RETVAL = newSVpvn("", 0);
640 mp_clear(&mpi);
475 mp_init_multi(&mpi, &tmp, NULL);
476 if (len == 0) {
477 RETVAL = newSVpvn("", 0);
478 }
479 else {
480 if (mp_read_unsigned_bin(&mpi, in_data, (unsigned long)len) == CRYPT_OK) {
481 mp_copy(&mpi, &tmp);
482 while (mp_iszero(&tmp) == MP_NO) {
483 mp_div_d(&tmp, (mp_digit)radix, &tmp, &d);
484 digits++;
485 }
486 if (digits == 0) {
487 RETVAL = newSVpvn("", 0);
488 }
489 else {
490 RETVAL = NEWSV(0, digits + 2); /* +2 for sign and NUL byte */
491 SvPOK_only(RETVAL);
492 out_data = SvPVX(RETVAL);
493 mp_toradix(&mpi, out_data, radix);
494 SvCUR_set(RETVAL, strlen(out_data));
495 }
641496 }
642497 else {
643 RETVAL = NEWSV(0, digits + 2); /* +2 for sign and NUL byte */
644 SvPOK_only(RETVAL);
645 out_data = SvPVX(RETVAL);
646 mp_toradix(&mpi, out_data, radix);
647 SvCUR_set(RETVAL, strlen(out_data));
648 mp_clear(&mpi);
649 }
650 }
651 else {
652 mp_clear(&mpi);
653 XSRETURN_UNDEF;
654 }
655 }
656 OUTPUT:
657 RETVAL
658
659 SV *
660 CryptX__ltc_build_settings()
661 CODE:
662 RETVAL = newSVpv(crypt_build_settings, 0);
663 OUTPUT:
664 RETVAL
665
666 SV *
667 CryptX__ltc_mp_name()
668 CODE:
669 RETVAL = newSVpv(ltc_mp.name, 0);
670 OUTPUT:
671 RETVAL
672
673 int
674 CryptX__ltc_mp_bits_per_digit()
675 CODE:
676 RETVAL = ltc_mp.bits_per_digit;
498 RETVAL = newSVpvn(NULL, 0); /* undef */
499 }
500 }
501 mp_clear_multi(&tmp, &mpi, NULL);
502 }
503 OUTPUT:
504 RETVAL
505
506 SV *
507 encode_b64(SV * in)
508 ALIAS:
509 encode_b64u = 1
510 CODE:
511 {
512 int rv;
513 STRLEN in_len;
514 unsigned long out_len;
515 unsigned char *out_data, *in_data;
516
517 if (!SvPOK(in)) XSRETURN_UNDEF;
518 in_data = (unsigned char *) SvPVbyte(in, in_len);
519 if (in_len == 0) {
520 RETVAL = newSVpvn("", 0);
521 }
522 else {
523 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
524 RETVAL = NEWSV(0, out_len); /* avoid zero! */
525 SvPOK_only(RETVAL);
526 out_data = (unsigned char *)SvPVX(RETVAL);
527 if (ix == 1)
528 rv = base64url_encode(in_data, (unsigned long)in_len, out_data, &out_len);
529 else
530 rv = base64_encode(in_data, (unsigned long)in_len, out_data, &out_len);
531 if (rv != CRYPT_OK) {
532 SvREFCNT_dec(RETVAL);
533 XSRETURN_UNDEF;
534 }
535 SvCUR_set(RETVAL, out_len);
536 }
537 }
538 OUTPUT:
539 RETVAL
540
541 SV *
542 decode_b64(SV * in)
543 ALIAS:
544 decode_b64u = 1
545 CODE:
546 {
547 int rv;
548 STRLEN in_len;
549 unsigned long out_len;
550 unsigned char *out_data, *in_data;
551
552 if (!SvPOK(in)) XSRETURN_UNDEF;
553 in_data = (unsigned char *)SvPVbyte(in, in_len);
554 if (in_len == 0) {
555 RETVAL = newSVpvn("", 0);
556 }
557 else {
558 out_len = (unsigned long)in_len;
559 RETVAL = NEWSV(0, out_len); /* avoid zero! */
560 SvPOK_only(RETVAL);
561 out_data = (unsigned char *)SvPVX(RETVAL);
562 if (ix == 1)
563 rv = base64url_decode(in_data, (unsigned long)in_len, out_data, &out_len);
564 else
565 rv = base64_decode(in_data, (unsigned long)in_len, out_data, &out_len);
566 if (rv != CRYPT_OK) {
567 SvREFCNT_dec(RETVAL);
568 XSRETURN_UNDEF;
569 }
570 SvCUR_set(RETVAL, out_len);
571 }
572 }
573 OUTPUT:
574 RETVAL
575
576 SV *
577 encode_b32r(SV *in)
578 ALIAS:
579 encode_b32b = 1
580 encode_b32z = 2
581 encode_b32c = 3
582 CODE:
583 {
584 STRLEN in_len;
585 unsigned long out_len;
586 unsigned char *out_data, *in_data;
587 int id = -1;
588
589 if (!SvPOK(in)) XSRETURN_UNDEF;
590 if (ix == 0) id = BASE32_RFC4648;
591 if (ix == 1) id = BASE32_BASE32HEX;
592 if (ix == 2) id = BASE32_ZBASE32;
593 if (ix == 3) id = BASE32_CROCKFORD;
594 if (id == -1) XSRETURN_UNDEF;
595 in_data = (unsigned char *) SvPVbyte(in, in_len);
596 if (in_len == 0) {
597 RETVAL = newSVpvn("", 0);
598 }
599 else {
600 out_len = (unsigned long)((8 * in_len + 4) / 5);
601 RETVAL = NEWSV(0, out_len); /* avoid zero! */
602 SvPOK_only(RETVAL);
603 out_data = (unsigned char *)SvPVX(RETVAL);
604 if (base32_encode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
605 SvREFCNT_dec(RETVAL);
606 XSRETURN_UNDEF;
607 }
608 SvCUR_set(RETVAL, out_len);
609 }
610 }
611 OUTPUT:
612 RETVAL
613
614 SV *
615 decode_b32r(SV *in)
616 ALIAS:
617 decode_b32b = 1
618 decode_b32z = 2
619 decode_b32c = 3
620 CODE:
621 {
622 STRLEN in_len;
623 unsigned long out_len;
624 unsigned char *out_data, *in_data;
625 int id = -1;
626
627 if (!SvPOK(in)) XSRETURN_UNDEF;
628 if (ix == 0) id = BASE32_RFC4648;
629 if (ix == 1) id = BASE32_BASE32HEX;
630 if (ix == 2) id = BASE32_ZBASE32;
631 if (ix == 3) id = BASE32_CROCKFORD;
632 if (id == -1) XSRETURN_UNDEF;
633 in_data = (unsigned char *)SvPVbyte(in, in_len);
634 if (in_len == 0) {
635 RETVAL = newSVpvn("", 0);
636 }
637 else {
638 out_len = (unsigned long)in_len;
639 RETVAL = NEWSV(0, out_len); /* avoid zero! */
640 SvPOK_only(RETVAL);
641 out_data = (unsigned char *)SvPVX(RETVAL);
642 if (base32_decode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
643 SvREFCNT_dec(RETVAL);
644 XSRETURN_UNDEF;
645 }
646 SvCUR_set(RETVAL, out_len);
647 }
648 }
649 OUTPUT:
650 RETVAL
651
652 SV *
653 increment_octets_le(SV * in)
654 CODE:
655 {
656 STRLEN len, i = 0;
657 unsigned char *out_data, *in_data;
658
659 if (!SvPOK(in)) XSRETURN_UNDEF;
660 in_data = (unsigned char *)SvPVbyte(in, len);
661 if (len == 0) {
662 RETVAL = newSVpvn("", 0);
663 }
664 else {
665 RETVAL = NEWSV(0, len); /* avoid zero! */
666 SvPOK_only(RETVAL);
667 SvCUR_set(RETVAL, len);
668 out_data = (unsigned char *)SvPVX(RETVAL);
669 Copy(in_data, out_data, len, unsigned char);
670 while (i < len) {
671 out_data[i]++;
672 if (0 != out_data[i]) break;
673 i++;
674 }
675 if (i == len) {
676 SvREFCNT_dec(RETVAL);
677 croak("FATAL: increment_octets_le overflow");
678 }
679 }
680 }
681 OUTPUT:
682 RETVAL
683
684 SV *
685 increment_octets_be(SV * in)
686 CODE:
687 {
688 STRLEN len, i = 0;
689 unsigned char *out_data, *in_data;
690
691 if (!SvPOK(in)) XSRETURN_UNDEF;
692 in_data = (unsigned char *)SvPVbyte(in, len);
693 if (len == 0) {
694 RETVAL = newSVpvn("", 0);
695 }
696 else {
697 RETVAL = NEWSV(0, len); /* avoid zero! */
698 SvPOK_only(RETVAL);
699 SvCUR_set(RETVAL, len);
700 out_data = (unsigned char *)SvPVX(RETVAL);
701 Copy(in_data, out_data, len, unsigned char);
702 while (i < len) {
703 out_data[len - 1 - i]++;
704 if (0 != out_data[len - 1 - i]) break;
705 i++;
706 }
707 if (i == len) {
708 SvREFCNT_dec(RETVAL);
709 croak("FATAL: increment_octets_be overflow");
710 }
711 }
712 }
677713 OUTPUT:
678714 RETVAL
679715
8181 lib/Crypt/Digest/BLAKE2s_224.pm
8282 lib/Crypt/Digest/BLAKE2s_256.pm
8383 lib/Crypt/Digest/CHAES.pm
84 lib/Crypt/Digest/Keccak224.pm
85 lib/Crypt/Digest/Keccak256.pm
86 lib/Crypt/Digest/Keccak384.pm
87 lib/Crypt/Digest/Keccak512.pm
8488 lib/Crypt/Digest/MD2.pm
8589 lib/Crypt/Digest/MD4.pm
8690 lib/Crypt/Digest/MD5.pm
388392 src/ltc/pk/asn1/der/boolean/der_encode_boolean.c
389393 src/ltc/pk/asn1/der/boolean/der_length_boolean.c
390394 src/ltc/pk/asn1/der/choice/der_decode_choice.c
395 src/ltc/pk/asn1/der/custom_type/der_decode_custom_type.c
396 src/ltc/pk/asn1/der/custom_type/der_encode_custom_type.c
397 src/ltc/pk/asn1/der/custom_type/der_length_custom_type.c
398 src/ltc/pk/asn1/der/general/der_asn1_maps.c
399 src/ltc/pk/asn1/der/general/der_decode_asn1_identifier.c
400 src/ltc/pk/asn1/der/general/der_decode_asn1_length.c
401 src/ltc/pk/asn1/der/general/der_encode_asn1_identifier.c
402 src/ltc/pk/asn1/der/general/der_encode_asn1_length.c
403 src/ltc/pk/asn1/der/general/der_length_asn1_identifier.c
404 src/ltc/pk/asn1/der/general/der_length_asn1_length.c
391405 src/ltc/pk/asn1/der/generalizedtime/der_decode_generalizedtime.c
392406 src/ltc/pk/asn1/der/generalizedtime/der_encode_generalizedtime.c
393407 src/ltc/pk/asn1/der/generalizedtime/der_length_generalizedtime.c
409423 src/ltc/pk/asn1/der/sequence/der_decode_sequence_ex.c
410424 src/ltc/pk/asn1/der/sequence/der_decode_sequence_flexi.c
411425 src/ltc/pk/asn1/der/sequence/der_decode_sequence_multi.c
412 src/ltc/pk/asn1/der/sequence/der_decode_subject_public_key_info.c
413426 src/ltc/pk/asn1/der/sequence/der_encode_sequence_ex.c
414427 src/ltc/pk/asn1/der/sequence/der_encode_sequence_multi.c
415 src/ltc/pk/asn1/der/sequence/der_encode_subject_public_key_info.c
416428 src/ltc/pk/asn1/der/sequence/der_length_sequence.c
417429 src/ltc/pk/asn1/der/sequence/der_sequence_free.c
418430 src/ltc/pk/asn1/der/sequence/der_sequence_shrink.c
429441 src/ltc/pk/asn1/der/utf8/der_decode_utf8_string.c
430442 src/ltc/pk/asn1/der/utf8/der_encode_utf8_string.c
431443 src/ltc/pk/asn1/der/utf8/der_length_utf8_string.c
444 src/ltc/pk/asn1/x509/x509_decode_subject_public_key_info.c
445 src/ltc/pk/asn1/x509/x509_encode_subject_public_key_info.c
432446 src/ltc/pk/dh/dh.c
433447 src/ltc/pk/dh/dh_check_pubkey.c
434448 src/ltc/pk/dh/dh_export.c
457471 src/ltc/pk/ecc/ecc_ansi_x963_export.c
458472 src/ltc/pk/ecc/ecc_ansi_x963_import.c
459473 src/ltc/pk/ecc/ecc_decrypt_key.c
460 src/ltc/pk/ecc/ecc_dp_clear.c
461 src/ltc/pk/ecc/ecc_dp_fill_from_sets.c
462 src/ltc/pk/ecc/ecc_dp_from_oid.c
463 src/ltc/pk/ecc/ecc_dp_from_params.c
464 src/ltc/pk/ecc/ecc_dp_init.c
465 src/ltc/pk/ecc/ecc_dp_set.c
466474 src/ltc/pk/ecc/ecc_encrypt_key.c
467475 src/ltc/pk/ecc/ecc_export.c
468 src/ltc/pk/ecc/ecc_export_full.c
469 src/ltc/pk/ecc/ecc_export_raw.c
476 src/ltc/pk/ecc/ecc_export_openssl.c
470477 src/ltc/pk/ecc/ecc_free.c
478 src/ltc/pk/ecc/ecc_get_key.c
479 src/ltc/pk/ecc/ecc_get_set.c
471480 src/ltc/pk/ecc/ecc_get_size.c
472481 src/ltc/pk/ecc/ecc_import.c
473 src/ltc/pk/ecc/ecc_import_full.c
482 src/ltc/pk/ecc/ecc_import_openssl.c
474483 src/ltc/pk/ecc/ecc_import_pkcs8.c
475 src/ltc/pk/ecc/ecc_import_raw.c
484 src/ltc/pk/ecc/ecc_import_x509.c
476485 src/ltc/pk/ecc/ecc_make_key.c
486 src/ltc/pk/ecc/ecc_set_dp.c
487 src/ltc/pk/ecc/ecc_set_dp_internal.c
488 src/ltc/pk/ecc/ecc_set_key.c
477489 src/ltc/pk/ecc/ecc_shared_secret.c
478490 src/ltc/pk/ecc/ecc_sign_hash.c
479491 src/ltc/pk/ecc/ecc_sizes.c
480492 src/ltc/pk/ecc/ecc_verify_hash.c
481 src/ltc/pk/ecc/ecc_verify_key.c
482493 src/ltc/pk/ecc/ltc_ecc_export_point.c
483494 src/ltc/pk/ecc/ltc_ecc_import_point.c
484495 src/ltc/pk/ecc/ltc_ecc_is_point.c
485496 src/ltc/pk/ecc/ltc_ecc_is_point_at_infinity.c
486 src/ltc/pk/ecc/ltc_ecc_is_valid_idx.c
487497 src/ltc/pk/ecc/ltc_ecc_map.c
488498 src/ltc/pk/ecc/ltc_ecc_mul2add.c
489499 src/ltc/pk/ecc/ltc_ecc_mulmod.c
491501 src/ltc/pk/ecc/ltc_ecc_points.c
492502 src/ltc/pk/ecc/ltc_ecc_projective_add_point.c
493503 src/ltc/pk/ecc/ltc_ecc_projective_dbl_point.c
504 src/ltc/pk/ecc/ltc_ecc_verify_key.c
494505 src/ltc/pk/pkcs1/pkcs_1_i2osp.c
495506 src/ltc/pk/pkcs1/pkcs_1_mgf1.c
496507 src/ltc/pk/pkcs1/pkcs_1_oaep_decode.c
903914 t/digest_blake2s_224.t
904915 t/digest_blake2s_256.t
905916 t/digest_chaes.t
917 t/digest_keccak224.t
918 t/digest_keccak256.t
919 t/digest_keccak384.t
920 t/digest_keccak512.t
906921 t/digest_md2.t
907922 t/digest_md4.t
908923 t/digest_md5.t
4444 "url" : "https://github.com/DCIT/perl-CryptX"
4545 }
4646 },
47 "version" : "0.056",
47 "version" : "0.059",
4848 "x_serialization_backend" : "JSON::PP version 2.94"
4949 }
2121 resources:
2222 bugtracker: https://github.com/DCIT/perl-CryptX/issues
2323 repository: https://github.com/DCIT/perl-CryptX
24 version: '0.056'
24 version: '0.059'
2525 x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
22 use ExtUtils::MakeMaker;
33 use Config;
44
5 my @myobjs = map { s|.c$|$Config{obj_ext}|; $_ } grep { $_ !~ m|^src/ltc/\.*tab\.c$| } (
6 glob('src/ltm/*.c'),
7 glob('src/ltc/*/*.c'),
8 glob('src/ltc/*/*/*.c'),
9 glob('src/ltc/*/*/*/*.c'),
10 glob('src/ltc/*/*/*/*/*.c'),
11 );
12 my $myextlib = "src/liballinone$Config{lib_ext}";
13 my $mycflags = "$Config{ccflags} $Config{cccdlflags} $Config{optimize}";
5 my (@EUMM_INC_LIB, $myarflags, $mycflags);
146
15 #FIX: this is particularly useful for Debian https://github.com/DCIT/perl-CryptX/pull/39
16 $mycflags .= " $ENV{CFLAGS}" if $ENV{CFLAGS};
17 $mycflags .= " $ENV{CPPFLAGS}" if $ENV{CPPFLAGS};
7 if ($ENV{CRYPTX_CFLAGS} || $ENV{CRYPTX_LDFLAGS}) {
8 # EXPERIMENTAL: use system libraries libtomcrypt + libtommath
9 # e.g.
10 # CRYPTX_LDFLAGS='-L/usr/local/lib -ltommath -ltomcrypt' CRYPTX_CFLAGS='-DLTM_DESC -I/usr/local/include' perl Makefile.PL
11 print "CRYPTX_CFLAGS = $ENV{CRYPTX_CFLAGS}\n" if $ENV{CRYPTX_CFLAGS};
12 print "CRYPTX_LDFLAGS = $ENV{CRYPTX_LDFLAGS}\n" if $ENV{CRYPTX_LDFLAGS};
13 @EUMM_INC_LIB = (
14 INC => $ENV{CRYPTX_CFLAGS},
15 LIBS => [ $ENV{CRYPTX_LDFLAGS} ],
16 );
17 }
18 else {
19 # PREFERRED: use bundled libtomcrypt + libtommath (from ./src subdir)
20 my @myobjs = map { s|.c$|$Config{obj_ext}|; $_ } grep { $_ !~ m|^src/ltc/\.*tab\.c$| } (
21 glob('src/ltm/*.c'),
22 glob('src/ltc/*/*.c'),
23 glob('src/ltc/*/*/*.c'),
24 glob('src/ltc/*/*/*/*.c'),
25 glob('src/ltc/*/*/*/*/*.c'),
26 );
27 $mycflags = "$Config{ccflags} $Config{cccdlflags} $Config{optimize}";
1828
19 #FIX: gcc with -flto is a trouble maker see https://github.com/DCIT/perl-CryptX/issues/32
20 $mycflags =~ s/-flto\b//g;
29 #FIX: this is particularly useful for Debian https://github.com/DCIT/perl-CryptX/pull/39
30 $mycflags .= " $ENV{CFLAGS}" if $ENV{CFLAGS};
31 $mycflags .= " $ENV{CPPFLAGS}" if $ENV{CPPFLAGS};
2132
22 #FIX: avoid -Wwrite-strings -Wcast-qual -pedantic -pedantic-errors -ansi -std=c89
23 $mycflags =~ s/-pedantic\b//g;
24 $mycflags =~ s/-pedantic-errors\b//g;
25 $mycflags =~ s/-std=c89\b//g;
26 $mycflags =~ s/-ansi\b//g;
27 $mycflags =~ s/-Wwrite-strings\b//g;
28 $mycflags =~ s/-Wcast-qual\b//g;
33 #FIX: gcc with -flto is a trouble maker see https://github.com/DCIT/perl-CryptX/issues/32
34 $mycflags =~ s/-flto\b//g;
2935
30 #FIX: avoid "ar: fatal: Numeric group ID too large" see https://github.com/DCIT/perl-CryptX/issues/33
31 my $myarflags = '$(AR_STATIC_ARGS)';
32 if ($^O ne 'MSWin32' && $Config{ar}) {
33 # for ar's "deterministic mode" we need GNU binutils 2.20+ (2009-10-16)
34 my $arver = `$Config{ar} --version 2>/dev/null`;
35 my ($maj, $min) = $arver =~ /^GNU ar [^\d]*(\d)\.(\d+)\.\d+/s;
36 $myarflags = 'rcD' if ($maj && $min && $maj >= 2 && $min >= 20) || $arver=~ /^BSD ar /;
36 #FIX: avoid -Wwrite-strings -Wcast-qual -pedantic -pedantic-errors -ansi -std=c89
37 $mycflags =~ s/-pedantic\b//g;
38 $mycflags =~ s/-pedantic-errors\b//g;
39 $mycflags =~ s/-std=c89\b//g;
40 $mycflags =~ s/-ansi\b//g;
41 $mycflags =~ s/-Wwrite-strings\b//g;
42 $mycflags =~ s/-Wcast-qual\b//g;
43
44 #FIX: avoid "ar: fatal: Numeric group ID too large" see https://github.com/DCIT/perl-CryptX/issues/33
45 $myarflags = '$(AR_STATIC_ARGS)';
46 if ($^O ne 'MSWin32' && $Config{ar}) {
47 # for ar's "deterministic mode" we need GNU binutils 2.20+ (2009-10-16)
48 my $arver = `$Config{ar} --version 2>/dev/null`;
49 my ($maj, $min) = $arver =~ /^GNU ar [^\d]*(\d)\.(\d+)\.\d+/s;
50 $myarflags = 'rcD' if ($maj && $min && $maj >= 2 && $min >= 20) || $arver=~ /^BSD ar /;
51 }
52 @EUMM_INC_LIB = (
53 INC => '-DLTM_DESC -Isrc/ltc/headers -Isrc/ltm',
54 MYEXTLIB => "src/liballinone$Config{lib_ext}",
55 clean => { 'FILES' => join(' ', @myobjs, "src/liballinone$Config{lib_ext}") },
56 );
3757 }
3858
3959 my %eumm_args = (
4565 LICENSE => 'perl_5',
4666 META_MERGE => { resources => { repository => 'https://github.com/DCIT/perl-CryptX', bugtracker => 'https://github.com/DCIT/perl-CryptX/issues' } },
4767 dist => { 'PREOP' => 'perldoc -u lib/CryptX.pm | pod2markdown > README.md' },
48 ($ENV{CRYPTX_CFLAGS} || $ENV{CRYPTX_LDFLAGS})
49 ?
50 (
51 # e.g. CRYPTX_LDFLAGS='-L/usr/local/lib -ltommath -ltomcrypt' CRYPTX_CFLAGS='-DLTM_DESC -I/usr/local/include' perl Makefile.PL
52 INC => $ENV{CRYPTX_CFLAGS},
53 LIBS => [ $ENV{CRYPTX_LDFLAGS} ],
54 )
55 :
56 (
57 # bundled libtomcrypt + libtommath
58 INC => '-DLTM_DESC -Isrc/ltc/headers -Isrc/ltm',
59 MYEXTLIB => $myextlib,
60 clean => { 'FILES' => join(' ', @myobjs, $myextlib) },
61 )
68 @EUMM_INC_LIB
6269 );
6370
64 my $eumm_ver = eval $ExtUtils::MakeMaker::VERSION;
71 my $eumm_ver = eval $ExtUtils::MakeMaker::VERSION;
6572 delete $eumm_args{MIN_PERL_VERSION} if $eumm_ver < 6.48;
6673 delete $eumm_args{META_ADD} if $eumm_ver < 6.46;
6774 delete $eumm_args{META_MERGE} if $eumm_ver < 6.46;
6976
7077 WriteMakefile(%eumm_args);
7178
72 # ARFLAGS=\$(AR_STATIC_ARGS) RANLIB=\$(RANLIB) AR=\$(AR)
79 sub MY::postamble {
80 return "" unless $mycflags && $myarflags;
7381
74 sub MY::postamble {
75 my $myextlib = qq{
82 my $extra_targets = qq{
7683 \$(MYEXTLIB): src/Makefile
7784 cd src && \$(MAKE) ARFLAGS="$myarflags" RANLIB="\$(RANLIB)" AR="\$(AR)" CC="\$(CC)" LIB_EXT=\$(LIB_EXT) OBJ_EXT=\$(OBJ_EXT) CFLAGS="$mycflags"
7885 };
7986
80 $myextlib = qq{
87 $extra_targets = qq{
8188 \$(MYEXTLIB): src/Makefile
8289 cd src && \$(MAKE) -f Makefile.nmake CFLAGS="$mycflags"
8390 } if $^O eq 'MSWin32' && $Config{make} =~ /nmake/ && $Config{cc} =~ /cl/;
8491
85 $myextlib = qq{
92 $extra_targets = qq{
8693 \$(MYEXTLIB): src/Makefile
8794 cd src && \$(MAKE) CC="$Config{cc}" CFLAGS="$mycflags"
8895 } if $^O eq 'MSWin32' && $Config{cc} =~ /gcc/;
8996
90 my $version_patch = q{
97 $extra_targets .= q{
9198 versionsync:
9299 $(NOECHO) perl _generators/version_patch.pl sync
93100 versioninc:
106113 $(NOECHO) perl -Mblib t/openssl/dsa-test.pl
107114 $(NOECHO) perl -Mblib t/openssl/ecc-test.pl
108115 $(NOECHO) perl -Mblib t/openssl/rsa-test.pl
116 rebuild-pre:
117 $(RM_F) src/liballinone.a
118 $(TOUCH) CryptX.xs
119 rebuild: rebuild-pre all
120
109121 };
110122
111 return "$myextlib\n$version_patch";
123 return $extra_targets;
112124 }
55
66 Cryptography in CryptX is based on [https://github.com/libtom/libtomcrypt](https://github.com/libtom/libtomcrypt)
77
8 Currently available modules:
8 Available modules:
99
1010 - Symmetric ciphers - see [Crypt::Cipher](https://metacpan.org/pod/Crypt::Cipher) and related modules
1111
3535 [Crypt::Digest::CHAES](https://metacpan.org/pod/Crypt::Digest::CHAES), [Crypt::Digest::MD2](https://metacpan.org/pod/Crypt::Digest::MD2), [Crypt::Digest::MD4](https://metacpan.org/pod/Crypt::Digest::MD4), [Crypt::Digest::MD5](https://metacpan.org/pod/Crypt::Digest::MD5), [Crypt::Digest::RIPEMD128](https://metacpan.org/pod/Crypt::Digest::RIPEMD128), [Crypt::Digest::RIPEMD160](https://metacpan.org/pod/Crypt::Digest::RIPEMD160),
3636 [Crypt::Digest::RIPEMD256](https://metacpan.org/pod/Crypt::Digest::RIPEMD256), [Crypt::Digest::RIPEMD320](https://metacpan.org/pod/Crypt::Digest::RIPEMD320), [Crypt::Digest::SHA1](https://metacpan.org/pod/Crypt::Digest::SHA1), [Crypt::Digest::SHA224](https://metacpan.org/pod/Crypt::Digest::SHA224), [Crypt::Digest::SHA256](https://metacpan.org/pod/Crypt::Digest::SHA256), [Crypt::Digest::SHA384](https://metacpan.org/pod/Crypt::Digest::SHA384),
3737 [Crypt::Digest::SHA512](https://metacpan.org/pod/Crypt::Digest::SHA512), [Crypt::Digest::SHA512\_224](https://metacpan.org/pod/Crypt::Digest::SHA512_224), [Crypt::Digest::SHA512\_256](https://metacpan.org/pod/Crypt::Digest::SHA512_256), [Crypt::Digest::Tiger192](https://metacpan.org/pod/Crypt::Digest::Tiger192), [Crypt::Digest::Whirlpool](https://metacpan.org/pod/Crypt::Digest::Whirlpool),
38 [Crypt::Digest::Keccak224](https://metacpan.org/pod/Crypt::Digest::Keccak224), [Crypt::Digest::Keccak256](https://metacpan.org/pod/Crypt::Digest::Keccak256), [Crypt::Digest::Keccak384](https://metacpan.org/pod/Crypt::Digest::Keccak384), [Crypt::Digest::Keccak512](https://metacpan.org/pod/Crypt::Digest::Keccak512),
3839 [Crypt::Digest::SHA3\_224](https://metacpan.org/pod/Crypt::Digest::SHA3_224), [Crypt::Digest::SHA3\_256](https://metacpan.org/pod/Crypt::Digest::SHA3_256), [Crypt::Digest::SHA3\_384](https://metacpan.org/pod/Crypt::Digest::SHA3_384), [Crypt::Digest::SHA3\_512](https://metacpan.org/pod/Crypt::Digest::SHA3_512), [Crypt::Digest::SHAKE](https://metacpan.org/pod/Crypt::Digest::SHAKE)
3940
4041 - Checksums
4142
42 [Crypt::Checksum](https://metacpan.org/pod/Crypt::Checksum), [Crypt::Checksum::Adler32](https://metacpan.org/pod/Crypt::Checksum::Adler32), [Crypt::Checksum::CRC32](https://metacpan.org/pod/Crypt::Checksum::CRC32)
43 [Crypt::Checksum::Adler32](https://metacpan.org/pod/Crypt::Checksum::Adler32), [Crypt::Checksum::CRC32](https://metacpan.org/pod/Crypt::Checksum::CRC32)
4344
4445 - Message Authentication Codes
4546
5051
5152 [Crypt::PK::RSA](https://metacpan.org/pod/Crypt::PK::RSA), [Crypt::PK::DSA](https://metacpan.org/pod/Crypt::PK::DSA), [Crypt::PK::ECC](https://metacpan.org/pod/Crypt::PK::ECC), [Crypt::PK::DH](https://metacpan.org/pod/Crypt::PK::DH)
5253
53 - Cryptographically secure random number generators
54 - Cryptographically secure random number generators - see [Crypt::PRNG](https://metacpan.org/pod/Crypt::PRNG) and related modules
5455
55 [Crypt::PRNG](https://metacpan.org/pod/Crypt::PRNG), [Crypt::PRNG::Fortuna](https://metacpan.org/pod/Crypt::PRNG::Fortuna), [Crypt::PRNG::Yarrow](https://metacpan.org/pod/Crypt::PRNG::Yarrow), [Crypt::PRNG::RC4](https://metacpan.org/pod/Crypt::PRNG::RC4), [Crypt::PRNG::Sober128](https://metacpan.org/pod/Crypt::PRNG::Sober128), [Crypt::PRNG::ChaCha20](https://metacpan.org/pod/Crypt::PRNG::ChaCha20)
56 [Crypt::PRNG::Fortuna](https://metacpan.org/pod/Crypt::PRNG::Fortuna), [Crypt::PRNG::Yarrow](https://metacpan.org/pod/Crypt::PRNG::Yarrow), [Crypt::PRNG::RC4](https://metacpan.org/pod/Crypt::PRNG::RC4), [Crypt::PRNG::Sober128](https://metacpan.org/pod/Crypt::PRNG::Sober128), [Crypt::PRNG::ChaCha20](https://metacpan.org/pod/Crypt::PRNG::ChaCha20)
5657
5758 - Key derivation functions - PBKDF1, PBKDF2 and HKDF
5859
6869
6970 # COPYRIGHT
7071
71 Copyright (c) 2013+ DCIT, a.s. [http://www.dcit.cz](http://www.dcit.cz) / Karel Miko
72 Copyright (c) 2013+ DCIT, a.s. [https://www.dcit.cz](https://www.dcit.cz) / Karel Miko
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::CCM
11
2 PROTOTYPES: DISABLE
3
24 Crypt::AuthEnc::CCM
3 _new(char * cipher_name, SV * key, SV * nonce, SV * adata, int tag_len, int pt_len)
5 new(Class, char * cipher_name, SV * key, SV * nonce, SV * adata, int tag_len, int pt_len)
46 CODE:
57 {
68 unsigned char *k=NULL;
2022 if (!SvPOK(adata)) croak("FATAL: adata must be string/buffer scalar");
2123 h = (unsigned char *) SvPVbyte(adata, h_len);
2224
23 id = find_cipher(cipher_name);
25 id = _find_cipher(cipher_name);
2426 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
2527
26 Newz(0, RETVAL, 1, struct ccm_struct);
28 Newz(0, RETVAL, 1, ccm_state);
2729 if (!RETVAL) croak("FATAL: Newz failed");
2830
29 rv = ccm_init(&RETVAL->state, id, k, (int)k_len, (int)pt_len, (int)tag_len, (int)h_len); /* XXX-TODO why int? */
31 rv = ccm_init(RETVAL, id, k, (int)k_len, (int)pt_len, (int)tag_len, (int)h_len); /* XXX-TODO why int? */
3032 if (rv != CRYPT_OK) {
3133 Safefree(RETVAL);
3234 croak("FATAL: ccm_init failed: %s", error_to_string(rv));
3335 }
34 rv = ccm_add_nonce(&RETVAL->state, n, (unsigned long)n_len);
36 rv = ccm_add_nonce(RETVAL, n, (unsigned long)n_len);
3537 if (rv != CRYPT_OK) {
3638 Safefree(RETVAL);
3739 croak("FATAL: ccm_add_nonce failed: %s", error_to_string(rv));
3840 }
39 rv = ccm_add_aad(&RETVAL->state, h, (unsigned long)h_len);
41 rv = ccm_add_aad(RETVAL, h, (unsigned long)h_len);
4042 if (rv != CRYPT_OK) {
4143 Safefree(RETVAL);
4244 croak("FATAL: ccm_add_aad failed: %s", error_to_string(rv));
4345 }
44 RETVAL->direction = -1;
45 RETVAL->tag_len = tag_len;
46 RETVAL->pt_len = pt_len;
4746 }
4847 OUTPUT:
4948 RETVAL
5655 Crypt::AuthEnc::CCM
5756 clone(Crypt::AuthEnc::CCM self)
5857 CODE:
59 Newz(0, RETVAL, 1, struct ccm_struct);
58 Newz(0, RETVAL, 1, ccm_state);
6059 if (!RETVAL) croak("FATAL: Newz failed");
61 Copy(&self->state, &RETVAL->state, 1, struct ccm_struct);
60 Copy(self, RETVAL, 1, ccm_state);
6261 OUTPUT:
6362 RETVAL
6463
7574 RETVAL = newSVpvn("", 0);
7675 }
7776 else {
78 if (self->direction == -1) self->direction = CCM_ENCRYPT;
79 if (self->direction != CCM_ENCRYPT) {
80 croak("FATAL: encrypt_add failed: wrong direction");
81 }
82 if (self->pt_len < in_data_len) croak("FATAL: encrypt_add failed: pt_len mismatch");
83 RETVAL = NEWSV(0, in_data_len);
77 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
8478 SvPOK_only(RETVAL);
8579 SvCUR_set(RETVAL, in_data_len);
8680 out_data = (unsigned char *)SvPVX(RETVAL);
87 rv = ccm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, self->direction);
81 rv = ccm_process(self, in_data, (unsigned long)in_data_len, out_data, CCM_ENCRYPT);
8882 if (rv != CRYPT_OK) {
8983 SvREFCNT_dec(RETVAL);
9084 croak("FATAL: ccm_process failed: %s", error_to_string(rv));
9185 }
92 self->pt_len -= (unsigned long)in_data_len;
9386 }
9487 }
9588 OUTPUT:
108101 RETVAL = newSVpvn("", 0);
109102 }
110103 else {
111 if (self->direction == -1) self->direction = CCM_DECRYPT;
112 if (self->direction != CCM_DECRYPT) {
113 croak("FATAL: decrypt_add failed: wrong direction");
114 }
115 if (self->pt_len < in_data_len) croak("FATAL: decrypt_add failed: pt_len mismatch");
116 RETVAL = NEWSV(0, in_data_len);
104 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
117105 SvPOK_only(RETVAL);
118106 SvCUR_set(RETVAL, in_data_len);
119107 out_data = (unsigned char *)SvPVX(RETVAL);
120 rv = ccm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, CCM_DECRYPT);
108 rv = ccm_process(self, out_data, (unsigned long)in_data_len, in_data, CCM_DECRYPT);
121109 if (rv != CRYPT_OK) {
122110 SvREFCNT_dec(RETVAL);
123111 croak("FATAL: ccm_process failed: %s", error_to_string(rv));
124112 }
125 self->pt_len -= (unsigned long)in_data_len;
126113 }
127114 }
128115 OUTPUT:
134121 {
135122 int rv;
136123 unsigned char tag[MAXBLOCKSIZE];
137 unsigned long tag_len = self->tag_len;
138
139 if (self->direction != CCM_ENCRYPT) {
140 croak("FATAL: encrypt_done failed: wrong direction");
141 }
142 if (self->pt_len != 0) croak("FATAL: encrypt_done failed: pt_len mismatch");
143 rv = ccm_done(&self->state, tag, &tag_len);
124 unsigned long tag_len = MAXBLOCKSIZE;
125
126 rv = ccm_done(self, tag, &tag_len);
144127 if (rv != CRYPT_OK) croak("FATAL: ccm_done failed: %s", error_to_string(rv));
145128 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
146129 }
151134 {
152135 int rv;
153136 unsigned char tag[MAXBLOCKSIZE];
154 unsigned long tag_len = self->tag_len;
137 unsigned long tag_len = MAXBLOCKSIZE;
155138 STRLEN expected_tag_len;
156139 unsigned char *expected_tag;
157140
158 if (self->direction != CCM_DECRYPT) {
159 croak("FATAL: decrypt_done failed: wrong direction");
160 }
161 if (self->pt_len != 0) croak("FATAL: decrypt_done failed: pt_len mismatch");
162 rv = ccm_done(&self->state, tag, &tag_len);
141 rv = ccm_done(self, tag, &tag_len);
163142 if (rv != CRYPT_OK) croak("FATAL: ccm_done failed: %s", error_to_string(rv));
164143 if (items == 1) {
165144 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
178157 }
179158 }
180159 }
160
161 void
162 ccm_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *header, unsigned long tag_len, SV *plaintext)
163 PPCODE:
164 {
165 STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0;
166 unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL;
167 int rv, id;
168 unsigned char tag[MAXBLOCKSIZE];
169 SV *output;
170
171 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
172 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
173 if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
174 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
175
176 id = _find_cipher(cipher_name);
177 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
178 output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */
179 SvPOK_only(output);
180 SvCUR_set(output, pt_len);
181 if(tag_len < 4 || tag_len > 16) tag_len = 16;
182
183 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
184 pt, (unsigned long)pt_len, (unsigned char *)SvPVX(output), tag, &tag_len, CCM_ENCRYPT);
185
186 if (rv != CRYPT_OK) {
187 SvREFCNT_dec(output);
188 croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
189 }
190 XPUSHs(sv_2mortal(output));
191 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
192 }
193
194 void
195 ccm_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv)
196 PPCODE:
197 {
198 STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0;
199 unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL;
200 int rv, id;
201 unsigned char tag[MAXBLOCKSIZE];
202 unsigned long tag_len;
203 SV *output;
204
205 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
206 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
207 if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
208 if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len);
209 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
210
211 id = _find_cipher(cipher_name);
212 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
213 output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */
214 SvPOK_only(output);
215 SvCUR_set(output, ct_len);
216 tag_len = (unsigned long)t_len;
217 Copy(t, tag, t_len, unsigned char);
218
219 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
220 (unsigned char *)SvPVX(output), (unsigned long)ct_len, ct, tag, &tag_len, CCM_DECRYPT);
221
222 if (rv != CRYPT_OK) {
223 SvREFCNT_dec(output);
224 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
225 }
226 else {
227 XPUSHs(sv_2mortal(output));
228 }
229 }
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::ChaCha20Poly1305
11
2 PROTOTYPES: DISABLE
3
24 Crypt::AuthEnc::ChaCha20Poly1305
3 _new(SV * key, SV * nonce = NULL)
5 new(Class, SV * key, SV * nonce = NULL)
46 CODE:
57 {
68 int rv;
1416 iv = (unsigned char *) SvPVbyte(nonce, iv_len);
1517 }
1618
17 Newz(0, RETVAL, 1, struct chacha20poly1305_struct);
19 Newz(0, RETVAL, 1, chacha20poly1305_state);
1820 if (!RETVAL) croak("FATAL: Newz failed");
1921
20 rv = chacha20poly1305_init(&RETVAL->state, k, (unsigned long)k_len);
22 rv = chacha20poly1305_init(RETVAL, k, (unsigned long)k_len);
2123 if (rv != CRYPT_OK) {
2224 Safefree(RETVAL);
2325 croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv));
2426 }
2527
2628 if (iv && iv_len > 0) {
27 rv = chacha20poly1305_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
29 rv = chacha20poly1305_setiv(RETVAL, iv, (unsigned long)iv_len);
2830 if (rv != CRYPT_OK) {
2931 Safefree(RETVAL);
3032 croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
4244 Crypt::AuthEnc::ChaCha20Poly1305
4345 clone(Crypt::AuthEnc::ChaCha20Poly1305 self)
4446 CODE:
45 Newz(0, RETVAL, 1, struct chacha20poly1305_struct);
47 Newz(0, RETVAL, 1, chacha20poly1305_state);
4648 if (!RETVAL) croak("FATAL: Newz failed");
47 Copy(&self->state, &RETVAL->state, 1, struct chacha20poly1305_struct);
49 Copy(self, RETVAL, 1, chacha20poly1305_state);
4850 OUTPUT:
4951 RETVAL
5052
5860
5961 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
6062 iv = (unsigned char *) SvPVbyte(nonce, iv_len);
61 rv = chacha20poly1305_setiv(&self->state, iv, (unsigned long)iv_len);
63 rv = chacha20poly1305_setiv(self, iv, (unsigned long)iv_len);
6264 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
6365 XPUSHs(ST(0)); /* return self */;
6466 }
7375
7476 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
7577 iv = (unsigned char *) SvPVbyte(nonce, iv_len);
76 rv = chacha20poly1305_setiv_rfc7905(&self->state, iv, (unsigned long)iv_len, (ulong64)seqnum);
78 rv = chacha20poly1305_setiv_rfc7905(self, iv, (unsigned long)iv_len, (ulong64)seqnum);
7779 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv_rfc7905 failed: %s", error_to_string(rv));
7880 XPUSHs(ST(0)); /* return self */
7981 }
8789 unsigned char *in_data;
8890
8991 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
90 rv = chacha20poly1305_add_aad(&self->state, in_data, (unsigned long)in_data_len);
92 rv = chacha20poly1305_add_aad(self, in_data, (unsigned long)in_data_len);
9193 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_add_aad failed: %s", error_to_string(rv));
9294 XPUSHs(ST(0)); /* return self */
9395 }
105107 RETVAL = newSVpvn("", 0);
106108 }
107109 else {
108 RETVAL = NEWSV(0, in_data_len);
110 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
109111 SvPOK_only(RETVAL);
110112 SvCUR_set(RETVAL, in_data_len);
111113 out_data = (unsigned char *)SvPVX(RETVAL);
112 rv = chacha20poly1305_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
114 rv = chacha20poly1305_decrypt(self, in_data, (unsigned long)in_data_len, out_data);
113115 if (rv != CRYPT_OK) {
114116 SvREFCNT_dec(RETVAL);
115117 croak("FATAL: chacha20poly1305_decrypt failed: %s", error_to_string(rv));
132134 RETVAL = newSVpvn("", 0);
133135 }
134136 else {
135 RETVAL = NEWSV(0, in_data_len);
137 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
136138 SvPOK_only(RETVAL);
137139 SvCUR_set(RETVAL, in_data_len);
138140 out_data = (unsigned char *)SvPVX(RETVAL);
139 rv = chacha20poly1305_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
141 rv = chacha20poly1305_encrypt(self, in_data, (unsigned long)in_data_len, out_data);
140142 if (rv != CRYPT_OK) {
141143 SvREFCNT_dec(RETVAL);
142144 croak("FATAL: chacha20poly1305_encrypt failed: %s", error_to_string(rv));
154156 unsigned char tag[MAXBLOCKSIZE];
155157 unsigned long tag_len = sizeof(tag);
156158
157 rv = chacha20poly1305_done(&self->state, tag, &tag_len);
159 rv = chacha20poly1305_done(self, tag, &tag_len);
158160 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_done failed: %s", error_to_string(rv));
159161 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
160162 }
169171 STRLEN expected_tag_len;
170172 unsigned char *expected_tag;
171173
172 rv = chacha20poly1305_done(&self->state, tag, &tag_len);
174 rv = chacha20poly1305_done(self, tag, &tag_len);
173175 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_done failed: %s", error_to_string(rv));
174176 if (items == 1) {
175177 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
188190 }
189191 }
190192 }
193
194 void
195 chacha20poly1305_encrypt_authenticate(SV *key, SV *nonce, SV *header, SV *plaintext)
196 PPCODE:
197 {
198 STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0;
199 unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL;
200 int rv;
201 unsigned char tag[MAXBLOCKSIZE];
202 unsigned long tag_len = sizeof(tag);
203 SV *output;
204
205 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
206 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
207 if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
208 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
209
210 output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */
211 SvPOK_only(output);
212 SvCUR_set(output, pt_len);
213
214 rv = chacha20poly1305_memory(k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len,
215 pt, (unsigned long)pt_len, (unsigned char *)SvPVX(output), tag, &tag_len,
216 CHACHA20POLY1305_ENCRYPT);
217
218 if (rv != CRYPT_OK) {
219 SvREFCNT_dec(output);
220 croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
221 }
222 XPUSHs(sv_2mortal(output));
223 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
224 }
225
226 void
227 chacha20poly1305_decrypt_verify(SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv)
228 PPCODE:
229 {
230 STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0;
231 unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL;
232 int rv;
233 unsigned char tag[MAXBLOCKSIZE];
234 unsigned long tag_len;
235 SV *output;
236
237 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
238 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
239 if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
240 if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len);
241 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
242
243 output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */
244 SvPOK_only(output);
245 SvCUR_set(output, ct_len);
246 tag_len = (unsigned long)t_len;
247 Copy(t, tag, t_len, unsigned char);
248
249 rv = chacha20poly1305_memory(k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len,
250 ct, (unsigned long)ct_len, (unsigned char *)SvPVX(output), tag, &tag_len,
251 CHACHA20POLY1305_DECRYPT);
252
253 if (rv != CRYPT_OK) {
254 SvREFCNT_dec(output);
255 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
256 }
257 else {
258 XPUSHs(sv_2mortal(output));
259 }
260 }
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::EAX
11
2 PROTOTYPES: DISABLE
3
24 Crypt::AuthEnc::EAX
3 _new(char * cipher_name, SV * key, SV * nonce, SV * adata=&PL_sv_undef)
5 new(Class, char * cipher_name, SV * key, SV * nonce, SV * adata=&PL_sv_undef)
46 CODE:
57 {
68 STRLEN k_len=0;
2022 h = (unsigned char *) SvPVbyte(adata, h_len);
2123 }
2224
23 id = find_cipher(cipher_name);
25 id = _find_cipher(cipher_name);
2426 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
2527
26 Newz(0, RETVAL, 1, struct eax_struct);
28 Newz(0, RETVAL, 1, eax_state);
2729 if (!RETVAL) croak("FATAL: Newz failed");
2830
29 rv = eax_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len);
31 rv = eax_init(RETVAL, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len);
3032 if (rv != CRYPT_OK) {
3133 Safefree(RETVAL);
3234 croak("FATAL: eax setup failed: %s", error_to_string(rv));
4345 Crypt::AuthEnc::EAX
4446 clone(Crypt::AuthEnc::EAX self)
4547 CODE:
46 Newz(0, RETVAL, 1, struct eax_struct);
48 Newz(0, RETVAL, 1, eax_state);
4749 if (!RETVAL) croak("FATAL: Newz failed");
48 Copy(&self->state, &RETVAL->state, 1, struct eax_struct);
50 Copy(self, RETVAL, 1, eax_state);
4951 OUTPUT:
5052 RETVAL
5153
6264 RETVAL = newSVpvn("", 0);
6365 }
6466 else {
65 RETVAL = NEWSV(0, in_data_len);
67 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
6668 SvPOK_only(RETVAL);
6769 SvCUR_set(RETVAL, in_data_len);
6870 out_data = (unsigned char *)SvPVX(RETVAL);
69 rv = eax_encrypt(&self->state, in_data, out_data, (unsigned long)in_data_len);
71 rv = eax_encrypt(self, in_data, out_data, (unsigned long)in_data_len);
7072 if (rv != CRYPT_OK) {
7173 SvREFCNT_dec(RETVAL);
7274 croak("FATAL: eax_encrypt failed: %s", error_to_string(rv));
8991 RETVAL = newSVpvn("", 0);
9092 }
9193 else {
92 RETVAL = NEWSV(0, in_data_len);
94 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
9395 SvPOK_only(RETVAL);
9496 SvCUR_set(RETVAL, in_data_len);
9597 out_data = (unsigned char *)SvPVX(RETVAL);
96 rv = eax_decrypt(&self->state, in_data, out_data, (unsigned long)in_data_len);
98 rv = eax_decrypt(self, in_data, out_data, (unsigned long)in_data_len);
9799 if (rv != CRYPT_OK) {
98100 SvREFCNT_dec(RETVAL);
99101 croak("FATAL: eax_decrypt failed: %s", error_to_string(rv));
111113 unsigned char tag[MAXBLOCKSIZE];
112114 unsigned long tag_len = sizeof(tag);
113115
114 rv = eax_done(&self->state, tag, &tag_len);
116 rv = eax_done(self, tag, &tag_len);
115117 if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv));
116118 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
117119 }
126128 STRLEN expected_tag_len;
127129 unsigned char *expected_tag;
128130
129 rv = eax_done(&self->state, tag, &tag_len);
131 rv = eax_done(self, tag, &tag_len);
130132 if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv));
131133 if (items == 1) {
132134 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
154156 int rv;
155157 unsigned char *h;
156158 h = (unsigned char *)SvPVbyte(adata, h_len);
157 rv = eax_addheader(&self->state, h, (unsigned long)h_len);
159 rv = eax_addheader(self, h, (unsigned long)h_len);
158160 if (rv != CRYPT_OK) croak("FATAL: eax_addheader failed: %s", error_to_string(rv));
159161 XPUSHs(ST(0)); /* return self */
160162 }
163
164 void
165 eax_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *header, SV *plaintext)
166 PPCODE:
167 {
168 STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0;
169 unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL;
170 int rv, id;
171 unsigned char tag[MAXBLOCKSIZE];
172 unsigned long tag_len = sizeof(tag);
173 SV *output;
174
175 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
176 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
177 if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
178 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
179
180 id = _find_cipher(cipher_name);
181 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
182 output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */
183 SvPOK_only(output);
184 SvCUR_set(output, pt_len);
185
186 rv = eax_encrypt_authenticate_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len,
187 h, (unsigned long)h_len, pt, (unsigned long)pt_len,
188 (unsigned char *)SvPVX(output), tag, &tag_len);
189
190 if (rv != CRYPT_OK) {
191 SvREFCNT_dec(output);
192 croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
193 }
194 XPUSHs(sv_2mortal(output));
195 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
196 }
197
198 void
199 eax_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv)
200 PPCODE:
201 {
202 STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0;
203 unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL;
204 int rv, id, stat = 0;
205 unsigned char tag[MAXBLOCKSIZE];
206 unsigned long tag_len;
207 SV *output;
208
209 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
210 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
211 if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
212 if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len);
213 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
214
215 id = _find_cipher(cipher_name);
216 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
217 output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */
218 SvPOK_only(output);
219 SvCUR_set(output, ct_len);
220 tag_len = (unsigned long)t_len;
221 Copy(t, tag, t_len, unsigned char);
222
223 rv = eax_decrypt_verify_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len,
224 ct, (unsigned long)ct_len, (unsigned char *)SvPVX(output), tag, tag_len, &stat);
225
226 if (rv != CRYPT_OK || stat != 1) {
227 SvREFCNT_dec(output);
228 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
229 }
230 else {
231 XPUSHs(sv_2mortal(output));
232 }
233 }
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM
11
2 PROTOTYPES: DISABLE
3
24 Crypt::AuthEnc::GCM
3 _new(char * cipher_name, SV * key, SV * nonce = NULL)
5 new(Class, char * cipher_name, SV * key, SV * nonce = NULL)
46 CODE:
57 {
68 STRLEN k_len = 0, iv_len = 0;
1416 iv = (unsigned char *)SvPVbyte(nonce, iv_len);
1517 }
1618
17 id = find_cipher(cipher_name);
19 id = _find_cipher(cipher_name);
1820 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1921
20 Newz(0, RETVAL, 1, struct gcm_struct);
22 Newz(0, RETVAL, 1, gcm_state);
2123 if (!RETVAL) croak("FATAL: Newz failed");
2224
23 rv = gcm_init(&RETVAL->state, id, k, (unsigned long)k_len);
25 rv = gcm_init(RETVAL, id, k, (unsigned long)k_len);
2426 if (rv != CRYPT_OK) {
2527 Safefree(RETVAL);
2628 croak("FATAL: gcm_init failed: %s", error_to_string(rv));
2729 }
2830
2931 if (iv && iv_len > 0) {
30 rv = gcm_add_iv(&RETVAL->state, iv, (unsigned long)iv_len);
32 rv = gcm_add_iv(RETVAL, iv, (unsigned long)iv_len);
3133 if (rv != CRYPT_OK) {
3234 Safefree(RETVAL);
3335 croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
4547 Crypt::AuthEnc::GCM
4648 clone(Crypt::AuthEnc::GCM self)
4749 CODE:
48 Newz(0, RETVAL, 1, struct gcm_struct);
50 Newz(0, RETVAL, 1, gcm_state);
4951 if (!RETVAL) croak("FATAL: Newz failed");
50 Copy(&self->state, &RETVAL->state, 1, struct gcm_struct);
52 Copy(self, RETVAL, 1, gcm_state);
5153 OUTPUT:
5254 RETVAL
5355
5658 PPCODE:
5759 {
5860 int rv;
59 rv = gcm_reset(&self->state);
61 rv = gcm_reset(self);
6062 if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv));
6163 XPUSHs(ST(0)); /* return self */
6264 }
7577 }
7678 else
7779 {
78 RETVAL = NEWSV(0, in_data_len);
80 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
7981 SvPOK_only(RETVAL);
8082 SvCUR_set(RETVAL, in_data_len);
8183 out_data = (unsigned char *)SvPVX(RETVAL);
82 rv = gcm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT);
84 rv = gcm_process(self, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT);
8385 if (rv != CRYPT_OK) {
8486 SvREFCNT_dec(RETVAL);
8587 croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
98100 unsigned char *in_data;
99101
100102 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
101 rv = gcm_add_iv(&self->state, in_data, (unsigned long)in_data_len);
103 rv = gcm_add_iv(self, in_data, (unsigned long)in_data_len);
102104 if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
103105 XPUSHs(ST(0)); /* return self */
104106 }
112114 unsigned char *in_data;
113115
114116 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
115 rv = gcm_add_aad(&self->state, in_data, (unsigned long)in_data_len);
117 rv = gcm_add_aad(self, in_data, (unsigned long)in_data_len);
116118 if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv));
117119 XPUSHs(ST(0)); /* return self */
118120 }
130132 RETVAL = newSVpvn("", 0);
131133 }
132134 else {
133 RETVAL = NEWSV(0, in_data_len);
135 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
134136 SvPOK_only(RETVAL);
135137 SvCUR_set(RETVAL, in_data_len);
136138 out_data = (unsigned char *)SvPVX(RETVAL);
137 rv = gcm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT);
139 rv = gcm_process(self, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT);
138140 if (rv != CRYPT_OK) {
139141 SvREFCNT_dec(RETVAL);
140142 croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
153155 unsigned char tag[MAXBLOCKSIZE];
154156 unsigned long tag_len = sizeof(tag);
155157
156 rv = gcm_done(&self->state, tag, &tag_len);
158 rv = gcm_done(self, tag, &tag_len);
157159 if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv));
158160 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
159161 }
168170 STRLEN expected_tag_len;
169171 unsigned char *expected_tag;
170172
171 rv = gcm_done(&self->state, tag, &tag_len);
173 rv = gcm_done(self, tag, &tag_len);
172174 if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv));
173175 if (items == 1) {
174176 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
187189 }
188190 }
189191 }
192
193 void
194 gcm_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *header = NULL, SV *plaintext)
195 PPCODE:
196 {
197 STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0;
198 unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL;
199 int rv, id;
200 unsigned char tag[MAXBLOCKSIZE];
201 unsigned long tag_len = sizeof(tag);
202 SV *output;
203
204 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
205 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
206 if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
207 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
208
209 id = _find_cipher(cipher_name);
210 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
211 output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */
212 SvPOK_only(output);
213 SvCUR_set(output, pt_len);
214
215 rv = gcm_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len,
216 pt, (unsigned long)pt_len, (unsigned char *)SvPVX(output), tag, &tag_len, GCM_ENCRYPT);
217
218 if (rv != CRYPT_OK) {
219 SvREFCNT_dec(output);
220 croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
221 }
222 XPUSHs(sv_2mortal(output));
223 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
224 }
225
226 void
227 gcm_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv)
228 PPCODE:
229 {
230 STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0;
231 unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL;
232 int rv, id;
233 unsigned char tag[MAXBLOCKSIZE];
234 unsigned long tag_len;
235 SV *output;
236
237 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
238 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
239 if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
240 if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len);
241 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
242
243 id = _find_cipher(cipher_name);
244 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
245 output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */
246 SvPOK_only(output);
247 SvCUR_set(output, ct_len);
248 tag_len = (unsigned long)t_len;
249 Copy(t, tag, t_len, unsigned char);
250
251 rv = gcm_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len,
252 (unsigned char *)SvPVX(output), (unsigned long)ct_len, ct, tag, &tag_len, GCM_DECRYPT);
253
254 if (rv != CRYPT_OK) {
255 SvREFCNT_dec(output);
256 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
257 }
258 else {
259 XPUSHs(sv_2mortal(output));
260 }
261 }
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::OCB
11
2 PROTOTYPES: DISABLE
3
24 Crypt::AuthEnc::OCB
3 _new(char * cipher_name, SV * key, SV * nonce, unsigned long taglen)
5 new(Class, char * cipher_name, SV * key, SV * nonce, unsigned long taglen)
46 CODE:
57 {
68 STRLEN k_len=0;
1416 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
1517 n = (unsigned char *) SvPVbyte(nonce, n_len);
1618
17 id = find_cipher(cipher_name);
19 id = _find_cipher(cipher_name);
1820 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1921
20 Newz(0, RETVAL, 1, struct ocb_struct);
22 Newz(0, RETVAL, 1, ocb3_state);
2123 if (!RETVAL) croak("FATAL: Newz failed");
2224
23 rv = ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, taglen);
25 rv = ocb3_init(RETVAL, id, k, (unsigned long)k_len, n, (unsigned long)n_len, taglen);
2426 if (rv != CRYPT_OK) {
2527 Safefree(RETVAL);
2628 croak("FATAL: ocb setup failed: %s", error_to_string(rv));
3739 Crypt::AuthEnc::OCB
3840 clone(Crypt::AuthEnc::OCB self)
3941 CODE:
40 Newz(0, RETVAL, 1, struct ocb_struct);
42 Newz(0, RETVAL, 1, ocb3_state);
4143 if (!RETVAL) croak("FATAL: Newz failed");
42 Copy(&self->state, &RETVAL->state, 1, struct ocb_struct);
44 Copy(self, RETVAL, 1, ocb3_state);
4345 OUTPUT:
4446 RETVAL
4547
5456 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
5557
5658 if (in_data_len>0) {
57 rv = ocb3_add_aad(&self->state, in_data, (unsigned long)in_data_len);
59 rv = ocb3_add_aad(self, in_data, (unsigned long)in_data_len);
5860 if (rv != CRYPT_OK) croak("FATAL: ocb3_add_aad failed: %s", error_to_string(rv));
5961 }
6062 XPUSHs(ST(0)); /* return self */
7678 if (in_data_len % 16) {
7779 croak ("FATAL: sizeof(data) should be multiple of 16");
7880 }
79 RETVAL = NEWSV(0, in_data_len);
80 SvPOK_only(RETVAL);
81 SvCUR_set(RETVAL, in_data_len);
82 out_data = (unsigned char *)SvPVX(RETVAL);
83 rv = ocb3_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
81 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
82 SvPOK_only(RETVAL);
83 SvCUR_set(RETVAL, in_data_len);
84 out_data = (unsigned char *)SvPVX(RETVAL);
85 rv = ocb3_encrypt(self, in_data, (unsigned long)in_data_len, out_data);
8486 if (rv != CRYPT_OK) {
8587 SvREFCNT_dec(RETVAL);
8688 croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv));
100102
101103 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
102104 if (in_data_len == 0) {
103 rv = ocb3_encrypt_last(&self->state, in_data, 0, NULL);
104 if (rv != CRYPT_OK) {
105 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
106 }
107 RETVAL = newSVpvn("", 0);
108 }
109 else {
110 RETVAL = NEWSV(0, in_data_len);
111 SvPOK_only(RETVAL);
112 SvCUR_set(RETVAL, in_data_len);
113 out_data = (unsigned char *)SvPVX(RETVAL);
114 rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
105 rv = ocb3_encrypt_last(self, in_data, 0, NULL);
106 if (rv != CRYPT_OK) {
107 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
108 }
109 RETVAL = newSVpvn("", 0);
110 }
111 else {
112 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
113 SvPOK_only(RETVAL);
114 SvCUR_set(RETVAL, in_data_len);
115 out_data = (unsigned char *)SvPVX(RETVAL);
116 rv = ocb3_encrypt_last(self, in_data, (unsigned long)in_data_len, out_data);
115117 if (rv != CRYPT_OK) {
116118 SvREFCNT_dec(RETVAL);
117119 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
137139 if (in_data_len % 16) {
138140 croak ("FATAL: sizeof(data) should be multiple of 16");
139141 }
140 RETVAL = NEWSV(0, in_data_len);
141 SvPOK_only(RETVAL);
142 SvCUR_set(RETVAL, in_data_len);
143 out_data = (unsigned char *)SvPVX(RETVAL);
144 rv = ocb3_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
142 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
143 SvPOK_only(RETVAL);
144 SvCUR_set(RETVAL, in_data_len);
145 out_data = (unsigned char *)SvPVX(RETVAL);
146 rv = ocb3_decrypt(self, in_data, (unsigned long)in_data_len, out_data);
145147 if (rv != CRYPT_OK) {
146148 SvREFCNT_dec(RETVAL);
147149 croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv));
161163
162164 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
163165 if (in_data_len == 0) {
164 rv = ocb3_decrypt_last(&self->state, in_data, 0, NULL);
165 if (rv != CRYPT_OK) {
166 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
167 }
168 RETVAL = newSVpvn("", 0);
169 }
170 else {
171 RETVAL = NEWSV(0, in_data_len);
172 SvPOK_only(RETVAL);
173 SvCUR_set(RETVAL, in_data_len);
174 out_data = (unsigned char *)SvPVX(RETVAL);
175 rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
166 rv = ocb3_decrypt_last(self, in_data, 0, NULL);
167 if (rv != CRYPT_OK) {
168 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
169 }
170 RETVAL = newSVpvn("", 0);
171 }
172 else {
173 RETVAL = NEWSV(0, in_data_len); /* avoid zero! */
174 SvPOK_only(RETVAL);
175 SvCUR_set(RETVAL, in_data_len);
176 out_data = (unsigned char *)SvPVX(RETVAL);
177 rv = ocb3_decrypt_last(self, in_data, (unsigned long)in_data_len, out_data);
176178 if (rv != CRYPT_OK) {
177179 SvREFCNT_dec(RETVAL);
178180 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
190192 unsigned char tag[MAXBLOCKSIZE];
191193 unsigned long tag_len = sizeof(tag);
192194
193 rv = ocb3_done(&self->state, tag, &tag_len);
195 rv = ocb3_done(self, tag, &tag_len);
194196 if (rv != CRYPT_OK) croak("FATAL: ocb3_done_encrypt failed: %s", error_to_string(rv));
195197
196198 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
206208 STRLEN expected_tag_len;
207209 unsigned char *expected_tag;
208210
209 rv = ocb3_done(&self->state, tag, &tag_len);
211 rv = ocb3_done(self, tag, &tag_len);
210212 if (rv != CRYPT_OK) croak("FATAL: ocb3_done_decrypt failed: %s", error_to_string(rv));
211213 if (items == 1) {
212214 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
225227 }
226228 }
227229 }
230
231 void
232 ocb_encrypt_authenticate(char *cipher_name, SV *key, SV *nonce, SV *header, unsigned long tag_len, SV *plaintext)
233 PPCODE:
234 {
235 STRLEN k_len = 0, n_len = 0, h_len = 0, pt_len = 0;
236 unsigned char *k = NULL, *n = NULL, *h = NULL, *pt = NULL;
237 int rv, id;
238 unsigned char tag[MAXBLOCKSIZE];
239 SV *output;
240
241 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
242 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
243 if (SvPOK(plaintext)) pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
244 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
245
246 id = _find_cipher(cipher_name);
247 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
248 output = NEWSV(0, pt_len > 0 ? pt_len : 1); /* avoid zero! */
249 SvPOK_only(output);
250 SvCUR_set(output, pt_len);
251 if(tag_len < 4 || tag_len > 16) tag_len = 16;
252
253 rv = ocb3_encrypt_authenticate_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len,
254 h, (unsigned long)h_len, pt, (unsigned long)pt_len,
255 (unsigned char *)SvPVX(output), tag, &tag_len);
256
257 if (rv != CRYPT_OK) {
258 SvREFCNT_dec(output);
259 croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
260 }
261 XPUSHs(sv_2mortal(output));
262 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
263 }
264
265 void
266 ocb_decrypt_verify(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tagsv)
267 PPCODE:
268 {
269 STRLEN k_len = 0, n_len = 0, h_len = 0, ct_len = 0, t_len = 0;
270 unsigned char *k = NULL, *n = NULL, *h = NULL, *ct = NULL, *t = NULL;
271 int rv, id, stat = 0;
272 SV *output;
273
274 if (SvPOK(key)) k = (unsigned char *) SvPVbyte(key, k_len);
275 if (SvPOK(nonce)) n = (unsigned char *) SvPVbyte(nonce, n_len);
276 if (SvPOK(ciphertext)) ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
277 if (SvPOK(tagsv)) t = (unsigned char *) SvPVbyte(tagsv, t_len);
278 if (SvPOK(header)) h = (unsigned char *) SvPVbyte(header, h_len);
279
280 id = _find_cipher(cipher_name);
281 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
282 output = NEWSV(0, ct_len > 0 ? ct_len : 1); /* avoid zero! */
283 SvPOK_only(output);
284 SvCUR_set(output, ct_len);
285
286 rv = ocb3_decrypt_verify_memory(id, k, (unsigned long)k_len, n, (unsigned long)n_len,
287 h, (unsigned long)h_len, ct, (unsigned long)ct_len,
288 (unsigned char *)SvPVX(output), t, (unsigned long)t_len, &stat);
289
290 if (rv != CRYPT_OK || stat != 1) {
291 SvREFCNT_dec(output);
292 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
293 }
294 else {
295 XPUSHs(sv_2mortal(output));
296 }
297 }
00 MODULE = CryptX PACKAGE = Math::BigInt::LTM
11
2 PROTOTYPES: DISABLE
23
34 ##############################################################################
45 # _new()
00 MODULE = CryptX PACKAGE = Crypt::Checksum::Adler32
1
2 PROTOTYPES: DISABLE
13
24 Crypt::Checksum::Adler32
35 new(Class)
5052
5153 SV *
5254 digest(Crypt::Checksum::Adler32 self)
55 ALIAS:
56 hexdigest = 1
57 intdigest = 2
5358 CODE:
5459 {
55 unsigned char hash[4];
60 int rv;
61 unsigned char hash[4], out[8];
62 unsigned long outlen = 8;
63 unsigned int ui32;
64
5665 adler32_finish(self, hash, 4); /* returns void */
57 RETVAL = newSVpvn((char *) hash, 4);
66 if (ix == 1) {
67 rv = _base16_encode(hash, 4, out, &outlen);
68 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
69 RETVAL = newSVpvn((char *)out, outlen);
70 }
71 else if (ix == 2) {
72 LOAD32H(ui32, hash);
73 RETVAL = newSVuv(ui32);
74 }
75 else {
76 RETVAL = newSVpvn((char *) hash, 4);
77 }
5878 }
5979 OUTPUT:
6080 RETVAL
6181
6282 SV *
63 hexdigest(Crypt::Checksum::Adler32 self)
83 adler32_data(...)
84 ALIAS:
85 adler32_data_hex = 1
86 adler32_data_int = 2
6487 CODE:
6588 {
66 unsigned long i;
67 unsigned char hash[4];
68 char hash_hex[4*2 + 1];
69 adler32_finish(self, hash, 4); /* returns void */
70 hash_hex[0] = '\0';
71 for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]);
72 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
89 adler32_state st;
90 int rv, j;
91 unsigned char hash[4], out[8], *in;
92 unsigned long outlen = 8;
93 unsigned int ui32;
94 STRLEN inlen;
95
96 adler32_init(&st);
97 for(j = 0; j < items; j++) {
98 in = (unsigned char *)SvPVbyte(ST(j), inlen);
99 if (inlen > 0) {
100 adler32_update(&st, in, (unsigned long)inlen); /* returns void */
101 }
102 }
103 adler32_finish(&st, hash, 4); /* returns void */
104 if (ix == 1) {
105 rv = _base16_encode(hash, 4, out, &outlen);
106 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
107 RETVAL = newSVpvn((char *)out, outlen);
108 }
109 else if (ix == 2) {
110 LOAD32H(ui32, hash);
111 RETVAL = newSVuv(ui32);
112 }
113 else {
114 RETVAL = newSVpvn((char *) hash, 4);
115 }
73116 }
74117 OUTPUT:
75118 RETVAL
76
00 MODULE = CryptX PACKAGE = Crypt::Checksum::CRC32
1
2 PROTOTYPES: DISABLE
13
24 Crypt::Checksum::CRC32
35 new(Class)
5052
5153 SV *
5254 digest(Crypt::Checksum::CRC32 self)
55 ALIAS:
56 hexdigest = 1
57 intdigest = 2
5358 CODE:
5459 {
55 unsigned char hash[4];
60 int rv;
61 unsigned char hash[4], out[8];
62 unsigned long outlen = 8;
63 unsigned int ui32;
64
5665 crc32_finish(self, hash, 4); /* returns void */
57 RETVAL = newSVpvn((char *) hash, 4);
66 if (ix == 1) {
67 rv = _base16_encode(hash, 4, out, &outlen);
68 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
69 RETVAL = newSVpvn((char *)out, outlen);
70 }
71 else if (ix == 2) {
72 LOAD32H(ui32, hash);
73 RETVAL = newSVuv(ui32);
74 }
75 else {
76 RETVAL = newSVpvn((char *) hash, 4);
77 }
5878 }
5979 OUTPUT:
6080 RETVAL
6181
6282 SV *
63 hexdigest(Crypt::Checksum::CRC32 self)
83 crc32_data(...)
84 ALIAS:
85 crc32_data_hex = 1
86 crc32_data_int = 2
6487 CODE:
6588 {
66 unsigned long i;
67 unsigned char hash[4];
68 char hash_hex[4*2 + 1];
69 crc32_finish(self, hash, 4); /* returns void */
70 hash_hex[0] = '\0';
71 for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]);
72 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
89 crc32_state st;
90 int rv, j;
91 unsigned char hash[4], out[8], *in;
92 unsigned long outlen = 8;
93 unsigned int ui32;
94 STRLEN inlen;
95
96 crc32_init(&st);
97 for(j = 0; j < items; j++) {
98 in = (unsigned char *)SvPVbyte(ST(j), inlen);
99 if (inlen > 0) {
100 crc32_update(&st, in, (unsigned long)inlen); /* returns void */
101 }
102 }
103 crc32_finish(&st, hash, 4); /* returns void */
104 if (ix == 1) {
105 rv = _base16_encode(hash, 4, out, &outlen);
106 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
107 RETVAL = newSVpvn((char *)out, outlen);
108 }
109 else if (ix == 2) {
110 LOAD32H(ui32, hash);
111 RETVAL = newSVuv(ui32);
112 }
113 else {
114 RETVAL = newSVpvn((char *) hash, 4);
115 }
73116 }
74117 OUTPUT:
75118 RETVAL
76
00 MODULE = CryptX PACKAGE = Crypt::Cipher
11
2 PROTOTYPES: DISABLE
3
24 Crypt::Cipher
3 _new(cipher_name, key, rounds=0)
4 char * cipher_name
5 SV * key
6 int rounds
5 new(char * class, ...)
76 CODE:
87 {
98 STRLEN key_len;
10 unsigned char *key_data=NULL;
11 int rv;
12 int id;
9 unsigned char *key_data = NULL;
10 SV *key;
11 char *cipher_name;
12 int rv, id, rounds = 0, idx;
13
14 /* we need to handle:
15 Crypt::Cipher->new('AES');
16 Crypt::Cipher::AES->new();
17 */
18 idx = strcmp("Crypt::Cipher", class) == 0 ? 1 : 0;
19 if (idx + 1 > items) croak("FATAL: missing argument");
20 cipher_name = SvPVX(ST(idx));
21 key = ST(idx + 1);
22 if (idx + 3 <= items) rounds = (int)SvIV(ST(idx + 2));
1323
1424 if (!SvPOK (key)) croak("FATAL: key must be string scalar");
1525 key_data = (unsigned char *)SvPVbyte(key, key_len);
1626
17 id = find_cipher(cipher_name);
27 id = _find_cipher(cipher_name);
1828 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1929
2030 Newz(0, RETVAL, 1, struct cipher_struct);
2131 if (!RETVAL) croak("FATAL: Newz failed");
2232
23 RETVAL->id = id;
2433 RETVAL->desc = &cipher_descriptor[id];
2534 rv = RETVAL->desc->setup(key_data, (int)key_len, rounds, &RETVAL->skey);
2635 if (rv != CRYPT_OK) {
3241 RETVAL
3342
3443 void
35 DESTROY(self)
36 Crypt::Cipher self
44 DESTROY(Crypt::Cipher self)
3745 CODE:
3846 Safefree(self);
3947
40 int
41 _max_keysize(self, ...)
42 Crypt::Cipher self
43 CODE:
44 RETVAL = self->desc->max_key_length;
45 OUTPUT:
46 RETVAL
47
48 int
49 _min_keysize(self, ...)
50 Crypt::Cipher self
51 CODE:
52 RETVAL = self->desc->min_key_length;
53 OUTPUT:
54 RETVAL
55
56 int
57 _blocksize(self, ...)
58 Crypt::Cipher self
59 CODE:
60 RETVAL = self->desc->block_length;
61 OUTPUT:
62 RETVAL
63
64 int
65 _default_rounds(self, ...)
66 Crypt::Cipher self
67 CODE:
68 RETVAL = self->desc->default_rounds;
69 OUTPUT:
70 RETVAL
71
7248 SV *
73 encrypt(self, data)
74 Crypt::Cipher self
75 SV * data
49 encrypt(Crypt::Cipher self, SV * data)
7650 CODE:
7751 {
7852 int rv;
8357 RETVAL = newSVpvn("", 0);
8458 }
8559 else if (len == (STRLEN)self->desc->block_length) {
86 RETVAL = NEWSV(0, len);
60 RETVAL = NEWSV(0, len); /* avoid zero! */
8761 SvPOK_only(RETVAL);
8862 SvCUR_set(RETVAL, len);
8963 rv = self->desc->ecb_encrypt((unsigned char *)plaintext, (unsigned char *)SvPVX(RETVAL), &self->skey);
10074 RETVAL
10175
10276 SV *
103 decrypt(self, data)
104 Crypt::Cipher self
105 SV * data
77 decrypt(Crypt::Cipher self, SV * data)
10678 CODE:
10779 {
10880 int rv;
11385 RETVAL = newSVpvn("", 0);
11486 }
11587 else if (len == (STRLEN)self->desc->block_length) {
116 RETVAL = NEWSV(0, len);
88 RETVAL = NEWSV(0, len); /* avoid zero! */
11789 SvPOK_only(RETVAL);
11890 SvCUR_set(RETVAL, len);
11991 rv = self->desc->ecb_decrypt((unsigned char *)ciphertext, (unsigned char *)SvPVX(RETVAL), &self->skey);
130102 RETVAL
131103
132104 int
133 _block_length_by_name(cipher_name)
134 char * cipher_name
105 blocksize(SV * param, char * extra = NULL)
135106 CODE:
136107 {
137 int rv, id;
138
139 id = find_cipher(cipher_name);
140 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
141
142 rv = cipher_descriptor[id].block_length;
143 if (!rv) XSRETURN_UNDEF;
144 RETVAL = rv;
108 if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
109 IV tmp = SvIV((SV*)SvRV(param));
110 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
111 RETVAL = obj->desc->block_length;
112 }
113 else {
114 char *name = SvPOK(param) && strcmp(SvPVX(param), "Crypt::Cipher") ? SvPVX(param) : extra;
115 int rv, id = _find_cipher(name);
116 if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
117 rv = cipher_descriptor[id].block_length;
118 if (!rv) croak("FATAL: invalid block_length for '%s'", name);
119 RETVAL = rv;
120 }
145121 }
146122 OUTPUT:
147123 RETVAL
148124
149125 int
150 _min_key_length_by_name(cipher_name)
151 char * cipher_name
126 max_keysize(SV * param, char * extra = NULL)
152127 CODE:
153128 {
154 int rv, id;
155
156 id = find_cipher(cipher_name);
157 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
158
159 rv = cipher_descriptor[id].min_key_length;
160 if (!rv) XSRETURN_UNDEF;
161 RETVAL = rv;
129 if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
130 IV tmp = SvIV((SV*)SvRV(param));
131 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
132 RETVAL = obj->desc->max_key_length;
133 }
134 else {
135 char *name = SvPOK(param) && strcmp(SvPVX(param), "Crypt::Cipher") ? SvPVX(param) : extra;
136 int rv, id = _find_cipher(name);
137 if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
138 rv = cipher_descriptor[id].max_key_length;
139 if (!rv) croak("FATAL: invalid max_key_length for '%s'", name);
140 RETVAL = rv;
141 }
162142 }
163143 OUTPUT:
164144 RETVAL
165145
166146 int
167 _max_key_length_by_name(cipher_name)
168 char * cipher_name
147 min_keysize(SV * param, char * extra = NULL)
169148 CODE:
170149 {
171 int rv, id;
172
173 id = find_cipher(cipher_name);
174 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
175
176 rv = cipher_descriptor[id].max_key_length;
177 if (!rv) XSRETURN_UNDEF;
178 RETVAL = rv;
150 if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
151 IV tmp = SvIV((SV*)SvRV(param));
152 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
153 RETVAL = obj->desc->min_key_length;
154 }
155 else {
156 char *name = SvPOK(param) && strcmp(SvPVX(param), "Crypt::Cipher") ? SvPVX(param) : extra;
157 int rv, id = _find_cipher(name);
158 if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
159 rv = cipher_descriptor[id].min_key_length;
160 if (!rv) croak("FATAL: invalid min_key_length for '%s'", name);
161 RETVAL = rv;
162 }
179163 }
180164 OUTPUT:
181165 RETVAL
182166
183167 int
184 _default_rounds_by_name(cipher_name)
185 char * cipher_name
168 default_rounds(SV * param, char * extra = NULL)
186169 CODE:
187170 {
188 int rv, id;
189
190 id = find_cipher(cipher_name);
191 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
192
193 rv = cipher_descriptor[id].default_rounds;
194 if (!rv) XSRETURN_UNDEF;
195 RETVAL = rv;
171 if (sv_isobject(param) && sv_derived_from(param, "Crypt::Cipher")) {
172 IV tmp = SvIV((SV*)SvRV(param));
173 Crypt__Cipher obj = INT2PTR(Crypt__Cipher, tmp);
174 RETVAL = obj->desc->default_rounds;
175 }
176 else {
177 char *name = SvPOK(param) && strcmp(SvPVX(param), "Crypt::Cipher") ? SvPVX(param) : extra;
178 int rv, id = _find_cipher(name);
179 if (id == -1) croak("FATAL: find_cipher failed for '%s'", name);
180 rv = cipher_descriptor[id].default_rounds;
181 if (!rv) XSRETURN_UNDEF;
182 RETVAL = rv;
183 }
196184 }
197185 OUTPUT:
198186 RETVAL
00 MODULE = CryptX PACKAGE = Crypt::Digest
11
2 PROTOTYPES: DISABLE
3
24 Crypt::Digest
3 _new(digest_name)
4 char * digest_name
5 new(char * cname, char * pname = NULL)
56 CODE:
67 {
78 int rv;
89 int id;
10 char *digest_name = strcmp(cname, "Crypt::Digest") == 0 ? pname : cname;
911
10 id = find_hash(digest_name);
12 id = _find_hash(digest_name);
1113 if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name);
1214
1315 Newz(0, RETVAL, 1, struct digest_struct);
1416 if (!RETVAL) croak("FATAL: Newz failed");
1517
16 RETVAL->id = id;
1718 RETVAL->desc = &hash_descriptor[id];
1819 rv = RETVAL->desc->init(&RETVAL->state);
1920 if (rv != CRYPT_OK) {
2526 RETVAL
2627
2728 void
28 DESTROY(self)
29 Crypt::Digest self
29 DESTROY(Crypt::Digest self)
3030 CODE:
3131 Safefree(self);
3232
3333 void
34 reset(self)
35 Crypt::Digest self
34 reset(Crypt::Digest self)
3635 PPCODE:
3736 {
3837 int rv;
4241 }
4342
4443 Crypt::Digest
45 clone(self)
46 Crypt::Digest self
44 clone(Crypt::Digest self)
4745 CODE:
4846 Newz(0, RETVAL, 1, struct digest_struct);
4947 if (!RETVAL) croak("FATAL: Newz failed");
5957 int rv, i;
6058 unsigned char *in;
6159
62 for(i=1; i<items; i++) {
60 for(i = 1; i < items; i++) {
6361 in = (unsigned char *)SvPVbyte(ST(i), inlen);
64 if (inlen>0) {
62 if (inlen > 0) {
6563 rv = self->desc->process(&self->state, in, (unsigned long)inlen);
6664 if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv));
6765 }
7068 }
7169
7270 SV *
73 digest(self)
74 Crypt::Digest self
71 digest(Crypt::Digest self)
72 ALIAS:
73 hexdigest = 1
74 b64digest = 2
75 b64udigest = 3
7576 CODE:
7677 {
78 int rv;
79 unsigned long outlen;
7780 unsigned char hash[MAXBLOCKSIZE];
78 int rv;
81 char out[MAXBLOCKSIZE*2];
7982
8083 rv = self->desc->done(&self->state, hash);
8184 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
82 RETVAL = newSVpvn((char *) hash, self->desc->hashsize);
85
86 outlen = sizeof(out);
87 if (ix == 3) {
88 rv = base64url_encode(hash, self->desc->hashsize, (unsigned char *)out, &outlen);
89 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
90 RETVAL = newSVpvn(out, outlen);
91 }
92 else if (ix == 2) {
93 rv = base64_encode(hash, self->desc->hashsize, (unsigned char *)out, &outlen);
94 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
95 RETVAL = newSVpvn(out, outlen);
96 }
97 else if (ix == 1) {
98 rv = _base16_encode(hash, self->desc->hashsize, (unsigned char *)out, &outlen);
99 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
100 RETVAL = newSVpvn(out, outlen);
101 }
102 else {
103 RETVAL = newSVpvn((char *) hash, self->desc->hashsize);
104 }
83105 }
84106 OUTPUT:
85107 RETVAL
86108
87109 SV *
88 hexdigest(self)
89 Crypt::Digest self
110 digest_data(char * digest_name, ...)
111 ALIAS:
112 digest_data_hex = 1
113 digest_data_b64 = 2
114 digest_data_b64u = 3
90115 CODE:
91116 {
92 int rv;
93 unsigned long i;
94 unsigned char hash[MAXBLOCKSIZE];
95 char hash_hex[MAXBLOCKSIZE*2 + 1];
117 STRLEN inlen;
118 int rv, id, i;
119 unsigned char *in, hash[MAXBLOCKSIZE];
120 unsigned long len = sizeof(hash), outlen;
121 char out[MAXBLOCKSIZE*2];
122 hash_state md;
96123
97 rv = self->desc->done(&self->state, hash);
124 id = _find_hash(digest_name);
125 if (id == -1) croak("FATAL: find_digest failed for '%s'", digest_name);
126
127 /* digest_data("SHA1", $data1, $data2, $data3); */
128 len = hash_descriptor[id].hashsize;
129 rv = hash_descriptor[id].init(&md);
130 if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv));
131 for (i = 1; i < items; i++) {
132 in = (unsigned char *)SvPVbyte(ST(i), inlen);
133 if (inlen > 0) {
134 rv = hash_descriptor[id].process(&md, in, (unsigned long)inlen);
135 if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv));
136 }
137 }
138 rv = hash_descriptor[id].done(&md, hash);
98139 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
99140
100 hash_hex[0] = '\0';
101 for(i=0; i<self->desc->hashsize; i++)
102 sprintf(&hash_hex[2*i], "%02x", hash[i]);
103 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
104 }
105 OUTPUT:
106 RETVAL
107
108 SV *
109 b64digest(self)
110 Crypt::Digest self
111 CODE:
112 {
113 int rv;
114 unsigned long outlen;
115 unsigned char hash[MAXBLOCKSIZE];
116 char hash_base64[MAXBLOCKSIZE*2 + 1];
117
118 rv = self->desc->done(&self->state, hash);
119 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
120
121 outlen = sizeof(hash_base64);
122 rv = base64_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen);
123 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
124 RETVAL = newSVpvn(hash_base64, outlen);
125 }
126 OUTPUT:
127 RETVAL
128
129 SV *
130 b64udigest(self)
131 Crypt::Digest self
132 CODE:
133 {
134 int rv;
135 unsigned long outlen;
136 unsigned char hash[MAXBLOCKSIZE];
137 char hash_base64[MAXBLOCKSIZE*2 + 1];
138
139 rv = self->desc->done(&self->state, hash);
140 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
141
142 outlen = sizeof(hash_base64);
143 rv = base64url_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen);
144 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
145 RETVAL = newSVpvn(hash_base64, outlen);
141 outlen = sizeof(out);
142 if (ix == 3) {
143 rv = base64url_encode(hash, len, (unsigned char *)out, &outlen);
144 if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv));
145 RETVAL = newSVpvn((char *) out, outlen);
146 }
147 else if (ix == 2) {
148 rv = base64_encode(hash, len, (unsigned char *)out, &outlen);
149 if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv));
150 RETVAL = newSVpvn((char *) out, outlen);
151 }
152 else if (ix == 1) {
153 rv = _base16_encode(hash, len, (unsigned char *)out, &outlen);
154 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
155 RETVAL = newSVpvn((char *) out, outlen);
156 }
157 else {
158 RETVAL = newSVpvn((char *) hash, len);
159 }
146160 }
147161 OUTPUT:
148162 RETVAL
149163
150164 int
151 _hashsize(self)
152 Crypt::Digest self
153 CODE:
154 RETVAL = self->desc->hashsize;
155 OUTPUT:
156 RETVAL
157
158 int
159 _hashsize_by_name(digest_name)
160 char * digest_name
165 hashsize(SV * param, char * extra = NULL)
161166 CODE:
162167 {
163 int rv, id;
164
165 id = find_hash(digest_name);
166 if (id == -1) croak("FATAL: find_digest failed for '%s'", digest_name);
167
168 rv = hash_descriptor[id].hashsize;
169 if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);;
170 RETVAL = rv;
168 if (sv_isobject(param) && sv_derived_from(param, "Crypt::Digest")) {
169 IV tmp = SvIV((SV*)SvRV(param));
170 Crypt__Digest obj = INT2PTR(Crypt__Digest, tmp);
171 RETVAL = obj->desc->hashsize;
172 }
173 else {
174 char *digest_name;
175 int rv, id;
176 digest_name = SvPOK(param) && strcmp(SvPVX(param), "Crypt::Digest") ? SvPVX(param) : extra;
177 id = _find_hash(digest_name);
178 if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name);
179 rv = hash_descriptor[id].hashsize;
180 if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);;
181 RETVAL = rv;
182 }
171183 }
172184 OUTPUT:
173185 RETVAL
00 MODULE = CryptX PACKAGE = Crypt::Digest::SHAKE
11
2 PROTOTYPES: DISABLE
3
24 Crypt::Digest::SHAKE
3 _new(int num)
5 new(Class, int num)
46 CODE:
57 {
68 int rv;
6769 int rv;
6870 unsigned char *out_data;
6971
70 RETVAL = NEWSV(0, out_len);
71 SvPOK_only(RETVAL);
72 SvCUR_set(RETVAL, out_len);
73 out_data = (unsigned char *)SvPVX(RETVAL);
74 rv = sha3_shake_done(&self->state, out_data, (unsigned long)out_len);
75 if (rv != CRYPT_OK) {
76 SvREFCNT_dec(RETVAL);
77 croak("FATAL: sha3_shake_done failed: %s", error_to_string(rv));
72 if (out_len == 0) {
73 RETVAL = newSVpvn("", 0);
74 }
75 else {
76 RETVAL = NEWSV(0, out_len); /* avoid zero! */
77 SvPOK_only(RETVAL);
78 SvCUR_set(RETVAL, out_len);
79 out_data = (unsigned char *)SvPVX(RETVAL);
80 rv = sha3_shake_done(&self->state, out_data, (unsigned long)out_len);
81 if (rv != CRYPT_OK) {
82 SvREFCNT_dec(RETVAL);
83 croak("FATAL: sha3_shake_done failed: %s", error_to_string(rv));
84 }
7885 }
7986 }
8087 OUTPUT:
00 MODULE = CryptX PACKAGE = Crypt::KeyDerivation
11
2 PROTOTYPES: DISABLE
3
24 SV *
3 _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
5 pbkdf1(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
46 CODE:
57 {
6 /*
7 int pkcs_5_alg1(const unsigned char *password, unsigned long password_len,
8 const unsigned char *salt,
9 int iteration_count, int hash_idx,
10 unsigned char *out, unsigned long *outlen)
11 */
128 int rv, id;
139 unsigned char *output;
1410 unsigned char *password_ptr=NULL;
1612 unsigned char *salt_ptr=NULL;
1713 STRLEN salt_len=0;
1814
19 id = find_hash(hash_name);
20 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
15 if (output_len == 0) {
16 RETVAL = newSVpvn("", 0);
17 }
18 else {
19 id = _find_hash(hash_name);
20 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
2121
22 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
23 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
24 if (salt_len < 8) croak("FATAL: salt_len has to be 8");
22 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
23 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
24 if (salt_len < 8) croak("FATAL: salt_len has to be 8");
2525
26 RETVAL = NEWSV(0, output_len);
27 SvPOK_only(RETVAL);
28 SvCUR_set(RETVAL, output_len);
29 output = (unsigned char *)SvPVX(RETVAL);
26 RETVAL = NEWSV(0, output_len); /* avoid zero! */
27 SvPOK_only(RETVAL);
28 SvCUR_set(RETVAL, output_len);
29 output = (unsigned char *)SvPVX(RETVAL);
3030
31 rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
32 if (rv != CRYPT_OK) {
33 SvREFCNT_dec(RETVAL);
34 croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
31 rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
32 if (rv != CRYPT_OK) {
33 SvREFCNT_dec(RETVAL);
34 croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
35 }
36 SvCUR_set(RETVAL, output_len);
3537 }
36 SvCUR_set(RETVAL, output_len);
3738 }
3839 OUTPUT:
3940 RETVAL
4041
4142 SV *
42 _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
43 pbkdf2(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
4344 CODE:
4445 {
45 /*
46 int pkcs_5_alg2(const unsigned char *password, unsigned long password_len,
47 const unsigned char *salt, unsigned long salt_len,
48 int iteration_count, int hash_idx,
49 unsigned char *out, unsigned long *outlen)
50 */
5146 int rv, id;
5247 unsigned char *output;
5348 unsigned char *password_ptr=NULL;
5550 unsigned char *salt_ptr=NULL;
5651 STRLEN salt_len=0;
5752
58 id = find_hash(hash_name);
59 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
53 if (output_len == 0) {
54 RETVAL = newSVpvn("", 0);
55 }
56 else {
57 id = _find_hash(hash_name);
58 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
6059