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