0
votes

I'm just started to work with IAR compiler and ARM microControllers. In the first step I want to do an RSA encryption using my AT91SAM7S MCU (I know that this is not a good first step! ;) ).

Anyway, after Googling I found this site containing two files named rsa.h and rsa.c that implement RSA algorithm for Embedded devices.

So I download this files and put them in the directory of my program, (In the same directory that main.c is).

Now, when I trying to build and compile this project, I face the following errors :

Building configuration: 4rsa - Debug Updating build tree...

3  file(s) deleted. 
Updating build tree... 
main.c 
Error[Pe020]: identifier "uint64_t" is undefined C:\4rsa\rsa.h 22 
Error while running C/C++ Compiler 
rsa.c 
Fatal Error[Pe005]: could not open source file "cross_studio_io.h" C:\4rsa\rsa.c 22 
Error while running C/C++ Compiler 

Total number of errors: 2 
Total number of warnings: 0 

Total number of errors: 2 
Total number of warnings: 0 

It seems that I must download and add some libraries to my program, but I don't have any idea which libraries I am need and where I can download them.


FYI:

This is contents of rsa.h:

/**************************************************************************/
/*! 
    \file     rsa.h
    \author   Kyle Loudon
              modified: microBuilder.eu
    \date     4 January, 2010
    \version  1.0

    Basic RSA-encryption using 64-bit math (32-bit keys).

    Based on the examples from "Mastering Algorithms with C" by
    Kyle Loudon (O'Reilly, 1999).
*/
/**************************************************************************/
#include <stdlib.h>
#ifndef _RSA_H_
#define _RSA_H_

/* In a secure implementation, huge_t should be at least 400 decimal digits, *
 * instead of the 20 provided by a 64-bit value.  This means that key values *
 * can be no longer than 10 digits in length in the current implementation.  */
typedef uint64_t huge_t;

/* Structure for RSA public keys. */
typedef struct rsaPubKey_s
{
  huge_t e;
  huge_t n;
} 
rsaPubKey_t;

/* Define a structure for RSA private keys. */
typedef struct rsaPriKey_s
{
  huge_t d;
  huge_t n;
} 
rsaPriKey_t;

void rsaTest();
void rsaEncrypt(huge_t plaintext, huge_t *ciphertext, rsaPubKey_t pubkey);
void rsaDecrypt(huge_t ciphertext, huge_t *plaintext, rsaPriKey_t prikey);

#endif

This is contents of rsa.c:

/**************************************************************************/
/*! 
    \file     rsa.c
    \author   Kyle Loudon
              modified: microBuilder.eu
    \date     4 January, 2010
    \version  1.0

    Basic RSA-encryption using 64-bit math (32-bit keys).

    Based on the examples from "Mastering Algorithms with C" by
    Kyle Loudon (O'Reilly, 1999).

    Note: The rsaTest function uses debug_printf in Rowley Associate's
    Crossworks for ARM.  If you wish to use an alternative means to
    display the test results, cross_studio_io.h can be removed from the
    include list, and debug_printf can be renamed to a different
    output method.
*/
/**************************************************************************/

#include <cross_studio_io.h>

#include "rsa.h"

static huge_t modexp(huge_t a, huge_t b, huge_t n) 
{
  huge_t y;
  y = 1;

  /*  Compute pow(a, b) % n using the binary square and multiply method. */
  while (b != 0) 
  {
    /*  For each 1 in b, accumulate y. */
    if (b & 1)
    {
      y = (y * a) % n;
    }

    /* Square a for each bit in b. */
    a = (a * a) % n;

    /*  Prepare for the next bit in b. */
    b = b >> 1;
  }

  return y;
}

void rsaTest()
{
  huge_t      rsaOrig, rsaDecrypted, rsaEncrypted;
  rsaPubKey_t publicKey;
  rsaPriKey_t privateKey;
  int         i;

  debug_printf("Encrypting with RSA\n");

  // Values based on 64-bit math (huge_t = unsigned long long)
  // which will result in more secure encryption, but also
  // increases the size of the encrypted text
  publicKey.e = 21;
  publicKey.n = 16484947;
  privateKey.d = 15689981;
  privateKey.n = 16484947;

  // Alternative values with 32-bit math (huge_t = unsigned long)
  // or when smaller encrypted text is desired
  // publicKey.e = 17;
  // publicKey.n = 209;
  // privateKey.d = 53;
  // privateKey.n = 209;

  debug_printf("d=%lld, e=%lld, n=%lld\n", 
                privateKey.d, publicKey.e, publicKey.n);

  for (i = 0; i < 128; i++) 
  {  
     rsaOrig = i;
     rsaEncrypt(rsaOrig, &rsaEncrypted, publicKey);
     rsaDecrypt(rsaEncrypted, &rsaDecrypted, privateKey);

     if (rsaOrig == rsaDecrypted)
     {
        debug_printf("In=%5lld, Encrypted=%10lld, Out=%5lld (OK)\n", 
                      rsaOrig, rsaEncrypted, rsaDecrypted);
     }
     else
     {
        debug_printf("In=%5lld, Encrypted=%10lld, Out=%5lld (ERROR)\n", 
                      rsaOrig, rsaEncrypted, rsaDecrypted);
     }  
  }
}

void rsaEncrypt(huge_t plaintext, huge_t *ciphertext, rsaPubKey_t pubkey) 
{
  *ciphertext = modexp(plaintext, pubkey.e, pubkey.n);

  return;
}

void rsaDecrypt(huge_t ciphertext, huge_t *plaintext, rsaPriKey_t prikey) 
{
  *plaintext = modexp(ciphertext, prikey.d, prikey.n);

  return;
}

And this is my IAR IDE output:

enter image description here

enter image description here

How can I handle these errors?

Please help me to getting started with this device. Thanks in advance.

1
For the first problem, you should include <stdint.h> which defines the fixed-width integer types. However, if the compiler doesn't support e.g. 64-bit types, then you won't have the unt64_t type. - Some programmer dude
As for the missing header file, maybe it's in a non-standard location? Have you tried searching for it? - Some programmer dude

1 Answers

0
votes

rsa.h is incorrectly written. It needs to include stdint.h. This is not related to IAR, ARM or anything else.