Package list libcryptx-perl / 4ac9434
New upstream version 0.054+006 Damyan Ivanov 3 years ago
191 changed file(s) with 19860 addition(s) and 1676 deletion(s). Raw diff Collapse all Expand all
00 Changes for CryptX
11
2 TODO:
3 - XS croaks should report the "real caller" (Crypt::Mac::*, Crypt::Mode::*, ...)
4 - CCM interface new-add-add-done
2 0.054_* 2017-10-XX
3 - new Crypt::Cipher::IDEA
4 - new Crypt::Cipher::Serpent
5 - new Crypt::Stream::Salsa20
6 - new Crypt::Stream::Sosemanuk
7 - added CCM OO interface: new-add-add-done
58
69 0.054 2017-10-12
710 - fix Crypt::PK::DSA verify
3232
3333 typedef struct ccm_struct { /* used by Crypt::AuthEnc::CCM */
3434 ccm_state state;
35 int direction;
36 int tag_len;
37 unsigned long pt_len;
3538 int id;
3639 } *Crypt__AuthEnc__CCM;
3740
5962 chacha_state state;
6063 int id;
6164 } *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;
6275
6376 typedef struct rc4_struct { /* used by Crypt::Stream::RC4 */
6477 rc4_state state;
323336 PROTOTYPES: DISABLE
324337
325338 BOOT:
326 if(register_cipher(&blowfish_desc)==-1) { croak("FATAL: cannot register_cipher blowfish"); }
327 if(register_cipher(&rc5_desc)==-1) { croak("FATAL: cannot register_cipher rc5"); }
328 if(register_cipher(&rc6_desc)==-1) { croak("FATAL: cannot register_cipher rc6"); }
329 if(register_cipher(&rc2_desc)==-1) { croak("FATAL: cannot register_cipher rc2"); }
330 if(register_cipher(&saferp_desc)==-1) { croak("FATAL: cannot register_cipher saferp"); }
331 if(register_cipher(&safer_k64_desc)==-1) { croak("FATAL: cannot register_cipher safer_k64"); }
332 if(register_cipher(&safer_k128_desc)==-1) { croak("FATAL: cannot register_cipher safer_k128"); }
333 if(register_cipher(&safer_sk64_desc)==-1) { croak("FATAL: cannot register_cipher safer_sk64"); }
334 if(register_cipher(&safer_sk128_desc)==-1) { croak("FATAL: cannot register_cipher safer_sk128"); }
335 if(register_cipher(&aes_desc)==-1) { croak("FATAL: cannot register_cipher aes"); }
336 if(register_cipher(&xtea_desc)==-1) { croak("FATAL: cannot register_cipher xtea"); }
337 if(register_cipher(&twofish_desc)==-1) { croak("FATAL: cannot register_cipher twofish"); }
338 if(register_cipher(&des_desc)==-1) { croak("FATAL: cannot register_cipher des"); }
339 if(register_cipher(&des3_desc)==-1) { croak("FATAL: cannot register_cipher des3"); }
340 if(register_cipher(&cast5_desc)==-1) { croak("FATAL: cannot register_cipher cast5"); }
341 if(register_cipher(&noekeon_desc)==-1) { croak("FATAL: cannot register_cipher noekeon"); }
342 if(register_cipher(&skipjack_desc)==-1) { croak("FATAL: cannot register_cipher skipjack"); }
343 if(register_cipher(&khazad_desc)==-1) { croak("FATAL: cannot register_cipher khazad"); }
344 if(register_cipher(&anubis_desc)==-1) { croak("FATAL: cannot register_cipher anubis"); }
345 if(register_cipher(&kseed_desc)==-1) { croak("FATAL: cannot register_cipher kseed"); }
346 if(register_cipher(&kasumi_desc)==-1) { croak("FATAL: cannot register_cipher kasumi"); }
347 if(register_cipher(&multi2_desc)==-1) { croak("FATAL: cannot register_cipher multi2"); }
348 if(register_cipher(&camellia_desc)==-1) { croak("FATAL: cannot register_cipher camellia"); }
349 /* --- */
350 if(register_hash(&chc_desc)==-1) { croak("FATAL: cannot register_hash chc_hash"); }
351 if(register_hash(&md2_desc)==-1) { croak("FATAL: cannot register_hash md2"); }
352 if(register_hash(&md4_desc)==-1) { croak("FATAL: cannot register_hash md4"); }
353 if(register_hash(&md5_desc)==-1) { croak("FATAL: cannot register_hash md5"); }
354 if(register_hash(&rmd128_desc)==-1) { croak("FATAL: cannot register_hash rmd128"); }
355 if(register_hash(&rmd160_desc)==-1) { croak("FATAL: cannot register_hash rmd160"); }
356 if(register_hash(&rmd256_desc)==-1) { croak("FATAL: cannot register_hash rmd256"); }
357 if(register_hash(&rmd320_desc)==-1) { croak("FATAL: cannot register_hash rmd320"); }
358 if(register_hash(&sha1_desc)==-1) { croak("FATAL: cannot register_hash sha1"); }
359 if(register_hash(&sha224_desc)==-1) { croak("FATAL: cannot register_hash sha224"); }
360 if(register_hash(&sha256_desc)==-1) { croak("FATAL: cannot register_hash sha256"); }
361 if(register_hash(&sha384_desc)==-1) { croak("FATAL: cannot register_hash sha384"); }
362 if(register_hash(&sha512_desc)==-1) { croak("FATAL: cannot register_hash sha512"); }
363 if(register_hash(&sha512_224_desc)==-1) { croak("FATAL: cannot register_hash sha512_224"); }
364 if(register_hash(&sha512_256_desc)==-1) { croak("FATAL: cannot register_hash sha512_256"); }
365 if(register_hash(&sha3_224_desc)==-1) { croak("FATAL: cannot register_hash sha3_224"); }
366 if(register_hash(&sha3_256_desc)==-1) { croak("FATAL: cannot register_hash sha3_256"); }
367 if(register_hash(&sha3_384_desc)==-1) { croak("FATAL: cannot register_hash sha3_384"); }
368 if(register_hash(&sha3_512_desc)==-1) { croak("FATAL: cannot register_hash sha3_512"); }
369 if(register_hash(&tiger_desc)==-1) { croak("FATAL: cannot register_hash tiger"); }
370 if(register_hash(&whirlpool_desc)==-1) { croak("FATAL: cannot register_hash whirlpool"); }
371 if(register_hash(&blake2b_160_desc)==-1) { croak("FATAL: cannot register_hash blake2b_160"); }
372 if(register_hash(&blake2b_256_desc)==-1) { croak("FATAL: cannot register_hash blake2b_256"); }
373 if(register_hash(&blake2b_384_desc)==-1) { croak("FATAL: cannot register_hash blake2b_384"); }
374 if(register_hash(&blake2b_512_desc)==-1) { croak("FATAL: cannot register_hash blake2b_512"); }
375 if(register_hash(&blake2s_128_desc)==-1) { croak("FATAL: cannot register_hash blake2s_128"); }
376 if(register_hash(&blake2s_160_desc)==-1) { croak("FATAL: cannot register_hash blake2s_160"); }
377 if(register_hash(&blake2s_224_desc)==-1) { croak("FATAL: cannot register_hash blake2s_224"); }
378 if(register_hash(&blake2s_256_desc)==-1) { croak("FATAL: cannot register_hash blake2s_256"); }
379 /* --- */
380 if(chc_register(find_cipher("aes"))==-1) { croak("FATAL: chc_register failed"); }
381 /* --- */
382 if(register_prng(&fortuna_desc)==-1) { croak("FATAL: cannot register_prng fortuna"); }
383 if(register_prng(&yarrow_desc)==-1) { croak("FATAL: cannot register_prng yarrow"); }
384 if(register_prng(&rc4_desc)==-1) { croak("FATAL: cannot register_prng rc4"); }
385 if(register_prng(&sober128_desc)==-1) { croak("FATAL: cannot register_prng sober128"); }
386 if(register_prng(&chacha20_prng_desc)==-1) { croak("FATAL: cannot register_prng chacha20"); }
387 /* --- */
388 #ifdef TFM_DESC
389 ltc_mp = tfm_desc;
390 #else
391 ltc_mp = ltm_desc;
392 #endif
339 if(register_all_ciphers() != CRYPT_OK) { croak("FATAL: register_all_ciphers failed"); }
340 if(register_all_hashes() != CRYPT_OK) { croak("FATAL: register_all_hashes failed"); }
341 if(register_all_prngs() != CRYPT_OK) { croak("FATAL: register_all_prngs failed"); }
342 if(crypt_mp_init("ltm") != CRYPT_OK) { croak("FATAL: crypt_mp_init failed"); }
393343
394344 SV *
395345 CryptX__encode_base64url(SV * in)
398348 STRLEN in_len;
399349 unsigned long out_len;
400350 unsigned char *out_data, *in_data;
401 int rv;
402351
403352 if (!SvPOK(in)) XSRETURN_UNDEF;
404353 in_data = (unsigned char *) SvPVbyte(in, in_len);
405 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
406 Newz(0, out_data, out_len, unsigned char);
407 if (!out_data) croak("FATAL: Newz failed [%ld]", out_len);
408 rv = base64url_encode(in_data, (unsigned long)in_len, out_data, &out_len);
409 RETVAL = (rv == CRYPT_OK) ? newSVpvn((char *)out_data, out_len) : newSVpvn(NULL, 0);
410 Safefree(out_data);
354 if (in_len == 0) {
355 RETVAL = newSVpvn("", 0);
356 }
357 else {
358 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
359 RETVAL = NEWSV(0, out_len);
360 SvPOK_only(RETVAL);
361 out_data = (unsigned char *)SvPVX(RETVAL);
362 if (base64url_encode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
363 SvREFCNT_dec(RETVAL);
364 XSRETURN_UNDEF;
365 }
366 SvCUR_set(RETVAL, out_len);
367 }
411368 }
412369 OUTPUT:
413370 RETVAL
419376 STRLEN in_len;
420377 unsigned long out_len;
421378 unsigned char *out_data, *in_data;
422 int rv;
423379
424380 if (!SvPOK(in)) XSRETURN_UNDEF;
425381 in_data = (unsigned char *) SvPVbyte(in, in_len);
426 out_len = (unsigned long)in_len;
427 Newz(0, out_data, out_len, unsigned char);
428 if (!out_data) croak("FATAL: Newz failed [%ld]", out_len);
429 rv = base64url_decode(in_data, (unsigned long)in_len, out_data, &out_len);
430 RETVAL = (rv == CRYPT_OK) ? newSVpvn((char *)out_data, out_len) : newSVpvn(NULL, 0);
431 Safefree(out_data);
382 if (in_len == 0) {
383 RETVAL = newSVpvn("", 0);
384 }
385 else {
386 out_len = (unsigned long)in_len;
387 RETVAL = NEWSV(0, out_len);
388 SvPOK_only(RETVAL);
389 out_data = (unsigned char *)SvPVX(RETVAL);
390 if (base64url_decode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
391 SvREFCNT_dec(RETVAL);
392 XSRETURN_UNDEF;
393 }
394 SvCUR_set(RETVAL, out_len);
395 }
432396 }
433397 OUTPUT:
434398 RETVAL
440404 STRLEN in_len;
441405 unsigned long out_len;
442406 unsigned char *out_data, *in_data;
443 int rv;
444407
445408 if (!SvPOK(in)) XSRETURN_UNDEF;
446409 in_data = (unsigned char *) SvPVbyte(in, in_len);
447 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
448 Newz(0, out_data, out_len, unsigned char);
449 if (!out_data) croak("FATAL: Newz failed [%ld]", out_len);
450 rv = base64_encode(in_data, (unsigned long)in_len, out_data, &out_len);
451 RETVAL = (rv == CRYPT_OK) ? newSVpvn((char *)out_data, out_len) : newSVpvn(NULL, 0);
452 Safefree(out_data);
410 if (in_len == 0) {
411 RETVAL = newSVpvn("", 0);
412 }
413 else {
414 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
415 RETVAL = NEWSV(0, out_len);
416 SvPOK_only(RETVAL);
417 out_data = (unsigned char *)SvPVX(RETVAL);
418 if (base64_encode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
419 SvREFCNT_dec(RETVAL);
420 XSRETURN_UNDEF;
421 }
422 SvCUR_set(RETVAL, out_len);
423 }
453424 }
454425 OUTPUT:
455426 RETVAL
461432 STRLEN in_len;
462433 unsigned long out_len;
463434 unsigned char *out_data, *in_data;
464 int rv;
465435
466436 if (!SvPOK(in)) XSRETURN_UNDEF;
437 in_data = (unsigned char *)SvPVbyte(in, in_len);
438 if (in_len == 0) {
439 RETVAL = newSVpvn("", 0);
440 }
441 else {
442 out_len = (unsigned long)in_len;
443 RETVAL = NEWSV(0, out_len);
444 SvPOK_only(RETVAL);
445 out_data = (unsigned char *)SvPVX(RETVAL);
446 if (base64_decode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
447 SvREFCNT_dec(RETVAL);
448 XSRETURN_UNDEF;
449 }
450 SvCUR_set(RETVAL, out_len);
451 }
452 }
453 OUTPUT:
454 RETVAL
455
456 SV *
457 CryptX__encode_b32(SV *in, unsigned idx)
458 CODE:
459 {
460 STRLEN in_len;
461 unsigned long out_len;
462 unsigned char *out_data, *in_data;
463 int id = -1;
464
465 if (!SvPOK(in)) XSRETURN_UNDEF;
466 if (idx == 0) id = BASE32_RFC4648;
467 if (idx == 1) id = BASE32_BASE32HEX;
468 if (idx == 2) id = BASE32_ZBASE32;
469 if (idx == 3) id = BASE32_CROCKFORD;
470 if (id == -1) XSRETURN_UNDEF;
467471 in_data = (unsigned char *) SvPVbyte(in, in_len);
468 out_len = (unsigned long)in_len;
469 Newz(0, out_data, out_len, unsigned char);
470 if (!out_data) croak("FATAL: Newz failed [%ld]", out_len);
471 rv = base64_decode(in_data, (unsigned long)in_len, out_data, &out_len);
472 RETVAL = (rv == CRYPT_OK) ? newSVpvn((char *)out_data, out_len) : newSVpvn(NULL, 0);
473 Safefree(out_data);
472 if (in_len == 0) {
473 RETVAL = newSVpvn("", 0);
474 }
475 else {
476 out_len = (unsigned long)((8 * in_len + 4) / 5);
477 RETVAL = NEWSV(0, out_len);
478 SvPOK_only(RETVAL);
479 out_data = (unsigned char *)SvPVX(RETVAL);
480 if (base32_encode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
481 SvREFCNT_dec(RETVAL);
482 XSRETURN_UNDEF;
483 }
484 SvCUR_set(RETVAL, out_len);
485 }
486 }
487 OUTPUT:
488 RETVAL
489
490 SV *
491 CryptX__decode_b32(SV *in, unsigned idx)
492 CODE:
493 {
494 STRLEN in_len;
495 unsigned long out_len;
496 unsigned char *out_data, *in_data;
497 int id = -1;
498
499 if (!SvPOK(in)) XSRETURN_UNDEF;
500 if (idx == 0) id = BASE32_RFC4648;
501 if (idx == 1) id = BASE32_BASE32HEX;
502 if (idx == 2) id = BASE32_ZBASE32;
503 if (idx == 3) id = BASE32_CROCKFORD;
504 if (id == -1) XSRETURN_UNDEF;
505 in_data = (unsigned char *)SvPVbyte(in, in_len);
506 if (in_len == 0) {
507 RETVAL = newSVpvn("", 0);
508 }
509 else {
510 out_len = (unsigned long)in_len;
511 RETVAL = NEWSV(0, out_len);
512 SvPOK_only(RETVAL);
513 out_data = (unsigned char *)SvPVX(RETVAL);
514 if (base32_decode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
515 SvREFCNT_dec(RETVAL);
516 XSRETURN_UNDEF;
517 }
518 SvCUR_set(RETVAL, out_len);
519 }
474520 }
475521 OUTPUT:
476522 RETVAL
483529 unsigned char *out_data, *in_data;
484530
485531 if (!SvPOK(in)) XSRETURN_UNDEF;
486 in_data = (unsigned char *) SvPVbyte(in, len);
532 in_data = (unsigned char *)SvPVbyte(in, len);
487533 if (len == 0) XSRETURN_UNDEF;
488534
489535 RETVAL = NEWSV(0, len);
490536 SvPOK_only(RETVAL);
491537 SvCUR_set(RETVAL, len);
492 out_data = (unsigned char *)SvPV_nolen(RETVAL);
538 out_data = (unsigned char *)SvPVX(RETVAL);
493539 Copy(in_data, out_data, len, unsigned char);
494540 while (i < len) {
495541 out_data[i]++;
496542 if (0 != out_data[i]) break;
497543 i++;
498544 }
499 if (i == len) croak("FATAL: increment_octets_le overflow");
545 if (i == len) {
546 SvREFCNT_dec(RETVAL);
547 croak("FATAL: increment_octets_le overflow");
548 }
500549 }
501550 OUTPUT:
502551 RETVAL
509558 unsigned char *out_data, *in_data;
510559
511560 if (!SvPOK(in)) XSRETURN_UNDEF;
512 in_data = (unsigned char *) SvPVbyte(in, len);
561 in_data = (unsigned char *)SvPVbyte(in, len);
513562 if (len == 0) XSRETURN_UNDEF;
514563
515564 RETVAL = NEWSV(0, len);
516565 SvPOK_only(RETVAL);
517566 SvCUR_set(RETVAL, len);
518 out_data = (unsigned char *)SvPV_nolen(RETVAL);
567 out_data = (unsigned char *)SvPVX(RETVAL);
519568 Copy(in_data, out_data, len, unsigned char);
520569 while (i < len) {
521570 out_data[len - 1 - i]++;
522571 if (0 != out_data[len - 1 - i]) break;
523572 i++;
524573 }
525 if (i == len) croak("FATAL: increment_octets_le overflow");
574 if (i == len) {
575 SvREFCNT_dec(RETVAL);
576 croak("FATAL: increment_octets_be overflow");
577 }
526578 }
527579 OUTPUT:
528580 RETVAL
536588 mp_int mpi;
537589
538590 if (in == NULL || strlen(in) == 0) XSRETURN_UNDEF;
539
540591 if (mp_init(&mpi) != CRYPT_OK) XSRETURN_UNDEF;
541592
542593 if (mp_read_radix(&mpi, in, radix) == CRYPT_OK) {
544595 RETVAL = NEWSV(0, len);
545596 SvPOK_only(RETVAL);
546597 SvCUR_set(RETVAL, len);
547 out_data = (unsigned char *)SvPV_nolen(RETVAL);
598 out_data = (unsigned char *)SvPVX(RETVAL);
548599 mp_to_unsigned_bin(&mpi, out_data);
549600 mp_clear(&mpi);
550601 }
579630 }
580631 mp_clear(&tmp);
581632
582 RETVAL = NEWSV(0, digits + 1);
583 SvPOK_only(RETVAL);
584 out_data = SvPV_nolen(RETVAL);
585 mp_toradix(&mpi, out_data, radix);
586 SvCUR_set(RETVAL, digits);
633 if (digits == 0) {
634 RETVAL = newSVpvn("", 0);
635 mp_clear(&mpi);
636 }
637 else {
638 RETVAL = NEWSV(0, digits + 2); /* +2 for sign and NUL byte */
639 SvPOK_only(RETVAL);
640 out_data = SvPVX(RETVAL);
641 mp_toradix(&mpi, out_data, radix);
642 SvCUR_set(RETVAL, strlen(out_data));
643 mp_clear(&mpi);
644 }
645 }
646 else {
587647 mp_clear(&mpi);
588 }
589 else {
590648 XSRETURN_UNDEF;
591 }
592 }
593 OUTPUT:
594 RETVAL
595
596 SV *
597 CryptX__encode_b32(SV *bytes, unsigned idx)
598 CODE:
599 {
600 STRLEN inlen, outlen, i, leven;
601 unsigned char *out, *in, *codes;
602 char *alphabet[] = {
603 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", /* rfc4648 */
604 "0123456789ABCDEFGHIJKLMNOPQRSTUV", /* base32hex */
605 "ybndrfg8ejkmcpqxot1uwisza345h769", /* zbase32 */
606 "0123456789ABCDEFGHJKMNPQRSTVWXYZ" /* crockford */
607 };
608
609 if (!SvOK(bytes)) {
610 /* for undefined input return "" (empty string) */
611 RETVAL = newSVpv("", 0);
612 }
613 else {
614 if (!SvPOK(bytes) || idx > 3) XSRETURN_UNDEF; /* error */
615 in = (unsigned char *) SvPVbyte(bytes, inlen);
616 if (in == NULL) XSRETURN_UNDEF; /* error */
617 if (inlen == 0) {
618 RETVAL = newSVpv("", 0);
619 }
620 else {
621 codes = (unsigned char*)alphabet[idx];
622 outlen = (8 * inlen + 4) / 5;
623 RETVAL = NEWSV(0, outlen);
624 SvPOK_only(RETVAL);
625 SvCUR_set(RETVAL, outlen);
626 out = (unsigned char *)SvPV_nolen(RETVAL);
627
628 leven = 5 * (inlen / 5);
629 for (i = 0; i < leven; i += 5) {
630 *out++ = codes[(in[0] >> 3) & 0x1F];
631 *out++ = codes[(((in[0] & 0x7) << 2) + (in[1] >> 6)) & 0x1F];
632 *out++ = codes[(in[1] >> 1) & 0x1F];
633 *out++ = codes[(((in[1] & 0x1) << 4) + (in[2] >> 4)) & 0x1F];
634 *out++ = codes[(((in[2] & 0xF) << 1) + (in[3] >> 7)) & 0x1F];
635 *out++ = codes[(in[3] >> 2) & 0x1F];
636 *out++ = codes[(((in[3] & 0x3) << 3) + (in[4] >> 5)) & 0x1F];
637 *out++ = codes[in[4] & 0x1F];
638 in += 5;
639 }
640 if (i < inlen) {
641 unsigned a = in[0];
642 unsigned b = (i+1 < inlen) ? in[1] : 0;
643 unsigned c = (i+2 < inlen) ? in[2] : 0;
644 unsigned d = (i+3 < inlen) ? in[3] : 0;
645 *out++ = codes[(a >> 3) & 0x1F];
646 *out++ = codes[(((a & 0x7) << 2) + (b >> 6)) & 0x1F];
647 if (i+1 < inlen) {
648 *out++ = codes[(b >> 1) & 0x1F];
649 *out++ = codes[(((b & 0x1) << 4) + (c >> 4)) & 0x1F];
650 }
651 if (i+2 < inlen) {
652 *out++ = codes[(((c & 0xF) << 1) + (d >> 7)) & 0x1F];
653 *out++ = codes[(d >> 2) & 0x1F];
654 }
655 if (i+3 < inlen) {
656 *out++ = codes[((d & 0x3) << 3) & 0x1F];
657 }
658 }
659 }
660 }
661 }
662 OUTPUT:
663 RETVAL
664
665 SV *
666 CryptX__decode_b32(SV *base32, unsigned idx)
667 CODE:
668 {
669 STRLEN x, inlen, outlen;
670 int y = 0;
671 ulong64 t = 0;
672 unsigned char c, *in, *out, *map;
673 unsigned char tables[4][43] = {
674 { /* rfc4648 ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 */
675 99/*0*/,99/*1*/,26/*2*/,27/*3*/,28/*4*/,29/*5*/,30/*6*/,31/*7*/,99/*8*/,99/*9*/,
676 99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
677 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/, 7/*H*/, 8/*I*/, 9/*J*/,10/*K*/,11/*L*/,12/*M*/,
678 13/*N*/,14/*O*/,15/*P*/,16/*Q*/,17/*R*/,18/*S*/,19/*T*/,20/*U*/,21/*V*/,22/*W*/,23/*X*/,24/*Y*/,25/*Z*/
679 },
680 { /* base32hex 0123456789ABCDEFGHIJKLMNOPQRSTUV */
681 0/*0*/, 1/*1*/, 2/*2*/, 3/*3*/, 4/*4*/, 5/*5*/, 6/*6*/, 7/*7*/, 8/*8*/, 9/*9*/,
682 99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
683 10/*A*/,11/*B*/,12/*C*/,13/*D*/,14/*E*/,15/*F*/,16/*G*/,17/*H*/,18/*I*/,19/*J*/,20/*K*/,21/*L*/,22/*M*/,
684 23/*N*/,24/*O*/,25/*P*/,26/*Q*/,27/*R*/,28/*S*/,29/*T*/,30/*U*/,31/*V*/,99/*W*/,99/*X*/,99/*Y*/,99/*Z*/
685 },
686 { /* zbase32 YBNDRFG8EJKMCPQXOT1UWISZA345H769 */
687 99/*0*/,18/*1*/,99/*2*/,25/*3*/,26/*4*/,27/*5*/,30/*6*/,29/*7*/, 7/*8*/,31/*9*/,
688 99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
689 24/*A*/, 1/*B*/,12/*C*/, 3/*D*/, 8/*E*/, 5/*F*/, 6/*G*/,28/*H*/,21/*I*/, 9/*J*/,10/*K*/,99/*L*/,11/*M*/,
690 2/*N*/,16/*O*/,13/*P*/,14/*Q*/, 4/*R*/,22/*S*/,17/*T*/,19/*U*/,99/*V*/,20/*W*/,15/*X*/, 0/*Y*/,23/*Z*/
691 },
692 { /* crockford 0123456789ABCDEFGHJKMNPQRSTVWXYZ + O=>0 + IL=>1 */
693 0/*0*/, 1/*1*/, 2/*2*/, 3/*3*/, 4/*4*/, 5/*5*/, 6/*6*/, 7/*7*/, 8/*8*/, 9/*9*/,
694 99/*:*/,99/*;*/,99/*<*/,99/*=*/,99/*>*/,99/*?*/,99/*@*/,
695 10/*A*/,11/*B*/,12/*C*/,13/*D*/,14/*E*/,15/*F*/,16/*G*/,17/*H*/, 1/*I*/,18/*J*/,19/*K*/, 1/*L*/,20/*M*/,
696 21/*N*/, 0/*O*/,22/*P*/,23/*Q*/,24/*R*/,25/*S*/,26/*T*/,99/*U*/,27/*V*/,28/*W*/,29/*X*/,30/*Y*/,31/*Z*/
697 }
698 };
699
700 if (!SvOK(base32)) {
701 /* for undefined input return "" (empty string) */
702 RETVAL = newSVpv("", 0);
703 }
704 else {
705 if (!SvPOK(base32) || idx > 3) XSRETURN_UNDEF; /* error */
706 in = (unsigned char *) SvPVbyte(base32, inlen);
707 if (in == NULL) XSRETURN_UNDEF; /* error */
708
709 while (inlen>0 && in[inlen-1] == '=') inlen--;
710 if (inlen == 0) {
711 RETVAL = newSVpv("", 0);
712 }
713 else {
714 x = inlen % 8;
715 if (x == 1 || x == 3 || x == 6) XSRETURN_UNDEF; /* error */
716 outlen = (inlen * 5) / 8;
717 RETVAL = NEWSV(0, outlen);
718 SvPOK_only(RETVAL);
719 SvCUR_set(RETVAL, outlen);
720 out = (unsigned char *)SvPV_nolen(RETVAL);
721 map = tables[idx];
722 for (x = 0; x < inlen; x++) {
723 c = in[x];
724 /* convert to upper case */
725 if ((c >= 'a') && (c <= 'z')) c -= 32;
726 /* '0' = 48 .. 'Z' = 90 */
727 if (c < 48 || c > 90 || map[c-48] > 31) XSRETURN_UNDEF; /* error */
728 t = (t<<5)|map[c-48];
729 if (++y == 8) {
730 *out++ = (unsigned char)((t>>32) & 255);
731 *out++ = (unsigned char)((t>>24) & 255);
732 *out++ = (unsigned char)((t>>16) & 255);
733 *out++ = (unsigned char)((t>> 8) & 255);
734 *out++ = (unsigned char)( t & 255);
735 y = 0;
736 t = 0;
737 }
738 }
739 if (y > 0) {
740 t = t << (5 * (8 - y));
741 if (y >= 2) *out++ = (unsigned char)((t>>32) & 255);
742 if (y >= 4) *out++ = (unsigned char)((t>>24) & 255);
743 if (y >= 5) *out++ = (unsigned char)((t>>16) & 255);
744 if (y >= 7) *out++ = (unsigned char)((t>> 8) & 255);
745 }
746 }
747649 }
748650 }
749651 OUTPUT:
786688 INCLUDE: inc/CryptX_AuthEnc_ChaCha20Poly1305.xs.inc
787689
788690 INCLUDE: inc/CryptX_Stream_ChaCha.xs.inc
691 INCLUDE: inc/CryptX_Stream_Salsa20.xs.inc
789692 INCLUDE: inc/CryptX_Stream_RC4.xs.inc
790693 INCLUDE: inc/CryptX_Stream_Sober128.xs.inc
694 INCLUDE: inc/CryptX_Stream_Sosemanuk.xs.inc
791695
792696 INCLUDE: inc/CryptX_Mac_F9.xs.inc
793697 INCLUDE: inc/CryptX_Mac_HMAC.xs.inc
3232 inc/CryptX_PRNG.xs.inc
3333 inc/CryptX_Stream_ChaCha.xs.inc
3434 inc/CryptX_Stream_RC4.xs.inc
35 inc/CryptX_Stream_Salsa20.xs.inc
3536 inc/CryptX_Stream_Sober128.xs.inc
37 inc/CryptX_Stream_Sosemanuk.xs.inc
3638 lib/Crypt/AuthEnc.pm
3739 lib/Crypt/AuthEnc/CCM.pm
3840 lib/Crypt/AuthEnc/ChaCha20Poly1305.pm
5052 lib/Crypt/Cipher/CAST5.pm
5153 lib/Crypt/Cipher/DES.pm
5254 lib/Crypt/Cipher/DES_EDE.pm
55 lib/Crypt/Cipher/IDEA.pm
5356 lib/Crypt/Cipher/KASUMI.pm
5457 lib/Crypt/Cipher/Khazad.pm
5558 lib/Crypt/Cipher/MULTI2.pm
6366 lib/Crypt/Cipher/SAFER_SK64.pm
6467 lib/Crypt/Cipher/SAFERP.pm
6568 lib/Crypt/Cipher/SEED.pm
69 lib/Crypt/Cipher/Serpent.pm
6670 lib/Crypt/Cipher/Skipjack.pm
6771 lib/Crypt/Cipher/Twofish.pm
6872 lib/Crypt/Cipher/XTEA.pm
128132 lib/Crypt/PRNG/Yarrow.pm
129133 lib/Crypt/Stream/ChaCha.pm
130134 lib/Crypt/Stream/RC4.pm
135 lib/Crypt/Stream/Salsa20.pm
131136 lib/Crypt/Stream/Sober128.pm
137 lib/Crypt/Stream/Sosemanuk.pm
132138 lib/CryptX.pm
133139 lib/Math/BigInt/LTM.pm
134140 LICENSE
145151 src/ltc/ciphers/camellia.c
146152 src/ltc/ciphers/cast5.c
147153 src/ltc/ciphers/des.c
154 src/ltc/ciphers/idea.c
148155 src/ltc/ciphers/kasumi.c
149156 src/ltc/ciphers/khazad.c
150157 src/ltc/ciphers/kseed.c
156163 src/ltc/ciphers/safer/safer.c
157164 src/ltc/ciphers/safer/safer_tab.c
158165 src/ltc/ciphers/safer/saferp.c
166 src/ltc/ciphers/serpent.c
159167 src/ltc/ciphers/skipjack.c
160168 src/ltc/ciphers/twofish/twofish.c
161169 src/ltc/ciphers/twofish/twofish_tab.c
295303 src/ltc/math/rand_prime.c
296304 src/ltc/math/tfm_desc.c
297305 src/ltc/misc/adler32.c
306 src/ltc/misc/base32/base32_decode.c
307 src/ltc/misc/base32/base32_encode.c
298308 src/ltc/misc/base64/base64_decode.c
299309 src/ltc/misc/base64/base64_encode.c
300310 src/ltc/misc/burn_stack.c
515525 src/ltc/stream/chacha/chacha_ivctr64.c
516526 src/ltc/stream/chacha/chacha_keystream.c
517527 src/ltc/stream/chacha/chacha_setup.c
518 src/ltc/stream/rc4/rc4.c
519 src/ltc/stream/sober128/sober128.c
528 src/ltc/stream/rc4/rc4_stream.c
529 src/ltc/stream/salsa20/salsa20_crypt.c
530 src/ltc/stream/salsa20/salsa20_done.c
531 src/ltc/stream/salsa20/salsa20_ivctr64.c
532 src/ltc/stream/salsa20/salsa20_keystream.c
533 src/ltc/stream/salsa20/salsa20_setup.c
534 src/ltc/stream/sober128/sober128_stream.c
520535 src/ltc/stream/sober128/sober128tab.c
536 src/ltc/stream/sosemanuk/sosemanuk.c
521537 src/ltm/bn_error.c
522538 src/ltm/bn_fast_mp_invmod.c
523539 src/ltm/bn_fast_mp_montgomery_reduce.c
673689 t/cipher_cast5.t
674690 t/cipher_des.t
675691 t/cipher_des_ede.t
692 t/cipher_idea.t
693 t/cipher_idea_compat.t
676694 t/cipher_kasumi.t
677695 t/cipher_khazad.t
678696 t/cipher_multi2.t
688706 t/cipher_saferp.t
689707 t/cipher_seed.t
690708 t/cipher_seed_test_vectors_bc.t
709 t/cipher_serpent.t
710 t/cipher_serpent_compat.t
691711 t/cipher_skipjack.t
692712 t/cipher_stream.t
713 t/cipher_stream_salsa20.t
693714 t/cipher_test_vectors_ltc.t
694715 t/cipher_test_vectors_openssl.t
695716 t/cipher_twofish.t
3535 }
3636 }
3737 },
38 "release_status" : "stable",
38 "release_status" : "unstable",
3939 "resources" : {
4040 "bugtracker" : {
4141 "web" : "https://github.com/DCIT/perl-CryptX/issues"
4444 "url" : "https://github.com/DCIT/perl-CryptX"
4545 }
4646 },
47 "version" : "0.054",
47 "version" : "0.054_006",
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.054'
24 version: 0.054_006
2525 x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
2222 $mycflags =~ s/-ansi\b//g;
2323 $mycflags =~ s/-Wwrite-strings\b//g;
2424 $mycflags =~ s/-Wcast-qual\b//g;
25
26 #FIX: this is particularly useful for Debian https://github.com/DCIT/perl-CryptX/pull/39
27 $mycflags = "$ENV{CFLAGS} $ENV{CPPFLAGS}" if $ENV{CFLAGS} || $ENV{CPPFLAGS};
2528
2629 #FIX: avoid "ar: fatal: Numeric group ID too large" see https://github.com/DCIT/perl-CryptX/issues/33
2730 my $myarflags = '$(AR_STATIC_ARGS)';
1010 - Ciphers - see [Crypt::Cipher](https://metacpan.org/pod/Crypt::Cipher) and related modules
1111
1212 [Crypt::Cipher::AES](https://metacpan.org/pod/Crypt::Cipher::AES), [Crypt::Cipher::Anubis](https://metacpan.org/pod/Crypt::Cipher::Anubis), [Crypt::Cipher::Blowfish](https://metacpan.org/pod/Crypt::Cipher::Blowfish), [Crypt::Cipher::Camellia](https://metacpan.org/pod/Crypt::Cipher::Camellia), [Crypt::Cipher::CAST5](https://metacpan.org/pod/Crypt::Cipher::CAST5), [Crypt::Cipher::DES](https://metacpan.org/pod/Crypt::Cipher::DES),
13 [Crypt::Cipher::DES\_EDE](https://metacpan.org/pod/Crypt::Cipher::DES_EDE), [Crypt::Cipher::KASUMI](https://metacpan.org/pod/Crypt::Cipher::KASUMI), [Crypt::Cipher::Khazad](https://metacpan.org/pod/Crypt::Cipher::Khazad), [Crypt::Cipher::MULTI2](https://metacpan.org/pod/Crypt::Cipher::MULTI2), [Crypt::Cipher::Noekeon](https://metacpan.org/pod/Crypt::Cipher::Noekeon), [Crypt::Cipher::RC2](https://metacpan.org/pod/Crypt::Cipher::RC2),
14 [Crypt::Cipher::RC5](https://metacpan.org/pod/Crypt::Cipher::RC5), [Crypt::Cipher::RC6](https://metacpan.org/pod/Crypt::Cipher::RC6), [Crypt::Cipher::SAFERP](https://metacpan.org/pod/Crypt::Cipher::SAFERP), [Crypt::Cipher::SAFER\_K128](https://metacpan.org/pod/Crypt::Cipher::SAFER_K128), [Crypt::Cipher::SAFER\_K64](https://metacpan.org/pod/Crypt::Cipher::SAFER_K64), [Crypt::Cipher::SAFER\_SK128](https://metacpan.org/pod/Crypt::Cipher::SAFER_SK128),
15 [Crypt::Cipher::SAFER\_SK64](https://metacpan.org/pod/Crypt::Cipher::SAFER_SK64), [Crypt::Cipher::SEED](https://metacpan.org/pod/Crypt::Cipher::SEED), [Crypt::Cipher::Skipjack](https://metacpan.org/pod/Crypt::Cipher::Skipjack), [Crypt::Cipher::Twofish](https://metacpan.org/pod/Crypt::Cipher::Twofish), [Crypt::Cipher::XTEA](https://metacpan.org/pod/Crypt::Cipher::XTEA)
13 [Crypt::Cipher::DES\_EDE](https://metacpan.org/pod/Crypt::Cipher::DES_EDE), [Crypt::Cipher::IDEA](https://metacpan.org/pod/Crypt::Cipher::IDEA), [Crypt::Cipher::KASUMI](https://metacpan.org/pod/Crypt::Cipher::KASUMI), [Crypt::Cipher::Khazad](https://metacpan.org/pod/Crypt::Cipher::Khazad), [Crypt::Cipher::MULTI2](https://metacpan.org/pod/Crypt::Cipher::MULTI2), [Crypt::Cipher::Noekeon](https://metacpan.org/pod/Crypt::Cipher::Noekeon),
14 [Crypt::Cipher::RC2](https://metacpan.org/pod/Crypt::Cipher::RC2), [Crypt::Cipher::RC5](https://metacpan.org/pod/Crypt::Cipher::RC5), [Crypt::Cipher::RC6](https://metacpan.org/pod/Crypt::Cipher::RC6), [Crypt::Cipher::SAFERP](https://metacpan.org/pod/Crypt::Cipher::SAFERP), [Crypt::Cipher::SAFER\_K128](https://metacpan.org/pod/Crypt::Cipher::SAFER_K128), [Crypt::Cipher::SAFER\_K64](https://metacpan.org/pod/Crypt::Cipher::SAFER_K64),
15 [Crypt::Cipher::SAFER\_SK128](https://metacpan.org/pod/Crypt::Cipher::SAFER_SK128), [Crypt::Cipher::SAFER\_SK64](https://metacpan.org/pod/Crypt::Cipher::SAFER_SK64), [Crypt::Cipher::SEED](https://metacpan.org/pod/Crypt::Cipher::SEED), [Crypt::Cipher::Serpent](https://metacpan.org/pod/Crypt::Cipher::Serpent), [Crypt::Cipher::Skipjack](https://metacpan.org/pod/Crypt::Cipher::Skipjack),
16 [Crypt::Cipher::Twofish](https://metacpan.org/pod/Crypt::Cipher::Twofish), [Crypt::Cipher::XTEA](https://metacpan.org/pod/Crypt::Cipher::XTEA)
1617
1718 - Block cipher modes
1819
2021
2122 - Stream ciphers
2223
23 [Crypt::Stream::RC4](https://metacpan.org/pod/Crypt::Stream::RC4), [Crypt::Stream::ChaCha](https://metacpan.org/pod/Crypt::Stream::ChaCha), [Crypt::Stream::Sober128](https://metacpan.org/pod/Crypt::Stream::Sober128)
24 [Crypt::Stream::RC4](https://metacpan.org/pod/Crypt::Stream::RC4), [Crypt::Stream::ChaCha](https://metacpan.org/pod/Crypt::Stream::ChaCha), [Crypt::Stream::Salsa20](https://metacpan.org/pod/Crypt::Stream::Salsa20), [Crypt::Stream::Sober128](https://metacpan.org/pod/Crypt::Stream::Sober128)
2425
2526 - Authenticated encryption modes
2627
00 MODULE = CryptX PACKAGE = Crypt::AuthEnc::CCM
11
2 Crypt::AuthEnc::CCM
3 _new(char * cipher_name, SV * key, SV * nonce, SV * adata, int tag_len, int pt_len)
4 CODE:
5 {
6 unsigned char *k=NULL;
7 STRLEN k_len=0;
8 unsigned char *n=NULL;
9 STRLEN n_len=0;
10 unsigned char *h=NULL;
11 STRLEN h_len=0;
12 int rv, id;
13
14 if (tag_len < 1 || tag_len > MAXBLOCKSIZE) croak("FATAL: invalid tag_len %d", tag_len);
15 if (pt_len < 0) croak("FATAL: invalid pt_len");
16 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
17 k = (unsigned char *) SvPVbyte(key, k_len);
18 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
19 n = (unsigned char *) SvPVbyte(nonce, n_len);
20 if (!SvPOK(adata)) croak("FATAL: adata must be string/buffer scalar");
21 h = (unsigned char *) SvPVbyte(adata, h_len);
22
23 id = find_cipher(cipher_name);
24 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
25
26 Newz(0, RETVAL, 1, struct ccm_struct);
27 if (!RETVAL) croak("FATAL: Newz failed");
28
29 rv = ccm_init(&RETVAL->state, id, k, (unsigned long)k_len, pt_len, tag_len, h_len);
30 if (rv != CRYPT_OK) {
31 Safefree(RETVAL);
32 croak("FATAL: ccm_init failed: %s", error_to_string(rv));
33 }
34 rv = ccm_add_nonce(&RETVAL->state, n, (unsigned long)n_len);
35 if (rv != CRYPT_OK) {
36 Safefree(RETVAL);
37 croak("FATAL: ccm_add_nonce failed: %s", error_to_string(rv));
38 }
39 rv = ccm_add_aad(&RETVAL->state, h, (unsigned long)h_len);
40 if (rv != CRYPT_OK) {
41 Safefree(RETVAL);
42 croak("FATAL: ccm_add_aad failed: %s", error_to_string(rv));
43 }
44 RETVAL->direction = -1;
45 RETVAL->tag_len = tag_len;
46 RETVAL->pt_len = pt_len;
47 }
48 OUTPUT:
49 RETVAL
50
251 void
3 _memory_encrypt(char *cipher_name, SV *key, SV *nonce, SV *header, unsigned long tag_len, SV *plaintext)
52 DESTROY(Crypt::AuthEnc::CCM self)
53 CODE:
54 Safefree(self);
55
56 Crypt::AuthEnc::CCM
57 clone(Crypt::AuthEnc::CCM self)
58 CODE:
59 Newz(0, RETVAL, 1, struct ccm_struct);
60 if (!RETVAL) croak("FATAL: Newz failed");
61 Copy(&self->state, &RETVAL->state, 1, struct ccm_struct);
62 OUTPUT:
63 RETVAL
64
65 SV *
66 encrypt_add(Crypt::AuthEnc::CCM self, SV * data)
67 CODE:
68 {
69 int rv;
70 STRLEN in_data_len;
71 unsigned char *in_data, *out_data;
72
73 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
74 if (in_data_len == 0) {
75 RETVAL = newSVpvn("", 0);
76 }
77 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);
84 SvPOK_only(RETVAL);
85 SvCUR_set(RETVAL, in_data_len);
86 out_data = (unsigned char *)SvPVX(RETVAL);
87 rv = ccm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, self->direction);
88 if (rv != CRYPT_OK) {
89 SvREFCNT_dec(RETVAL);
90 croak("FATAL: ccm_process failed: %s", error_to_string(rv));
91 }
92 self->pt_len -= in_data_len;
93 }
94 }
95 OUTPUT:
96 RETVAL
97
98 SV *
99 decrypt_add(Crypt::AuthEnc::CCM self, SV * data)
100 CODE:
101 {
102 int rv, i;
103 STRLEN in_data_len;
104 unsigned char *in_data, *out_data;
105
106 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
107 if (in_data_len == 0) {
108 RETVAL = newSVpvn("", 0);
109 }
110 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);
117 SvPOK_only(RETVAL);
118 SvCUR_set(RETVAL, in_data_len);
119 out_data = (unsigned char *)SvPVX(RETVAL);
120 rv = ccm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, CCM_DECRYPT);
121 if (rv != CRYPT_OK) {
122 SvREFCNT_dec(RETVAL);
123 croak("FATAL: ccm_process failed: %s", error_to_string(rv));
124 }
125 self->pt_len -= in_data_len;
126 }
127 }
128 OUTPUT:
129 RETVAL
130
131 void
132 encrypt_done(Crypt::AuthEnc::CCM self)
4133 PPCODE:
5134 {
6 STRLEN k_len, n_len, h_len, pt_len;
7 unsigned char *k, *n, *h, *pt;
8 int rv, id;
135 int rv;
9136 unsigned char tag[MAXBLOCKSIZE];
10 SV *ct;
137 unsigned long tag_len = self->tag_len;
11138
12 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
13 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
14 if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar");
15 if (!SvPOK(plaintext)) croak("FATAL: plaintext must be string/buffer scalar");
16 k = (unsigned char *) SvPVbyte(key, k_len);
17 n = (unsigned char *) SvPVbyte(nonce, n_len);
18 h = (unsigned char *) SvPVbyte(header, h_len);
19 pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
20
21 id = find_cipher(cipher_name);
22 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
23
24 ct = NEWSV(0, pt_len);
25 SvPOK_only(ct);
26 SvCUR_set(ct, pt_len);
27
28 if(tag_len<4 || tag_len>16) tag_len = 16;
29
30 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
31 pt, (unsigned long)pt_len, (unsigned char *)SvPV_nolen(ct), tag, &tag_len, CCM_ENCRYPT);
32 if (rv != CRYPT_OK) croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
33
34 XPUSHs(sv_2mortal(ct));
35 XPUSHs(sv_2mortal(newSVpvn((char*)tag,tag_len)));
36
37 /* int ccm_memory( int cipher,
38 const unsigned char *key, unsigned long keylen,
39 symmetric_key *uskey,
40 const unsigned char *nonce, unsigned long noncelen,
41 const unsigned char *header, unsigned long headerlen,
42 unsigned char *pt, unsigned long ptlen,
43 unsigned char *ct,
44 unsigned char *tag, unsigned long *taglen,
45 int direction); */
46
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);
144 if (rv != CRYPT_OK) croak("FATAL: ccm_done failed: %s", error_to_string(rv));
145 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
47146 }
48147
49148 void
50 _memory_decrypt(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tag)
149 decrypt_done(Crypt::AuthEnc::CCM self, ...)
51150 PPCODE:
52151 {
53 STRLEN k_len, n_len, h_len, ct_len, t_len;
54 unsigned char *k, *n, *h, *ct, *t;
55 int rv, id;
56 unsigned char xtag[MAXBLOCKSIZE];
57 unsigned long xtag_len;
58 SV *pt;
152 int rv;
153 unsigned char tag[MAXBLOCKSIZE];
154 unsigned long tag_len = self->tag_len;
155 STRLEN expected_tag_len;
156 unsigned char *expected_tag;
59157
60 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
61 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
62 if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar");
63 if (!SvPOK(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar");
64 if (!SvPOK(tag)) croak("FATAL: tag must be string/buffer scalar");
65 k = (unsigned char *) SvPVbyte(key, k_len);
66 n = (unsigned char *) SvPVbyte(nonce, n_len);
67 h = (unsigned char *) SvPVbyte(header, h_len);
68 ct = (unsigned char *) SvPVbyte(ciphertext, ct_len);
69 t = (unsigned char *) SvPVbyte(tag, t_len);
70
71 id = find_cipher(cipher_name);
72 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
73
74 pt = NEWSV(0, ct_len);
75 SvPOK_only(pt);
76 SvCUR_set(pt, ct_len);
77
78 xtag_len = (unsigned long)t_len;
79 Copy(t, xtag, t_len, unsigned char);
80
81 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
82 (unsigned char *)SvPV_nolen(pt), (unsigned long)ct_len, ct, xtag, &xtag_len, CCM_DECRYPT);
83 if (rv != CRYPT_OK) {
84 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
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);
163 if (rv != CRYPT_OK) croak("FATAL: ccm_done failed: %s", error_to_string(rv));
164 if (items == 1) {
165 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
85166 }
86167 else {
87 XPUSHs(sv_2mortal(pt));
168 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
169 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
170 if (expected_tag_len!=tag_len) {
171 XPUSHs(sv_2mortal(newSViv(0))); /* false */
172 }
173 else if (memNE(expected_tag, tag, tag_len)) {
174 XPUSHs(sv_2mortal(newSViv(0))); /* false */
175 }
176 else {
177 XPUSHs(sv_2mortal(newSViv(1))); /* true */
178 }
88179 }
89180 }
1818 if (!RETVAL) croak("FATAL: Newz failed");
1919
2020 rv = chacha20poly1305_init(&RETVAL->state, k, (unsigned long)k_len);
21 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv));
21 if (rv != CRYPT_OK) {
22 Safefree(RETVAL);
23 croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv));
24 }
2225
2326 if (iv && iv_len > 0) {
2427 rv = chacha20poly1305_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
25 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
28 if (rv != CRYPT_OK) {
29 Safefree(RETVAL);
30 croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
31 }
2632 }
2733 }
2834 OUTPUT:
95101 unsigned char *in_data, *out_data;
96102
97103 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
98 if (in_data_len==0) {
104 if (in_data_len == 0) {
99105 RETVAL = newSVpvn("", 0);
100106 }
101107 else {
102108 RETVAL = NEWSV(0, in_data_len);
103109 SvPOK_only(RETVAL);
104110 SvCUR_set(RETVAL, in_data_len);
105 out_data = (unsigned char *)SvPV_nolen(RETVAL);
111 out_data = (unsigned char *)SvPVX(RETVAL);
106112 rv = chacha20poly1305_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
107 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_decrypt failed: %s", error_to_string(rv));
113 if (rv != CRYPT_OK) {
114 SvREFCNT_dec(RETVAL);
115 croak("FATAL: chacha20poly1305_decrypt failed: %s", error_to_string(rv));
116 }
108117 }
109118 }
110119 OUTPUT:
119128 unsigned char *in_data, *out_data;
120129
121130 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
122 if (in_data_len==0) {
131 if (in_data_len == 0) {
123132 RETVAL = newSVpvn("", 0);
124133 }
125134 else {
126135 RETVAL = NEWSV(0, in_data_len);
127136 SvPOK_only(RETVAL);
128137 SvCUR_set(RETVAL, in_data_len);
129 out_data = (unsigned char *)SvPV_nolen(RETVAL);
138 out_data = (unsigned char *)SvPVX(RETVAL);
130139 rv = chacha20poly1305_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
131 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_encrypt failed: %s", error_to_string(rv));
140 if (rv != CRYPT_OK) {
141 SvREFCNT_dec(RETVAL);
142 croak("FATAL: chacha20poly1305_encrypt failed: %s", error_to_string(rv));
143 }
132144 }
133145 }
134146 OUTPUT:
163175 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
164176 }
165177 else {
166 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
178 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
167179 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
168180 if (expected_tag_len!=tag_len) {
169181 XPUSHs(sv_2mortal(newSViv(0))); /* false */
99 STRLEN n_len=0;
1010 unsigned char *h=NULL;
1111 STRLEN h_len=0;
12 int id;
12 int rv, id;
1313
1414 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1515 k = (unsigned char *) SvPVbyte(key, k_len);
1616 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
1717 n = (unsigned char *) SvPVbyte(nonce, n_len);
18 if(SvOK(adata)) { /* adata is optional param */
19 if(!SvPOK(adata)) croak("FATAL: adata must be string/buffer scalar");
18 if (SvOK(adata)) { /* adata is optional param */
19 if (!SvPOK(adata)) croak("FATAL: adata must be string/buffer scalar");
2020 h = (unsigned char *) SvPVbyte(adata, h_len);
2121 }
2222
2323 id = find_cipher(cipher_name);
24 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
24 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
2525
2626 Newz(0, RETVAL, 1, struct eax_struct);
2727 if (!RETVAL) croak("FATAL: Newz failed");
2828
29 if (eax_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len) != CRYPT_OK) {
30 croak("FATAL: eax setup failed");
29 rv = eax_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len);
30 if (rv != CRYPT_OK) {
31 Safefree(RETVAL);
32 croak("FATAL: eax setup failed: %s", error_to_string(rv));
3133 }
3234 }
3335 OUTPUT:
5658 unsigned char *in_data, *out_data;
5759
5860 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
59 if (in_data_len==0) {
61 if (in_data_len == 0) {
6062 RETVAL = newSVpvn("", 0);
6163 }
6264 else {
6365 RETVAL = NEWSV(0, in_data_len);
6466 SvPOK_only(RETVAL);
6567 SvCUR_set(RETVAL, in_data_len);
66 out_data = (unsigned char *)SvPV_nolen(RETVAL);
68 out_data = (unsigned char *)SvPVX(RETVAL);
6769 rv = eax_encrypt(&self->state, in_data, out_data, (unsigned long)in_data_len);
68 if (rv != CRYPT_OK) croak("FATAL: eax_encrypt failed: %s", error_to_string(rv));
70 if (rv != CRYPT_OK) {
71 SvREFCNT_dec(RETVAL);
72 croak("FATAL: eax_encrypt failed: %s", error_to_string(rv));
73 }
6974 }
7075 }
7176 OUTPUT:
8085 unsigned char *in_data, *out_data;
8186
8287 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
83 if (in_data_len==0) {
88 if (in_data_len == 0) {
8489 RETVAL = newSVpvn("", 0);
8590 }
8691 else {
8792 RETVAL = NEWSV(0, in_data_len);
8893 SvPOK_only(RETVAL);
8994 SvCUR_set(RETVAL, in_data_len);
90 out_data = (unsigned char *)SvPV_nolen(RETVAL);
95 out_data = (unsigned char *)SvPVX(RETVAL);
9196 rv = eax_decrypt(&self->state, in_data, out_data, (unsigned long)in_data_len);
92 if (rv != CRYPT_OK) croak("FATAL: eax_decrypt failed: %s", error_to_string(rv));
97 if (rv != CRYPT_OK) {
98 SvREFCNT_dec(RETVAL);
99 croak("FATAL: eax_decrypt failed: %s", error_to_string(rv));
100 }
93101 }
94102 }
95103 OUTPUT:
124132 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
125133 }
126134 else {
127 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
135 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
128136 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
129137 if (expected_tag_len!=tag_len) {
130138 XPUSHs(sv_2mortal(newSViv(0))); /* false */
2121 if (!RETVAL) croak("FATAL: Newz failed");
2222
2323 rv = gcm_init(&RETVAL->state, id, k, (unsigned long)k_len);
24 if (rv != CRYPT_OK) croak("FATAL: gcm_init failed: %s", error_to_string(rv));
24 if (rv != CRYPT_OK) {
25 Safefree(RETVAL);
26 croak("FATAL: gcm_init failed: %s", error_to_string(rv));
27 }
2528
2629 if (iv && iv_len > 0) {
2730 rv = gcm_add_iv(&RETVAL->state, iv, (unsigned long)iv_len);
28 if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
31 if (rv != CRYPT_OK) {
32 Safefree(RETVAL);
33 croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
34 }
2935 }
3036 }
3137 OUTPUT:
6470 unsigned char *in_data, *out_data;
6571
6672 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
67 if (in_data_len==0) {
73 if (in_data_len == 0) {
6874 RETVAL = newSVpvn("", 0);
6975 }
7076 else
7278 RETVAL = NEWSV(0, in_data_len);
7379 SvPOK_only(RETVAL);
7480 SvCUR_set(RETVAL, in_data_len);
75 out_data = (unsigned char *)SvPV_nolen(RETVAL);
81 out_data = (unsigned char *)SvPVX(RETVAL);
7682 rv = gcm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT);
77 if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
83 if (rv != CRYPT_OK) {
84 SvREFCNT_dec(RETVAL);
85 croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
86 }
7887 }
7988 }
8089 OUTPUT:
117126 unsigned char *in_data, *out_data;
118127
119128 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
120 if (in_data_len==0) {
129 if (in_data_len == 0) {
121130 RETVAL = newSVpvn("", 0);
122131 }
123132 else {
124133 RETVAL = NEWSV(0, in_data_len);
125134 SvPOK_only(RETVAL);
126135 SvCUR_set(RETVAL, in_data_len);
127 out_data = (unsigned char *)SvPV_nolen(RETVAL);
136 out_data = (unsigned char *)SvPVX(RETVAL);
128137 rv = gcm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT);
129 if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
138 if (rv != CRYPT_OK) {
139 SvREFCNT_dec(RETVAL);
140 croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv));
141 }
130142 }
131143 }
132144 OUTPUT:
162174 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
163175 }
164176 else {
165 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
177 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
166178 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
167179 if (expected_tag_len!=tag_len) {
168180 XPUSHs(sv_2mortal(newSViv(0))); /* false */
77 unsigned char *k=NULL;
88 unsigned char *n=NULL;
99 STRLEN n_len=0;
10 int id;
10 int rv, id;
1111
1212 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1313 k = (unsigned char *) SvPVbyte(key, k_len);
1515 n = (unsigned char *) SvPVbyte(nonce, n_len);
1616
1717 id = find_cipher(cipher_name);
18 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1919
2020 Newz(0, RETVAL, 1, struct ocb_struct);
2121 if (!RETVAL) croak("FATAL: Newz failed");
2222
23 if (ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, taglen) != CRYPT_OK) {
24 croak("FATAL: ocb setup failed");
23 rv = ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, taglen);
24 if (rv != CRYPT_OK) {
25 Safefree(RETVAL);
26 croak("FATAL: ocb setup failed: %s", error_to_string(rv));
2527 }
2628 }
2729 OUTPUT:
6769 unsigned char *in_data, *out_data;
6870
6971 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
70 if (in_data_len==0) {
71 RETVAL = newSVpvn("", 0);
72 }
73 else {
74 RETVAL = NEWSV(0, in_data_len);
75 SvPOK_only(RETVAL);
76 SvCUR_set(RETVAL, in_data_len);
77 out_data = (unsigned char *)SvPV_nolen(RETVAL);
78
79 if (in_data_len % (&self->state)->block_len)
72 if (in_data_len == 0) {
73 RETVAL = newSVpvn("", 0);
74 }
75 else {
76 if (in_data_len % (&self->state)->block_len) {
8077 croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len);
81
78 }
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);
8283 rv = ocb3_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
83 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv));
84 if (rv != CRYPT_OK) {
85 SvREFCNT_dec(RETVAL);
86 croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv));
87 }
8488 }
8589 }
8690 OUTPUT:
9599 unsigned char *in_data, *out_data;
96100
97101 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
98 if (in_data_len>0) {
99 RETVAL = NEWSV(0, in_data_len);
100 SvPOK_only(RETVAL);
101 SvCUR_set(RETVAL, in_data_len);
102 out_data = (unsigned char *)SvPV_nolen(RETVAL);
103 }
104 else {
105 RETVAL = newSVpvn("", 0);
106 out_data = NULL;
107 }
108 rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
109 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
102 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);
115 if (rv != CRYPT_OK) {
116 SvREFCNT_dec(RETVAL);
117 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
118 }
119 }
110120 }
111121 OUTPUT:
112122 RETVAL
120130 unsigned char *in_data, *out_data;
121131
122132 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
123 if (in_data_len==0) {
124 RETVAL = newSVpvn("", 0);
125 }
126 else {
127 RETVAL = NEWSV(0, in_data_len);
128 SvPOK_only(RETVAL);
129 SvCUR_set(RETVAL, in_data_len);
130 out_data = (unsigned char *)SvPV_nolen(RETVAL);
131
132 if (in_data_len % (&self->state)->block_len)
133 if (in_data_len == 0) {
134 RETVAL = newSVpvn("", 0);
135 }
136 else {
137 if (in_data_len % (&self->state)->block_len) {
133138 croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len);
134
139 }
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);
135144 rv = ocb3_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
136 if (rv != CRYPT_OK) croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv));
145 if (rv != CRYPT_OK) {
146 SvREFCNT_dec(RETVAL);
147 croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv));
148 }
137149 }
138150 }
139151 OUTPUT:
148160 unsigned char *in_data, *out_data;
149161
150162 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
151 if (in_data_len>0) {
152 RETVAL = NEWSV(0, in_data_len);
153 SvPOK_only(RETVAL);
154 SvCUR_set(RETVAL, in_data_len);
155 out_data = (unsigned char *)SvPV_nolen(RETVAL);
156 }
157 else {
158 RETVAL = newSVpvn("", 0);
159 out_data = NULL;
160 }
161 rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
162 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
163 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);
176 if (rv != CRYPT_OK) {
177 SvREFCNT_dec(RETVAL);
178 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
179 }
180 }
163181 }
164182 OUTPUT:
165183 RETVAL
194212 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
195213 }
196214 else {
197 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
215 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
198216 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
199217 if (expected_tag_len!=tag_len) {
200218 XPUSHs(sv_2mortal(newSViv(0))); /* false */
22 Crypt::Checksum::Adler32
33 new(Class)
44 CODE:
5 {
56 Newz(0, RETVAL, 1, adler32_state);
67 if (!RETVAL) croak("FATAL: Newz failed");
7 adler32_init(RETVAL);
8 adler32_init(RETVAL); /* returns void */
9 }
810 OUTPUT:
911 RETVAL
1012
1719 reset(Crypt::Checksum::Adler32 self)
1820 PPCODE:
1921 {
20 adler32_init(self);
22 adler32_init(self); /* returns void */
2123 XPUSHs(ST(0)); /* return self */
2224 }
2325
3941 unsigned char *in;
4042 for(i=1; i<items; i++) {
4143 in = (unsigned char *)SvPVbyte(ST(i), inlen);
42 if (inlen>0) adler32_update(self, in, (unsigned long)inlen);
44 if (inlen > 0) {
45 adler32_update(self, in, (unsigned long)inlen); /* returns void */
46 }
4347 }
4448 XPUSHs(ST(0)); /* return self */
4549 }
4953 CODE:
5054 {
5155 unsigned char hash[4];
52 adler32_finish(self, hash, 4);
56 adler32_finish(self, hash, 4); /* returns void */
5357 RETVAL = newSVpvn((char *) hash, 4);
5458 }
5559 OUTPUT:
6266 unsigned long i;
6367 unsigned char hash[4];
6468 char hash_hex[4*2 + 1];
65 adler32_finish(self, hash, 4);
69 adler32_finish(self, hash, 4); /* returns void */
6670 hash_hex[0] = '\0';
6771 for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]);
6872 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
22 Crypt::Checksum::CRC32
33 new(Class)
44 CODE:
5 {
6 int rv;
57 Newz(0, RETVAL, 1, crc32_state);
68 if (!RETVAL) croak("FATAL: Newz failed");
7 crc32_init(RETVAL);
9 crc32_init(RETVAL); /* returns void */
10 }
811 OUTPUT:
912 RETVAL
1013
1720 reset(Crypt::Checksum::CRC32 self)
1821 PPCODE:
1922 {
20 crc32_init(self);
23 crc32_init(self); /* returns void */
2124 XPUSHs(ST(0)); /* return self */
2225 }
2326
3942 unsigned char *in;
4043 for(i=1; i<items; i++) {
4144 in = (unsigned char *)SvPVbyte(ST(i), inlen);
42 if (inlen>0) crc32_update(self, in, (unsigned long)inlen);
45 if (inlen > 0) {
46 crc32_update(self, in, (unsigned long)inlen); /* returns void */
47 }
4348 }
4449 XPUSHs(ST(0)); /* return self */
4550 }
4954 CODE:
5055 {
5156 unsigned char hash[4];
52 crc32_finish(self, hash, 4);
57 crc32_finish(self, hash, 4); /* returns void */
5358 RETVAL = newSVpvn((char *) hash, 4);
5459 }
5560 OUTPUT:
6267 unsigned long i;
6368 unsigned char hash[4];
6469 char hash_hex[4*2 + 1];
65 crc32_finish(self, hash, 4);
70 crc32_finish(self, hash, 4); /* returns void */
6671 hash_hex[0] = '\0';
6772 for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]);
6873 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
1515 key_data = (unsigned char *)SvPVbyte(key, key_len);
1616
1717 id = find_cipher(cipher_name);
18 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1919
2020 Newz(0, RETVAL, 1, struct cipher_struct);
2121 if (!RETVAL) croak("FATAL: Newz failed");
2323 RETVAL->id = id;
2424 RETVAL->desc = &cipher_descriptor[id];
2525 rv = RETVAL->desc->setup(key_data, (int)key_len, rounds, &RETVAL->skey);
26 if(rv!=CRYPT_OK) croak("FATAL: cipher setup failed: %s", error_to_string(rv));
26 if (rv != CRYPT_OK) {
27 Safefree(RETVAL);
28 croak("FATAL: cipher setup failed: %s", error_to_string(rv));
29 }
2730 }
2831 OUTPUT:
2932 RETVAL
7679 STRLEN len;
7780 void *plaintext = SvPVbyte(data, len);
7881
79 if (len==0)
82 if (len == 0) {
8083 RETVAL = newSVpvn("", 0);
81 else if (len % self->desc->block_length)
82 croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", self->desc->block_length);
83 else {
84 /* idea from Crypt::Rijndael */
84 }
85 else if (len == self->desc->block_length) {
8586 RETVAL = NEWSV(0, len);
8687 SvPOK_only(RETVAL);
8788 SvCUR_set(RETVAL, len);
88 rv = self->desc->ecb_encrypt((unsigned char *)plaintext, (unsigned char *)SvPV_nolen(RETVAL), &self->skey);
89 if (rv!=CRYPT_OK) croak("FATAL: encrypt failed: %s", error_to_string(rv));
89 rv = self->desc->ecb_encrypt((unsigned char *)plaintext, (unsigned char *)SvPVX(RETVAL), &self->skey);
90 if (rv!=CRYPT_OK) {
91 SvREFCNT_dec(RETVAL);
92 croak("FATAL: encrypt failed: %s", error_to_string(rv));
93 }
94 }
95 else {
96 croak ("FATAL: input size not equal to blocksize (%d)", self->desc->block_length);
9097 }
9198 }
9299 OUTPUT:
102109 STRLEN len;
103110 void *ciphertext = SvPVbyte(data, len);
104111
105 if (len==0)
112 if (len == 0) {
106113 RETVAL = newSVpvn("", 0);
107 else if (len % self->desc->block_length)
108 croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", self->desc->block_length);
109 else {
110 /* idea from Crypt::Rijndael */
114 }
115 else if (len == self->desc->block_length) {
111116 RETVAL = NEWSV(0, len);
112117 SvPOK_only(RETVAL);
113118 SvCUR_set(RETVAL, len);
114 rv = self->desc->ecb_decrypt((unsigned char *)ciphertext, (unsigned char *)SvPV_nolen(RETVAL), &self->skey);
115 if (rv!=CRYPT_OK) croak("FATAL: decrypt failed: %s", error_to_string(rv));
119 rv = self->desc->ecb_decrypt((unsigned char *)ciphertext, (unsigned char *)SvPVX(RETVAL), &self->skey);
120 if (rv!=CRYPT_OK) {
121 SvREFCNT_dec(RETVAL);
122 croak("FATAL: decrypt failed: %s", error_to_string(rv));
123 }
124 }
125 else {
126 croak ("FATAL: input size not equal to blocksize (%d)", self->desc->block_length);
116127 }
117128 }
118129 OUTPUT:
126137 int rv, id;
127138
128139 id = find_cipher(cipher_name);
129 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
140 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
130141
131142 rv = cipher_descriptor[id].block_length;
132143 if (!rv) XSRETURN_UNDEF;
143154 int rv, id;
144155
145156 id = find_cipher(cipher_name);
146 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
157 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
147158
148159 rv = cipher_descriptor[id].min_key_length;
149160 if (!rv) XSRETURN_UNDEF;
160171 int rv, id;
161172
162173 id = find_cipher(cipher_name);
163 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
174 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
164175
165176 rv = cipher_descriptor[id].max_key_length;
166177 if (!rv) XSRETURN_UNDEF;
177188 int rv, id;
178189
179190 id = find_cipher(cipher_name);
180 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
191 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
181192
182193 rv = cipher_descriptor[id].default_rounds;
183194 if (!rv) XSRETURN_UNDEF;
88 int id;
99
1010 id = find_hash(digest_name);
11 if(id==-1) croak("FATAL: find_hash failed for '%s'", digest_name);
11 if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name);
1212
1313 Newz(0, RETVAL, 1, struct digest_struct);
1414 if (!RETVAL) croak("FATAL: Newz failed");
1616 RETVAL->id = id;
1717 RETVAL->desc = &hash_descriptor[id];
1818 rv = RETVAL->desc->init(&RETVAL->state);
19 if(rv!=CRYPT_OK) croak("FATAL: digest setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: digest setup failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
4346 Crypt::Digest self
4447 CODE:
4548 Newz(0, RETVAL, 1, struct digest_struct);
49 if (!RETVAL) croak("FATAL: Newz failed");
4650 Copy(&self->state, &RETVAL->state, 1, struct digest_struct);
4751 OUTPUT:
4852 RETVAL
159163 int rv, id;
160164
161165 id = find_hash(digest_name);
162 if(id==-1) croak("FATAL: find_digest failed for '%s'", digest_name);
166 if (id == -1) croak("FATAL: find_digest failed for '%s'", digest_name);
163167
164168 rv = hash_descriptor[id].hashsize;
165169 if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);;
1010
1111 RETVAL->num = num;
1212 rv = sha3_shake_init(&RETVAL->state, RETVAL->num);
13 if (rv != CRYPT_OK) croak("FATAL: sha3_shake_init failed: %s", error_to_string(rv));
13 if (rv != CRYPT_OK) {
14 Safefree(RETVAL);
15 croak("FATAL: sha3_shake_init failed: %s", error_to_string(rv));
16 }
1417 }
1518 OUTPUT:
1619 RETVAL
3437 clone(Crypt::Digest::SHAKE self)
3538 CODE:
3639 Newz(0, RETVAL, 1, struct digest_shake_struct);
40 if (!RETVAL) croak("FATAL: Newz failed");
3741 Copy(&self->state, &RETVAL->state, 1, struct digest_shake_struct);
3842 OUTPUT:
3943 RETVAL
6670 RETVAL = NEWSV(0, out_len);
6771 SvPOK_only(RETVAL);
6872 SvCUR_set(RETVAL, out_len);
69 out_data = (unsigned char *)SvPV_nolen(RETVAL);
73 out_data = (unsigned char *)SvPVX(RETVAL);
7074 rv = sha3_shake_done(&self->state, out_data, out_len);
71 if (rv != CRYPT_OK) croak("FATAL: sha3_shake_done failed: %s", error_to_string(rv));
75 if (rv != CRYPT_OK) {
76 SvREFCNT_dec(RETVAL);
77 croak("FATAL: sha3_shake_done failed: %s", error_to_string(rv));
78 }
7279 }
7380 OUTPUT:
7481 RETVAL
00 MODULE = CryptX PACKAGE = Crypt::KeyDerivation
11
22 SV *
3 _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, int len)
3 _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
44 CODE:
55 {
66 /*
1010 unsigned char *out, unsigned long *outlen)
1111 */
1212 int rv, id;
13 unsigned long output_len;
1413 unsigned char *output;
1514 unsigned char *password_ptr=NULL;
1615 STRLEN password_len=0;
1817 STRLEN salt_len=0;
1918
2019 id = find_hash(hash_name);
21 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
20 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
2221
2322 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
2423 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
2524 if (salt_len < 8) croak("FATAL: salt_len has to be 8");
2625
27 output_len = len;
28 Newz(0, output, output_len, unsigned char);
29 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
26 RETVAL = NEWSV(0, output_len);
27 SvPOK_only(RETVAL);
28 SvCUR_set(RETVAL, output_len);
29 output = (unsigned char *)SvPVX(RETVAL);
3030
3131 rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
32 if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
33
34 RETVAL = newSVpvn((char *)output, output_len);
35 Safefree(output);
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);
3637 }
3738 OUTPUT:
3839 RETVAL
3940
4041 SV *
41 _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, int len)
42 _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
4243 CODE:
4344 {
4445 /*
4849 unsigned char *out, unsigned long *outlen)
4950 */
5051 int rv, id;
51 unsigned long output_len;
5252 unsigned char *output;
5353 unsigned char *password_ptr=NULL;
5454 STRLEN password_len=0;
5656 STRLEN salt_len=0;
5757
5858 id = find_hash(hash_name);
59 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
59 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
6060
6161 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
6262 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
6363
64 output_len = len;
65 Newz(0, output, output_len, unsigned char);
66 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
64 RETVAL = NEWSV(0, output_len);
65 SvPOK_only(RETVAL);
66 SvCUR_set(RETVAL, output_len);
67 output = (unsigned char *)SvPVX(RETVAL);
6768
6869 rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len);
69 if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
70
71 RETVAL = newSVpvn((char *)output, output_len);
72 Safefree(output);
70 if (rv != CRYPT_OK) {
71 SvREFCNT_dec(RETVAL);
72 croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
73 }
74 SvCUR_set(RETVAL, output_len);
7375 }
7476 OUTPUT:
7577 RETVAL
9294 STRLEN salt_len=0;
9395
9496 id = find_hash(hash_name);
95 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
97 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
9698
9799 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
98100 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
123125 STRLEN info_len=0;
124126
125127 id = find_hash(hash_name);
126 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
128 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
127129
128130 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
129131 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
130132
131 Newz(0, output, output_len, unsigned char);
132 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
133 RETVAL = NEWSV(0, output_len);
134 SvPOK_only(RETVAL);
135 SvCUR_set(RETVAL, output_len);
136 output = (unsigned char *)SvPVX(RETVAL);
133137
134138 rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
135 if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
136
137 RETVAL = newSVpvn((char *)output, output_len);
138 Safefree(output);
139 if (rv != CRYPT_OK) {
140 SvREFCNT_dec(RETVAL);
141 croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
142 }
143 SvCUR_set(RETVAL, output_len);
139144 }
140145 OUTPUT:
141146 RETVAL
160165 STRLEN salt_len=0;
161166
162167 id = find_hash(hash_name);
163 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
168 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
164169
165 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
170 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
166171 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
167172 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
168173
169 Newz(0, output, output_len, unsigned char);
170 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
174 RETVAL = NEWSV(0, output_len);
175 SvPOK_only(RETVAL);
176 SvCUR_set(RETVAL, output_len);
177 output = (unsigned char *)SvPVX(RETVAL);
171178
172179 rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
173 if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
174
175 RETVAL = newSVpvn((char *)output, output_len);
176 Safefree(output);
180 if (rv != CRYPT_OK) {
181 SvREFCNT_dec(RETVAL);
182 croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
183 }
184 SvCUR_set(RETVAL, output_len);
177185 }
178186 OUTPUT:
179187 RETVAL
180
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = blake2bmac_init(&RETVAL->state, size, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: blake2b_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: blake2b_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = blake2smac_init(&RETVAL->state, size, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: blake2s_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: blake2s_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 rv = f9_init(&RETVAL->state, id, k, (unsigned long)k_len);
23 if (rv != CRYPT_OK) croak("FATAL: f9_init failed: %s", error_to_string(rv));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: f9_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1111 int id;
1212
1313 id = find_hash(hash_name);
14 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
14 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 rv = hmac_init(&RETVAL->state, id, k, (unsigned long)k_len);
23 if (rv != CRYPT_OK) croak("FATAL: hmac_init failed: %s", error_to_string(rv));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: hmac_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 rv = omac_init(&RETVAL->state, id, k, (unsigned long)k_len);
23 if (rv != CRYPT_OK) croak("FATAL: omac_init failed: %s", error_to_string(rv));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: omac_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 rv = pmac_init(&RETVAL->state, id, k, (unsigned long)k_len);
23 if (rv != CRYPT_OK) croak("FATAL: pmac_init failed: %s", error_to_string(rv));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: pmac_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = pelican_init(&RETVAL->state, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: pelican_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: pelican_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = poly1305_init(&RETVAL->state, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: poly1305_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: poly1305_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 rv = xcbc_init(&RETVAL->state, id, k, (unsigned long)k_len);
23 if (rv != CRYPT_OK) croak("FATAL: xcbc_init failed: %s", error_to_string(rv));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: xcbc_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1212 RETVAL->direction = 0;
1313 RETVAL->cipher_rounds = rounds;
1414 RETVAL->cipher_id = find_cipher(cipher_name);
15 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
15 if (RETVAL->cipher_id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 }
1619 }
1720 OUTPUT:
1821 RETVAL
7275 in_data_start = 0;
7376 has_tmp_block = 0;
7477 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
75 if (in_data_len==0) {
78 if (in_data_len == 0) {
7679 RETVAL = newSVpvn("", 0);
7780 }
7881 else {
79 if(self->padlen > 0) {
82 if (self->padlen > 0) {
8083 i = (blen - self->padlen);
8184 if (in_data_len >= i) { /* enough data to fill pad */
8285 Copy(in_data, self->pad+self->padlen, i, unsigned char);
101104 in_data_len -= i;
102105 }
103106
104 if (in_data_len>0) {
107 if (in_data_len > 0) {
105108 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
106109 RETVAL = NEWSV(0, i);
107110 SvPOK_only(RETVAL);
108111 SvCUR_set(RETVAL, i);
109 out_data = (unsigned char *)SvPV_nolen(RETVAL);
110
112 out_data = (unsigned char *)SvPVX(RETVAL);
111113 if (has_tmp_block) {
112114 Copy(tmp_block, out_data, blen, unsigned char);
113115 out_data += blen;
114116 }
115117 rv = cbc_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
116 if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
118 if (rv != CRYPT_OK) {
119 SvREFCNT_dec(RETVAL);
120 croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
121 }
117122 } /* in_data_len>0 */
118123 else if (has_tmp_block) {
119124 RETVAL = newSVpvn((char*)tmp_block, blen);
136141 blen = (&self->state)->blocklen;
137142 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
138143
139 if(self->padding_mode == 1) { /* pkcs5|7 padding */
144 if (self->padding_mode == 1) { /* pkcs5|7 padding */
140145 i = blen - self->padlen;
141146 if (i == 0) i = blen;
142147 for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i;
143148 rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state);
144149 if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
145150 }
146 else if(self->padding_mode == 2) { /* oneandzeroes padding */
151 else if (self->padding_mode == 2) { /* oneandzeroes padding */
147152 self->pad[self->padlen] = 0x80;
148153 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
149154 rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state);
175180 in_data_start = 0;
176181 has_tmp_block = 0;
177182 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
178 if (in_data_len==0) {
183 if (in_data_len == 0) {
179184 RETVAL = newSVpvn("", 0);
180185 }
181186 else {
182187
183 if(self->padlen == blen) {
188 if (self->padlen == blen) {
184189 rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state);
185190 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
186191 self->padlen = 0;
187192 has_tmp_block = 1;
188193 } /* padlen == blen */
189 else if(self->padlen > 0) {
194 else if (self->padlen > 0) {
190195 i = (blen - self->padlen); /* remaining bytes in padding buffer */
191196 if (in_data_len >= i) { /* enough data to fill pad */
192197 Copy(in_data, self->pad+self->padlen, i, unsigned char);
207212 }
208213 } /* padlen > 0 */
209214
210 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
215 /* here: a/ padlen == 1..16 && in_data_len == 0; b/ padlen == 0 && in_data_len > 0 */
211216 if (in_data_len>0) {
212217 i = (unsigned long)(in_data_len % blen);
213218 if (i>0) { /* save tail of data into pad */
218223 }
219224
220225 if (in_data_len>0) {
221 if(self->padlen == 0 && self->padding_mode !=0) {
226 if (self->padlen == 0 && self->padding_mode !=0) {
222227 /* in case of padding keep full pad if no more data */
223228 Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char);
224229 self->padlen = blen;
232237 RETVAL = NEWSV(0, i);
233238 SvPOK_only(RETVAL);
234239 SvCUR_set(RETVAL, i);
235 out_data = (unsigned char *)SvPV_nolen(RETVAL);
240 out_data = (unsigned char *)SvPVX(RETVAL);
236241 if (has_tmp_block) {
237242 Copy(tmp_block, out_data, blen, unsigned char);
238243 out_data += blen;
239244 }
240245 rv = cbc_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
241 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
246 if (rv != CRYPT_OK) {
247 SvREFCNT_dec(RETVAL);
248 croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
249 }
242250 }
243251 } /* in_data_len>0 */
244252 else if (has_tmp_block) {
267275 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
268276 rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state);
269277 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
270 if(self->padding_mode == 0) { /* no padding */
278 if (self->padding_mode == 0) { /* no padding */
271279 rv_len = blen;
272280 }
273 else if(self->padding_mode == 1) { /* pkcs5|7 padding */
281 else if (self->padding_mode == 1) { /* pkcs5|7 padding */
274282 i = tmp_block[blen-1];
275283 rv_len = blen - (i>blen ? blen : i);
276284 }
277 else if(self->padding_mode == 2) { /* oneandzeroes padding */
285 else if (self->padding_mode == 2) { /* oneandzeroes padding */
278286 rv_len = blen;
279287 while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--;
280288 if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--;
1010 RETVAL->direction = 0;
1111 RETVAL->cipher_rounds = rounds;
1212 RETVAL->cipher_id = find_cipher(cipher_name);
13 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
13 if (RETVAL->cipher_id == -1) {
14 Safefree(RETVAL);
15 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
16 }
1417 }
1518 OUTPUT:
1619 RETVAL
6366 unsigned char *in_data, *out_data;
6467
6568 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
66 if (in_data_len==0) {
69 if (in_data_len == 0) {
6770 RETVAL = newSVpvn("", 0);
6871 }
6972 else {
7073 RETVAL = NEWSV(0, in_data_len);
7174 SvPOK_only(RETVAL);
7275 SvCUR_set(RETVAL, in_data_len);
73 out_data = (unsigned char *)SvPV_nolen(RETVAL);
76 out_data = (unsigned char *)SvPVX(RETVAL);
7477
7578 if (self->direction == 1) {
7679 rv = cfb_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state);
77 if (rv != CRYPT_OK) croak("FATAL: cfb_encrypt failed: %s", error_to_string(rv));
80 if (rv != CRYPT_OK) {
81 SvREFCNT_dec(RETVAL);
82 croak("FATAL: cfb_encrypt failed: %s", error_to_string(rv));
83 }
7884 }
7985 else if (self->direction == -1) {
8086 rv = cfb_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state);
81 if (rv != CRYPT_OK) croak("FATAL: cfb_decrypt failed: %s", error_to_string(rv));
87 if (rv != CRYPT_OK) {
88 SvREFCNT_dec(RETVAL);
89 croak("FATAL: cfb_decrypt failed: %s", error_to_string(rv));
90 }
8291 }
8392 else {
93 SvREFCNT_dec(RETVAL);
8494 croak("FATAL: cfb_crypt failed: call start_encrypt or start_decrypt first");
8595 }
8696 }
1010 RETVAL->direction = 0;
1111 RETVAL->cipher_rounds = rounds;
1212 RETVAL->cipher_id = find_cipher(cipher_name);
13 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
13 if (RETVAL->cipher_id == -1) {
14 Safefree(RETVAL);
15 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
16 }
1417 if (ctr_mode == 0) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN;
1518 if (ctr_mode == 1) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN;
1619 if (ctr_mode == 2) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN|LTC_CTR_RFC3686;
6871 unsigned char *in_data, *out_data;
6972
7073 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
71 if (in_data_len==0) {
74 if (in_data_len == 0) {
7275 RETVAL = newSVpvn("", 0);
7376 }
7477 else {
7578 RETVAL = NEWSV(0, in_data_len);
7679 SvPOK_only(RETVAL);
7780 SvCUR_set(RETVAL, in_data_len);
78 out_data = (unsigned char *)SvPV_nolen(RETVAL);
81 out_data = (unsigned char *)SvPVX(RETVAL);
7982
8083 if (self->direction == 1) {
8184 rv = ctr_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state);
82 if (rv != CRYPT_OK) croak("FATAL: ctr_encrypt failed: %s", error_to_string(rv));
85 if (rv != CRYPT_OK) {
86 SvREFCNT_dec(RETVAL);
87 croak("FATAL: ctr_encrypt failed: %s", error_to_string(rv));
88 }
8389 }
8490 else if (self->direction == -1) {
8591 rv = ctr_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state);
86 if (rv != CRYPT_OK) croak("FATAL: ctr_decrypt failed: %s", error_to_string(rv));
92 if (rv != CRYPT_OK) {
93 SvREFCNT_dec(RETVAL);
94 croak("FATAL: ctr_decrypt failed: %s", error_to_string(rv));
95 }
8796 }
8897 else {
98 SvREFCNT_dec(RETVAL);
8999 croak("FATAL: ctr_crypt failed: call start_encrypt or start_decrypt first");
90100 }
91101 }
1212 RETVAL->direction = 0;
1313 RETVAL->cipher_rounds = rounds;
1414 RETVAL->cipher_id = find_cipher(cipher_name);
15 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
15 if (RETVAL->cipher_id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 }
1619 }
1720 OUTPUT:
1821 RETVAL
6568 in_data_start = 0;
6669 has_tmp_block = 0;
6770 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
68 if (in_data_len==0) {
71 if (in_data_len == 0) {
6972 RETVAL = newSVpvn("", 0);
7073 }
7174 else {
72 if(self->padlen > 0) {
75 if (self->padlen > 0) {
7376 i = (blen - self->padlen);
7477 if (in_data_len >= i) { /* enough data to fill pad */
7578 Copy(in_data, self->pad+self->padlen, i, unsigned char);
9497 in_data_len -= i;
9598 }
9699
97 if (in_data_len>0) {
100 if (in_data_len > 0) {
98101 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
99102 RETVAL = NEWSV(0, i);
100103 SvPOK_only(RETVAL);
101104 SvCUR_set(RETVAL, i);
102 out_data = (unsigned char *)SvPV_nolen(RETVAL);
103
105 out_data = (unsigned char *)SvPVX(RETVAL);
104106 if (has_tmp_block) {
105107 Copy(tmp_block, out_data, blen, unsigned char);
106108 out_data += blen;
107109 }
108110 rv = ecb_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
109 if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
111 if (rv != CRYPT_OK) {
112 SvREFCNT_dec(RETVAL);
113 croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
114 }
110115 } /* in_data_len>0 */
111116 else if (has_tmp_block) {
112117 RETVAL = newSVpvn((char*)tmp_block, blen);
129134 blen = (&self->state)->blocklen;
130135 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
131136
132 if(self->padding_mode == 1) { /* pkcs5|7 padding */
137 if (self->padding_mode == 1) { /* pkcs5|7 padding */
133138 i = blen - self->padlen;
134139 if (i == 0) i = blen;
135140 for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i;
136141 rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state);
137142 if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
138143 }
139 else if(self->padding_mode == 2) { /* oneandzeroes padding */
144 else if (self->padding_mode == 2) { /* oneandzeroes padding */
140145 self->pad[self->padlen] = 0x80;
141146 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
142147 rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state);
168173 in_data_start = 0;
169174 has_tmp_block = 0;
170175 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
171 if (in_data_len==0) {
176 if (in_data_len == 0) {
172177 RETVAL = newSVpvn("", 0);
173178 }
174179 else {
175180
176 if(self->padlen == blen) {
181 if (self->padlen == blen) {
177182 rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state);
178183 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
179184 self->padlen = 0;
180185 has_tmp_block = 1;
181186 } /* padlen == blen */
182 else if(self->padlen > 0) {
187 else if (self->padlen > 0) {
183188 i = (blen - self->padlen); /* remaining bytes in padding buffer */
184189 if (in_data_len >= i) { /* enough data to fill pad */
185190 Copy(in_data, self->pad+self->padlen, i, unsigned char);
200205 }
201206 } /* padlen > 0 */
202207
203 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
208 /* here: a/ padlen == 1..16 && in_data_len == 0; b/ padlen == 0 && in_data_len > 0 */
204209 if (in_data_len>0) {
205210 i = (unsigned long)(in_data_len % blen);
206211 if (i>0) { /* save tail of data into pad */
211216 }
212217
213218 if (in_data_len>0) {
214 if(self->padlen == 0 && self->padding_mode !=0) {
219 if (self->padlen == 0 && self->padding_mode !=0) {
215220 /* in case of padding keep full pad if no more data */
216221 Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char);
217222 self->padlen = blen;
225230 RETVAL = NEWSV(0, i);
226231 SvPOK_only(RETVAL);
227232 SvCUR_set(RETVAL, i);
228 out_data = (unsigned char *)SvPV_nolen(RETVAL);
233 out_data = (unsigned char *)SvPVX(RETVAL);
229234 if (has_tmp_block) {
230235 Copy(tmp_block, out_data, blen, unsigned char);
231236 out_data += blen;
232237 }
233238 rv = ecb_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
234 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
239 if (rv != CRYPT_OK) {
240 SvREFCNT_dec(RETVAL);
241 croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
242 }
235243 }
236244 } /* in_data_len>0 */
237245 else if (has_tmp_block) {
260268 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
261269 rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state);
262270 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
263 if(self->padding_mode == 0) { /* no padding */
271 if (self->padding_mode == 0) { /* no padding */
264272 rv_len = blen;
265273 }
266 else if(self->padding_mode == 1) { /* pkcs5|7 padding */
274 else if (self->padding_mode == 1) { /* pkcs5|7 padding */
267275 i = tmp_block[blen-1];
268276 rv_len = blen - (i>blen ? blen : i);
269277 }
270 else if(self->padding_mode == 2) { /* oneandzeroes padding */
278 else if (self->padding_mode == 2) { /* oneandzeroes padding */
271279 rv_len = blen;
272280 while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--;
273281 if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--;
1010 RETVAL->direction = 0;
1111 RETVAL->cipher_rounds = rounds;
1212 RETVAL->cipher_id = find_cipher(cipher_name);
13 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
13 if (RETVAL->cipher_id == -1) {
14 Safefree(RETVAL);
15 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
16 }
1417 }
1518 OUTPUT:
1619 RETVAL
6366 unsigned char *in_data, *out_data;
6467
6568 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
66 if (in_data_len==0) {
69 if (in_data_len == 0) {
6770 RETVAL = newSVpvn("", 0);
6871 }
6972 else {
7073 RETVAL = NEWSV(0, in_data_len);
7174 SvPOK_only(RETVAL);
7275 SvCUR_set(RETVAL, in_data_len);
73 out_data = (unsigned char *)SvPV_nolen(RETVAL);
76 out_data = (unsigned char *)SvPVX(RETVAL);
7477
7578 if (self->direction == 1) {
7679 rv = ofb_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state);
77 if (rv != CRYPT_OK) croak("FATAL: ofb_encrypt failed: %s", error_to_string(rv));
80 if (rv != CRYPT_OK) {
81 SvREFCNT_dec(RETVAL);
82 croak("FATAL: ofb_encrypt failed: %s", error_to_string(rv));
83 }
7884 }
7985 else if (self->direction == -1) {
8086 rv = ofb_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state);
81 if (rv != CRYPT_OK) croak("FATAL: ofb_decrypt failed: %s", error_to_string(rv));
87 if (rv != CRYPT_OK) {
88 SvREFCNT_dec(RETVAL);
89 croak("FATAL: ofb_decrypt failed: %s", error_to_string(rv));
90 }
8291 }
8392 else {
93 SvREFCNT_dec(RETVAL);
8494 croak("FATAL: ofb_crypt failed: call start_encrypt or start_decrypt first");
8595 }
8696 }
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
1010 RETVAL->pindex = find_prng("chacha20");
11 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
11 if (RETVAL->pindex == -1) {
12 Safefree(RETVAL);
13 croak("FATAL: find_prng('chacha20') failed");
14 }
1215 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
13 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
16 if (rv != CRYPT_OK) {
17 Safefree(RETVAL);
18 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
19 }
1420 }
1521 OUTPUT:
1622 RETVAL
148154 CODE:
149155 if (self->key.type == -1) XSRETURN_UNDEF;
150156 rv_hash = newHV();
151 /* =====> x */
157 /* x */
152158 siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0;
153159 if (siz>10000) {
154160 croak("FATAL: key2hash failed - 'x' too big number");
160166 else{
161167 not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0);
162168 }
163 /* =====> y */
169 /* y */
164170 siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0;
165171 if (siz>10000) {
166172 croak("FATAL: key2hash failed - 'y' too big number");
172178 else{
173179 not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0);
174180 }
175 /* =====> p */
181 /* p */
176182 siz = (self->key.prime) ? mp_unsigned_bin_size(self->key.prime) : 0;
177183 if (siz>10000) {
178184 croak("FATAL: key2hash failed - 'p' too big number");
185191 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
186192 }
187193
188 /* =====> g */
194 /* g */
189195 siz = (self->key.base) ? mp_unsigned_bin_size(self->key.base) : 0;
190196 if (siz>10000) {
191197 croak("FATAL: key2hash failed - 'g' too big number");
197203 else {
198204 not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0);
199205 }
200 /* =====> size */
206 /* size */
201207 not_used = hv_store(rv_hash, "size", 4, newSViv(dh_get_groupsize(&self->key)), 0);
202 /* =====> type */
208 /* type */
203209 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
204210 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
205211 RETVAL = newRV_noinc((SV*)rv_hash);
216222 CODE:
217223 if (self->key.type == -1) XSRETURN_UNDEF;
218224 rv_hash = newHV();
219 /* =====> p */
225 /* p */
220226 siz = (self->key.prime) ? mp_unsigned_bin_size(self->key.prime) : 0;
221227 if (siz>10000) {
222228 croak("FATAL: key2hash failed - 'p' too big number");
229235 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
230236 }
231237
232 /* =====> g */
238 /* g */
233239 siz = (self->key.base) ? mp_unsigned_bin_size(self->key.base) : 0;
234240 if (siz>10000) {
235241 croak("FATAL: key2hash failed - 'g' too big number");
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
1010 RETVAL->pindex = find_prng("chacha20");
11 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
11 if (RETVAL->pindex == -1) {
12 Safefree(RETVAL);
13 croak("FATAL: find_prng('chacha20') failed");
14 }
1215 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
13 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
16 if (rv != CRYPT_OK) {
17 Safefree(RETVAL);
18 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
19 }
1420 }
1521 OUTPUT:
1622 RETVAL
158164 qsize = mp_unsigned_bin_size(self->key.q);
159165 psize = mp_unsigned_bin_size(self->key.p);
160166 rv_hash = newHV();
161 /* =====> g */
167 /* g */
162168 siz = (self->key.g) ? mp_unsigned_bin_size(self->key.g) : 0;
163169 if (siz>10000) {
164170 croak("FATAL: key2hash failed - 'g' too big number");
170176 else{
171177 not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0);
172178 }
173 /* =====> q */
179 /* q */
174180 siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0;
175181 if (siz>10000) {
176182 croak("FATAL: key2hash failed - 'q' too big number");
182188 else{
183189 not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
184190 }
185 /* =====> p */
191 /* p */
186192 siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0;
187193 if (siz>10000) {
188194 croak("FATAL: key2hash failed - 'p' too big number");
194200 else{
195201 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
196202 }
197 /* =====> x */
203 /* x */
198204 siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0;
199205 if (siz>10000) {
200206 croak("FATAL: key2hash failed - 'x' too big number");
206212 else{
207213 not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0);
208214 }
209 /* =====> y */
215 /* y */
210216 siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0;
211217 if (siz>10000) {
212218 croak("FATAL: key2hash failed - 'y' too big number");
218224 else{
219225 not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0);
220226 }
221 /* =====> size */
227 /* size */
222228 not_used = hv_store(rv_hash, "size", 4, newSViv(qsize), 0);
223 /* =====> type */
229 /* type */
224230 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
225231 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
226232 RETVAL = newRV_noinc((SV*)rv_hash);
266272 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
267273
268274 hash_id = find_hash(hash_name);
269 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
275 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
270276 rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
271277 &self->pstate, self->pindex,
272278 hash_id, &self->key);
99 RETVAL->pindex = find_prng("chacha20");
1010 RETVAL->key.type = -1;
1111 ecc_dp_init(&RETVAL->dp);
12 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
12 if (RETVAL->pindex == -1) {
13 Safefree(RETVAL);
14 croak("FATAL: find_prng('chacha20') failed");
15 }
1316 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
14 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
17 if (rv != CRYPT_OK) {
18 Safefree(RETVAL);
19 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
20 }
1521 }
1622 OUTPUT:
1723 RETVAL
107113 if (self->key.type == -1) XSRETURN_UNDEF;
108114 esize = ecc_get_size(&self->key);
109115 rv_hash = newHV();
110 /* =====> k */
116 /* k */
111117 siz = (self->key.k) ? mp_unsigned_bin_size(self->key.k) : 0;
112118 if (siz>10000) {
113119 croak("FATAL: key2hash failed - 'k' too big number");
119125 else{
120126 not_used = hv_store(rv_hash, "k", 1, newSVpv("", 0), 0);
121127 }
122 /* =====> pub_x */
128 /* pub_x */
123129 siz = (self->key.pubkey.x) ? mp_unsigned_bin_size(self->key.pubkey.x) : 0;
124130 if (siz>10000) {
125131 croak("FATAL: key2hash failed - 'pub_x' too big number");
131137 else{
132138 not_used = hv_store(rv_hash, "pub_x", 5, newSVpv("", 0), 0);
133139 }
134 /* =====> pub_y */
140 /* pub_y */
135141 siz = (self->key.pubkey.y) ? mp_unsigned_bin_size(self->key.pubkey.y) : 0;
136142 if (siz>10000) {
137143 croak("FATAL: key2hash failed - 'pub_y' too big number");
143149 else{
144150 not_used = hv_store(rv_hash, "pub_y", 5, newSVpv("", 0), 0);
145151 }
146 /* =====> curve_... */
152 /* curve_... */
147153 if (self->key.dp) {
148154 not_used = hv_store(rv_hash, "curve_cofactor", 14, newSViv(self->key.dp->cofactor), 0);
149155 /* prepend leading zero if we have odd number of hexadecimal digits */
194200 not_used = hv_store(rv_hash, "curve_oid", 9, oid, 0);
195201 }
196202 }
197 /* =====> size */
203 /* size */
198204 not_used = hv_store(rv_hash, "size", 4, newSViv(esize), 0);
199 /* =====> type */
205 /* type */
200206 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
201207 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
202208 RETVAL = newRV_noinc((SV*)rv_hash);
283289 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
284290
285291 hash_id = find_hash(hash_name);
286 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
292 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
287293 rv = ecc_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
288294 &self->pstate, self->pindex,
289295 hash_id, &self->key);
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
1010 RETVAL->pindex = find_prng("chacha20");
11 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
11 if (RETVAL->pindex == -1) {
12 Safefree(RETVAL);
13 croak("FATAL: find_prng('chacha20') failed");
14 }
1215 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
13 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
16 if (rv != CRYPT_OK) {
17 Safefree(RETVAL);
18 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
19 }
1420 }
1521 OUTPUT:
1622 RETVAL
154160 if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF;
155161 nsize = mp_unsigned_bin_size(self->key.N);
156162 rv_hash = newHV();
157 /* =====> e */
163 /* e */
158164 siz = (self->key.e) ? mp_unsigned_bin_size(self->key.e) : 0;
159165 if (siz>10000) {
160166 croak("FATAL: key2hash failed - 'e' too big number");
166172 else{
167173 not_used = hv_store(rv_hash, "e", 1, newSVpv("", 0), 0);
168174 }
169 /* =====> d */
175 /* d */
170176 siz = (self->key.d) ? mp_unsigned_bin_size(self->key.d) : 0;
171177 if (siz>10000) {
172178 croak("FATAL: key2hash failed - 'd' too big number");
178184 else{
179185 not_used = hv_store(rv_hash, "d", 1, newSVpv("", 0), 0);
180186 }
181 /* =====> N */
187 /* N */
182188 siz = (self->key.N) ? nsize : 0;
183189 if (siz>10000) {
184190 croak("FATAL: key2hash failed - 'N' too big number");
190196 else{
191197 not_used = hv_store(rv_hash, "N", 1, newSVpv("", 0), 0);
192198 }
193 /* =====> q */
199 /* q */
194200 siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0;
195201 if (siz>10000) {
196202 croak("FATAL: key2hash failed - 'q' too big number");
202208 else{
203209 not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
204210 }
205 /* =====> p */
211 /* p */
206212 siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0;
207213 if (siz>10000) {
208214 croak("FATAL: key2hash failed - 'p' too big number");
214220 else{
215221 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
216222 }
217 /* =====> qP */
223 /* qP */
218224 siz = (self->key.qP) ? mp_unsigned_bin_size(self->key.qP) : 0;
219225 if (siz>10000) {
220226 croak("FATAL: key2hash failed - 'qP' too big number");
226232 else{
227233 not_used = hv_store(rv_hash, "qP", 2, newSVpv("", 0), 0);
228234 }
229 /* =====> dP */
235 /* dP */
230236 siz = (self->key.dP) ? mp_unsigned_bin_size(self->key.dP) : 0;
231237 if (siz>10000) {
232238 croak("FATAL: key2hash failed - 'dP' too big number");
238244 else{
239245 not_used = hv_store(rv_hash, "dP", 2, newSVpv("", 0), 0);
240246 }
241 /* =====> dQ */
247 /* dQ */
242248 siz = (self->key.dQ) ? mp_unsigned_bin_size(self->key.dQ) : 0;
243249 if (siz>10000) {
244250 croak("FATAL: key2hash failed - 'dQ' too big number");
250256 else{
251257 not_used = hv_store(rv_hash, "dQ", 2, newSVpv("", 0), 0);
252258 }
253 /* =====> size */
259 /* size */
254260 not_used = hv_store(rv_hash, "size", 4, newSViv(nsize), 0);
255 /* =====> type */
261 /* type */
256262 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
257263 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
258264 RETVAL = newRV_noinc((SV*)rv_hash);
302308 RETVAL = newSVpvn(NULL, 0); /* undef */
303309 if (strnEQ(padding, "oaep", 4)) {
304310 hash_id = find_hash(oaep_hash);
305 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
311 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
306312 lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len);
307313 rv = rsa_encrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len,
308314 &self->pstate, self->pindex,
347353 RETVAL = newSVpvn(NULL, 0); /* undef */
348354 if (strnEQ(padding, "oaep", 4)) {
349355 hash_id = find_hash(oaep_hash);
350 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
356 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
351357 lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len);
352358 rv = rsa_decrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len,
353359 hash_id, LTC_PKCS_1_OAEP, &stat, &self->key);
390396 RETVAL = newSVpvn(NULL, 0); /* undef */
391397 if (strnEQ(padding, "pss", 3)) {
392398 hash_id = find_hash(hash_name);
393 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
399 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
394400 rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_PSS,
395401 &self->pstate, self->pindex,
396402 hash_id, saltlen, &self->key);
399405 }
400406 else if (strnEQ(padding, "v1.5", 4)) {
401407 hash_id = find_hash(hash_name);
402 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
408 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
403409 rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_V1_5,
404410 &self->pstate, self->pindex,
405411 hash_id, 0, &self->key);
438444 stat = 0;
439445 if (strnEQ(padding, "pss", 3)) {
440446 hash_id = find_hash(hash_name);
441 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
447 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
442448 rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_PSS,
443449 hash_id, saltlen, &stat, &self->key);
444450 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
445451 }
446452 else if (strnEQ(padding, "v1.5", 4)) {
447453 hash_id = find_hash(hash_name);
448 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
454 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
449455 rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_V1_5,
450456 hash_id, 0, &stat, &self->key);
451457 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
1212 if (!RETVAL) croak("FATAL: Newz failed");
1313
1414 id = find_prng(prng_name);
15 if(id==-1) croak("FATAL: find_prng failed for '%s'", prng_name);
15 if (id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_prng failed for '%s'", prng_name);
18 }
1619 RETVAL->id = id;
1720 RETVAL->last_pid = curpid;
1821 RETVAL->desc = &prng_descriptor[id];
1922
2023 rv = RETVAL->desc->start(&RETVAL->state);
21 if (rv != CRYPT_OK) croak("FATAL: PRNG_start failed: %s", error_to_string(rv));
24 if (rv != CRYPT_OK) {
25 Safefree(RETVAL);
26 croak("FATAL: PRNG_start failed: %s", error_to_string(rv));
27 }
2228
23 if(SvOK(entropy)) {
29 if (SvOK(entropy)) {
2430 ent = (unsigned char *) SvPVbyte(entropy, ent_len);
2531 rv = RETVAL->desc->add_entropy(ent, (unsigned long)ent_len, &RETVAL->state);
26 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
32 if (rv != CRYPT_OK) {
33 Safefree(RETVAL);
34 croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
35 }
2736 }
2837 else {
29 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv));
38 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) {
39 Safefree(RETVAL);
40 croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv));
41 }
3042 rv = RETVAL->desc->add_entropy(entropy_buf, 40, &RETVAL->state);
31 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
43 if (rv != CRYPT_OK) {
44 Safefree(RETVAL);
45 croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
46 }
3247 }
3348 rv = RETVAL->desc->ready(&RETVAL->state);
34 if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv));
49 if (rv != CRYPT_OK) {
50 Safefree(RETVAL);
51 croak("FATAL: PRNG_ready failed: %s", error_to_string(rv));
52 }
3553 }
3654 OUTPUT:
3755 RETVAL
4967 {
5068 STRLEN in_len=0;
5169 unsigned char *in_buffer=NULL;
52 unsigned char entropy_buf[32];
70 unsigned char entropy_buf[40];
5371 int rv;
54 if(SvOK(entropy)) {
72 if (SvOK(entropy)) {
5573 in_buffer = (unsigned char *) SvPVbyte(entropy, in_len);
5674 rv = self->desc->add_entropy(in_buffer, (unsigned long)in_len, &self->state);
5775 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
5876 }
5977 else {
60 if (rng_get_bytes(entropy_buf, 32, NULL) != 32) croak("FATAL: rng_get_bytes failed");
61 rv = self->desc->add_entropy(entropy_buf, 32, &self->state);
78 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed");
79 rv = self->desc->add_entropy(entropy_buf, 40, &self->state);
6280 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
6381 }
6482 rv = self->desc->ready(&self->state);
7189 {
7290 int rv_len;
7391 unsigned char *rdata;
74 unsigned char entropy_buf[32];
92 unsigned char entropy_buf[40];
7593
7694 if (self->last_pid != curpid) {
77 rng_get_bytes(entropy_buf, 32, NULL);
78 self->desc->add_entropy(entropy_buf, 32, &self->state);
95 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed");
96 self->desc->add_entropy(entropy_buf, 40, &self->state);
7997 self->desc->ready(&self->state);
8098 self->last_pid = curpid;
8199 }
83101 RETVAL = NEWSV(0, output_len);
84102 SvPOK_only(RETVAL);
85103 SvCUR_set(RETVAL, output_len);
86 rdata = (unsigned char *)SvPV_nolen(RETVAL);
104 rdata = (unsigned char *)SvPVX(RETVAL);
87105 rv_len = (self->desc->read)(rdata, (unsigned long)output_len, &self->state);
88 if ((UV)rv_len != output_len) croak("FATAL: PRNG_read failed");
106 if ((UV)rv_len != output_len) {
107 SvREFCNT_dec(RETVAL);
108 croak("FATAL: PRNG_read failed");
109 }
89110 }
90111 OUTPUT:
91112 RETVAL
96117 {
97118 int i;
98119 unsigned char rdata[4];
99 unsigned char entropy_buf[32];
120 unsigned char entropy_buf[40];
100121
101122 if (self->last_pid != curpid) {
102 rng_get_bytes(entropy_buf, 32, NULL);
103 self->desc->add_entropy(entropy_buf, 32, &self->state);
123 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed");
124 self->desc->add_entropy(entropy_buf, 40, &self->state);
104125 self->desc->ready(&self->state);
105126 self->last_pid = curpid;
106127 }
119140 int i;
120141 unsigned long a, b; /* 32bit is enough */
121142 unsigned char rdata[7]; /* for double we need 53 bits */
122 unsigned char entropy_buf[32];
143 unsigned char entropy_buf[40];
123144 NV limit;
124145
125146 if (self->last_pid != curpid) {
126 rng_get_bytes(entropy_buf, 32, NULL);
127 self->desc->add_entropy(entropy_buf, 32, &self->state);
147 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed");
148 self->desc->add_entropy(entropy_buf, 40, &self->state);
128149 self->desc->ready(&self->state);
129150 self->last_pid = curpid;
130151 }
00 MODULE = CryptX PACKAGE = Crypt::Stream::ChaCha
11
22 Crypt::Stream::ChaCha
3 _new(SV * key, SV * nonce, UV counter = 0, int rounds = 20)
3 new(Class, SV * key, SV * nonce, UV counter = 0, int rounds = 20)
44 CODE:
55 {
66 int rv;
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = chacha_setup(&RETVAL->state, k, (unsigned long)k_len, rounds);
19 if (rv != CRYPT_OK) croak("FATAL: chacha_setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: chacha_setup failed: %s", error_to_string(rv));
22 }
2023
2124 if (iv_len == 12) {
2225 rv = chacha_ivctr32(&RETVAL->state, iv, (unsigned long)iv_len, (ulong32)counter);
23 if (rv != CRYPT_OK) croak("FATAL: chacha_ivctr32 failed: %s", error_to_string(rv));
26 if (rv != CRYPT_OK) {
27 Safefree(RETVAL);
28 croak("FATAL: chacha_ivctr32 failed: %s", error_to_string(rv));
29 }
2430 }
2531 else if (iv_len == 8) {
2632 rv = chacha_ivctr64(&RETVAL->state, iv, (unsigned long)iv_len, (ulong64)counter);
27 if (rv != CRYPT_OK) croak("FATAL: chacha_ivctr64 failed: %s", error_to_string(rv));
33 if (rv != CRYPT_OK) {
34 Safefree(RETVAL);
35 croak("FATAL: chacha_ivctr64 failed: %s", error_to_string(rv));
36 }
2837 }
2938 else {
39 Safefree(RETVAL);
3040 croak("FATAL: chacha IV length must be 8 or 12 bytes");
3141 }
3242 }
5868 RETVAL = NEWSV(0, out_len);
5969 SvPOK_only(RETVAL);
6070 SvCUR_set(RETVAL, out_len);
61 out_data = (unsigned char *)SvPV_nolen(RETVAL);
71 out_data = (unsigned char *)SvPVX(RETVAL);
6272 rv = chacha_keystream(&self->state, out_data, out_len);
63 if (rv != CRYPT_OK) croak("FATAL: chacha_keystream failed: %s", error_to_string(rv));
73 if (rv != CRYPT_OK) {
74 SvREFCNT_dec(RETVAL);
75 croak("FATAL: chacha_keystream failed: %s", error_to_string(rv));
76 }
6477 }
6578 OUTPUT:
6679 RETVAL
7487 unsigned char *in_data, *out_data;
7588
7689 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
77 if (in_data_len==0) {
90 if (in_data_len == 0) {
7891 RETVAL = newSVpvn("", 0);
7992 }
8093 else {
8194 RETVAL = NEWSV(0, in_data_len);
8295 SvPOK_only(RETVAL);
8396 SvCUR_set(RETVAL, in_data_len);
84 out_data = (unsigned char *)SvPV_nolen(RETVAL);
97 out_data = (unsigned char *)SvPVX(RETVAL);
8598 rv = chacha_crypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
86 if (rv != CRYPT_OK) croak("FATAL: chacha_crypt failed: %s", error_to_string(rv));
99 if (rv != CRYPT_OK) {
100 SvREFCNT_dec(RETVAL);
101 croak("FATAL: chacha_crypt failed: %s", error_to_string(rv));
102 }
87103 }
88104 }
89105 OUTPUT:
00 MODULE = CryptX PACKAGE = Crypt::Stream::RC4
11
22 Crypt::Stream::RC4
3 _new(SV * key)
3 new(Class, SV * key)
44 CODE:
55 {
66 int rv;
1414 if (!RETVAL) croak("FATAL: Newz failed");
1515
1616 rv = rc4_stream_setup(&RETVAL->state, k, (unsigned long)k_len);
17 if (rv != CRYPT_OK) croak("FATAL: rc4_stream_setup failed: %s", error_to_string(rv));
17 if (rv != CRYPT_OK) {
18 Safefree(RETVAL);
19 croak("FATAL: rc4_stream_setup failed: %s", error_to_string(rv));
20 }
1821 }
1922 OUTPUT:
2023 RETVAL
4447 RETVAL = NEWSV(0, out_len);
4548 SvPOK_only(RETVAL);
4649 SvCUR_set(RETVAL, out_len);
47 out_data = (unsigned char *)SvPV_nolen(RETVAL);
50 out_data = (unsigned char *)SvPVX(RETVAL);
4851 rv = rc4_stream_keystream(&self->state, out_data, out_len);
49 if (rv != CRYPT_OK) croak("FATAL: rc4_stream_keystream failed: %s", error_to_string(rv));
52 if (rv != CRYPT_OK) {
53 SvREFCNT_dec(RETVAL);
54 croak("FATAL: rc4_stream_keystream failed: %s", error_to_string(rv));
55 }
5056 }
5157 OUTPUT:
5258 RETVAL
6066 unsigned char *in_data, *out_data;
6167
6268 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
63 if (in_data_len==0) {
69 if (in_data_len == 0) {
6470 RETVAL = newSVpvn("", 0);
6571 }
6672 else {
6773 RETVAL = NEWSV(0, in_data_len);
6874 SvPOK_only(RETVAL);
6975 SvCUR_set(RETVAL, in_data_len);
70 out_data = (unsigned char *)SvPV_nolen(RETVAL);
76 out_data = (unsigned char *)SvPVX(RETVAL);
7177 rv = rc4_stream_crypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
72 if (rv != CRYPT_OK) croak("FATAL: rc4_stream_crypt failed: %s", error_to_string(rv));
78 if (rv != CRYPT_OK) {
79 SvREFCNT_dec(RETVAL);
80 croak("FATAL: rc4_stream_crypt failed: %s", error_to_string(rv));
81 }
7382 }
7483 }
7584 OUTPUT:
0 MODULE = CryptX PACKAGE = Crypt::Stream::Salsa20
1
2 Crypt::Stream::Salsa20
3 new(Class, SV * key, SV * nonce, UV counter = 0, int rounds = 20)
4 CODE:
5 {
6 int rv;
7 STRLEN iv_len=0, k_len=0;
8 unsigned char *iv=NULL, *k=NULL;
9
10 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
11 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
12 k = (unsigned char *)SvPVbyte(key, k_len);
13 iv = (unsigned char *)SvPVbyte(nonce, iv_len);
14
15 Newz(0, RETVAL, 1, struct salsa20_struct);
16 if (!RETVAL) croak("FATAL: Newz failed");
17
18 rv = salsa20_setup(&RETVAL->state, k, (unsigned long)k_len, rounds);
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: salsa20_setup failed: %s", error_to_string(rv));
22 }
23
24 rv = salsa20_ivctr64(&RETVAL->state, iv, (unsigned long)iv_len, (ulong64)counter);
25 if (rv != CRYPT_OK) {
26 Safefree(RETVAL);
27 croak("FATAL: salsa20_ivctr64 failed: %s", error_to_string(rv));
28 }
29 }
30 OUTPUT:
31 RETVAL
32
33 void
34 DESTROY(Crypt::Stream::Salsa20 self)
35 CODE:
36 salsa20_done(&self->state);
37 Safefree(self);
38
39 Crypt::Stream::Salsa20
40 clone(Crypt::Stream::Salsa20 self)
41 CODE:
42 Newz(0, RETVAL, 1, struct salsa20_struct);
43 if (!RETVAL) croak("FATAL: Newz failed");
44 Copy(&self->state, &RETVAL->state, 1, struct salsa20_struct);
45 OUTPUT:
46 RETVAL
47
48 SV *
49 keystream(Crypt::Stream::Salsa20 self, STRLEN out_len)
50 CODE:
51 {
52 int rv;
53 unsigned char *out_data;
54
55 RETVAL = NEWSV(0, out_len);
56 SvPOK_only(RETVAL);
57 SvCUR_set(RETVAL, out_len);
58 out_data = (unsigned char *)SvPVX(RETVAL);
59 rv = salsa20_keystream(&self->state, out_data, out_len);
60 if (rv != CRYPT_OK) {
61 SvREFCNT_dec(RETVAL);
62 croak("FATAL: salsa20_keystream failed: %s", error_to_string(rv));
63 }
64 }
65 OUTPUT:
66 RETVAL
67
68 SV *
69 crypt(Crypt::Stream::Salsa20 self, SV * data)
70 CODE:
71 {
72 int rv;
73 STRLEN in_data_len;
74 unsigned char *in_data, *out_data;
75
76 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
77 if (in_data_len == 0) {
78 RETVAL = newSVpvn("", 0);
79 }
80 else {
81 RETVAL = NEWSV(0, in_data_len);
82 SvPOK_only(RETVAL);
83 SvCUR_set(RETVAL, in_data_len);
84 out_data = (unsigned char *)SvPVX(RETVAL);
85 rv = salsa20_crypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
86 if (rv != CRYPT_OK) {
87 SvREFCNT_dec(RETVAL);
88 croak("FATAL: salsa20_crypt failed: %s", error_to_string(rv));
89 }
90 }
91 }
92 OUTPUT:
93 RETVAL
00 MODULE = CryptX PACKAGE = Crypt::Stream::Sober128
11
22 Crypt::Stream::Sober128
3 _new(SV * key, SV * nonce)
3 new(Class, SV * key, SV * nonce)
44 CODE:
55 {
66 int rv;
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = sober128_stream_setup(&RETVAL->state, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: sober128_stream_setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: sober128_stream_setup failed: %s", error_to_string(rv));
22 }
2023
2124 rv = sober128_stream_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
22 if (rv != CRYPT_OK) croak("FATAL: sober128_stream_setiv failed: %s", error_to_string(rv));
25 if (rv != CRYPT_OK) {
26 Safefree(RETVAL);
27 croak("FATAL: sober128_stream_setiv failed: %s", error_to_string(rv));
28 }
2329 }
2430 OUTPUT:
2531 RETVAL
4955 RETVAL = NEWSV(0, out_len);
5056 SvPOK_only(RETVAL);
5157 SvCUR_set(RETVAL, out_len);
52 out_data = (unsigned char *)SvPV_nolen(RETVAL);
58 out_data = (unsigned char *)SvPVX(RETVAL);
5359 rv = sober128_stream_keystream(&self->state, out_data, out_len);
54 if (rv != CRYPT_OK) croak("FATAL: sober128_stream_keystream failed: %s", error_to_string(rv));
60 if (rv != CRYPT_OK) {
61 SvREFCNT_dec(RETVAL);
62 croak("FATAL: sober128_stream_keystream failed: %s", error_to_string(rv));
63 }
5564 }
5665 OUTPUT:
5766 RETVAL
6574 unsigned char *in_data, *out_data;
6675
6776 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
68 if (in_data_len==0) {
77 if (in_data_len == 0) {
6978 RETVAL = newSVpvn("", 0);
7079 }
7180 else {
7281 RETVAL = NEWSV(0, in_data_len);
7382 SvPOK_only(RETVAL);
7483 SvCUR_set(RETVAL, in_data_len);
75 out_data = (unsigned char *)SvPV_nolen(RETVAL);
84 out_data = (unsigned char *)SvPVX(RETVAL);
7685 rv = sober128_stream_crypt(&self->state, in_data, (unsigned long)in_data_len, out_data);
77 if (rv != CRYPT_OK) croak("FATAL: sober128_stream_crypt failed: %s", error_to_string(rv));
86 if (rv != CRYPT_OK) {
87 SvREFCNT_dec(RETVAL);
88 croak("FATAL: sober128_stream_crypt failed: %s", error_to_string(rv));
89 }
7890 }
7991 }
8092 OUTPUT:
+100
-0