1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/types.h>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>
#include <memory>
#include <rtl/digest.h>
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/strbuf.hxx>
#include <string.h>
using namespace rtl;
namespace
{
const OString sSampleString = "This is a sample sentence, which we use to check some crypto functions in sal.";
const OString sSampleString_only_one_diff = "This is a sample sentence. which we use to check some crypto functions in sal.";
const rtlDigestAlgorithm constDigestAlgorithms[] =
{
rtl_Digest_AlgorithmMD2,
rtl_Digest_AlgorithmMD5,
rtl_Digest_AlgorithmSHA,
rtl_Digest_AlgorithmSHA1,
rtl_Digest_AlgorithmHMAC_MD5,
rtl_Digest_AlgorithmHMAC_SHA1,
};
const sal_uInt32 constDigestAlgorithmLengths[] =
{
RTL_DIGEST_LENGTH_MD2,
RTL_DIGEST_LENGTH_MD5,
RTL_DIGEST_LENGTH_SHA,
RTL_DIGEST_LENGTH_SHA1,
RTL_DIGEST_LENGTH_HMAC_MD5,
RTL_DIGEST_LENGTH_HMAC_SHA1,
};
const OString constSampleStringSums[] =
{
"647ee6c9d4aa5fdd374ed9d7a156acbf",
"b16b903e6fc0b62ae389013ed93fe531",
"eab2814429b2613301c8a077b806af3680548914",
"2bc5bdb7506a2cdc2fd27fc8b9889343012d5008",
"0b1b0e1a6f2e4420326354b031063605",
"1998c6a556915be76451bfb587fa7c34d849936e"
};
// Create hex-value string from the digest value to keep the string size minimal
OString createHex(const sal_uInt8* pKeyBuffer, sal_uInt32 nKeyLen)
{
OStringBuffer aBuffer(nKeyLen * 2 + 1);
for (sal_uInt32 i = 0; i < nKeyLen; ++i)
{
sal_Int32 nValue = (sal_Int32) pKeyBuffer[i];
if (nValue < 16)
aBuffer.append('0');
aBuffer.append(nValue, 16);
}
return aBuffer.makeStringAndClear();
}
OString getDigest(const OString& aMessage, rtlDigestAlgorithm aAlgorithm)
{
rtlDigest handle = rtl_digest_create(aAlgorithm);
const sal_uInt8* pData = reinterpret_cast<const sal_uInt8*>(aMessage.getStr());
sal_uInt32 nSize = aMessage.getLength();
rtl_digest_init(handle, pData, nSize);
rtl_digest_update(handle, pData, nSize);
sal_uInt32 nKeyLen = rtl_digest_queryLength(handle);
std::unique_ptr<sal_uInt8[]> pKeyBuffer(new sal_uInt8[nKeyLen]);
rtl_digest_get(handle, pKeyBuffer.get(), nKeyLen);
OString aSum = createHex(pKeyBuffer.get(), nKeyLen);
rtl_digest_destroy( handle );
return aSum;
}
class DigestTest : public CppUnit::TestFixture
{
public:
void testCreate()
{
for (size_t i = 0; i < SAL_N_ELEMENTS(constDigestAlgorithms); i++)
{
rtlDigest handle = rtl_digest_create( constDigestAlgorithms[i] );
CPPUNIT_ASSERT_MESSAGE("create digest", handle != nullptr);
rtl_digest_destroy( handle );
}
rtlDigest handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid );
CPPUNIT_ASSERT_EQUAL_MESSAGE("create invalid digest", static_cast<rtlDigest>(nullptr), handle);
rtl_digest_destroy( handle );
}
void testQuery()
{
for (size_t i = 0; i < SAL_N_ELEMENTS(constDigestAlgorithms); i++)
{
rtlDigest handle = rtl_digest_create(constDigestAlgorithms[i]);
rtlDigestAlgorithm aAlgo = rtl_digest_queryAlgorithm(handle);
CPPUNIT_ASSERT_EQUAL_MESSAGE("query handle", aAlgo, constDigestAlgorithms[i]);
rtl_digest_destroy( handle );
}
}
void testQueryLength()
{
rtlDigest handle;
sal_uInt32 nAlgoLength;
for (size_t i = 0; i < SAL_N_ELEMENTS(constDigestAlgorithms); i++)
{
handle = rtl_digest_create(constDigestAlgorithms[i]);
nAlgoLength = rtl_digest_queryLength(handle);
CPPUNIT_ASSERT_EQUAL_MESSAGE("query Length", nAlgoLength, constDigestAlgorithmLengths[i]);
rtl_digest_destroy( handle );
}
handle = rtl_digest_create( rtl_Digest_AlgorithmInvalid );
nAlgoLength = rtl_digest_queryLength(handle);
CPPUNIT_ASSERT_EQUAL_MESSAGE("query length", static_cast<sal_uInt32>(0), nAlgoLength);
rtl_digest_destroy( handle );
}
void testInit()
{
rtlDigestError aError;
rtlDigest handle;
handle = nullptr;
aError = rtl_digest_init(handle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("init(NULL, 0, 0)", rtl_Digest_E_Argument, aError);
handle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
aError = rtl_digest_init(handle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("init(handle, 0, 0)", rtl_Digest_E_None, aError);
rtl_digest_destroy( handle );
for (size_t i = 0; i < SAL_N_ELEMENTS(constDigestAlgorithms); i++)
{
handle = rtl_digest_create(constDigestAlgorithms[i]);
OString aMessage = sSampleString;
const sal_uInt8* pData = reinterpret_cast<const sal_uInt8*>(aMessage.getStr());
sal_uInt32 nSize = aMessage.getLength();
aError = rtl_digest_init(handle, pData, nSize);
CPPUNIT_ASSERT_EQUAL_MESSAGE("init(handle, pData, nSize)", rtl_Digest_E_None, aError);
rtl_digest_update(handle, pData, nSize);
sal_uInt32 nKeyLen = rtl_digest_queryLength( handle );
std::unique_ptr<sal_uInt8[]> pKeyBuffer(new sal_uInt8[nKeyLen]);
rtl_digest_get( handle, pKeyBuffer.get(), nKeyLen );
createHex(pKeyBuffer.get(), nKeyLen);
rtl_digest_destroy( handle );
}
}
void testEqual()
{
{
OString aMsg1 = sSampleString;
OString aMsg2 = sSampleString;
OString aSum1 = getDigest(aMsg1, rtl_Digest_AlgorithmMD5);
OString aSum2 = getDigest(aMsg2, rtl_Digest_AlgorithmMD5);
CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aSum1.getLength() == 32 && aSum2.getLength() == 32 );
CPPUNIT_ASSERT_EQUAL_MESSAGE("source is the same, dest must be also the same", aSum1, aSum2);
}
{
OString aMsg1 = sSampleString;
OString aMsg2 = sSampleString_only_one_diff;
OString aSum1 = getDigest(aMsg1, rtl_Digest_AlgorithmMD5);
OString aSum2 = getDigest(aMsg2, rtl_Digest_AlgorithmMD5);
CPPUNIT_ASSERT_MESSAGE("md5sum must have a length", aSum1.getLength() == 32 && aSum2.getLength() == 32 );
CPPUNIT_ASSERT_MESSAGE("differ only in one char", aSum1 != aSum2);
}
}
void testCheckSum()
{
for (size_t i = 0; i < SAL_N_ELEMENTS(constDigestAlgorithms); i++)
{
OString aSum = getDigest(sSampleString, constDigestAlgorithms[i]);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Checksum of sample string is wrong.", constSampleStringSums[i], aSum);
}
}
OString runCheckPBKDF2(OString& sPassword, bool bClearSalt, sal_uInt32 nCount)
{
sal_uInt32 nKeyLen = RTL_DIGEST_LENGTH_HMAC_SHA1;
std::unique_ptr<sal_uInt8[]> pKeyBuffer(new sal_uInt8[nKeyLen]);
memset(pKeyBuffer.get(), 0, nKeyLen);
sal_uInt8 const * pPassword = reinterpret_cast<sal_uInt8 const *>(sPassword.getStr());
sal_Int32 nPasswordLen = sPassword.getLength();
sal_uInt32 nSaltDataLen = RTL_DIGEST_LENGTH_HMAC_SHA1;
std::unique_ptr<sal_uInt8[]> pSaltData(new sal_uInt8[nSaltDataLen]);
memset(pSaltData.get(), 0, nSaltDataLen);
if (!bClearSalt)
{
// wilful contamination
pSaltData[0] = 1;
}
rtlDigestError aError = rtl_digest_PBKDF2(pKeyBuffer.get(), nKeyLen, pPassword, nPasswordLen, pSaltData.get(), nSaltDataLen, nCount);
CPPUNIT_ASSERT_EQUAL(rtl_Digest_E_None, aError);
rtl::OString aKey = createHex(pKeyBuffer.get(), nKeyLen);
// OString sSalt = createHex(pSaltData, nSaltDataLen);
// printf("Salt: %s\n", sSalt.getStr());
// CPPUNIT_ASSERT_MESSAGE("md5sum of sample string is wrong. Code changes or sample problems, please check.", aStr.equals(sSampleString_PBKDF2) );
return aKey;
}
void testPBKDF2()
{
OString aPassword = "Password";
// all permutations
runCheckPBKDF2(aPassword, false, 1);
runCheckPBKDF2(aPassword, false, 2);
runCheckPBKDF2(aPassword, true, 1);
runCheckPBKDF2(aPassword, true, 2);
runCheckPBKDF2(aPassword, false, 3);
runCheckPBKDF2(aPassword, false, 4);
runCheckPBKDF2(aPassword, true, 3);
runCheckPBKDF2(aPassword, true, 4);
}
void testUpdate()
{
rtlDigestError aError;
rtlDigest aHandle;
aHandle = nullptr;
aError = rtl_digest_update(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("does not handle wrong parameter", rtl_Digest_E_Argument, aError);
aHandle = nullptr;
aError = rtl_digest_updateMD2(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("does not handle wrong parameter", rtl_Digest_E_Argument, aError);
aError = rtl_digest_updateMD5(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("does not handle wrong parameter", rtl_Digest_E_Argument, aError);
aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD2 );
CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != nullptr);
const sal_uInt8* pData = reinterpret_cast<const sal_uInt8*>(sSampleString.getStr());
sal_uInt32 nSize = sSampleString.getLength();
aError = rtl_digest_updateMD2(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'pData' wrong", rtl_Digest_E_Argument, aError);
aError = rtl_digest_updateMD2(aHandle, pData, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'nSize' wrong", rtl_Digest_E_None, aError);
rtl_digest_destroyMD2(aHandle);
// use wrong Algorithm!!! This is volitional!
aHandle = rtl_digest_create(rtl_Digest_AlgorithmMD2);
CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != nullptr);
aError = rtl_digest_updateMD5(aHandle, pData, nSize);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'handle' wrong", rtl_Digest_E_Algorithm, aError);
rtl_digest_destroyMD5(aHandle);
aHandle = rtl_digest_create( rtl_Digest_AlgorithmMD5 );
CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != nullptr);
aError = rtl_digest_updateMD5(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'pData' wrong", rtl_Digest_E_Argument, aError);
aError = rtl_digest_updateMD5(aHandle, pData, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'nSize' wrong", rtl_Digest_E_None, aError);
rtl_digest_destroyMD5(aHandle);
}
void testGet()
{
rtlDigest aHandle;
rtlDigestError aError;
aHandle = nullptr;
aError = rtl_digest_get(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("does not handle wrong parameter", rtl_Digest_E_Argument, aError);
aHandle = nullptr;
aError = rtl_digest_getMD5(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("does not handle wrong parameter", rtl_Digest_E_Argument, aError);
// test with wrong algorithm
aHandle = rtl_digest_create(rtl_Digest_AlgorithmMD2);
CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD2", aHandle != nullptr);
sal_uInt32 nKeyLen = rtl_digest_queryLength(aHandle);
std::unique_ptr<sal_uInt8[]> pKeyBuffer(new sal_uInt8[nKeyLen]);
aError = rtl_digest_getMD5(aHandle, nullptr, 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle 2. parameter wrong", rtl_Digest_E_Argument, aError);
aError = rtl_digest_getMD5(aHandle, pKeyBuffer.get(), 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'handle' wrong", rtl_Digest_E_Algorithm, aError);
rtl_digest_destroyMD2(aHandle);
aHandle = rtl_digest_create(rtl_Digest_AlgorithmMD5);
CPPUNIT_ASSERT_MESSAGE("create with rtl_Digest_AlgorithmMD5", aHandle != nullptr);
aError = rtl_digest_getMD5(aHandle, nullptr, nKeyLen);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'pData' wrong", rtl_Digest_E_Argument, aError);
aError = rtl_digest_getMD5(aHandle, pKeyBuffer.get(), 0);
CPPUNIT_ASSERT_EQUAL_MESSAGE("handle parameter 'nSize' wrong", rtl_Digest_E_BufferSize, aError);
rtl_digest_destroyMD5(aHandle);
}
void testSHA1SumForBiggerInputData()
{
// The test data was extracted from oox encrypted document (salt + password).
// First case: 16 bytes salt + 34 bytes password (12345678901234567)
// Second case: 16 bytes salt + 36 bytes password (123456789012345678)
{
const unsigned char aData[] = {
0x37, 0x5f, 0x47, 0x7a, 0xd2, 0x13, 0xbe, 0xd2, 0x3c, 0x23, 0x33, 0x39,
0x68, 0x21, 0x03, 0x6d, 0x31, 0x00, 0x32, 0x00, 0x33, 0x00, 0x34, 0x00,
0x35, 0x00, 0x36, 0x00, 0x37, 0x00, 0x38, 0x00, 0x39, 0x00, 0x30, 0x00,
0x31, 0x00, 0x32, 0x00, 0x33, 0x00, 0x34, 0x00, 0x35, 0x00, 0x36, 0x00,
0x37, 0x00
};
std::unique_ptr<sal_uInt8[]> pResult(new sal_uInt8[RTL_DIGEST_LENGTH_SHA1]);
OString sExpected = "06f460d693aecdd3b5cbe8365408eccfc570f32a";
rtl_digest_SHA1(aData, sizeof(aData), pResult.get(), RTL_DIGEST_LENGTH_SHA1);
OString sKey = createHex(pResult.get(), RTL_DIGEST_LENGTH_SHA1);
CPPUNIT_ASSERT_EQUAL(sExpected, sKey);
}
{
#if 0 // Don't remove, but instead fix the test or something
// With this test case rtl_digest_SHA1 computes the wrong sum. This was confirmed
// by decryption of a MSO encrypted document. Replacing the rtl_digest_SHA1 calculation
// with sha1 calculation from NSS was able to decrypt the document.
const unsigned char aData[] = {
0x37, 0x5f, 0x47, 0x7a, 0xd2, 0x13, 0xbe, 0xd2, 0x3c, 0x23, 0x33, 0x39,
0x68, 0x21, 0x03, 0x6d, 0x31, 0x00, 0x32, 0x00, 0x33, 0x00, 0x34, 0x00,
0x35, 0x00, 0x36, 0x00, 0x37, 0x00, 0x38, 0x00, 0x39, 0x00, 0x30, 0x00,
0x31, 0x00, 0x32, 0x00, 0x33, 0x00, 0x34, 0x00, 0x35, 0x00, 0x36, 0x00,
0x37, 0x00, 0x38, 0x00
};
std::unique_ptr<sal_uInt8[]> pResult(new sal_uInt8[RTL_DIGEST_LENGTH_SHA1]);
OString sExpected = "0bfe41eb7fb3edf5f5a6de57192de4ba1b925758";
rtl_digest_SHA1(aData, sizeof(aData), pResult.get(), RTL_DIGEST_LENGTH_SHA1);
OString sKey = createHex(pResult.get(), RTL_DIGEST_LENGTH_SHA1);
CPPUNIT_ASSERT_EQUAL(sExpected, sKey);
#endif
}
}
CPPUNIT_TEST_SUITE(DigestTest);
CPPUNIT_TEST(testCreate);
CPPUNIT_TEST(testQuery);
CPPUNIT_TEST(testQueryLength);
CPPUNIT_TEST(testInit);
CPPUNIT_TEST(testEqual);
CPPUNIT_TEST(testCheckSum);
CPPUNIT_TEST(testPBKDF2);
CPPUNIT_TEST(testUpdate);
CPPUNIT_TEST(testGet);
CPPUNIT_TEST(testSHA1SumForBiggerInputData);
CPPUNIT_TEST_SUITE_END();
};
CPPUNIT_TEST_SUITE_REGISTRATION(DigestTest);
} // namespace rtl_digest
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|