aes.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* aes.h
  4. *
  5. * Copyright (C) 2006-2014 wolfSSL Inc.
  6. *
  7. * This file is part of CyaSSL.
  8. *
  9. * CyaSSL is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * CyaSSL is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef CTAO_CRYPT_AES_H
  24. #define CTAO_CRYPT_AES_H
  25. #include <stdint.h>
  26. #ifndef word32
  27. typedef uint32_t word32;
  28. #endif
  29. #ifndef byte
  30. typedef uint8_t byte;
  31. #endif
  32. #ifndef word
  33. #ifdef __x86_64__
  34. typedef uint64_t word;
  35. #else
  36. typedef uint32_t word;
  37. #endif
  38. #endif
  39. #define WORD_SIZE sizeof(word)
  40. #define ALIGN16 __attribute__((aligned (16)))
  41. enum {
  42. AES_ENC_TYPE = 1, /* cipher unique type */
  43. AES_ENCRYPTION = 0,
  44. AES_DECRYPTION = 1,
  45. AES_BLOCK_SIZE = 16
  46. };
  47. typedef struct AES {
  48. /* AESNI needs key first, rounds 2nd, not sure why yet */
  49. ALIGN16 word32 key[60];
  50. word32 rounds;
  51. ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
  52. ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
  53. word32 left;
  54. ALIGN16 byte H[AES_BLOCK_SIZE];
  55. /* key-based fast multiplication table. */
  56. ALIGN16 byte M0[256][AES_BLOCK_SIZE];
  57. } AES;
  58. int AESSetKey(AES *aes, const byte *key, word32 len, const byte *iv,
  59. int dir);
  60. int AESSetIV(AES *aes, const byte *iv);
  61. void AESEncrypt(AES *aes, const byte *in, byte *out);
  62. void AESDecrypt(AES *aes, const byte *in, byte *out);
  63. int AESCBCEncrypt(AES *aes, byte *out, const byte *in, word32 sz);
  64. int AESCBCDecrypt(AES *aes, byte *out, const byte *in, word32 sz);
  65. int AESCBCDecryptWithKey(byte *out, const byte *in, word32 inSz,
  66. const byte *key, word32 keySz, const byte *iv);
  67. void AESCTREncrypt(AES *aes, byte *out, const byte *in, word32 sz);
  68. int AESGCMSetKey(AES *aes, const byte *key, word32 len);
  69. int AESGCMEncrypt(AES *aes, byte *out, const byte *in, word32 sz,
  70. const byte *iv, word32 ivSz,
  71. byte* authTag, word32 authTagSz,
  72. const byte *authIn, word32 authInSz);
  73. int AESGCMDecrypt(AES *aes, byte *out, const byte *in, word32 sz,
  74. const byte *iv, word32 ivSz,
  75. const byte *authTag, word32 authTagSz,
  76. const byte *authIn, word32 authInSz);
  77. #endif /* CTAO_CRYPT_AES_H */