How to decrypt with RSA_PKCS_OAEP mechanism?

12/11/2017 1:49:11 AM
Gravatar
Total Posts 2

How to decrypt with RSA_PKCS_OAEP mechanism?

Hi,

I am trying to encrypt/decrypt using the RSA_PKCS_OAEP mechanism. So far I have generated an asymmetric key pair and have successfully used it to encrypt/decrypt with the RSA_PKCS mechanism, but cannot get the RSA_PKCS_OAEP mechanism to work. My sample code is below, which gives this error:

"Error Code:113, Translation:MECHANISM_PARAM_INVALID, Message: ---> Cryptware.NCryptoki.CryptokiException: Error n. 113
   at Cryptware.NCryptoki.Session.Encrypt(Byte[] data)"

 

        [StructLayout(LayoutKind.Sequential, Pack = 0, CharSet = CharSet.Unicode)]
        public struct CK_RSA_PKCS_OAEP_PARAMS
        {
            public ulong HashAlg;

            public ulong Mgf;

            public ulong Source;

            public IntPtr SourceData;

            public uint SourceDataLen;
        }

 

     public void TestAsymmetric() {

            var mechanismParams = new CK_RSA_PKCS_OAEP_PARAMS
            {
                HashAlg = Mechanism.CKM_SHA_1,
                Mgf = Mechanism.CKG_MGF1_SHA1,
                Source = 0,
                SourceData = IntPtr.Zero,
                SourceDataLen = 0,
            };

            var dataToEncrypt = new byte[] {1, 2, 3};

            var encryptMech = new Mechanism(Mechanism.CKM_RSA_PKCS_OAEP, mechanismParams);
            _session.EncryptInit(encryptMech, publicKey);


            var encrypted = _session.Encrypt(dataToEncrypt);

            var privateKey = ... // load the private key

            _session.DecryptInit(encryptMech, privateKey);
            var decrypted = _session.Decrypt(encrypted);

    }

12/11/2017 3:47:40 PM
Gravatar
Total Posts 33

Re: How to decrypt with RSA_PKCS_OAEP mechanism?

I think there is something wrong here

 

  var mechanismParams = new CK_RSA_PKCS_OAEP_PARAMS
            {
                HashAlg = Mechanism.CKM_SHA_1,
                Mgf = Mechanism.CKG_MGF1_SHA1,
                Source = 0,
                SourceData = IntPtr.Zero,
                SourceDataLen = 0,
            };

12/11/2017 11:07:55 PM
Gravatar
Total Posts 2

Re: How to decrypt with RSA_PKCS_OAEP mechanism?

Thanks yes I believe you are correct that there is something wrong with the parameters, but what should the correct parameters look like? I have also tried setting Source to 1.

12/13/2017 1:58:56 PM
Gravatar
Total Posts 33

Re: How to decrypt with RSA_PKCS_OAEP mechanism?

I don't know what's can be wrong, honestly... but why sourceDataLen is 0?

12/13/2017 3:09:57 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: How to decrypt with RSA_PKCS_OAEP mechanism?

 

The correct way to define RSA_PKCS_OAEP_PARAMS is this one:

 CK_RSA_PKCS_OAEP_PARAMS oaepParams = { CKM_SHA_1, CKG_MGF1_SHA1, 1, NULL_PTR, 0 };

that converted in C# corresponde to:

var mechanismParams = new CK_RSA_PKCS_OAEP_PARAMS
            {
                HashAlg = Mechanism.CKM_SHA_1,
                Mgf = Mechanism.CKG_MGF1_SHA1,
                Source = 1,
                SourceData = IntPtr.Zero,
                SourceDataLen = 0,
            };

Let's know if it works.