Advertisement
Guest User

AesBlock.c

a guest
Nov 23rd, 2017
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //  AesBlock
  3. //
  4. //  Encrypts or Decrypts a single 128 bit block specified on the command line as a hex string. Key is also on
  5. //  command line and may be 128, 192, or 256 bits in size.
  6. //
  7. //  This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
  8. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9.  
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. //  IMPORTS
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <stdbool.h>
  19. #include "CryptLib_Aes.h"
  20.  
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. //  INTERNAL FUNCTIONS
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24.  
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26. //  ReadHexData
  27. //
  28. //  Reads a string as hex and places it in Data. *pDataSize on entry specifies maximum number of bytes that can be
  29. //  read, and on return is set to how many were read. This will be zero if it failed to read any.
  30. //  This function ignores any character that isn't a hex character.
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. static
  33. void
  34.     ReadHexData
  35.     (
  36.         char const*         HexString,
  37.         uint8_t*            Data,
  38.         uint32_t*           pDataSize
  39.     )
  40. {
  41.     uint32_t        i;
  42.     char            holdingBuffer [3] = {0};
  43.     uint32_t        holdingBufferIndex = 0;
  44.     unsigned        hexToNumber;
  45.     unsigned        outputIndex = 0;
  46.  
  47.     for( i=0; i<strlen(HexString); i++ )
  48.     {
  49.         if(     ( HexString[i] >= '0' && HexString[i] <= '9' )
  50.             ||  ( HexString[i] >= 'A' && HexString[i] <= 'F' )
  51.             ||  ( HexString[i] >= 'a' && HexString[i] <= 'f' ) )
  52.         {
  53.             holdingBuffer[holdingBufferIndex] = HexString[i];
  54.             holdingBufferIndex += 1;
  55.  
  56.             if( 2 == holdingBufferIndex )
  57.             {
  58.                 // Have two digits now so read it as a byte.
  59.                 sscanf( holdingBuffer, "%x", &hexToNumber );
  60.                 Data[outputIndex] = (uint8_t) hexToNumber;
  61.                 outputIndex += 1;
  62.                 if( outputIndex == *pDataSize )
  63.                 {
  64.                     // No more space so stop reading
  65.                     break;
  66.                 }
  67.                 holdingBufferIndex = 0;
  68.             }
  69.         }
  70.     }
  71.  
  72.     *pDataSize = outputIndex;
  73. }
  74.  
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  76. //  PUBLIC FUNCTIONS
  77. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  78.  
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  80. //  main
  81. //
  82. //  Program entry point
  83. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  84. int
  85.     main
  86.     (
  87.         int             ArgC,
  88.         char**          ArgV
  89.     )
  90. {
  91.     uint8_t         block [128 / 8] = {0};
  92.     uint32_t        blockSize = sizeof( block );
  93.     uint8_t         key [256 / 8] = {0};
  94.     uint32_t        keySize = sizeof( key );
  95.     uint8_t*        bufferPtr;
  96.     uint32_t*       bufferSizePtr;
  97.     uint32_t        i;
  98.     uint32_t        paramIndex = 0;
  99.     bool            decryptMode = false;
  100.     AesContext      aesContext;
  101.  
  102.     if( 4 != ArgC && 3 != ArgC )
  103.     {
  104.         printf(
  105.             "Syntax\n"
  106.             "   AesBlock [-D] <KeyHex> <BlockHex>\n" );
  107.         return 1;
  108.     }
  109.  
  110.     for( i=1; i<(uint32_t)ArgC; i++ )
  111.     {
  112.         if( 0 == stricmp( ArgV[i], "-d" ) )
  113.         {
  114.             decryptMode = true;
  115.         }
  116.         else
  117.         {
  118.             if     ( 0 == paramIndex ) { bufferPtr = key; bufferSizePtr = &keySize; }
  119.             else if( 1 == paramIndex ) { bufferPtr = block; bufferSizePtr = &blockSize; }
  120.             else
  121.             {
  122.                 printf( "Invalid syntax\n" );
  123.                 exit( 1 );
  124.             }
  125.            
  126.             ReadHexData( ArgV[i], bufferPtr, bufferSizePtr );
  127.             paramIndex += 1;
  128.         }
  129.     }
  130.  
  131.     if( 128/8 != blockSize )
  132.     {
  133.         printf( "Invalid block size, must be 128 bits (was %u bits)\n", blockSize*8 );
  134.         exit( 1 );
  135.     }
  136.  
  137.     switch( keySize )
  138.     {
  139.         case 128/8: AesInitialise128( key, &aesContext ); break;
  140.         case 192/8: AesInitialise192( key, &aesContext ); break;
  141.         case 256/8: AesInitialise256( key, &aesContext ); break;
  142.         default:
  143.             printf( "Invalid key size, must be 128, 192, or 256 bits (was %u bits)\n", keySize*8 );
  144.             exit( 1 );
  145.     }
  146.  
  147.     if( decryptMode )
  148.     {
  149.         AesDecryptInPlace( &aesContext, block );
  150.     }
  151.     else
  152.     {
  153.         AesEncryptInPlace( &aesContext, block );
  154.     }
  155.  
  156.     // Display
  157.     for( i=0; i<sizeof(block); i++ )
  158.     {
  159.         printf( "%2.2x", block[i] );
  160.     }
  161.     printf( "\n" );
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement