diff options
| author | Paul Eggert | 2013-12-08 00:05:36 -0800 |
|---|---|---|
| committer | Paul Eggert | 2013-12-08 00:05:36 -0800 |
| commit | e9551b12f8c17876a32e1cd075c83af3e7950980 (patch) | |
| tree | a1130605fdba681c9118c71f5edd4ee2b9ef4c51 /lib | |
| parent | 02033d491fa708e28bb3568ff85dab4d0ceb076b (diff) | |
| download | emacs-e9551b12f8c17876a32e1cd075c83af3e7950980.tar.gz emacs-e9551b12f8c17876a32e1cd075c83af3e7950980.zip | |
Use libcrypto's checksum implementations if available, for speed.
On commonly used platform libcrypto uses architecture-specific
assembly code, which is significantly faster than the C code we
were using. See Pádraig Brady's note in
<http://lists.gnu.org/archive/html/bug-gnulib/2013-12/msg00000.html>.
Merge from gnulib, incorporating:
2013-12-07 md5, sha1, sha256, sha512: add gl_SET_CRYPTO_CHECK_DEFAULT
2013-12-07 md5, sha1, sha256, sha512: add 'auto', and set-default method
2013-12-04 include_next: minimize code duplication
2013-12-03 md5, sha1, sha256, sha512: support mandating use of openssl
2013-12-02 md5, sha1, sha256, sha512: use openssl routines if available
* configure.ac (--without-all): Set with_openssl_default too.
Use gl_SET_CRYPTO_CHECK_DEFAULT to default to 'auto'.
(HAVE_LIB_CRYPTO): New var.
Say whether Emacs is configured to use a crypto library.
* lib/gl_openssl.h, m4/absolute-header.m4, m4/gl-openssl.m4:
New files, copied from gnulib.
* lib/gnulib.mk, m4/gnulib-comp.m4: Regenerate.
* lib/md5.c, lib/md5.h, lib/sha1.c, lib/sha1.h:
* lib/sha256.c, lib/sha256.h, lib/sha512.c, lib/sha512.h:
* m4/include_next.m4, m4/md5.m4, m4/sha1.m4, m4/sha256.m4, m4/sha512.m4:
Update from gnulib.
* src/Makefile.in (LIB_CRYPTO): New macro.
(LIBES): Use it.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/gl_openssl.h | 116 | ||||
| -rw-r--r-- | lib/gnulib.mk | 17 | ||||
| -rw-r--r-- | lib/md5.c | 7 | ||||
| -rw-r--r-- | lib/md5.h | 20 | ||||
| -rw-r--r-- | lib/sha1.c | 7 | ||||
| -rw-r--r-- | lib/sha1.h | 21 | ||||
| -rw-r--r-- | lib/sha256.c | 7 | ||||
| -rw-r--r-- | lib/sha256.h | 30 | ||||
| -rw-r--r-- | lib/sha512.c | 7 | ||||
| -rw-r--r-- | lib/sha512.h | 31 |
10 files changed, 229 insertions, 34 deletions
diff --git a/lib/gl_openssl.h b/lib/gl_openssl.h new file mode 100644 index 00000000000..1fb61066b11 --- /dev/null +++ b/lib/gl_openssl.h | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | /* gl_openssl.h -- wrap openssl crypto hash routines in gnulib interface | ||
| 2 | |||
| 3 | Copyright (C) 2013 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify | ||
| 6 | it under the terms of the GNU General Public License as published by | ||
| 7 | the Free Software Foundation; either version 3 of the License, or | ||
| 8 | (at your option) any later version. | ||
| 9 | |||
| 10 | This program 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 General Public License for more details. | ||
| 14 | |||
| 15 | You should have received a copy of the GNU General Public License | ||
| 16 | along with this program. If not, see <http://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 *res) | ||
| 94 | { OPENSSL_FN (_Final) (res, (_gl_CTX *) ctx); return res; } | ||
| 95 | |||
| 96 | GL_OPENSSL_INLINE void * | ||
| 97 | GL_CRYPTO_FN (_buffer) (const char *buf, size_t len, void *res) | ||
| 98 | { return OPENSSL_FN () ((const unsigned char *) buf, len, res); } | ||
| 99 | |||
| 100 | GL_OPENSSL_INLINE void * | ||
| 101 | GL_CRYPTO_FN (_read_ctx) (const struct _gl_ctx *ctx, void *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) (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 | ||
diff --git a/lib/gnulib.mk b/lib/gnulib.mk index 44cdc0cdaac..9617452a796 100644 --- a/lib/gnulib.mk +++ b/lib/gnulib.mk | |||
| @@ -33,6 +33,15 @@ libgnu_a_LIBADD = $(gl_LIBOBJS) | |||
| 33 | libgnu_a_DEPENDENCIES = $(gl_LIBOBJS) | 33 | libgnu_a_DEPENDENCIES = $(gl_LIBOBJS) |
| 34 | EXTRA_libgnu_a_SOURCES = | 34 | EXTRA_libgnu_a_SOURCES = |
| 35 | 35 | ||
| 36 | ## begin gnulib module absolute-header | ||
| 37 | |||
| 38 | # Use this preprocessor expression to decide whether #include_next works. | ||
| 39 | # Do not rely on a 'configure'-time test for this, since the expression | ||
| 40 | # might appear in an installed header, which is used by some other compiler. | ||
| 41 | HAVE_INCLUDE_NEXT = (__GNUC__ || 60000000 <= __DECC_VER) | ||
| 42 | |||
| 43 | ## end gnulib module absolute-header | ||
| 44 | |||
| 36 | ## begin gnulib module alloca-opt | 45 | ## begin gnulib module alloca-opt |
| 37 | 46 | ||
| 38 | BUILT_SOURCES += $(ALLOCA_H) | 47 | BUILT_SOURCES += $(ALLOCA_H) |
| @@ -152,7 +161,7 @@ EXTRA_DIST += count-trailing-zeros.h | |||
| 152 | 161 | ||
| 153 | libgnu_a_SOURCES += md5.c | 162 | libgnu_a_SOURCES += md5.c |
| 154 | 163 | ||
| 155 | EXTRA_DIST += md5.h | 164 | EXTRA_DIST += gl_openssl.h md5.h |
| 156 | 165 | ||
| 157 | ## end gnulib module crypto/md5 | 166 | ## end gnulib module crypto/md5 |
| 158 | 167 | ||
| @@ -160,7 +169,7 @@ EXTRA_DIST += md5.h | |||
| 160 | 169 | ||
| 161 | libgnu_a_SOURCES += sha1.c | 170 | libgnu_a_SOURCES += sha1.c |
| 162 | 171 | ||
| 163 | EXTRA_DIST += sha1.h | 172 | EXTRA_DIST += gl_openssl.h sha1.h |
| 164 | 173 | ||
| 165 | ## end gnulib module crypto/sha1 | 174 | ## end gnulib module crypto/sha1 |
| 166 | 175 | ||
| @@ -168,7 +177,7 @@ EXTRA_DIST += sha1.h | |||
| 168 | 177 | ||
| 169 | libgnu_a_SOURCES += sha256.c | 178 | libgnu_a_SOURCES += sha256.c |
| 170 | 179 | ||
| 171 | EXTRA_DIST += sha256.h | 180 | EXTRA_DIST += gl_openssl.h sha256.h |
| 172 | 181 | ||
| 173 | ## end gnulib module crypto/sha256 | 182 | ## end gnulib module crypto/sha256 |
| 174 | 183 | ||
| @@ -176,7 +185,7 @@ EXTRA_DIST += sha256.h | |||
| 176 | 185 | ||
| 177 | libgnu_a_SOURCES += sha512.c | 186 | libgnu_a_SOURCES += sha512.c |
| 178 | 187 | ||
| 179 | EXTRA_DIST += sha512.h | 188 | EXTRA_DIST += gl_openssl.h sha512.h |
| 180 | 189 | ||
| 181 | ## end gnulib module crypto/sha512 | 190 | ## end gnulib module crypto/sha512 |
| 182 | 191 | ||
| @@ -21,6 +21,9 @@ | |||
| 21 | 21 | ||
| 22 | #include <config.h> | 22 | #include <config.h> |
| 23 | 23 | ||
| 24 | #if HAVE_OPENSSL_MD5 | ||
| 25 | # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE | ||
| 26 | #endif | ||
| 24 | #include "md5.h" | 27 | #include "md5.h" |
| 25 | 28 | ||
| 26 | #include <stdalign.h> | 29 | #include <stdalign.h> |
| @@ -61,6 +64,7 @@ | |||
| 61 | # error "invalid BLOCKSIZE" | 64 | # error "invalid BLOCKSIZE" |
| 62 | #endif | 65 | #endif |
| 63 | 66 | ||
| 67 | #if ! HAVE_OPENSSL_MD5 | ||
| 64 | /* This array contains the bytes used to pad the buffer to the next | 68 | /* This array contains the bytes used to pad the buffer to the next |
| 65 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ | 69 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ |
| 66 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; | 70 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; |
| @@ -128,6 +132,7 @@ md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) | |||
| 128 | 132 | ||
| 129 | return md5_read_ctx (ctx, resbuf); | 133 | return md5_read_ctx (ctx, resbuf); |
| 130 | } | 134 | } |
| 135 | #endif | ||
| 131 | 136 | ||
| 132 | /* Compute MD5 message digest for bytes read from STREAM. The | 137 | /* Compute MD5 message digest for bytes read from STREAM. The |
| 133 | resulting message digest number will be written into the 16 bytes | 138 | resulting message digest number will be written into the 16 bytes |
| @@ -202,6 +207,7 @@ process_partial_block: | |||
| 202 | return 0; | 207 | return 0; |
| 203 | } | 208 | } |
| 204 | 209 | ||
| 210 | #if ! HAVE_OPENSSL_MD5 | ||
| 205 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The | 211 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The |
| 206 | result is always in little endian byte order, so that a byte-wise | 212 | result is always in little endian byte order, so that a byte-wise |
| 207 | output yields to the wanted ASCII representation of the message | 213 | output yields to the wanted ASCII representation of the message |
| @@ -459,3 +465,4 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) | |||
| 459 | ctx->C = C; | 465 | ctx->C = C; |
| 460 | ctx->D = D; | 466 | ctx->D = D; |
| 461 | } | 467 | } |
| 468 | #endif | ||
| @@ -23,6 +23,10 @@ | |||
| 23 | #include <stdio.h> | 23 | #include <stdio.h> |
| 24 | #include <stdint.h> | 24 | #include <stdint.h> |
| 25 | 25 | ||
| 26 | # if HAVE_OPENSSL_MD5 | ||
| 27 | # include <openssl/md5.h> | ||
| 28 | # endif | ||
| 29 | |||
| 26 | #define MD5_DIGEST_SIZE 16 | 30 | #define MD5_DIGEST_SIZE 16 |
| 27 | #define MD5_BLOCK_SIZE 64 | 31 | #define MD5_BLOCK_SIZE 64 |
| 28 | 32 | ||
| @@ -57,6 +61,10 @@ | |||
| 57 | extern "C" { | 61 | extern "C" { |
| 58 | # endif | 62 | # endif |
| 59 | 63 | ||
| 64 | # if HAVE_OPENSSL_MD5 | ||
| 65 | # define GL_OPENSSL_NAME 5 | ||
| 66 | # include "gl_openssl.h" | ||
| 67 | # else | ||
| 60 | /* Structure to save state of computation between the single steps. */ | 68 | /* Structure to save state of computation between the single steps. */ |
| 61 | struct md5_ctx | 69 | struct md5_ctx |
| 62 | { | 70 | { |
| @@ -106,11 +114,6 @@ extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW; | |||
| 106 | extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW; | 114 | extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW; |
| 107 | 115 | ||
| 108 | 116 | ||
| 109 | /* Compute MD5 message digest for bytes read from STREAM. The | ||
| 110 | resulting message digest number will be written into the 16 bytes | ||
| 111 | beginning at RESBLOCK. */ | ||
| 112 | extern int __md5_stream (FILE *stream, void *resblock) __THROW; | ||
| 113 | |||
| 114 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The | 117 | /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The |
| 115 | result is always in little endian byte order, so that a byte-wise | 118 | result is always in little endian byte order, so that a byte-wise |
| 116 | output yields to the wanted ASCII representation of the message | 119 | output yields to the wanted ASCII representation of the message |
| @@ -118,6 +121,13 @@ extern int __md5_stream (FILE *stream, void *resblock) __THROW; | |||
| 118 | extern void *__md5_buffer (const char *buffer, size_t len, | 121 | extern void *__md5_buffer (const char *buffer, size_t len, |
| 119 | void *resblock) __THROW; | 122 | void *resblock) __THROW; |
| 120 | 123 | ||
| 124 | # endif | ||
| 125 | /* Compute MD5 message digest for bytes read from STREAM. The | ||
| 126 | resulting message digest number will be written into the 16 bytes | ||
| 127 | beginning at RESBLOCK. */ | ||
| 128 | extern int __md5_stream (FILE *stream, void *resblock) __THROW; | ||
| 129 | |||
| 130 | |||
| 121 | # ifdef __cplusplus | 131 | # ifdef __cplusplus |
| 122 | } | 132 | } |
| 123 | # endif | 133 | # endif |
diff --git a/lib/sha1.c b/lib/sha1.c index 778389affc5..b1a24f9968d 100644 --- a/lib/sha1.c +++ b/lib/sha1.c | |||
| @@ -23,6 +23,9 @@ | |||
| 23 | 23 | ||
| 24 | #include <config.h> | 24 | #include <config.h> |
| 25 | 25 | ||
| 26 | #if HAVE_OPENSSL_SHA1 | ||
| 27 | # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE | ||
| 28 | #endif | ||
| 26 | #include "sha1.h" | 29 | #include "sha1.h" |
| 27 | 30 | ||
| 28 | #include <stdalign.h> | 31 | #include <stdalign.h> |
| @@ -46,6 +49,7 @@ | |||
| 46 | # error "invalid BLOCKSIZE" | 49 | # error "invalid BLOCKSIZE" |
| 47 | #endif | 50 | #endif |
| 48 | 51 | ||
| 52 | #if ! HAVE_OPENSSL_SHA1 | ||
| 49 | /* This array contains the bytes used to pad the buffer to the next | 53 | /* This array contains the bytes used to pad the buffer to the next |
| 50 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ | 54 | 64-byte boundary. (RFC 1321, 3.1: Step 1) */ |
| 51 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; | 55 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; |
| @@ -116,6 +120,7 @@ sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf) | |||
| 116 | 120 | ||
| 117 | return sha1_read_ctx (ctx, resbuf); | 121 | return sha1_read_ctx (ctx, resbuf); |
| 118 | } | 122 | } |
| 123 | #endif | ||
| 119 | 124 | ||
| 120 | /* Compute SHA1 message digest for bytes read from STREAM. The | 125 | /* Compute SHA1 message digest for bytes read from STREAM. The |
| 121 | resulting message digest number will be written into the 16 bytes | 126 | resulting message digest number will be written into the 16 bytes |
| @@ -190,6 +195,7 @@ sha1_stream (FILE *stream, void *resblock) | |||
| 190 | return 0; | 195 | return 0; |
| 191 | } | 196 | } |
| 192 | 197 | ||
| 198 | #if ! HAVE_OPENSSL_SHA1 | ||
| 193 | /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The | 199 | /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The |
| 194 | result is always in little endian byte order, so that a byte-wise | 200 | result is always in little endian byte order, so that a byte-wise |
| 195 | output yields to the wanted ASCII representation of the message | 201 | output yields to the wanted ASCII representation of the message |
| @@ -424,3 +430,4 @@ sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) | |||
| 424 | e = ctx->E += e; | 430 | e = ctx->E += e; |
| 425 | } | 431 | } |
| 426 | } | 432 | } |
| 433 | #endif | ||
diff --git a/lib/sha1.h b/lib/sha1.h index ddd386f9144..0ec953e8357 100644 --- a/lib/sha1.h +++ b/lib/sha1.h | |||
| @@ -22,12 +22,20 @@ | |||
| 22 | # include <stdio.h> | 22 | # include <stdio.h> |
| 23 | # include <stdint.h> | 23 | # include <stdint.h> |
| 24 | 24 | ||
| 25 | # if HAVE_OPENSSL_SHA1 | ||
| 26 | # include <openssl/sha.h> | ||
| 27 | # endif | ||
| 28 | |||
| 25 | # ifdef __cplusplus | 29 | # ifdef __cplusplus |
| 26 | extern "C" { | 30 | extern "C" { |
| 27 | # endif | 31 | # endif |
| 28 | 32 | ||
| 29 | #define SHA1_DIGEST_SIZE 20 | 33 | #define SHA1_DIGEST_SIZE 20 |
| 30 | 34 | ||
| 35 | # if HAVE_OPENSSL_SHA1 | ||
| 36 | # define GL_OPENSSL_NAME 1 | ||
| 37 | # include "gl_openssl.h" | ||
| 38 | # else | ||
| 31 | /* Structure to save state of computation between the single steps. */ | 39 | /* Structure to save state of computation between the single steps. */ |
| 32 | struct sha1_ctx | 40 | struct sha1_ctx |
| 33 | { | 41 | { |
| @@ -42,7 +50,6 @@ struct sha1_ctx | |||
| 42 | uint32_t buffer[32]; | 50 | uint32_t buffer[32]; |
| 43 | }; | 51 | }; |
| 44 | 52 | ||
| 45 | |||
| 46 | /* Initialize structure containing state of computation. */ | 53 | /* Initialize structure containing state of computation. */ |
| 47 | extern void sha1_init_ctx (struct sha1_ctx *ctx); | 54 | extern void sha1_init_ctx (struct sha1_ctx *ctx); |
| 48 | 55 | ||
| @@ -73,17 +80,19 @@ extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf); | |||
| 73 | extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf); | 80 | extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf); |
| 74 | 81 | ||
| 75 | 82 | ||
| 76 | /* Compute SHA1 message digest for bytes read from STREAM. The | ||
| 77 | resulting message digest number will be written into the 20 bytes | ||
| 78 | beginning at RESBLOCK. */ | ||
| 79 | extern int sha1_stream (FILE *stream, void *resblock); | ||
| 80 | |||
| 81 | /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The | 83 | /* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The |
| 82 | result is always in little endian byte order, so that a byte-wise | 84 | result is always in little endian byte order, so that a byte-wise |
| 83 | output yields to the wanted ASCII representation of the message | 85 | output yields to the wanted ASCII representation of the message |
| 84 | digest. */ | 86 | digest. */ |
| 85 | extern void *sha1_buffer (const char *buffer, size_t len, void *resblock); | 87 | extern void *sha1_buffer (const char *buffer, size_t len, void *resblock); |
| 86 | 88 | ||
| 89 | # endif | ||
| 90 | /* Compute SHA1 message digest for bytes read from STREAM. The | ||
| 91 | resulting message digest number will be written into the 20 bytes | ||
| 92 | beginning at RESBLOCK. */ | ||
| 93 | extern int sha1_stream (FILE *stream, void *resblock); | ||
| 94 | |||
| 95 | |||
| 87 | # ifdef __cplusplus | 96 | # ifdef __cplusplus |
| 88 | } | 97 | } |
| 89 | # endif | 98 | # endif |
diff --git a/lib/sha256.c b/lib/sha256.c index 4b2cee37fb5..9d6912cdc79 100644 --- a/lib/sha256.c +++ b/lib/sha256.c | |||
| @@ -22,6 +22,9 @@ | |||
| 22 | 22 | ||
| 23 | #include <config.h> | 23 | #include <config.h> |
| 24 | 24 | ||
| 25 | #if HAVE_OPENSSL_SHA256 | ||
| 26 | # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE | ||
| 27 | #endif | ||
| 25 | #include "sha256.h" | 28 | #include "sha256.h" |
| 26 | 29 | ||
| 27 | #include <stdalign.h> | 30 | #include <stdalign.h> |
| @@ -45,6 +48,7 @@ | |||
| 45 | # error "invalid BLOCKSIZE" | 48 | # error "invalid BLOCKSIZE" |
| 46 | #endif | 49 | #endif |
| 47 | 50 | ||
| 51 | #if ! HAVE_OPENSSL_SHA256 | ||
| 48 | /* This array contains the bytes used to pad the buffer to the next | 52 | /* This array contains the bytes used to pad the buffer to the next |
| 49 | 64-byte boundary. */ | 53 | 64-byte boundary. */ |
| 50 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; | 54 | static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; |
| @@ -163,6 +167,7 @@ sha224_finish_ctx (struct sha256_ctx *ctx, void *resbuf) | |||
| 163 | sha256_conclude_ctx (ctx); | 167 | sha256_conclude_ctx (ctx); |
| 164 | return sha224_read_ctx (ctx, resbuf); | 168 | return sha224_read_ctx (ctx, resbuf); |
| 165 | } | 169 | } |
| 170 | #endif | ||
| 166 | 171 | ||
| 167 | /* Compute SHA256 message digest for bytes read from STREAM. The | 172 | /* Compute SHA256 message digest for bytes read from STREAM. The |
| 168 | resulting message digest number will be written into the 32 bytes | 173 | resulting message digest number will be written into the 32 bytes |
| @@ -308,6 +313,7 @@ sha224_stream (FILE *stream, void *resblock) | |||
| 308 | return 0; | 313 | return 0; |
| 309 | } | 314 | } |
| 310 | 315 | ||
| 316 | #if ! HAVE_OPENSSL_SHA256 | ||
| 311 | /* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The | 317 | /* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The |
| 312 | result is always in little endian byte order, so that a byte-wise | 318 | result is always in little endian byte order, so that a byte-wise |
| 313 | output yields to the wanted ASCII representation of the message | 319 | output yields to the wanted ASCII representation of the message |
| @@ -567,3 +573,4 @@ sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) | |||
| 567 | h = ctx->state[7] += h; | 573 | h = ctx->state[7] += h; |
| 568 | } | 574 | } |
| 569 | } | 575 | } |
| 576 | #endif | ||
diff --git a/lib/sha256.h b/lib/sha256.h index 7e6252285bb..6ee326b0466 100644 --- a/lib/sha256.h +++ b/lib/sha256.h | |||
| @@ -21,10 +21,23 @@ | |||
| 21 | # include <stdio.h> | 21 | # include <stdio.h> |
| 22 | # include <stdint.h> | 22 | # include <stdint.h> |
| 23 | 23 | ||
| 24 | # if HAVE_OPENSSL_SHA256 | ||
| 25 | # include <openssl/sha.h> | ||
| 26 | # endif | ||
| 27 | |||
| 24 | # ifdef __cplusplus | 28 | # ifdef __cplusplus |
| 25 | extern "C" { | 29 | extern "C" { |
| 26 | # endif | 30 | # endif |
| 27 | 31 | ||
| 32 | enum { SHA224_DIGEST_SIZE = 224 / 8 }; | ||
| 33 | enum { SHA256_DIGEST_SIZE = 256 / 8 }; | ||
| 34 | |||
| 35 | # if HAVE_OPENSSL_SHA256 | ||
| 36 | # define GL_OPENSSL_NAME 224 | ||
| 37 | # include "gl_openssl.h" | ||
| 38 | # define GL_OPENSSL_NAME 256 | ||
| 39 | # include "gl_openssl.h" | ||
| 40 | # else | ||
| 28 | /* Structure to save state of computation between the single steps. */ | 41 | /* Structure to save state of computation between the single steps. */ |
| 29 | struct sha256_ctx | 42 | struct sha256_ctx |
| 30 | { | 43 | { |
| @@ -35,9 +48,6 @@ struct sha256_ctx | |||
| 35 | uint32_t buffer[32]; | 48 | uint32_t buffer[32]; |
| 36 | }; | 49 | }; |
| 37 | 50 | ||
| 38 | enum { SHA224_DIGEST_SIZE = 224 / 8 }; | ||
| 39 | enum { SHA256_DIGEST_SIZE = 256 / 8 }; | ||
| 40 | |||
| 41 | /* Initialize structure containing state of computation. */ | 51 | /* Initialize structure containing state of computation. */ |
| 42 | extern void sha256_init_ctx (struct sha256_ctx *ctx); | 52 | extern void sha256_init_ctx (struct sha256_ctx *ctx); |
| 43 | extern void sha224_init_ctx (struct sha256_ctx *ctx); | 53 | extern void sha224_init_ctx (struct sha256_ctx *ctx); |
| @@ -71,12 +81,6 @@ extern void *sha256_read_ctx (const struct sha256_ctx *ctx, void *resbuf); | |||
| 71 | extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf); | 81 | extern void *sha224_read_ctx (const struct sha256_ctx *ctx, void *resbuf); |
| 72 | 82 | ||
| 73 | 83 | ||
| 74 | /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The | ||
| 75 | resulting message digest number will be written into the 32 (28) bytes | ||
| 76 | beginning at RESBLOCK. */ | ||
| 77 | extern int sha256_stream (FILE *stream, void *resblock); | ||
| 78 | extern int sha224_stream (FILE *stream, void *resblock); | ||
| 79 | |||
| 80 | /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The | 84 | /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The |
| 81 | result is always in little endian byte order, so that a byte-wise | 85 | result is always in little endian byte order, so that a byte-wise |
| 82 | output yields to the wanted ASCII representation of the message | 86 | output yields to the wanted ASCII representation of the message |
| @@ -84,6 +88,14 @@ extern int sha224_stream (FILE *stream, void *resblock); | |||
| 84 | extern void *sha256_buffer (const char *buffer, size_t len, void *resblock); | 88 | extern void *sha256_buffer (const char *buffer, size_t len, void *resblock); |
| 85 | extern void *sha224_buffer (const char *buffer, size_t len, void *resblock); | 89 | extern void *sha224_buffer (const char *buffer, size_t len, void *resblock); |
| 86 | 90 | ||
| 91 | # endif | ||
| 92 | /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The | ||
| 93 | resulting message digest number will be written into the 32 (28) bytes | ||
| 94 | beginning at RESBLOCK. */ | ||
| 95 | extern int sha256_stream (FILE *stream, void *resblock); | ||
| 96 | extern int sha224_stream (FILE *stream, void *resblock); | ||
| 97 | |||
| 98 | |||
| 87 | # ifdef __cplusplus | 99 | # ifdef __cplusplus |
| 88 | } | 100 | } |
| 89 | # endif | 101 | # endif |
diff --git a/lib/sha512.c b/lib/sha512.c index 79f11257474..8429bb9b03a 100644 --- a/lib/sha512.c +++ b/lib/sha512.c | |||
| @@ -22,6 +22,9 @@ | |||
| 22 | 22 | ||
| 23 | #include <config.h> | 23 | #include <config.h> |
| 24 | 24 | ||
| 25 | #if HAVE_OPENSSL_SHA512 | ||
| 26 | # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE | ||
| 27 | #endif | ||
| 25 | #include "sha512.h" | 28 | #include "sha512.h" |
| 26 | 29 | ||
| 27 | #include <stdalign.h> | 30 | #include <stdalign.h> |
| @@ -52,6 +55,7 @@ | |||
| 52 | # error "invalid BLOCKSIZE" | 55 | # error "invalid BLOCKSIZE" |
| 53 | #endif | 56 | #endif |
| 54 | 57 | ||
| 58 | #if ! HAVE_OPENSSL_SHA512 | ||
| 55 | /* This array contains the bytes used to pad the buffer to the next | 59 | /* This array contains the bytes used to pad the buffer to the next |
| 56 | 128-byte boundary. */ | 60 | 128-byte boundary. */ |
| 57 | static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ }; | 61 | static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ }; |
| @@ -171,6 +175,7 @@ sha384_finish_ctx (struct sha512_ctx *ctx, void *resbuf) | |||
| 171 | sha512_conclude_ctx (ctx); | 175 | sha512_conclude_ctx (ctx); |
| 172 | return sha384_read_ctx (ctx, resbuf); | 176 | return sha384_read_ctx (ctx, resbuf); |
| 173 | } | 177 | } |
| 178 | #endif | ||
| 174 | 179 | ||
| 175 | /* Compute SHA512 message digest for bytes read from STREAM. The | 180 | /* Compute SHA512 message digest for bytes read from STREAM. The |
| 176 | resulting message digest number will be written into the 64 bytes | 181 | resulting message digest number will be written into the 64 bytes |
| @@ -316,6 +321,7 @@ sha384_stream (FILE *stream, void *resblock) | |||
| 316 | return 0; | 321 | return 0; |
| 317 | } | 322 | } |
| 318 | 323 | ||
| 324 | #if ! HAVE_OPENSSL_SHA512 | ||
| 319 | /* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The | 325 | /* Compute SHA512 message digest for LEN bytes beginning at BUFFER. The |
| 320 | result is always in little endian byte order, so that a byte-wise | 326 | result is always in little endian byte order, so that a byte-wise |
| 321 | output yields to the wanted ASCII representation of the message | 327 | output yields to the wanted ASCII representation of the message |
| @@ -619,3 +625,4 @@ sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx) | |||
| 619 | h = ctx->state[7] = u64plus (ctx->state[7], h); | 625 | h = ctx->state[7] = u64plus (ctx->state[7], h); |
| 620 | } | 626 | } |
| 621 | } | 627 | } |
| 628 | #endif | ||
diff --git a/lib/sha512.h b/lib/sha512.h index 2e78a5f9404..1de93da2f8a 100644 --- a/lib/sha512.h +++ b/lib/sha512.h | |||
| @@ -19,13 +19,25 @@ | |||
| 19 | # define SHA512_H 1 | 19 | # define SHA512_H 1 |
| 20 | 20 | ||
| 21 | # include <stdio.h> | 21 | # include <stdio.h> |
| 22 | |||
| 23 | # include "u64.h" | 22 | # include "u64.h" |
| 24 | 23 | ||
| 24 | # if HAVE_OPENSSL_SHA512 | ||
| 25 | # include <openssl/sha.h> | ||
| 26 | # endif | ||
| 27 | |||
| 25 | # ifdef __cplusplus | 28 | # ifdef __cplusplus |
| 26 | extern "C" { | 29 | extern "C" { |
| 27 | # endif | 30 | # endif |
| 28 | 31 | ||
| 32 | enum { SHA384_DIGEST_SIZE = 384 / 8 }; | ||
| 33 | enum { SHA512_DIGEST_SIZE = 512 / 8 }; | ||
| 34 | |||
| 35 | # if HAVE_OPENSSL_SHA512 | ||
| 36 | # define GL_OPENSSL_NAME 384 | ||
| 37 | # include "gl_openssl.h" | ||
| 38 | # define GL_OPENSSL_NAME 512 | ||
| 39 | # include "gl_openssl.h" | ||
| 40 | # else | ||
| 29 | /* Structure to save state of computation between the single steps. */ | 41 | /* Structure to save state of computation between the single steps. */ |
| 30 | struct sha512_ctx | 42 | struct sha512_ctx |
| 31 | { | 43 | { |
| @@ -36,9 +48,6 @@ struct sha512_ctx | |||
| 36 | u64 buffer[32]; | 48 | u64 buffer[32]; |
| 37 | }; | 49 | }; |
| 38 | 50 | ||
| 39 | enum { SHA384_DIGEST_SIZE = 384 / 8 }; | ||
| 40 | enum { SHA512_DIGEST_SIZE = 512 / 8 }; | ||
| 41 | |||
| 42 | /* Initialize structure containing state of computation. */ | 51 | /* Initialize structure containing state of computation. */ |
| 43 | extern void sha512_init_ctx (struct sha512_ctx *ctx); | 52 | extern void sha512_init_ctx (struct sha512_ctx *ctx); |
| 44 | extern void sha384_init_ctx (struct sha512_ctx *ctx); | 53 | extern void sha384_init_ctx (struct sha512_ctx *ctx); |
| @@ -75,12 +84,6 @@ extern void *sha512_read_ctx (const struct sha512_ctx *ctx, void *resbuf); | |||
| 75 | extern void *sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf); | 84 | extern void *sha384_read_ctx (const struct sha512_ctx *ctx, void *resbuf); |
| 76 | 85 | ||
| 77 | 86 | ||
| 78 | /* Compute SHA512 (SHA384) message digest for bytes read from STREAM. The | ||
| 79 | resulting message digest number will be written into the 64 (48) bytes | ||
| 80 | beginning at RESBLOCK. */ | ||
| 81 | extern int sha512_stream (FILE *stream, void *resblock); | ||
| 82 | extern int sha384_stream (FILE *stream, void *resblock); | ||
| 83 | |||
| 84 | /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER. The | 87 | /* Compute SHA512 (SHA384) message digest for LEN bytes beginning at BUFFER. The |
| 85 | result is always in little endian byte order, so that a byte-wise | 88 | result is always in little endian byte order, so that a byte-wise |
| 86 | output yields to the wanted ASCII representation of the message | 89 | output yields to the wanted ASCII representation of the message |
| @@ -88,6 +91,14 @@ extern int sha384_stream (FILE *stream, void *resblock); | |||
| 88 | extern void *sha512_buffer (const char *buffer, size_t len, void *resblock); | 91 | extern void *sha512_buffer (const char *buffer, size_t len, void *resblock); |
| 89 | extern void *sha384_buffer (const char *buffer, size_t len, void *resblock); | 92 | extern void *sha384_buffer (const char *buffer, size_t len, void *resblock); |
| 90 | 93 | ||
| 94 | # endif | ||
| 95 | /* Compute SHA512 (SHA384) message digest for bytes read from STREAM. The | ||
| 96 | resulting message digest number will be written into the 64 (48) bytes | ||
| 97 | beginning at RESBLOCK. */ | ||
| 98 | extern int sha512_stream (FILE *stream, void *resblock); | ||
| 99 | extern int sha384_stream (FILE *stream, void *resblock); | ||
| 100 | |||
| 101 | |||
| 91 | # ifdef __cplusplus | 102 | # ifdef __cplusplus |
| 92 | } | 103 | } |
| 93 | # endif | 104 | # endif |