PageRenderTime 76ms CodeModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  32. #include "lltut.h"
  33. #include "llblowfishcipher.h"
  34. #include "lluuid.h"
  35. namespace tut
  36. {
  37. class LLData
  38. {
  39. public:
  40. unsigned char* mInput;
  41. int mInputSize;
  42. LLData()
  43. {
  44. // \n to make it easier to create text files
  45. // for testing with command line openssl
  46. mInput = (unsigned char*)"01234567890123456789012345678901234\n";
  47. mInputSize = 36;
  48. }
  49. bool matchFile(const std::string& filename,
  50. const std::string& data)
  51. {
  52. LLFILE* fp = LLFile::fopen(filename, "rb");
  53. if (!fp)
  54. {
  55. // sometimes test is run inside the indra directory
  56. std::string path = "test/";
  57. path += filename;
  58. fp = LLFile::fopen(path, "rb");
  59. }
  60. if (!fp)
  61. {
  62. llwarns << "unabled to open " << filename << llendl;
  63. return false;
  64. }
  65. std::string good;
  66. good.resize(256);
  67. size_t got = fread(&good[0], 1, 256, fp);
  68. lldebugs << "matchFile read " << got << llendl;
  69. fclose(fp);
  70. good.resize(got);
  71. return (good == data);
  72. }
  73. };
  74. typedef test_group<LLData> blowfish_test;
  75. typedef blowfish_test::object blowfish_object;
  76. // Create test with name that can be selected on
  77. // command line of test app.
  78. tut::blowfish_test blowfish("blowfish");
  79. template<> template<>
  80. void blowfish_object::test<1>()
  81. {
  82. LLUUID blank;
  83. LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES);
  84. U32 dst_len = cipher.requiredEncryptionSpace(36);
  85. ensure("encryption space 36",
  86. (dst_len == 40) );
  87. // Blowfish adds an additional 8-byte block if your
  88. // input is an exact multiple of 8
  89. dst_len = cipher.requiredEncryptionSpace(8);
  90. ensure("encryption space 8",
  91. (dst_len == 16) );
  92. }
  93. template<> template<>
  94. void blowfish_object::test<2>()
  95. {
  96. LLUUID blank;
  97. LLBlowfishCipher cipher(&blank.mData[0], UUID_BYTES);
  98. std::string result;
  99. result.resize(256);
  100. U32 count = cipher.encrypt(mInput, mInputSize,
  101. (U8*) &result[0], 256);
  102. ensure("encrypt output count",
  103. (count == 40) );
  104. result.resize(count);
  105. ensure("encrypt null key", matchFile("blowfish.1.bin", result));
  106. }
  107. template<> template<>
  108. void blowfish_object::test<3>()
  109. {
  110. // same as base64 test id
  111. LLUUID id("526a1e07-a19d-baed-84c4-ff08a488d15e");
  112. LLBlowfishCipher cipher(&id.mData[0], UUID_BYTES);
  113. std::string result;
  114. result.resize(256);
  115. U32 count = cipher.encrypt(mInput, mInputSize,
  116. (U8*) &result[0], 256);
  117. ensure("encrypt output count",
  118. (count == 40) );
  119. result.resize(count);
  120. ensure("encrypt real key", matchFile("blowfish.2.bin", result));
  121. }
  122. }