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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 uint8_t* key,
  28. int _keySize)
  29. : keySize(_keySize), out(_out), counter()
  30. {
  31. msg = new uint8_t[MaxMessageSize + 16 + 2];
  32. if (keySize == 128)
  33. EAX_SET_KEY(&eaxCtx128, aes128_set_encrypt_key, aes128_encrypt, key);
  34. else if (keySize == 256)
  35. EAX_SET_KEY(&eaxCtx256, aes256_set_encrypt_key, aes256_encrypt, key);
  36. else
  37. assert(!"incorrect key size");
  38. }
  39. AESOutStream::~AESOutStream()
  40. {
  41. delete[] msg;
  42. }
  43. void AESOutStream::flush()
  44. {
  45. BufferedOutStream::flush();
  46. out->flush();
  47. }
  48. void AESOutStream::cork(bool enable)
  49. {
  50. BufferedOutStream::cork(enable);
  51. out->cork(enable);
  52. }
  53. bool AESOutStream::flushBuffer()
  54. {
  55. while (sentUpTo < ptr) {
  56. size_t n = ptr - sentUpTo;
  57. if (n > MaxMessageSize)
  58. n = MaxMessageSize;
  59. writeMessage(sentUpTo, n);
  60. sentUpTo += n;
  61. }
  62. return true;
  63. }
  64. void AESOutStream::writeMessage(const uint8_t* data, size_t length)
  65. {
  66. msg[0] = (length & 0xff00) >> 8;
  67. msg[1] = length & 0xff;
  68. if (keySize == 128) {
  69. EAX_SET_NONCE(&eaxCtx128, aes128_encrypt, 16, counter);
  70. EAX_UPDATE(&eaxCtx128, aes128_encrypt, 2, msg);
  71. EAX_ENCRYPT(&eaxCtx128, aes128_encrypt, length, msg + 2, data);
  72. EAX_DIGEST(&eaxCtx128, aes128_encrypt, 16, msg + 2 + length);
  73. } else {
  74. EAX_SET_NONCE(&eaxCtx256, aes256_encrypt, 16, counter);
  75. EAX_UPDATE(&eaxCtx256, aes256_encrypt, 2, msg);
  76. EAX_ENCRYPT(&eaxCtx256, aes256_encrypt, length, msg + 2, data);
  77. EAX_DIGEST(&eaxCtx256, aes256_encrypt, 16, msg + 2 + length);
  78. }
  79. out->writeBytes(msg, 2 + length + 16);
  80. out->flush();
  81. // Update nonce by incrementing the counter as a
  82. // 128bit little endian unsigned integer
  83. for (int i = 0; i < 16; ++i) {
  84. // increment until there is no carry
  85. if (++counter[i] != 0) {
  86. break;
  87. }
  88. }
  89. }
  90. #endif