1 // *********************************************************************
    2 //   Custom DPA Handler code example - User data en/decryption         *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserEncryption.c,v $
    7 // Version: $Revision: 1.15 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //
   13 // *********************************************************************
   14 
   15 // On-line DPA documentation https://doc.iqrf.org/DpaTechGuide/
   16 
   17 /* This example shows the usage of the user data en/decryption using AES-128
   18 
   19 User peripheral PNUM=0x20 and 2 commands are implemented:
   20 * PCMD=0 returns first 16 bytes of peripheral RAM encrypted by User key
   21 * PCMD=1 writes 16 bytes of data encrypted by the User key to the peripheral RAM
   22 
   23 The initial content of the peripheral RAM after the device is restarted is 0,1,2,3,4,5,6,7,8,"AES-128".
   24 
   25 You can use a public on-line AES tools to en/decrypt to play with the commands.
   26 
   27 [Example 1]
   28 * Restart module
   29 * Run PCMD=0. It returns the initial text from peripheral RAM encrypted by the User key (initial value is 00000000000000000000000000000000). Returned data is ce61d785c84e30883973340aea2e6dcf
   30 * Go to http://testprotect.com/appendix/AEScalc
   31 * Enter 00000000000000000000000000000000 to "AES key (in hex):"
   32 * Enter ce61d785c84e30883973340aea2e6dcf to "Input Data (in hex):"
   33 * Press "Decrypt it", the result is 0001020304050607084145532d313238, which corresponds to the expected 0,1,2,3,4,5,6,7,8,"AES-128"
   34 
   35 [Example 2]
   36 * Go to http://testprotect.com/appendix/AEScalc
   37 * Enter 00000000000000000000000000000000 to "AES key (in hex):"
   38 * Enter aaBBccDDeeFFaaBBccDDeeFFaaBBccDD to "Input Data (in hex):"
   39 * Press "Encrypt it", the result is e37967714d9be9e66bc03629427de452
   40 * Run PCMD=1 with data "e37967714d9be9e66bc03629427de452"
   41 * Read 16 bytes from address 0 of peripheral Ram. The result is expected aaBBccDDeeFFaaBBccDDeeFFaaBBccDD
   42 
   43 Note: another AES tool that works well is http://aes.online-domain-tools.com/
   44 
   45 */
   46 
   47 // Default IQRF include (modify the path according to your setup)
   48 #include "IQRF.h"
   49 
   50 // Default DPA header (modify the path according to your setup)
   51 #include "DPA.h"
   52 // Default Custom DPA Handler header (modify the path according to your setup)
   53 #include "DPAcustomHandler.h"
   54 
   55 //############################################################################################
   56 
   57 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   58 //############################################################################################
   59 bit CustomDpaHandler()
   60 //############################################################################################
   61 {
   62   // Handler presence mark
   63   clrwdt();
   64 
   65   // Detect DPA event to handle
   66   switch ( GetDpaEvent() )
   67   {
   68     // -------------------------------------------------
   69     case DpaEvent_Interrupt:
   70       // Do an extra quick background interrupt work
   71       // ! The time spent handling this event is critical.If there is no interrupt to handle return immediately otherwise keep the code as fast as possible.
   72       // ! Make sure the event is the 1st case in the main switch statement at the handler routine.This ensures that the event is handled as the 1st one.
   73       // ! It is desirable that this event is handled with immediate return even if it is not used by the custom handler because the Interrupt event is raised on every MCU interrupt and the “empty” return handler ensures the shortest possible interrupt routine response time.
   74       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   75       // ! Make sure race condition does not occur when accessing those variables at other places.
   76       // ! Make sure( inspect.lst file generated by C compiler ) compiler does not create any hidden temporary local variable( occurs when using division, multiplication or bit shifts ) at the event handler code.The name of such variable is usually Cnumbercnt.
   77       // ! Do not call any OS functions except setINDFx().
   78       // ! Do not use any OS variables especially for writing access.
   79       // ! All above rules apply also to any other function being called from the event handler code, although calling any function from Interrupt event is not recommended because of additional MCU stack usage.
   80 
   81       return Carry;
   82 
   83       // -------------------------------------------------
   84     case DpaEvent_Init:
   85       // Do a one time initialization before main loop starts
   86 
   87       // Initialize en/decrypted user data
   88       PeripheralRam[0x0] = 0;
   89       PeripheralRam[0x1] = 1;
   90       PeripheralRam[0x2] = 2;
   91       PeripheralRam[0x3] = 3;
   92       PeripheralRam[0x4] = 4;
   93       PeripheralRam[0x5] = 5;
   94       PeripheralRam[0x6] = 6;
   95       PeripheralRam[0x7] = 7;
   96       PeripheralRam[0x8] = 8;
   97       PeripheralRam[0x9] = 'A';
   98       PeripheralRam[0xA] = 'E';
   99       PeripheralRam[0xB] = 'S';
  100       PeripheralRam[0xC] = '-';
  101       PeripheralRam[0xD] = '1';
  102       PeripheralRam[0xE] = '2';
  103       PeripheralRam[0xF] = '8';
  104       break;
  105 
  106       // -------------------------------------------------
  107     case DpaEvent_DpaRequest:
  108       // Called to interpret DPA request for peripherals
  109       // -------------------------------------------------
  110       // Peripheral enumeration
  111       if ( IsDpaEnumPeripheralsRequest() )
  112       {
  113         // We implement 1 user peripheral
  114         _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 1;
  115         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
  116         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F;
  117         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0x1234;
  118 
  119 DpaHandleReturnTRUE:
  120         return TRUE;
  121       }
  122       // -------------------------------------------------
  123       // Get information about peripheral
  124       else if ( IsDpaPeripheralInfoRequest() )
  125       {
  126         if ( _PNUM == PNUM_USER + 0 )
  127         {
  128           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_USER_AREA;
  129           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ_WRITE;
  130           goto DpaHandleReturnTRUE;
  131         }
  132 
  133         break;
  134       }
  135       // -------------------------------------------------
  136       else
  137       {
  138         // Handle peripheral command
  139         if ( _PNUM == PNUM_USER + 0 )
  140         {
  141           switch ( _PCMD )
  142           {
  143             // Read data
  144             case 0x00:
  145             {
  146               // No input data
  147               if ( _DpaDataLength != 0 )
  148                 DpaApiReturnPeripheralError( ERROR_DATA_LEN );
  149 
  150               // Copy data to the result, the result length is 16 bytes (one AES block)
  151               copyMemoryBlock( PeripheralRam, _DpaMessage.Response.PData, _DpaDataLength = 16 );
  152               // Encrypt data (_DpaMessage.Response.PData is actually bufferRF)
  153               encryptBufferRF( 1 );
  154               return TRUE;
  155             }
  156 
  157             // Write data
  158             case 0x01:
  159             {
  160               // Exactly one AES block of input data is expected
  161               if ( _DpaDataLength != 16 )
  162                 DpaApiReturnPeripheralError( ERROR_DATA_LEN );
  163 
  164               // Decrypt data to write (_DpaMessage.Response.PData is actually bufferRF)
  165               decryptBufferRF( 1 );
  166               // Copy written data to the buffer
  167               copyMemoryBlock( _DpaMessage.Request.PData, PeripheralRam, 16 );
  168               // No data is written
  169               _DpaDataLength = 0;
  170               return TRUE;
  171             }
  172 
  173             // Invalid command
  174             default:
  175               DpaApiReturnPeripheralError( ERROR_PCMD );
  176           }
  177         }
  178       }
  179   }
  180 
  181   return FALSE;
  182 }
  183 
  184 //############################################################################################
  185 // Default Custom DPA Handler header; 2nd include to implement Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup) 
  186 #include "DPAcustomHandler.h"
  187 //############################################################################################