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