| 3779 | } |
| 3780 | |
| 3781 | dsc* evlRsaEncryptDecrypt(thread_db* tdbb, const SysFunction* function, const NestValueArray& args, |
| 3782 | impure_value* impure, bool encryptFlag) |
| 3783 | { |
| 3784 | tomcryptInitializer(); |
| 3785 | |
| 3786 | fb_assert(args.getCount() == RSA_CRYPT_ARG_MAX || args.getCount() == RSA_CRYPT_ARG_MAX - 1); |
| 3787 | |
| 3788 | Request* request = tdbb->getRequest(); |
| 3789 | |
| 3790 | // parse args and check correctness |
| 3791 | const dsc* dscs[RSA_CRYPT_ARG_MAX]; |
| 3792 | for (unsigned i = 0; i < args.getCount(); ++i) |
| 3793 | dscs[i] = EVL_expr(tdbb, request, args[i]); |
| 3794 | SSHORT pkcs15 = args.getCount() < RSA_CRYPT_ARG_MAX ? 0 : *(SSHORT*)(dscs[RSA_CRYPT_ARG_PKCS_1_5]->dsc_address); |
| 3795 | |
| 3796 | MetaName hashName; |
| 3797 | if (dscs[RSA_CRYPT_ARG_HASH]) |
| 3798 | MOV_get_metaname(tdbb, dscs[RSA_CRYPT_ARG_HASH], hashName); |
| 3799 | if (!hashName.hasData()) |
| 3800 | hashName = "SHA256"; |
| 3801 | string aName(hashName); |
| 3802 | aName.lower(); |
| 3803 | int hash = find_hash(aName.c_str()); |
| 3804 | if (hash < 0) |
| 3805 | status_exception::raise(Arg::Gds(isc_tom_hash_bad) << hashName); |
| 3806 | |
| 3807 | DscValue data(tdbb, dscs[RSA_CRYPT_ARG_VALUE]); |
| 3808 | if (!data.getBytes()) |
| 3809 | return nullptr; |
| 3810 | |
| 3811 | DscValue key(tdbb, dscs[RSA_CRYPT_ARG_KEY], "crypt key"); |
| 3812 | if (!key.getBytes()) |
| 3813 | return nullptr; |
| 3814 | |
| 3815 | DscValue lParam(tdbb, dscs[RSA_CRYPT_ARG_LPARAM]); |
| 3816 | |
| 3817 | // Run tomcrypt functions |
| 3818 | rsa_key rsaKey; |
| 3819 | tomCheck(rsa_import(key.getBytes(), key.getLength(), &rsaKey), Arg::Gds(isc_tom_rsa_import)); |
| 3820 | |
| 3821 | unsigned long outlen = encryptFlag ? 256 : 190; |
| 3822 | UCharBuffer outBuf; |
| 3823 | int stat = 0; |
| 3824 | int cryptRc = encryptFlag ? |
| 3825 | rsa_encrypt_key_ex(data.getBytes(), data.getLength(), outBuf.getBuffer(outlen), &outlen, |
| 3826 | lParam.getBytes(), lParam.getLength(), prng().getState(), prng().getIndex(), hash, |
| 3827 | pkcs15 ? LTC_PKCS_1_V1_5 : LTC_PKCS_1_OAEP, &rsaKey) : |
| 3828 | rsa_decrypt_key_ex(data.getBytes(), data.getLength(), outBuf.getBuffer(outlen), &outlen, |
| 3829 | lParam.getBytes(), lParam.getLength(), hash, |
| 3830 | pkcs15 ? LTC_PKCS_1_V1_5 : LTC_PKCS_1_OAEP, &stat, &rsaKey); |
| 3831 | rsa_free(&rsaKey); |
| 3832 | tomCheck(cryptRc, Arg::Gds(encryptFlag ? isc_tom_crypt_cip : isc_tom_decrypt_cip) << "RSA"); |
| 3833 | if ((!encryptFlag) && (!stat)) |
| 3834 | status_exception::raise(Arg::Gds(isc_tom_oaep)); |
| 3835 | |
| 3836 | dsc result; |
| 3837 | result.makeText(outlen, ttype_binary, outBuf.begin()); |
| 3838 | EVL_make_value(tdbb, &result, impure); |
no test coverage detected