From fc0b68f105a191c7cd4dbb85251ada870119d470 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 23 Jun 2019 21:36:16 -0700 Subject: * Makefile (OBJS): New objects chksum.o and chksums/sha256.o. * chksum.c, chksum.h, chksums/sha256.c, chksums/sha256.h: New files. * lib.c (init): Call chksum_init. * txr.1: Documented. * LICENSE: Add SHA-256 copyright notice. --- chksum.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 chksum.c (limited to 'chksum.c') diff --git a/chksum.c b/chksum.c new file mode 100644 index 00000000..78af6b8d --- /dev/null +++ b/chksum.c @@ -0,0 +1,142 @@ +/* Copyright 2019 + * Kaz Kylheku + * Vancouver, Canada + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "lib.h" +#include "gc.h" +#include "itypes.h" +#include "signal.h" +#include "unwind.h" +#include "eval.h" +#include "stream.h" +#include "arith.h" +#include "utf8.h" +#include "buf.h" +#include "chksums/sha256.h" +#include "chksum.h" + +val sha256_stream(val stream, val nbytes) +{ + SHA256_t s256; + unsigned char *hash = chk_malloc(SHA256_DIGEST_LENGTH); + val bfsz = num_fast(BUFSIZ); + val buf = make_buf(bfsz, nil, nil); + SHA256_init(&s256); + + if (null_or_missing_p(nbytes)) { + for (;;) { + val read = fill_buf(buf, zero, stream); + cnum rd = c_num(read); + + if (!rd) + break; + + SHA256_update(&s256, buf->b.data, rd); + } + } else { + while (ge(nbytes, bfsz)) { + val read = fill_buf(buf, zero, stream); + cnum rd = c_num(read); + + if (zerop(read)) + break; + + SHA256_update(&s256, buf->b.data, rd); + nbytes = minus(nbytes, read); + } + + buf_set_length(buf, nbytes, nil); + + { + val read = fill_buf(buf, zero, stream); + cnum rd = c_num(read); + if (rd) + SHA256_update(&s256, buf->b.data, rd); + } + } + + SHA256_final(&s256, hash); + return make_borrowed_buf(num_fast(SHA256_DIGEST_LENGTH), hash); +} + +static val sha256_buf(val buf, val self) +{ + SHA256_t s256; + unsigned char *hash = chk_malloc(SHA256_DIGEST_LENGTH); + SHA256_init(&s256); + ucnum len = c_unum(buf->b.len); + mem_t *data = buf->b.data; + const size_t szmax = convert(size_t, -1) / 4 + 1; + + while (len >= szmax) { + SHA256_update(&s256, data, szmax); + data += szmax; + len -= szmax; + } + + if (len > 0) + SHA256_update(&s256, data, len); + + SHA256_final(&s256, hash); + return make_borrowed_buf(num_fast(SHA256_DIGEST_LENGTH), hash); +} + +static val sha256_str(val str, val self) +{ + val s = make_byte_input_stream(str); + return sha256_stream(s, nil); +} + +val sha256(val obj) +{ + val self = lit("sha256"); + + switch (type(obj)) { + case STR: + case LSTR: + case LIT: + return sha256_str(obj, self); + case BUF: + return sha256_buf(obj, self); + default: + uw_throwf(error_s, lit("~a: cannot hash ~s, only buffer and strings"), + self, obj, nao); + } +} + +void chksum_init(void) +{ + reg_fun(intern(lit("sha256-stream"), user_package), func_n2o(sha256_stream, 1)); + reg_fun(intern(lit("sha256"), user_package), func_n1(sha256)); +} -- cgit v1.2.3