Lines 414-551
Link Here
|
414 |
|
414 |
|
415 |
/* |
415 |
/* |
416 |
* call-seq: |
416 |
* call-seq: |
417 |
* rsa.public_encrypt(string) => String |
|
|
418 |
* rsa.public_encrypt(string, padding) => String |
419 |
* |
420 |
* Encrypt _string_ with the public key. _padding_ defaults to PKCS1_PADDING. |
421 |
* The encrypted string output can be decrypted using #private_decrypt. |
422 |
*/ |
423 |
static VALUE |
424 |
ossl_rsa_public_encrypt(int argc, VALUE *argv, VALUE self) |
425 |
{ |
426 |
RSA *rsa; |
427 |
const BIGNUM *rsa_n; |
428 |
int buf_len, pad; |
429 |
VALUE str, buffer, padding; |
430 |
|
431 |
GetRSA(self, rsa); |
432 |
RSA_get0_key(rsa, &rsa_n, NULL, NULL); |
433 |
if (!rsa_n) |
434 |
ossl_raise(eRSAError, "incomplete RSA"); |
435 |
rb_scan_args(argc, argv, "11", &buffer, &padding); |
436 |
pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding); |
437 |
StringValue(buffer); |
438 |
str = rb_str_new(0, RSA_size(rsa)); |
439 |
buf_len = RSA_public_encrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer), |
440 |
(unsigned char *)RSTRING_PTR(str), rsa, pad); |
441 |
if (buf_len < 0) ossl_raise(eRSAError, NULL); |
442 |
rb_str_set_len(str, buf_len); |
443 |
|
444 |
return str; |
445 |
} |
446 |
|
447 |
/* |
448 |
* call-seq: |
449 |
* rsa.public_decrypt(string) => String |
450 |
* rsa.public_decrypt(string, padding) => String |
451 |
* |
452 |
* Decrypt _string_, which has been encrypted with the private key, with the |
453 |
* public key. _padding_ defaults to PKCS1_PADDING. |
454 |
*/ |
455 |
static VALUE |
456 |
ossl_rsa_public_decrypt(int argc, VALUE *argv, VALUE self) |
457 |
{ |
458 |
RSA *rsa; |
459 |
const BIGNUM *rsa_n; |
460 |
int buf_len, pad; |
461 |
VALUE str, buffer, padding; |
462 |
|
463 |
GetRSA(self, rsa); |
464 |
RSA_get0_key(rsa, &rsa_n, NULL, NULL); |
465 |
if (!rsa_n) |
466 |
ossl_raise(eRSAError, "incomplete RSA"); |
467 |
rb_scan_args(argc, argv, "11", &buffer, &padding); |
468 |
pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding); |
469 |
StringValue(buffer); |
470 |
str = rb_str_new(0, RSA_size(rsa)); |
471 |
buf_len = RSA_public_decrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer), |
472 |
(unsigned char *)RSTRING_PTR(str), rsa, pad); |
473 |
if (buf_len < 0) ossl_raise(eRSAError, NULL); |
474 |
rb_str_set_len(str, buf_len); |
475 |
|
476 |
return str; |
477 |
} |
478 |
|
479 |
/* |
480 |
* call-seq: |
481 |
* rsa.private_encrypt(string) => String |
482 |
* rsa.private_encrypt(string, padding) => String |
483 |
* |
484 |
* Encrypt _string_ with the private key. _padding_ defaults to PKCS1_PADDING. |
485 |
* The encrypted string output can be decrypted using #public_decrypt. |
486 |
*/ |
487 |
static VALUE |
488 |
ossl_rsa_private_encrypt(int argc, VALUE *argv, VALUE self) |
489 |
{ |
490 |
RSA *rsa; |
491 |
const BIGNUM *rsa_n; |
492 |
int buf_len, pad; |
493 |
VALUE str, buffer, padding; |
494 |
|
495 |
GetRSA(self, rsa); |
496 |
RSA_get0_key(rsa, &rsa_n, NULL, NULL); |
497 |
if (!rsa_n) |
498 |
ossl_raise(eRSAError, "incomplete RSA"); |
499 |
if (!RSA_PRIVATE(self, rsa)) |
500 |
ossl_raise(eRSAError, "private key needed."); |
501 |
rb_scan_args(argc, argv, "11", &buffer, &padding); |
502 |
pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding); |
503 |
StringValue(buffer); |
504 |
str = rb_str_new(0, RSA_size(rsa)); |
505 |
buf_len = RSA_private_encrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer), |
506 |
(unsigned char *)RSTRING_PTR(str), rsa, pad); |
507 |
if (buf_len < 0) ossl_raise(eRSAError, NULL); |
508 |
rb_str_set_len(str, buf_len); |
509 |
|
510 |
return str; |
511 |
} |
512 |
|
513 |
/* |
514 |
* call-seq: |
515 |
* rsa.private_decrypt(string) => String |
516 |
* rsa.private_decrypt(string, padding) => String |
517 |
* |
518 |
* Decrypt _string_, which has been encrypted with the public key, with the |
519 |
* private key. _padding_ defaults to PKCS1_PADDING. |
520 |
*/ |
521 |
static VALUE |
522 |
ossl_rsa_private_decrypt(int argc, VALUE *argv, VALUE self) |
523 |
{ |
524 |
RSA *rsa; |
525 |
const BIGNUM *rsa_n; |
526 |
int buf_len, pad; |
527 |
VALUE str, buffer, padding; |
528 |
|
529 |
GetRSA(self, rsa); |
530 |
RSA_get0_key(rsa, &rsa_n, NULL, NULL); |
531 |
if (!rsa_n) |
532 |
ossl_raise(eRSAError, "incomplete RSA"); |
533 |
if (!RSA_PRIVATE(self, rsa)) |
534 |
ossl_raise(eRSAError, "private key needed."); |
535 |
rb_scan_args(argc, argv, "11", &buffer, &padding); |
536 |
pad = (argc == 1) ? RSA_PKCS1_PADDING : NUM2INT(padding); |
537 |
StringValue(buffer); |
538 |
str = rb_str_new(0, RSA_size(rsa)); |
539 |
buf_len = RSA_private_decrypt(RSTRING_LENINT(buffer), (unsigned char *)RSTRING_PTR(buffer), |
540 |
(unsigned char *)RSTRING_PTR(str), rsa, pad); |
541 |
if (buf_len < 0) ossl_raise(eRSAError, NULL); |
542 |
rb_str_set_len(str, buf_len); |
543 |
|
544 |
return str; |
545 |
} |
546 |
|
547 |
/* |
548 |
* call-seq: |
549 |
* rsa.sign_pss(digest, data, salt_length:, mgf1_hash:) -> String |
417 |
* rsa.sign_pss(digest, data, salt_length:, mgf1_hash:) -> String |
550 |
* |
418 |
* |
551 |
* Signs _data_ using the Probabilistic Signature Scheme (RSA-PSS) and returns |
419 |
* Signs _data_ using the Probabilistic Signature Scheme (RSA-PSS) and returns |
Lines 925-934
Link Here
|
925 |
rb_define_alias(cRSA, "to_s", "export"); |
793 |
rb_define_alias(cRSA, "to_s", "export"); |
926 |
rb_define_method(cRSA, "to_der", ossl_rsa_to_der, 0); |
794 |
rb_define_method(cRSA, "to_der", ossl_rsa_to_der, 0); |
927 |
rb_define_method(cRSA, "public_key", ossl_rsa_to_public_key, 0); |
795 |
rb_define_method(cRSA, "public_key", ossl_rsa_to_public_key, 0); |
928 |
rb_define_method(cRSA, "public_encrypt", ossl_rsa_public_encrypt, -1); |
|
|
929 |
rb_define_method(cRSA, "public_decrypt", ossl_rsa_public_decrypt, -1); |
930 |
rb_define_method(cRSA, "private_encrypt", ossl_rsa_private_encrypt, -1); |
931 |
rb_define_method(cRSA, "private_decrypt", ossl_rsa_private_decrypt, -1); |
932 |
rb_define_method(cRSA, "sign_pss", ossl_rsa_sign_pss, -1); |
796 |
rb_define_method(cRSA, "sign_pss", ossl_rsa_sign_pss, -1); |
933 |
rb_define_method(cRSA, "verify_pss", ossl_rsa_verify_pss, -1); |
797 |
rb_define_method(cRSA, "verify_pss", ossl_rsa_verify_pss, -1); |
934 |
|
798 |
|
Lines 946-956
Link Here
|
946 |
|
810 |
|
947 |
rb_define_method(cRSA, "params", ossl_rsa_get_params, 0); |
811 |
rb_define_method(cRSA, "params", ossl_rsa_get_params, 0); |
948 |
|
812 |
|
949 |
DefRSAConst(PKCS1_PADDING); |
|
|
950 |
DefRSAConst(SSLV23_PADDING); |
951 |
DefRSAConst(NO_PADDING); |
952 |
DefRSAConst(PKCS1_OAEP_PADDING); |
953 |
|
954 |
/* |
813 |
/* |
955 |
* TODO: Test it |
814 |
* TODO: Test it |
956 |
rb_define_method(cRSA, "blinding_on!", ossl_rsa_blinding_on, 0); |
815 |
rb_define_method(cRSA, "blinding_on!", ossl_rsa_blinding_on, 0); |