2007-09-20 04:37:48 +08:00
|
|
|
/* Functions to compute SHA256 message digest of files or memory blocks.
|
|
|
|
according to the definition of SHA256 in FIPS 180-2.
|
2021-01-03 03:32:25 +08:00
|
|
|
Copyright (C) 2007-2021 Free Software Foundation, Inc.
|
2007-09-20 04:37:48 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-10 07:18:22 +08:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2007-09-20 04:37:48 +08:00
|
|
|
|
|
|
|
/* Written by Ulrich Drepper <drepper@redhat.com>, 2007. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <endian.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-05-01 23:46:34 +08:00
|
|
|
#include <stdint.h>
|
2007-09-20 04:37:48 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "sha256.h"
|
|
|
|
|
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
|
|
# ifdef _LIBC
|
|
|
|
# include <byteswap.h>
|
|
|
|
# define SWAP(n) bswap_32 (n)
|
2011-07-03 00:30:03 +08:00
|
|
|
# define SWAP64(n) bswap_64 (n)
|
2007-09-20 04:37:48 +08:00
|
|
|
# else
|
|
|
|
# define SWAP(n) \
|
|
|
|
(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
|
2011-07-03 00:30:03 +08:00
|
|
|
# define SWAP64(n) \
|
|
|
|
(((n) << 56) \
|
|
|
|
| (((n) & 0xff00) << 40) \
|
|
|
|
| (((n) & 0xff0000) << 24) \
|
|
|
|
| (((n) & 0xff000000) << 8) \
|
|
|
|
| (((n) >> 8) & 0xff000000) \
|
|
|
|
| (((n) >> 24) & 0xff0000) \
|
|
|
|
| (((n) >> 40) & 0xff00) \
|
|
|
|
| ((n) >> 56))
|
2007-09-20 04:37:48 +08:00
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define SWAP(n) (n)
|
2011-07-03 00:30:03 +08:00
|
|
|
# define SWAP64(n) (n)
|
2007-09-20 04:37:48 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* This array contains the bytes used to pad the buffer to the next
|
|
|
|
64-byte boundary. (FIPS 180-2:5.1.1) */
|
|
|
|
static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
|
|
|
|
|
|
|
|
|
|
|
|
/* Constants for SHA256 from FIPS 180-2:4.2.2. */
|
|
|
|
static const uint32_t K[64] =
|
|
|
|
{
|
|
|
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
|
|
|
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
|
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
|
|
|
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
|
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
|
|
|
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
|
|
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
|
|
|
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
|
|
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
|
|
|
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
|
|
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
|
|
|
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
|
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
|
|
|
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
|
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
|
|
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
|
|
};
|
|
|
|
|
2016-10-29 03:47:44 +08:00
|
|
|
void __sha256_process_block (const void *, size_t, struct sha256_ctx *);
|
2007-09-20 04:37:48 +08:00
|
|
|
|
|
|
|
/* Initialize structure containing state of computation.
|
|
|
|
(FIPS 180-2:5.3.2) */
|
|
|
|
void
|
2015-10-17 04:21:49 +08:00
|
|
|
__sha256_init_ctx (struct sha256_ctx *ctx)
|
2007-09-20 04:37:48 +08:00
|
|
|
{
|
|
|
|
ctx->H[0] = 0x6a09e667;
|
|
|
|
ctx->H[1] = 0xbb67ae85;
|
|
|
|
ctx->H[2] = 0x3c6ef372;
|
|
|
|
ctx->H[3] = 0xa54ff53a;
|
|
|
|
ctx->H[4] = 0x510e527f;
|
|
|
|
ctx->H[5] = 0x9b05688c;
|
|
|
|
ctx->H[6] = 0x1f83d9ab;
|
|
|
|
ctx->H[7] = 0x5be0cd19;
|
|
|
|
|
2011-07-03 00:30:03 +08:00
|
|
|
ctx->total64 = 0;
|
2007-09-20 04:37:48 +08:00
|
|
|
ctx->buflen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Process the remaining bytes in the internal buffer and the usual
|
|
|
|
prolog according to the standard and write the result to RESBUF.
|
|
|
|
|
|
|
|
IMPORTANT: On some systems it is required that RESBUF is correctly
|
|
|
|
aligned for a 32 bits value. */
|
|
|
|
void *
|
2015-10-17 04:21:49 +08:00
|
|
|
__sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
|
2007-09-20 04:37:48 +08:00
|
|
|
{
|
|
|
|
/* Take yet unprocessed bytes into account. */
|
|
|
|
uint32_t bytes = ctx->buflen;
|
|
|
|
size_t pad;
|
|
|
|
|
|
|
|
/* Now count remaining bytes. */
|
2011-07-03 00:30:03 +08:00
|
|
|
ctx->total64 += bytes;
|
2007-09-20 04:37:48 +08:00
|
|
|
|
|
|
|
pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
|
|
|
|
memcpy (&ctx->buffer[bytes], fillbuf, pad);
|
|
|
|
|
|
|
|
/* Put the 64-bit file length in *bits* at the end of the buffer. */
|
2014-03-27 04:37:35 +08:00
|
|
|
#if _STRING_ARCH_unaligned
|
2011-07-20 04:53:43 +08:00
|
|
|
ctx->buffer64[(bytes + pad) / 8] = SWAP64 (ctx->total64 << 3);
|
2011-07-03 00:30:03 +08:00
|
|
|
#else
|
2011-07-20 04:53:43 +08:00
|
|
|
ctx->buffer32[(bytes + pad + 4) / 4] = SWAP (ctx->total[TOTAL64_low] << 3);
|
Break some lines before not after operators.
The GNU Coding Standards specify that line breaks in expressions
should go before an operator, not after one. This patch fixes various
code to do this. It only changes code that appears to be mostly
following GNU style anyway, not files and directories with
substantially different formatting. It is not exhaustive even for
files using GNU style (for example, changes to sysdeps files are
deferred for subsequent cleanups). Some files changed are shared with
gnulib, but most are specific to glibc. Changes were made manually,
with places to change found by grep (so some cases, e.g. where the
operator was followed by a comment at end of line, are particularly
liable to have been missed by grep, but I did include cases where the
operator was followed by backslash-newline).
This patch generally does not attempt to address other coding style
issues in the expressions changed (for example, missing spaces before
'(', or lack of parentheses to ensure indentation of continuation
lines properly reflects operator precedence).
Tested for x86_64, and with build-many-glibcs.py.
* benchtests/bench-memmem.c (simple_memmem): Break lines before
rather than after operators.
* benchtests/bench-skeleton.c (TIMESPEC_AFTER): Likewise.
* crypt/md5.c (md5_finish_ctx): Likewise.
* crypt/sha256.c (__sha256_finish_ctx): Likewise.
* crypt/sha512.c (__sha512_finish_ctx): Likewise.
* elf/cache.c (load_aux_cache): Likewise.
* elf/dl-load.c (open_verify): Likewise.
* elf/get-dynamic-info.h (elf_get_dynamic_info): Likewise.
* elf/readelflib.c (process_elf_file): Likewise.
* elf/rtld.c (dl_main): Likewise.
* elf/sprof.c (generate_call_graph): Likewise.
* hurd/ctty-input.c (_hurd_ctty_input): Likewise.
* hurd/ctty-output.c (_hurd_ctty_output): Likewise.
* hurd/dtable.c (reauth_dtable): Likewise.
* hurd/getdport.c (__getdport): Likewise.
* hurd/hurd/signal.h (_hurd_interrupted_rpc_timeout): Likewise.
* hurd/hurd/sigpreempt.h (HURD_PREEMPT_SIGNAL_P): Likewise.
* hurd/hurdfault.c (_hurdsig_fault_catch_exception_raise):
Likewise.
* hurd/hurdioctl.c (fioctl): Likewise.
* hurd/hurdselect.c (_hurd_select): Likewise.
* hurd/hurdsig.c (_hurdsig_abort_rpcs): Likewise.
(STOPSIGS): Likewise.
* hurd/hurdstartup.c (_hurd_startup): Likewise.
* hurd/intr-msg.c (_hurd_intr_rpc_mach_msg): Likewise.
* hurd/lookup-retry.c (__hurd_file_name_lookup_retry): Likewise.
* hurd/msgportdemux.c (msgport_server): Likewise.
* hurd/setauth.c (_hurd_setauth): Likewise.
* include/features.h (__GLIBC_USE_DEPRECATED_SCANF): Likewise.
* libio/libioP.h [IO_DEBUG] (CHECK_FILE): Likewise.
* locale/programs/ld-ctype.c (set_class_defaults): Likewise.
* localedata/tests-mbwc/tst_swscanf.c (tst_swscanf): Likewise.
* login/tst-utmp.c (do_check): Likewise.
(simulate_login): Likewise.
* mach/lowlevellock.h (lll_lock): Likewise.
(lll_trylock): Likewise.
* math/test-fenv.c (ALL_EXC): Likewise.
* math/test-fenvinline.c (ALL_EXC): Likewise.
* misc/sys/cdefs.h (__attribute_deprecated_msg__): Likewise.
* nis/nis_call.c (__do_niscall3): Likewise.
* nis/nis_callback.c (cb_prog_1): Likewise.
* nis/nis_defaults.c (searchaccess): Likewise.
* nis/nis_findserv.c (__nis_findfastest_with_timeout): Likewise.
* nis/nis_ismember.c (internal_ismember): Likewise.
* nis/nis_local_names.c (nis_local_principal): Likewise.
* nis/nss_nis/nis-rpc.c (_nss_nis_getrpcbyname_r): Likewise.
* nis/nss_nisplus/nisplus-netgrp.c (_nss_nisplus_getnetgrent_r):
Likewise.
* nis/ypclnt.c (yp_match): Likewise.
(yp_first): Likewise.
(yp_next): Likewise.
(yp_master): Likewise.
(yp_order): Likewise.
* nscd/hstcache.c (cache_addhst): Likewise.
* nscd/initgrcache.c (addinitgroupsX): Likewise.
* nss/nss_compat/compat-pwd.c (copy_pwd_changes): Likewise.
(internal_getpwuid_r): Likewise.
* nss/nss_compat/compat-spwd.c (copy_spwd_changes): Likewise.
* posix/glob.h (__GLOB_FLAGS): Likewise.
* posix/regcomp.c (peek_token): Likewise.
(peek_token_bracket): Likewise.
(parse_expression): Likewise.
* posix/regexec.c (sift_states_iter_mb): Likewise.
(check_node_accept_bytes): Likewise.
* posix/tst-spawn3.c (do_test): Likewise.
* posix/wordexp-test.c (testit): Likewise.
* posix/wordexp.c (parse_tilde): Likewise.
(exec_comm): Likewise.
* posix/wordexp.h (__WRDE_FLAGS): Likewise.
* resource/vtimes.c (TIMEVAL_TO_VTIMES): Likewise.
* setjmp/sigjmp.c (__sigjmp_save): Likewise.
* stdio-common/printf_fp.c (__printf_fp_l): Likewise.
* stdio-common/tst-fileno.c (do_test): Likewise.
* stdio-common/vfprintf-internal.c (vfprintf): Likewise.
* stdlib/strfmon_l.c (__vstrfmon_l_internal): Likewise.
* stdlib/strtod_l.c (round_and_return): Likewise.
(____STRTOF_INTERNAL): Likewise.
* stdlib/tst-strfrom.h (TEST_STRFROM): Likewise.
* string/strcspn.c (STRCSPN): Likewise.
* string/test-memmem.c (simple_memmem): Likewise.
* termios/tcsetattr.c (tcsetattr): Likewise.
* time/alt_digit.c (_nl_parse_alt_digit): Likewise.
* time/asctime.c (asctime_internal): Likewise.
* time/strptime_l.c (__strptime_internal): Likewise.
* time/sys/time.h (timercmp): Likewise.
* time/tzfile.c (__tzfile_compute): Likewise.
2019-02-22 09:32:36 +08:00
|
|
|
ctx->buffer32[(bytes + pad) / 4] = SWAP ((ctx->total[TOTAL64_high] << 3)
|
|
|
|
| (ctx->total[TOTAL64_low] >> 29));
|
2011-07-03 00:30:03 +08:00
|
|
|
#endif
|
2007-09-20 04:37:48 +08:00
|
|
|
|
|
|
|
/* Process last bytes. */
|
2016-10-29 03:47:44 +08:00
|
|
|
__sha256_process_block (ctx->buffer, bytes + pad + 8, ctx);
|
2007-09-20 04:37:48 +08:00
|
|
|
|
|
|
|
/* Put result from CTX in first 32 bytes following RESBUF. */
|
|
|
|
for (unsigned int i = 0; i < 8; ++i)
|
|
|
|
((uint32_t *) resbuf)[i] = SWAP (ctx->H[i]);
|
|
|
|
|
|
|
|
return resbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2015-10-17 04:21:49 +08:00
|
|
|
__sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
|
2007-09-20 04:37:48 +08:00
|
|
|
{
|
|
|
|
/* When we already have some bits in our internal buffer concatenate
|
|
|
|
both inputs first. */
|
|
|
|
if (ctx->buflen != 0)
|
|
|
|
{
|
|
|
|
size_t left_over = ctx->buflen;
|
|
|
|
size_t add = 128 - left_over > len ? len : 128 - left_over;
|
|
|
|
|
|
|
|
memcpy (&ctx->buffer[left_over], buffer, add);
|
|
|
|
ctx->buflen += add;
|
|
|
|
|
|
|
|
if (ctx->buflen > 64)
|
|
|
|
{
|
2016-10-29 03:47:44 +08:00
|
|
|
__sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
|
2007-09-20 04:37:48 +08:00
|
|
|
|
|
|
|
ctx->buflen &= 63;
|
|
|
|
/* The regions in the following copy operation cannot overlap. */
|
|
|
|
memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
|
|
|
|
ctx->buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = (const char *) buffer + add;
|
|
|
|
len -= add;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process available complete blocks. */
|
|
|
|
if (len >= 64)
|
|
|
|
{
|
|
|
|
#if !_STRING_ARCH_unaligned
|
|
|
|
/* To check alignment gcc has an appropriate operator. Other
|
|
|
|
compilers don't. */
|
|
|
|
# if __GNUC__ >= 2
|
|
|
|
# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
|
|
|
|
# else
|
|
|
|
# define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint32_t) != 0)
|
|
|
|
# endif
|
|
|
|
if (UNALIGNED_P (buffer))
|
|
|
|
while (len > 64)
|
|
|
|
{
|
2016-10-29 03:47:44 +08:00
|
|
|
__sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
|
2007-09-20 04:37:48 +08:00
|
|
|
buffer = (const char *) buffer + 64;
|
|
|
|
len -= 64;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2016-10-29 03:47:44 +08:00
|
|
|
__sha256_process_block (buffer, len & ~63, ctx);
|
2007-09-20 04:37:48 +08:00
|
|
|
buffer = (const char *) buffer + (len & ~63);
|
|
|
|
len &= 63;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move remaining bytes into internal buffer. */
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
size_t left_over = ctx->buflen;
|
|
|
|
|
|
|
|
memcpy (&ctx->buffer[left_over], buffer, len);
|
|
|
|
left_over += len;
|
|
|
|
if (left_over >= 64)
|
|
|
|
{
|
2016-10-29 03:47:44 +08:00
|
|
|
__sha256_process_block (ctx->buffer, 64, ctx);
|
2007-09-20 04:37:48 +08:00
|
|
|
left_over -= 64;
|
|
|
|
memcpy (ctx->buffer, &ctx->buffer[64], left_over);
|
|
|
|
}
|
|
|
|
ctx->buflen = left_over;
|
|
|
|
}
|
|
|
|
}
|
2012-11-13 07:17:31 +08:00
|
|
|
|
|
|
|
#include <sha256-block.c>
|