8 Replies Latest reply on May 6, 2013 5:00 AM by user_78878863

    Cryptography with PSoC Creator 2.2

    user_108325373

       Hello,

         

      I'm new in this forum, and this is my first message. Several times I've searched in this forum some advice for my problems, and often I've found it. But now, I've not found no solutions, so I'm here to ask directly to you.

         

      I need to implement a decryption of a RSA 1024 crypted message, I've got PSoC Creator  2.2 Component Pack 5 (2.2.0.293) and a CY8CKIT-001 PSoC Development Board with a PSoC 5 board mounted.

         

      I tried to use some libraries such as PolarSSL or OpenSSL but I had some problems with the dependencies, so I want to ask you if somebody know about any thin library that implements a RSA 1024 decryption for Cortex M3 architecture like PSoC 5 (or in general for embedded systems), and if someone had ever used it.

         

      Thanks a lot for your time.

         

      Regards

         

      Antonio

        • 1. Re: Cryptography with PSoC Creator 2.2
          user_78878863

          A google search for "rsa library cortex m3" yields e.g. https://code.google.com/p/lpc1343codebase/source/browse/trunk/drivers/rsa/rsa.c?r=131 . Otherwise, there is also cyassl: (http://www.yassl.com/yaSSL/Products-cyassl.html ) or MatrixSSL (http://www.matrixssl.org/ ) which both claim availability on ARM.

          • 2. Re: Cryptography with PSoC Creator 2.2
            user_108325373

             Hi, thanks a lot for the reply.

               

            I've already searched on Google, I've found your links, unfortunately I need a 1024 bits implementation, I've tried the cyassl-2.6.0 library but for the several dependences I gave up, for search something thinner, now I try to use matrixssl-3-4-2-open library, I hope to find out a solution. I hope that someone have already used some library like this in PSoC Creator. Anyway, if the Matrix library works I'll tell you here.

               

            Antonio

            • 3. Re: Cryptography with PSoC Creator 2.2
              user_78878863

              I wish you good luck! And I'm looking forward to hear about your success (and even if it doesn't work out)

              1 of 1 people found this helpful
              • 4. Re: Cryptography with PSoC Creator 2.2
                user_78878863

                Btw: Wikipedia says for PolarSSL "It is also highly modular: each component, such as a cryptographic function, can be used independently from the rest of the framework".

                   

                So it should be possible to just use the RSA implementation, and remove the rest of the library. This should remove most (if not all) of the dependencies. You might want look at its predecessor (xyssl) too, maybe its easier there...

                1 of 1 people found this helpful
                • 5. Re: Cryptography with PSoC Creator 2.2
                  user_78878863

                  Actually, I just tested that. I copied rsa.c, rsa.h, config.h, bignum.h from the PolarSSL distribution, and removed the POLARSSL_PKCS1_V21 and POLARSSL_SELF_TEST defines from config.h. After that, the code compiled just fine.

                     

                  Note that I did not test whether encryption / decryption works...

                  1 of 1 people found this helpful
                  • 6. Re: Cryptography with PSoC Creator 2.2
                    user_108325373

                     Hello,

                       

                    thanks a lot for your advice, I've tried several libraries, now I'm trying again PolarSSL, the point is that I need not to generate a RSA Key because I already have one, so I need to set my private RSA Key to the library, I've the PEM and the DER format. For 1024 bit(and more) RSA I think I need to bignum.c and bn_mul.h in addiction with the files you told me. Anyway, I'm going to find a way to set my private RSA Key (different by RSA certificate) and if I'll be able to do this I'll try to decrypt..if you have more information I'll be grateful to you.

                       

                     

                       

                    Antonio

                    • 7. Re: Cryptography with PSoC Creator 2.2
                      user_108325373

                      Good news, I resolved all the problems, now I'm going to do a summary of all:

                         

                      I used the PolarSSL library, but not all of it, indeed I only copied these files:

                         

                       

                         

                      bignum.h

                         

                      bn_mul.h

                         

                      config.h

                         

                      rsa.h

                         

                      bignum.c

                         

                      rsa.c

                         

                      and removed the POLARSSL_PKCS1_V21 and POLARSSL_SELF_TEST defines from config.h

                         

                      With some compilers and version of PolarSSL there will be some errors, in this case it needs to edit bignum.c as descript in this page: http://e2e.ti.com/support/development_tools/compiler/f/343/t/197852.aspx

                         

                      add this lines:

                         

                       

                         

                      #define MULADDC_INIT \
                      { \
                      t_uint s0, s1, b0, b1; \
                      t_uint r0, r1, rx, ry; \
                      b0 = ( b << biH ) >> biH; \
                      b1 = ( b >> biH );

                         

                      #define MULADDC_CORE \
                      s0 = ( *s << biH ) >> biH; \
                      s1 = ( *s >> biH ); s++; \
                      rx = s0 * b1; r0 = s0 * b0; \
                      ry = s1 * b0; r1 = s1 * b1; \
                      r1 += ( rx >> biH ); \
                      r1 += ( ry >> biH ); \
                      rx <<= biH; ry <<= biH; \
                      r0 += rx; r1 += (r0 < rx); \
                      r0 += ry; r1 += (r0 < ry); \
                      r0 += c; r1 += (r0 < c); \
                      r0 += *d; r1 += (r0 < *d); \
                      c = r1; *(d++) = r0;

                         

                      #define MULADDC_STOP \
                      }

                         

                      and replace these lines:

                         

                       

                         

                      do {

                         
                                        *d += c; c = ( *d < c ); d++;   
                         
                                    }   
                         
                                    while     ( c != 0 );   
                         
                              
                         
                           with these lines:   
                         

                       

                         

                       

                         
                          do {   
                         
                          

                            t_uint temp = *d;
                           if (c != 0)
                                   *d = temp + c;
                           c = ( *d < c );
                           d++;
                      }
                      while( c != 0 );

                          

                      Ok, now it works, if you want to set your private key and you have the XML fornat you can use this functions:

                          

                       

                          

                      rsa.len = MAXDIGITS;

                          

                      mpi_read_string( &rsa.N , 16, RSA_N  );

                          

                      mpi_read_string( &rsa.E , 16, RSA_E  );

                          

                      mpi_read_string( &rsa.D , 16, RSA_D  );

                          

                      mpi_read_string( &rsa.P , 16, RSA_P  );

                          

                      mpi_read_string( &rsa.Q , 16, RSA_Q  );

                          

                      mpi_read_string( &rsa.DP, 16, RSA_DP );

                          

                      mpi_read_string( &rsa.DQ, 16, RSA_DQ );

                          

                      mpi_read_string( &rsa.QP, 16, RSA_QP );

                          

                       

                          

                      Finally I want to thank hli for his contribute to solve the problem. Thank you very much, I hope these posts will help someone else have to implements RSA1024

                          

                      Antonio

                          

                       

                          

                       

                          

                       

                         
                      • 8. Re: Cryptography with PSoC Creator 2.2
                        user_78878863

                        Thanks for your feedback! I'm happy that you got your problem solved.