Main Site Documentation

G120 MF 4.2.8 beta Crashes using RSACryptoServiceProvider


#1

The following code will crash the G120 and starts to output low level MCU exceptions to COM1… Remark that the encryption code is not called in this demo. So it has to do with low level init code…


using System;
using System.Security.Cryptography;
using System.Text;
using Microsoft.SPOT;

namespace G120Encryption
{
    public class Program
    {
        public static void Main()
        {
            Debug.Print("CryptoTest... actually doing nothing");
        }
    }


    public class BlockEncryption
    {
        public byte[] Encrypt(string stringToEncrypt)
        {
            return Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt));
        }

        public byte[] Encrypt(byte[] dataToEncrypt)
        {
            try
            {
                using (var rsa = new RSACryptoServiceProvider())
                {
                    return RsaEncrypt(dataToEncrypt, rsa.ExportParameters(false));
                }
            }

            catch (ArgumentNullException)
            {
                Debug.Print("Encryption failed");
                return null;
            }
        }

        public byte[] DecryptByteArray(byte[] dataToDecrypt)
        {
            using (var rsa = new RSACryptoServiceProvider())
            {
                return RsaDecrypt(dataToDecrypt, rsa.ExportParameters(true));
            }
        }


        private static byte[] RsaEncrypt(byte[] dataToEncrypt, RSAParameters rsaKeyInfo)
        {
            try
            {
                byte[] encryptedData;
                using (var rsa = new RSACryptoServiceProvider())
                {
                    rsa.ImportParameters(rsaKeyInfo);
                    encryptedData = rsa.Encrypt(dataToEncrypt);
                }
                return encryptedData;
            }
            catch (CryptographicException e)
            {
                Debug.Print(e.Message);
                return null;
            }
        }


        private static byte[] RsaDecrypt(byte[] dataToDecrypt, RSAParameters rsaKeyInfo)
        {
            byte[] decryptedData = null;
            try
            {
                using (var rsa = new RSACryptoServiceProvider())
                {
                    rsa.ImportParameters(rsaKeyInfo);
                    decryptedData = rsa.Decrypt(dataToDecrypt);
                }
            }
            catch (CryptographicException e)
            {
                Debug.Print(e.ToString());
            }
            return decryptedData;
        }
    }
}


#2

@ RobvanSchelven - I you remove the encryption class, it does not crash ?


#3

@ LouisCpro - Yes, it still crash… Must be in assembly System.Security or Microsoft.Spot.Security.PKCS11

After deploying this example a firmware flash is needed to get the device back working again.

I pinned the G120 firmware updater to my windows task-bar :frowning:


#4

@ RobvanSchelven - Whaoo !

Fortunetaly it is a beta !


#5

Wahu… this issue is solved with release 4.2.9


#6

This is great. By the way, the current SDK is not in beta but posted under beta as we are doing some more testing to be 110% sure :slight_smile:


#7

Hello!
This thread is very old but I have the same problem with an fez cerberus board.
I have the package version 2014R1 installed.
I have also try the 2014R2 beta2.

Loader (TinyBooter) version information:
4.2.6.1 on this device.

Firmware (TinyCLR) version information:
4.2.6.2 on this device.