aes.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /******************************************************************************
  2. *
  3. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  4. *
  5. * This is a simple and straightforward implementation of the AES Rijndael
  6. * 128-bit block cipher designed by Vincent Rijmen and Joan Daemen. The focus
  7. * of this work was correctness & accuracy. It is written in 'C' without any
  8. * particular focus upon optimization or speed. It should be endian (memory
  9. * byte order) neutral since the few places that care are handled explicitly.
  10. *
  11. * This implementation of Rijndael was created by Steven M. Gibson of GRC.com.
  12. *
  13. * It is intended for general purpose use, but was written in support of GRC's
  14. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  15. *
  16. * See: http://csrc.nist.gov/archive/aes/rijndael/wsdindex.html
  17. *
  18. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  19. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  20. *
  21. *******************************************************************************/
  22. #ifndef AES_HEADER
  23. #define AES_HEADER
  24. /******************************************************************************/
  25. #define AES_DECRYPTION 0 // whether AES decryption is supported
  26. /******************************************************************************/
  27. #include <string.h>
  28. #define ENCRYPT 1 // specify whether we're encrypting
  29. #define DECRYPT 0 // or decrypting
  30. #if defined(_MSC_VER)
  31. #include <basetsd.h>
  32. typedef UINT32 uint32_t;
  33. #else
  34. #include <inttypes.h>
  35. #endif
  36. typedef unsigned char uchar; // add some convienent shorter types
  37. typedef unsigned int uint;
  38. /******************************************************************************
  39. * AES_INIT_KEYGEN_TABLES : MUST be called once before any AES use
  40. ******************************************************************************/
  41. void aes_init_keygen_tables(void);
  42. /******************************************************************************
  43. * AES_CONTEXT : cipher context / holds inter-call data
  44. ******************************************************************************/
  45. typedef struct {
  46. int mode; // 1 for Encryption, 0 for Decryption
  47. int rounds; // keysize-based rounds count
  48. uint32_t* rk; // pointer to current round key
  49. uint32_t buf[68]; // key expansion buffer
  50. } aes_context;
  51. /******************************************************************************
  52. * AES_SETKEY : called to expand the key for encryption or decryption
  53. ******************************************************************************/
  54. int aes_setkey(
  55. aes_context* ctx, // pointer to context
  56. int mode, // 1 or 0 for Encrypt/Decrypt
  57. const uchar* key, // AES input key
  58. uint keysize); // size in bytes (must be 16, 24, 32 for
  59. // 128, 192 or 256-bit keys respectively)
  60. // returns 0 for success
  61. /******************************************************************************
  62. * AES_CIPHER : called to encrypt or decrypt ONE 128-bit block of data
  63. ******************************************************************************/
  64. int aes_cipher(
  65. aes_context* ctx, // pointer to context
  66. const uchar input[16], // 128-bit block to en/decipher
  67. uchar output[16]); // 128-bit output result block
  68. // returns 0 for success
  69. #endif /* AES_HEADER */