Skip to content

Instantly share code, notes, and snippets.

@loreb
Created December 17, 2014 20:22
Show Gist options
  • Save loreb/4b7230f1e5d4cd7e4f4e to your computer and use it in GitHub Desktop.
Save loreb/4b7230f1e5d4cd7e4f4e to your computer and use it in GitHub Desktop.
get arc4random*.c from libressl - quick hack
#! /bin/sh
# Self-contained - get arc4random for emergencies.
set -e # Not in "#!" to test different shells.
# TODO figure out the source files automagically?
test 0 -eq $(ls|wc -l) || { echo>&2 "run $0 in an empty directory"; exit 100; }
# Fix $OS if needed (eg "win")
if [ x = x"$OS" ] ; then
OS=$(uname -s)
fi
# Borrowed from wikipedia + $PLAN9/bin/9c
case x"$OS" in
(*Darwin*)
OS=osx
;;
(*MINGW*|*MSYS*)
OS=win
;;
(*SunOS*)
OS=solaris
;;
esac
OS=$(echo "$OS"|tr 'A-Z' 'a-z')
URLS="
https://github.com/libressl-portable/portable/raw/master/crypto/compat/arc4random.h
https://github.com/libressl-portable/openbsd/raw/master/src/lib/libcrypto/crypto/arc4random_$OS.h
https://github.com/libressl-portable/openbsd/raw/master/src/lib/libcrypto/crypto/getentropy_$OS.c
https://github.com/libressl-portable/openbsd/raw/master/src/lib/libc/crypt/arc4random_uniform.c
https://github.com/libressl-portable/openbsd/raw/master/src/lib/libc/crypt/arc4random.c
https://github.com/libressl-portable/openbsd/raw/master/src/lib/libc/crypt/chacha_private.h
https://github.com/libressl-portable/openbsd/raw/master/src/lib/libc/string/explicit_bzero.c
"
if [ x != x"$ZSH_VERSION" ] ; then
# $URLS is multiline; zsh is too fancy for it:
# $ s='a b c' ; for i in $s ... ($s is NOT split into a,b,c!)
# Ok in at least bash, dash, ksh derivatives.
echo "FIXME zsh" >&2
exit 123
fi
wget -nv $URLS
# The '-i' flag is STUPID: "sed -iFOO" means "use FOO as backup extension",
# so an empty string means "edit in place", but "-i FOO" means
# "edit in place; FOO as a separate argument"...
# TODO FIXME -i is actually a GNU extension (but *BSD => some arc4random)
# Problem: Debian doesn't ship ed(1) by default(!) - use ex(1)
grep -q stdint.h arc4random_uniform.c \
|| sed -i 's/sys\/types.h/stdint.h/' arc4random_uniform.c
# ex -c '
# 1 insert
# #include <stdint.h>
# .
# w
# q
# ' arc4random_uniform.c
# Tested with ex(1) from traditional-vi.
# CYA - it's just a q&d script after all...
outofdate() {
exec>&2
echo "$0: #1 you did something wrong (nonempty dir etc);"
echo "$0: #2 missind dependency (openssl?);"
echo "$0: #3 bsd-ism, try eg _BSD_SOURCE..."
echo "$0: #4 this script is outdated - if so, please report it."
exit 123
}
# Test that it works.
set -C
cat >test4random.c <<EOF
#include <stdint.h>
#include <stdlib.h> /* size_t */
/*XXX This is a PRIVATE HEADER, DO NOT INCLUDE!!! */
/*#include "arc4random.h"*/
extern uint32_t arc4random(void);
void arc4random_buf(void *, size_t);
extern uint32_t arc4random_uniform(uint32_t);
#include <stdio.h>
int main()
{
printf("%d %d\n", (int)arc4random(), (int)arc4random_uniform(arc4random()));
printf("arc4random ok\n");
return 0;
}
EOF
LPTHREAD=
grep -q pthread.h *.[ch] && LPTHREAD=-lpthread
# lcrypto for SHA512 in the getentropy_fallback()
LCRYPTO=
grep -q ssl/ *.c && LCRYPTO=-lcrypto
# You may need to #define _BSD_SOURCE or similar.
${CC:-cc} *.c -o test4random $LCRYPTO $LPTHREAD || outofdate
./test4random
# Not-so-fun-fact: on openbsd, MINHERIT_ZERO brought a BIG speedup
# (no need to getpid() every time); on linux, getpid is fast.
# How fast? On my machine, ~25x faster than getppid(),
# due to http://yarchive.net/comp/linux/getpid_caching.html
exec>&2
echo "# To use arc4random in myprog.c, try these commands."
echo "rm -f test4random.c *.o"
echo "cc -c *.c && ar cru arc4random.a *.o"
test x = x"$LCRYPTO$LPTHREAD" \
|| echo "cc myprog.c arc4random.a $LCRYPTO $LPTHREAD"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment