diff options
Diffstat (limited to 'gl/gl_openssl.h')
-rw-r--r-- | gl/gl_openssl.h | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/gl/gl_openssl.h b/gl/gl_openssl.h new file mode 100644 index 0000000..f63c386 --- /dev/null +++ b/gl/gl_openssl.h | |||
@@ -0,0 +1,116 @@ | |||
1 | /* Wrap openssl crypto hash routines in gnulib interface. -*- coding: utf-8 -*- | ||
2 | |||
3 | Copyright (C) 2013-2023 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 | #ifndef GL_OPENSSL_NAME | ||
21 | # error "Please define GL_OPENSSL_NAME to 1,5,256 etc." | ||
22 | #endif | ||
23 | |||
24 | #ifndef _GL_INLINE_HEADER_BEGIN | ||
25 | # error "Please include config.h first." | ||
26 | #endif | ||
27 | _GL_INLINE_HEADER_BEGIN | ||
28 | #ifndef GL_OPENSSL_INLINE | ||
29 | # define GL_OPENSSL_INLINE _GL_INLINE | ||
30 | #endif | ||
31 | |||
32 | /* Concatenate two preprocessor tokens. */ | ||
33 | #define _GLCRYPTO_CONCAT_(prefix, suffix) prefix##suffix | ||
34 | #define _GLCRYPTO_CONCAT(prefix, suffix) _GLCRYPTO_CONCAT_ (prefix, suffix) | ||
35 | |||
36 | #if GL_OPENSSL_NAME == 5 | ||
37 | # define OPENSSL_ALG md5 | ||
38 | #else | ||
39 | # define OPENSSL_ALG _GLCRYPTO_CONCAT (sha, GL_OPENSSL_NAME) | ||
40 | #endif | ||
41 | |||
42 | /* Context type mappings. */ | ||
43 | #if BASE_OPENSSL_TYPE != GL_OPENSSL_NAME | ||
44 | # undef BASE_OPENSSL_TYPE | ||
45 | # if GL_OPENSSL_NAME == 224 | ||
46 | # define BASE_OPENSSL_TYPE 256 | ||
47 | # elif GL_OPENSSL_NAME == 384 | ||
48 | # define BASE_OPENSSL_TYPE 512 | ||
49 | # endif | ||
50 | # define md5_CTX MD5_CTX | ||
51 | # define sha1_CTX SHA_CTX | ||
52 | # define sha224_CTX SHA256_CTX | ||
53 | # define sha224_ctx sha256_ctx | ||
54 | # define sha256_CTX SHA256_CTX | ||
55 | # define sha384_CTX SHA512_CTX | ||
56 | # define sha384_ctx sha512_ctx | ||
57 | # define sha512_CTX SHA512_CTX | ||
58 | # undef _gl_CTX | ||
59 | # undef _gl_ctx | ||
60 | # define _gl_CTX _GLCRYPTO_CONCAT (OPENSSL_ALG, _CTX) /* openssl type. */ | ||
61 | # define _gl_ctx _GLCRYPTO_CONCAT (OPENSSL_ALG, _ctx) /* gnulib type. */ | ||
62 | |||
63 | struct _gl_ctx { _gl_CTX CTX; }; | ||
64 | #endif | ||
65 | |||
66 | /* Function name mappings. */ | ||
67 | #define md5_prefix MD5 | ||
68 | #define sha1_prefix SHA1 | ||
69 | #define sha224_prefix SHA224 | ||
70 | #define sha256_prefix SHA256 | ||
71 | #define sha384_prefix SHA384 | ||
72 | #define sha512_prefix SHA512 | ||
73 | #define _GLCRYPTO_PREFIX _GLCRYPTO_CONCAT (OPENSSL_ALG, _prefix) | ||
74 | #define OPENSSL_FN(suffix) _GLCRYPTO_CONCAT (_GLCRYPTO_PREFIX, suffix) | ||
75 | #define GL_CRYPTO_FN(suffix) _GLCRYPTO_CONCAT (OPENSSL_ALG, suffix) | ||
76 | |||
77 | GL_OPENSSL_INLINE void | ||
78 | GL_CRYPTO_FN (_init_ctx) (struct _gl_ctx *ctx) | ||
79 | { (void) OPENSSL_FN (_Init) ((_gl_CTX *) ctx); } | ||
80 | |||
81 | /* These were never exposed by gnulib. */ | ||
82 | #if ! (GL_OPENSSL_NAME == 224 || GL_OPENSSL_NAME == 384) | ||
83 | GL_OPENSSL_INLINE void | ||
84 | GL_CRYPTO_FN (_process_bytes) (const void *buf, size_t len, struct _gl_ctx *ctx) | ||
85 | { OPENSSL_FN (_Update) ((_gl_CTX *) ctx, buf, len); } | ||
86 | |||
87 | GL_OPENSSL_INLINE void | ||
88 | GL_CRYPTO_FN (_process_block) (const void *buf, size_t len, struct _gl_ctx *ctx) | ||
89 | { GL_CRYPTO_FN (_process_bytes) (buf, len, ctx); } | ||
90 | #endif | ||
91 | |||
92 | GL_OPENSSL_INLINE void * | ||
93 | GL_CRYPTO_FN (_finish_ctx) (struct _gl_ctx *ctx, void *restrict res) | ||
94 | { OPENSSL_FN (_Final) ((unsigned char *) res, (_gl_CTX *) ctx); return res; } | ||
95 | |||
96 | GL_OPENSSL_INLINE void * | ||
97 | GL_CRYPTO_FN (_buffer) (const char *buf, size_t len, void *restrict res) | ||
98 | { return OPENSSL_FN () ((const unsigned char *) buf, len, (unsigned char *) res); } | ||
99 | |||
100 | GL_OPENSSL_INLINE void * | ||
101 | GL_CRYPTO_FN (_read_ctx) (const struct _gl_ctx *ctx, void *restrict res) | ||
102 | { | ||
103 | /* Assume any unprocessed bytes in ctx are not to be ignored. */ | ||
104 | _gl_CTX tmp_ctx = *(_gl_CTX *) ctx; | ||
105 | OPENSSL_FN (_Final) ((unsigned char *) res, &tmp_ctx); | ||
106 | return res; | ||
107 | } | ||
108 | |||
109 | /* Undef so we can include multiple times. */ | ||
110 | #undef GL_CRYPTO_FN | ||
111 | #undef OPENSSL_FN | ||
112 | #undef _GLCRYPTO_PREFIX | ||
113 | #undef OPENSSL_ALG | ||
114 | #undef GL_OPENSSL_NAME | ||
115 | |||
116 | _GL_INLINE_HEADER_END | ||