PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/llxorcipher.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 128 lines | 82 code | 15 blank | 31 comment | 15 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
 27#include "linden_common.h"
 28
 29#include "llxorcipher.h"
 30
 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
 66U32 LLXORCipher::encrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
 67{
 68	if(!src || !src_len || !dst || !dst_len || !mPad) return 0;
 69	U8* pad_end = mPad + mPadLen;
 70	U32 count = src_len;
 71	while(count--)
 72	{
 73		*dst++ = *src++ ^ *mHead++;
 74		if(mHead >= pad_end) mHead = mPad;
 75	}
 76	return src_len;
 77}
 78
 79U32 LLXORCipher::decrypt(const U8* src, U32 src_len, U8* dst, U32 dst_len)
 80{
 81	// xor is a symetric cipher, thus, just call the other function.
 82	return encrypt(src, src_len, dst, dst_len);
 83}
 84
 85U32 LLXORCipher::requiredEncryptionSpace(U32 len) const
 86{
 87	return len;
 88}
 89
 90void LLXORCipher::init(const U8* pad, U32 pad_len)
 91{
 92	if(mPad)
 93	{
 94		delete [] mPad;
 95		mPad = NULL;
 96		mPadLen = 0;
 97	}
 98	if(pad && pad_len)
 99	{
100		mPadLen = pad_len;
101		mPad = new U8[mPadLen];
102		if (mPad != NULL)
103		{
104			memcpy(mPad, pad, mPadLen); /* Flawfinder : ignore */
105		}
106	}
107	mHead = mPad;
108}
109
110#ifdef _DEBUG
111// static
112BOOL LLXORCipher::testHarness()
113{
114	const U32 PAD_LEN = 3;
115	const U8 PAD[] = "abc";
116	const S32 MSG_LENGTH = 12;
117	const char MESSAGE[MSG_LENGTH+1] = "gesundheight";		/* Flawfinder : ignore */
118	U8 encrypted[MSG_LENGTH];
119	U8 decrypted[MSG_LENGTH];
120
121	LLXORCipher cipher(PAD, PAD_LEN);
122	cipher.encrypt((U8*)MESSAGE, MSG_LENGTH, encrypted, MSG_LENGTH);
123	cipher.decrypt(encrypted, MSG_LENGTH, decrypted, MSG_LENGTH);
124
125	if(0 != memcmp((void*)MESSAGE, decrypted, MSG_LENGTH)) return FALSE;
126	return TRUE;
127}
128#endif