daily pastebin goal
11%
SHARE
TWEET

CryptLib_Aes.h

a guest Dec 10th, 2017 326 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //  CryptLib_Aes
  3. //
  4. //  Implementation of AES block cipher. This implementation was modified from LibTomCrypt written by Tom St Denis
  5. //  (https://github.com/libtom). Modified by WaterJuice retaining Public Domain license.
  6. //  Derived from Public Domain source by original authors:
  7. //      Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
  8. //      Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
  9. //      Paulo Barreto <paulo.barreto@terra.com.br>
  10. //
  11. //  AES is a block cipher that operates on 128 bit blocks. Encryption an Decryption routines use an AesContext which
  12. //  must be initialised with the key. An AesContext can be initialised with a 128, 192, or 256 bit key. Use the
  13. //  AesInitialise[n] functions to initialise the context with the key. Once an AES context is initialised its contents
  14. //  are not changed by the encrypting and decrypting functions. A context only needs to be initialised once for any
  15. //  given key and the context may be used by the encrypt/decrypt functions in simultaneous threads.
  16. //  All operations are performed BYTE wise and this implementation works in both little and endian processors.
  17. //  There are no alignment requirements with the keys and data blocks.
  18. //
  19. //  This is free and unencumbered software released into the public domain - December 2017 waterjuice.org
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21.  
  22. #pragma once
  23.  
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  25. //  IMPORTS
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27.  
  28. #include <stdint.h>
  29.  
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //  TYPES
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33.  
  34. #define AES_KEY_SIZE_128        16
  35. #define AES_KEY_SIZE_192        24
  36. #define AES_KEY_SIZE_256        32
  37. #define AES_BLOCK_SIZE          16
  38.  
  39. // AesContext - This must be initialised using AesInitialise128, AesInitialise192 or AesInitialise256
  40. // Do not modify the contents of this structure directly.
  41. typedef struct
  42. {
  43.     uint32_t        eK[60];
  44.     uint32_t        dK[60];
  45.     uint_fast32_t   Nr;
  46. } AesContext;
  47.  
  48. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  49. //  PUBLIC FUNCTIONS
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51.  
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  53. //  AesInitialise
  54. //
  55. //  Initialises an AesContext with an AES Key. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
  56. //  Returns 0 if successful, or -1 if invalid KeySize provided
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. int
  59.     AesInitialise
  60.     (
  61.         AesContext*         Context,                // [out]
  62.         void const*         Key,                    // [in]
  63.         uint32_t            KeySize                 // [in]
  64.     );
  65.  
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. //  AesEncrypt
  68. //
  69. //  Performs an AES encryption of one block (128 bits) with the AesContext initialised with one of the functions
  70. //  AesInitialise[n]. Input and Output can point to same memory location, however it is more efficient to use
  71. //  AesEncryptInPlace in this situation.
  72. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. void
  74.     AesEncrypt
  75.     (
  76.         AesContext const*   Context,                    // [in]
  77.         uint8_t const       Input [AES_BLOCK_SIZE],     // [in]
  78.         uint8_t             Output [AES_BLOCK_SIZE]     // [out]
  79.     );
  80.  
  81. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  82. //  AesDecrypt
  83. //
  84. //  Performs an AES decryption of one block (128 bits) with the AesContext initialised with one of the functions
  85. //  AesInitialise[n]. Input and Output can point to same memory location, however it is more efficient to use
  86. //  AesDecryptInPlace in this situation.
  87. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. void
  89.     AesDecrypt
  90.     (
  91.         AesContext const*   Context,                    // [in]
  92.         uint8_t const       Input [AES_BLOCK_SIZE],     // [in]
  93.         uint8_t             Output [AES_BLOCK_SIZE]     // [out]
  94.     );
  95.  
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. //  AesEncryptInPlace
  98. //
  99. //  Performs an AES encryption of one block (128 bits) with the AesContext initialised with one of the functions
  100. //  AesInitialise[n]. The encryption is performed in place.
  101. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. void
  103.     AesEncryptInPlace
  104.     (
  105.         AesContext const*   Context,                    // [in]
  106.         uint8_t             Block [AES_BLOCK_SIZE]      // [in out]
  107.     );
  108.  
  109. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  110. //  AesDecryptInPlace
  111. //
  112. //  Performs an AES decryption of one block (128 bits) with the AesContext initialised with one of the functions
  113. //  AesInitialise[n]. The decryption is performed in place.
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. void
  116.     AesDecryptInPlace
  117.     (
  118.         AesContext const*   Context,                    // [in]
  119.         uint8_t             Block [AES_BLOCK_SIZE]      // [in out]
  120.     );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top