/*- * Copyright (c) 2015 Taylor R. Campbell * 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 AUTHOR 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 AUTHOR 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. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include "freadline.h" #include "hexdigit.h" #include "unirandom.h" #include "blake2b.h" static const uint8_t key[BLAKE2B_MAX_KEY] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f, 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27, 0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f, 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, 0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f, }; int main(int argc, char **argv) { char hex[256*2 + 2]; size_t len, n; unsigned char buf[256]; struct blake2b ctx; uint8_t h[BLAKE2B_MAX_DIGEST]; size_t lineno; unsigned i, c0, c1; (void)argc; /* ignore */ (void)argv; /* ignore */ if (blake2b_selftest() == -1) errx(1, "blake2b selftest failed"); for (lineno = 1; freadline(hex, sizeof(hex), &len, stdin) != EOF; lineno++) { if (hex[len - 1] != '\n') errx(1, "line %zu too long", lineno); if (((len - 1) % 2) != 0) errx(1, "line %zu: odd number of hex digits", lineno); n = (len - 1) / 2; for (i = 0; i < n; i++) { unsigned char hi, lo; if (hexdigit(hex[2*i], &hi) == -1) errx(1, "line %zu char %u: invalid hex", lineno, 2*i); if (hexdigit(hex[2*i + 1], &lo) == -1) errx(1, "line %zu char %u: invalid hex", lineno, 2*i + 1); buf[i] = (hi << 4) | lo; } blake2b_init(&ctx, BLAKE2B_MAX_DIGEST, key, sizeof(key)); if (n == 0) { if (unirandom(2)) blake2b_update(&ctx, buf, 0); } else { c0 = unirandom(n); c1 = unirandom(n - c0); blake2b_update(&ctx, buf, c0); blake2b_update(&ctx, buf + c0, c1); blake2b_update(&ctx, buf + c0 + c1, n - (c0 + c1)); } blake2b_final(&ctx, h); for (i = 0; i < sizeof(h); i++) { if (printf("%02hhx", h[i]) < 0) err(1, "printf"); } if (printf("\n") < 0) err(1, "printf"); } return 0; }