You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.0 KiB

9 years ago
9 years ago
  1. /* Copyright (c) 2012, Oracle and/or its affiliates.
  2. Copyright (c) 2014, 2017, MariaDB
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software Foundation,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
  13. /**
  14. @file
  15. @brief
  16. Wrapper functions for OpenSSL, YaSSL implementations. Also provides a
  17. Compatibility layer to make available YaSSL's SHAn implementation.
  18. */
  19. #include <my_global.h>
  20. #include <stdarg.h>
  21. #define HASH_SIZE (NUM > 1 ? NUM/8 : 20)
  22. #if defined(HAVE_YASSL)
  23. #include "sha.hpp"
  24. #define xCONTEXT(x) TaoCrypt::SHA ## x
  25. #define yCONTEXT(y) xCONTEXT(y)
  26. #define CONTEXT yCONTEXT(NUM)
  27. #define SHA1 SHA
  28. static void sha_init(CONTEXT *context)
  29. {
  30. context->Init();
  31. }
  32. /*
  33. this is a variant of sha_init to be used in this file only.
  34. does nothing for yassl, because the context's constructor was called automatically.
  35. */
  36. static void sha_init_fast(CONTEXT *context)
  37. {
  38. }
  39. static void sha_input(CONTEXT *context, const uchar *buf, unsigned len)
  40. {
  41. context->Update((const TaoCrypt::byte *) buf, len);
  42. }
  43. static void sha_result(CONTEXT *context, uchar digest[HASH_SIZE])
  44. {
  45. context->Final((TaoCrypt::byte *) digest);
  46. }
  47. #elif defined(HAVE_OPENSSL)
  48. #include <openssl/sha.h>
  49. #define xCONTEXT(x) SHA ## x ## _CTX
  50. #define yCONTEXT(y) xCONTEXT(y)
  51. #define CONTEXT yCONTEXT(NUM)
  52. #define SHA1_CTX SHA_CTX
  53. #define SHA224_CTX SHA256_CTX
  54. #define SHA384_CTX SHA512_CTX
  55. #define xSHA_Init(x) SHA ## x ## _Init
  56. #define xSHA_Update(x) SHA ## x ## _Update
  57. #define xSHA_Final(x) SHA ## x ## _Final
  58. #define ySHA_Init(y) xSHA_Init(y)
  59. #define ySHA_Update(y) xSHA_Update(y)
  60. #define ySHA_Final(y) xSHA_Final(y)
  61. #define SHA_Init ySHA_Init(NUM)
  62. #define SHA_Update ySHA_Update(NUM)
  63. #define SHA_Final ySHA_Final(NUM)
  64. static void sha_init(CONTEXT *context)
  65. {
  66. SHA_Init(context);
  67. }
  68. static void sha_init_fast(CONTEXT *context)
  69. {
  70. sha_init(context);
  71. }
  72. static void sha_input(CONTEXT *context, const uchar *buf, unsigned len)
  73. {
  74. SHA_Update(context, buf, len);
  75. }
  76. static void sha_result(CONTEXT *context, uchar digest[HASH_SIZE])
  77. {
  78. SHA_Final(digest, context);
  79. }
  80. #endif /* HAVE_YASSL */
  81. #define xmy_sha_multi(x) my_sha ## x ## _multi
  82. #define xmy_sha_context_size(x) my_sha ## x ## _context_size
  83. #define xmy_sha_init(x) my_sha ## x ## _init
  84. #define xmy_sha_input(x) my_sha ## x ## _input
  85. #define xmy_sha_result(x) my_sha ## x ## _result
  86. #define xmy_sha(x) my_sha ## x
  87. #define ymy_sha_multi(y) xmy_sha_multi(y)
  88. #define ymy_sha_context_size(y) xmy_sha_context_size(y)
  89. #define ymy_sha_init(y) xmy_sha_init(y)
  90. #define ymy_sha_input(y) xmy_sha_input(y)
  91. #define ymy_sha_result(y) xmy_sha_result(y)
  92. #define ymy_sha(y) xmy_sha(y)
  93. #define my_sha_multi ymy_sha_multi(NUM)
  94. #define my_sha_context_size ymy_sha_context_size(NUM)
  95. #define my_sha_init ymy_sha_init(NUM)
  96. #define my_sha_input ymy_sha_input(NUM)
  97. #define my_sha_result ymy_sha_result(NUM)
  98. #define my_sha ymy_sha(NUM)
  99. /**
  100. Wrapper function to compute SHAn message digest.
  101. @param digest [out] Computed SHAn digest
  102. @param buf [in] Message to be computed
  103. @param len [in] Length of the message
  104. @return void
  105. */
  106. void my_sha(uchar *digest, const char *buf, size_t len)
  107. {
  108. CONTEXT context;
  109. sha_init_fast(&context);
  110. sha_input(&context, (const uchar *)buf, len);
  111. sha_result(&context, digest);
  112. }
  113. /**
  114. Wrapper function to compute SHAn message digest for
  115. two messages in order to emulate shaN(msg1, msg2).
  116. @param digest [out] Computed SHAn digest
  117. @param buf1 [in] First message
  118. @param len1 [in] Length of first message
  119. @param buf2 [in] Second message
  120. @param len2 [in] Length of second message
  121. @return void
  122. */
  123. void my_sha_multi(uchar *digest, ...)
  124. {
  125. va_list args;
  126. va_start(args, digest);
  127. CONTEXT context;
  128. const uchar *str;
  129. sha_init_fast(&context);
  130. for (str= va_arg(args, const uchar*); str; str= va_arg(args, const uchar*))
  131. sha_input(&context, str, va_arg(args, size_t));
  132. sha_result(&context, digest);
  133. va_end(args);
  134. }
  135. size_t my_sha_context_size()
  136. {
  137. return sizeof(CONTEXT);
  138. }
  139. void my_sha_init(void *context)
  140. {
  141. sha_init((CONTEXT *)context);
  142. }
  143. void my_sha_input(void *context, const uchar *buf, size_t len)
  144. {
  145. sha_input((CONTEXT *)context, buf, len);
  146. }
  147. void my_sha_result(void *context, uchar *digest)
  148. {
  149. sha_result((CONTEXT *)context, digest);
  150. }