Advertisement
Guest User

CryptLib_Aes.h

a guest
Nov 23rd, 2017
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.67 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //  CryptLib_Aes
  3. //
  4. //  Implementation of AES block cipher. Originally written by Kokke (https://github.com/kokke). Modified by WaterJuice
  5. //  retaining Public Domain license.
  6. //
  7. //  AES is a block cipher that operates on 128 bit blocks. Encryption an Decryption routines use an AesContext which
  8. //  must be initialised with the key. An AesContext can be initialised with a 128, 192, or 256 bit key. Use the
  9. //  AesInitialise[n] functions to initialise the context with the key. Once an AES context is initialised its contents
  10. //  are not changed by the encrypting and decrypting functions. A context only needs to be initialised once for any
  11. //  given key and the context may be used by the encrypt/decrypt functions in simultaneous threads.
  12. //  All operations are performed byte wise and this implementation works in both little and endian processors.
  13. //  There are no alignment requirements with the keys and data blocks.
  14. //
  15. //  This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17.  
  18. #pragma once
  19.  
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. //  IMPORTS
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  23.  
  24. #include <stdint.h>
  25.  
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27. //  TYPES
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29.  
  30. #define AES_KEY_SIZE_128        16
  31. #define AES_KEY_SIZE_192        24
  32. #define AES_KEY_SIZE_256        32
  33. #define AES_BLOCK_SIZE          16
  34.  
  35. // AesContext - This must be initialised using AesInitialise128, AesInitialise192 or AesInitialise256
  36. // Do not modify the contents of this structure directly.
  37. typedef struct
  38. {
  39.     uint32_t    KeySizeInWords;
  40.     uint32_t    NumberOfRounds;
  41.     uint8_t     RoundKey[240];
  42. } AesContext;
  43.  
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. //  PUBLIC FUNCTIONS
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47.  
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. //  AesInitialise128
  50. //
  51. //  Initialises an AesContext with a 128 bit key.
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. void
  54.     AesInitialise128
  55.     (
  56.         uint8_t const   Key [AES_KEY_SIZE_128],         // [in]
  57.         AesContext*     Context                         // [out]
  58.     );
  59.  
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. //  AesInitialise192
  62. //
  63. //  Initialises an AesContext with a 192 bit key.
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. void
  66.     AesInitialise192
  67.     (
  68.         uint8_t const   Key [AES_KEY_SIZE_192],         // [in]
  69.         AesContext*     Context                         // [out]
  70.     );
  71.  
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. //  AesInitialise256
  74. //
  75. //  Initialises an AesContext with a 256 bit key.
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77. void
  78.     AesInitialise256
  79.     (
  80.         uint8_t const   Key [AES_KEY_SIZE_256],         // [in]
  81.         AesContext*     Context                         // [out]
  82.     );
  83.  
  84. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. //  AesEncrypt
  86. //
  87. //  Performs an AES encryption of one block (128 bits) with the AesContext initialised with one of the functions
  88. //  AesInitialise[n]. Input and Output can point to same memory location, however it is more efficient to use
  89. //  AesEncryptInPlace in this situation.
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. void
  92.     AesEncrypt
  93.     (
  94.         AesContext const*   Context,                    // [in]
  95.         uint8_t const       Input [AES_BLOCK_SIZE],     // [in]
  96.         uint8_t             Output [AES_BLOCK_SIZE]     // [out]
  97.     );
  98.  
  99. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. //  AesDecrypt
  101. //
  102. //  Performs an AES decryption of one block (128 bits) with the AesContext initialised with one of the functions
  103. //  AesInitialise[n]. Input and Output can point to same memory location, however it is more efficient to use
  104. //  AesDecryptInPlace in this situation.
  105. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. void
  107.     AesDecrypt
  108.     (
  109.         AesContext const*   Context,                    // [in]
  110.         uint8_t const       Input [AES_BLOCK_SIZE],     // [in]
  111.         uint8_t             Output [AES_BLOCK_SIZE]     // [out]
  112.     );
  113.  
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. //  AesEncryptInPlace
  116. //
  117. //  Performs an AES encryption of one block (128 bits) with the AesContext initialised with one of the functions
  118. //  AesInitialise[n]. The encryption is performed in place.
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. void
  121.     AesEncryptInPlace
  122.     (
  123.         AesContext const*   Context,                    // [in]
  124.         uint8_t             Block [AES_BLOCK_SIZE]      // [in out]
  125.     );
  126.  
  127. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. //  AesDecryptInPlace
  129. //
  130. //  Performs an AES decryption of one block (128 bits) with the AesContext initialised with one of the functions
  131. //  AesInitialise[n]. The decryption is performed in place.
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. void
  134.     AesDecryptInPlace
  135.     (
  136.         AesContext const*   Context,                    // [in]
  137.         uint8_t             Block [AES_BLOCK_SIZE]      // [in out]
  138.     );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement