/indra/llmessage/llxorcipher.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 128 lines · 82 code · 15 blank · 31 comment · 16 complexity · 06ba9f24dc31a42c1edcb2c4c1bd6c1e MD5 · raw file

  1. /**
  2. * @file llxorcipher.cpp
  3. * @brief Implementation of LLXORCipher
  4. *
  5. * $LicenseInfo:firstyear=2003&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library 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 GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llxorcipher.h"
  28. #include "llerror.h"
  29. ///----------------------------------------------------------------------------
  30. /// Class LLXORCipher
  31. ///----------------------------------------------------------------------------
  32. LLXORCipher::LLXORCipher(const U8* pad, U32 pad_len) :
  33. mPad(NULL),
  34. mHead(NULL),
  35. mPadLen(0)
  36. {
  37. init(pad, pad_len);
  38. }
  39. // Destroys the object
  40. LLXORCipher::~LLXORCipher()
  41. {
  42. init(NULL, 0);
  43. }
  44. LLXORCipher::LLXORCipher(const LLXORCipher& cipher) :
  45. mPad(NULL),
  46. mHead(NULL),
  47. mPadLen(0)
  48. {
  49. init(cipher.mPad, cipher.mPadLen);
  50. }
  51. LLXORCipher& LLXORCipher::operator=(const LLXORCipher& cipher)
  52. {
  53. if(this == &cipher) return *this;
  54. init(cipher.mPad, cipher.mPadLen);
  55. return *this;
  56. }
  57. U32 LLXORCipher::encrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
  58. {
  59. if(!src || !src_len || !dst || !dst_len || !mPad) return 0;
  60. U8* pad_end = mPad + mPadLen;
  61. U32 count = src_len;
  62. while(count--)
  63. {
  64. *dst++ = *src++ ^ *mHead++;
  65. if(mHead >= pad_end) mHead = mPad;
  66. }
  67. return src_len;
  68. }
  69. U32 LLXORCipher::decrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
  70. {
  71. // xor is a symetric cipher, thus, just call the other function.
  72. return encrypt(src, src_len, dst, dst_len);
  73. }
  74. U32 LLXORCipher::requiredEncryptionSpace(U32 len) const
  75. {
  76. return len;
  77. }
  78. void LLXORCipher::init(const U8* pad, U32 pad_len)
  79. {
  80. if(mPad)
  81. {
  82. delete [] mPad;
  83. mPad = NULL;
  84. mPadLen = 0;
  85. }
  86. if(pad && pad_len)
  87. {
  88. mPadLen = pad_len;
  89. mPad = new U8[mPadLen];
  90. if (mPad != NULL)
  91. {
  92. memcpy(mPad, pad, mPadLen); /* Flawfinder : ignore */
  93. }
  94. }
  95. mHead = mPad;
  96. }
  97. #ifdef _DEBUG
  98. // static
  99. BOOL LLXORCipher::testHarness()
  100. {
  101. const U32 PAD_LEN = 3;
  102. const U8 PAD[] = "abc";
  103. const S32 MSG_LENGTH = 12;
  104. const char MESSAGE[MSG_LENGTH+1] = "gesundheight"; /* Flawfinder : ignore */
  105. U8 encrypted[MSG_LENGTH];
  106. U8 decrypted[MSG_LENGTH];
  107. LLXORCipher cipher(PAD, PAD_LEN);
  108. cipher.encrypt((U8*)MESSAGE, MSG_LENGTH, encrypted, MSG_LENGTH);
  109. cipher.decrypt(encrypted, MSG_LENGTH, decrypted, MSG_LENGTH);
  110. if(0 != memcmp((void*)MESSAGE, decrypted, MSG_LENGTH)) return FALSE;
  111. return TRUE;
  112. }
  113. #endif