PageRenderTime 39ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/test/llblowfish_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 141 lines | 84 code | 19 blank | 38 comment | 7 complexity | deaa9a53d4dd388a47e640def59e78bd MD5 | raw file
  1/** 
  2 * @file llblowfish_tut.cpp
  3 * @author James Cook, james@lindenlab.com
  4 * @date 2007-02-04
  5 *
  6 * Data files generated with:
  7 * openssl enc -bf-cbc -in blowfish.digits.txt -out blowfish.1.bin -K 00000000000000000000000000000000 -iv 0000000000000000 -p
  8 * openssl enc -bf-cbc -in blowfish.digits.txt -out blowfish.2.bin -K 526a1e07a19dbaed84c4ff08a488d15e -iv 0000000000000000 -p
  9 *
 10 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
 11 * Second Life Viewer Source Code
 12 * Copyright (C) 2010, Linden Research, Inc.
 13 * 
 14 * This library is free software; you can redistribute it and/or
 15 * modify it under the terms of the GNU Lesser General Public
 16 * License as published by the Free Software Foundation;
 17 * version 2.1 of the License only.
 18 * 
 19 * This library is distributed in the hope that it will be useful,
 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 22 * Lesser General Public License for more details.
 23 * 
 24 * You should have received a copy of the GNU Lesser General Public
 25 * License along with this library; if not, write to the Free Software
 26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 27 * 
 28 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 29 * $/LicenseInfo$
 30 */
 31
 32#include "linden_common.h"
 33#include "lltut.h"
 34
 35#include "llblowfishcipher.h"
 36
 37#include "lluuid.h"
 38
 39namespace tut
 40{
 41	class LLData
 42	{
 43	public:
 44		unsigned char* mInput;
 45		int mInputSize;
 46
 47		LLData()
 48		{
 49			// \n to make it easier to create text files
 50			// for testing with command line openssl
 51			mInput = (unsigned char*)"01234567890123456789012345678901234\n";
 52			mInputSize = 36;
 53		}
 54
 55		bool matchFile(const std::string& filename,
 56					   const std::string& data)
 57		{
 58			LLFILE* fp = LLFile::fopen(filename, "rb");
 59			if (!fp) 
 60			{
 61				// sometimes test is run inside the indra directory
 62				std::string path = "test/";
 63				path += filename;
 64				fp = LLFile::fopen(path, "rb");
 65			}
 66			if (!fp)
 67			{
 68				llwarns << "unabled to open " << filename << llendl;
 69				return false;
 70			}
 71
 72			std::string good;
 73			good.resize(256);
 74			size_t got = fread(&good[0], 1, 256, fp);
 75			lldebugs << "matchFile read " << got << llendl;
 76			fclose(fp);
 77			good.resize(got);
 78		
 79			return (good == data);
 80		}
 81	};
 82	typedef test_group<LLData> blowfish_test;
 83	typedef blowfish_test::object blowfish_object;
 84	// Create test with name that can be selected on
 85	// command line of test app.
 86	tut::blowfish_test blowfish("blowfish");
 87
 88	template<> template<>
 89	void blowfish_object::test<1>()
 90	{
 91		LLUUID blank;
 92		LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES);
 93
 94		U32 dst_len = cipher.requiredEncryptionSpace(36);
 95		ensure("encryption space 36",
 96				(dst_len == 40) );
 97
 98		// Blowfish adds an additional 8-byte block if your
 99		// input is an exact multiple of 8
100		dst_len = cipher.requiredEncryptionSpace(8);
101		ensure("encryption space 8",
102				(dst_len == 16)  );
103	}
104
105	template<> template<>
106	void blowfish_object::test<2>()
107	{
108		LLUUID blank;
109		LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES);
110
111		std::string result;
112		result.resize(256);
113		U32 count = cipher.encrypt(mInput, mInputSize,
114				(U8*) &result[0], 256);
115
116		ensure("encrypt output count",
117				(count == 40) );
118		result.resize(count);
119
120		ensure("encrypt null key", matchFile("blowfish.1.bin", result));
121	}
122
123	template<> template<>
124	void blowfish_object::test<3>()
125	{
126        // same as base64 test id
127		LLUUID id("526a1e07-a19d-baed-84c4-ff08a488d15e");
128		LLBlowfishCipher cipher(&id.mData[0], UUID_BYTES);
129
130		std::string result;
131		result.resize(256);
132		U32 count = cipher.encrypt(mInput, mInputSize,
133				(U8*) &result[0], 256);
134
135		ensure("encrypt output count",
136				(count == 40) );
137		result.resize(count);
138
139		ensure("encrypt real key", matchFile("blowfish.2.bin", result));
140	}
141}