gl_openssl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* gl_openssl.h -- wrap openssl crypto hash routines in gnulib interface
  2. Copyright (C) 2013-2015 Free Software Foundation, Inc.
  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; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Pádraig Brady */
  14. #ifndef GL_OPENSSL_NAME
  15. # error "Please define GL_OPENSSL_NAME to 1,5,256 etc."
  16. #endif
  17. #ifndef _GL_INLINE_HEADER_BEGIN
  18. # error "Please include config.h first."
  19. #endif
  20. _GL_INLINE_HEADER_BEGIN
  21. #ifndef GL_OPENSSL_INLINE
  22. # define GL_OPENSSL_INLINE _GL_INLINE
  23. #endif
  24. /* Concatenate two preprocessor tokens. */
  25. #define _GLCRYPTO_CONCAT_(prefix, suffix) prefix##suffix
  26. #define _GLCRYPTO_CONCAT(prefix, suffix) _GLCRYPTO_CONCAT_ (prefix, suffix)
  27. #if GL_OPENSSL_NAME == 5
  28. # define OPENSSL_ALG md5
  29. #else
  30. # define OPENSSL_ALG _GLCRYPTO_CONCAT (sha, GL_OPENSSL_NAME)
  31. #endif
  32. /* Context type mappings. */
  33. #if BASE_OPENSSL_TYPE != GL_OPENSSL_NAME
  34. # undef BASE_OPENSSL_TYPE
  35. # if GL_OPENSSL_NAME == 224
  36. # define BASE_OPENSSL_TYPE 256
  37. # elif GL_OPENSSL_NAME == 384
  38. # define BASE_OPENSSL_TYPE 512
  39. # endif
  40. # define md5_CTX MD5_CTX
  41. # define sha1_CTX SHA_CTX
  42. # define sha224_CTX SHA256_CTX
  43. # define sha224_ctx sha256_ctx
  44. # define sha256_CTX SHA256_CTX
  45. # define sha384_CTX SHA512_CTX
  46. # define sha384_ctx sha512_ctx
  47. # define sha512_CTX SHA512_CTX
  48. # undef _gl_CTX
  49. # undef _gl_ctx
  50. # define _gl_CTX _GLCRYPTO_CONCAT (OPENSSL_ALG, _CTX) /* openssl type. */
  51. # define _gl_ctx _GLCRYPTO_CONCAT (OPENSSL_ALG, _ctx) /* gnulib type. */
  52. struct _gl_ctx { _gl_CTX CTX; };
  53. #endif
  54. /* Function name mappings. */
  55. #define md5_prefix MD5
  56. #define sha1_prefix SHA1
  57. #define sha224_prefix SHA224
  58. #define sha256_prefix SHA256
  59. #define sha384_prefix SHA384
  60. #define sha512_prefix SHA512
  61. #define _GLCRYPTO_PREFIX _GLCRYPTO_CONCAT (OPENSSL_ALG, _prefix)
  62. #define OPENSSL_FN(suffix) _GLCRYPTO_CONCAT (_GLCRYPTO_PREFIX, suffix)
  63. #define GL_CRYPTO_FN(suffix) _GLCRYPTO_CONCAT (OPENSSL_ALG, suffix)
  64. GL_OPENSSL_INLINE void
  65. GL_CRYPTO_FN (_init_ctx) (struct _gl_ctx *ctx)
  66. { (void) OPENSSL_FN (_Init) ((_gl_CTX *) ctx); }
  67. /* These were never exposed by gnulib. */
  68. #if ! (GL_OPENSSL_NAME == 224 || GL_OPENSSL_NAME == 384)
  69. GL_OPENSSL_INLINE void
  70. GL_CRYPTO_FN (_process_bytes) (const void *buf, size_t len, struct _gl_ctx *ctx)
  71. { OPENSSL_FN (_Update) ((_gl_CTX *) ctx, buf, len); }
  72. GL_OPENSSL_INLINE void
  73. GL_CRYPTO_FN (_process_block) (const void *buf, size_t len, struct _gl_ctx *ctx)
  74. { GL_CRYPTO_FN (_process_bytes) (buf, len, ctx); }
  75. #endif
  76. GL_OPENSSL_INLINE void *
  77. GL_CRYPTO_FN (_finish_ctx) (struct _gl_ctx *ctx, void *res)
  78. { OPENSSL_FN (_Final) ((unsigned char *) res, (_gl_CTX *) ctx); return res; }
  79. GL_OPENSSL_INLINE void *
  80. GL_CRYPTO_FN (_buffer) (const char *buf, size_t len, void *res)
  81. { return OPENSSL_FN () ((const unsigned char *) buf, len, (unsigned char *) res); }
  82. GL_OPENSSL_INLINE void *
  83. GL_CRYPTO_FN (_read_ctx) (const struct _gl_ctx *ctx, void *res)
  84. {
  85. /* Assume any unprocessed bytes in ctx are not to be ignored. */
  86. _gl_CTX tmp_ctx = *(_gl_CTX *) ctx;
  87. OPENSSL_FN (_Final) ((unsigned char *) res, &tmp_ctx);
  88. return res;
  89. }
  90. /* Undef so we can include multiple times. */
  91. #undef GL_CRYPTO_FN
  92. #undef OPENSSL_FN
  93. #undef _GLCRYPTO_PREFIX
  94. #undef OPENSSL_ALG
  95. #undef GL_OPENSSL_NAME
  96. _GL_INLINE_HEADER_END