You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aes.h 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2015-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. //
  5. // C++ wrapper around ctaes, a constant-time AES implementation
  6. #ifndef STARWELS_CRYPTO_AES_H
  7. #define STARWELS_CRYPTO_AES_H
  8. extern "C" {
  9. #include "crypto/ctaes/ctaes.h"
  10. }
  11. static const int AES_BLOCKSIZE = 16;
  12. static const int AES128_KEYSIZE = 16;
  13. static const int AES256_KEYSIZE = 32;
  14. /** An encryption class for AES-128. */
  15. class AES128Encrypt
  16. {
  17. private:
  18. AES128_ctx ctx;
  19. public:
  20. AES128Encrypt(const unsigned char key[16]);
  21. ~AES128Encrypt();
  22. void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
  23. };
  24. /** A decryption class for AES-128. */
  25. class AES128Decrypt
  26. {
  27. private:
  28. AES128_ctx ctx;
  29. public:
  30. AES128Decrypt(const unsigned char key[16]);
  31. ~AES128Decrypt();
  32. void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
  33. };
  34. /** An encryption class for AES-256. */
  35. class AES256Encrypt
  36. {
  37. private:
  38. AES256_ctx ctx;
  39. public:
  40. AES256Encrypt(const unsigned char key[32]);
  41. ~AES256Encrypt();
  42. void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
  43. };
  44. /** A decryption class for AES-256. */
  45. class AES256Decrypt
  46. {
  47. private:
  48. AES256_ctx ctx;
  49. public:
  50. AES256Decrypt(const unsigned char key[32]);
  51. ~AES256Decrypt();
  52. void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
  53. };
  54. class AES256CBCEncrypt
  55. {
  56. public:
  57. AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
  58. ~AES256CBCEncrypt();
  59. int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
  60. private:
  61. const AES256Encrypt enc;
  62. const bool pad;
  63. unsigned char iv[AES_BLOCKSIZE];
  64. };
  65. class AES256CBCDecrypt
  66. {
  67. public:
  68. AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
  69. ~AES256CBCDecrypt();
  70. int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
  71. private:
  72. const AES256Decrypt dec;
  73. const bool pad;
  74. unsigned char iv[AES_BLOCKSIZE];
  75. };
  76. class AES128CBCEncrypt
  77. {
  78. public:
  79. AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
  80. ~AES128CBCEncrypt();
  81. int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
  82. private:
  83. const AES128Encrypt enc;
  84. const bool pad;
  85. unsigned char iv[AES_BLOCKSIZE];
  86. };
  87. class AES128CBCDecrypt
  88. {
  89. public:
  90. AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
  91. ~AES128CBCDecrypt();
  92. int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
  93. private:
  94. const AES128Decrypt dec;
  95. const bool pad;
  96. unsigned char iv[AES_BLOCKSIZE];
  97. };
  98. #endif // STARWELS_CRYPTO_AES_H