aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llmessage/llxorcipher.cpp
diff options
context:
space:
mode:
authorJacek Antonelli2008-08-15 23:44:46 -0500
committerJacek Antonelli2008-08-15 23:44:46 -0500
commit38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch)
treeadca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llmessage/llxorcipher.cpp
parentREADME.txt (diff)
downloadmeta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2
meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz
Second Life viewer sources 1.13.2.12
Diffstat (limited to '')
-rw-r--r--linden/indra/llmessage/llxorcipher.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llxorcipher.cpp b/linden/indra/llmessage/llxorcipher.cpp
new file mode 100644
index 0000000..7a72866
--- /dev/null
+++ b/linden/indra/llmessage/llxorcipher.cpp
@@ -0,0 +1,127 @@
1/**
2 * @file llxorcipher.cpp
3 * @brief Implementation of LLXORCipher
4 *
5 * Copyright (c) 2003-2007, Linden Research, Inc.
6 *
7 * The source code in this file ("Source Code") is provided by Linden Lab
8 * to you under the terms of the GNU General Public License, version 2.0
9 * ("GPL"), unless you have obtained a separate licensing agreement
10 * ("Other License"), formally executed by you and Linden Lab. Terms of
11 * the GPL can be found in doc/GPL-license.txt in this distribution, or
12 * online at http://secondlife.com/developers/opensource/gplv2
13 *
14 * There are special exceptions to the terms and conditions of the GPL as
15 * it is applied to this Source Code. View the full text of the exception
16 * in the file doc/FLOSS-exception.txt in this software distribution, or
17 * online at http://secondlife.com/developers/opensource/flossexception
18 *
19 * By copying, modifying or distributing this software, you acknowledge
20 * that you have read and understood your obligations described above,
21 * and agree to abide by those obligations.
22 *
23 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
24 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
25 * COMPLETENESS OR PERFORMANCE.
26 */
27
28#include "linden_common.h"
29
30#include "llcrypto.h"
31#include "llerror.h"
32
33///----------------------------------------------------------------------------
34/// Class LLXORCipher
35///----------------------------------------------------------------------------
36
37LLXORCipher::LLXORCipher(const U8* pad, U32 pad_len) :
38 mPad(NULL),
39 mHead(NULL),
40 mPadLen(0)
41{
42 init(pad, pad_len);
43}
44
45// Destroys the object
46LLXORCipher::~LLXORCipher()
47{
48 init(NULL, 0);
49}
50
51LLXORCipher::LLXORCipher(const LLXORCipher& cipher) :
52 mPad(NULL),
53 mHead(NULL),
54 mPadLen(0)
55{
56 init(cipher.mPad, cipher.mPadLen);
57}
58
59LLXORCipher& LLXORCipher::operator=(const LLXORCipher& cipher)
60{
61 if(this == &cipher) return *this;
62 init(cipher.mPad, cipher.mPadLen);
63 return *this;
64}
65
66BOOL LLXORCipher::encrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
67{
68 if(!src || !src_len || !dst || !dst_len || !mPad) return FALSE;
69 U8* pad_end = mPad + mPadLen;
70 while(src_len--)
71 {
72 *dst++ = *src++ ^ *mHead++;
73 if(mHead >= pad_end) mHead = mPad;
74 }
75 return TRUE;
76}
77
78BOOL LLXORCipher::decrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
79{
80 // xor is a symetric cipher, thus, just call the other function.
81 return encrypt(src, src_len, dst, dst_len);
82}
83
84U32 LLXORCipher::requiredEncryptionSpace(U32 len)
85{
86 return len;
87}
88
89void LLXORCipher::init(const U8* pad, U32 pad_len)
90{
91 if(mPad)
92 {
93 delete [] mPad;
94 mPad = NULL;
95 mPadLen = 0;
96 }
97 if(pad && pad_len)
98 {
99 mPadLen = pad_len;
100 mPad = new U8[mPadLen];
101 if (mPad != NULL)
102 {
103 memcpy(mPad, pad, mPadLen); /* Flawfinder : ignore */
104 }
105 }
106 mHead = mPad;
107}
108
109#ifdef _DEBUG
110// static
111BOOL LLXORCipher::testHarness()
112{
113 const U32 PAD_LEN = 3;
114 const U8 PAD[] = "abc";
115 const S32 MSG_LENGTH = 12;
116 const char MESSAGE[MSG_LENGTH+1] = "gesundheight"; /* Flawfinder : ignore */
117 U8 encrypted[MSG_LENGTH];
118 U8 decrypted[MSG_LENGTH];
119
120 LLXORCipher cipher(PAD, PAD_LEN);
121 cipher.encrypt((U8*)MESSAGE, MSG_LENGTH, encrypted, MSG_LENGTH);
122 cipher.decrypt(encrypted, MSG_LENGTH, decrypted, MSG_LENGTH);
123
124 if(0 != memcmp((void*)MESSAGE, decrypted, MSG_LENGTH)) return FALSE;
125 return TRUE;
126}
127#endif