From: <iro...@us...> - 2004-02-18 22:14:23
|
Update of /cvsroot/perl-openssl/Crypt/OpenSSL/RSA In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20797 Modified Files: RSA.xs Log Message: factor out some common code Index: RSA.xs =================================================================== RCS file: /cvsroot/perl-openssl/Crypt/OpenSSL/RSA/RSA.xs,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** RSA.xs 18 Feb 2004 21:37:32 -0000 1.47 --- RSA.xs 18 Feb 2004 22:04:28 -0000 1.48 *************** *** 25,28 **** --- 25,33 ---- __FILE__, __LINE__, ERR_reason_error_string(ERR_get_error())); + #define PACKAGE_CROAK(p_message) croak("%s:%d: %s", (p_message)) + #define CHECK_NEW(p_var, p_size, p_type) \ + if (New(0, p_var, p_size, p_type) == NULL) \ + { PACKAGE_CROAK("unable to alloc buffer"); } + char is_private(rsaData* p_rsa) { *************** *** 36,43 **** SV* rsaSvRef; ! if (New(0, rsa, 1, rsaData) == NULL) ! { ! croak("unable to allocate memory"); ! } rsa->rsa = p_rsa; rsa->hashMode = NID_sha1; --- 41,45 ---- SV* rsaSvRef; ! CHECK_NEW(rsa, 1, rsaData) rsa->rsa = p_rsa; rsa->hashMode = NID_sha1; *************** *** 73,81 **** text = SvPV(text_SV, text_length); ! if (New(0, message_digest, get_digest_length(hash_method), char) == NULL) ! { ! croak("unable to allocate buffer for message digest in package " ! PACKAGE_NAME); ! } switch(hash_method) --- 75,79 ---- text = SvPV(text_SV, text_length); ! CHECK_NEW(message_digest, get_digest_length(hash_method), char) switch(hash_method) *************** *** 161,164 **** --- 159,188 ---- } + SV* rsa_crypt(rsaData* p_rsa, SV* p_from, + int (*p_crypt)(int, unsigned char*, unsigned char*, RSA*, int)) + { + int from_length, to_length; + int size; + unsigned char* from; + unsigned char* to; + SV* sv; + + from = SvPV(p_from, from_length); + size = RSA_size(p_rsa->rsa); + CHECK_NEW(to, size, char) + + to_length = p_crypt(from_length, from, to, p_rsa->rsa, p_rsa->padding); + + if (to_length < 0) + { + Safefree(to); + CHECK_OPEN_SSL(0) + } + sv = newSVpv(to, to_length); + Safefree(to); + return sv; + } + + MODULE = Crypt::OpenSSL::RSA PACKAGE = Crypt::OpenSSL::RSA PROTOTYPES: DISABLE *************** *** 354,513 **** SV* ! encrypt(p_rsa, plaintext_SV) rsaData* p_rsa; ! SV* plaintext_SV; ! PREINIT: ! int plaintext_length; ! unsigned char* plaintext; ! unsigned char* ciphertext; ! size_t size; ! int ciphertext_length; ! RSA* rsa; CODE: ! plaintext = SvPV(plaintext_SV, plaintext_length); ! ! rsa = p_rsa->rsa; ! ! size = RSA_size(rsa); ! if (New(0,ciphertext, size, char) == NULL) ! { ! croak("unable to allocate buffer for ciphertext in package " ! PACKAGE_NAME); ! } ! ! ciphertext_length = RSA_public_encrypt( ! plaintext_length, plaintext, ciphertext, rsa, p_rsa->padding); ! ! if (ciphertext_length < 0) ! { ! Safefree(ciphertext); ! CHECK_OPEN_SSL(0) ! } ! ! RETVAL = newSVpv(ciphertext, size); OUTPUT: RETVAL SV* ! decrypt(p_rsa, ciphertext_SV) rsaData* p_rsa; ! SV* ciphertext_SV; ! PREINIT: ! int ciphertext_length; /* Needed to pass to SvPV */ ! int plaintext_length; ! char* plaintext; ! char* ciphertext; ! unsigned long size; ! RSA* rsa; CODE: ! { ! if (! is_private(p_rsa)) ! { ! croak("Public keys cannot decrypt messages."); ! } ! ! ciphertext = SvPV(ciphertext_SV, ciphertext_length); ! ! rsa = p_rsa->rsa; ! size = RSA_size(rsa); ! if (New(0, plaintext, size, char) == NULL) ! { ! croak("unable to allocate buffer for plaintext in package " ! PACKAGE_NAME); ! } ! ! plaintext_length = RSA_private_decrypt( ! size, ciphertext, plaintext, rsa, p_rsa->padding); ! ! if (plaintext_length < 0) ! { ! Safefree(plaintext); ! CHECK_OPEN_SSL(0) ! } ! ! RETVAL = newSVpv(plaintext, plaintext_length); ! Safefree(plaintext); ! } OUTPUT: RETVAL SV* ! private_encrypt(p_rsa, plaintext_SV, ...) rsaData* p_rsa; ! SV* plaintext_SV; ! PREINIT: ! int plaintext_length; ! unsigned char* plaintext; ! unsigned char* ciphertext; ! size_t size; ! int ciphertext_length; ! RSA* rsa; ! CODE: ! if (! is_private(p_rsa)) ! { ! croak("Public keys cannot private_encrypt messages."); ! } ! ! plaintext = SvPV(plaintext_SV, plaintext_length); ! ! rsa = p_rsa->rsa; ! ! size = RSA_size(rsa); ! if (New(0,ciphertext, size, char) == NULL) ! { ! croak ("unable to allocate buffer for ciphertext in package " ! PACKAGE_NAME); ! } ! ! ciphertext_length = RSA_private_encrypt( ! plaintext_length, plaintext, ciphertext, rsa, p_rsa->padding); ! ! if (ciphertext_length < 0) ! { ! Safefree(ciphertext); ! CHECK_OPEN_SSL(0) ! } ! ! RETVAL = newSVpv(ciphertext, size); OUTPUT: RETVAL - SV* ! public_decrypt(p_rsa, ciphertext_SV) rsaData* p_rsa; ! SV* ciphertext_SV; ! PREINIT: ! int ciphertext_length; /* Needed to pass to SvPV */ ! int plaintext_length; ! char* plaintext; ! char* ciphertext; ! unsigned long size; ! RSA* rsa; CODE: ! ! ciphertext = SvPV(ciphertext_SV, ciphertext_length); ! ! rsa = p_rsa->rsa; ! size = RSA_size(rsa); ! if (New(0, plaintext, size, char) == NULL) ! { ! croak("unable to allocate buffer for plaintext in package " ! PACKAGE_NAME); ! } ! ! plaintext_length = RSA_public_decrypt( ! size, ciphertext, plaintext, rsa, p_rsa->padding); ! if (plaintext_length < 0) ! { ! Safefree(plaintext); ! CHECK_OPEN_SSL(0) ! } ! ! RETVAL = newSVpv(plaintext, plaintext_length); ! Safefree(plaintext); OUTPUT: ! RETVAL int --- 378,415 ---- SV* ! encrypt(p_rsa, p_plaintext) rsaData* p_rsa; ! SV* p_plaintext; CODE: ! RETVAL = rsa_crypt(p_rsa, p_plaintext, RSA_public_encrypt); OUTPUT: RETVAL SV* ! decrypt(p_rsa, p_ciphertext) rsaData* p_rsa; ! SV* p_ciphertext; CODE: ! RETVAL = rsa_crypt(p_rsa, p_ciphertext, RSA_private_decrypt); OUTPUT: RETVAL SV* ! private_encrypt(p_rsa, p_plaintext) rsaData* p_rsa; ! SV* p_plaintext; CODE: ! RETVAL = rsa_crypt(p_rsa, p_plaintext, RSA_private_encrypt); OUTPUT: RETVAL SV* ! public_decrypt(p_rsa, p_ciphertext) rsaData* p_rsa; ! SV* p_ciphertext; CODE: ! RETVAL = rsa_crypt(p_rsa, p_ciphertext, RSA_public_decrypt); OUTPUT: ! RETVAL int *************** *** 594,598 **** rsaData* p_rsa; CODE: ! p_rsa->padding = RSA_SSLV23_PADDING; SV* --- 496,500 ---- rsaData* p_rsa; CODE: ! p_rsa->padding = RSA_SSLV23_PADDING; SV* *************** *** 613,621 **** rsa = p_rsa->rsa; ! if (New(0, signature, RSA_size(rsa), char) == NULL) ! { ! croak("unable to allocate buffer for ciphertext in package " ! PACKAGE_NAME); ! } digest = get_message_digest(text_SV, p_rsa->hashMode); --- 515,519 ---- rsa = p_rsa->rsa; ! CHECK_NEW(signature, RSA_size(rsa), char) digest = get_message_digest(text_SV, p_rsa->hashMode); |