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.

AESOutStream.cxx 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 2022 Dinglan Peng
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include <rdr/Exception.h>
  23. #include <rdr/AESOutStream.h>
  24. #ifdef HAVE_NETTLE
  25. using namespace rdr;
  26. const int MaxMessageSize = 8192;
  27. AESOutStream::AESOutStream(OutStream* _out, const U8* key, int _keySize)
  28. : keySize(_keySize), out(_out), counter()
  29. {
  30. msg = new U8[MaxMessageSize + 16 + 2];
  31. if (keySize == 128)
  32. EAX_SET_KEY(&eaxCtx128, aes128_set_encrypt_key, aes128_encrypt, key);
  33. else if (keySize == 256)
  34. EAX_SET_KEY(&eaxCtx256, aes256_set_encrypt_key, aes256_encrypt, key);
  35. else
  36. assert(!"incorrect key size");
  37. }
  38. AESOutStream::~AESOutStream()
  39. {
  40. delete[] msg;
  41. }
  42. void AESOutStream::flush()
  43. {
  44. BufferedOutStream::flush();
  45. out->flush();
  46. }
  47. void AESOutStream::cork(bool enable)
  48. {
  49. BufferedOutStream::cork(enable);
  50. out->cork(enable);
  51. }
  52. bool AESOutStream::flushBuffer()
  53. {
  54. while (sentUpTo < ptr) {
  55. size_t n = ptr - sentUpTo;
  56. if (n > MaxMessageSize)
  57. n = MaxMessageSize;
  58. writeMessage(sentUpTo, n);
  59. sentUpTo += n;
  60. }
  61. return true;
  62. }
  63. void AESOutStream::writeMessage(const U8* data, size_t length)
  64. {
  65. msg[0] = (length & 0xff00) >> 8;
  66. msg[1] = length & 0xff;
  67. if (keySize == 128) {
  68. EAX_SET_NONCE(&eaxCtx128, aes128_encrypt, 16, counter);
  69. EAX_UPDATE(&eaxCtx128, aes128_encrypt, 2, msg);
  70. EAX_ENCRYPT(&eaxCtx128, aes128_encrypt, length, msg + 2, data);
  71. EAX_DIGEST(&eaxCtx128, aes128_encrypt, 16, msg + 2 + length);
  72. } else {
  73. EAX_SET_NONCE(&eaxCtx256, aes256_encrypt, 16, counter);
  74. EAX_UPDATE(&eaxCtx256, aes256_encrypt, 2, msg);
  75. EAX_ENCRYPT(&eaxCtx256, aes256_encrypt, length, msg + 2, data);
  76. EAX_DIGEST(&eaxCtx256, aes256_encrypt, 16, msg + 2 + length);
  77. }
  78. out->writeBytes(msg, 2 + length + 16);
  79. out->flush();
  80. // Update nonce by incrementing the counter as a
  81. // 128bit little endian unsigned integer
  82. for (int i = 0; i < 16; ++i) {
  83. // increment until there is no carry
  84. if (++counter[i] != 0) {
  85. break;
  86. }
  87. }
  88. }
  89. #endif