Package list libcryptx-perl / fd97f1a
XS tuning Karel Miko 3 years ago
2 changed file(s) with 237 addition(s) and 302 deletion(s). Raw diff Collapse all Expand all
421421 if(crypt_mp_init("ltm") != CRYPT_OK) { croak("FATAL: crypt_mp_init failed"); }
422422
423423 SV *
424 CryptX__encode_base64url(SV * in)
425 CODE:
426 {
427 STRLEN in_len;
428 unsigned long out_len;
429 unsigned char *out_data, *in_data;
430
431 if (!SvPOK(in)) XSRETURN_UNDEF;
432 in_data = (unsigned char *) SvPVbyte(in, in_len);
433 if (in_len == 0) {
434 RETVAL = newSVpvn("", 0);
435 }
436 else {
437 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
438 RETVAL = NEWSV(0, out_len); /* avoid zero! */
439 SvPOK_only(RETVAL);
440 out_data = (unsigned char *)SvPVX(RETVAL);
441 if (base64url_encode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
442 SvREFCNT_dec(RETVAL);
443 XSRETURN_UNDEF;
444 }
445 SvCUR_set(RETVAL, out_len);
446 }
447 }
448 OUTPUT:
449 RETVAL
450
451 SV *
452 CryptX__decode_base64url(SV * in)
453 CODE:
454 {
455 STRLEN in_len;
456 unsigned long out_len;
457 unsigned char *out_data, *in_data;
458
459 if (!SvPOK(in)) XSRETURN_UNDEF;
460 in_data = (unsigned char *) SvPVbyte(in, in_len);
461 if (in_len == 0) {
462 RETVAL = newSVpvn("", 0);
463 }
464 else {
465 out_len = (unsigned long)in_len;
466 RETVAL = NEWSV(0, out_len); /* avoid zero! */
467 SvPOK_only(RETVAL);
468 out_data = (unsigned char *)SvPVX(RETVAL);
469 if (base64url_decode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
470 SvREFCNT_dec(RETVAL);
471 XSRETURN_UNDEF;
472 }
473 SvCUR_set(RETVAL, out_len);
474 }
475 }
476 OUTPUT:
477 RETVAL
478
479 SV *
480 CryptX__encode_base64(SV * in)
481 CODE:
482 {
483 STRLEN in_len;
484 unsigned long out_len;
485 unsigned char *out_data, *in_data;
486
487 if (!SvPOK(in)) XSRETURN_UNDEF;
488 in_data = (unsigned char *) SvPVbyte(in, in_len);
489 if (in_len == 0) {
490 RETVAL = newSVpvn("", 0);
491 }
492 else {
493 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
494 RETVAL = NEWSV(0, out_len); /* avoid zero! */
495 SvPOK_only(RETVAL);
496 out_data = (unsigned char *)SvPVX(RETVAL);
497 if (base64_encode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
498 SvREFCNT_dec(RETVAL);
499 XSRETURN_UNDEF;
500 }
501 SvCUR_set(RETVAL, out_len);
502 }
503 }
504 OUTPUT:
505 RETVAL
506
507 SV *
508 CryptX__decode_base64(SV * in)
509 CODE:
510 {
511 STRLEN in_len;
512 unsigned long out_len;
513 unsigned char *out_data, *in_data;
514
515 if (!SvPOK(in)) XSRETURN_UNDEF;
516 in_data = (unsigned char *)SvPVbyte(in, in_len);
517 if (in_len == 0) {
518 RETVAL = newSVpvn("", 0);
519 }
520 else {
521 out_len = (unsigned long)in_len;
522 RETVAL = NEWSV(0, out_len); /* avoid zero! */
523 SvPOK_only(RETVAL);
524 out_data = (unsigned char *)SvPVX(RETVAL);
525 if (base64_decode(in_data, (unsigned long)in_len, out_data, &out_len) != CRYPT_OK) {
526 SvREFCNT_dec(RETVAL);
527 XSRETURN_UNDEF;
528 }
529 SvCUR_set(RETVAL, out_len);
530 }
531 }
532 OUTPUT:
533 RETVAL
534
535 SV *
536 CryptX__encode_b32(SV *in, unsigned idx)
537 CODE:
538 {
539 STRLEN in_len;
540 unsigned long out_len;
541 unsigned char *out_data, *in_data;
542 int id = -1;
543
544 if (!SvPOK(in)) XSRETURN_UNDEF;
545 if (idx == 0) id = BASE32_RFC4648;
546 if (idx == 1) id = BASE32_BASE32HEX;
547 if (idx == 2) id = BASE32_ZBASE32;
548 if (idx == 3) id = BASE32_CROCKFORD;
549 if (id == -1) XSRETURN_UNDEF;
550 in_data = (unsigned char *) SvPVbyte(in, in_len);
551 if (in_len == 0) {
552 RETVAL = newSVpvn("", 0);
553 }
554 else {
555 out_len = (unsigned long)((8 * in_len + 4) / 5);
556 RETVAL = NEWSV(0, out_len); /* avoid zero! */
557 SvPOK_only(RETVAL);
558 out_data = (unsigned char *)SvPVX(RETVAL);
559 if (base32_encode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
560 SvREFCNT_dec(RETVAL);
561 XSRETURN_UNDEF;
562 }
563 SvCUR_set(RETVAL, out_len);
564 }
565 }
566 OUTPUT:
567 RETVAL
568
569 SV *
570 CryptX__decode_b32(SV *in, unsigned idx)
571 CODE:
572 {
573 STRLEN in_len;
574 unsigned long out_len;
575 unsigned char *out_data, *in_data;
576 int id = -1;
577
578 if (!SvPOK(in)) XSRETURN_UNDEF;
579 if (idx == 0) id = BASE32_RFC4648;
580 if (idx == 1) id = BASE32_BASE32HEX;
581 if (idx == 2) id = BASE32_ZBASE32;
582 if (idx == 3) id = BASE32_CROCKFORD;
583 if (id == -1) XSRETURN_UNDEF;
584 in_data = (unsigned char *)SvPVbyte(in, in_len);
585 if (in_len == 0) {
586 RETVAL = newSVpvn("", 0);
587 }
588 else {
589 out_len = (unsigned long)in_len;
590 RETVAL = NEWSV(0, out_len); /* avoid zero! */
591 SvPOK_only(RETVAL);
592 out_data = (unsigned char *)SvPVX(RETVAL);
593 if (base32_decode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
594 SvREFCNT_dec(RETVAL);
595 XSRETURN_UNDEF;
596 }
597 SvCUR_set(RETVAL, out_len);
598 }
599 }
600 OUTPUT:
601 RETVAL
602
603 SV *
604 CryptX__increment_octets_le(SV * in)
605 CODE:
606 {
607 STRLEN len, i = 0;
608 unsigned char *out_data, *in_data;
609
610 if (!SvPOK(in)) XSRETURN_UNDEF;
611 in_data = (unsigned char *)SvPVbyte(in, len);
612 if (len == 0) {
613 RETVAL = newSVpvn("", 0);
614 }
615 else {
616 RETVAL = NEWSV(0, len); /* avoid zero! */
617 SvPOK_only(RETVAL);
618 SvCUR_set(RETVAL, len);
619 out_data = (unsigned char *)SvPVX(RETVAL);
620 Copy(in_data, out_data, len, unsigned char);
621 while (i < len) {
622 out_data[i]++;
623 if (0 != out_data[i]) break;
624 i++;
625 }
626 if (i == len) {
627 SvREFCNT_dec(RETVAL);
628 croak("FATAL: increment_octets_le overflow");
629 }
630 }
631 }
632 OUTPUT:
633 RETVAL
634
635 SV *
636 CryptX__increment_octets_be(SV * in)
637 CODE:
638 {
639 STRLEN len, i = 0;
640 unsigned char *out_data, *in_data;
641
642 if (!SvPOK(in)) XSRETURN_UNDEF;
643 in_data = (unsigned char *)SvPVbyte(in, len);
644 if (len == 0) {
645 RETVAL = newSVpvn("", 0);
646 }
647 else {
648 RETVAL = NEWSV(0, len); /* avoid zero! */
649 SvPOK_only(RETVAL);
650 SvCUR_set(RETVAL, len);
651 out_data = (unsigned char *)SvPVX(RETVAL);
652 Copy(in_data, out_data, len, unsigned char);
653 while (i < len) {
654 out_data[len - 1 - i]++;
655 if (0 != out_data[len - 1 - i]) break;
656 i++;
657 }
658 if (i == len) {
659 SvREFCNT_dec(RETVAL);
660 croak("FATAL: increment_octets_be overflow");
661 }
662 }
663 }
664 OUTPUT:
665 RETVAL
666
667 SV *
668 CryptX__radix_to_bin(char *in, int radix)
424 CryptX__ltc_build_settings()
425 CODE:
426 RETVAL = newSVpv(crypt_build_settings, 0);
427 OUTPUT:
428 RETVAL
429
430 SV *
431 CryptX__ltc_mp_name()
432 CODE:
433 RETVAL = newSVpv(ltc_mp.name, 0);
434 OUTPUT:
435 RETVAL
436
437 int
438 CryptX__ltc_mp_bits_per_digit()
439 CODE:
440 RETVAL = ltc_mp.bits_per_digit;
441 OUTPUT:
442 RETVAL
443
444 MODULE = CryptX PACKAGE = Crypt::Misc
445
446 PROTOTYPES: DISABLE
447
448 SV *
449 _radix_to_bin(char *in, int radix)
669450 CODE:
670451 {
671452 STRLEN len;
699480 RETVAL
700481
701482 SV *
702 CryptX__bin_to_radix(SV *in, int radix)
483 _bin_to_radix(SV *in, int radix)
703484 CODE:
704485 {
705486 STRLEN len;
743524 RETVAL
744525
745526 SV *
746 CryptX__ltc_build_settings()
747 CODE:
748 RETVAL = newSVpv(crypt_build_settings, 0);
749 OUTPUT:
750 RETVAL
751
752 SV *
753 CryptX__ltc_mp_name()
754 CODE:
755 RETVAL = newSVpv(ltc_mp.name, 0);
756 OUTPUT:
757 RETVAL
758
759 int
760 CryptX__ltc_mp_bits_per_digit()
761 CODE:
762 RETVAL = ltc_mp.bits_per_digit;
527 encode_b64(SV * in)
528 ALIAS:
529 encode_b64u = 1
530 CODE:
531 {
532 int rv;
533 STRLEN in_len;
534 unsigned long out_len;
535 unsigned char *out_data, *in_data;
536
537 if (!SvPOK(in)) XSRETURN_UNDEF;
538 in_data = (unsigned char *) SvPVbyte(in, in_len);
539 if (in_len == 0) {
540 RETVAL = newSVpvn("", 0);
541 }
542 else {
543 out_len = (unsigned long)(4 * ((in_len + 2) / 3) + 1);
544 RETVAL = NEWSV(0, out_len); /* avoid zero! */
545 SvPOK_only(RETVAL);
546 out_data = (unsigned char *)SvPVX(RETVAL);
547 if (ix == 1)
548 rv = base64url_encode(in_data, (unsigned long)in_len, out_data, &out_len);
549 else
550 rv = base64_encode(in_data, (unsigned long)in_len, out_data, &out_len);
551 if (rv != CRYPT_OK) {
552 SvREFCNT_dec(RETVAL);
553 XSRETURN_UNDEF;
554 }
555 SvCUR_set(RETVAL, out_len);
556 }
557 }
558 OUTPUT:
559 RETVAL
560
561 SV *
562 decode_b64(SV * in)
563 ALIAS:
564 decode_b64u = 1
565 CODE:
566 {
567 int rv;
568 STRLEN in_len;
569 unsigned long out_len;
570 unsigned char *out_data, *in_data;
571
572 if (!SvPOK(in)) XSRETURN_UNDEF;
573 in_data = (unsigned char *)SvPVbyte(in, in_len);
574 if (in_len == 0) {
575 RETVAL = newSVpvn("", 0);
576 }
577 else {
578 out_len = (unsigned long)in_len;
579 RETVAL = NEWSV(0, out_len); /* avoid zero! */
580 SvPOK_only(RETVAL);
581 out_data = (unsigned char *)SvPVX(RETVAL);
582 if (ix == 1)
583 rv = base64url_decode(in_data, (unsigned long)in_len, out_data, &out_len);
584 else
585 rv = base64_decode(in_data, (unsigned long)in_len, out_data, &out_len);
586 if (rv != CRYPT_OK) {
587 SvREFCNT_dec(RETVAL);
588 XSRETURN_UNDEF;
589 }
590 SvCUR_set(RETVAL, out_len);
591 }
592 }
593 OUTPUT:
594 RETVAL
595
596 SV *
597 encode_b32r(SV *in)
598 ALIAS:
599 encode_b32b = 1
600 encode_b32z = 2
601 encode_b32c = 3
602 CODE:
603 {
604 STRLEN in_len;
605 unsigned long out_len;
606 unsigned char *out_data, *in_data;
607 int id = -1;
608
609 if (!SvPOK(in)) XSRETURN_UNDEF;
610 if (ix == 0) id = BASE32_RFC4648;
611 if (ix == 1) id = BASE32_BASE32HEX;
612 if (ix == 2) id = BASE32_ZBASE32;
613 if (ix == 3) id = BASE32_CROCKFORD;
614 if (id == -1) XSRETURN_UNDEF;
615 in_data = (unsigned char *) SvPVbyte(in, in_len);
616 if (in_len == 0) {
617 RETVAL = newSVpvn("", 0);
618 }
619 else {
620 out_len = (unsigned long)((8 * in_len + 4) / 5);
621 RETVAL = NEWSV(0, out_len); /* avoid zero! */
622 SvPOK_only(RETVAL);
623 out_data = (unsigned char *)SvPVX(RETVAL);
624 if (base32_encode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
625 SvREFCNT_dec(RETVAL);
626 XSRETURN_UNDEF;
627 }
628 SvCUR_set(RETVAL, out_len);
629 }
630 }
631 OUTPUT:
632 RETVAL
633
634 SV *
635 decode_b32r(SV *in)
636 ALIAS:
637 decode_b32b = 1
638 decode_b32z = 2
639 decode_b32c = 3
640 CODE:
641 {
642 int rv;
643 STRLEN in_len;
644 unsigned long out_len;
645 unsigned char *out_data, *in_data;
646 int id = -1;
647
648 if (!SvPOK(in)) XSRETURN_UNDEF;
649 if (ix == 0) id = BASE32_RFC4648;
650 if (ix == 1) id = BASE32_BASE32HEX;
651 if (ix == 2) id = BASE32_ZBASE32;
652 if (ix == 3) id = BASE32_CROCKFORD;
653 if (id == -1) XSRETURN_UNDEF;
654 in_data = (unsigned char *)SvPVbyte(in, in_len);
655 if (in_len == 0) {
656 RETVAL = newSVpvn("", 0);
657 }
658 else {
659 out_len = (unsigned long)in_len;
660 RETVAL = NEWSV(0, out_len); /* avoid zero! */
661 SvPOK_only(RETVAL);
662 out_data = (unsigned char *)SvPVX(RETVAL);
663 if (base32_decode(in_data, (unsigned long)in_len, out_data, &out_len, id) != CRYPT_OK) {
664 SvREFCNT_dec(RETVAL);
665 XSRETURN_UNDEF;
666 }
667 SvCUR_set(RETVAL, out_len);
668 }
669 }
670 OUTPUT:
671 RETVAL
672
673 SV *
674 increment_octets_le(SV * in)
675 CODE:
676 {
677 STRLEN len, i = 0;
678 unsigned char *out_data, *in_data;
679
680 if (!SvPOK(in)) XSRETURN_UNDEF;
681 in_data = (unsigned char *)SvPVbyte(in, len);
682 if (len == 0) {
683 RETVAL = newSVpvn("", 0);
684 }
685 else {
686 RETVAL = NEWSV(0, len); /* avoid zero! */
687 SvPOK_only(RETVAL);
688 SvCUR_set(RETVAL, len);
689 out_data = (unsigned char *)SvPVX(RETVAL);
690 Copy(in_data, out_data, len, unsigned char);
691 while (i < len) {
692 out_data[i]++;
693 if (0 != out_data[i]) break;
694 i++;
695 }
696 if (i == len) {
697 SvREFCNT_dec(RETVAL);
698 croak("FATAL: increment_octets_le overflow");
699 }
700 }
701 }
702 OUTPUT:
703 RETVAL
704
705 SV *
706 increment_octets_be(SV * in)
707 CODE:
708 {
709 STRLEN len, i = 0;
710 unsigned char *out_data, *in_data;
711
712 if (!SvPOK(in)) XSRETURN_UNDEF;
713 in_data = (unsigned char *)SvPVbyte(in, len);
714 if (len == 0) {
715 RETVAL = newSVpvn("", 0);
716 }
717 else {
718 RETVAL = NEWSV(0, len); /* avoid zero! */
719 SvPOK_only(RETVAL);
720 SvCUR_set(RETVAL, len);
721 out_data = (unsigned char *)SvPVX(RETVAL);
722 Copy(in_data, out_data, len, unsigned char);
723 while (i < len) {
724 out_data[len - 1 - i]++;
725 if (0 != out_data[len - 1 - i]) break;
726 i++;
727 }
728 if (i == len) {
729 SvREFCNT_dec(RETVAL);
730 croak("FATAL: increment_octets_be overflow");
731 }
732 }
733 }
763734 OUTPUT:
764735 RETVAL
765736
3434 use Crypt::Cipher;
3535 use Crypt::PRNG 'random_bytes';
3636
37 sub encode_b64 {
38 CryptX::_encode_base64(@_);
39 }
40
41 sub decode_b64 {
42 CryptX::_decode_base64(@_);
43 }
44
45 sub encode_b64u {
46 CryptX::_encode_base64url(@_);
47 }
48
49 sub decode_b64u {
50 CryptX::_decode_base64url(@_);
51 }
52
5337 sub _encode_b58 {
5438 my ($bytes, $alphabet) = @_;
5539
6044 if ($bytes =~ /^(\x00+)/) {
6145 $base58 = ('0' x length($1));
6246 }
63 $base58 .= CryptX::_bin_to_radix($bytes, 58);
47 $base58 .= _bin_to_radix($bytes, 58);
6448
6549 if (defined $alphabet) {
6650 my $default = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
9175 $base58 = $2;
9276 $bytes = ("\x00" x length($1));
9377 }
94 $bytes .= CryptX::_radix_to_bin($base58, 58) if defined $base58 && length($base58) > 0;
78 $bytes .= _radix_to_bin($base58, 58) if defined $base58 && length($base58) > 0;
9579
9680 return $bytes;
9781 }
10791 sub encode_b58r { _encode_b58(shift, "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz") } # Ripple
10892 sub encode_b58t { _encode_b58(shift, "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz") } # Tipple
10993 sub encode_b58s { _encode_b58(shift, "gsphnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCr65jkm8oFqi1tuvAxyz") } # Stellar
110
111 sub encode_b32r { CryptX::_encode_b32(shift, 0) } # rfc4648
112 sub encode_b32b { CryptX::_encode_b32(shift, 1) } # base32hex
113 sub encode_b32z { CryptX::_encode_b32(shift, 2) } # zbase32
114 sub encode_b32c { CryptX::_encode_b32(shift, 3) } # crockford
115
116 sub decode_b32r { CryptX::_decode_b32(shift, 0) } # rfc4648
117 sub decode_b32b { CryptX::_decode_b32(shift, 1) } # base32hex
118 sub decode_b32z { CryptX::_decode_b32(shift, 2) } # zbase32
119 sub decode_b32c { CryptX::_decode_b32(shift, 3) } # crockford
120
121
122 sub increment_octets_be {
123 CryptX::_increment_octets_be(@_);
124 #$_[0] = CryptX::_increment_octets_be($_[0]);
125 }
126
127 sub increment_octets_le {
128 CryptX::_increment_octets_le(@_);
129 #$_[0] = CryptX::_increment_octets_le($_[0]);
130 }
13194
13295 sub pem_to_der {
13396 my ($data, $password) = @_;
193156 }
194157
195158 sub read_rawfile {
159 # $data = read_rawfile($filename);
196160 my $f = shift;
197161 croak "FATAL: read_rawfile() non-existing file '$f'" unless -f $f;
198162 open my $fh, "<", $f or croak "FATAL: read_rawfile() cannot open file '$f': $!";