Advertisement
waterjuice

CryptLib_AesCtr.h

Nov 28th, 2017
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.73 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //  CryptLib_AesCtr
  3. //
  4. //  Implementation of AES CTR stream cipher.
  5. //
  6. //  Depends on: CryptoLib_Aes
  7. //
  8. //  AES CTR is a stream cipher using the AES block cipher in counter mode.
  9. //  This implementation works on both little and big endian architectures.
  10. //
  11. //  This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13.  
  14. #pragma once
  15.  
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. //  IMPORTS
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19.  
  20. #include <stdint.h>
  21. #include "CryptLib_Aes.h"
  22.  
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. //  TYPES
  25. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  26.  
  27. #define AES_CTR_IV_SIZE             8
  28.  
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. //  TYPES
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32.  
  33. // AesCtrContext
  34. // Do not modify the contents of this structure directly.
  35. typedef struct
  36. {
  37.     AesContext      Aes;
  38.     uint8_t         IV [AES_CTR_IV_SIZE];
  39.     uint64_t        StreamIndex;
  40.     uint64_t        CurrentCipherBlockIndex;
  41.     uint8_t         CurrentCipherBlock [AES_BLOCK_SIZE];
  42. } AesCtrContext;
  43.  
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. //  PUBLIC FUNCTIONS
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47.  
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. //  AesCtrInitialise
  50. //
  51. //  Initialises an AesCtrContext with an already initialised AesContext and a IV. This function can quickly be used
  52. //  to change the IV without requiring the more length processes of reinitialising an AES key.
  53. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. void
  55.     AesCtrInitialise
  56.     (
  57.         AesContext const*   InitialisedAesContext,  // [in]
  58.         uint8_t const       IV [AES_CTR_IV_SIZE],   // [in]
  59.         AesCtrContext*      Context                 // [out]
  60.     );
  61.  
  62. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. //  AesCtrInitialiseWithKey
  64. //
  65. //  Initialises an AesCtrContext with an AES Key and an IV. This combines the initialising an AES Context and then
  66. //  running AesCtrInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
  67. //  Returns 0 if successful, or -1 if invalid KeySize provided
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. int
  70.     AesCtrInitialiseWithKey
  71.     (
  72.         uint8_t const*      Key,                    // [in]
  73.         uint32_t            KeySize,                // [in]
  74.         uint8_t const       IV [AES_CTR_IV_SIZE],   // [in]
  75.         AesCtrContext*      Context                 // [out]
  76.     );
  77.  
  78. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. //  AesCtrSetStreamIndex
  80. //
  81. //  Sets the current stream index to any arbitrary position. Setting to 0 sets it to the beginning of the stream. Any
  82. //  subsequent output will start from this position
  83. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  84. void
  85.     AesCtrSetStreamIndex
  86.     (
  87.         AesCtrContext*      Context,                // [in out]
  88.         uint64_t            StreamIndex             // [in]
  89.     );
  90.  
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. //  AesCtrXor
  93. //
  94. //  XORs the stream of byte of the AesCtrContext from its current stream position onto the specified buffer. This will
  95. //  advance the stream index by that number of bytes.
  96. //  Use once over data to encrypt it. Use it a second time over the same data from the same stream position and the
  97. //  data will be decrypted.
  98. //  InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
  99. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. void
  101.     AesCtrXor
  102.     (
  103.         AesCtrContext*      Context,                // [in out]
  104.         void const*         InBuffer,               // [in]
  105.         void*               OutBuffer,              // [out]
  106.         uint32_t            Size                    // [in]
  107.     );
  108.  
  109. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. //  AesCtrOutput
  111. //
  112. //  Outputs the stream of byte of the AesCtrContext from its current stream position. This will advance the stream
  113. //  index by that number of bytes.
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. void
  116.     AesCtrOutput
  117.     (
  118.         AesCtrContext*      Context,                // [in out]
  119.         void*               Buffer,                 // [out]
  120.         uint32_t            Size                    // [in]
  121.     );
  122.  
  123. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. //  AesCtrXorWithKey
  125. //
  126. //  This function combines AesCtrInitialiseWithKey and AesCtrXor. This is suitable when encrypting/decypting data in
  127. //  one go with a key that is not going to be reused.
  128. //  This will used the provided Key and IV and generate a stream that is XORed over Buffer.
  129. //  Returns 0 if successful, or -1 if invalid KeySize provided
  130. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. int
  132.     AesCtrXorWithKey
  133.     (
  134.         uint8_t const*      Key,                    // [in]
  135.         uint32_t            KeySize,                // [in]
  136.         uint8_t const       IV [AES_CTR_IV_SIZE],   // [in]
  137.         void const*         InBuffer,               // [in]
  138.         void*               OutBuffer,              // [out]
  139.         uint32_t            BufferSize              // [in]
  140.     );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement