Advertisement
waterjuice

AesCtrOutput.c

Nov 28th, 2017
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.46 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //  AesCtrOutput
  3. //
  4. //  Outputs bytes from an AES CTR stream. Key and IV are taken from command line. Bytes are output as hex
  5. //
  6. //  This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8.  
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. //  IMPORTS
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include "CryptLib_AesCtr.h"
  18.  
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. //  DEFINITIONS
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. #ifndef __min
  24.    #define __min( x, y )  (((x) < (y))?(x):(y))
  25. #endif
  26.  
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. //  CONSTANTS
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30.  
  31. #define BUFFER_SIZE             1024
  32.  
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  34. //  FUNCTIONS
  35. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36.  
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. //  ReadHexData
  39. //
  40. //  Reads a string as hex and places it in Data. *pDataSize on entry specifies maximum number of bytes that can be
  41. //  read, and on return is set to how many were read. This will be zero if it failed to read any.
  42. //  This function ignores any character that isn't a hex character.
  43. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  44. static
  45. void
  46.     ReadHexData
  47.     (
  48.         char const*         HexString,          // [in]
  49.         uint8_t*            Data,               // [out]
  50.         uint32_t*           pDataSize           // [in out]
  51.     )
  52. {
  53.     uint32_t        i;
  54.     char            holdingBuffer [3] = {0};
  55.     uint32_t        holdingBufferIndex = 0;
  56.     unsigned        hexToNumber;
  57.     unsigned        outputIndex = 0;
  58.  
  59.     for( i=0; i<strlen(HexString); i++ )
  60.     {
  61.         if(     ( HexString[i] >= '0' && HexString[i] <= '9' )
  62.             ||  ( HexString[i] >= 'A' && HexString[i] <= 'F' )
  63.             ||  ( HexString[i] >= 'a' && HexString[i] <= 'f' ) )
  64.         {
  65.             holdingBuffer[holdingBufferIndex] = HexString[i];
  66.             holdingBufferIndex += 1;
  67.  
  68.             if( 2 == holdingBufferIndex )
  69.             {
  70.                 // Have two digits now so read it as a byte.
  71.                 sscanf( holdingBuffer, "%x", &hexToNumber );
  72.                 Data[outputIndex] = (uint8_t) hexToNumber;
  73.                 outputIndex += 1;
  74.                 if( outputIndex == *pDataSize )
  75.                 {
  76.                     // No more space so stop reading
  77.                     break;
  78.                 }
  79.                 holdingBufferIndex = 0;
  80.             }
  81.         }
  82.     }
  83.  
  84.     *pDataSize = outputIndex;
  85. }
  86.  
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. //  main
  89. //
  90. //  Program entry point
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. int
  93.     main
  94.     (
  95.         int             ArgC,
  96.         char**          ArgV
  97.     )
  98. {
  99.     uint32_t        numBytes;
  100.     uint32_t        i;
  101.     uint8_t         buffer [BUFFER_SIZE];
  102.     uint32_t        amountLeft;
  103.     uint32_t        chunk;
  104.     AesCtrContext   aesCtr = {0};
  105.     uint8_t         key [AES_KEY_SIZE_256];
  106.     uint32_t        keySize = sizeof(key);
  107.     uint8_t         IV [AES_CTR_IV_SIZE];
  108.     uint32_t        IVSize = sizeof(IV);
  109.  
  110.     if( 4 != ArgC )
  111.     {
  112.         printf(
  113.             "Syntax\n"
  114.             "   AesCtrOutput <Key> <IV> <NumBytes>\n"
  115.             "     <Key> - 128, 192, or 256 bit written as hex\n"
  116.             "     <IV>  - 64 bit written as hex\n"
  117.             "     <NumBytes> - Number of bytes of stream to output\n" );
  118.         return 1;
  119.     }
  120.  
  121.     ReadHexData( ArgV[1], key, &keySize );
  122.     if( AES_KEY_SIZE_128 != keySize && AES_KEY_SIZE_192 != keySize && AES_KEY_SIZE_256 != keySize )
  123.     {
  124.         printf( "Invalid key size. Must be 128, 192, or 256 bits\n" );
  125.         return 1;
  126.     }
  127.  
  128.     ReadHexData( ArgV[2], IV, &IVSize );
  129.     if( AES_CTR_IV_SIZE != IVSize )
  130.     {
  131.         printf( "Invalid IV size. Must be 64 bits\n" );
  132.         return 1;
  133.     }
  134.  
  135.     numBytes = atoi( ArgV[3] );
  136.  
  137.     AesCtrInitialiseWithKey( key, keySize, IV, &aesCtr );
  138.  
  139.     amountLeft = numBytes;
  140.     while( amountLeft > 0 )
  141.     {
  142.         chunk = __min( amountLeft, BUFFER_SIZE );
  143.         AesCtrOutput( &aesCtr, buffer, chunk );
  144.         amountLeft -= chunk;
  145.  
  146.         for( i=0; i<chunk; i++ )
  147.         {
  148.             printf( "%2.2x", buffer[i] );
  149.         }
  150.     }
  151.  
  152.     printf( "\n" );
  153.  
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement