4 Replies Latest reply on Nov 20, 2019 1:47 PM by AnCi_2234676

    PSoC 6 RSA Encryption Example?

    AnCi_2234676

      Hi,

       

      Can anyone provide me an example for RSA encryption using the crypto module on the psoc 6 using PSoC Creator?

       

      I can't seem to get it to work. I started with the example code for the TRNG (CE221295). I changed the build settings to include the full crypto library. When I call Cy_Crypto_Rsa_Proc(), only the first 32 bytes of my input message (padded with PKCS1 v1.5) gets copied to the output buffer and nothing else... but it returns 0!

       

      Can anyone help me with this?

        • 1. Re: PSoC 6 RSA Encryption Example?
          DheerajK_81

          We have provided the sample implementation in the PDL documentation for RSA. Pasting it here anyway.

          #define RSA_MODULO_LENGTH 2048u
            #define RSA_MODULO_DATA_SIZE (RSA_MODULO_LENGTH / 8)
             typedef struct
            {
             cy_stc_crypto_rsa_pub_key_t publicKeyStruct;
            uint8_t moduloData[RSA_MODULO_DATA_SIZE];
            uint8_t expData [32];
            uint8_t k1Data [RSA_MODULO_DATA_SIZE+4];
            uint8_t k2Data [RSA_MODULO_DATA_SIZE];
            uint8_t k3Data [RSA_MODULO_DATA_SIZE];
            } cy_stc_public_key_t;
             /* All data arrays should be 4-byte aligned */
            cy_stc_public_key_t cy_publicKey =
            {
            .publicKeyStruct =
            {
            .moduloPtr = 0,
            .moduloLength = RSA_MODULO_LENGTH,
            .pubExpPtr = 0,
            .pubExpLength = 24,
            .barretCoefPtr = NULL,
            .inverseModuloPtr = NULL,
            .rBarPtr = NULL
            },
            .moduloData =
            {  /* modulus in Little Endian for a public key - rsa_public.txt */
            0xD9u, 0x94u, 0x94u, 0x38u, 0xA4u, 0xE0u, 0x50u, 0xA1u,
            0xADu, 0xC5u, 0xE2u, 0x66u, 0xA9u, 0x7Fu, 0xE7u, 0xD7u,
            0xA8u, 0x10u, 0x87u, 0x3Au, 0xBEu, 0xB3u, 0x0Eu, 0x6Au,
            0xB2u, 0x8Bu, 0x2Eu, 0x8Du, 0xC2u, 0x45u, 0x41u, 0xA6u,
            0xDBu, 0xEBu, 0x90u, 0x20u, 0x56u, 0xECu, 0xFDu, 0x8Cu,
            0x23u, 0x09u, 0x13u, 0x5Du, 0x65u, 0xA2u, 0xADu, 0x9Du,
            0x3Bu, 0xF2u, 0x5Bu, 0xF6u, 0xABu, 0x2Eu, 0xFEu, 0xF2u,
            0x69u, 0x98u, 0x29u, 0x6Du, 0xD1u, 0x2Eu, 0x91u, 0x5Au,
            0x65u, 0x83u, 0xF5u, 0x7Fu, 0x8Eu, 0x73u, 0xFFu, 0xA1u,
            0x8Cu, 0x70u, 0x07u, 0xDFu, 0x4Du, 0xF4u, 0x79u, 0xB7u,
            0x18u, 0xC1u, 0xA3u, 0x2Bu, 0x82u, 0x5Bu, 0x9Eu, 0xE2u,
            0xF0u, 0xA0u, 0xB8u, 0xDAu, 0x19u, 0xADu, 0xBBu, 0x2Bu,
            0xD5u, 0x07u, 0x5Au, 0x85u, 0x12u, 0x03u, 0xD8u, 0x60u,
            0x53u, 0x3Du, 0xC6u, 0x34u, 0xE2u, 0x7Fu, 0x96u, 0x4Cu,
            0x26u, 0x1Eu, 0x82u, 0xB1u, 0x85u, 0xC3u, 0x0Du, 0x54u,
            0x68u, 0x37u, 0x97u, 0x58u, 0x19u, 0x36u, 0x43u, 0x9Cu,
            0xD9u, 0xC3u, 0x42u, 0xEBu, 0xBEu, 0xE2u, 0x8Fu, 0x72u,
            0xF1u, 0x5Eu, 0x2Au, 0x15u, 0x56u, 0x52u, 0xD4u, 0x6Du,
            0x61u, 0x97u, 0x16u, 0xFEu, 0xC3u, 0xF9u, 0x17u, 0x3Cu,
            0x37u, 0xD5u, 0xE1u, 0xA1u, 0x0Au, 0xB7u, 0xD9u, 0x65u,
            0xA1u, 0x15u, 0xECu, 0xC7u, 0x39u, 0xECu, 0xEDu, 0x39u,
            0x98u, 0x96u, 0x66u, 0x50u, 0x8Cu, 0x25u, 0xC3u, 0x29u,
            0xB9u, 0xF8u, 0x25u, 0x55u, 0x92u, 0x7Au, 0xBFu, 0xFBu,
            0x45u, 0x2Au, 0x28u, 0x8Au, 0xF9u, 0xE5u, 0xE2u, 0x30u,
            0x72u, 0x0Eu, 0x0Au, 0x1Cu, 0x25u, 0x09u, 0x86u, 0x6Fu,
            0xF6u, 0x6Fu, 0x15u, 0xEDu, 0x14u, 0xE6u, 0x1Eu, 0x53u,
            0x5Au, 0x15u, 0x25u, 0xB9u, 0x5Eu, 0xC9u, 0xBAu, 0x48u,
            0xA3u, 0xE3u, 0x93u, 0x62u, 0x3Cu, 0x6Cu, 0x3Cu, 0x83u,
            0x17u, 0x29u, 0xFBu, 0xAEu, 0x91u, 0x47u, 0xC9u, 0x41u,
            0x2Fu, 0xF9u, 0x82u, 0x29u, 0x7Bu, 0xB4u, 0x5Au, 0x93u,
            0x01u, 0x95u, 0xDBu, 0x08u, 0x7Eu, 0x7Bu, 0x99u, 0x1Eu,
            0xD4u, 0x25u, 0xD3u, 0x2Au, 0xF4u, 0xC7u, 0x9Fu, 0xB3u,
            },
             /* Little endian exponent for a public key - rsa_public.txt */
            .expData = { 0x01, 0x00, 0x01 },
            .k1Data = { 0, },
            .k2Data = { 0, },
            .k3Data = { 0, },
            };
             /* Present encrypted signature of the image */
            CY_ALIGN(4) uint8_t rsaEncryptedSign[RSA_MODULO_DATA_SIZE] =
            {
            0x12u, 0xCEu, 0x13u, 0x83u, 0x4Fu, 0xFFu, 0x39u, 0x9Bu,
            0x33u, 0xDEu, 0xDCu, 0xDBu, 0x7Cu, 0x62u, 0xA5u, 0x10u,
            0x24u, 0x07u, 0xEAu, 0x4Cu, 0x04u, 0x4Au, 0xCEu, 0x7Bu,
            0x8Cu, 0xD8u, 0xD4u, 0x64u, 0xD1u, 0x98u, 0xA2u, 0x33u,
            0x2Eu, 0xFFu, 0x06u, 0x13u, 0xD6u, 0x5Au, 0x50u, 0x3Cu,
            0xA2u, 0x5Eu, 0xE3u, 0x11u, 0x54u, 0x7Cu, 0x6Au, 0x49u,
            0x39u, 0xA3u, 0x62u, 0x02u, 0x66u, 0xB0u, 0x19u, 0x82u,
            0xFBu, 0x5Du, 0x15u, 0xB2u, 0x0Bu, 0xF7u, 0xECu, 0x6Cu,
            0xBEu, 0xEBu, 0x04u, 0x1Fu, 0x0Bu, 0x5Bu, 0x18u, 0x0Eu,
            0x96u, 0x03u, 0xC4u, 0x1Eu, 0x56u, 0xB6u, 0x1Fu, 0xF2u,
            0x08u, 0x7Au, 0x81u, 0x96u, 0x86u, 0xA4u, 0x93u, 0x5Du,
            0x66u, 0x63u, 0x10u, 0xD5u, 0x9Bu, 0xA9u, 0xD7u, 0x52u,
            0xCDu, 0xEFu, 0x23u, 0xDCu, 0x58u, 0xC7u, 0x3Du, 0x72u,
            0x3Fu, 0x09u, 0x5Eu, 0x3Bu, 0x03u, 0xF9u, 0x91u, 0x10u,
            0x63u, 0x2Au, 0x56u, 0xFAu, 0xCEu, 0x8Cu, 0x8Au, 0xBFu,
            0xB5u, 0xA1u, 0xA7u, 0x0Fu, 0xBBu, 0xD8u, 0xACu, 0x7Fu,
            0x43u, 0x3Cu, 0xBFu, 0x11u, 0xD8u, 0xAEu, 0x55u, 0xE7u,
            0x23u, 0xF8u, 0xA4u, 0xE0u, 0x56u, 0xCFu, 0x3Fu, 0x21u,
            0xA9u, 0xB6u, 0x01u, 0x34u, 0xDFu, 0xB6u, 0xDCu, 0xAAu,
            0x29u, 0xB0u, 0x97u, 0x9Du, 0xD9u, 0xAAu, 0x47u, 0xD4u,
            0xD9u, 0x4Au, 0x97u, 0x52u, 0x3Cu, 0xB9u, 0x36u, 0xDAu,
            0xF6u, 0xA6u, 0x14u, 0x97u, 0xDEu, 0xE6u, 0x76u, 0xBFu,
            0x1Bu, 0x23u, 0xDBu, 0x68u, 0x66u, 0xEAu, 0x0Eu, 0xC6u,
            0xD3u, 0x52u, 0x05u, 0x86u, 0x26u, 0x04u, 0x31u, 0xCEu,
            0xCFu, 0x8Du, 0x13u, 0x84u, 0x81u, 0xF2u, 0x7Bu, 0xB7u,
            0xDCu, 0x93u, 0x23u, 0x93u, 0xD1u, 0x0Cu, 0xF4u, 0xDFu,
            0x37u, 0x44u, 0x3Du, 0xD7u, 0xAFu, 0xBFu, 0xAFu, 0x32u,
            0xE7u, 0x31u, 0x50u, 0x70u, 0x62u, 0xC3u, 0xABu, 0x31u,
            0x51u, 0x28u, 0x2Bu, 0x0Bu, 0x31u, 0xC6u, 0xD3u, 0x0Fu,
            0x74u, 0xE6u, 0x31u, 0x21u, 0xF0u, 0xA3u, 0x40u, 0x5Du,
            0xEDu, 0xFFu, 0xC9u, 0xEBu, 0x5Au, 0x65u, 0xF3u, 0xCBu,
            0x67u, 0x50u, 0x62u, 0x2Du, 0x8Au, 0xEAu, 0xC3u, 0xBDu,
            };
             /* Previously calculated SHA-256 digest of the image */
            CY_ALIGN(4) uint8_t sha256Digest[CY_CRYPTO_SHA256_DIGEST_SIZE] =
            {
            0xf4u, 0xcbu, 0x29u, 0x53u, 0xadu, 0xc2u, 0xbdu, 0x49u,
            0x6bu, 0x43u, 0x28u, 0x2bu, 0x63u, 0xb7u, 0x0du, 0x16u,
            0xa9u, 0xd4u, 0x90u, 0x65u, 0x6eu, 0x7bu, 0x6du, 0xb7u,
            0x0bu, 0x17u, 0xefu, 0x60u, 0x02u, 0x12u, 0x0fu, 0x90u
            };
             /* Temporary decryption buffer */
            CY_ALIGN(4) uint8_t rsaOutput[RSA_MODULO_DATA_SIZE];
             cy_stc_crypto_context_rsa_t  cryptoRsaContext;
             cy_stc_crypto_context_rsa_ver_t cryptoRsaVerContext;
             cy_en_crypto_rsa_ver_result_t  verResult;
             cy_en_crypto_status_t  cryptoStatus;
            cy_publicKey.publicKeyStruct.moduloPtr = cy_publicKey.moduloData;
            cy_publicKey.publicKeyStruct.pubExpPtr = cy_publicKey.expData;
             /* NOTE: When signature placed as BIG-endian data (ex. generated by openssl), we should invert it for processing */
             Cy_Crypto_InvertEndianness(rsaEncryptedSign, 256);
            cryptoStatus = Cy_Crypto_Rsa_Proc(
            &cy_publicKey.publicKeyStruct,
            (const uint32_t*)rsaEncryptedSign, sizeof(rsaEncryptedSign),
            (uint32_t *)rsaOutput,
            &cryptoRsaContext);
             /* ... check for errors... */
            cryptoStatus = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
             /* ... check for errors... */
             Cy_Crypto_InvertEndianness(rsaOutput, 256);
             /* Verify decrypted signature by calculated SHA digest from data image */
            cryptoStatus = Cy_Crypto_Rsa_Verify(
            &verResult,
             CY_CRYPTO_MODE_SHA256,
            (const uint32_t*)sha256Digest,
            (const uint32_t*)rsaOutput, sizeof(rsaOutput),
            &cryptoRsaVerContext);
             /* ... check for errors... */
             /* Wait crypto become available */
            cryptoStatus = Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
             /* ... check for errors... */
            if (CY_CRYPTO_RSA_VERIFY_SUCCESS == verResult)
            {
             /* Verification OK */
            }
             else
            {
             /* Verification FAILED */
            }
          

           

          Let me know if you need clarification on anything.

           

          Regards,

          Dheeraj

          • 2. Re: PSoC 6 RSA Encryption Example?
            AnCi_2234676

            Thanks for the reply.

             

            This helped me figure out that the moduloLength field of the public key must be in number of bits and not bytes. Now, my buffer gets filled with encrypted data, however, I am still unable to decrypt it.

             

            Ultimately, what I want to do is encrypt 16 bytes of data using RSA and PCKS1 v1.5 padding with a key generated from openssl, and then be able to decrypt it with openssl.

             

            Here is the key I am using for my tests (also attached ):

            Private-Key: (2048 bit)

            modulus:

                00:d1:52:93:10:79:8d:bd:a1:62:7e:27:07:c4:39:

                c3:f9:f3:8e:17:46:f3:04:46:35:23:24:41:07:ad:

                3e:5d:e3:63:f4:ac:bc:9a:1e:ce:91:4e:48:2f:5a:

                af:8d:4b:4e:3c:ec:07:97:d0:58:77:07:57:b7:ee:

                6a:00:4d:e1:2e:61:c6:24:82:29:2e:ed:07:3e:e3:

                e3:e6:49:b0:88:28:40:98:1c:7c:a3:03:e2:57:72:

                e1:61:3b:7c:ef:7b:6a:29:0b:a4:c1:6b:7e:80:a2:

                d7:9c:3e:2b:14:19:05:34:59:26:84:94:63:12:8d:

                6f:ab:09:3f:da:60:6d:4e:94:c3:18:d3:10:15:dc:

                bb:0d:9b:79:ac:90:05:52:0b:cc:99:f4:9c:e2:33:

                75:10:59:2d:95:d6:80:b0:07:94:78:fe:83:b7:05:

                fd:a6:7d:71:15:c0:cb:35:56:86:82:95:b8:a3:0c:

                b2:89:d3:72:e3:39:79:81:f1:f4:eb:a6:17:a7:81:

                31:f8:0e:8f:cc:70:3f:dc:62:0e:f5:1f:f5:47:f2:

                26:22:a6:69:c7:8c:c7:c8:d2:ce:be:50:6b:0d:f9:

                c1:77:e4:e1:ab:71:f8:0e:da:c4:a7:02:80:56:2c:

                21:f8:99:c1:68:bc:1c:31:4f:4f:c4:2f:a2:3e:95:

                fa:9f

            publicExponent: 65537 (0x10001)

            privateExponent:

                09:da:76:3b:c7:4d:09:c9:28:06:c2:35:96:62:15:

                bc:95:5f:71:ef:10:0f:7a:72:93:e5:e7:fd:c1:84:

                f7:0c:76:2f:15:56:87:76:bd:c8:8f:43:cc:9e:db:

                36:ae:7d:cf:56:59:01:27:78:92:99:d0:e2:42:a1:

                d0:c3:d9:be:c2:31:7e:94:75:f5:d4:88:8b:c8:54:

                79:09:ac:fa:ab:d2:66:96:a8:b3:4f:c2:7e:d5:54:

                90:ac:29:34:68:bd:d5:92:cb:bd:a3:1e:c0:55:6a:

                0a:db:ce:51:52:e0:30:d7:a5:7d:86:32:a7:b3:ea:

                b6:c5:67:b1:12:ea:f2:50:ee:90:90:4c:43:4c:53:

                e8:62:b0:7d:13:11:34:6b:38:11:31:36:12:7f:05:

                4f:70:74:77:3a:cc:93:2e:ff:df:4c:8e:c6:2a:a9:

                44:31:8c:a4:89:dd:74:1e:2d:bf:7d:ca:a9:8f:e5:

                74:3a:0e:f5:0d:1d:2a:af:fd:ae:4f:77:38:3c:0e:

                01:fb:d3:14:ad:75:0e:6b:d7:a9:fb:6f:b2:c1:bb:

                6f:e1:3b:d8:ac:98:76:a0:fe:a4:de:46:f4:30:81:

                fa:4e:71:e2:4f:ab:93:7f:bc:7a:66:09:0d:30:b4:

                36:1f:b1:58:32:d6:0c:4a:51:c4:09:e9:af:6c:cc:

                81

            prime1:

                00:f2:ce:9b:cd:72:2f:fa:44:fe:f2:25:de:91:99:

                35:7c:d7:b5:7d:c1:6b:38:25:ae:2f:78:99:b1:e6:

                ed:d5:d3:11:7e:1c:7e:f5:de:ae:23:c1:d9:3a:b5:

                db:86:ce:e5:9a:7f:e4:db:78:2c:37:a8:27:d6:d6:

                a7:a7:95:fe:a5:78:e3:bd:09:82:9e:b0:2b:45:11:

                4c:cc:9b:4a:de:da:cc:f2:63:25:32:d3:61:f1:5f:

                b2:73:2d:6d:b3:25:b8:a5:1a:76:4d:f5:4d:f7:47:

                d0:1c:58:4e:13:8f:c5:cb:cf:8b:69:87:84:88:12:

                07:e9:4f:f3:2a:49:24:d4:c1

            prime2:

                00:dc:b2:34:34:51:24:3a:f5:0a:53:44:58:23:da:

                53:a8:44:41:ac:0e:5a:38:3b:43:5b:14:95:dd:24:

                95:0d:35:1e:0e:eb:a5:dd:23:c4:f1:60:4f:4a:f5:

                d7:f3:32:f0:15:8f:91:24:a5:a6:a2:9e:0f:8a:f3:

                e2:f5:6e:74:1e:7a:5a:e2:8c:4f:46:ca:12:8c:6f:

                e4:df:9d:de:92:f7:70:dd:a5:0f:8d:43:4b:af:e3:

                67:73:64:a9:a4:33:a5:96:9c:f6:12:32:51:a7:0f:

                4b:0d:b3:51:ce:02:e9:fe:f5:dd:2d:d1:1a:83:ae:

                64:85:b5:67:64:ad:c9:c7:5f

            exponent1:

                1d:e5:ff:1d:6f:40:25:2b:35:d1:8f:84:36:9d:8d:

                0e:90:69:52:d0:ec:68:ff:8a:d1:61:a2:5b:5b:ba:

                51:a8:29:f1:43:85:06:b5:a2:ef:d8:40:ed:8f:d3:

                36:68:67:b1:7c:aa:2b:b4:42:8d:9f:f1:37:ed:3a:

                bb:d2:26:87:2d:b9:ad:5f:6c:6c:29:f9:4a:09:f5:

                51:04:96:55:96:cf:c1:d1:1f:95:80:9e:00:e0:ed:

                d2:6c:f7:a3:25:b3:f4:f0:ab:3c:b2:46:25:7a:fc:

                4f:61:38:4c:04:5a:30:dc:9f:a8:20:fd:da:db:8d:

                89:d7:44:74:22:89:9c:81

            exponent2:

                00:c2:93:e7:4c:fd:01:39:6d:d3:e5:a8:0b:21:c9:

                1b:1a:ee:db:0b:cc:37:a4:fe:33:1b:79:61:cb:f0:

                b5:ed:48:18:b1:44:f8:1e:a9:ab:b8:b8:96:10:5c:

                02:42:6c:75:9a:3c:d2:d6:01:aa:92:13:53:56:ca:

                42:1e:eb:7c:3b:66:da:fc:84:82:5c:c2:4a:6a:41:

                39:04:22:d7:1a:b2:48:73:52:55:0c:d1:f9:48:20:

                f1:93:9e:7b:a6:c6:d6:46:9d:2a:a2:ce:74:89:1b:

                05:ac:15:9e:99:48:9e:6f:e8:eb:51:ba:db:6a:9f:

                f6:06:a0:f2:80:a4:6c:93:77

            coefficient:

                00:88:f2:b9:1d:fa:ba:8e:38:69:52:19:c3:35:e6:

                5e:26:1f:8c:05:6c:d9:f0:ac:cc:ab:81:ec:0f:5c:

                3b:81:69:49:9a:5a:4e:a4:c0:e7:bd:d7:c1:6f:84:

                97:88:0a:e1:89:8e:dd:cc:29:62:b1:1e:71:9c:a3:

                a4:80:95:1e:18:27:74:ab:6c:46:0e:94:3e:ec:5b:

                10:34:47:d2:7f:31:54:c2:5d:2f:f3:7e:58:65:c5:

                45:95:6f:79:22:97:a9:b6:f5:33:cf:ee:75:d4:cf:

                f2:75:89:b8:8b:d6:47:cf:c2:86:3d:04:2b:dc:6a:

                45:46:45:59:75:fe:31:2b:5c

            -----BEGIN RSA PRIVATE KEY-----

            MIIEpAIBAAKCAQEA0VKTEHmNvaFificHxDnD+fOOF0bzBEY1IyRBB60+XeNj9Ky8

            mh7OkU5IL1qvjUtOPOwHl9BYdwdXt+5qAE3hLmHGJIIpLu0HPuPj5kmwiChAmBx8

            owPiV3LhYTt873tqKQukwWt+gKLXnD4rFBkFNFkmhJRjEo1vqwk/2mBtTpTDGNMQ

            Fdy7DZt5rJAFUgvMmfSc4jN1EFktldaAsAeUeP6DtwX9pn1xFcDLNVaGgpW4owyy

            idNy4zl5gfH066YXp4Ex+A6PzHA/3GIO9R/1R/ImIqZpx4zHyNLOvlBrDfnBd+Th

            q3H4DtrEpwKAViwh+JnBaLwcMU9PxC+iPpX6nwIDAQABAoIBAAnadjvHTQnJKAbC

            NZZiFbyVX3HvEA96cpPl5/3BhPcMdi8VVod2vciPQ8ye2zaufc9WWQEneJKZ0OJC

            odDD2b7CMX6UdfXUiIvIVHkJrPqr0maWqLNPwn7VVJCsKTRovdWSy72jHsBVagrb

            zlFS4DDXpX2GMqez6rbFZ7ES6vJQ7pCQTENMU+hisH0TETRrOBExNhJ/BU9wdHc6

            zJMu/99MjsYqqUQxjKSJ3XQeLb99yqmP5XQ6DvUNHSqv/a5Pdzg8DgH70xStdQ5r

            16n7b7LBu2/hO9ismHag/qTeRvQwgfpOceJPq5N/vHpmCQ0wtDYfsVgy1gxKUcQJ

            6a9szIECgYEA8s6bzXIv+kT+8iXekZk1fNe1fcFrOCWuL3iZsebt1dMRfhx+9d6u

            I8HZOrXbhs7lmn/k23gsN6gn1tanp5X+pXjjvQmCnrArRRFMzJtK3trM8mMlMtNh

            8V+ycy1tsyW4pRp2TfVN90fQHFhOE4/Fy8+LaYeEiBIH6U/zKkkk1MECgYEA3LI0

            NFEkOvUKU0RYI9pTqERBrA5aODtDWxSV3SSVDTUeDuul3SPE8WBPSvXX8zLwFY+R

            JKWmop4PivPi9W50Hnpa4oxPRsoSjG/k353ekvdw3aUPjUNLr+Nnc2SppDOllpz2

            EjJRpw9LDbNRzgLp/vXdLdEag65khbVnZK3Jx18CgYAd5f8db0AlKzXRj4Q2nY0O

            kGlS0Oxo/4rRYaJbW7pRqCnxQ4UGtaLv2EDtj9M2aGexfKortEKNn/E37Tq70iaH

            LbmtX2xsKflKCfVRBJZVls/B0R+VgJ4A4O3SbPejJbP08Ks8skYlevxPYThMBFow

            3J+oIP3a242J10R0IomcgQKBgQDCk+dM/QE5bdPlqAshyRsa7tsLzDek/jMbeWHL

            8LXtSBixRPgeqau4uJYQXAJCbHWaPNLWAaqSE1NWykIe63w7Ztr8hIJcwkpqQTkE

            ItcaskhzUlUM0flIIPGTnnumxtZGnSqiznSJGwWsFZ6ZSJ5v6OtRuttqn/YGoPKA

            pGyTdwKBgQCI8rkd+rqOOGlSGcM15l4mH4wFbNnwrMyrgewPXDuBaUmaWk6kwOe9

            18FvhJeICuGJjt3MKWKxHnGco6SAlR4YJ3SrbEYOlD7sWxA0R9J/MVTCXS/zflhl

            xUWVb3kil6m29TPP7nXUz/J1ibiL1kfPwoY9BCvcakVGRVl1/jErXA==

            -----END RSA PRIVATE KEY-----

             

            My public key looks like this:

             

            #define CRYPTO_RSA_KEY_LENGTH                 (256u) // 2048 bits
            #define CRYPTO_RSA_KEY_BIT_LENGTH            (2048u)
            
            
            uint8_t rsaModulus[CRYPTO_RSA_KEY_LENGTH] = {
                0xd1u,0x52u,0x93u,0x10u,0x79u,0x8du,0xbdu,0xa1u,0x62u,0x7eu,0x27u,0x07u,0xc4u,0x39u,0xc3u,0xf9u,
                0xf3u,0x8eu,0x17u,0x46u,0xf3u,0x04u,0x46u,0x35u,0x23u,0x24u,0x41u,0x07u,0xadu,0x3eu,0x5du,0xe3u,
                0x63u,0xf4u,0xacu,0xbcu,0x9au,0x1eu,0xceu,0x91u,0x4eu,0x48u,0x2fu,0x5au,0xafu,0x8du,0x4bu,0x4eu,
                0x3cu,0xecu,0x07u,0x97u,0xd0u,0x58u,0x77u,0x07u,0x57u,0xb7u,0xeeu,0x6au,0x00u,0x4du,0xe1u,0x2eu,
                0x61u,0xc6u,0x24u,0x82u,0x29u,0x2eu,0xedu,0x07u,0x3eu,0xe3u,0xe3u,0xe6u,0x49u,0xb0u,0x88u,0x28u,
                0x40u,0x98u,0x1cu,0x7cu,0xa3u,0x03u,0xe2u,0x57u,0x72u,0xe1u,0x61u,0x3bu,0x7cu,0xefu,0x7bu,0x6au,
                0x29u,0x0bu,0xa4u,0xc1u,0x6bu,0x7eu,0x80u,0xa2u,0xd7u,0x9cu,0x3eu,0x2bu,0x14u,0x19u,0x05u,0x34u,
                0x59u,0x26u,0x84u,0x94u,0x63u,0x12u,0x8du,0x6fu,0xabu,0x09u,0x3fu,0xdau,0x60u,0x6du,0x4eu,0x94u,
                0xc3u,0x18u,0xd3u,0x10u,0x15u,0xdcu,0xbbu,0x0du,0x9bu,0x79u,0xacu,0x90u,0x05u,0x52u,0x0bu,0xccu,
                0x99u,0xf4u,0x9cu,0xe2u,0x33u,0x75u,0x10u,0x59u,0x2du,0x95u,0xd6u,0x80u,0xb0u,0x07u,0x94u,0x78u,
                0xfeu,0x83u,0xb7u,0x05u,0xfdu,0xa6u,0x7du,0x71u,0x15u,0xc0u,0xcbu,0x35u,0x56u,0x86u,0x82u,0x95u,
                0xb8u,0xa3u,0x0cu,0xb2u,0x89u,0xd3u,0x72u,0xe3u,0x39u,0x79u,0x81u,0xf1u,0xf4u,0xebu,0xa6u,0x17u,
                0xa7u,0x81u,0x31u,0xf8u,0x0eu,0x8fu,0xccu,0x70u,0x3fu,0xdcu,0x62u,0x0eu,0xf5u,0x1fu,0xf5u,0x47u,
                0xf2u,0x26u,0x22u,0xa6u,0x69u,0xc7u,0x8cu,0xc7u,0xc8u,0xd2u,0xceu,0xbeu,0x50u,0x6bu,0x0du,0xf9u,
                0xc1u,0x77u,0xe4u,0xe1u,0xabu,0x71u,0xf8u,0x0eu,0xdau,0xc4u,0xa7u,0x02u,0x80u,0x56u,0x2cu,0x21u,
                0xf8u,0x99u,0xc1u,0x68u,0xbcu,0x1cu,0x31u,0x4fu,0x4fu,0xc4u,0x2fu,0xa2u,0x3eu,0x95u,0xfau,0x9fu
            };
            
            
            uint8_t rsaExponent[] = { 0x01, 0x00, 0x01 };
            
            
            cy_stc_crypto_rsa_pub_key_t const rsaPublicKey = {
                    .moduloPtr        = (uint8_t*)&rsaModulus,
                    .moduloLength     = CRYPTO_RSA_KEY_BIT_LENGTH,
                    .pubExpPtr        = (uint8_t*)&rsaExponent,
                    .pubExpLength     = 24,
                    .barretCoefPtr    = NULL,
                    .inverseModuloPtr = NULL,
                    .rBarPtr          = NULL
            };
            

             

            The padded buffer I am trying to encrypt is this:

             

            CY_ALIGN(4) uint8_t paddedMessage[CRYPTO_RSA_KEY_LENGTH] = {
                         0x00,0x02,0xa6,0x8a,0xb8,0x74,0x95,0x29,0x97,0x40,0x34,0x8d,0xd3,0x4d,0xd9,0x0c
                        ,0xb0,0x44,0x5f,0x2b,0xac,0x29,0xd8,0x54,0xbe,0x8a,0x40,0x99,0x15,0xfb,0xa8,0xde
                        ,0x47,0xc3,0xde,0xb5,0xcd,0x81,0xc9,0x92,0x6c,0x69,0xc8,0xe1,0x01,0x95,0xf0,0xec
                        ,0xca,0xb8,0x8d,0x62,0x36,0x48,0xc4,0x89,0xe8,0xba,0xaa,0xec,0x5f,0xbd,0xbe,0x0d
                        ,0xf4,0xc8,0x1d,0x6b,0x2d,0x15,0x39,0x99,0x8c,0x10,0xb2,0xec,0xe9,0xd4,0x91,0x39
                        ,0xfa,0x70,0x28,0x3b,0x9d,0x53,0xa1,0x21,0x84,0x83,0xb3,0xf5,0x66,0x1e,0x30,0xf9
                        ,0x4c,0xc2,0x34,0x8d,0xd1,0xf5,0x86,0xe2,0xcc,0x91,0xe8,0x33,0x26,0x37,0xbd,0xa7
                        ,0x8f,0xaa,0xd2,0x2a,0x62,0x69,0x15,0xd4,0xf6,0x80,0x48,0xf6,0xdb,0xdf,0xa6,0xc6
                        ,0x4d,0x8a,0xff,0x1c,0x5b,0x0b,0x32,0x2c,0x16,0xbe,0xeb,0x37,0x81,0x20,0x1e,0xf2
                        ,0x90,0x46,0x9b,0xac,0x72,0x15,0xbb,0x22,0xd4,0xf5,0x7e,0x45,0x04,0x30,0xb1,0xd0
                        ,0x7c,0xe6,0xce,0x52,0xe2,0x2a,0x5f,0x49,0x5e,0xf2,0x21,0xee,0x42,0x03,0x6f,0x12
                        ,0x64,0x6b,0x89,0xc9,0xa9,0xc9,0x07,0xd1,0xc9,0x6f,0xaa,0x4f,0x86,0xde,0x20,0x21
                        ,0x4e,0xc2,0x37,0xa2,0x53,0x94,0xc5,0x37,0x64,0x95,0x4d,0x3f,0x6e,0x3d,0x2f,0x64
                        ,0x07,0x9e,0x96,0x7b,0x50,0x14,0x4f,0xf6,0x48,0xd7,0x7b,0xed,0x44,0x60,0xbb,0xef
                        ,0xa4,0x28,0x65,0x39,0xd2,0xfa,0x1d,0x5a,0x20,0xb8,0x89,0x2f,0xa4,0x67,0xa2,0x00
                        ,0x40,0xcb,0x85,0x7e,0xec,0x0e,0x63,0x69,0xfb,0x83,0x17,0x77,0x8a,0xd1,0xaa,0x84
                };
            

             

            My encryption function is this:

             

            void Crypto_RsaEncrypt(const uint8_t paddedMessage[CRYPTO_RSA_KEY_LENGTH], uint8_t out[CRYPTO_RSA_KEY_LENGTH]) {
                cy_en_crypto_status_t status;
                cy_stc_crypto_context_rsa_t cryptoRsaContext;
                Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
                status = Cy_Crypto_Rsa_Proc(&rsaPublicKey, (const uint32_t*)paddedMessage, CRYPTO_RSA_KEY_LENGTH, (uint32_t*)out, &cryptoRsaContext);
                Cy_Crypto_Sync(CY_CRYPTO_SYNC_BLOCKING);
            }
            

             

            To my understanding, I must use Cy_Crypto_Rsa_InvertEndianness on the rsaModulus since the output of openssl is in big endian (which I do). Aside from that, do you see anythong wrong?

             

            Thanks in advance!

            • 3. Re: PSoC 6 RSA Encryption Example?
              AnCi_2234676

              Here is where I'm at with my test project.

              Let me know if you see anything with it.

               

              Thanks

              • 4. Re: PSoC 6 RSA Encryption Example?
                AnCi_2234676

                Success!

                My issue was with endianness. Inverting the input buffer before encrypting and the output buffer after encrypting did the trick.

                It is kind of mentioned in the pdl doc, but it is not that clear, not to me at least.