diff options
Diffstat (limited to 'contrib/syslinux-4.02/com32/libutil')
19 files changed, 3153 insertions, 0 deletions
diff --git a/contrib/syslinux-4.02/com32/libutil/Makefile b/contrib/syslinux-4.02/com32/libutil/Makefile new file mode 100644 index 0000000..02789ca --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/Makefile @@ -0,0 +1,65 @@ +## ----------------------------------------------------------------------- +## +## Copyright 2001-2008 H. Peter Anvin - All Rights Reserved +## +## Permission is hereby granted, free of charge, to any person +## obtaining a copy of this software and associated documentation +## files (the "Software"), to deal in the Software without +## restriction, including without limitation the rights to use, +## copy, modify, merge, publish, distribute, sublicense, and/or +## sell copies of the Software, and to permit persons to whom +## the Software is furnished to do so, subject to the following +## conditions: +## +## The above copyright notice and this permission notice shall +## be included in all copies or substantial portions of the Software. +## +## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +## EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +## OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +## NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +## HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +## WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +## OTHER DEALINGS IN THE SOFTWARE. +## +## ----------------------------------------------------------------------- + +## +## Utility companion library for the COM32 library +## + +topdir = ../.. +include ../MCONFIG + +LIBOBJS = ansiline.o ansiraw.o get_key.o sha1hash.o unbase64.o \ + md5.o crypt-md5.o sha256crypt.o sha512crypt.o base64.o +LNXLIBOBJS = $(patsubst %.o,%.lo,$(LIBOBJS)) + +all: libutil_com.a libutil_lnx.a + +libutil_com.a: $(LIBOBJS) + rm -f $@ + $(AR) cq $@ $(LIBOBJS) + $(RANLIB) $@ + +libutil_lnx.a: $(LNXLIBOBJS) + rm -f $@ + $(AR) cq $@ $(LNXLIBOBJS) + $(RANLIB) $@ + +tidy dist: + rm -f *.o *.lo *.lst *.elf .*.d *.tmp + +clean: tidy + rm -f *.lnx libutil_lnx.a + +spotless: clean + rm -f *.lss *.a *.c32 *.com + rm -f *~ \#* + +install: all + mkdir -m 755 -p $(INSTALLROOT)$(COM32DIR) + install -m 644 libutil_com.a libutil_lnx.a $(INSTALLROOT)$(COM32DIR) + +-include .*.d diff --git a/contrib/syslinux-4.02/com32/libutil/ansiline.c b/contrib/syslinux-4.02/com32/libutil/ansiline.c new file mode 100644 index 0000000..18c7149 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/ansiline.c @@ -0,0 +1,83 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * ansiline.c + * + * Configures the console for ANSI output in line mode; versions + * for COM32 and Linux support. + */ + +#ifdef __COM32__ + +#include <stdio.h> +#include <unistd.h> +#include <console.h> + +void console_ansi_std(void) +{ + openconsole(&dev_stdcon_r, &dev_ansiserial_w); +} + +#else + +#include <stdio.h> +#include <termios.h> + +static struct termios original_termios_settings; + +static void __attribute__ ((constructor)) console_init(void) +{ + tcgetattr(0, &original_termios_settings); +} + +static void __attribute__ ((destructor)) console_cleanup(void) +{ + tcsetattr(0, TCSANOW, &original_termios_settings); +} + +void console_ansi_std(void) +{ + struct termios tio; + + /* Disable stdio buffering */ + setbuf(stdin, NULL); + setbuf(stdout, NULL); + setbuf(stderr, NULL); + + /* Set the termios flag so we behave the same as libcom32 */ + tcgetattr(0, &tio); + tio.c_iflag &= ~ICRNL; + tio.c_iflag |= IGNCR; + tio.c_lflag |= ICANON | ECHO; + if (!tio.c_oflag & OPOST) + tio.c_oflag = 0; + tio.c_oflag |= OPOST | ONLCR; + tcsetattr(0, TCSANOW, &tio); +} + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/ansiraw.c b/contrib/syslinux-4.02/com32/libutil/ansiraw.c new file mode 100644 index 0000000..2afd48a --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/ansiraw.c @@ -0,0 +1,85 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * ansiraw.c + * + * Configures the console for ANSI output in raw mode; versions + * for COM32 and Linux support. + */ + +#ifdef __COM32__ + +#include <stdio.h> +#include <unistd.h> +#include <console.h> + +void console_ansi_raw(void) +{ + openconsole(&dev_rawcon_r, &dev_ansiserial_w); +} + +#else + +#include <stdio.h> +#include <termios.h> + +static struct termios original_termios_settings; + +static void __attribute__ ((constructor)) console_init(void) +{ + tcgetattr(0, &original_termios_settings); +} + +static void __attribute__ ((destructor)) console_cleanup(void) +{ + tcsetattr(0, TCSANOW, &original_termios_settings); +} + +void console_ansi_raw(void) +{ + struct termios tio; + + /* Disable stdio buffering */ + setbuf(stdin, NULL); + setbuf(stdout, NULL); + setbuf(stderr, NULL); + + /* Set the termios flag so we behave the same as libcom32 */ + tcgetattr(0, &tio); + tio.c_iflag &= ~ICRNL; + tio.c_iflag |= IGNCR; + tio.c_lflag &= ~(ISIG | ICANON | ECHO); + if (!tio.c_oflag & OPOST) + tio.c_oflag = 0; + tio.c_oflag |= OPOST | ONLCR; + tio.c_cc[VMIN] = 0; + tio.c_cc[VTIME] = 1; /* Don't 100% busy-wait in Linux */ + tcsetattr(0, TCSAFLUSH, &tio); +} + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/base64.c b/contrib/syslinux-4.02/com32/libutil/base64.c new file mode 100644 index 0000000..74b101c --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/base64.c @@ -0,0 +1,95 @@ +/* + * Output a base64 string. + * + * Options include: + * - Character 62 and 63; + * - To pad or not to pad. + */ + +#include <inttypes.h> +#include <base64.h> + +size_t genbase64(char *output, const void *input, size_t size, int flags) +{ + static char charz[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_"; + uint8_t buf[3]; + int j; + const uint8_t *p; + char *q; + uint32_t bv; + int left = size; + + charz[62] = (char)flags; + charz[63] = (char)(flags >> 8); + + p = input; + q = output; + + while (left > 0) { + if (left < 3) { + buf[0] = p[0]; + buf[1] = (left > 1) ? p[1] : 0; + buf[2] = 0; + p = buf; + } + + bv = (p[0] << 16) | (p[1] << 8) | p[2]; + p += 3; + left -= 3; + + for (j = 0; j < 4; j++) { + *q++ = charz[(bv >> 18) & 0x3f]; + bv <<= 6; + } + } + + switch (left) { + case -1: + if (flags & BASE64_PAD) + q[-1] = '='; + else + q--; + break; + + case -2: + if (flags & BASE64_PAD) + q[-2] = q[-1] = '='; + else + q -= 2; + break; + + default: + break; + } + + *q = '\0'; + + return q - output; +} + +#ifdef TEST + +#include <stdio.h> +#include <string.h> + +int main(int argc, char *argv[]) +{ + int i; + char buf[4096]; + int len, bytes; + + for (i = 1; i < argc; i++) { + printf("Original: \"%s\"\n", argv[i]); + + len = strlen(argv[i]); + bytes = genbase64(buf, argv[i], len, BASE64_MIME | BASE64_PAD); + printf(" MIME: \"%s\" (%d)\n", buf, bytes); + bytes = genbase64(buf, argv[i], len, BASE64_SAFE); + printf(" Safe: \"%s\" (%d)\n", buf, bytes); + } + + return 0; +} + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/crypt-md5.c b/contrib/syslinux-4.02/com32/libutil/crypt-md5.c new file mode 100644 index 0000000..49e9532 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/crypt-md5.c @@ -0,0 +1,173 @@ +/*- + * Copyright (c) 2003 Poul-Henning Kamp + * 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. + */ + +#include <inttypes.h> +#include <md5.h> +#include <string.h> + +/* + * UNIX password + */ + +static char *_crypt_to64(char *s, uint32_t v, int n) +{ + static const char itoa64[64] = "./0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + while (--n >= 0) { + *s++ = itoa64[v & 0x3f]; + v >>= 6; + } + return s; +} + +char *crypt_md5(const char *pw, const char *salt) +{ + MD5_CTX ctx, ctx1; + unsigned long l; + int sl, pl; + uint32_t i; + uint8_t final[MD5_SIZE]; + const char *sp; + static char passwd[120]; /* Output buffer */ + static const char magic[] = "$1$"; + char *p; + const int magic_len = sizeof magic - 1; + int pwlen = strlen(pw); + + /* Refine the Salt first */ + sp = salt; + + /* If it starts with the magic string, then skip that */ + if (!strncmp(sp, magic, magic_len)) + sp += magic_len; + + /* Compute the salt length: + it stops at the first '$', max 8 chars */ + for (sl = 0; sl < 8 && sp[sl] && sp[sl] != '$'; sl++) ; + + MD5Init(&ctx); + + /* The password first, since that is what is most unknown */ + MD5Update(&ctx, pw, pwlen); + + /* Then our magic string */ + MD5Update(&ctx, magic, magic_len); + + /* Then the raw salt */ + MD5Update(&ctx, sp, sl); + + /* Then just as many characters of the MD5(pw,salt,pw) */ + MD5Init(&ctx1); + MD5Update(&ctx1, pw, pwlen); + MD5Update(&ctx1, sp, sl); + MD5Update(&ctx1, pw, pwlen); + MD5Final(final, &ctx1); + for (pl = pwlen; pl > 0; pl -= MD5_SIZE) + MD5Update(&ctx, final, pl > MD5_SIZE ? MD5_SIZE : pl); + + /* Don't leave anything around in vm they could use. */ + memset(final, 0, sizeof final); + + /* Then something really weird... */ + for (i = pwlen; i; i >>= 1) + if (i & 1) + MD5Update(&ctx, final, 1); + else + MD5Update(&ctx, pw, 1); + + /* Now make the output string */ + p = passwd; + + memcpy(p, magic, magic_len); + p += magic_len; + + memcpy(p, sp, sl); + p += sl; + + *p++ = '$'; + + MD5Final(final, &ctx); + + /* + * and now, just to make sure things don't run too fast + * On a 60 Mhz Pentium this takes 34 msec, so you would + * need 30 seconds to build a 1000 entry dictionary... + */ + for (i = 0; i < 1000; i++) { + MD5Init(&ctx1); + if (i & 1) + MD5Update(&ctx1, pw, pwlen); + else + MD5Update(&ctx1, final, MD5_SIZE); + + if (i % 3) + MD5Update(&ctx1, sp, sl); + + if (i % 7) + MD5Update(&ctx1, pw, pwlen); + + if (i & 1) + MD5Update(&ctx1, final, MD5_SIZE); + else + MD5Update(&ctx1, pw, pwlen); + MD5Final(final, &ctx1); + } + + l = (final[0] << 16) | (final[6] << 8) | final[12]; + p = _crypt_to64(p, l, 4); + l = (final[1] << 16) | (final[7] << 8) | final[13]; + p = _crypt_to64(p, l, 4); + l = (final[2] << 16) | (final[8] << 8) | final[14]; + p = _crypt_to64(p, l, 4); + l = (final[3] << 16) | (final[9] << 8) | final[15]; + p = _crypt_to64(p, l, 4); + l = (final[4] << 16) | (final[10] << 8) | final[5]; + p = _crypt_to64(p, l, 4); + l = final[11]; + p = _crypt_to64(p, l, 2); + *p = '\0'; + + /* Don't leave anything around in vm they could use. */ + memset(final, 0, sizeof final); + + return passwd; +} + +#ifdef TEST +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + int i; + + for (i = 2; i < argc; i += 2) { + puts(crypt_md5(argv[i], argv[i - 1])); + } + return 0; +} + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/get_key.c b/contrib/syslinux-4.02/com32/libutil/get_key.c new file mode 100644 index 0000000..f277b43 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/get_key.c @@ -0,0 +1,172 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * get_key.c + * + * Get a single key, and try to pick apart function key codes. + * This doesn't decode anywhere close to all possiblities, but + * hopefully is enough to be useful. + */ + +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <unistd.h> +#include <time.h> +#include <sys/times.h> +#include <getkey.h> +#include <libutil.h> + +struct keycode { + int code; + int seqlen; + const unsigned char *seq; +}; + +#define MAXLEN 8 +#define CODE(x,y) { x, (sizeof y)-1, (const unsigned char *)(y) } + +static const struct keycode keycodes[] = { + /* First, the BIOS combined codes */ + CODE(KEY_F1, "\0\x3B"), + CODE(KEY_F2, "\0\x3C"), + CODE(KEY_F3, "\0\x3D"), + CODE(KEY_F4, "\0\x3E"), + CODE(KEY_F5, "\0\x3F"), + CODE(KEY_F6, "\0\x40"), + CODE(KEY_F7, "\0\x41"), + CODE(KEY_F8, "\0\x42"), + CODE(KEY_F9, "\0\x43"), + CODE(KEY_F10, "\0\x44"), + CODE(KEY_F11, "\0\x85"), + CODE(KEY_F12, "\0\x86"), + + CODE(KEY_UP, "\0\x48"), + CODE(KEY_DOWN, "\0\x50"), + CODE(KEY_LEFT, "\0\x4B"), + CODE(KEY_RIGHT, "\0\x4D"), + CODE(KEY_PGUP, "\0\x49"), + CODE(KEY_PGDN, "\0\x51"), + CODE(KEY_HOME, "\0\x47"), + CODE(KEY_END, "\0\x4F"), + CODE(KEY_INSERT, "\0\x52"), + CODE(KEY_DELETE, "\0\x53"), + + /* Now, VT/xterm/Linux codes */ + CODE(KEY_F1, "\033[[A"), + CODE(KEY_F1, "\033OP"), + CODE(KEY_F2, "\033[[B"), + CODE(KEY_F2, "\033OQ"), + CODE(KEY_F3, "\033[[C"), + CODE(KEY_F3, "\033OR"), + CODE(KEY_F4, "\033[[D"), + CODE(KEY_F4, "\033OS"), + CODE(KEY_F5, "\033[[E"), + CODE(KEY_F5, "\033[15~"), + CODE(KEY_F6, "\033[17~"), + CODE(KEY_F7, "\033[18~"), + CODE(KEY_F8, "\033[19~"), + CODE(KEY_F9, "\033[20~"), + CODE(KEY_F10, "\033[21~"), + CODE(KEY_F11, "\033[23~"), + CODE(KEY_F12, "\033[24~"), + + CODE(KEY_UP, "\033[A"), + CODE(KEY_DOWN, "\033[B"), + CODE(KEY_LEFT, "\033[D"), + CODE(KEY_RIGHT, "\033[C"), + CODE(KEY_PGUP, "\033[5~"), + CODE(KEY_PGUP, "\033[V"), + CODE(KEY_PGDN, "\033[6~"), + CODE(KEY_PGDN, "\033[U"), + CODE(KEY_HOME, "\033[1~"), + CODE(KEY_HOME, "\033[H"), + CODE(KEY_END, "\033[4~"), + CODE(KEY_END, "\033[F"), + CODE(KEY_END, "\033OF"), + CODE(KEY_INSERT, "\033[2~"), + CODE(KEY_INSERT, "\033[@"), + CODE(KEY_DELETE, "\033[3~"), +}; + +#define NCODES ((int)(sizeof keycodes/sizeof(struct keycode))) + +#define KEY_TIMEOUT ((CLK_TCK+9)/10) + +int get_key(FILE * f, clock_t timeout) +{ + unsigned char buffer[MAXLEN]; + int nc, i, rv; + const struct keycode *kc; + int another; + unsigned char ch; + clock_t start; + + /* We typically start in the middle of a clock tick */ + if (timeout) + timeout++; + + nc = 0; + start = times(NULL); + do { + rv = read(fileno(f), &ch, 1); + if (rv == 0 || (rv == -1 && errno == EAGAIN)) { + clock_t lateness = times(NULL) - start; + if (nc && lateness > 1 + KEY_TIMEOUT) { + if (nc == 1) + return buffer[0]; /* timeout in sequence */ + else if (timeout && lateness > timeout) + return KEY_NONE; + } else if (!nc && timeout && lateness > timeout) + return KEY_NONE; /* timeout before sequence */ + + do_idle(); + + another = 1; + continue; + } + + start = times(NULL); + + buffer[nc++] = ch; + + another = 0; + for (i = 0, kc = keycodes; i < NCODES; i++, kc++) { + if (nc == kc->seqlen && !memcmp(buffer, kc->seq, nc)) + return kc->code; + else if (nc < kc->seqlen && !memcmp(buffer, kc->seq, nc)) { + another = 1; + break; + } + } + } while (another); + + /* We got an unrecognized sequence; return the first character */ + /* We really should remember this and return subsequent characters later */ + return buffer[0]; +} diff --git a/contrib/syslinux-4.02/com32/libutil/include/base64.h b/contrib/syslinux-4.02/com32/libutil/include/base64.h new file mode 100644 index 0000000..b71855b --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/base64.h @@ -0,0 +1,51 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2005-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * base64.h + * + * Simple routines for handing base64 text + */ + +#ifndef LIBUTIL_BASE64_H +#define LIBUTIL_BASE64_H + +#include <stddef.h> + +#define BASE64_PAD 0x10000 + +/* There is plenty of disagreement w.r.t. the last few characters... */ +#define BASE64_MIME ('+' + ('/' << 8)) +#define BASE64_SAFE ('-' + ('_' << 8)) +#define BASE64_CRYPT ('.' + ('/' << 8)) +#define BASE64_URL ('*' + ('-' << 8)) /* Haven't seen myself */ +#define BASE64_REGEX ('|' + ('-' << 8)) /* Ditto... */ + +size_t genbase64(char *output, const void *digest, size_t size, int flags); +size_t unbase64(unsigned char *, size_t, const char *); + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/include/consoles.h b/contrib/syslinux-4.02/com32/libutil/include/consoles.h new file mode 100644 index 0000000..07652ed --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/consoles.h @@ -0,0 +1,40 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * consoles.h + * + * Utility functions for common console configurations + */ + +#ifndef LIBUTIL_CONSOLES_H +#define LIBUTIL_CONSOLES_H + +void console_ansi_std(void); +void console_ansi_raw(void); + +#endif /* LIBUTIL_CONSOLES_H */ diff --git a/contrib/syslinux-4.02/com32/libutil/include/getkey.h b/contrib/syslinux-4.02/com32/libutil/include/getkey.h new file mode 100644 index 0000000..52312a2 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/getkey.h @@ -0,0 +1,80 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * getkey.h + * + * Function to get a key symbol and parse it + */ + +#ifndef LIBUTIL_GETKEY_H +#define LIBUTIL_GETKEY_H + +#include <stdio.h> +#include <unistd.h> +#include <sys/times.h> + +#ifndef CLK_TCK +# define CLK_TCK sysconf(_SC_CLK_TCK) +#endif + +#define KEY_NONE (-1) + +#define KEY_CTRL(x) ((x) & 0x001f) +#define KEY_BACKSPACE 0x0008 +#define KEY_TAB 0x0009 +#define KEY_ENTER 0x000d +#define KEY_ESC 0x001b +#define KEY_DEL 0x007f + +#define KEY_F1 0x0100 +#define KEY_F2 0x0101 +#define KEY_F3 0x0102 +#define KEY_F4 0x0103 +#define KEY_F5 0x0104 +#define KEY_F6 0x0105 +#define KEY_F7 0x0106 +#define KEY_F8 0x0107 +#define KEY_F9 0x0108 +#define KEY_F10 0x0109 +#define KEY_F11 0x010A +#define KEY_F12 0x010B + +#define KEY_UP 0x0120 +#define KEY_DOWN 0x0121 +#define KEY_LEFT 0x0122 +#define KEY_RIGHT 0x0123 +#define KEY_PGUP 0x0124 +#define KEY_PGDN 0x0125 +#define KEY_HOME 0x0126 +#define KEY_END 0x0127 +#define KEY_INSERT 0x0128 +#define KEY_DELETE 0x0129 + +int get_key(FILE *, clock_t); + +#endif /* LIBUTIL_GETKEY_H */ diff --git a/contrib/syslinux-4.02/com32/libutil/include/libutil.h b/contrib/syslinux-4.02/com32/libutil/include/libutil.h new file mode 100644 index 0000000..af46a75 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/libutil.h @@ -0,0 +1,51 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2005-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * libutil.h + * + * Misc libutil functions + */ + +#ifndef LIBUTIL_LIBUTIL_H +#define LIBUTIL_LIBUTIL_H + +#ifdef __COM32__ + +# include <syslinux/idle.h> + +# define do_idle syslinux_idle + +#else /* not __COM32__ */ + +# include <sched.h> + +# define do_idle sched_yield + +#endif + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/include/md5.h b/contrib/syslinux-4.02/com32/libutil/include/md5.h new file mode 100644 index 0000000..db38c66 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/md5.h @@ -0,0 +1,46 @@ +/* MD5.H - header file for MD5C.C + */ + +/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All +rights reserved. + +License to copy and use this software is granted provided that it +is identified as the "RSA Data Security, Inc. MD5 Message-Digest +Algorithm" in all material mentioning or referencing this software +or this function. + +License is also granted to make and use derivative works provided +that such works are identified as "derived from the RSA Data +Security, Inc. MD5 Message-Digest Algorithm" in all material +mentioning or referencing the derived work. + +RSA Data Security, Inc. makes no representations concerning either +the merchantability of this software or the suitability of this +software for any particular purpose. It is provided "as is" +without express or implied warranty of any kind. +These notices must be retained in any copies of any part of this +documentation and/or software. + */ + +#ifndef _LIBUTIL_MD5_H +#define _LIBUTIL_MD5_H + +#include <stddef.h> +#include <stdint.h> + +#define MD5_SIZE 16 /* 128 bits */ + +/* MD5 context. */ +typedef struct { + uint32_t state[4]; /* state (ABCD) */ + uint32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ +} MD5_CTX; + +void MD5Init(MD5_CTX *); +void MD5Update(MD5_CTX *, const void *, size_t); +void MD5Final(unsigned char[MD5_SIZE], MD5_CTX *); + +char *crypt_md5(const char *, const char *); + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/include/minmax.h b/contrib/syslinux-4.02/com32/libutil/include/minmax.h new file mode 100644 index 0000000..eb6e39a --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/minmax.h @@ -0,0 +1,42 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +#ifndef _MINMAX_H +#define _MINMAX_H + +/* + * minmax.h: Type-independent safe min/max macros + */ + +#define min(x,y) ({ __typeof(x) xx = (x); \ + __typeof(y) yy = (y); \ + xx < yy ? xx : yy; }) +#define max(x,y) ({ __typeof(x) xx = (x); \ + __typeof(y) yy = (y); \ + xx > yy ? xx : yy; }) + +#endif /* _MINMAX_H */ diff --git a/contrib/syslinux-4.02/com32/libutil/include/sha1.h b/contrib/syslinux-4.02/com32/libutil/include/sha1.h new file mode 100644 index 0000000..3b35e77 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/sha1.h @@ -0,0 +1,18 @@ +#ifndef LIBUTIL_SHA1_H +#define LIBUTIL_SHA1_H + +#include <stdint.h> + +typedef struct { + uint32_t state[5]; + uint32_t count[2]; + unsigned char buffer[64]; +} SHA1_CTX; + +void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]); +void SHA1Init(SHA1_CTX * context); +void SHA1Update(SHA1_CTX * context, const unsigned char *data, uint32_t len); /* + JHB */ +void SHA1Final(unsigned char digest[20], SHA1_CTX * context); + +#endif /* LIBUTIL_SHA1_H */ diff --git a/contrib/syslinux-4.02/com32/libutil/include/xcrypt.h b/contrib/syslinux-4.02/com32/libutil/include/xcrypt.h new file mode 100644 index 0000000..c9ced46 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/include/xcrypt.h @@ -0,0 +1,10 @@ +#ifndef _LIBUTIL_XCRYPT_H +#define _LIBUTIL_XCRYPT_H + +/* Extended crypt() implementations */ + +char *crypt_md5(const char *, const char *); +char *sha256_crypt(const char *, const char *); +char *sha512_crypt(const char *, const char *); + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/md5.c b/contrib/syslinux-4.02/com32/libutil/md5.c new file mode 100644 index 0000000..bb7e51a --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/md5.c @@ -0,0 +1,273 @@ +/* + * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm + * + * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All + * rights reserved. + * + * License to copy and use this software is granted provided that it + * is identified as the "RSA Data Security, Inc. MD5 Message-Digest + * Algorithm" in all material mentioning or referencing this software + * or this function. + * + * License is also granted to make and use derivative works provided + * that such works are identified as "derived from the RSA Data + * Security, Inc. MD5 Message-Digest Algorithm" in all material + * mentioning or referencing the derived work. + * + * RSA Data Security, Inc. makes no representations concerning either + * the merchantability of this software or the suitability of this + * software for any particular purpose. It is provided "as is" + * without express or implied warranty of any kind. + * + * These notices must be retained in any copies of any part of this + * documentation and/or software. + * + * This code is the same as the code published by RSA Inc. It has been + * edited for clarity and style only. + */ + +#include <string.h> +#include <endian.h> +#include <md5.h> + +static void MD5Transform(uint32_t[4], const unsigned char[64]); + +#define Encode memcpy +#define Decode memcpy + +static unsigned char PADDING[64] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G, H and I are basic MD5 functions. */ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* ROTATE_LEFT rotates x left n bits. */ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* + * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. + * Rotation is separate from addition to prevent recomputation. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \ + (a) = ROTATE_LEFT ((a), (s)); \ + (a) += (b); \ + } + +/* MD5 initialization. Begins an MD5 operation, writing a new context. */ + +void MD5Init(MD5_CTX * context) +{ + context->count[0] = context->count[1] = 0; + + /* Load magic initialization constants. */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* + * MD5 block update operation. Continues an MD5 message-digest + * operation, processing another message block, and updating the + * context. + */ + +void MD5Update(MD5_CTX * context, const void *in, size_t inputLen) +{ + unsigned int i, idx, partLen; + const unsigned char *input = in; + + /* Compute number of bytes mod 64 */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((uint32_t) inputLen << 3)) + < ((uint32_t) inputLen << 3)) + context->count[1]++; + context->count[1] += ((uint32_t) inputLen >> 29); + + partLen = 64 - idx; + + /* Transform as many times as possible. */ + if (inputLen >= partLen) { + memcpy((void *)&context->buffer[idx], (const void *)input, partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform(context->state, &input[i]); + + idx = 0; + } else + i = 0; + + /* Buffer remaining input */ + memcpy((void *)&context->buffer[idx], (const void *)&input[i], + inputLen - i); +} + +/* + * MD5 padding. Adds padding followed by original length. + */ + +static void MD5Pad(MD5_CTX * context) +{ + unsigned char bits[8]; + unsigned int idx, padLen; + + /* Save number of bits */ + Encode(bits, context->count, 8); + + /* Pad out to 56 mod 64. */ + idx = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (idx < 56) ? (56 - idx) : (120 - idx); + MD5Update(context, PADDING, padLen); + + /* Append length (before padding) */ + MD5Update(context, bits, 8); +} + +/* + * MD5 finalization. Ends an MD5 message-digest operation, writing the + * the message digest and zeroizing the context. + */ + +void MD5Final(unsigned char digest[16], MD5_CTX * context) +{ + /* Do padding. */ + MD5Pad(context); + + /* Store state in digest */ + Encode(digest, context->state, 16); + + /* Zeroize sensitive information. */ + memset((void *)context, 0, sizeof(*context)); +} + +/* MD5 basic transformation. Transforms state based on block. */ + +static void MD5Transform(state, block) +uint32_t state[4]; +const unsigned char block[64]; +{ + uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode(x, block, 64); + + /* Round 1 */ +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 + FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 + GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 + HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. */ + memset((void *)x, 0, sizeof(x)); +} diff --git a/contrib/syslinux-4.02/com32/libutil/sha1hash.c b/contrib/syslinux-4.02/com32/libutil/sha1hash.c new file mode 100644 index 0000000..3379693 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/sha1hash.c @@ -0,0 +1,364 @@ +/* +SHA-1 in C +By Steve Reid <sreid@sea-to-sky.net> +100% Public Domain + +----------------- +Modified 7/98 +By James H. Brown <jbrown@burgoyne.com> +Still 100% Public Domain + +Corrected a problem which generated improper hash values on 16 bit machines +Routine SHA1Update changed from + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int +len) +to + void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned +long len) + +The 'len' parameter was declared an int which works fine on 32 bit machines. +However, on 16 bit machines an int is too small for the shifts being done +against +it. This caused the hash function to generate incorrect values if len was +greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update(). + +Since the file IO in main() reads 16K at a time, any file 8K or larger would +be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million +"a"s). + +I also changed the declaration of variables i & j in SHA1Update to +unsigned long from unsigned int for the same reason. + +These changes should make no difference to any 32 bit implementations since +an +int and a long are the same size in those environments. + +-- +I also corrected a few compiler warnings generated by Borland C. +1. Added #include <process.h> for exit() prototype +2. Removed unused variable 'j' in SHA1Final +3. Changed exit(0) to return(0) at end of main. + +ALL changes I made can be located by searching for comments containing 'JHB' +----------------- +Modified 8/98 +By Steve Reid <sreid@sea-to-sky.net> +Still 100% public domain + +1- Removed #include <process.h> and used return() instead of exit() +2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall) +3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net + +----------------- +Modified 4/01 +By Saul Kravitz <Saul.Kravitz@celera.com> +Still 100% PD +Modified to run on Compaq Alpha hardware. + +----------------- +Modified 2/03 +By H. Peter Anvin <hpa@zytor.com> +Still 100% PD +Modified to run on any hardware with <inttypes.h> and <netinet/in.h> +Changed the driver program + +*/ + +/* +Test Vectors (from FIPS PUB 180-1) +"abc" + A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D +"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 +A million repetitions of "a" + 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F +*/ + +/* #define SHA1HANDSOFF */ + +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include <netinet/in.h> /* For htonl/ntohl/htons/ntohs */ + +#include "sha1.h" + +#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) + +/* blk0() and blk() perform the initial expand. */ +/* I got the idea of expanding during the round function from SSLeay */ +#define blk0(i) (block->l[i] = ntohl(block->l[i])) +#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ + ^block->l[(i+2)&15]^block->l[i&15],1)) + +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ +#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); +#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); +#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); +#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); + +#ifdef VERBOSE /* SAK */ +void SHAPrintContext(SHA1_CTX * context, char *msg) +{ + printf("%s (%d,%d) %x %x %x %x %x\n", + msg, + context->count[0], context->count[1], + context->state[0], + context->state[1], + context->state[2], context->state[3], context->state[4]); +} +#endif + +/* Hash a single 512-bit block. This is the core of the algorithm. */ + +void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) +{ + uint32_t a, b, c, d, e; + typedef union { + unsigned char c[64]; + uint32_t l[16]; + } CHAR64LONG16; + CHAR64LONG16 *block; +#ifdef SHA1HANDSOFF + static unsigned char workspace[64]; + block = (CHAR64LONG16 *) workspace; + memcpy(block, buffer, 64); +#else + block = (CHAR64LONG16 *) buffer; +#endif + /* Copy context->state[] to working vars */ + a = state[0]; + b = state[1]; + c = state[2]; + d = state[3]; + e = state[4]; + /* 4 rounds of 20 operations each. Loop unrolled. */ + R0(a, b, c, d, e, 0); + R0(e, a, b, c, d, 1); + R0(d, e, a, b, c, 2); + R0(c, d, e, a, b, 3); + R0(b, c, d, e, a, 4); + R0(a, b, c, d, e, 5); + R0(e, a, b, c, d, 6); + R0(d, e, a, b, c, 7); + R0(c, d, e, a, b, 8); + R0(b, c, d, e, a, 9); + R0(a, b, c, d, e, 10); + R0(e, a, b, c, d, 11); + R0(d, e, a, b, c, 12); + R0(c, d, e, a, b, 13); + R0(b, c, d, e, a, 14); + R0(a, b, c, d, e, 15); + R1(e, a, b, c, d, 16); + R1(d, e, a, b, c, 17); + R1(c, d, e, a, b, 18); + R1(b, c, d, e, a, 19); + R2(a, b, c, d, e, 20); + R2(e, a, b, c, d, 21); + R2(d, e, a, b, c, 22); + R2(c, d, e, a, b, 23); + R2(b, c, d, e, a, 24); + R2(a, b, c, d, e, 25); + R2(e, a, b, c, d, 26); + R2(d, e, a, b, c, 27); + R2(c, d, e, a, b, 28); + R2(b, c, d, e, a, 29); + R2(a, b, c, d, e, 30); + R2(e, a, b, c, d, 31); + R2(d, e, a, b, c, 32); + R2(c, d, e, a, b, 33); + R2(b, c, d, e, a, 34); + R2(a, b, c, d, e, 35); + R2(e, a, b, c, d, 36); + R2(d, e, a, b, c, 37); + R2(c, d, e, a, b, 38); + R2(b, c, d, e, a, 39); + R3(a, b, c, d, e, 40); + R3(e, a, b, c, d, 41); + R3(d, e, a, b, c, 42); + R3(c, d, e, a, b, 43); + R3(b, c, d, e, a, 44); + R3(a, b, c, d, e, 45); + R3(e, a, b, c, d, 46); + R3(d, e, a, b, c, 47); + R3(c, d, e, a, b, 48); + R3(b, c, d, e, a, 49); + R3(a, b, c, d, e, 50); + R3(e, a, b, c, d, 51); + R3(d, e, a, b, c, 52); + R3(c, d, e, a, b, 53); + R3(b, c, d, e, a, 54); + R3(a, b, c, d, e, 55); + R3(e, a, b, c, d, 56); + R3(d, e, a, b, c, 57); + R3(c, d, e, a, b, 58); + R3(b, c, d, e, a, 59); + R4(a, b, c, d, e, 60); + R4(e, a, b, c, d, 61); + R4(d, e, a, b, c, 62); + R4(c, d, e, a, b, 63); + R4(b, c, d, e, a, 64); + R4(a, b, c, d, e, 65); + R4(e, a, b, c, d, 66); + R4(d, e, a, b, c, 67); + R4(c, d, e, a, b, 68); + R4(b, c, d, e, a, 69); + R4(a, b, c, d, e, 70); + R4(e, a, b, c, d, 71); + R4(d, e, a, b, c, 72); + R4(c, d, e, a, b, 73); + R4(b, c, d, e, a, 74); + R4(a, b, c, d, e, 75); + R4(e, a, b, c, d, 76); + R4(d, e, a, b, c, 77); + R4(c, d, e, a, b, 78); + R4(b, c, d, e, a, 79); + /* Add the working vars back into context.state[] */ + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + state[4] += e; + /* Wipe variables */ + a = b = c = d = e = 0; +} + +/* SHA1Init - Initialize new context */ + +void SHA1Init(SHA1_CTX * context) +{ + /* SHA1 initialization constants */ + context->state[0] = 0x67452301; + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; + context->state[4] = 0xC3D2E1F0; + context->count[0] = context->count[1] = 0; +} + +/* Run your data through this. */ + +void SHA1Update(SHA1_CTX * context, const unsigned char *data, uint32_t len) +{ /* + JHB */ + uint32_t i, j; /* JHB */ + +#ifdef VERBOSE + SHAPrintContext(context, "before"); +#endif + j = (context->count[0] >> 3) & 63; + if ((context->count[0] += len << 3) < (len << 3)) + context->count[1]++; + context->count[1] += (len >> 29); + if ((j + len) > 63) { + memcpy(&context->buffer[j], data, (i = 64 - j)); + SHA1Transform(context->state, context->buffer); + for (; i + 63 < len; i += 64) { + SHA1Transform(context->state, &data[i]); + } + j = 0; + } else + i = 0; + memcpy(&context->buffer[j], &data[i], len - i); +#ifdef VERBOSE + SHAPrintContext(context, "after "); +#endif +} + +/* Add padding and return the message digest. */ + +void SHA1Final(unsigned char digest[20], SHA1_CTX * context) +{ + uint32_t i; /* JHB */ + unsigned char finalcount[8]; + + for (i = 0; i < 8; i++) { + finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] + >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ + } + SHA1Update(context, (unsigned char *)"\200", 1); + while ((context->count[0] & 504) != 448) { + SHA1Update(context, (unsigned char *)"\0", 1); + } + SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() + */ + for (i = 0; i < 20; i++) { + digest[i] = (unsigned char) + ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); + } + /* Wipe variables */ + i = 0; /* JHB */ + memset(context->buffer, 0, 64); + memset(context->state, 0, 20); + memset(context->count, 0, 8); + memset(finalcount, 0, 8); /* SWR */ +#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ + SHA1Transform(context->state, context->buffer); +#endif +} + +/*************************************************************/ + +/* This is not quite the MIME base64 algorithm: it uses _ instead of /, + and instead of padding the output with = characters we just make the + output shorter. */ +char *mybase64(uint8_t digest[20]) +{ + static const char charz[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_"; + uint8_t input[21]; + static char output[28]; + int i, j; + uint8_t *p; + char *q; + uint32_t bv; + + memcpy(input, digest, 20); + input[20] = 0; /* Pad to multiple of 3 bytes */ + + p = input; + q = output; + for (i = 0; i < 7; i++) { + bv = (p[0] << 16) | (p[1] << 8) | p[2]; + p += 3; + for (j = 0; j < 4; j++) { + *q++ = charz[(bv >> 18) & 0x3f]; + bv <<= 6; + } + } + *--q = '\0'; /* The last character is not significant */ + return output; +} + +#ifdef FOR_TESTING_ONLY + +int main(int argc, char **argv) +{ + int i; + SHA1_CTX context; + uint8_t digest[20], buffer[16384]; + FILE *file; + + if (argc < 2) { + file = stdin; + } else { + if (!(file = fopen(argv[1], "rb"))) { + fputs("Unable to open file.", stderr); + return (-1); + } + } + SHA1Init(&context); + while (!feof(file)) { /* note: what if ferror(file) */ + i = fread(buffer, 1, 16384, file); + SHA1Update(&context, buffer, i); + } + SHA1Final(digest, &context); + fclose(file); + + puts(mybase64(digest)); + + return 0; +} + +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/sha256crypt.c b/contrib/syslinux-4.02/com32/libutil/sha256crypt.c new file mode 100644 index 0000000..adc7b09 --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/sha256crypt.c @@ -0,0 +1,680 @@ +/* SHA256-based Unix crypt implementation. + Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>. */ + +#include <alloca.h> +#include <endian.h> +#include <errno.h> +#include <limits.h> +#include <stdint.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <minmax.h> +#include <sys/types.h> + +#include "xcrypt.h" + +#define MIN(x,y) min(x,y) +#define MAX(x,y) max(x,y) + +/* Structure to save state of computation between the single steps. */ +struct sha256_ctx { + uint32_t H[8]; + + uint32_t total[2]; + uint32_t buflen; + char buffer[128]; /* NB: always correctly aligned for uint32_t. */ +}; + +#if __BYTE_ORDER == __LITTLE_ENDIAN +# define SWAP(n) \ + (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) +#else +# define SWAP(n) (n) +#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 +}; + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 64 == 0. */ +static void +sha256_process_block(const void *buffer, size_t len, struct sha256_ctx *ctx) +{ + unsigned int t; + const uint32_t *words = buffer; + size_t nwords = len / sizeof(uint32_t); + uint32_t a = ctx->H[0]; + uint32_t b = ctx->H[1]; + uint32_t c = ctx->H[2]; + uint32_t d = ctx->H[3]; + uint32_t e = ctx->H[4]; + uint32_t f = ctx->H[5]; + uint32_t g = ctx->H[6]; + uint32_t h = ctx->H[7]; + + /* First increment the byte count. FIPS 180-2 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 64 bytes in each round of + the loop. */ + while (nwords > 0) { + uint32_t W[64]; + uint32_t a_save = a; + uint32_t b_save = b; + uint32_t c_save = c; + uint32_t d_save = d; + uint32_t e_save = e; + uint32_t f_save = f; + uint32_t g_save = g; + uint32_t h_save = h; + + /* Operators defined in FIPS 180-2:4.1.2. */ +#define Ch(x, y, z) ((x & y) ^ (~x & z)) +#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) +#define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22)) +#define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25)) +#define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3)) +#define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10)) + + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ +#define CYCLIC(w, s) ((w >> s) | (w << (32 - s))) + + /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */ + for (t = 0; t < 16; ++t) { + W[t] = SWAP(*words); + ++words; + } + for (t = 16; t < 64; ++t) + W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; + + /* The actual computation according to FIPS 180-2:6.2.2 step 3. */ + for (t = 0; t < 64; ++t) { + uint32_t T1 = h + S1(e) + Ch(e, f, g) + K[t] + W[t]; + uint32_t T2 = S0(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + } + + /* Add the starting values of the context according to FIPS 180-2:6.2.2 + step 4. */ + a += a_save; + b += b_save; + c += c_save; + d += d_save; + e += e_save; + f += f_save; + g += g_save; + h += h_save; + + /* Prepare for the next round. */ + nwords -= 16; + } + + /* Put checksum in context given as argument. */ + ctx->H[0] = a; + ctx->H[1] = b; + ctx->H[2] = c; + ctx->H[3] = d; + ctx->H[4] = e; + ctx->H[5] = f; + ctx->H[6] = g; + ctx->H[7] = h; +} + +/* Initialize structure containing state of computation. + (FIPS 180-2:5.3.2) */ +static void sha256_init_ctx(struct sha256_ctx *ctx) +{ + 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; + + ctx->total[0] = ctx->total[1] = 0; + 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. */ +static void *sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) +{ + unsigned int i; + /* Take yet unprocessed bytes into account. */ + uint32_t bytes = ctx->buflen; + size_t pad; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + 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. */ + *(uint32_t *) & ctx->buffer[bytes + pad + 4] = SWAP(ctx->total[0] << 3); + *(uint32_t *) & ctx->buffer[bytes + pad] = SWAP((ctx->total[1] << 3) | + (ctx->total[0] >> 29)); + + /* Process last bytes. */ + sha256_process_block(ctx->buffer, bytes + pad + 8, ctx); + + /* Put result from CTX in first 32 bytes following RESBUF. */ + for (i = 0; i < 8; ++i) + ((uint32_t *) resbuf)[i] = SWAP(ctx->H[i]); + + return resbuf; +} + +static void +sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx) +{ + /* 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) { + sha256_process_block(ctx->buffer, ctx->buflen & ~63, ctx); + + 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) { +/* 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) { + sha256_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *)buffer + 64; + len -= 64; + } else { + sha256_process_block(buffer, len & ~63, ctx); + 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) { + sha256_process_block(ctx->buffer, 64, ctx); + left_over -= 64; + memcpy(ctx->buffer, &ctx->buffer[64], left_over); + } + ctx->buflen = left_over; + } +} + +/* Define our magic string to mark salt for SHA256 "encryption" + replacement. */ +static const char sha256_salt_prefix[] = "$5$"; + +/* Prefix for optional rounds specification. */ +static const char sha256_rounds_prefix[] = "rounds="; + +/* Maximum salt string length. */ +#define SALT_LEN_MAX 16U +/* Default number of rounds if not explicitly specified. */ +#define ROUNDS_DEFAULT 5000UL +/* Minimum number of rounds. */ +#define ROUNDS_MIN 1000UL +/* Maximum number of rounds. */ +#define ROUNDS_MAX 999999999UL + +/* Table with characters for base64 transformation. */ +static const char b64t[64] = + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +static char *sha256_crypt_r(const char *key, const char *salt, char *buffer, + int buflen) +{ + unsigned char alt_result[32] + __attribute__ ((__aligned__(__alignof__(uint32_t)))); + unsigned char temp_result[32] + __attribute__ ((__aligned__(__alignof__(uint32_t)))); + struct sha256_ctx ctx; + struct sha256_ctx alt_ctx; + size_t salt_len; + size_t key_len; + size_t cnt; + char *cp; + char *copied_key = NULL; + char *copied_salt = NULL; + char *p_bytes; + char *s_bytes; + /* Default number of rounds. */ + size_t rounds = ROUNDS_DEFAULT; + bool rounds_custom = false; + + /* Find beginning of salt string. The prefix should normally always + be present. Just in case it is not. */ + if (strncmp(sha256_salt_prefix, salt, sizeof(sha256_salt_prefix) - 1) == 0) + /* Skip salt prefix. */ + salt += sizeof(sha256_salt_prefix) - 1; + + if (strncmp(salt, sha256_rounds_prefix, sizeof(sha256_rounds_prefix) - 1) + == 0) { + const char *num = salt + sizeof(sha256_rounds_prefix) - 1; + char *endp; + unsigned long int srounds = strtoul(num, &endp, 10); + if (*endp == '$') { + salt = endp + 1; + rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX)); + rounds_custom = true; + } + } + + salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX); + key_len = strlen(key); + + if ((key - (char *)0) % __alignof__(uint32_t) != 0) { + char *tmp = (char *)alloca(key_len + __alignof__(uint32_t)); + key = copied_key = memcpy(tmp + __alignof__(uint32_t) + - (tmp - (char *)0) % __alignof__(uint32_t), + key, key_len); + } + + if ((salt - (char *)0) % __alignof__(uint32_t) != 0) { + char *tmp = (char *)alloca(salt_len + __alignof__(uint32_t)); + salt = copied_salt = memcpy(tmp + __alignof__(uint32_t) + - (tmp - (char *)0) % __alignof__(uint32_t), + salt, salt_len); + } + + /* Prepare for the real work. */ + sha256_init_ctx(&ctx); + + /* Add the key string. */ + sha256_process_bytes(key, key_len, &ctx); + + /* The last part is the salt string. This must be at most 8 + characters and it ends at the first `$' character (for + compatibility with existing implementations). */ + sha256_process_bytes(salt, salt_len, &ctx); + + /* Compute alternate SHA256 sum with input KEY, SALT, and KEY. The + final result will be added to the first context. */ + sha256_init_ctx(&alt_ctx); + + /* Add key. */ + sha256_process_bytes(key, key_len, &alt_ctx); + + /* Add salt. */ + sha256_process_bytes(salt, salt_len, &alt_ctx); + + /* Add key again. */ + sha256_process_bytes(key, key_len, &alt_ctx); + + /* Now get result of this (32 bytes) and add it to the other + context. */ + sha256_finish_ctx(&alt_ctx, alt_result); + + /* Add for any character in the key one byte of the alternate sum. */ + for (cnt = key_len; cnt > 32; cnt -= 32) + sha256_process_bytes(alt_result, 32, &ctx); + sha256_process_bytes(alt_result, cnt, &ctx); + + /* Take the binary representation of the length of the key and for every + 1 add the alternate sum, for every 0 the key. */ + for (cnt = key_len; cnt; cnt >>= 1) + if ((cnt & 1) != 0) + sha256_process_bytes(alt_result, 32, &ctx); + else + sha256_process_bytes(key, key_len, &ctx); + + /* Create intermediate result. */ + sha256_finish_ctx(&ctx, alt_result); + + /* Start computation of P byte sequence. */ + sha256_init_ctx(&alt_ctx); + + /* For every character in the password add the entire password. */ + for (cnt = 0; cnt < key_len; ++cnt) + sha256_process_bytes(key, key_len, &alt_ctx); + + /* Finish the digest. */ + sha256_finish_ctx(&alt_ctx, temp_result); + + /* Create byte sequence P. */ + cp = p_bytes = alloca(key_len); + for (cnt = key_len; cnt >= 32; cnt -= 32) + cp = mempcpy(cp, temp_result, 32); + memcpy(cp, temp_result, cnt); + + /* Start computation of S byte sequence. */ + sha256_init_ctx(&alt_ctx); + + /* For every character in the password add the entire password. */ + for (cnt = 0; cnt < (size_t)16 + alt_result[0]; ++cnt) + sha256_process_bytes(salt, salt_len, &alt_ctx); + + /* Finish the digest. */ + sha256_finish_ctx(&alt_ctx, temp_result); + + /* Create byte sequence S. */ + cp = s_bytes = alloca(salt_len); + for (cnt = salt_len; cnt >= 32; cnt -= 32) + cp = mempcpy(cp, temp_result, 32); + memcpy(cp, temp_result, cnt); + + /* Repeatedly run the collected hash value through SHA256 to burn + CPU cycles. */ + for (cnt = 0; cnt < rounds; ++cnt) { + /* New context. */ + sha256_init_ctx(&ctx); + + /* Add key or last result. */ + if ((cnt & 1) != 0) + sha256_process_bytes(p_bytes, key_len, &ctx); + else + sha256_process_bytes(alt_result, 32, &ctx); + + /* Add salt for numbers not divisible by 3. */ + if (cnt % 3 != 0) + sha256_process_bytes(s_bytes, salt_len, &ctx); + + /* Add key for numbers not divisible by 7. */ + if (cnt % 7 != 0) + sha256_process_bytes(p_bytes, key_len, &ctx); + + /* Add key or last result. */ + if ((cnt & 1) != 0) + sha256_process_bytes(alt_result, 32, &ctx); + else + sha256_process_bytes(p_bytes, key_len, &ctx); + + /* Create intermediate result. */ + sha256_finish_ctx(&ctx, alt_result); + } + + /* Now we can construct the result string. It consists of three + parts. */ + cp = stpncpy(buffer, sha256_salt_prefix, MAX(0, buflen)); + buflen -= sizeof(sha256_salt_prefix) - 1; + + if (rounds_custom) { + int n = snprintf(cp, MAX(0, buflen), "%s%zu$", + sha256_rounds_prefix, rounds); + cp += n; + buflen -= n; + } + + cp = stpncpy(cp, salt, MIN((size_t) MAX(0, buflen), salt_len)); + buflen -= MIN((size_t) MAX(0, buflen), salt_len); + + if (buflen > 0) { + *cp++ = '$'; + --buflen; + } +#define b64_from_24bit(B2, B1, B0, N) \ + do { \ + unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \ + int n = (N); \ + while (n-- > 0 && buflen > 0) \ + { \ + *cp++ = b64t[w & 0x3f]; \ + --buflen; \ + w >>= 6; \ + } \ + } while (0) + + b64_from_24bit(alt_result[0], alt_result[10], alt_result[20], 4); + b64_from_24bit(alt_result[21], alt_result[1], alt_result[11], 4); + b64_from_24bit(alt_result[12], alt_result[22], alt_result[2], 4); + b64_from_24bit(alt_result[3], alt_result[13], alt_result[23], 4); + b64_from_24bit(alt_result[24], alt_result[4], alt_result[14], 4); + b64_from_24bit(alt_result[15], alt_result[25], alt_result[5], 4); + b64_from_24bit(alt_result[6], alt_result[16], alt_result[26], 4); + b64_from_24bit(alt_result[27], alt_result[7], alt_result[17], 4); + b64_from_24bit(alt_result[18], alt_result[28], alt_result[8], 4); + b64_from_24bit(alt_result[9], alt_result[19], alt_result[29], 4); + b64_from_24bit(0, alt_result[31], alt_result[30], 3); + if (buflen <= 0) { + errno = ERANGE; + buffer = NULL; + } else + *cp = '\0'; /* Terminate the string. */ + + /* Clear the buffer for the intermediate result so that people + attaching to processes or reading core dumps cannot get any + information. We do it in this way to clear correct_words[] + inside the SHA256 implementation as well. */ + sha256_init_ctx(&ctx); + sha256_finish_ctx(&ctx, alt_result); + memset(temp_result, '\0', sizeof(temp_result)); + memset(p_bytes, '\0', key_len); + memset(s_bytes, '\0', salt_len); + memset(&ctx, '\0', sizeof(ctx)); + memset(&alt_ctx, '\0', sizeof(alt_ctx)); + if (copied_key != NULL) + memset(copied_key, '\0', key_len); + if (copied_salt != NULL) + memset(copied_salt, '\0', salt_len); + + return buffer; +} + +/* This entry point is equivalent to the `crypt' function in Unix + libcs. */ +char *sha256_crypt(const char *key, const char *salt) +{ + /* We don't want to have an arbitrary limit in the size of the + password. We can compute an upper bound for the size of the + result in advance and so we can prepare the buffer we pass to + `sha256_crypt_r'. */ + static char *buffer; + static int buflen; + int needed = (sizeof(sha256_salt_prefix) - 1 + + sizeof(sha256_rounds_prefix) + 9 + 1 + + strlen(salt) + 1 + 43 + 1); + + if (buflen < needed) { + char *new_buffer = (char *)realloc(buffer, needed); + if (new_buffer == NULL) + return NULL; + + buffer = new_buffer; + buflen = needed; + } + + return sha256_crypt_r(key, salt, buffer, buflen); +} + +#ifdef TEST +static const struct { + const char *input; + const char result[32]; +} tests[] = { + /* Test vectors from FIPS 180-2: appendix B.1. */ + { + "abc", + "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23" + "\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad"}, + /* Test vectors from FIPS 180-2: appendix B.2. */ + { + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39" + "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"}, + /* Test vectors from the NESSIE project. */ + { + "", "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24" + "\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55"}, + { + "a", "\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d" + "\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb"}, + { + "message digest", + "\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad" + "\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50"}, + { + "abcdefghijklmnopqrstuvwxyz", + "\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52" + "\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73"}, + { + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39" + "\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1"}, + { + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80" + "\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0"}, + { + "123456789012345678901234567890123456789012345678901234567890" + "12345678901234567890", + "\xf3\x71\xbc\x4a\x31\x1f\x2b\x00\x9e\xef\x95\x2d\xd8\x3c\xa8\x0e" + "\x2b\x60\x02\x6c\x8e\x93\x55\x92\xd0\xf9\xc3\x08\x45\x3c\x81\x3e"} +}; + +#define ntests (sizeof (tests) / sizeof (tests[0])) + +static const struct { + const char *salt; + const char *input; + const char *expected; +} tests2[] = { + { + "$5$saltstring", "Hello world!", + "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5"}, { + "$5$rounds=10000$saltstringsaltstring", "Hello world!", + "$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2." + "opqey6IcA"}, { + "$5$rounds=5000$toolongsaltstring", "This is just a test", + "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8" + "mGRcvxa5"}, { + "$5$rounds=1400$anotherlongsaltstring", + "a very much longer text to encrypt. This one even stretches over more" + "than one line.", + "$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12" + "oP84Bnq1"}, { + "$5$rounds=77777$short", + "we have a short salt string but not a short password", + "$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/"}, + { + "$5$rounds=123456$asaltof16chars..", "a short string", + "$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/" + "cZKmF/wJvD"}, { +"$5$rounds=10$roundstoolow", "the minimum number is still observed", + "$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97" + "2bIC"},}; +#define ntests2 (sizeof (tests2) / sizeof (tests2[0])) + +int main(void) +{ + struct sha256_ctx ctx; + char sum[32]; + int result = 0; + int cnt; + + for (cnt = 0; cnt < (int)ntests; ++cnt) { + sha256_init_ctx(&ctx); + sha256_process_bytes(tests[cnt].input, strlen(tests[cnt].input), &ctx); + sha256_finish_ctx(&ctx, sum); + if (memcmp(tests[cnt].result, sum, 32) != 0) { + printf("test %d run %d failed\n", cnt, 1); + result = 1; + } + + sha256_init_ctx(&ctx); + for (int i = 0; tests[cnt].input[i] != '\0'; ++i) + sha256_process_bytes(&tests[cnt].input[i], 1, &ctx); + sha256_finish_ctx(&ctx, sum); + if (memcmp(tests[cnt].result, sum, 32) != 0) { + printf("test %d run %d failed\n", cnt, 2); + result = 1; + } + } + + /* Test vector from FIPS 180-2: appendix B.3. */ + char buf[1000]; + memset(buf, 'a', sizeof(buf)); + sha256_init_ctx(&ctx); + for (int i = 0; i < 1000; ++i) + sha256_process_bytes(buf, sizeof(buf), &ctx); + sha256_finish_ctx(&ctx, sum); + static const char expected[32] = + "\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67" + "\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0"; + if (memcmp(expected, sum, 32) != 0) { + printf("test %d failed\n", cnt); + result = 1; + } + + for (cnt = 0; cnt < ntests2; ++cnt) { + char *cp = sha256_crypt(tests2[cnt].input, tests2[cnt].salt); + + if (strcmp(cp, tests2[cnt].expected) != 0) { + printf("test %d: expected \"%s\", got \"%s\"\n", + cnt, tests2[cnt].expected, cp); + result = 1; + } + } + + if (result == 0) + puts("all tests OK"); + + return result; +} +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/sha512crypt.c b/contrib/syslinux-4.02/com32/libutil/sha512crypt.c new file mode 100644 index 0000000..9db9c0c --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/sha512crypt.c @@ -0,0 +1,749 @@ +/* SHA512-based Unix crypt implementation. + Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>. */ + +#include <alloca.h> +#include <endian.h> +#include <errno.h> +#include <limits.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <minmax.h> +#include <sys/types.h> + +#include "xcrypt.h" + +#define MIN(x,y) min(x,y) +#define MAX(x,y) max(x,y) + +/* Structure to save state of computation between the single steps. */ +struct sha512_ctx { + uint64_t H[8]; + + uint64_t total[2]; + uint64_t buflen; + char buffer[256]; /* NB: always correctly aligned for uint64_t. */ +}; + +#if __BYTE_ORDER == __LITTLE_ENDIAN +# define SWAP(n) \ + (((n) << 56) \ + | (((n) & 0xff00) << 40) \ + | (((n) & 0xff0000) << 24) \ + | (((n) & 0xff000000) << 8) \ + | (((n) >> 8) & 0xff000000) \ + | (((n) >> 24) & 0xff0000) \ + | (((n) >> 40) & 0xff00) \ + | ((n) >> 56)) +#else +# define SWAP(n) (n) +#endif + +/* This array contains the bytes used to pad the buffer to the next + 64-byte boundary. (FIPS 180-2:5.1.2) */ +static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ... */ }; + +/* Constants for SHA512 from FIPS 180-2:4.2.3. */ +static const uint64_t K[80] = { + UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), + UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc), + UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), + UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118), + UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), + UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2), + UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), + UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694), + UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), + UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65), + UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), + UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5), + UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), + UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4), + UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), + UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70), + UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), + UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df), + UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), + UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b), + UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), + UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30), + UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), + UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8), + UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), + UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8), + UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), + UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3), + UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), + UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec), + UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), + UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b), + UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), + UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178), + UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), + UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b), + UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), + UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c), + UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), + UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817) +}; + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 128 == 0. */ +static void +sha512_process_block(const void *buffer, size_t len, struct sha512_ctx *ctx) +{ + unsigned int t; + const uint64_t *words = buffer; + size_t nwords = len / sizeof(uint64_t); + uint64_t a = ctx->H[0]; + uint64_t b = ctx->H[1]; + uint64_t c = ctx->H[2]; + uint64_t d = ctx->H[3]; + uint64_t e = ctx->H[4]; + uint64_t f = ctx->H[5]; + uint64_t g = ctx->H[6]; + uint64_t h = ctx->H[7]; + + /* First increment the byte count. FIPS 180-2 specifies the possible + length of the file up to 2^128 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 128 bytes in each round of + the loop. */ + while (nwords > 0) { + uint64_t W[80]; + uint64_t a_save = a; + uint64_t b_save = b; + uint64_t c_save = c; + uint64_t d_save = d; + uint64_t e_save = e; + uint64_t f_save = f; + uint64_t g_save = g; + uint64_t h_save = h; + + /* Operators defined in FIPS 180-2:4.1.2. */ +#define Ch(x, y, z) ((x & y) ^ (~x & z)) +#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) +#define S0(x) (CYCLIC (x, 28) ^ CYCLIC (x, 34) ^ CYCLIC (x, 39)) +#define S1(x) (CYCLIC (x, 14) ^ CYCLIC (x, 18) ^ CYCLIC (x, 41)) +#define R0(x) (CYCLIC (x, 1) ^ CYCLIC (x, 8) ^ (x >> 7)) +#define R1(x) (CYCLIC (x, 19) ^ CYCLIC (x, 61) ^ (x >> 6)) + + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ +#define CYCLIC(w, s) ((w >> s) | (w << (64 - s))) + + /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ + for (t = 0; t < 16; ++t) { + W[t] = SWAP(*words); + ++words; + } + for (t = 16; t < 80; ++t) + W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; + + /* The actual computation according to FIPS 180-2:6.3.2 step 3. */ + for (t = 0; t < 80; ++t) { + uint64_t T1 = h + S1(e) + Ch(e, f, g) + K[t] + W[t]; + uint64_t T2 = S0(a) + Maj(a, b, c); + h = g; + g = f; + f = e; + e = d + T1; + d = c; + c = b; + b = a; + a = T1 + T2; + } + + /* Add the starting values of the context according to FIPS 180-2:6.3.2 + step 4. */ + a += a_save; + b += b_save; + c += c_save; + d += d_save; + e += e_save; + f += f_save; + g += g_save; + h += h_save; + + /* Prepare for the next round. */ + nwords -= 16; + } + + /* Put checksum in context given as argument. */ + ctx->H[0] = a; + ctx->H[1] = b; + ctx->H[2] = c; + ctx->H[3] = d; + ctx->H[4] = e; + ctx->H[5] = f; + ctx->H[6] = g; + ctx->H[7] = h; +} + +/* Initialize structure containing state of computation. + (FIPS 180-2:5.3.3) */ +static void sha512_init_ctx(struct sha512_ctx *ctx) +{ + ctx->H[0] = UINT64_C(0x6a09e667f3bcc908); + ctx->H[1] = UINT64_C(0xbb67ae8584caa73b); + ctx->H[2] = UINT64_C(0x3c6ef372fe94f82b); + ctx->H[3] = UINT64_C(0xa54ff53a5f1d36f1); + ctx->H[4] = UINT64_C(0x510e527fade682d1); + ctx->H[5] = UINT64_C(0x9b05688c2b3e6c1f); + ctx->H[6] = UINT64_C(0x1f83d9abfb41bd6b); + ctx->H[7] = UINT64_C(0x5be0cd19137e2179); + + ctx->total[0] = ctx->total[1] = 0; + 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. */ +static void *sha512_finish_ctx(struct sha512_ctx *ctx, void *resbuf) +{ + unsigned int i; + /* Take yet unprocessed bytes into account. */ + uint64_t bytes = ctx->buflen; + size_t pad; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + pad = bytes >= 112 ? 128 + 112 - bytes : 112 - bytes; + memcpy(&ctx->buffer[bytes], fillbuf, pad); + + /* Put the 128-bit file length in *bits* at the end of the buffer. */ + *(uint64_t *) & ctx->buffer[bytes + pad + 8] = SWAP(ctx->total[0] << 3); + *(uint64_t *) & ctx->buffer[bytes + pad] = SWAP((ctx->total[1] << 3) | + (ctx->total[0] >> 61)); + + /* Process last bytes. */ + sha512_process_block(ctx->buffer, bytes + pad + 16, ctx); + + /* Put result from CTX in first 64 bytes following RESBUF. */ + for (i = 0; i < 8; ++i) + ((uint64_t *) resbuf)[i] = SWAP(ctx->H[i]); + + return resbuf; +} + +static void +sha512_process_bytes(const void *buffer, size_t len, struct sha512_ctx *ctx) +{ + /* 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 = 256 - left_over > len ? len : 256 - left_over; + + memcpy(&ctx->buffer[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 128) { + sha512_process_block(ctx->buffer, ctx->buflen & ~127, ctx); + + ctx->buflen &= 127; + /* The regions in the following copy operation cannot overlap. */ + memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~127], + ctx->buflen); + } + + buffer = (const char *)buffer + add; + len -= add; + } + + /* Process available complete blocks. */ + if (len >= 128) { +#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__ (uint64_t) != 0) +# else +# define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint64_t) != 0) +# endif + if (UNALIGNED_P(buffer)) + while (len > 128) { + sha512_process_block(memcpy(ctx->buffer, buffer, 128), 128, + ctx); + buffer = (const char *)buffer + 128; + len -= 128; + } else +#endif + { + sha512_process_block(buffer, len & ~127, ctx); + buffer = (const char *)buffer + (len & ~127); + len &= 127; + } + } + + /* 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 >= 128) { + sha512_process_block(ctx->buffer, 128, ctx); + left_over -= 128; + memcpy(ctx->buffer, &ctx->buffer[128], left_over); + } + ctx->buflen = left_over; + } +} + +/* Define our magic string to mark salt for SHA512 "encryption" + replacement. */ +static const char sha512_salt_prefix[] = "$6$"; + +/* Prefix for optional rounds specification. */ +static const char sha512_rounds_prefix[] = "rounds="; + +/* Maximum salt string length. */ +#define SALT_LEN_MAX 16U +/* Default number of rounds if not explicitly specified. */ +#define ROUNDS_DEFAULT 5000UL +/* Minimum number of rounds. */ +#define ROUNDS_MIN 1000UL +/* Maximum number of rounds. */ +#define ROUNDS_MAX 999999999UL + +/* Table with characters for base64 transformation. */ +static const char b64t[64] = + "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + +static char *sha512_crypt_r(const char *key, const char *salt, char *buffer, + int buflen) +{ + unsigned char alt_result[64] + __attribute__ ((__aligned__(__alignof__(uint64_t)))); + unsigned char temp_result[64] + __attribute__ ((__aligned__(__alignof__(uint64_t)))); + struct sha512_ctx ctx; + struct sha512_ctx alt_ctx; + size_t salt_len; + size_t key_len; + size_t cnt; + char *cp; + char *copied_key = NULL; + char *copied_salt = NULL; + char *p_bytes; + char *s_bytes; + /* Default number of rounds. */ + size_t rounds = ROUNDS_DEFAULT; + bool rounds_custom = false; + + /* Find beginning of salt string. The prefix should normally always + be present. Just in case it is not. */ + if (strncmp(sha512_salt_prefix, salt, sizeof(sha512_salt_prefix) - 1) == 0) + /* Skip salt prefix. */ + salt += sizeof(sha512_salt_prefix) - 1; + + if (strncmp(salt, sha512_rounds_prefix, sizeof(sha512_rounds_prefix) - 1) + == 0) { + const char *num = salt + sizeof(sha512_rounds_prefix) - 1; + char *endp; + unsigned long int srounds = strtoul(num, &endp, 10); + if (*endp == '$') { + salt = endp + 1; + rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX)); + rounds_custom = true; + } + } + + salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX); + key_len = strlen(key); + + if ((key - (char *)0) % __alignof__(uint64_t) != 0) { + char *tmp = (char *)alloca(key_len + __alignof__(uint64_t)); + key = copied_key = memcpy(tmp + __alignof__(uint64_t) + - (tmp - (char *)0) % __alignof__(uint64_t), + key, key_len); + } + + if ((salt - (char *)0) % __alignof__(uint64_t) != 0) { + char *tmp = (char *)alloca(salt_len + __alignof__(uint64_t)); + salt = copied_salt = memcpy(tmp + __alignof__(uint64_t) + - (tmp - (char *)0) % __alignof__(uint64_t), + salt, salt_len); + } + + /* Prepare for the real work. */ + sha512_init_ctx(&ctx); + + /* Add the key string. */ + sha512_process_bytes(key, key_len, &ctx); + + /* The last part is the salt string. This must be at most 8 + characters and it ends at the first `$' character (for + compatibility with existing implementations). */ + sha512_process_bytes(salt, salt_len, &ctx); + + /* Compute alternate SHA512 sum with input KEY, SALT, and KEY. The + final result will be added to the first context. */ + sha512_init_ctx(&alt_ctx); + + /* Add key. */ + sha512_process_bytes(key, key_len, &alt_ctx); + + /* Add salt. */ + sha512_process_bytes(salt, salt_len, &alt_ctx); + + /* Add key again. */ + sha512_process_bytes(key, key_len, &alt_ctx); + + /* Now get result of this (64 bytes) and add it to the other + context. */ + sha512_finish_ctx(&alt_ctx, alt_result); + + /* Add for any character in the key one byte of the alternate sum. */ + for (cnt = key_len; cnt > 64; cnt -= 64) + sha512_process_bytes(alt_result, 64, &ctx); + sha512_process_bytes(alt_result, cnt, &ctx); + + /* Take the binary representation of the length of the key and for every + 1 add the alternate sum, for every 0 the key. */ + for (cnt = key_len; cnt > 0; cnt >>= 1) + if ((cnt & 1) != 0) + sha512_process_bytes(alt_result, 64, &ctx); + else + sha512_process_bytes(key, key_len, &ctx); + + /* Create intermediate result. */ + sha512_finish_ctx(&ctx, alt_result); + + /* Start computation of P byte sequence. */ + sha512_init_ctx(&alt_ctx); + + /* For every character in the password add the entire password. */ + for (cnt = 0; cnt < key_len; ++cnt) + sha512_process_bytes(key, key_len, &alt_ctx); + + /* Finish the digest. */ + sha512_finish_ctx(&alt_ctx, temp_result); + + /* Create byte sequence P. */ + cp = p_bytes = alloca(key_len); + for (cnt = key_len; cnt >= 64; cnt -= 64) + cp = mempcpy(cp, temp_result, 64); + memcpy(cp, temp_result, cnt); + + /* Start computation of S byte sequence. */ + sha512_init_ctx(&alt_ctx); + + /* For every character in the password add the entire password. */ + for (cnt = 0; cnt < (size_t)16 + alt_result[0]; ++cnt) + sha512_process_bytes(salt, salt_len, &alt_ctx); + + /* Finish the digest. */ + sha512_finish_ctx(&alt_ctx, temp_result); + + /* Create byte sequence S. */ + cp = s_bytes = alloca(salt_len); + for (cnt = salt_len; cnt >= 64; cnt -= 64) + cp = mempcpy(cp, temp_result, 64); + memcpy(cp, temp_result, cnt); + + /* Repeatedly run the collected hash value through SHA512 to burn + CPU cycles. */ + for (cnt = 0; cnt < rounds; ++cnt) { + /* New context. */ + sha512_init_ctx(&ctx); + + /* Add key or last result. */ + if ((cnt & 1) != 0) + sha512_process_bytes(p_bytes, key_len, &ctx); + else + sha512_process_bytes(alt_result, 64, &ctx); + + /* Add salt for numbers not divisible by 3. */ + if (cnt % 3 != 0) + sha512_process_bytes(s_bytes, salt_len, &ctx); + + /* Add key for numbers not divisible by 7. */ + if (cnt % 7 != 0) + sha512_process_bytes(p_bytes, key_len, &ctx); + + /* Add key or last result. */ + if ((cnt & 1) != 0) + sha512_process_bytes(alt_result, 64, &ctx); + else + sha512_process_bytes(p_bytes, key_len, &ctx); + + /* Create intermediate result. */ + sha512_finish_ctx(&ctx, alt_result); + } + + /* Now we can construct the result string. It consists of three + parts. */ + cp = stpncpy(buffer, sha512_salt_prefix, MAX(0, buflen)); + buflen -= sizeof(sha512_salt_prefix) - 1; + + if (rounds_custom) { + int n = snprintf(cp, MAX(0, buflen), "%s%zu$", + sha512_rounds_prefix, rounds); + cp += n; + buflen -= n; + } + + cp = stpncpy(cp, salt, MIN((size_t) MAX(0, buflen), salt_len)); + buflen -= MIN((size_t) MAX(0, buflen), salt_len); + + if (buflen > 0) { + *cp++ = '$'; + --buflen; + } +#define b64_from_24bit(B2, B1, B0, N) \ + do { \ + unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \ + int n = (N); \ + while (n-- > 0 && buflen > 0) \ + { \ + *cp++ = b64t[w & 0x3f]; \ + --buflen; \ + w >>= 6; \ + } \ + } while (0) + + b64_from_24bit(alt_result[0], alt_result[21], alt_result[42], 4); + b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4); + b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4); + b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4); + b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4); + b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4); + b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4); + b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4); + b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4); + b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4); + b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4); + b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4); + b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4); + b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4); + b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4); + b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4); + b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4); + b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4); + b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4); + b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4); + b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4); + b64_from_24bit(0, 0, alt_result[63], 2); + + if (buflen <= 0) { + errno = ERANGE; + buffer = NULL; + } else + *cp = '\0'; /* Terminate the string. */ + + /* Clear the buffer for the intermediate result so that people + attaching to processes or reading core dumps cannot get any + information. We do it in this way to clear correct_words[] + inside the SHA512 implementation as well. */ + sha512_init_ctx(&ctx); + sha512_finish_ctx(&ctx, alt_result); + memset(temp_result, '\0', sizeof(temp_result)); + memset(p_bytes, '\0', key_len); + memset(s_bytes, '\0', salt_len); + memset(&ctx, '\0', sizeof(ctx)); + memset(&alt_ctx, '\0', sizeof(alt_ctx)); + if (copied_key != NULL) + memset(copied_key, '\0', key_len); + if (copied_salt != NULL) + memset(copied_salt, '\0', salt_len); + + return buffer; +} + +/* This entry point is equivalent to the `crypt' function in Unix + libcs. */ +char *sha512_crypt(const char *key, const char *salt) +{ + /* We don't want to have an arbitrary limit in the size of the + password. We can compute an upper bound for the size of the + result in advance and so we can prepare the buffer we pass to + `sha512_crypt_r'. */ + static char *buffer; + static int buflen; + int needed = (sizeof(sha512_salt_prefix) - 1 + + sizeof(sha512_rounds_prefix) + 9 + 1 + + strlen(salt) + 1 + 86 + 1); + + if (buflen < needed) { + char *new_buffer = (char *)realloc(buffer, needed); + if (new_buffer == NULL) + return NULL; + + buffer = new_buffer; + buflen = needed; + } + + return sha512_crypt_r(key, salt, buffer, buflen); +} + +#ifdef TEST +static const struct { + const char *input; + const char result[64]; +} tests[] = { + /* Test vectors from FIPS 180-2: appendix C.1. */ + { + "abc", + "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31" + "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a" + "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd" + "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f"}, + /* Test vectors from FIPS 180-2: appendix C.2. */ + { + "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" + "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", + "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f" + "\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18" + "\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a" + "\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09"}, + /* Test vectors from the NESSIE project. */ + { + "", "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07" + "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce" + "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f" + "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e"}, + { + "a", "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2" + "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53" + "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46" + "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75"}, + { + "message digest", + "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51" + "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33" + "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20" + "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c"}, + { + "abcdefghijklmnopqrstuvwxyz", + "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81" + "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29" + "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59" + "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1"}, + { + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16" + "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35" + "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0" + "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45"}, + { + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93" + "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4" + "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab" + "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94"}, + { + "123456789012345678901234567890123456789012345678901234567890" + "12345678901234567890", + "\x72\xec\x1e\xf1\x12\x4a\x45\xb0\x47\xe8\xb7\xc7\x5a\x93\x21\x95" + "\x13\x5b\xb6\x1d\xe2\x4e\xc0\xd1\x91\x40\x42\x24\x6e\x0a\xec\x3a" + "\x23\x54\xe0\x93\xd7\x6f\x30\x48\xb4\x56\x76\x43\x46\x90\x0c\xb1" + "\x30\xd2\xa4\xfd\x5d\xd1\x6a\xbb\x5e\x30\xbc\xb8\x50\xde\xe8\x43"} +}; + +#define ntests (sizeof (tests) / sizeof (tests[0])) + +static const struct { + const char *salt; + const char *input; + const char *expected; +} tests2[] = { + { + "$6$saltstring", "Hello world!", + "$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu" + "esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"}, { + "$6$rounds=10000$saltstringsaltstring", "Hello world!", + "$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb" + "HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v."}, { + "$6$rounds=5000$toolongsaltstring", "This is just a test", + "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ" + "zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0"}, { + "$6$rounds=1400$anotherlongsaltstring", + "a very much longer text to encrypt. This one even stretches over more" + "than one line.", + "$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP" + "vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1"}, { + "$6$rounds=77777$short", + "we have a short salt string but not a short password", + "$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g" + "ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0"}, { + "$6$rounds=123456$asaltof16chars..", "a short string", + "$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc" + "elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1"}, { +"$6$rounds=10$roundstoolow", "the minimum number is still observed", + "$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x" + "hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX."},}; +#define ntests2 (sizeof (tests2) / sizeof (tests2[0])) + +int main(void) +{ + struct sha512_ctx ctx; + char sum[64]; + int result = 0; + int cnt; + + for (cnt = 0; cnt < (int)ntests; ++cnt) { + sha512_init_ctx(&ctx); + sha512_process_bytes(tests[cnt].input, strlen(tests[cnt].input), &ctx); + sha512_finish_ctx(&ctx, sum); + if (memcmp(tests[cnt].result, sum, 64) != 0) { + printf("test %d run %d failed\n", cnt, 1); + result = 1; + } + + sha512_init_ctx(&ctx); + for (int i = 0; tests[cnt].input[i] != '\0'; ++i) + sha512_process_bytes(&tests[cnt].input[i], 1, &ctx); + sha512_finish_ctx(&ctx, sum); + if (memcmp(tests[cnt].result, sum, 64) != 0) { + printf("test %d run %d failed\n", cnt, 2); + result = 1; + } + } + + /* Test vector from FIPS 180-2: appendix C.3. */ + char buf[1000]; + memset(buf, 'a', sizeof(buf)); + sha512_init_ctx(&ctx); + for (int i = 0; i < 1000; ++i) + sha512_process_bytes(buf, sizeof(buf), &ctx); + sha512_finish_ctx(&ctx, sum); + static const char expected[64] = + "\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63" + "\x8e\x1f\x98\xb1\x3b\x20\x44\x28\x56\x32\xa8\x03\xaf\xa9\x73\xeb" + "\xde\x0f\xf2\x44\x87\x7e\xa6\x0a\x4c\xb0\x43\x2c\xe5\x77\xc3\x1b" + "\xeb\x00\x9c\x5c\x2c\x49\xaa\x2e\x4e\xad\xb2\x17\xad\x8c\xc0\x9b"; + if (memcmp(expected, sum, 64) != 0) { + printf("test %d failed\n", cnt); + result = 1; + } + + for (cnt = 0; cnt < ntests2; ++cnt) { + char *cp = sha512_crypt(tests2[cnt].input, tests2[cnt].salt); + + if (strcmp(cp, tests2[cnt].expected) != 0) { + printf("test %d: expected \"%s\", got \"%s\"\n", + cnt, tests2[cnt].expected, cp); + result = 1; + } + } + + if (result == 0) + puts("all tests OK"); + + return result; +} +#endif diff --git a/contrib/syslinux-4.02/com32/libutil/unbase64.c b/contrib/syslinux-4.02/com32/libutil/unbase64.c new file mode 100644 index 0000000..3cbf3fb --- /dev/null +++ b/contrib/syslinux-4.02/com32/libutil/unbase64.c @@ -0,0 +1,76 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2005-2008 H. Peter Anvin - All Rights Reserved + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall + * be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * ----------------------------------------------------------------------- */ + +/* + * unbase64.c + * + * Convert a string in base64 format to a byte array. + */ + +#include <string.h> +#include <base64.h> + +static const unsigned char _base64chars[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +size_t unbase64(unsigned char *buffer, size_t bufsiz, const char *txt) +{ + unsigned int bits = 0; + int nbits = 0; + char base64tbl[256]; + int i; + char v; + size_t nbytes = 0; + + memset(base64tbl, -1, sizeof base64tbl); + + for (i = 0; _base64chars[i]; i++) { + base64tbl[_base64chars[i]] = i; + } + + /* Also support filesystem safe alternate base64 encoding */ + base64tbl['.'] = 62; + base64tbl['-'] = 62; + base64tbl['_'] = 63; + + while (*txt) { + if ((v = base64tbl[(unsigned char)*txt]) >= 0) { + bits <<= 6; + bits += v; + nbits += 6; + if (nbits >= 8) { + if (nbytes < bufsiz) + *buffer++ = (bits >> (nbits - 8)); + nbytes++; + nbits -= 8; + } + } + txt++; + } + + return nbytes; +} |