diff options
Diffstat (limited to 'linden/indra/test/llblowfish_tut.cpp')
-rw-r--r-- | linden/indra/test/llblowfish_tut.cpp | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/linden/indra/test/llblowfish_tut.cpp b/linden/indra/test/llblowfish_tut.cpp new file mode 100644 index 0000000..0d85ade --- /dev/null +++ b/linden/indra/test/llblowfish_tut.cpp | |||
@@ -0,0 +1,144 @@ | |||
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 | * Copyright (c) 2007-2007, Linden Research, Inc. | ||
11 | * | ||
12 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
13 | * to you under the terms of the GNU General Public License, version 2.0 | ||
14 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
15 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
16 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
18 | * | ||
19 | * There are special exceptions to the terms and conditions of the GPL as | ||
20 | * it is applied to this Source Code. View the full text of the exception | ||
21 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
22 | * online at http://secondlife.com/developers/opensource/flossexception | ||
23 | * | ||
24 | * By copying, modifying or distributing this software, you acknowledge | ||
25 | * that you have read and understood your obligations described above, | ||
26 | * and agree to abide by those obligations. | ||
27 | * | ||
28 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
29 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
30 | * COMPLETENESS OR PERFORMANCE. | ||
31 | */ | ||
32 | |||
33 | #include "linden_common.h" | ||
34 | #include "lltut.h" | ||
35 | |||
36 | #include "llblowfishcipher.h" | ||
37 | |||
38 | #include <string> | ||
39 | #include <stdio.h> | ||
40 | #include "lluuid.h" | ||
41 | |||
42 | namespace tut | ||
43 | { | ||
44 | class LLData | ||
45 | { | ||
46 | public: | ||
47 | unsigned char* mInput; | ||
48 | int mInputSize; | ||
49 | |||
50 | LLData() | ||
51 | { | ||
52 | // \n to make it easier to create text files | ||
53 | // for testing with command line openssl | ||
54 | mInput = (unsigned char*)"01234567890123456789012345678901234\n"; | ||
55 | mInputSize = 36; | ||
56 | } | ||
57 | |||
58 | bool matchFile(const char* filename, | ||
59 | const std::string& data) | ||
60 | { | ||
61 | FILE* fp = fopen(filename, "rb"); | ||
62 | if (!fp) | ||
63 | { | ||
64 | // sometimes test is run inside the indra directory | ||
65 | std::string path = "test/"; | ||
66 | path += filename; | ||
67 | fp = fopen(path.c_str(), "rb"); | ||
68 | } | ||
69 | if (!fp) | ||
70 | { | ||
71 | llwarns << "unabled to open " << filename << llendl; | ||
72 | return false; | ||
73 | } | ||
74 | |||
75 | std::string good; | ||
76 | good.resize(256); | ||
77 | size_t got = fread(&good[0], 1, 256, fp); | ||
78 | lldebugs << "matchFile read " << got << llendl; | ||
79 | fclose(fp); | ||
80 | good.resize(got); | ||
81 | |||
82 | return (good == data); | ||
83 | } | ||
84 | }; | ||
85 | typedef test_group<LLData> blowfish_test; | ||
86 | typedef blowfish_test::object blowfish_object; | ||
87 | // Create test with name that can be selected on | ||
88 | // command line of test app. | ||
89 | tut::blowfish_test blowfish("blowfish"); | ||
90 | |||
91 | template<> template<> | ||
92 | void blowfish_object::test<1>() | ||
93 | { | ||
94 | LLUUID blank; | ||
95 | LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES); | ||
96 | |||
97 | U32 dst_len = cipher.requiredEncryptionSpace(36); | ||
98 | ensure("encryption space 36", | ||
99 | (dst_len == 40) ); | ||
100 | |||
101 | // Blowfish adds an additional 8-byte block if your | ||
102 | // input is an exact multiple of 8 | ||
103 | dst_len = cipher.requiredEncryptionSpace(8); | ||
104 | ensure("encryption space 8", | ||
105 | (dst_len == 16) ); | ||
106 | } | ||
107 | |||
108 | template<> template<> | ||
109 | void blowfish_object::test<2>() | ||
110 | { | ||
111 | LLUUID blank; | ||
112 | LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES); | ||
113 | |||
114 | std::string result; | ||
115 | result.resize(256); | ||
116 | U32 count = cipher.encrypt(mInput, mInputSize, | ||
117 | (U8*) &result[0], 256); | ||
118 | |||
119 | ensure("encrypt output count", | ||
120 | (count == 40) ); | ||
121 | result.resize(count); | ||
122 | |||
123 | ensure("encrypt null key", matchFile("blowfish.1.bin", result)); | ||
124 | } | ||
125 | |||
126 | template<> template<> | ||
127 | void blowfish_object::test<3>() | ||
128 | { | ||
129 | // same as base64 test id | ||
130 | LLUUID id("526a1e07-a19d-baed-84c4-ff08a488d15e"); | ||
131 | LLBlowfishCipher cipher(&id.mData[0], UUID_BYTES); | ||
132 | |||
133 | std::string result; | ||
134 | result.resize(256); | ||
135 | U32 count = cipher.encrypt(mInput, mInputSize, | ||
136 | (U8*) &result[0], 256); | ||
137 | |||
138 | ensure("encrypt output count", | ||
139 | (count == 40) ); | ||
140 | result.resize(count); | ||
141 | |||
142 | ensure("encrypt real key", matchFile("blowfish.2.bin", result)); | ||
143 | } | ||
144 | } | ||