From 89d1e5b8f23627481f0d40a46ad79ebe60a423a0 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Wed, 2 Nov 2022 23:12:32 -0700 Subject: [PATCH] Rearrange code and make a faster sha256sum program --- libc/integral/c.inc | 11 +- libc/runtime/cocmd.c | 6 +- libc/stdio/alloc.c | 62 +++++++ libc/stdio/fclose.c | 15 +- libc/stdio/fclose_s.c | 10 +- libc/stdio/fdopen.c | 14 +- libc/stdio/ferror_unlocked.c | 5 +- libc/stdio/flushlbf.c | 1 - libc/stdio/fmemopen.c | 25 ++- libc/stdio/internal.h | 6 +- libc/stdio/setvbuf.c | 9 +- libc/stdio/stderr-init.S | 2 +- libc/stdio/stderr.c | 1 - libc/stdio/stdin-init.S | 2 +- libc/stdio/stdin.c | 1 - libc/stdio/stdio.h | 28 +-- libc/stdio/stdout-init.S | 2 +- libc/stdio/stdout.c | 3 +- libc/thread/pthread_create.c | 1 - third_party/mbedtls/md5.c | 88 +-------- third_party/mbedtls/md5t.c | 110 +++++++++++ third_party/mbedtls/sha1.c | 95 +--------- third_party/mbedtls/sha1t.c | 117 ++++++++++++ third_party/mbedtls/sha256.c | 121 ------------ third_party/mbedtls/sha256t.c | 144 +++++++++++++++ third_party/mbedtls/sha512.c | 145 --------------- third_party/mbedtls/sha512t.c | 169 +++++++++++++++++ tool/build/lib/panel.c | 1 + tool/build/pledge.c | 1 + tool/build/sha256sum.c | 243 +++++++++++++++++++++++++ tool/emacs/cosmo-cpp-constants.el | 7 + tool/emacs/cosmo-platform-constants.el | 5 +- 32 files changed, 933 insertions(+), 517 deletions(-) create mode 100644 libc/stdio/alloc.c create mode 100644 third_party/mbedtls/md5t.c create mode 100644 third_party/mbedtls/sha1t.c create mode 100644 third_party/mbedtls/sha256t.c create mode 100644 third_party/mbedtls/sha512t.c create mode 100644 tool/build/sha256sum.c diff --git a/libc/integral/c.inc b/libc/integral/c.inc index 2d5629fd5..9931953b7 100644 --- a/libc/integral/c.inc +++ b/libc/integral/c.inc @@ -52,8 +52,11 @@ #endif #endif -#if defined(__STRICT_ANSI__) || \ - (!defined(__GNUC__) && !__has_builtin(unreachable)) +#ifdef _MSC_VER +#define __builtin_unreachable() __assume(0) +#elif defined(__STRICT_ANSI__) || \ + !((__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 405 || \ + defined(__clang__) || defined(__INTEL_COMPILER)) #define __builtin_unreachable() \ for (;;) { \ } @@ -234,7 +237,9 @@ typedef struct { #endif #ifndef dontinline -#if !defined(__STRICT_ANSI__) && \ +#ifdef _MSC_VER +#define dontinline __declspec(noinline) +#elif !defined(__STRICT_ANSI__) && \ (__has_attribute(__noinline__) || \ (__GNUC__ + 0) * 100 + (__GNUC_MINOR__ + 0) >= 301) #define dontinline __attribute__((__noinline__)) diff --git a/libc/runtime/cocmd.c b/libc/runtime/cocmd.c index 3100fff88..2252d436d 100644 --- a/libc/runtime/cocmd.c +++ b/libc/runtime/cocmd.c @@ -83,9 +83,9 @@ static void Log(const char *s, ...) { va_start(va, s); errbuf[0] = 0; do { - strlcat(errbuf, s, sizeof(argbuf)); + strlcat(errbuf, s, sizeof(errbuf)); } while ((s = va_arg(va, const char *))); - strlcat(errbuf, "\n", sizeof(argbuf)); + strlcat(errbuf, "\n", sizeof(errbuf)); Write(2, errbuf); va_end(va); } @@ -95,7 +95,7 @@ static wontreturn void Wexit(int rc, const char *s, ...) { va_start(va, s); errbuf[0] = 0; do { - strlcat(errbuf, s, sizeof(argbuf)); + strlcat(errbuf, s, sizeof(errbuf)); } while ((s = va_arg(va, const char *))); Write(2, errbuf); va_end(va); diff --git a/libc/stdio/alloc.c b/libc/stdio/alloc.c new file mode 100644 index 000000000..013154cf0 --- /dev/null +++ b/libc/stdio/alloc.c @@ -0,0 +1,62 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" +#include "libc/intrin/atomic.h" +#include "libc/intrin/kmalloc.h" +#include "libc/stdio/internal.h" +#include "libc/str/str.h" +#include "libc/thread/thread.h" + +static _Atomic(FILE *) __stdio_freelist; + +FILE *__stdio_alloc(void) { + FILE *f; + f = atomic_load_explicit(&__stdio_freelist, memory_order_relaxed); + while (f) { + if (atomic_compare_exchange_weak_explicit( + &__stdio_freelist, &f, + atomic_load_explicit(&f->next, memory_order_relaxed), + memory_order_relaxed, memory_order_relaxed)) { + atomic_store_explicit(&f->next, 0, memory_order_relaxed); + break; + } + } + if (!f) { + f = kmalloc(sizeof(FILE)); + } + if (f) { + ((pthread_mutex_t *)f->lock)->_type = PTHREAD_MUTEX_RECURSIVE; + } + return f; +} + +void __stdio_free(FILE *f) { + FILE *g; + _unassert(!atomic_load_explicit(&f->next, memory_order_relaxed)); + bzero(f, sizeof(*f)); + g = atomic_load_explicit(&__stdio_freelist, memory_order_relaxed); + for (;;) { + atomic_store_explicit(&f->next, g, memory_order_relaxed); + if (atomic_compare_exchange_weak_explicit(&__stdio_freelist, &g, f, + memory_order_relaxed, + memory_order_relaxed)) { + break; + } + } +} diff --git a/libc/stdio/fclose.c b/libc/stdio/fclose.c index 092639843..28131e951 100644 --- a/libc/stdio/fclose.c +++ b/libc/stdio/fclose.c @@ -19,6 +19,7 @@ #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/errno.h" +#include "libc/intrin/weaken.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/internal.h" @@ -27,21 +28,21 @@ /** * Closes standard i/o stream and its underlying thing. * - * @param f is the file object, which is always free if it's heap, - * otherwise its resources are released and fields updated + * @param f is the file object * @return 0 on success or -1 on error, which can be a trick for * differentiating between EOF and real errors during previous * i/o calls, without needing to call ferror() - * @see fclose_s() */ int fclose(FILE *f) { int rc; if (!f) return 0; __fflush_unregister(f); fflush(f); - free_s(&f->getln); - if (!f->nofree) { - free_s(&f->buf); + if (_weaken(free)) { + _weaken(free)(f->getln); + if (!f->nofree && f->buf != f->mem) { + _weaken(free)(f->buf); + } } f->state = EOF; if (f->noclose) { @@ -55,6 +56,6 @@ int fclose(FILE *f) { errno = f->state; rc = EOF; } - free_s(&f); + __stdio_free(f); return rc; } diff --git a/libc/stdio/fclose_s.c b/libc/stdio/fclose_s.c index aeb86aa01..3b0a887ab 100644 --- a/libc/stdio/fclose_s.c +++ b/libc/stdio/fclose_s.c @@ -19,14 +19,8 @@ #include "libc/intrin/lockxchg.h" #include "libc/stdio/stdio.h" -/** - * Closes standard i/o stream and its underlying thing. - * - * @param f is the heap file object pointer, to close+free+clear - * @return 0 on success or -1 on error, which can be a trick for - * differentiating between EOF and real errors during previous - * i/o calls, without needing to call ferror() - */ +// TODO(jart): delete + int fclose_s(FILE **fp) { FILE *f = NULL; return fclose(lockxchg(fp, &f)); diff --git a/libc/stdio/fdopen.c b/libc/stdio/fdopen.c index ad295a27d..5ffa8c668 100644 --- a/libc/stdio/fdopen.c +++ b/libc/stdio/fdopen.c @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/mem/mem.h" #include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/o.h" @@ -34,19 +33,16 @@ */ FILE *fdopen(int fd, const char *mode) { FILE *f; - if ((f = calloc(1, sizeof(FILE)))) { + if ((f = __stdio_alloc())) { f->fd = fd; f->bufmode = ischardev(fd) ? _IOLBF : _IOFBF; f->iomode = fopenflags(mode); - ((pthread_mutex_t *)f->lock)->_type = PTHREAD_MUTEX_RECURSIVE; + f->buf = f->mem; f->size = BUFSIZ; - if ((f->buf = malloc(f->size))) { - if ((f->iomode & O_ACCMODE) != O_RDONLY) { - __fflush_register(f); - } - return f; + if ((f->iomode & O_ACCMODE) != O_RDONLY) { + __fflush_register(f); } - free(f); + return f; } return NULL; } diff --git a/libc/stdio/ferror_unlocked.c b/libc/stdio/ferror_unlocked.c index 46f2e988f..15981a3af 100644 --- a/libc/stdio/ferror_unlocked.c +++ b/libc/stdio/ferror_unlocked.c @@ -16,16 +16,17 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/errno.h" #include "libc/stdio/stdio.h" /** * Returns nonzero if stream is in error state. * * @param f is file stream pointer - * @return non-zero if and only if it's an error state + * @return non-zero w/ errno only if `f` is in error state * @note EOF doesn't count * @see ferror(), feof() */ errno_t ferror_unlocked(FILE *f) { - return f->state > 0 ? f->state : 0; + return f->state > 0 ? (errno = f->state) : 0; } diff --git a/libc/stdio/flushlbf.c b/libc/stdio/flushlbf.c index a254e3c3c..4cb0ae050 100644 --- a/libc/stdio/flushlbf.c +++ b/libc/stdio/flushlbf.c @@ -17,7 +17,6 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" -#include "libc/thread/thread.h" #include "libc/stdio/fflush.internal.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" diff --git a/libc/stdio/fmemopen.c b/libc/stdio/fmemopen.c index 1425ae79a..363ac1437 100644 --- a/libc/stdio/fmemopen.c +++ b/libc/stdio/fmemopen.c @@ -16,36 +16,48 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/intrin/weaken.h" #include "libc/mem/mem.h" +#include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/sysv/consts/o.h" #include "libc/sysv/errfuns.h" #include "libc/thread/thread.h" +// TODO(jart): POSIX says buffer needs to grow in write modes? + /** * Opens buffer as stream. * * @param buf becomes owned by this function, and is allocated if NULL * @return new stream or NULL w/ errno + * @raise ENOMEM if `buf` is NULL and we failed to allocate it + * @raise ENOMEM if `buf` is NULL and malloc() wasn't linked + * @raise EINVAL if `buf` is NULL when `+` isn't in `mode` */ FILE *fmemopen(void *buf, size_t size, const char *mode) { FILE *f; char *p; + int iomode; unsigned flags; - if (size && size > 0x7ffff000) { + iomode = fopenflags(mode); + if ((size && size > 0x7ffff000) || // + (!buf && (iomode & O_ACCMODE) != O_RDWR)) { einval(); return NULL; } - if (!(f = calloc(1, sizeof(FILE)))) { + if (!(f = __stdio_alloc())) { return NULL; } if (buf) { f->nofree = true; } else { if (!size) size = BUFSIZ; - if (!(buf = calloc(1, size))) { - free(f); + // TODO(jart): Why do we need calloc()? + if (!_weaken(calloc) || !(buf = _weaken(calloc)(1, size))) { + __stdio_free(f); + enomem(); return NULL; } } @@ -53,9 +65,8 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) { f->buf = buf; f->end = size; f->size = size; - f->iomode = fopenflags(mode); - ((pthread_mutex_t *)f->lock)->_type = PTHREAD_MUTEX_RECURSIVE; - if (f->iomode & O_APPEND) { + f->iomode = iomode; + if (iomode & O_APPEND) { if ((p = memchr(buf, '\0', size))) { f->beg = p - (char *)buf; } else { diff --git a/libc/stdio/internal.h b/libc/stdio/internal.h index f4320b40a..c6749ddd0 100644 --- a/libc/stdio/internal.h +++ b/libc/stdio/internal.h @@ -7,16 +7,14 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -extern char g_stdinbuf[BUFSIZ]; -extern char g_stdoutbuf[BUFSIZ]; -extern char g_stderrbuf[BUFSIZ]; - hidden extern uint64_t g_rando; int __fflush_impl(FILE *) hidden; int __fflush_register(FILE *) hidden; void __fflush_unregister(FILE *) hidden; bool __stdio_isok(FILE *) hidden; +FILE *__stdio_alloc(void) hidden; +void __stdio_free(FILE *) hidden; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/stdio/setvbuf.c b/libc/stdio/setvbuf.c index ac58cf08e..0d7b862e5 100644 --- a/libc/stdio/setvbuf.c +++ b/libc/stdio/setvbuf.c @@ -16,6 +16,8 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/intrin/weaken.h" +#include "libc/mem/mem.h" #include "libc/runtime/runtime.h" #include "libc/stdio/lock.internal.h" #include "libc/stdio/stdio.h" @@ -35,7 +37,12 @@ int setvbuf(FILE *f, char *buf, int mode, size_t size) { flockfile(f); if (buf) { if (!size) size = BUFSIZ; - if (!f->nofree && f->buf != buf) free_s(&f->buf); + if (!f->nofree && // + f->buf != buf && // + f->buf != f->mem && // + _weaken(free)) { + _weaken(free)(f->buf); + } f->buf = buf; f->size = size; f->nofree = true; diff --git a/libc/stdio/stderr-init.S b/libc/stdio/stderr-init.S index 4be4fc667..ebbfcfd9a 100644 --- a/libc/stdio/stderr-init.S +++ b/libc/stdio/stderr-init.S @@ -31,7 +31,7 @@ pop 12(%rax) mov O_WRONLY,%edx mov %edx,4(%rax) #→ f.iomode - ezlea __stderr_buf,cx + lea 0x50(%rax),%rcx #← f.mem mov %rcx,0x18(%rax) #→ f.buf movl $BUFSIZ,0x20(%rax) #→ f.size movb $PTHREAD_MUTEX_RECURSIVE,0x38+4(%rax) #→ f.lock._type diff --git a/libc/stdio/stderr.c b/libc/stdio/stderr.c index 9529789e3..b63b9d765 100644 --- a/libc/stdio/stderr.c +++ b/libc/stdio/stderr.c @@ -27,7 +27,6 @@ STATIC_YOINK("_init_stderr"); FILE *stderr; hidden FILE __stderr; -hidden unsigned char __stderr_buf[BUFSIZ]; static textstartup void __stderr_init() { __fflush_register(stderr); diff --git a/libc/stdio/stdin-init.S b/libc/stdio/stdin-init.S index 36d79b6b3..caf7af75b 100644 --- a/libc/stdio/stdin-init.S +++ b/libc/stdio/stdin-init.S @@ -27,7 +27,7 @@ ezlea __stdin,ax mov O_RDONLY,%edx mov %edx,4(%rax) #→ f.iomode - ezlea __stdin_buf,cx + lea 0x50(%rax),%rcx #← f.mem mov %rcx,0x18(%rax) #→ f.buf movl $BUFSIZ,0x20(%rax) #→ f.size movb $PTHREAD_MUTEX_RECURSIVE,0x38+4(%rax) #→ f.lock._type diff --git a/libc/stdio/stdin.c b/libc/stdio/stdin.c index 716a0b613..c828d7e48 100644 --- a/libc/stdio/stdin.c +++ b/libc/stdio/stdin.c @@ -27,7 +27,6 @@ STATIC_YOINK("_init_stdin"); FILE *stdin; hidden FILE __stdin; -hidden unsigned char __stdin_buf[BUFSIZ]; static textstartup void __stdin_init() { __fflush_register(stdin); diff --git a/libc/stdio/stdio.h b/libc/stdio/stdio.h index 22e4d5966..b36a03579 100644 --- a/libc/stdio/stdio.h +++ b/libc/stdio/stdio.h @@ -15,19 +15,21 @@ COSMOPOLITAN_C_START_ ╚────────────────────────────────────────────────────────────────────────────│*/ typedef struct FILE { - uint8_t bufmode; /* 0x00 _IOFBF, etc. (ignored if fd=-1) */ - bool noclose; /* 0x01 for fake dup() todo delete! */ - uint32_t iomode; /* 0x04 O_RDONLY, etc. (ignored if fd=-1) */ - int32_t state; /* 0x08 0=OK, -1=EOF, >0=errno */ - int fd; /* 0x0c ≥0=fd, -1=closed|buffer */ - uint32_t beg; /* 0x10 */ - uint32_t end; /* 0x14 */ - char *buf; /* 0x18 */ - uint32_t size; /* 0x20 */ - uint32_t nofree; /* 0x24 */ - int pid; /* 0x28 */ - char *getln; /* 0x30 */ - char lock[16]; /* 0x38 */ + uint8_t bufmode; /* 0x00 _IOFBF, etc. (ignored if fd=-1) */ + bool noclose; /* 0x01 for fake dup() todo delete! */ + uint32_t iomode; /* 0x04 O_RDONLY, etc. (ignored if fd=-1) */ + int32_t state; /* 0x08 0=OK, -1=EOF, >0=errno */ + int fd; /* 0x0c ≥0=fd, -1=closed|buffer */ + uint32_t beg; /* 0x10 */ + uint32_t end; /* 0x14 */ + char *buf; /* 0x18 */ + uint32_t size; /* 0x20 */ + uint32_t nofree; /* 0x24 */ + int pid; /* 0x28 */ + char *getln; /* 0x30 */ + char lock[16]; /* 0x38 */ + _Atomic(struct FILE *) next; /* 0x48 */ + char mem[BUFSIZ]; /* 0x50 */ } FILE; extern FILE *stdin; diff --git a/libc/stdio/stdout-init.S b/libc/stdio/stdout-init.S index 71dd87488..db218be67 100644 --- a/libc/stdio/stdout-init.S +++ b/libc/stdio/stdout-init.S @@ -29,7 +29,7 @@ pop 0x0c(%rax) #→ f.fd mov O_WRONLY,%edx mov %edx,4(%rax) #→ f.iomode - ezlea __stdout_buf,cx + lea 0x50(%rax),%rcx #← f.mem mov %rcx,0x18(%rax) #→ f.buf movl $BUFSIZ,0x20(%rax) #→ f.size movb $PTHREAD_MUTEX_RECURSIVE,0x38+4(%rax) #→ f.lock._type diff --git a/libc/stdio/stdout.c b/libc/stdio/stdout.c index 493974895..2539c0966 100644 --- a/libc/stdio/stdout.c +++ b/libc/stdio/stdout.c @@ -16,9 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/pushpop.h" #include "libc/calls/calls.h" #include "libc/dce.h" +#include "libc/intrin/pushpop.h" #include "libc/stdio/internal.h" #include "libc/stdio/stdio.h" @@ -30,7 +30,6 @@ STATIC_YOINK("_init_stdout"); FILE *stdout; hidden FILE __stdout; -hidden unsigned char __stdout_buf[BUFSIZ]; static textstartup void __stdout_init() { struct FILE *sf; diff --git a/libc/thread/pthread_create.c b/libc/thread/pthread_create.c index 84df4dde6..408a793ee 100644 --- a/libc/thread/pthread_create.c +++ b/libc/thread/pthread_create.c @@ -38,7 +38,6 @@ #include "libc/runtime/clone.internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" -#include "libc/stdio/fflush.internal.h" #include "libc/stdio/stdio.h" #include "libc/sysv/consts/clone.h" #include "libc/sysv/consts/map.h" diff --git a/third_party/mbedtls/md5.c b/third_party/mbedtls/md5.c index ce67a44e1..21018d3fc 100644 --- a/third_party/mbedtls/md5.c +++ b/third_party/mbedtls/md5.c @@ -28,8 +28,8 @@ Mbed TLS (Apache 2.0)\\n\ Copyright ARM Limited\\n\ Copyright Mbed TLS Contributors\""); asm(".include \"libc/disclaimer.inc\""); - /* clang-format off */ + /* * RFC 1321 compliant MD5 implementation * @@ -400,89 +400,3 @@ const mbedtls_md_info_t mbedtls_md5_info = { (void *)mbedtls_md5_finish_ret, (void *)mbedtls_md5_ret, }; - -#if defined(MBEDTLS_SELF_TEST) -/* - * RFC 1321 test vectors - */ -static const unsigned char md5_test_buf[7][81] = -{ - { "" }, - { "a" }, - { "abc" }, - { "message digest" }, - { "abcdefghijklmnopqrstuvwxyz" }, - { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, - { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" } -}; - -static const size_t md5_test_buflen[7] = -{ - 0, 1, 3, 14, 26, 62, 80 -}; - -static const unsigned char md5_test_sum[7][16] = -{ - { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, - 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E }, - { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, - 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 }, - { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, - 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 }, - { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, - 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 }, - { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, - 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B }, - { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, - 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F }, - { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, - 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } -}; - -/** - * \brief Checkup routine - * - * \return 0 if successful, or 1 if the test failed - * - * \warning MD5 is considered a weak message digest and its use - * constitutes a security risk. We recommend considering - * stronger message digests instead. - * - */ -int mbedtls_md5_self_test( int verbose ) -{ - int i, ret = 0; - unsigned char md5sum[16]; - - for( i = 0; i < 7; i++ ) - { - if( verbose != 0 ) - mbedtls_printf( " MD5 test #%d: ", i + 1 ); - - ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum ); - if( ret != 0 ) - goto fail; - - if( timingsafe_bcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) - { - ret = 1; - goto fail; - } - - if( verbose != 0 ) - mbedtls_printf( "passed\n" ); - } - - if( verbose != 0 ) - mbedtls_printf( "\n" ); - - return( 0 ); - -fail: - if( verbose != 0 ) - mbedtls_printf( "failed\n" ); - - return( ret ); -} - -#endif /* MBEDTLS_SELF_TEST */ diff --git a/third_party/mbedtls/md5t.c b/third_party/mbedtls/md5t.c new file mode 100644 index 000000000..03d61073a --- /dev/null +++ b/third_party/mbedtls/md5t.c @@ -0,0 +1,110 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright The Mbed TLS Contributors │ +│ │ +│ Licensed under the Apache License, Version 2.0 (the "License"); │ +│ you may not use this file except in compliance with the License. │ +│ You may obtain a copy of the License at │ +│ │ +│ http://www.apache.org/licenses/LICENSE-2.0 │ +│ │ +│ Unless required by applicable law or agreed to in writing, software │ +│ distributed under the License is distributed on an "AS IS" BASIS, │ +│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ +│ See the License for the specific language governing permissions and │ +│ limitations under the License. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/str/str.h" +#include "third_party/mbedtls/md5.h" +#include "third_party/mbedtls/platform.h" + +asm(".ident\t\"\\n\\n\ +Mbed TLS (Apache 2.0)\\n\ +Copyright ARM Limited\\n\ +Copyright Mbed TLS Contributors\""); +asm(".include \"libc/disclaimer.inc\""); +// clang-format off + +/* + * RFC 1321 test vectors + */ +static const unsigned char md5_test_buf[7][81] = +{ + { "" }, + { "a" }, + { "abc" }, + { "message digest" }, + { "abcdefghijklmnopqrstuvwxyz" }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" }, + { "12345678901234567890123456789012345678901234567890123456789012345678901234567890" } +}; + +static const size_t md5_test_buflen[7] = +{ + 0, 1, 3, 14, 26, 62, 80 +}; + +static const unsigned char md5_test_sum[7][16] = +{ + { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, + 0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E }, + { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8, + 0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 }, + { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0, + 0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 }, + { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D, + 0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 }, + { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00, + 0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B }, + { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5, + 0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F }, + { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55, + 0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } +}; + +/** + * \brief Checkup routine + * + * \return 0 if successful, or 1 if the test failed + * + * \warning MD5 is considered a weak message digest and its use + * constitutes a security risk. We recommend considering + * stronger message digests instead. + * + */ +int mbedtls_md5_self_test( int verbose ) +{ + int i, ret = 0; + unsigned char md5sum[16]; + + for( i = 0; i < 7; i++ ) + { + if( verbose != 0 ) + mbedtls_printf( " MD5 test #%d: ", i + 1 ); + + ret = mbedtls_md5_ret( md5_test_buf[i], md5_test_buflen[i], md5sum ); + if( ret != 0 ) + goto fail; + + if( timingsafe_bcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) + { + ret = 1; + goto fail; + } + + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + + if( verbose != 0 ) + mbedtls_printf( "\n" ); + + return( 0 ); + +fail: + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); + + return( ret ); +} diff --git a/third_party/mbedtls/sha1.c b/third_party/mbedtls/sha1.c index 7efe84d7c..c8e172461 100644 --- a/third_party/mbedtls/sha1.c +++ b/third_party/mbedtls/sha1.c @@ -15,8 +15,8 @@ │ See the License for the specific language governing permissions and │ │ limitations under the License. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/bits.h" #include "libc/intrin/asan.internal.h" +#include "libc/intrin/bits.h" #include "libc/macros.internal.h" #include "libc/nexgen32e/sha.h" #include "libc/nexgen32e/x86feature.h" @@ -577,96 +577,3 @@ const mbedtls_md_info_t mbedtls_sha1_info = { (void *)mbedtls_sha1_finish_ret, (void *)mbedtls_sha1_ret, }; - -#if defined(MBEDTLS_SELF_TEST) -/* - * FIPS-180-1 test vectors - */ -static const unsigned char sha1_test_buf[3][57] = -{ - { "abc" }, - { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, - { "" } -}; - -static const size_t sha1_test_buflen[3] = -{ - 3, 56, 1000 -}; - -static const unsigned char sha1_test_sum[3][20] = -{ - { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, - 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }, - { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, - 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }, - { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, - 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F } -}; - -/** - * \brief The SHA-1 checkup routine. - * - * \warning SHA-1 is considered a weak message digest and its use - * constitutes a security risk. We recommend considering - * stronger message digests instead. - * - * \return \c 0 on success. - * \return \c 1 on failure. - * - */ -int mbedtls_sha1_self_test( int verbose ) -{ - int i, j, buflen, ret = 0; - unsigned char buf[1024]; - unsigned char sha1sum[20]; - mbedtls_sha1_context ctx; - mbedtls_sha1_init( &ctx ); - /* - * SHA-1 - */ - for( i = 0; i < 3; i++ ) - { - if( verbose != 0 ) - mbedtls_printf( " SHA-1 test #%d: ", i + 1 ); - if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) - goto fail; - if( i == 2 ) - { - memset( buf, 'a', buflen = 1000 ); - for( j = 0; j < 1000; j++ ) - { - ret = mbedtls_sha1_update_ret( &ctx, buf, buflen ); - if( ret != 0 ) - goto fail; - } - } - else - { - ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i], - sha1_test_buflen[i] ); - if( ret != 0 ) - goto fail; - } - if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 ) - goto fail; - if( timingsafe_bcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) - { - ret = 1; - goto fail; - } - if( verbose != 0 ) - mbedtls_printf( "passed\n" ); - } - if( verbose != 0 ) - mbedtls_printf( "\n" ); - goto exit; -fail: - if( verbose != 0 ) - mbedtls_printf( "failed\n" ); -exit: - mbedtls_sha1_free( &ctx ); - return( ret ); -} - -#endif /* MBEDTLS_SELF_TEST */ diff --git a/third_party/mbedtls/sha1t.c b/third_party/mbedtls/sha1t.c new file mode 100644 index 000000000..475b8cc4c --- /dev/null +++ b/third_party/mbedtls/sha1t.c @@ -0,0 +1,117 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright The Mbed TLS Contributors │ +│ │ +│ Licensed under the Apache License, Version 2.0 (the "License"); │ +│ you may not use this file except in compliance with the License. │ +│ You may obtain a copy of the License at │ +│ │ +│ http://www.apache.org/licenses/LICENSE-2.0 │ +│ │ +│ Unless required by applicable law or agreed to in writing, software │ +│ distributed under the License is distributed on an "AS IS" BASIS, │ +│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ +│ See the License for the specific language governing permissions and │ +│ limitations under the License. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/str/str.h" +#include "third_party/mbedtls/platform.h" +#include "third_party/mbedtls/sha1.h" + +asm(".ident\t\"\\n\\n\ +Mbed TLS (Apache 2.0)\\n\ +Copyright ARM Limited\\n\ +Copyright Mbed TLS Contributors\""); +asm(".include \"libc/disclaimer.inc\""); +// clang-format off + +/* + * FIPS-180-1 test vectors + */ +static const unsigned char sha1_test_buf[3][57] = +{ + { "abc" }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, + { "" } +}; + +static const size_t sha1_test_buflen[3] = +{ + 3, 56, 1000 +}; + +static const unsigned char sha1_test_sum[3][20] = +{ + { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E, + 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D }, + { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE, + 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 }, + { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E, + 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F } +}; + +/** + * \brief The SHA-1 checkup routine. + * + * \warning SHA-1 is considered a weak message digest and its use + * constitutes a security risk. We recommend considering + * stronger message digests instead. + * + * \return \c 0 on success. + * \return \c 1 on failure. + * + */ +int mbedtls_sha1_self_test( int verbose ) +{ + int i, j, buflen, ret = 0; + unsigned char buf[1024]; + unsigned char sha1sum[20]; + mbedtls_sha1_context ctx; + mbedtls_sha1_init( &ctx ); + /* + * SHA-1 + */ + for( i = 0; i < 3; i++ ) + { + if( verbose != 0 ) + mbedtls_printf( " SHA-1 test #%d: ", i + 1 ); + if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 ) + goto fail; + if( i == 2 ) + { + memset( buf, 'a', buflen = 1000 ); + for( j = 0; j < 1000; j++ ) + { + ret = mbedtls_sha1_update_ret( &ctx, buf, buflen ); + if( ret != 0 ) + goto fail; + } + } + else + { + ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i], + sha1_test_buflen[i] ); + if( ret != 0 ) + goto fail; + } + if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 ) + goto fail; + if( timingsafe_bcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) + { + ret = 1; + goto fail; + } + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + if( verbose != 0 ) + mbedtls_printf( "\n" ); + goto exit; +fail: + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); +exit: + mbedtls_sha1_free( &ctx ); + return( ret ); +} diff --git a/third_party/mbedtls/sha256.c b/third_party/mbedtls/sha256.c index c7543a87a..dd1f6674e 100644 --- a/third_party/mbedtls/sha256.c +++ b/third_party/mbedtls/sha256.c @@ -26,7 +26,6 @@ #include "third_party/mbedtls/endian.h" #include "third_party/mbedtls/error.h" #include "third_party/mbedtls/md.h" -#include "third_party/mbedtls/platform.h" #include "third_party/mbedtls/sha256.h" asm(".ident\t\"\\n\\n\ @@ -510,123 +509,3 @@ const mbedtls_md_info_t mbedtls_sha256_info = { (void *)mbedtls_sha256_finish_ret, mbedtls_sha256_ret_256, }; - -#if defined(MBEDTLS_SELF_TEST) -/* - * FIPS-180-2 test vectors - */ -static const unsigned char sha256_test_buf[3][57] = -{ - { "abc" }, - { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, - { "" } -}; - -static const size_t sha256_test_buflen[3] = -{ - 3, 56, 1000 -}; - -static const unsigned char sha256_test_sum[6][32] = -{ - /* - * SHA-224 test vectors - */ - { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, - 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, - 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, - 0xE3, 0x6C, 0x9D, 0xA7 }, - { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, - 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, - 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, - 0x52, 0x52, 0x25, 0x25 }, - { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, - 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, - 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE, - 0x4E, 0xE7, 0xAD, 0x67 }, - - /* - * SHA-256 test vectors - */ - { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, - 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, - 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, - 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD }, - { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, - 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, - 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, - 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }, - { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, - 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67, - 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E, - 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 } -}; - -/** - * \brief The SHA-224 and SHA-256 checkup routine. - * - * \return \c 0 on success. - * \return \c 1 on failure. - */ -int mbedtls_sha256_self_test( int verbose ) -{ - int i, j, k, buflen, ret = 0; - unsigned char *buf; - unsigned char sha256sum[32]; - mbedtls_sha256_context ctx; - buf = mbedtls_calloc( 1024, sizeof(unsigned char) ); - if( NULL == buf ) - { - if( verbose != 0 ) - mbedtls_printf( "Buffer allocation failed\n" ); - return( 1 ); - } - mbedtls_sha256_init( &ctx ); - for( i = 0; i < 6; i++ ) - { - j = i % 3; - k = i < 3; - if( verbose != 0 ) - mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); - if( ( ret = mbedtls_sha256_starts_ret( &ctx, k ) ) != 0 ) - goto fail; - if( j == 2 ) - { - memset( buf, 'a', buflen = 1000 ); - for( j = 0; j < 1000; j++ ) - { - ret = mbedtls_sha256_update_ret( &ctx, buf, buflen ); - if( ret != 0 ) - goto fail; - } - } - else - { - ret = mbedtls_sha256_update_ret( &ctx, sha256_test_buf[j], - sha256_test_buflen[j] ); - if( ret != 0 ) - goto fail; - } - if( ( ret = mbedtls_sha256_finish_ret( &ctx, sha256sum ) ) != 0 ) - goto fail; - if( timingsafe_bcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 ) - { - ret = 1; - goto fail; - } - if( verbose != 0 ) - mbedtls_printf( "passed\n" ); - } - if( verbose != 0 ) - mbedtls_printf( "\n" ); - goto exit; -fail: - if( verbose != 0 ) - mbedtls_printf( "failed\n" ); -exit: - mbedtls_sha256_free( &ctx ); - mbedtls_free( buf ); - return( ret ); -} - -#endif /* MBEDTLS_SELF_TEST */ diff --git a/third_party/mbedtls/sha256t.c b/third_party/mbedtls/sha256t.c new file mode 100644 index 000000000..cc66b462c --- /dev/null +++ b/third_party/mbedtls/sha256t.c @@ -0,0 +1,144 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright The Mbed TLS Contributors │ +│ │ +│ Licensed under the Apache License, Version 2.0 (the "License"); │ +│ you may not use this file except in compliance with the License. │ +│ You may obtain a copy of the License at │ +│ │ +│ http://www.apache.org/licenses/LICENSE-2.0 │ +│ │ +│ Unless required by applicable law or agreed to in writing, software │ +│ distributed under the License is distributed on an "AS IS" BASIS, │ +│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ +│ See the License for the specific language governing permissions and │ +│ limitations under the License. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/str/str.h" +#include "third_party/mbedtls/platform.h" +#include "third_party/mbedtls/sha256.h" + +asm(".ident\t\"\\n\\n\ +Mbed TLS (Apache 2.0)\\n\ +Copyright ARM Limited\\n\ +Copyright Mbed TLS Contributors\""); +asm(".include \"libc/disclaimer.inc\""); +// clang-format off + +/* + * FIPS-180-2 test vectors + */ +static const unsigned char sha256_test_buf[3][57] = +{ + { "abc" }, + { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, + { "" } +}; + +static const size_t sha256_test_buflen[3] = +{ + 3, 56, 1000 +}; + +static const unsigned char sha256_test_sum[6][32] = +{ + /* + * SHA-224 test vectors + */ + { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, + 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, + 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, + 0xE3, 0x6C, 0x9D, 0xA7 }, + { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, + 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, + 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, + 0x52, 0x52, 0x25, 0x25 }, + { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, + 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, + 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE, + 0x4E, 0xE7, 0xAD, 0x67 }, + + /* + * SHA-256 test vectors + */ + { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, + 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, + 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, + 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD }, + { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, + 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, + 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, + 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }, + { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, + 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67, + 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E, + 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 } +}; + +/** + * \brief The SHA-224 and SHA-256 checkup routine. + * + * \return \c 0 on success. + * \return \c 1 on failure. + */ +int mbedtls_sha256_self_test( int verbose ) +{ + int i, j, k, buflen, ret = 0; + unsigned char *buf; + unsigned char sha256sum[32]; + mbedtls_sha256_context ctx; + buf = mbedtls_calloc( 1024, sizeof(unsigned char) ); + if( NULL == buf ) + { + if( verbose != 0 ) + mbedtls_printf( "Buffer allocation failed\n" ); + return( 1 ); + } + mbedtls_sha256_init( &ctx ); + for( i = 0; i < 6; i++ ) + { + j = i % 3; + k = i < 3; + if( verbose != 0 ) + mbedtls_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); + if( ( ret = mbedtls_sha256_starts_ret( &ctx, k ) ) != 0 ) + goto fail; + if( j == 2 ) + { + memset( buf, 'a', buflen = 1000 ); + for( j = 0; j < 1000; j++ ) + { + ret = mbedtls_sha256_update_ret( &ctx, buf, buflen ); + if( ret != 0 ) + goto fail; + } + } + else + { + ret = mbedtls_sha256_update_ret( &ctx, sha256_test_buf[j], + sha256_test_buflen[j] ); + if( ret != 0 ) + goto fail; + } + if( ( ret = mbedtls_sha256_finish_ret( &ctx, sha256sum ) ) != 0 ) + goto fail; + if( timingsafe_bcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 ) + { + ret = 1; + goto fail; + } + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + if( verbose != 0 ) + mbedtls_printf( "\n" ); + goto exit; +fail: + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); +exit: + mbedtls_sha256_free( &ctx ); + mbedtls_free( buf ); + return( ret ); +} diff --git a/third_party/mbedtls/sha512.c b/third_party/mbedtls/sha512.c index 90da99e3d..7561e3e51 100644 --- a/third_party/mbedtls/sha512.c +++ b/third_party/mbedtls/sha512.c @@ -455,149 +455,4 @@ const mbedtls_md_info_t mbedtls_sha512_info = { mbedtls_sha512_ret_512, }; -#if defined(MBEDTLS_SELF_TEST) - -/* - * FIPS-180-2 test vectors - */ -static const unsigned char sha512_test_buf[3][113] = -{ - { "abc" }, - { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" - "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" }, - { "" } -}; - -static const size_t sha512_test_buflen[3] = -{ - 3, 112, 1000 -}; - -static const unsigned char sha512_test_sum[][64] = -{ -#if !defined(MBEDTLS_SHA512_NO_SHA384) - /* - * SHA-384 test vectors - */ - { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, - 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, - 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, - 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, - 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, - 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 }, - { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, - 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, - 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, - 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, - 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, - 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 }, - { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB, - 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C, - 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52, - 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B, - 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB, - 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 }, -#endif /* !MBEDTLS_SHA512_NO_SHA384 */ - /* - * SHA-512 test vectors - */ - { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, - 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, - 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, - 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, - 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, - 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, - 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, - 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F }, - { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, - 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, - 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, - 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, - 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, - 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, - 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, - 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 }, - { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64, - 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63, - 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28, - 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB, - 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A, - 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B, - 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E, - 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B } -}; - -/** - * \brief The SHA-384 or SHA-512 checkup routine. - * - * \return \c 0 on success. - * \return \c 1 on failure. - */ -int mbedtls_sha512_self_test( int verbose ) -{ - int i, j, k, buflen, ret = 0; - unsigned char *buf; - unsigned char sha512sum[64]; - mbedtls_sha512_context ctx; - buf = mbedtls_calloc( 1024, sizeof(unsigned char) ); - if( NULL == buf ) - { - if( verbose != 0 ) - mbedtls_printf( "Buffer allocation failed\n" ); - return( 1 ); - } - mbedtls_sha512_init( &ctx ); - for( i = 0; i < (int) ARRAYLEN(sha512_test_sum); i++ ) - { - j = i % 3; -#ifndef MBEDTLS_SHA512_NO_SHA384 - k = i < 3; -#else - k = 0; -#endif - if( verbose != 0 ) - mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); - if( ( ret = mbedtls_sha512_starts_ret( &ctx, k ) ) != 0 ) - goto fail; - if( j == 2 ) - { - memset( buf, 'a', buflen = 1000 ); - for( j = 0; j < 1000; j++ ) - { - ret = mbedtls_sha512_update_ret( &ctx, buf, buflen ); - if( ret != 0 ) - goto fail; - } - } - else - { - ret = mbedtls_sha512_update_ret( &ctx, sha512_test_buf[j], - sha512_test_buflen[j] ); - if( ret != 0 ) - goto fail; - } - if( ( ret = mbedtls_sha512_finish_ret( &ctx, sha512sum ) ) != 0 ) - goto fail; - if( timingsafe_bcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) - { - ret = 1; - goto fail; - } - if( verbose != 0 ) - mbedtls_printf( "passed\n" ); - } - if( verbose != 0 ) - mbedtls_printf( "\n" ); - goto exit; -fail: - if( verbose != 0 ) - mbedtls_printf( "failed\n" ); -exit: - mbedtls_sha512_free( &ctx ); - mbedtls_free( buf ); - return( ret ); -} - -#endif /* MBEDTLS_SELF_TEST */ - #endif /* MBEDTLS_SHA512_C */ diff --git a/third_party/mbedtls/sha512t.c b/third_party/mbedtls/sha512t.c new file mode 100644 index 000000000..63c95c8f6 --- /dev/null +++ b/third_party/mbedtls/sha512t.c @@ -0,0 +1,169 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright The Mbed TLS Contributors │ +│ │ +│ Licensed under the Apache License, Version 2.0 (the "License"); │ +│ you may not use this file except in compliance with the License. │ +│ You may obtain a copy of the License at │ +│ │ +│ http://www.apache.org/licenses/LICENSE-2.0 │ +│ │ +│ Unless required by applicable law or agreed to in writing, software │ +│ distributed under the License is distributed on an "AS IS" BASIS, │ +│ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │ +│ See the License for the specific language governing permissions and │ +│ limitations under the License. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/macros.internal.h" +#include "libc/str/str.h" +#include "third_party/mbedtls/platform.h" +#include "third_party/mbedtls/sha512.h" + +asm(".ident\t\"\\n\\n\ +Mbed TLS (Apache 2.0)\\n\ +Copyright ARM Limited\\n\ +Copyright Mbed TLS Contributors\""); +asm(".include \"libc/disclaimer.inc\""); +// clang-format off + +/* + * FIPS-180-2 test vectors + */ +static const unsigned char sha512_test_buf[3][113] = +{ + { "abc" }, + { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" }, + { "" } +}; + +static const size_t sha512_test_buflen[3] = +{ + 3, 112, 1000 +}; + +static const unsigned char sha512_test_sum[][64] = +{ +#if !defined(MBEDTLS_SHA512_NO_SHA384) + /* + * SHA-384 test vectors + */ + { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B, + 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07, + 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63, + 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED, + 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23, + 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 }, + { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8, + 0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47, + 0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2, + 0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12, + 0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9, + 0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 }, + { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB, + 0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C, + 0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52, + 0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B, + 0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB, + 0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 }, +#endif /* !MBEDTLS_SHA512_NO_SHA384 */ + /* + * SHA-512 test vectors + */ + { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, + 0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31, + 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, + 0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, + 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8, + 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, + 0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E, + 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F }, + { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA, + 0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F, + 0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1, + 0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18, + 0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4, + 0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A, + 0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54, + 0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 }, + { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64, + 0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63, + 0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28, + 0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB, + 0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A, + 0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B, + 0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E, + 0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B } +}; + +/** + * \brief The SHA-384 or SHA-512 checkup routine. + * + * \return \c 0 on success. + * \return \c 1 on failure. + */ +int mbedtls_sha512_self_test( int verbose ) +{ + int i, j, k, buflen, ret = 0; + unsigned char *buf; + unsigned char sha512sum[64]; + mbedtls_sha512_context ctx; + buf = mbedtls_calloc( 1024, sizeof(unsigned char) ); + if( NULL == buf ) + { + if( verbose != 0 ) + mbedtls_printf( "Buffer allocation failed\n" ); + return( 1 ); + } + mbedtls_sha512_init( &ctx ); + for( i = 0; i < (int) ARRAYLEN(sha512_test_sum); i++ ) + { + j = i % 3; +#ifndef MBEDTLS_SHA512_NO_SHA384 + k = i < 3; +#else + k = 0; +#endif + if( verbose != 0 ) + mbedtls_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); + if( ( ret = mbedtls_sha512_starts_ret( &ctx, k ) ) != 0 ) + goto fail; + if( j == 2 ) + { + memset( buf, 'a', buflen = 1000 ); + for( j = 0; j < 1000; j++ ) + { + ret = mbedtls_sha512_update_ret( &ctx, buf, buflen ); + if( ret != 0 ) + goto fail; + } + } + else + { + ret = mbedtls_sha512_update_ret( &ctx, sha512_test_buf[j], + sha512_test_buflen[j] ); + if( ret != 0 ) + goto fail; + } + if( ( ret = mbedtls_sha512_finish_ret( &ctx, sha512sum ) ) != 0 ) + goto fail; + if( timingsafe_bcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) + { + ret = 1; + goto fail; + } + if( verbose != 0 ) + mbedtls_printf( "passed\n" ); + } + if( verbose != 0 ) + mbedtls_printf( "\n" ); + goto exit; +fail: + if( verbose != 0 ) + mbedtls_printf( "failed\n" ); +exit: + mbedtls_sha512_free( &ctx ); + mbedtls_free( buf ); + return( ret ); +} diff --git a/tool/build/lib/panel.c b/tool/build/lib/panel.c index b0ac02b8b..817873de5 100644 --- a/tool/build/lib/panel.c +++ b/tool/build/lib/panel.c @@ -57,6 +57,7 @@ ssize_t PrintPanels(int fd, long pn, struct Panel *p, long tyn, long txn) { AppendChar(&b, ' '); x += 1; } + AppendFmt(&b, "\e[%d;%dH", y + 1, x + 1); // bsd utf-8 :( while (x < p[i].right || j < l->i) { wc = '\0'; width = 0; diff --git a/tool/build/pledge.c b/tool/build/pledge.c index 7149965ea..a54f426f6 100644 --- a/tool/build/pledge.c +++ b/tool/build/pledge.c @@ -116,6 +116,7 @@ usage: pledge.com [-hnN] PROG ARGS...\n\ \n\ pledge.com v1.8\n\ copyright 2022 justine alexandra roberts tunney\n\ +notice licenses are embedded in the binary\n\ https://twitter.com/justinetunney\n\ https://linkedin.com/in/jtunney\n\ https://justine.lol/pledge/\n\ diff --git a/tool/build/sha256sum.c b/tool/build/sha256sum.c new file mode 100644 index 000000000..67938f226 --- /dev/null +++ b/tool/build/sha256sum.c @@ -0,0 +1,243 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/assert.h" +#include "libc/calls/calls.h" +#include "libc/errno.h" +#include "libc/fmt/itoa.h" +#include "libc/runtime/runtime.h" +#include "libc/stdio/stdio.h" +#include "libc/str/str.h" +#include "libc/str/tab.internal.h" +#include "third_party/getopt/getopt.h" +#include "third_party/mbedtls/sha256.h" + +#define PROG "sha256sum" +#define USAGE \ + "\ +Usage: " PROG " [-?hbctw] [PATH...]\n\ + -h help\n\ + -c check mode\n\ + -b binary mode\n\ + -t textual mode\n\ + -w warning mode\n\ +\n\ +cosmopolitan " PROG " v1.0\n\ +copyright 2022 justine alexandra roberts tunney\n\ +notice licenses are embedded in the binary\n\ +https://twitter.com/justinetunney\n\ +https://github.com/jart\n\ +\n\ +This program is an Actually Portable Executable (APE) rewrite of the\n\ +GNU coreutils sha256sum command. Our version is better because it goes\n\ +400% faster than coreutils if the Intel® SHA-NI™ ISA is available,\n\ +otherwise this command goes 50% faster than coreutils. This executable\n\ +will work consistently on Linux/Mac/Windows/FreeBSD/NetBSD/OpenBSD, so\n\ +consider vendoring it in your repo to avoid platform portability toil.\n\ +" + +static bool g_warn; +static char g_mode; +static bool g_check; +static int g_mismatches; + +static void GetOpts(int argc, char *argv[]) { + int opt; + g_mode = ' '; + while ((opt = getopt(argc, argv, "?hbctw")) != -1) { + switch (opt) { + case 'w': + g_warn = true; + break; + case 'c': + g_check = true; + break; + case 't': + g_mode = ' '; + break; + case 'b': + g_mode = '*'; + break; + case 'h': + case '?': + write(1, USAGE, sizeof(USAGE) - 1); + exit(0); + default: + write(2, USAGE, sizeof(USAGE) - 1); + exit(64); + } + } +} + +static ssize_t Write(int fd, const char *s, ...) { + va_list va; + char buf[512]; + buf[0] = 0; + va_start(va, s); + do { + strlcat(buf, s, sizeof(buf)); + } while ((s = va_arg(va, const char *))); + va_end(va); + return write(fd, buf, strlen(buf)); +} + +static bool IsModeCharacter(char c) { + switch (c) { + case ' ': + case '*': + return true; + default: + return false; + } +} + +static bool IsSupportedPath(const char *path) { + size_t i; + for (i = 0;; ++i) { + switch (path[i]) { + case 0: + if (i) return true; + // fallthrough + case '\r': + case '\n': + case '\\': + Write(2, PROG, ": ", path, ": unsupported path\n", 0); + return false; + default: + break; + } + } +} + +static bool GetDigest(const char *path, FILE *f, unsigned char digest[32]) { + size_t got; + unsigned char buf[512]; + mbedtls_sha256_context ctx; + mbedtls_sha256_init(&ctx); + _unassert(!mbedtls_sha256_starts_ret(&ctx, false)); + while ((got = fread(buf, 1, sizeof(buf), f))) { + _unassert(!mbedtls_sha256_update_ret(&ctx, buf, got)); + } + if (ferror(f)) { + Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", 0); + return false; + } + _unassert(!mbedtls_sha256_finish_ret(&ctx, digest)); + mbedtls_sha256_free(&ctx); + return true; +} + +static bool ProduceDigest(const char *path, FILE *f) { + char hexdigest[65]; + char mode[2] = {g_mode}; + unsigned char digest[32]; + if (!IsSupportedPath(path)) return false; + if (!GetDigest(path, f, digest)) return false; + hexpcpy(hexdigest, digest, 32); + Write(1, hexdigest, " ", mode, path, "\n", 0); + return true; +} + +static bool CheckDigests(const char *path, FILE *f) { + FILE *f2; + bool k = true; + int a, b, i, line; + const char *path2, *status; + unsigned char wantdigest[32], gotdigest[32]; + char buf[64 + 2 + PATH_MAX + 1 + 1], *p; + for (line = 0; fgets(buf, sizeof(buf), f); ++line) { + if (!*_chomp(buf)) continue; + for (p = buf, i = 0; i < 32; ++i) { + if ((a = kHexToInt[*p++ & 255]) == -1) goto InvalidLine; + if ((b = kHexToInt[*p++ & 255]) == -1) goto InvalidLine; + wantdigest[i] = a << 4 | b; + } + if (*p++ != ' ') goto InvalidLine; + if (!IsModeCharacter(*p++)) goto InvalidLine; + path2 = p; + if (!*path2) goto InvalidLine; + if (!IsSupportedPath(path2)) continue; + if ((f2 = fopen(path2, "rb"))) { + if (GetDigest(path2, f2, gotdigest)) { + if (!memcmp(wantdigest, gotdigest, 32)) { + status = "OK"; + } else { + status = "FAILED"; + ++g_mismatches; + k = false; + } + Write(1, path2, ": ", status, "\n", 0); + } else { + k = false; + } + fclose(f2); + } else { + Write(2, PROG, ": ", path2, ": ", _strerdoc(errno), "\n", 0); + k = false; + } + continue; + InvalidLine: + if (g_warn) { + char linestr[12]; + FormatInt32(linestr, line + 1); + Write(2, PROG, ": ", path, ":", linestr, ": ", + "improperly formatted checksum line", "\n", 0); + } + } + if (ferror(f)) { + Write(2, PROG, ": ", path, ": ", _strerdoc(errno), "\n", 0); + k = false; + } + return k; +} + +static bool Process(const char *path, FILE *f) { + if (g_check) { + return CheckDigests(path, f); + } else { + return ProduceDigest(path, f); + } +} + +int main(int argc, char *argv[]) { + int i; + FILE *f; + bool k = true; + GetOpts(argc, argv); + if (optind == argc) { + f = stdin; + k &= Process("-", f); + } else { + for (i = optind; i < argc; ++i) { + if ((f = fopen(argv[i], "rb"))) { + k &= Process(argv[i], f); + fclose(f); + } else { + Write(2, PROG, ": ", argv[i], ": ", _strerdoc(errno), "\n", 0); + k = false; + } + } + } + if (g_mismatches) { + char ibuf[12]; + FormatInt32(ibuf, g_mismatches); + Write(2, PROG, ": WARNING: ", ibuf, " computed checksum did NOT match\n", + 0); + } + return !k; +} diff --git a/tool/emacs/cosmo-cpp-constants.el b/tool/emacs/cosmo-cpp-constants.el index a9ca7fb8c..61dad14d7 100644 --- a/tool/emacs/cosmo-cpp-constants.el +++ b/tool/emacs/cosmo-cpp-constants.el @@ -163,8 +163,15 @@ "__MRECORD_MCOUNT__" "__FNO_OMIT_FRAME_POINTER__")) +(defconst cosmo-cpp-constants-clang + '("__llvm__" + "__clang__" + "_MSC_VER" + "__INTEL_COMPILER")) + (defconst cosmo-cpp-constants (append cosmo-cpp-constants-c11 + cosmo-cpp-constants-clang cosmo-cpp-constants-gcc-92 cosmo-cpp-constants-gcc-412 cosmo-cpp-constants-chibicc diff --git a/tool/emacs/cosmo-platform-constants.el b/tool/emacs/cosmo-platform-constants.el index 7c7121880..e1a98d9ea 100644 --- a/tool/emacs/cosmo-platform-constants.el +++ b/tool/emacs/cosmo-platform-constants.el @@ -71,9 +71,6 @@ "__NO_MATH_ERRNO__" "__gnu__")) - (clang - '("__llvm__")) - (cosmo '("__COSMOPOLITAN__" "__ANDROID__" @@ -81,7 +78,7 @@ ) (concat "\\_<" - (regexp-opt (append clang cpp92 gcc412 cosmo)) + (regexp-opt (append cpp92 gcc412 cosmo)) "\\_>"))) (provide 'cosmo-platform-constants)