PageRenderTime 67ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/tnl/symmetricCipher.cpp

https://code.google.com/p/bitfighter/
C++ | 103 lines | 68 code | 9 blank | 26 comment | 6 complexity | 8fb6191a9e3c4ae2f73cdcb3649d9934 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.0, GPL-3.0
  1. //-----------------------------------------------------------------------------------
  2. //
  3. // Torque Network Library
  4. // Copyright (C) 2004 GarageGames.com, Inc.
  5. // For more information see http://www.opentnl.org
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // For use in products that are not compatible with the terms of the GNU
  13. // General Public License, alternative licensing options are available
  14. // from GarageGames.com.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program; if not, write to the Free Software
  23. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. //
  25. //------------------------------------------------------------------------------------
  26. #include "tnl.h"
  27. #include "tnlSymmetricCipher.h"
  28. #include "tnlByteBuffer.h"
  29. #include "../libtomcrypt/mycrypt.h"
  30. namespace TNL {
  31. SymmetricCipher::SymmetricCipher(const U8 symmetricKey[SymmetricCipher::KeySize], const U8 initVector[SymmetricCipher::BlockSize])
  32. {
  33. rijndael_setup(symmetricKey, KeySize, 0, &mSymmetricKey);
  34. memcpy(mInitVector, initVector, BlockSize);
  35. memcpy(mCounter, initVector, BlockSize);
  36. rijndael_ecb_encrypt((U8 *) mCounter, mPad, &mSymmetricKey);
  37. mPadLen = 0;
  38. }
  39. SymmetricCipher::SymmetricCipher(const ByteBuffer *theByteBuffer)
  40. {
  41. if(theByteBuffer->getBufferSize() != KeySize * 2)
  42. {
  43. U8 buffer[KeySize];
  44. memset(buffer, 0, KeySize);
  45. rijndael_setup(buffer, KeySize, 0, &mSymmetricKey);
  46. memcpy(mInitVector, buffer, BlockSize);
  47. }
  48. else
  49. {
  50. rijndael_setup(theByteBuffer->getBuffer(), KeySize, 0, &mSymmetricKey);
  51. memcpy(mInitVector, theByteBuffer->getBuffer() + KeySize, BlockSize);
  52. }
  53. memcpy(mCounter, mInitVector, BlockSize);
  54. rijndael_ecb_encrypt((U8 *) mCounter, mPad, &mSymmetricKey);
  55. mPadLen = 0;
  56. }
  57. void SymmetricCipher::setupCounter(U32 counterValue1, U32 counterValue2, U32 counterValue3, U32 counterValue4)
  58. {
  59. mCounter[0] = convertHostToLEndian(convertLEndianToHost(mInitVector[0]) + counterValue1);
  60. mCounter[1] = convertHostToLEndian(convertLEndianToHost(mInitVector[1]) + counterValue2);
  61. mCounter[2] = convertHostToLEndian(convertLEndianToHost(mInitVector[2]) + counterValue3);
  62. mCounter[3] = convertHostToLEndian(convertLEndianToHost(mInitVector[3]) + counterValue4);
  63. rijndael_ecb_encrypt((U8 *) mCounter, mPad, &mSymmetricKey);
  64. mPadLen = 0;
  65. }
  66. void SymmetricCipher::encrypt(const U8 *plainText, U8 *cipherText, U32 len)
  67. {
  68. while(len-- > 0)
  69. {
  70. if(mPadLen == BlockSize)
  71. {
  72. // we've reached the end of the pad, so compute a new pad
  73. rijndael_ecb_encrypt(mPad, mPad, &mSymmetricKey);
  74. mPadLen = 0;
  75. }
  76. U8 encryptedChar = *plainText++ ^ mPad[mPadLen];
  77. mPad[mPadLen++] = *cipherText++ = encryptedChar;
  78. }
  79. }
  80. void SymmetricCipher::decrypt(const U8 *cipherText, U8 *plainText, U32 len)
  81. {
  82. while(len-- > 0)
  83. {
  84. if(mPadLen == BlockSize)
  85. {
  86. rijndael_ecb_encrypt(mPad, mPad, &mSymmetricKey);
  87. mPadLen = 0;
  88. }
  89. U8 encryptedChar = *cipherText++;
  90. *plainText++ = encryptedChar ^ mPad[mPadLen];
  91. mPad[mPadLen++] = encryptedChar;
  92. }
  93. }
  94. };