mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
91 lines
4.4 KiB
C
91 lines
4.4 KiB
C
|
/*-*- 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 2021 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/str/blake2.h"
|
||
|
#include "libc/str/str.h"
|
||
|
#include "libc/testlib/ezbench.h"
|
||
|
#include "libc/testlib/hyperion.h"
|
||
|
#include "libc/testlib/testlib.h"
|
||
|
|
||
|
const signed char kHexToInt[256] = {
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x10
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x20
|
||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 0x30
|
||
|
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x40
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x50
|
||
|
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x60
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x70
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x80
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x90
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xa0
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xb0
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xc0
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xd0
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xe0
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xf0
|
||
|
};
|
||
|
|
||
|
uint8_t *EZBLAKE2B256(const char *s, size_t n) {
|
||
|
static uint8_t digest[BLAKE2B256_DIGEST_LENGTH];
|
||
|
BLAKE2B256(s, n, digest);
|
||
|
return digest;
|
||
|
}
|
||
|
|
||
|
uint8_t *HEXBLAKE2B256(const char *s) {
|
||
|
char *p;
|
||
|
int a, b;
|
||
|
size_t i, n;
|
||
|
static uint8_t digest[BLAKE2B256_DIGEST_LENGTH];
|
||
|
n = strlen(s);
|
||
|
assert(!(n & 1));
|
||
|
n /= 2;
|
||
|
p = malloc(n);
|
||
|
for (i = 0; i < n; ++i) {
|
||
|
a = kHexToInt[s[i * 2 + 0] & 255];
|
||
|
b = kHexToInt[s[i * 2 + 1] & 255];
|
||
|
assert(a != -1);
|
||
|
assert(b != -1);
|
||
|
p[i] = a << 4 | b;
|
||
|
}
|
||
|
BLAKE2B256(p, n, digest);
|
||
|
free(p);
|
||
|
return digest;
|
||
|
}
|
||
|
|
||
|
TEST(BLAKE2B256Test, ABC) {
|
||
|
EXPECT_BINEQ(
|
||
|
"bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319",
|
||
|
EZBLAKE2B256("abc", 3));
|
||
|
EXPECT_BINEQ(
|
||
|
"0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8",
|
||
|
HEXBLAKE2B256(""));
|
||
|
EXPECT_BINEQ(
|
||
|
"03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314",
|
||
|
HEXBLAKE2B256("00"));
|
||
|
/* TODO(jart): do rest */
|
||
|
}
|
||
|
|
||
|
BENCH(blake2, bench) {
|
||
|
EZBENCH2("blake2b256 0", donothing, EZBLAKE2B256(0, 0));
|
||
|
EZBENCH2("blake2b256 8", donothing, EZBLAKE2B256("helloooo", 8));
|
||
|
EZBENCH2("blake2b256 22851", donothing,
|
||
|
EZBLAKE2B256(kHyperion, kHyperionSize));
|
||
|
}
|