- 99E516C7C6B48C92437207404D2605637575722DF4D49F11C35D9B9E90A93C26EE6EB0DD5472EEAF364ECCC49F66F48A6C966FB8DDF3CD1DAE459450D573EFA9
+ C506F05A540F43FF34059CC5398431AA58B1F4F2294C38A1A793B19D9CCDC9B21C21A06577C95623401EF4154009E77301B640FD323FDEA9E3570C1407368BD6
eucrypt/smg_rsa/rsa.c
(69 . 7)(69 . 9)
611 /* mpi_powm can't handle output and input being same */
612 assert (output != input);
613
614 /* the actual rsa op */
615 mpi_powm( output, input, pk->e, pk->n );
616
617 }
618
619 void secret_rsa( MPI output, MPI input, RSA_secret_key *skey ) {
(136 . 3)(138 . 94)
621
622 }
623
624 void rsa_oaep_encrypt( MPI output, MPI input, RSA_public_key *pk) {
625 /* precondition: output is different from input */
626 assert( output != input );
627
628 /* precondition: output has enough memory allocated */
629 unsigned int nlimbs_n = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS);
630 assert( mpi_get_alloced( output ) >= nlimbs_n);
631
632 /* precondition: input is at most max_len_msg octets long */
633 unsigned int nlimbs_msg = mpi_nlimb_hint_from_nbytes( max_len_msg );
634 assert( mpi_get_nlimbs( input ) <= nlimbs_msg);
635
636 /* Step 1: oaep padding */
637 /* get message char array and length */
638 int msglen = 0;
639 int sign;
640 unsigned char * msg = mpi_get_buffer( input, &msglen, &sign);
641 /* allocate memory for result */
642 int encrlen = KEY_LENGTH_OCTETS;
643 unsigned char * encr = xmalloc( encrlen );
644 int entlen = KEY_LENGTH_OCTETS;
645 unsigned char * entropy = xmalloc( entlen );
646 int success = -10;
647 /* call oaep until result is strictly < N of the rsa key to use */
648 MPI oaep = mpi_alloc( nlimbs_n ); /* result of oaep encrypt/pad */
649
650 int nread;
651 do {
652 /* get random bits */
653 do {
654 nread = get_random_octets( entlen, entropy );
655 } while (nread != entlen);
656
657 oaep_encrypt_c( msg, msglen, entropy, entlen, encr, encrlen, &success);
658 if (success > 0) {
659 /* set the obtained oaep to output mpi and compare to N of the rsa key */
660 /* NB: 0-led encr WILL GET TRUNCATED!! */
661 mpi_set_buffer( oaep, encr, encrlen, 0);
662 }
663 printf(".");
664 }
665 while ( success <=0 || mpi_cmp( oaep, pk->n ) >= 0 );
666
667 printf("\n");
668 /* Step2 : call rsa for final result */
669 public_rsa( output, oaep, pk );
670
671 /* clear up */
672 xfree( msg );
673 xfree( encr );
674 xfree( entropy );
675 mpi_free( oaep );
676 }
677
678 void rsa_oaep_decrypt( MPI output, MPI input, RSA_secret_key *sk, int *success)
679 {
680 *success = -1;
681 unsigned int nlimbs_n = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS);
682 unsigned int nlimbs_msg = mpi_nlimb_hint_from_nbytes( max_len_msg );
683
684 /* preconditions */
685 assert( output != input );
686 assert( mpi_get_alloced( output ) >= nlimbs_msg);
687 assert( mpi_get_nlimbs( input ) == nlimbs_n);
688
689 /* rsa */
690 MPI rsa_decr = mpi_alloc( nlimbs_n );
691 secret_rsa( rsa_decr, input, sk );
692
693 /* oaep */
694 unsigned encr_len, decr_len;
695 int sign, flag;
696 char *oaep_encr = mpi_get_buffer( rsa_decr, &encr_len, &sign );
697 char *oaep_decr = xmalloc( encr_len );
698 decr_len = encr_len;
699 oaep_decrypt_c( oaep_encr, encr_len, oaep_decr, &decr_len, &flag );
700
701 /* check status */
702 if ( flag > 0 ) {
703 *success = 1;
704 mpi_set_buffer( output, oaep_decr, decr_len, 0 );
705 }
706 else
707 *success = -1;
708
709 /* cleanup */
710 mpi_free( rsa_decr );
711 xfree( oaep_encr );
712 xfree( oaep_decr );
713 }
714