TcWrapper.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Tomcrypt library <= firebird : c++ wrapper.
  3. *
  4. * The contents of this file are subject to the Initial
  5. * Developer's Public License Version 1.0 (the "License");
  6. * you may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at
  8. * https://www.firebirdsql.org/en/initial-developer-s-public-license-version-1-0/
  9. *
  10. * Software distributed under the License is distributed AS IS,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing rights
  13. * and limitations under the License.
  14. *
  15. * The Original Code was created by Alexander Peshkoff
  16. * for the Firebird Open Source RDBMS project.
  17. *
  18. * Copyright (c) 2020 Alexander Peshkoff <[email protected]>
  19. * and all contributors signed below.
  20. *
  21. * All Rights Reserved.
  22. * Contributor(s): ______________________________________.
  23. */
  24. #include "TcWrapper.h"
  25. namespace {
  26. // LTC hack
  27. class GInit
  28. {
  29. public:
  30. GInit()
  31. {
  32. ltc_mp = ltm_desc;
  33. }
  34. };
  35. GInit gInit;
  36. }
  37. void error(ThrowStatusWrapper* status, const char* text)
  38. {
  39. if (! status)
  40. throw text;
  41. ISC_STATUS_ARRAY v;
  42. v[0] = isc_arg_gds;
  43. v[1] = isc_random;
  44. v[2] = isc_arg_string;
  45. v[3] = (ISC_STATUS) text;
  46. v[4] = isc_arg_end;
  47. throw FbException(status, v);
  48. }
  49. void check(ThrowStatusWrapper* status, int err, const char* text)
  50. {
  51. if (err == CRYPT_OK)
  52. return;
  53. char buf[256];
  54. snprintf(buf, sizeof(buf), "%s: %s", text, error_to_string(err));
  55. error(status, buf);
  56. }
  57. unsigned readHexKey(ThrowStatusWrapper* status, const char* hex, unsigned char* key, unsigned bufSize)
  58. {
  59. unsigned char* k = key;
  60. const char* const end = hex + strlen(hex) - 1;
  61. for (const char* s = hex; s < end; s += 2)
  62. {
  63. if (k - key >= bufSize)
  64. break;
  65. // FF
  66. char ss[3];
  67. ss[0] = s[0];
  68. ss[1] = s[1];
  69. ss[2] = 0;
  70. unsigned c = strtoul(ss, NULL, 16);
  71. if (c > 255)
  72. error(status, "Key format error");
  73. *k++ = static_cast<unsigned char>(c);
  74. }
  75. return static_cast<unsigned>(k - key);
  76. }
  77. void PseudoRandom::init(ThrowStatusWrapper* status)
  78. {
  79. // LTC hack
  80. ltc_mp = ltm_desc;
  81. // register yarrow
  82. index = register_prng(&yarrow_desc);
  83. if (index == -1)
  84. error(status, "Error registering PRNG yarrow");
  85. // setup the PRNG
  86. check(status, yarrow_start(&state), "Error starting PRNG yarrow");
  87. check(status, rng_make_prng(64, index, &state, NULL), "Error setting up PRNG yarrow");
  88. }
  89. void PseudoRandom::fini()
  90. {
  91. yarrow_done(&state);
  92. }
  93. const PseudoRandom::PrngDescriptor* PseudoRandom::getDsc()
  94. {
  95. return &yarrow_desc;
  96. }
  97. void Hash::init(ThrowStatusWrapper* status, const ltc_hash_descriptor* desc)
  98. {
  99. // LTC hack
  100. ltc_mp = ltm_desc;
  101. /* register SHA256 */
  102. index = register_hash(desc);
  103. if (index == -1)
  104. error(status, "Error registering SHA256");
  105. }