Import libnacl from dnscrypt-proxy

This commit is contained in:
Frank Denis 2013-01-19 16:02:02 -08:00
parent 5a86d9e029
commit 95b9c37cc5
429 changed files with 18685 additions and 0 deletions

0
AUTHORS Normal file
View File

1
COPYING Normal file
View File

@ -0,0 +1 @@
Public domain

0
ChangeLog Normal file
View File

0
NEWS Normal file
View File

1
README Normal file
View File

@ -0,0 +1 @@
See README.markdown

0
README.markdown Normal file
View File

11
autogen.sh Executable file
View File

@ -0,0 +1,11 @@
#! /bin/sh
if [ -x "`which autoreconf 2>/dev/null`" ] ; then
exec autoreconf -ivf
fi
aclocal && \
autoheader && \
automake --add-missing --force-missing --include-deps && \
autoconf

204
configure.ac Normal file
View File

@ -0,0 +1,204 @@
AC_PREREQ([2.61])
AC_INIT([nacl-ref],[0.0.1],[https://github.com/jedisct1/nacl-ref])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_SRCDIR([src/libnacl/PROTOTYPES.c])
AC_CONFIG_HEADER([config.h])
AC_CANONICAL_HOST
AM_INIT_AUTOMAKE([1.9 dist-bzip2 tar-ustar])
AM_MAINTAINER_MODE
AM_DEP_TRACK
AC_SUBST(VERSION)
ISODATE=`date +%Y-%m-%d`
AC_SUBST(ISODATE)
LX_CFLAGS=${CFLAGS-NONE}
AC_PROG_CC_C99
AC_USE_SYSTEM_EXTENSIONS
CPPFLAGS="$CPPFLAGS -D_FORTIFY_SOURCE=2"
AS_IF([test "$cross_compiling" != no],
AC_DEFINE(CROSS_COMPILING,,[define if you are cross-compiling])
)
AX_CHECK_COMPILE_FLAG([-fPIC], [
AX_CHECK_LINK_FLAG([-fPIC],
[CFLAGS="$CFLAGS -fPIC"]
)
])
AS_IF([test "$enable_pie" != "no"],[
AX_CHECK_COMPILE_FLAG([-fPIE], [
AX_CHECK_LINK_FLAG([-fPIE],
[AX_CHECK_LINK_FLAG([-pie],
[CFLAGS="$CFLAGS -fPIE"
LDFLAGS="$LDFLAGS -pie"])
])
])
])
AX_CHECK_COMPILE_FLAG([-fcatch-undefined-c99-behavior],
[CFLAGS="$CFLAGS -fcatch-undefined-c99-behavior"],
[AX_CHECK_COMPILE_FLAG([-fwrapv], [CFLAGS="$CFLAGS -fwrapv"])]
)
AX_CHECK_COMPILE_FLAG([-fno-strict-aliasing], [CFLAGS="$CFLAGS -fno-strict-aliasing"])
AX_CHECK_COMPILE_FLAG([-fno-strict-overflow], [CFLAGS="$CFLAGS -fno-strict-overflow"])
case $host_os in
cygwin* | mingw* | pw32* | cegcc*)
AX_CHECK_LINK_FLAG([-Wl,--dynamicbase], [LDFLAGS="$LDFLAGS -Wl,--dynamicbase"])
AX_CHECK_LINK_FLAG([-Wl,--nxcompat], [LDFLAGS="$LDFLAGS -Wl,--nxcompat"])
AX_CHECK_COMPILE_FLAG([-fstack-protector-all], [
AX_CHECK_LINK_FLAG([-fstack-protector-all],
[CFLAGS="$CFLAGS -fstack-protector-all"]
)
])
;;
dragonfly*)
AX_CHECK_COMPILE_FLAG([-fstack-protector], [
AX_CHECK_LINK_FLAG([-fstack-protector],
[CFLAGS="$CFLAGS -fstack-protector"]
)
])
;;
*)
AX_CHECK_COMPILE_FLAG([-fstack-protector-all], [
AX_CHECK_LINK_FLAG([-fstack-protector-all],
[CFLAGS="$CFLAGS -fstack-protector-all"]
)
])
;;
esac
AX_CHECK_COMPILE_FLAG([-Wbounded], [CFLAGS="$CFLAGS -Wbounded"])
AX_CHECK_COMPILE_FLAG([-Winit-self], [CFLAGS="$CFLAGS -Winit-self"])
AX_CHECK_COMPILE_FLAG([-Wwrite-strings], [CFLAGS="$CFLAGS -Wwrite-strings"])
AX_CHECK_COMPILE_FLAG([-Wdiv-by-zero], [CFLAGS="$CFLAGS -Wdiv-by-zero"])
AC_ARG_VAR([CWFLAGS], [define to compilation flags for generating extra warnings])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wall], [CWFLAGS="$CWFLAGS -Wall"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wextra], [CWFLAGS="$CWFLAGS -Wextra"])
AC_MSG_CHECKING(for clang)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[
#ifndef __clang__
be sad
#endif
]])],
[AC_MSG_RESULT(yes)
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wno-unknown-warning-option],
[CWFLAGS="$CWFLAGS -Wno-unknown-warning-option"])],
[AC_MSG_RESULT(no)
])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wbad-function-cast], [CWFLAGS="$CWFLAGS -Wbad-function-cast"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wcast-align], [CWFLAGS="$CWFLAGS -Wcast-align"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wcast-qual], [CWFLAGS="$CWFLAGS -Wcast-qual"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wchar-subscripts], [CWFLAGS="$CWFLAGS -Wchar-subscripts"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wcomment], [CWFLAGS="$CWFLAGS -Wcomment"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wfloat-equal], [CWFLAGS="$CWFLAGS -Wfloat-equal"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wformat=2], [CWFLAGS="$CWFLAGS -Wformat=2"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wimplicit], [CWFLAGS="$CWFLAGS -Wimplicit"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmissing-declarations], [CWFLAGS="$CWFLAGS -Wmissing-declarations"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wmissing-prototypes], [CWFLAGS="$CWFLAGS -Wmissing-prototypes"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wnormalized=id], [CWFLAGS="$CWFLAGS -Wnormalized=id"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Woverride-init], [CWFLAGS="$CWFLAGS -Woverride-init"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wparentheses], [CWFLAGS="$CWFLAGS -Wparentheses"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wpointer-arith], [CWFLAGS="$CWFLAGS -Wpointer-arith"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wredundant-decls], [CWFLAGS="$CWFLAGS -Wredundant-decls"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wstrict-prototypes], [CWFLAGS="$CWFLAGS -Wstrict-prototypes"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wswitch-enum], [CWFLAGS="$CWFLAGS -Wswitch-enum"])
AX_CHECK_COMPILE_FLAG([$CWFLAGS -Wvariable-decl], [CWFLAGS="$CWFLAGS -Wvariable-decl"])
AX_CHECK_LINK_FLAG([-Wl,-z,relro], [LDFLAGS="$LDFLAGS -Wl,-z,relro"])
AX_CHECK_LINK_FLAG([-Wl,-z,now], [LDFLAGS="$LDFLAGS -Wl,-z,now"])
AX_CHECK_LINK_FLAG([-Wl,-z,noexecstack], [LDFLAGS="$LDFLAGS -Wl,-z,noexecstack"])
AC_ARG_VAR([AR], [path to the ar utility])
AC_CHECK_TOOL([AR], [ar], [ar])
AS_IF([test -d /usr/local/include], [
CPPFLAGS="$CPPFLAGS -I/usr/local/include"
])
AS_IF([test -d /usr/local/lib], [
LDFLAGS="$LDFLAGS -L/usr/local/lib"
])
dnl Checks for typedefs, structures, and compiler characteristics.
AC_C_BIGENDIAN
dnl Switches.
AC_ARG_ENABLE(pie,
[AS_HELP_STRING(--enable-pie,Produce position independent executables @<:@default=yes@:>@)],
enable_pie=$enableval, enable_pie="maybe")
AC_ARG_ENABLE(ssp,
[AS_HELP_STRING(--disable-ssp,Don't compile with -fstack-protector)],
[AS_IF([test "x$enableval" = "xno"], [
nxflags=""
for flag in `echo $CFLAGS`; do
case "$flag" in
-fstack-protector*) ;;
*) AS_VAR_APPEND([nxflags], [" $flag"]) ;;
esac
done
CFLAGS="$nxflags"
])
])
AC_ARG_ENABLE(debug,
[AS_HELP_STRING(--enable-debug,For maintainers only - please do not use)],
[
AS_IF([test "x$LX_CFLAGS" = "xNONE"], [
nxflags=""
for flag in `echo $CFLAGS`; do
case "$flag" in
-O*) ;;
-g*) ;;
*) AS_VAR_APPEND([nxflags], [" $flag"]) ;;
esac
done
CFLAGS="$nxflags -O0 -g3"
])
CPPFLAGS="$CPPFLAGS -DDEBUG=1"
])
AC_ARG_WITH(safecode,
[AS_HELP_STRING(--with-safecode,For maintainers only - please do not use)],
[AS_IF([test "x$withval" = "xyes"], [
AC_ARG_VAR([SAFECODE_HOME], [set to the safecode base directory])
: ${SAFECODE_HOME:=/opt/safecode}
LDFLAGS="$LDFLAGS -L${SAFECODE_HOME}/lib"
LIBS="$LIBS -lsc_dbg_rt -lpoolalloc_bitmap -lstdc++"
CFLAGS="$CFLAGS -fmemsafety"
])
])
NACL_HOSTNAME="localhost"
NACL_PATH=libnacl/build/${NACL_HOSTNAME}
AC_SUBST([MAINT])
AC_SUBST([NACL_PATH])
dnl Output.
AH_VERBATIM([NDEBUG], [/* Never ever ignore assertions */
#ifdef NDEBUG
#/**/undef/**/ NDEBUG
#endif])
AC_CONFIG_FILES([Makefile
src/Makefile
src/libnacl/Makefile
src/libnacl/tests/Makefile
src/libnacl/okcompilers/c
src/libnacl/okcompilers/do])
AC_OUTPUT
chmod +x src/libnacl/do
chmod +x src/libnacl/okcompilers/do

View File

@ -0,0 +1,73 @@
# ===========================================================================
# http://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS])
#
# DESCRIPTION
#
# Check whether the given FLAG works with the current language's compiler
# or gives an error. (Warnings, however, are ignored)
#
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
# success/failure.
#
# If EXTRA-FLAGS is defined, it is added to the current language's default
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
# force the compiler to issue an error when a bad flag is given.
#
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
# macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.
#
# LICENSE
#
# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# As a special exception, the respective Autoconf Macro's copyright owner
# gives unlimited permission to copy, distribute and modify the configure
# scripts that are the output of Autoconf when processing the Macro. You
# need not follow the terms of the GNU General Public License when using
# or distributing such scripts, even though portions of the text of the
# Macro appear in them. The GNU General Public License (GPL) does govern
# all other use of the material that constitutes the Autoconf Macro.
#
# This special exception to the GPL applies to versions of the Autoconf
# Macro released by the Autoconf Archive. When you make and distribute a
# modified version of the Autoconf Macro, you may extend this special
# exception to the GPL to apply to your modified version as well.
#serial 2
AC_DEFUN([AX_CHECK_COMPILE_FLAG],
[AC_PREREQ(2.59)dnl for _AC_LANG_PREFIX
AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
_AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]],
[[char x[42U], fodder = 0;if (fodder > -1000 && fgets(x,1000,stdin)) puts(x)]])],
[AS_VAR_SET(CACHEVAR,[yes])],
[AS_VAR_SET(CACHEVAR,[no])])
_AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
AS_IF([test x"AS_VAR_GET(CACHEVAR)" = xyes],
[m4_default([$2], :)],
[m4_default([$3], :)])
AS_VAR_POPDEF([CACHEVAR])dnl
])dnl AX_CHECK_COMPILE_FLAGS

78
m4/ax_check_gnu_make.m4 Normal file
View File

@ -0,0 +1,78 @@
# ===========================================================================
# http://www.gnu.org/software/autoconf-archive/ax_check_gnu_make.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_GNU_MAKE()
#
# DESCRIPTION
#
# This macro searches for a GNU version of make. If a match is found, the
# makefile variable `ifGNUmake' is set to the empty string, otherwise it
# is set to "#". This is useful for including a special features in a
# Makefile, which cannot be handled by other versions of make. The
# variable _cv_gnu_make_command is set to the command to invoke GNU make
# if it exists, the empty string otherwise.
#
# Here is an example of its use:
#
# Makefile.in might contain:
#
# # A failsafe way of putting a dependency rule into a makefile
# $(DEPEND):
# $(CC) -MM $(srcdir)/*.c > $(DEPEND)
#
# @ifGNUmake@ ifeq ($(DEPEND),$(wildcard $(DEPEND)))
# @ifGNUmake@ include $(DEPEND)
# @ifGNUmake@ endif
#
# Then configure.in would normally contain:
#
# AX_CHECK_GNU_MAKE()
# AC_OUTPUT(Makefile)
#
# Then perhaps to cause gnu make to override any other make, we could do
# something like this (note that GNU make always looks for GNUmakefile
# first):
#
# if ! test x$_cv_gnu_make_command = x ; then
# mv Makefile GNUmakefile
# echo .DEFAULT: > Makefile ;
# echo \ $_cv_gnu_make_command \$@ >> Makefile;
# fi
#
# Then, if any (well almost any) other make is called, and GNU make also
# exists, then the other make wraps the GNU make.
#
# LICENSE
#
# Copyright (c) 2008 John Darrington <j.darrington@elvis.murdoch.edu.au>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 7
AC_DEFUN([AX_CHECK_GNU_MAKE], [ AC_CACHE_CHECK( for GNU make,_cv_gnu_make_command,
_cv_gnu_make_command='' ;
dnl Search all the common names for GNU make
for a in "$MAKE" make gmake gnumake ; do
if test -z "$a" ; then continue ; fi ;
if ( sh -c "$a --version" 2> /dev/null | grep GNU 2>&1 > /dev/null ) ; then
_cv_gnu_make_command=$a ;
break;
fi
done ;
) ;
dnl If there was a GNU version, then set @ifGNUmake@ to the empty string, '#' otherwise
if test "x$_cv_gnu_make_command" != "x" ; then
ifGNUmake='' ;
else
ifGNUmake='#' ;
AC_MSG_RESULT("Not found");
fi
AC_SUBST(ifGNUmake)
] )

72
m4/ax_check_link_flag.m4 Normal file
View File

@ -0,0 +1,72 @@
# ===========================================================================
# http://www.gnu.org/software/autoconf-archive/ax_check_link_flag.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_LINK_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS])
#
# DESCRIPTION
#
# Check whether the given FLAG works with the linker or gives an error.
# (Warnings, however, are ignored)
#
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
# success/failure.
#
# If EXTRA-FLAGS is defined, it is added to the linker's default flags
# when the check is done. The check is thus made with the flags: "LDFLAGS
# EXTRA-FLAGS FLAG". This can for example be used to force the linker to
# issue an error when a bad flag is given.
#
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
# macro in sync with AX_CHECK_{PREPROC,COMPILE}_FLAG.
#
# LICENSE
#
# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# As a special exception, the respective Autoconf Macro's copyright owner
# gives unlimited permission to copy, distribute and modify the configure
# scripts that are the output of Autoconf when processing the Macro. You
# need not follow the terms of the GNU General Public License when using
# or distributing such scripts, even though portions of the text of the
# Macro appear in them. The GNU General Public License (GPL) does govern
# all other use of the material that constitutes the Autoconf Macro.
#
# This special exception to the GPL applies to versions of the Autoconf
# Macro released by the Autoconf Archive. When you make and distribute a
# modified version of the Autoconf Macro, you may extend this special
# exception to the GPL to apply to your modified version as well.
#serial 2
AC_DEFUN([AX_CHECK_LINK_FLAG],
[AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_ldflags_$4_$1])dnl
AC_CACHE_CHECK([whether the linker accepts $1], CACHEVAR, [
ax_check_save_flags=$LDFLAGS
LDFLAGS="$LDFLAGS $4 $1"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>]],
[[char x[42U];if (fgets(x,1000,stdin)) puts(x)]])],
[AS_VAR_SET(CACHEVAR,[yes])],
[AS_VAR_SET(CACHEVAR,[no])])
LDFLAGS=$ax_check_save_flags])
AS_IF([test x"AS_VAR_GET(CACHEVAR)" = xyes],
[m4_default([$2], :)],
[m4_default([$3], :)])
AS_VAR_POPDEF([CACHEVAR])dnl
])dnl AX_CHECK_LINK_FLAGS

302
m4/ax_pthread.m4 Normal file
View File

@ -0,0 +1,302 @@
# ===========================================================================
# http://www.gnu.org/software/autoconf-archive/ax_pthread.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
#
# DESCRIPTION
#
# This macro figures out how to build C programs using POSIX threads. It
# sets the PTHREAD_LIBS output variable to the threads library and linker
# flags, and the PTHREAD_CFLAGS output variable to any special C compiler
# flags that are needed. (The user can also force certain compiler
# flags/libs to be tested by setting these environment variables.)
#
# Also sets PTHREAD_CC to any special C compiler that is needed for
# multi-threaded programs (defaults to the value of CC otherwise). (This
# is necessary on AIX to use the special cc_r compiler alias.)
#
# NOTE: You are assumed to not only compile your program with these flags,
# but also link it with them as well. e.g. you should link with
# $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS $LIBS
#
# If you are only building threads programs, you may wish to use these
# variables in your default LIBS, CFLAGS, and CC:
#
# LIBS="$PTHREAD_LIBS $LIBS"
# CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
# CC="$PTHREAD_CC"
#
# In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute constant
# has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to that name
# (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
#
# Also HAVE_PTHREAD_PRIO_INHERIT is defined if pthread is found and the
# PTHREAD_PRIO_INHERIT symbol is defined when compiling with
# PTHREAD_CFLAGS.
#
# ACTION-IF-FOUND is a list of shell commands to run if a threads library
# is found, and ACTION-IF-NOT-FOUND is a list of commands to run it if it
# is not found. If ACTION-IF-FOUND is not specified, the default action
# will define HAVE_PTHREAD.
#
# Please let the authors know if this macro fails on any platform, or if
# you have any other suggestions or comments. This macro was based on work
# by SGJ on autoconf scripts for FFTW (http://www.fftw.org/) (with help
# from M. Frigo), as well as ac_pthread and hb_pthread macros posted by
# Alejandro Forero Cuervo to the autoconf macro repository. We are also
# grateful for the helpful feedback of numerous users.
#
# Updated for Autoconf 2.68 by Daniel Richard G.
#
# LICENSE
#
# Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
# Copyright (c) 2011 Daniel Richard G. <skunk@iSKUNK.ORG>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# As a special exception, the respective Autoconf Macro's copyright owner
# gives unlimited permission to copy, distribute and modify the configure
# scripts that are the output of Autoconf when processing the Macro. You
# need not follow the terms of the GNU General Public License when using
# or distributing such scripts, even though portions of the text of the
# Macro appear in them. The GNU General Public License (GPL) does govern
# all other use of the material that constitutes the Autoconf Macro.
#
# This special exception to the GPL applies to versions of the Autoconf
# Macro released by the Autoconf Archive. When you make and distribute a
# modified version of the Autoconf Macro, you may extend this special
# exception to the GPL to apply to your modified version as well.
#serial 16
AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD])
AC_DEFUN([AX_PTHREAD], [
AC_REQUIRE([AC_CANONICAL_HOST])
AC_LANG_PUSH([C])
ax_pthread_ok=no
# We used to check for pthread.h first, but this fails if pthread.h
# requires special compiler flags (e.g. on True64 or Sequent).
# It gets checked for in the link test anyway.
# First of all, check if the user has set any of the PTHREAD_LIBS,
# etcetera environment variables, and if threads linking works using
# them:
if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
save_LIBS="$LIBS"
LIBS="$PTHREAD_LIBS $LIBS"
AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
AC_TRY_LINK_FUNC(pthread_join, ax_pthread_ok=yes)
AC_MSG_RESULT($ax_pthread_ok)
if test x"$ax_pthread_ok" = xno; then
PTHREAD_LIBS=""
PTHREAD_CFLAGS=""
fi
LIBS="$save_LIBS"
CFLAGS="$save_CFLAGS"
fi
# We must check for the threads library under a number of different
# names; the ordering is very important because some systems
# (e.g. DEC) have both -lpthread and -lpthreads, where one of the
# libraries is broken (non-POSIX).
# Create a list of thread flags to try. Items starting with a "-" are
# C compiler flags, and other items are library names, except for "none"
# which indicates that we try without any flags at all, and "pthread-config"
# which is a program returning the flags for the Pth emulation library.
ax_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
# The ordering *is* (sometimes) important. Some notes on the
# individual items follow:
# pthreads: AIX (must check this before -lpthread)
# none: in case threads are in libc; should be tried before -Kthread and
# other compiler flags to prevent continual compiler warnings
# -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
# -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
# lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
# -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
# -pthreads: Solaris/gcc
# -mthreads: Mingw32/gcc, Lynx/gcc
# -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
# doesn't hurt to check since this sometimes defines pthreads too;
# also defines -D_REENTRANT)
# ... -mt is also the pthreads flag for HP/aCC
# pthread: Linux, etcetera
# --thread-safe: KAI C++
# pthread-config: use pthread-config program (for GNU Pth library)
case "${host_cpu}-${host_os}" in
*solaris*)
# On Solaris (at least, for some versions), libc contains stubbed
# (non-functional) versions of the pthreads routines, so link-based
# tests will erroneously succeed. (We need to link with -pthreads/-mt/
# -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
# a function called by this macro, so we could check for that, but
# who knows whether they'll stub that too in a future libc.) So,
# we'll just look for -pthreads and -lpthread first:
ax_pthread_flags="-pthreads pthread -mt -pthread $ax_pthread_flags"
;;
*-darwin*)
ax_pthread_flags="-pthread $ax_pthread_flags"
;;
esac
if test x"$ax_pthread_ok" = xno; then
for flag in $ax_pthread_flags; do
case $flag in
none)
AC_MSG_CHECKING([whether pthreads work without any flags])
;;
-*)
AC_MSG_CHECKING([whether pthreads work with $flag])
PTHREAD_CFLAGS="$flag"
;;
pthread-config)
AC_CHECK_PROG(ax_pthread_config, pthread-config, yes, no)
if test x"$ax_pthread_config" = xno; then continue; fi
PTHREAD_CFLAGS="`pthread-config --cflags`"
PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
;;
*)
AC_MSG_CHECKING([for the pthreads library -l$flag])
PTHREAD_LIBS="-l$flag"
;;
esac
save_LIBS="$LIBS"
save_CFLAGS="$CFLAGS"
LIBS="$PTHREAD_LIBS $LIBS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
# Check for various functions. We must include pthread.h,
# since some functions may be macros. (On the Sequent, we
# need a special flag -Kthread to make this header compile.)
# We check for pthread_join because it is in -lpthread on IRIX
# while pthread_create is in libc. We check for pthread_attr_init
# due to DEC craziness with -lpthreads. We check for
# pthread_cleanup_push because it is one of the few pthread
# functions on Solaris that doesn't have a non-functional libc stub.
# We try pthread_create on general principles.
AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>
static void routine(void *a) { a = 0; }
static void *start_routine(void *a) { return a; }],
[pthread_t th; pthread_attr_t attr;
pthread_create(&th, 0, start_routine, 0);
pthread_join(th, 0);
pthread_attr_init(&attr);
pthread_cleanup_push(routine, 0);
pthread_cleanup_pop(0) /* ; */])],
[ax_pthread_ok=yes],
[])
LIBS="$save_LIBS"
CFLAGS="$save_CFLAGS"
AC_MSG_RESULT($ax_pthread_ok)
if test "x$ax_pthread_ok" = xyes; then
break;
fi
PTHREAD_LIBS=""
PTHREAD_CFLAGS=""
done
fi
# Various other checks:
if test "x$ax_pthread_ok" = xyes; then
save_LIBS="$LIBS"
LIBS="$PTHREAD_LIBS $LIBS"
save_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
# Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
AC_MSG_CHECKING([for joinable pthread attribute])
attr_name=unknown
for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <pthread.h>],
[int attr = $attr; return attr /* ; */])],
[attr_name=$attr; break],
[])
done
AC_MSG_RESULT($attr_name)
if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
[Define to necessary symbol if this constant
uses a non-standard name on your system.])
fi
AC_MSG_CHECKING([if more special flags are required for pthreads])
flag=no
case "${host_cpu}-${host_os}" in
*-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
*solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
esac
AC_MSG_RESULT(${flag})
if test "x$flag" != xno; then
PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
fi
AC_CACHE_CHECK([for PTHREAD_PRIO_INHERIT],
ax_cv_PTHREAD_PRIO_INHERIT, [
AC_LINK_IFELSE([
AC_LANG_PROGRAM([[#include <pthread.h>]], [[int i = PTHREAD_PRIO_INHERIT;]])],
[ax_cv_PTHREAD_PRIO_INHERIT=yes],
[ax_cv_PTHREAD_PRIO_INHERIT=no])
])
AS_IF([test "x$ax_cv_PTHREAD_PRIO_INHERIT" = "xyes"],
AC_DEFINE([HAVE_PTHREAD_PRIO_INHERIT], 1, [Have PTHREAD_PRIO_INHERIT.]))
LIBS="$save_LIBS"
CFLAGS="$save_CFLAGS"
# More AIX lossage: must compile with xlc_r or cc_r
if test x"$GCC" != xyes; then
AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
else
PTHREAD_CC=$CC
fi
else
PTHREAD_CC="$CC"
fi
AC_SUBST(PTHREAD_LIBS)
AC_SUBST(PTHREAD_CFLAGS)
AC_SUBST(PTHREAD_CC)
# Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
if test x"$ax_pthread_ok" = xyes; then
ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])
:
else
ax_pthread_ok=no
$2
fi
AC_LANG_POP
])dnl AX_PTHREAD

3
src/Makefile.am Normal file
View File

@ -0,0 +1,3 @@
SUBDIRS = \
libnacl

56
src/libnacl/MACROS Normal file
View File

@ -0,0 +1,56 @@
crypto_verify
crypto_verify_BYTES
crypto_core
crypto_core_OUTPUTBYTES
crypto_core_INPUTBYTES
crypto_core_KEYBYTES
crypto_core_CONSTBYTES
crypto_hashblocks
crypto_hashblocks_STATEBYTES
crypto_hashblocks_BLOCKBYTES
crypto_hash
crypto_hash_BYTES
crypto_stream
crypto_stream_xor
crypto_stream_beforenm
crypto_stream_afternm
crypto_stream_xor_afternm
crypto_stream_KEYBYTES
crypto_stream_NONCEBYTES
crypto_stream_BEFORENMBYTES
crypto_onetimeauth
crypto_onetimeauth_verify
crypto_onetimeauth_BYTES
crypto_onetimeauth_KEYBYTES
crypto_auth
crypto_auth_verify
crypto_auth_BYTES
crypto_auth_KEYBYTES
crypto_secretbox
crypto_secretbox_open
crypto_secretbox_KEYBYTES
crypto_secretbox_NONCEBYTES
crypto_secretbox_ZEROBYTES
crypto_secretbox_BOXZEROBYTES
crypto_scalarmult
crypto_scalarmult_base
crypto_scalarmult_BYTES
crypto_scalarmult_SCALARBYTES
crypto_box
crypto_box_open
crypto_box_keypair
crypto_box_beforenm
crypto_box_afternm
crypto_box_open_afternm
crypto_box_PUBLICKEYBYTES
crypto_box_SECRETKEYBYTES
crypto_box_BEFORENMBYTES
crypto_box_NONCEBYTES
crypto_box_ZEROBYTES
crypto_box_BOXZEROBYTES
crypto_sign
crypto_sign_open
crypto_sign_keypair
crypto_sign_BYTES
crypto_sign_PUBLICKEYBYTES
crypto_sign_SECRETKEYBYTES

11
src/libnacl/OPERATIONS Normal file
View File

@ -0,0 +1,11 @@
crypto_verify
crypto_core
crypto_hashblocks
crypto_hash
crypto_stream
crypto_onetimeauth
crypto_auth
crypto_secretbox
crypto_scalarmult
crypto_box
crypto_sign

26
src/libnacl/PROTOTYPES.c Normal file
View File

@ -0,0 +1,26 @@
extern int crypto_verify(const unsigned char *,const unsigned char *);
extern int crypto_core(unsigned char *,const unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_hashblocks(unsigned char *,const unsigned char *,unsigned long long);
extern int crypto_hash(unsigned char *,const unsigned char *,unsigned long long);
extern int crypto_stream(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_stream_xor(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_stream_beforenm(unsigned char *,const unsigned char *);
extern int crypto_stream_afternm(unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_stream_xor_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_onetimeauth(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_onetimeauth_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_auth(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_auth_verify(const unsigned char *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_secretbox(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_secretbox_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_scalarmult(unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_scalarmult_base(unsigned char *,const unsigned char *);
extern int crypto_box(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_box_open(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_box_keypair(unsigned char *,unsigned char *);
extern int crypto_box_beforenm(unsigned char *,const unsigned char *,const unsigned char *);
extern int crypto_box_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_box_open_afternm(unsigned char *,const unsigned char *,unsigned long long,const unsigned char *,const unsigned char *);
extern int crypto_sign(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_sign_open(unsigned char *,unsigned long long *,const unsigned char *,unsigned long long,const unsigned char *);
extern int crypto_sign_keypair(unsigned char *,unsigned char *);

View File

@ -0,0 +1,17 @@
extern std::string crypto_auth(const std::string &,const std::string &);
extern void crypto_auth_verify(const std::string &,const std::string &,const std::string &);
extern std::string crypto_box(const std::string &,const std::string &,const std::string &,const std::string &);
extern std::string crypto_box_open(const std::string &,const std::string &,const std::string &,const std::string &);
extern std::string crypto_box_keypair(std::string *);
extern std::string crypto_hash(const std::string &);
extern std::string crypto_onetimeauth(const std::string &,const std::string &);
extern void crypto_onetimeauth_verify(const std::string &,const std::string &,const std::string &);
extern std::string crypto_scalarmult(const std::string &,const std::string &);
extern std::string crypto_scalarmult_base(const std::string &);
extern std::string crypto_secretbox(const std::string &,const std::string &,const std::string &);
extern std::string crypto_secretbox_open(const std::string &,const std::string &,const std::string &);
extern std::string crypto_stream(size_t,const std::string &,const std::string &);
extern std::string crypto_stream_xor(const std::string &,const std::string &,const std::string &);
extern std::string crypto_sign(const std::string &,const std::string &);
extern std::string crypto_sign_open(const std::string &,const std::string &);
extern std::string crypto_sign_keypair(std::string *);

View File

@ -0,0 +1,64 @@
/*
commandline/nacl-sha256.c version 20080713
D. J. Bernstein
Public domain.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "crypto_hash_sha256.h"
unsigned char *input;
unsigned long long inputalloc;
unsigned long long inputlen;
unsigned char h[crypto_hash_sha256_BYTES];
void h_print(void)
{
int i;
for (i = 0;i < crypto_hash_sha256_BYTES;++i) printf("%02x",255 & (int) h[i]);
printf("\n");
}
int main()
{
struct stat st;
int ch;
if (fstat(0,&st) == 0) {
input = mmap(0,st.st_size,PROT_READ,MAP_SHARED,0,0);
if (input != MAP_FAILED) {
crypto_hash_sha256(h,input,st.st_size);
h_print();
return 0;
}
}
input = 0;
inputalloc = 0;
inputlen = 0;
while ((ch = getchar()) != EOF) {
if (inputlen >= inputalloc) {
void *newinput;
while (inputlen >= inputalloc)
inputalloc = inputalloc * 2 + 1;
if (posix_memalign(&newinput,16,inputalloc) != 0) return 111;
memcpy(newinput,input,inputlen);
free(input);
input = newinput;
}
input[inputlen++] = ch;
}
crypto_hash_sha256(h,input,inputlen);
h_print();
return 0;
}

View File

@ -0,0 +1,64 @@
/*
commandline/nacl-sha512.c version 20080713
D. J. Bernstein
Public domain.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "crypto_hash_sha512.h"
unsigned char *input;
unsigned long long inputalloc;
unsigned long long inputlen;
unsigned char h[crypto_hash_sha512_BYTES];
void h_print(void)
{
int i;
for (i = 0;i < crypto_hash_sha512_BYTES;++i) printf("%02x",255 & (int) h[i]);
printf("\n");
}
int main()
{
struct stat st;
int ch;
if (fstat(0,&st) == 0) {
input = mmap(0,st.st_size,PROT_READ,MAP_SHARED,0,0);
if (input != MAP_FAILED) {
crypto_hash_sha512(h,input,st.st_size);
h_print();
return 0;
}
}
input = 0;
inputalloc = 0;
inputlen = 0;
while ((ch = getchar()) != EOF) {
if (inputlen >= inputalloc) {
void *newinput;
while (inputlen >= inputalloc)
inputalloc = inputalloc * 2 + 1;
if (posix_memalign(&newinput,16,inputalloc) != 0) return 111;
memcpy(newinput,input,inputlen);
free(input);
input = newinput;
}
input[inputlen++] = ch;
}
crypto_hash_sha512(h,input,inputlen);
h_print();
return 0;
}

View File

@ -0,0 +1,80 @@
/*
cpucycles/alpha.c version 20060316
D. J. Bernstein
Public domain.
*/
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
static long long tod(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
static long long rpcc(void)
{
unsigned long long t;
asm volatile("rpcc %0" : "=r"(t));
return t & 0xffffffff;
}
static long long firstrpcc;
static long long firsttod;
static long long lastrpcc;
static long long lasttod;
static double mhz = 0;
static void init(void)
{
firstrpcc = rpcc();
firsttod = tod();
do {
lastrpcc = rpcc();
lasttod = tod();
} while (lasttod - firsttod < 10000);
lastrpcc -= firstrpcc; lastrpcc &= 0xffffffff;
lasttod -= firsttod;
mhz = (double) lastrpcc / (double) lasttod;
}
long long cpucycles_alpha(void)
{
double x;
long long y;
if (!mhz) init();
lastrpcc = rpcc();
lasttod = tod();
lastrpcc -= firstrpcc; lastrpcc &= 0xffffffff;
lasttod -= firsttod;
/* Number of cycles since firstrpcc is lastrpcc + 2^32 y for unknown y. */
/* Number of microseconds since firsttod is lasttod. */
x = (lasttod * mhz - lastrpcc) * 0.00000000023283064365386962890625;
y = x;
while (x > y + 0.5) y += 1;
while (x < y - 0.5) y -= 1;
y *= 4294967296ULL;
lastrpcc += y;
mhz = (double) lastrpcc / (double) lasttod;
return firstrpcc + lastrpcc;
}
long long cpucycles_alpha_persecond(void)
{
if (!mhz) init();
return 1000000.0 * mhz;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles alpha.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_alpha_h
#define CPUCYCLES_alpha_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_alpha(void);
extern long long cpucycles_alpha_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "alpha"
#define cpucycles cpucycles_alpha
#define cpucycles_persecond cpucycles_alpha_persecond
#endif
#endif

View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <sys/types.h>
#include "osfreq.c"
long long cpucycles_amd64cpuinfo(void)
{
unsigned long long result;
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a" (result) :: "%rdx");
return result;
}
long long cpucycles_amd64cpuinfo_persecond(void)
{
return osfreq();
}

View File

@ -0,0 +1,27 @@
/*
cpucycles amd64cpuinfo.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_amd64cpuinfo_h
#define CPUCYCLES_amd64cpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64cpuinfo(void);
extern long long cpucycles_amd64cpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64cpuinfo"
#define cpucycles cpucycles_amd64cpuinfo
#define cpucycles_persecond cpucycles_amd64cpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
long long cpucycles_amd64cpuspeed(void)
{
unsigned long long result;
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a" (result) :: "%rdx");
return result;
}
long long cpucycles_amd64cpuspeed_persecond(void)
{
int oid[2];
int val;
size_t size;
oid[0] = CTL_HW;
oid[1] = HW_CPUSPEED;
size = sizeof val;
if (sysctl(oid,2,&val,&size,0,0) == -1) return 0;
if (size != sizeof val) return 0;
return val * 1000000LL;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles amd64cpuspeed.h version 20090716
Matthew Dempsky
Public domain.
*/
#ifndef CPUCYCLES_amd64cpuspeed_h
#define CPUCYCLES_amd64cpuspeed_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64cpuspeed(void);
extern long long cpucycles_amd64cpuspeed_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64cpuspeed"
#define cpucycles cpucycles_amd64cpuspeed
#define cpucycles_persecond cpucycles_amd64cpuspeed_persecond
#endif
#endif

View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <sys/types.h>
long long cpucycles_amd64tscfreq(void)
{
unsigned long long result;
asm volatile(".byte 15;.byte 49;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a" (result) :: "%rdx");
return result;
}
long long cpucycles_amd64tscfreq_persecond(void)
{
long result = 0;
size_t resultlen = sizeof(long);
sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
return result;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles amd64tscfreq.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_amd64tscfreq_h
#define CPUCYCLES_amd64tscfreq_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_amd64tscfreq(void);
extern long long cpucycles_amd64tscfreq_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "amd64tscfreq"
#define cpucycles cpucycles_amd64tscfreq
#define cpucycles_persecond cpucycles_amd64tscfreq_persecond
#endif
#endif

View File

@ -0,0 +1,83 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <spu_mfcio.h>
static long myround(double u)
{
long result = u;
while (result + 0.5 < u) result += 1;
while (result - 0.5 > u) result -= 1;
return result;
}
static long long microseconds(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
static long long timebase(void)
{
unsigned long long result;
result = -spu_read_decrementer();
return 0xffffffff & result;
}
static double cpufrequency = 0;
static long tbcycles = 0;
static double guesstbcycles(void)
{
long long tb0; long long us0;
long long tb1; long long us1;
tb0 = timebase();
us0 = microseconds();
do {
tb1 = timebase();
us1 = microseconds();
} while (us1 - us0 < 10000 || tb1 - tb0 < 1000);
if (tb1 <= tb0) return 0;
tb1 -= tb0;
us1 -= us0;
return (cpufrequency * 0.000001 * (double) us1) / (double) tb1;
}
static void init(void)
{
int loop;
double guess1;
double guess2;
spu_write_decrementer(0xffffffff);
cpufrequency = 3192000000.0;
for (loop = 0;loop < 100;++loop) {
guess1 = guesstbcycles();
guess2 = guesstbcycles();
tbcycles = myround(guess1);
if (guess1 - tbcycles > 0.1) continue;
if (tbcycles - guess1 > 0.1) continue;
if (guess2 - tbcycles > 0.1) continue;
if (tbcycles - guess2 > 0.1) continue;
return;
}
tbcycles = 0;
}
long long cpucycles_celllinux(void)
{
if (!tbcycles) init();
return timebase() * tbcycles;
}
long long cpucycles_celllinux_persecond(void)
{
if (!tbcycles) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles celllinux.h version 20081201
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_celllinux_h
#define CPUCYCLES_celllinux_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_celllinux(void);
extern long long cpucycles_celllinux_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "celllinux"
#define cpucycles cpucycles_celllinux
#define cpucycles_persecond cpucycles_celllinux_persecond
#endif
#endif

View File

@ -0,0 +1,73 @@
/*
cpucycles/cortex.c version 20101203
D. J. Bernstein
Public domain.
*/
#define SCALE 1
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
static int enabled = 0;
static int prev[3];
static unsigned long long prevcycles = 0;
static int now[3];
static long long cyclespersec = 0;
static void readticks(unsigned int *result)
{
struct timeval t;
unsigned int cc;
if (!enabled) {
asm volatile("mcr p15, 0, %0, c9, c12, 0" :: "r"(17));
asm volatile("mcr p15, 0, %0, c9, c12, 1" :: "r"(0x8000000f));
asm volatile("mcr p15, 0, %0, c9, c12, 3" :: "r"(0x8000000f));
enabled = 1;
}
asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(cc));
gettimeofday(&t,(struct timezone *) 0);
result[0] = cc;
result[1] = t.tv_usec;
result[2] = t.tv_sec;
}
long long cpucycles_cortex(void)
{
unsigned long long delta4;
int deltan;
int deltas;
unsigned long long guesscycles;
readticks(now);
delta4 = (unsigned int) (now[0] - prev[0]); /* unsigned change in number of cycles mod 2^32 */
deltan = now[1] - prev[1]; /* signed change in number of nanoseconds mod 10^9 */
deltas = now[2] - prev[2]; /* signed change in number of seconds */
if ((deltas == 0 && deltan < 200000) || (deltas == 1 && deltan < -800000))
return (prevcycles + delta4) * SCALE;
prev[0] = now[0];
prev[1] = now[1];
prev[2] = now[2];
if ((deltas == 0 && deltan < 300000) || (deltas == 1 && deltan < -700000)) {
// actual number of cycles cannot have increased by 2^32 in <0.3ms
cyclespersec = 1000000 * (unsigned long long) delta4;
cyclespersec /= deltan + 1000000 * (long long) deltas;
} else {
guesscycles = deltas * cyclespersec;
guesscycles += (deltan * cyclespersec) / 1000000;
while (delta4 + 2147483648ULL < guesscycles) delta4 += 4294967296ULL;
/* XXX: could do longer-term extrapolation here */
}
prevcycles += delta4;
return prevcycles * SCALE;
}
long long cpucycles_cortex_persecond(void)
{
while (!cyclespersec) cpucycles_cortex();
return cyclespersec * SCALE;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles cortex.h version 20100912
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_cortex_h
#define CPUCYCLES_cortex_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_cortex(void);
extern long long cpucycles_cortex_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "cortex"
#define cpucycles cpucycles_cortex
#define cpucycles_persecond cpucycles_cortex_persecond
#endif
#endif

View File

@ -0,0 +1,62 @@
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
static int fddev = -1;
static int prev[3];
static unsigned long long prevcycles = 0;
static int now[3];
static long long cyclespersec = 0;
static void readdev(unsigned int *result)
{
if (read(fddev,result,12) == 12) return;
result[0] = result[1] = result[2] = 0;
}
long long cpucycles_dev4ns(void)
{
unsigned long long delta4;
int deltan;
int deltas;
unsigned long long guesscycles;
if (fddev == -1) {
fddev = open("/dev/cpucycles4ns",O_RDONLY);
readdev(prev);
}
readdev(now);
delta4 = (unsigned int) (now[0] - prev[0]); /* unsigned change in number of cycles mod 2^32 */
deltan = now[1] - prev[1]; /* signed change in number of nanoseconds mod 10^9 */
deltas = now[2] - prev[2]; /* signed change in number of seconds */
if ((deltas == 0 && deltan < 200000000) || (deltas == 1 && deltan < -800000000))
return prevcycles + delta4;
prev[0] = now[0];
prev[1] = now[1];
prev[2] = now[2];
if ((deltas == 0 && deltan < 300000000) || (deltas == 1 && deltan < -700000000)) {
// actual number of cycles cannot have increased by 2^32 in <0.3ms
cyclespersec = 1000000000 * (unsigned long long) delta4;
cyclespersec /= deltan + 1000000000 * (long long) deltas;
} else {
guesscycles = deltas * cyclespersec;
guesscycles += (deltan * cyclespersec) / 1000000000;
while (delta4 + 2147483648ULL < guesscycles) delta4 += 4294967296ULL;
/* XXX: could do longer-term extrapolation here */
}
prevcycles += delta4;
return prevcycles;
}
long long cpucycles_dev4ns_persecond(void)
{
while (!cyclespersec) cpucycles_dev4ns();
return cyclespersec;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles dev4ns.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_dev4ns_h
#define CPUCYCLES_dev4ns_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_dev4ns(void);
extern long long cpucycles_dev4ns_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "dev4ns"
#define cpucycles cpucycles_dev4ns
#define cpucycles_persecond cpucycles_dev4ns_persecond
#endif
#endif

44
src/libnacl/cpucycles/do Executable file
View File

@ -0,0 +1,44 @@
#!/bin/sh -e
okabi | (
while read abi
do
rm -f cpucycles.o cpucycles.h
(
echo monotoniccpuinfo
echo monotonic
echo gettimeofday
) | (
while read n
do
okc-$abi | (
while read c
do
echo "=== `date` === Trying $n.c with $c..." >&2
rm -f test cpucycles-impl.o cpucycles-impl.h cpucycles-impl.c
cp $n.c cpucycles-impl.c || continue
cp $n.h cpucycles-impl.h || continue
$c -c cpucycles-impl.c || continue
$c -o test test.c cpucycles-impl.o || continue
echo "=== `date` === Success. Using $n.c." >&2
mkdir -p lib/$abi
mv cpucycles-impl.o lib/$abi/cpucycles.o
mkdir -p include/$abi
mv cpucycles-impl.h include/$abi/cpucycles.h
exit 0
done
exit 111
) && exit 0
done
exit 111
) || (
echo ===== Giving up. >&2
rm -f test cpucycles-impl.o cpucycles-impl.h cpucycles-impl.c
exit 111
) || exit 0
done
exit 0
) || exit 111

View File

@ -0,0 +1,32 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "osfreq.c"
static double cpufrequency = 0;
static void init(void)
{
cpufrequency = osfreq();
}
long long cpucycles_gettimeofday(void)
{
double result;
struct timeval t;
if (!cpufrequency) init();
gettimeofday(&t,(struct timezone *) 0);
result = t.tv_usec;
result *= 0.000001;
result += (double) t.tv_sec;
result *= cpufrequency;
return result;
}
long long cpucycles_gettimeofday_persecond(void)
{
if (!cpufrequency) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles gettimeofday.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_gettimeofday_h
#define CPUCYCLES_gettimeofday_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_gettimeofday(void);
extern long long cpucycles_gettimeofday_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "gettimeofday"
#define cpucycles cpucycles_gettimeofday
#define cpucycles_persecond cpucycles_gettimeofday_persecond
#endif
#endif

View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/pstat.h>
#include <machine/inline.h>
long long cpucycles_hppapstat(void)
{
register long long result;
_MFCTL(16,result);
return result;
}
long long cpucycles_hppapstat_persecond(void)
{
struct pst_processor pst;
union pstun pu;
double result;
pu.pst_processor = &pst;
if (pstat(PSTAT_PROCESSOR,pu,sizeof(pst),1,0) < 0) return 0;
result = pst.psp_iticksperclktick;
result *= (double) sysconf(_SC_CLK_TCK);
return result;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles hppapstat.h version 20060319
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_hppapstat_h
#define CPUCYCLES_hppapstat_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_hppapstat(void);
extern long long cpucycles_hppapstat_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "hppapstat"
#define cpucycles cpucycles_hppapstat
#define cpucycles_persecond cpucycles_hppapstat_persecond
#endif
#endif

View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <sys/types.h>
#include "osfreq.c"
long long cpucycles_ia64cpuinfo(void)
{
long long result;
asm volatile("mov %0=ar.itc" : "=r"(result));
return result;
}
long long cpucycles_ia64cpuinfo_persecond(void)
{
return osfreq();
}

View File

@ -0,0 +1,27 @@
/*
cpucycles ia64cpuinfo.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_ia64cpuinfo_h
#define CPUCYCLES_ia64cpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_ia64cpuinfo(void);
extern long long cpucycles_ia64cpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "ia64cpuinfo"
#define cpucycles cpucycles_ia64cpuinfo
#define cpucycles_persecond cpucycles_ia64cpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,65 @@
/*
cpucycles/mips.c version 20100803
D. J. Bernstein
Public domain.
*/
#define SCALE 2
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
static int prev[3];
static unsigned long long prevcycles = 0;
static int now[3];
static long long cyclespersec = 0;
static void readticks(unsigned int *result)
{
struct timeval t;
unsigned int cc;
asm volatile(".byte 59; .byte 16; .byte 2; .byte 124; move %0,$2" : "=r"(cc) : : "$2");
gettimeofday(&t,(struct timezone *) 0);
result[0] = cc;
result[1] = t.tv_usec;
result[2] = t.tv_sec;
}
long long cpucycles_mips(void)
{
unsigned long long delta4;
int deltan;
int deltas;
unsigned long long guesscycles;
readticks(now);
delta4 = (unsigned int) (now[0] - prev[0]); /* unsigned change in number of cycles mod 2^32 */
deltan = now[1] - prev[1]; /* signed change in number of nanoseconds mod 10^9 */
deltas = now[2] - prev[2]; /* signed change in number of seconds */
if ((deltas == 0 && deltan < 200000) || (deltas == 1 && deltan < -800000))
return (prevcycles + delta4) * SCALE;
prev[0] = now[0];
prev[1] = now[1];
prev[2] = now[2];
if ((deltas == 0 && deltan < 300000) || (deltas == 1 && deltan < -700000)) {
// actual number of cycles cannot have increased by 2^32 in <0.3ms
cyclespersec = 1000000 * (unsigned long long) delta4;
cyclespersec /= deltan + 1000000 * (long long) deltas;
} else {
guesscycles = deltas * cyclespersec;
guesscycles += (deltan * cyclespersec) / 1000000;
while (delta4 + 2147483648ULL < guesscycles) delta4 += 4294967296ULL;
/* XXX: could do longer-term extrapolation here */
}
prevcycles += delta4;
return prevcycles * SCALE;
}
long long cpucycles_mips_persecond(void)
{
while (!cyclespersec) cpucycles_mips();
return cyclespersec * SCALE;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles mips.h version 20100802
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_mips_h
#define CPUCYCLES_mips_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_mips(void);
extern long long cpucycles_mips_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "mips"
#define cpucycles cpucycles_mips
#define cpucycles_persecond cpucycles_mips_persecond
#endif
#endif

View File

@ -0,0 +1,35 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
static double cpufrequency = 0;
static void init(void)
{
long result = 0; size_t resultlen = sizeof(long);
sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
cpufrequency = result;
}
long long cpucycles_monotonic(void)
{
double result;
struct timespec t;
if (!cpufrequency) init();
clock_gettime(CLOCK_MONOTONIC,&t);
result = t.tv_nsec;
result *= 0.000000001;
result += (double) t.tv_sec;
result *= cpufrequency;
return result;
}
long long cpucycles_monotonic_persecond(void)
{
if (!cpufrequency) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles monotonic.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_monotonic_h
#define CPUCYCLES_monotonic_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_monotonic(void);
extern long long cpucycles_monotonic_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "monotonic"
#define cpucycles cpucycles_monotonic
#define cpucycles_persecond cpucycles_monotonic_persecond
#endif
#endif

View File

@ -0,0 +1,34 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include "osfreq.c"
static double cpufrequency = 0;
static void init(void)
{
cpufrequency = osfreq();
}
long long cpucycles_monotoniccpuinfo(void)
{
double result;
struct timespec t;
if (!cpufrequency) init();
clock_gettime(CLOCK_MONOTONIC,&t);
result = t.tv_nsec;
result *= 0.000000001;
result += (double) t.tv_sec;
result *= cpufrequency;
return result;
}
long long cpucycles_monotoniccpuinfo_persecond(void)
{
if (!cpufrequency) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles monotoniccpuinfo.h version 20100804
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_monotoniccpuinfo_h
#define CPUCYCLES_monotoniccpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_monotoniccpuinfo(void);
extern long long cpucycles_monotoniccpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "monotoniccpuinfo"
#define cpucycles cpucycles_monotoniccpuinfo
#define cpucycles_persecond cpucycles_monotoniccpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,65 @@
static double osfreq(void)
{
FILE *f;
double result;
int s;
f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
if (f) {
s = fscanf(f,"%lf",&result);
fclose(f);
if (s > 0) return 1000.0 * result;
}
f = fopen("/sys/devices/system/cpu/cpu0/clock_tick", "r");
if (f) {
s = fscanf(f,"%lf",&result);
fclose(f);
if (s > 0) return result;
}
f = fopen("/proc/cpuinfo","r");
if (f) {
for (;;) {
s = fscanf(f,"cpu MHz : %lf",&result);
if (s > 0) break;
if (s == 0) s = fscanf(f,"%*[^\n]\n");
if (s < 0) { result = 0; break; }
}
fclose(f);
if (result) return 1000000.0 * result;
}
f = fopen("/proc/cpuinfo","r");
if (f) {
for (;;) {
s = fscanf(f,"clock : %lf",&result);
if (s > 0) break;
if (s == 0) s = fscanf(f,"%*[^\n]\n");
if (s < 0) { result = 0; break; }
}
fclose(f);
if (result) return 1000000.0 * result;
}
f = popen("/usr/sbin/lsattr -E -l proc0 -a frequency 2>/dev/null","r");
if (f) {
s = fscanf(f,"frequency %lf",&result);
pclose(f);
if (s > 0) return result;
}
f = popen("/usr/sbin/psrinfo -v 2>/dev/null","r");
if (f) {
for (;;) {
s = fscanf(f," The %*s processor operates at %lf MHz",&result);
if (s > 0) break;
if (s == 0) s = fscanf(f,"%*[^\n]\n");
if (s < 0) { result = 0; break; }
}
pclose(f);
if (result) return 1000000.0 * result;
}
return 0;
}

View File

@ -0,0 +1,95 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "osfreq.c"
static long myround(double u)
{
long result = u;
while (result + 0.5 < u) result += 1;
while (result - 0.5 > u) result -= 1;
return result;
}
static long long microseconds(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
static int tbshift = 0;
static long long timebase(void)
{
unsigned long high;
unsigned long low;
unsigned long newhigh;
unsigned long long result;
asm volatile(
"7:mftbu %0;mftb %1;mftbu %2;cmpw %0,%2;bne 7b"
: "=r" (high), "=r" (low), "=r" (newhigh)
);
result = high;
result <<= 32;
result |= low;
return result >> tbshift;
}
static double cpufrequency = 0;
static long tbcycles = 0;
static double guesstbcycles(void)
{
long long tb0; long long us0;
long long tb1; long long us1;
tb0 = timebase();
us0 = microseconds();
do {
tb1 = timebase();
us1 = microseconds();
} while (us1 - us0 < 10000 || tb1 - tb0 < 1000);
if (tb1 <= tb0) return 0;
tb1 -= tb0;
us1 -= us0;
return (cpufrequency * 0.000001 * (double) us1) / (double) tb1;
}
static void init(void)
{
int loop;
double guess1;
double guess2;
cpufrequency = osfreq();
if (!cpufrequency) return;
for (tbshift = 0;tbshift < 10;++tbshift) {
for (loop = 0;loop < 100;++loop) {
guess1 = guesstbcycles();
guess2 = guesstbcycles();
tbcycles = myround(guess1);
if (guess1 - tbcycles > 0.1) continue;
if (tbcycles - guess1 > 0.1) continue;
if (guess2 - tbcycles > 0.1) continue;
if (tbcycles - guess2 > 0.1) continue;
return;
}
}
tbcycles = 0;
}
long long cpucycles_powerpccpuinfo(void)
{
if (!tbcycles) init();
return timebase() * tbcycles;
}
long long cpucycles_powerpccpuinfo_persecond(void)
{
if (!tbcycles) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles powerpccpuinfo.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_powerpccpuinfo_h
#define CPUCYCLES_powerpccpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_powerpccpuinfo(void);
extern long long cpucycles_powerpccpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "powerpccpuinfo"
#define cpucycles cpucycles_powerpccpuinfo
#define cpucycles_persecond cpucycles_powerpccpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,43 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <mach/mach_time.h>
#define timebase mach_absolute_time
static int cpumib[2] = { CTL_HW, HW_CPU_FREQ } ;
static int tbmib[2] = { CTL_HW, HW_TB_FREQ } ;
static long myround(double u)
{
long result = u;
while (result + 0.5 < u) result += 1;
while (result - 0.5 > u) result -= 1;
return result;
}
static long tbcycles = 0;
static void init(void)
{
unsigned int cpufrequency = 0; size_t cpufrequencylen = sizeof(unsigned int);
unsigned int tbfrequency = 0; size_t tbfrequencylen = sizeof(unsigned int);
sysctl(cpumib,2,&cpufrequency,&cpufrequencylen,0,0);
sysctl(tbmib,2,&tbfrequency,&tbfrequencylen,0,0);
if (tbfrequency > 0)
tbcycles = myround((double) (unsigned long long) cpufrequency
/ (double) (unsigned long long) tbfrequency);
}
long long cpucycles_powerpcmacos(void)
{
if (!tbcycles) init();
return timebase() * tbcycles;
}
long long cpucycles_powerpcmacos_persecond(void)
{
unsigned int result = 0; size_t resultlen = sizeof(unsigned int);
sysctl(cpumib,2,&result,&resultlen,0,0);
return (unsigned long long) result;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles powerpcmacos.h version 20060319
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_powerpcmacos_h
#define CPUCYCLES_powerpcmacos_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_powerpcmacos(void);
extern long long cpucycles_powerpcmacos_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "powerpcmacos"
#define cpucycles cpucycles_powerpcmacos
#define cpucycles_persecond cpucycles_powerpcmacos_persecond
#endif
#endif

View File

@ -0,0 +1,39 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
static double cpufrequency = 0;
static void init(void)
{
FILE *f;
f = popen("hinv -c processor | awk '{if ($3==\"MHZ\") print $2*1000000}'","r");
if (!f) return;
if (fscanf(f,"%lf",&cpufrequency) < 1) cpufrequency = 0;
pclose(f);
if (!cpufrequency) return;
}
long long cpucycles_sgi(void)
{
double result;
struct timespec t;
if (!cpufrequency) init();
clock_gettime(CLOCK_SGI_CYCLE,&t);
result = t.tv_nsec;
result *= 0.000000001;
result += (double) t.tv_sec;
result *= cpufrequency;
return result;
}
long long cpucycles_sgi_persecond(void)
{
if (!cpufrequency) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles sgi.h version 20070916
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_sgi_h
#define CPUCYCLES_sgi_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_sgi(void);
extern long long cpucycles_sgi_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "sgi"
#define cpucycles cpucycles_sgi
#define cpucycles_persecond cpucycles_sgi_persecond
#endif
#endif

View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <sys/types.h>
#include "osfreq.c"
long long cpucycles_sparc32cpuinfo(void)
{
long long result;
asm volatile(".word 2202075136; .word 2570088480; srl %%g1,0,%L0; mov %%o4,%H0"
: "=r" (result) : : "g1","o4");
return result;
}
long long cpucycles_sparc32cpuinfo_persecond(void)
{
return osfreq();
}

View File

@ -0,0 +1,27 @@
/*
cpucycles sparc32cpuinfo.h version 20100804
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_sparc32cpuinfo_h
#define CPUCYCLES_sparc32cpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_sparc32cpuinfo(void);
extern long long cpucycles_sparc32cpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "sparc32cpuinfo"
#define cpucycles cpucycles_sparc32cpuinfo
#define cpucycles_persecond cpucycles_sparc32cpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <sys/types.h>
#include "osfreq.c"
long long cpucycles_sparccpuinfo(void)
{
long long result;
asm volatile("rd %%tick,%0" : "=r" (result));
return result;
}
long long cpucycles_sparccpuinfo_persecond(void)
{
return osfreq();
}

View File

@ -0,0 +1,27 @@
/*
cpucycles sparccpuinfo.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_sparccpuinfo_h
#define CPUCYCLES_sparccpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_sparccpuinfo(void);
extern long long cpucycles_sparccpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "sparccpuinfo"
#define cpucycles cpucycles_sparccpuinfo
#define cpucycles_persecond cpucycles_sparccpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,27 @@
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include "cpucycles-impl.h"
static long long tod(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
long long todstart;
long long todend;
long long cpustart;
long long cpuend;
long long cyclespersecond;
long long cyclespertod;
long long t[1001];
int main()
{
return 0;
}

View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include <sys/types.h>
#include "osfreq.c"
long long cpucycles_x86cpuinfo(void)
{
long long result;
asm volatile(".byte 15;.byte 49" : "=A" (result));
return result;
}
long long cpucycles_x86cpuinfo_persecond(void)
{
return osfreq();
}

View File

@ -0,0 +1,27 @@
/*
cpucycles x86cpuinfo.h version 20100803
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_x86cpuinfo_h
#define CPUCYCLES_x86cpuinfo_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_x86cpuinfo(void);
extern long long cpucycles_x86cpuinfo_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "x86cpuinfo"
#define cpucycles cpucycles_x86cpuinfo
#define cpucycles_persecond cpucycles_x86cpuinfo_persecond
#endif
#endif

View File

@ -0,0 +1,24 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
long long cpucycles_x86cpuspeed(void)
{
long long result;
asm volatile(".byte 15;.byte 49" : "=A" (result));
return result;
}
long long cpucycles_x86cpuspeed_persecond(void)
{
int oid[2];
int val;
size_t size;
oid[0] = CTL_HW;
oid[1] = HW_CPUSPEED;
size = sizeof val;
if (sysctl(oid,2,&val,&size,0,0) == -1) return 0;
if (size != sizeof val) return 0;
return val * 1000000LL;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles x86cpuspeed.h version 20090716
Matthew Dempsky
Public domain.
*/
#ifndef CPUCYCLES_x86cpuspeed_h
#define CPUCYCLES_x86cpuspeed_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_x86cpuspeed(void);
extern long long cpucycles_x86cpuspeed_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "x86cpuspeed"
#define cpucycles cpucycles_x86cpuspeed
#define cpucycles_persecond cpucycles_x86cpuspeed_persecond
#endif
#endif

View File

@ -0,0 +1,60 @@
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
long long cpucycles_x86estimate(void)
{
long long result;
asm volatile(".byte 15;.byte 49" : "=A" (result));
return result;
}
static long long microseconds(void)
{
struct timeval t;
gettimeofday(&t,(struct timezone *) 0);
return t.tv_sec * (long long) 1000000 + t.tv_usec;
}
static double guessfreq(void)
{
long long tb0; long long us0;
long long tb1; long long us1;
tb0 = cpucycles_x86estimate();
us0 = microseconds();
do {
tb1 = cpucycles_x86estimate();
us1 = microseconds();
} while (us1 - us0 < 10000 || tb1 - tb0 < 1000);
if (tb1 <= tb0) return 0;
tb1 -= tb0;
us1 -= us0;
return ((double) tb1) / (0.000001 * (double) us1);
}
static double cpufrequency = 0;
static void init(void)
{
double guess1;
double guess2;
int loop;
for (loop = 0;loop < 100;++loop) {
guess1 = guessfreq();
guess2 = guessfreq();
if (guess1 > 1.01 * guess2) continue;
if (guess2 > 1.01 * guess1) continue;
cpufrequency = 0.5 * (guess1 + guess2);
break;
}
}
long long cpucycles_x86estimate_persecond(void)
{
if (!cpufrequency) init();
return cpufrequency;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles x86estimate.h version 20070121
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_x86estimate_h
#define CPUCYCLES_x86estimate_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_x86estimate(void);
extern long long cpucycles_x86estimate_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "x86estimate"
#define cpucycles cpucycles_x86estimate
#define cpucycles_persecond cpucycles_x86estimate_persecond
#endif
#endif

View File

@ -0,0 +1,18 @@
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
long long cpucycles_x86tscfreq(void)
{
long long result;
asm volatile(".byte 15;.byte 49" : "=A" (result));
return result;
}
long long cpucycles_x86tscfreq_persecond(void)
{
long result = 0;
size_t resultlen = sizeof(long);
sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
return result;
}

View File

@ -0,0 +1,27 @@
/*
cpucycles x86tscfreq.h version 20060318
D. J. Bernstein
Public domain.
*/
#ifndef CPUCYCLES_x86tscfreq_h
#define CPUCYCLES_x86tscfreq_h
#ifdef __cplusplus
extern "C" {
#endif
extern long long cpucycles_x86tscfreq(void);
extern long long cpucycles_x86tscfreq_persecond(void);
#ifdef __cplusplus
}
#endif
#ifndef cpucycles_implementation
#define cpucycles_implementation "x86tscfreq"
#define cpucycles cpucycles_x86tscfreq
#define cpucycles_persecond cpucycles_x86tscfreq_persecond
#endif
#endif

View File

@ -0,0 +1,16 @@
#include <stdio.h>
int main()
{
char ch;
int loop = 0;
while (scanf("%c",&ch) == 1) {
printf("0x%02x,",255 & (int) ch);
if (++loop == 16) {
loop = 0;
printf("\n");
}
}
printf("0x00\n");
return 0;
}

41
src/libnacl/cpuid/cpuid.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void nope()
{
exit(1);
}
int main()
{
unsigned long x[4];
unsigned long y[4];
int i;
int j;
char c;
signal(SIGILL,nope);
x[0] = 0;
x[1] = 0;
x[2] = 0;
x[3] = 0;
asm volatile(".byte 15;.byte 162" : "=a"(x[0]),"=b"(x[1]),"=c"(x[3]),"=d"(x[2]) : "0"(0) );
if (!x[0]) return 0;
asm volatile(".byte 15;.byte 162" : "=a"(y[0]),"=b"(y[1]),"=c"(y[2]),"=d"(y[3]) : "0"(1) );
for (i = 1;i < 4;++i)
for (j = 0;j < 4;++j) {
c = x[i] >> (8 * j);
if (c < 32) c = 32;
if (c > 126) c = 126;
putchar(c);
}
printf("-%08x-%08x\n",y[0],y[3]);
return 0;
}

37
src/libnacl/cpuid/do Executable file
View File

@ -0,0 +1,37 @@
#!/bin/sh -e
mkdir include
(
echo x86
echo unknown
) | (
while read n
do
okabi | (
while read abi
do
okc-$abi | (
while read c
do
echo "=== `date` === Trying $n.c with $c..." >&2
rm -f cpuid.c
cp $n.c cpuid.c || continue
$c -o cpuid cpuid.c || continue
$c -o cbytes cbytes.c || continue
./cpuid > cpuid.out || continue
echo 'static const char cpuid[] = {' > cpuid.h || continue
./cbytes < cpuid.out >> cpuid.h || continue
echo '} ;' >> cpuid.h || continue
cp cpuid.h include/cpuid.h || continue
cat cpuid.out
exit 0
done
exit 111
) && exit 0
done
exit 111
) && exit 0
done
exit 111
)

View File

@ -0,0 +1,7 @@
#include <stdio.h>
main()
{
printf("unknown CPU ID\n");
return 0;
}

41
src/libnacl/cpuid/x86.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void nope()
{
exit(1);
}
int main()
{
unsigned long x[4];
unsigned long y[4];
int i;
int j;
char c;
signal(SIGILL,nope);
x[0] = 0;
x[1] = 0;
x[2] = 0;
x[3] = 0;
asm volatile(".byte 15;.byte 162" : "=a"(x[0]),"=b"(x[1]),"=c"(x[3]),"=d"(x[2]) : "0"(0) );
if (!x[0]) return 0;
asm volatile(".byte 15;.byte 162" : "=a"(y[0]),"=b"(y[1]),"=c"(y[2]),"=d"(y[3]) : "0"(1) );
for (i = 1;i < 4;++i)
for (j = 0;j < 4;++j) {
c = x[i] >> (8 * j);
if (c < 32) c = 32;
if (c > 126) c = 126;
putchar(c);
}
printf("-%08x-%08x\n",(unsigned int) y[0],(unsigned int) y[3]);
return 0;
}

View File

@ -0,0 +1 @@
3bd7abd4f4dce04396f2ac7cb1cff70607f692411c49a1563b037d31e1662632

View File

@ -0,0 +1,2 @@
#define CRYPTO_BYTES 32
#define CRYPTO_KEYBYTES 32

View File

@ -0,0 +1,83 @@
/*
* 20080913
* D. J. Bernstein
* Public domain.
* */
#include "crypto_hashblocks_sha256.h"
#include "crypto_auth.h"
#define blocks crypto_hashblocks_sha256
typedef unsigned int uint32;
static const char iv[32] = {
0x6a,0x09,0xe6,0x67,
0xbb,0x67,0xae,0x85,
0x3c,0x6e,0xf3,0x72,
0xa5,0x4f,0xf5,0x3a,
0x51,0x0e,0x52,0x7f,
0x9b,0x05,0x68,0x8c,
0x1f,0x83,0xd9,0xab,
0x5b,0xe0,0xcd,0x19,
} ;
int crypto_auth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
{
unsigned char h[32];
unsigned char padded[128];
int i;
unsigned long long bits = 512 + (inlen << 3);
for (i = 0;i < 32;++i) h[i] = iv[i];
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x36;
for (i = 32;i < 64;++i) padded[i] = 0x36;
blocks(h,padded,64);
blocks(h,in,inlen);
in += inlen;
inlen &= 63;
in -= inlen;
for (i = 0;i < inlen;++i) padded[i] = in[i];
padded[inlen] = 0x80;
if (inlen < 56) {
for (i = inlen + 1;i < 56;++i) padded[i] = 0;
padded[56] = bits >> 56;
padded[57] = bits >> 48;
padded[58] = bits >> 40;
padded[59] = bits >> 32;
padded[60] = bits >> 24;
padded[61] = bits >> 16;
padded[62] = bits >> 8;
padded[63] = bits;
blocks(h,padded,64);
} else {
for (i = inlen + 1;i < 120;++i) padded[i] = 0;
padded[120] = bits >> 56;
padded[121] = bits >> 48;
padded[122] = bits >> 40;
padded[123] = bits >> 32;
padded[124] = bits >> 24;
padded[125] = bits >> 16;
padded[126] = bits >> 8;
padded[127] = bits;
blocks(h,padded,128);
}
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x5c;
for (i = 32;i < 64;++i) padded[i] = 0x5c;
for (i = 0;i < 32;++i) padded[64 + i] = h[i];
for (i = 0;i < 32;++i) out[i] = iv[i];
for (i = 32;i < 64;++i) padded[64 + i] = 0;
padded[64 + 32] = 0x80;
padded[64 + 62] = 3;
blocks(out,padded,128);
return 0;
}

View File

@ -0,0 +1,9 @@
#include "crypto_verify_32.h"
#include "crypto_auth.h"
int crypto_auth_verify(const unsigned char *h,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
{
unsigned char correct[32];
crypto_auth(correct,in,inlen,k);
return crypto_verify_32(h,correct);
}

View File

View File

@ -0,0 +1 @@
2f5e8a6a0cac012d8d001351d7d583e69f91390df46305c3608e0c2893491886

View File

@ -0,0 +1,2 @@
#define CRYPTO_BYTES 32
#define CRYPTO_KEYBYTES 32

View File

@ -0,0 +1,86 @@
/*
* 20080913
* D. J. Bernstein
* Public domain.
* */
#include "crypto_hashblocks_sha512.h"
#include "crypto_auth.h"
#define blocks crypto_hashblocks_sha512
typedef unsigned long long uint64;
static const unsigned char iv[64] = {
0x6a,0x09,0xe6,0x67,0xf3,0xbc,0xc9,0x08,
0xbb,0x67,0xae,0x85,0x84,0xca,0xa7,0x3b,
0x3c,0x6e,0xf3,0x72,0xfe,0x94,0xf8,0x2b,
0xa5,0x4f,0xf5,0x3a,0x5f,0x1d,0x36,0xf1,
0x51,0x0e,0x52,0x7f,0xad,0xe6,0x82,0xd1,
0x9b,0x05,0x68,0x8c,0x2b,0x3e,0x6c,0x1f,
0x1f,0x83,0xd9,0xab,0xfb,0x41,0xbd,0x6b,
0x5b,0xe0,0xcd,0x19,0x13,0x7e,0x21,0x79
} ;
int crypto_auth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
{
unsigned char h[64];
unsigned char padded[256];
int i;
unsigned long long bytes = 128 + inlen;
for (i = 0;i < 64;++i) h[i] = iv[i];
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x36;
for (i = 32;i < 128;++i) padded[i] = 0x36;
blocks(h,padded,128);
blocks(h,in,inlen);
in += inlen;
inlen &= 127;
in -= inlen;
for (i = 0;i < inlen;++i) padded[i] = in[i];
padded[inlen] = 0x80;
if (inlen < 112) {
for (i = inlen + 1;i < 119;++i) padded[i] = 0;
padded[119] = bytes >> 61;
padded[120] = bytes >> 53;
padded[121] = bytes >> 45;
padded[122] = bytes >> 37;
padded[123] = bytes >> 29;
padded[124] = bytes >> 21;
padded[125] = bytes >> 13;
padded[126] = bytes >> 5;
padded[127] = bytes << 3;
blocks(h,padded,128);
} else {
for (i = inlen + 1;i < 247;++i) padded[i] = 0;
padded[247] = bytes >> 61;
padded[248] = bytes >> 53;
padded[249] = bytes >> 45;
padded[250] = bytes >> 37;
padded[251] = bytes >> 29;
padded[252] = bytes >> 21;
padded[253] = bytes >> 13;
padded[254] = bytes >> 5;
padded[255] = bytes << 3;
blocks(h,padded,256);
}
for (i = 0;i < 32;++i) padded[i] = k[i] ^ 0x5c;
for (i = 32;i < 128;++i) padded[i] = 0x5c;
for (i = 0;i < 64;++i) padded[128 + i] = h[i];
for (i = 0;i < 64;++i) h[i] = iv[i];
for (i = 64;i < 128;++i) padded[128 + i] = 0;
padded[128 + 64] = 0x80;
padded[128 + 126] = 6;
blocks(h,padded,256);
for (i = 0;i < 32;++i) out[i] = h[i];
return 0;
}

View File

@ -0,0 +1,9 @@
#include "crypto_verify_32.h"
#include "crypto_auth.h"
int crypto_auth_verify(const unsigned char *h,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
{
unsigned char correct[32];
crypto_auth(correct,in,inlen,k);
return crypto_verify_32(h,correct);
}

View File

@ -0,0 +1,69 @@
#include "crypto_auth.h"
#include "randombytes.h"
#include "cpucycles.h"
extern void printentry(long long,const char *,long long *,long long);
extern unsigned char *alignedcalloc(unsigned long long);
extern const char *primitiveimplementation;
extern const char *implementationversion;
extern const char *sizenames[];
extern const long long sizes[];
extern void allocate(void);
extern void measure(void);
const char *primitiveimplementation = crypto_auth_IMPLEMENTATION;
const char *implementationversion = crypto_auth_VERSION;
const char *sizenames[] = { "outputbytes", "keybytes", 0 };
const long long sizes[] = { crypto_auth_BYTES, crypto_auth_KEYBYTES };
#define MAXTEST_BYTES 4096
#ifdef SUPERCOP
#define MGAP 8192
#else
#define MGAP 8
#endif
static unsigned char *k;
static unsigned char *m;
static unsigned char *h;
void preallocate(void)
{
}
void allocate(void)
{
k = alignedcalloc(crypto_auth_KEYBYTES);
m = alignedcalloc(MAXTEST_BYTES);
h = alignedcalloc(crypto_auth_BYTES);
}
#define TIMINGS 15
static long long cycles[TIMINGS + 1];
void measure(void)
{
int i;
int loop;
int mlen;
for (loop = 0;loop < LOOPS;++loop) {
for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / MGAP) {
randombytes(k,crypto_auth_KEYBYTES);
randombytes(m,mlen);
randombytes(h,crypto_auth_BYTES);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_auth(h,m,mlen,k);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_auth_verify(h,m,mlen,k);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"verify_cycles",cycles,TIMINGS);
}
}
}

View File

@ -0,0 +1,121 @@
/*
* crypto_auth/try.c version 20090118
* D. J. Bernstein
* Public domain.
*/
#include <stdlib.h>
#include "crypto_hash_sha256.h"
#include "crypto_auth.h"
#include "windows/windows-quirks.h"
extern unsigned char *alignedcalloc(unsigned long long);
const char *primitiveimplementation = crypto_auth_IMPLEMENTATION;
#define MAXTEST_BYTES 10000
#define CHECKSUM_BYTES 4096
#define TUNE_BYTES 1536
static unsigned char *h;
static unsigned char *m;
static unsigned char *k;
static unsigned char *h2;
static unsigned char *m2;
static unsigned char *k2;
void preallocate(void)
{
}
void allocate(void)
{
h = alignedcalloc(crypto_auth_BYTES);
m = alignedcalloc(MAXTEST_BYTES);
k = alignedcalloc(crypto_auth_KEYBYTES);
h2 = alignedcalloc(crypto_auth_BYTES);
m2 = alignedcalloc(MAXTEST_BYTES + crypto_auth_BYTES);
k2 = alignedcalloc(crypto_auth_KEYBYTES + crypto_auth_BYTES);
}
void predoit(void)
{
}
void doit(void)
{
crypto_auth(h,m,TUNE_BYTES,k);
crypto_auth_verify(h,m,TUNE_BYTES,k);
}
char checksum[crypto_auth_BYTES * 2 + 1];
const char *checksum_compute(void)
{
long long i;
long long j;
for (i = 0;i < CHECKSUM_BYTES;++i) {
long long mlen = i;
long long klen = crypto_auth_KEYBYTES;
long long hlen = crypto_auth_BYTES;
for (j = -16;j < 0;++j) h[j] = random();
for (j = -16;j < 0;++j) k[j] = random();
for (j = -16;j < 0;++j) m[j] = random();
for (j = hlen;j < hlen + 16;++j) h[j] = random();
for (j = klen;j < klen + 16;++j) k[j] = random();
for (j = mlen;j < mlen + 16;++j) m[j] = random();
for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
for (j = -16;j < klen + 16;++j) k2[j] = k[j];
for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
if (crypto_auth(h,m,mlen,k) != 0) return "crypto_auth returns nonzero";
for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_auth overwrites k";
for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_auth overwrites m";
for (j = -16;j < 0;++j) if (h[j] != h2[j]) return "crypto_auth writes before output";
for (j = hlen;j < hlen + 16;++j) if (h[j] != h2[j]) return "crypto_auth writes after output";
for (j = -16;j < 0;++j) h[j] = random();
for (j = -16;j < 0;++j) k[j] = random();
for (j = -16;j < 0;++j) m[j] = random();
for (j = hlen;j < hlen + 16;++j) h[j] = random();
for (j = klen;j < klen + 16;++j) k[j] = random();
for (j = mlen;j < mlen + 16;++j) m[j] = random();
for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
for (j = -16;j < klen + 16;++j) k2[j] = k[j];
for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
if (crypto_auth(m2,m2,mlen,k) != 0) return "crypto_auth returns nonzero";
for (j = 0;j < hlen;++j) if (m2[j] != h[j]) return "crypto_auth does not handle m overlap";
for (j = 0;j < hlen;++j) m2[j] = m[j];
if (crypto_auth(k2,m2,mlen,k2) != 0) return "crypto_auth returns nonzero";
for (j = 0;j < hlen;++j) if (k2[j] != h[j]) return "crypto_auth does not handle k overlap";
for (j = 0;j < hlen;++j) k2[j] = k[j];
if (crypto_auth_verify(h,m,mlen,k) != 0) return "crypto_auth_verify returns nonzero";
for (j = -16;j < hlen + 16;++j) if (h[j] != h2[j]) return "crypto_auth overwrites h";
for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_auth overwrites k";
for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_auth overwrites m";
crypto_hash_sha256(h2,h,hlen);
for (j = 0;j < klen;++j) k[j] ^= h2[j % 32];
if (crypto_auth(h,m,mlen,k) != 0) return "crypto_auth returns nonzero";
if (crypto_auth_verify(h,m,mlen,k) != 0) return "crypto_auth_verify returns nonzero";
crypto_hash_sha256(h2,h,hlen);
for (j = 0;j < mlen;++j) m[j] ^= h2[j % 32];
m[mlen] = h2[0];
}
if (crypto_auth(h,m,CHECKSUM_BYTES,k) != 0) return "crypto_auth returns nonzero";
if (crypto_auth_verify(h,m,CHECKSUM_BYTES,k) != 0) return "crypto_auth_verify returns nonzero";
for (i = 0;i < crypto_auth_BYTES;++i) {
checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
}
checksum[2 * i] = 0;
return 0;
}

View File

@ -0,0 +1,11 @@
#include <string>
using std::string;
#include "crypto_auth.h"
string crypto_auth(const string &m,const string &k)
{
if (k.size() != crypto_auth_KEYBYTES) throw "incorrect key length";
unsigned char a[crypto_auth_BYTES];
crypto_auth(a,(const unsigned char *) m.c_str(),m.size(),(const unsigned char *) k.c_str());
return string((char *) a,crypto_auth_BYTES);
}

View File

@ -0,0 +1,14 @@
#include <string>
using std::string;
#include "crypto_auth.h"
void crypto_auth_verify(const string &a,const string &m,const string &k)
{
if (k.size() != crypto_auth_KEYBYTES) throw "incorrect key length";
if (a.size() != crypto_auth_BYTES) throw "incorrect authenticator length";
if (crypto_auth_verify(
(const unsigned char *) a.c_str(),
(const unsigned char *) m.c_str(),m.size(),
(const unsigned char *) k.c_str()) == 0) return;
throw "invalid authenticator";
}

View File

@ -0,0 +1 @@
5fac7400caabc14a99c5c0bc13fb1df5e468e870382a3a1c

View File

@ -0,0 +1,22 @@
#include "crypto_secretbox_xsalsa20poly1305.h"
#include "crypto_box.h"
int crypto_box_afternm(
unsigned char *c,
const unsigned char *m,unsigned long long mlen,
const unsigned char *n,
const unsigned char *k
)
{
return crypto_secretbox_xsalsa20poly1305(c,m,mlen,n,k);
}
int crypto_box_open_afternm(
unsigned char *m,
const unsigned char *c,unsigned long long clen,
const unsigned char *n,
const unsigned char *k
)
{
return crypto_secretbox_xsalsa20poly1305_open(m,c,clen,n,k);
}

View File

@ -0,0 +1,6 @@
#define CRYPTO_PUBLICKEYBYTES 32
#define CRYPTO_SECRETKEYBYTES 32
#define CRYPTO_BEFORENMBYTES 32
#define CRYPTO_NONCEBYTES 24
#define CRYPTO_ZEROBYTES 32
#define CRYPTO_BOXZEROBYTES 16

View File

@ -0,0 +1,17 @@
#include "crypto_core_hsalsa20.h"
#include "crypto_scalarmult_curve25519.h"
#include "crypto_box.h"
static const unsigned char sigma[16] = "expand 32-byte k";
static const unsigned char n[16] = {0};
int crypto_box_beforenm(
unsigned char *k,
const unsigned char *pk,
const unsigned char *sk
)
{
unsigned char s[32];
crypto_scalarmult_curve25519(s,sk,pk);
return crypto_core_hsalsa20(k,n,s,sigma);
}

View File

@ -0,0 +1,27 @@
#include "crypto_box.h"
int crypto_box(
unsigned char *c,
const unsigned char *m,unsigned long long mlen,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk
)
{
unsigned char k[crypto_box_BEFORENMBYTES];
crypto_box_beforenm(k,pk,sk);
return crypto_box_afternm(c,m,mlen,n,k);
}
int crypto_box_open(
unsigned char *m,
const unsigned char *c,unsigned long long clen,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk
)
{
unsigned char k[crypto_box_BEFORENMBYTES];
crypto_box_beforenm(k,pk,sk);
return crypto_box_open_afternm(m,c,clen,n,k);
}

View File

@ -0,0 +1,12 @@
#include "crypto_scalarmult_curve25519.h"
#include "crypto_box.h"
#include "randombytes.h"
int crypto_box_keypair(
unsigned char *pk,
unsigned char *sk
)
{
randombytes(sk,32);
return crypto_scalarmult_curve25519_base(pk,sk);
}

View File

@ -0,0 +1,137 @@
#include <stdlib.h>
#include "randombytes.h"
#include "cpucycles.h"
#include "crypto_box.h"
extern void printentry(long long,const char *,long long *,long long);
extern unsigned char *alignedcalloc(unsigned long long);
extern const char *primitiveimplementation;
extern const char *implementationversion;
extern const char *sizenames[];
extern const long long sizes[];
extern void allocate(void);
extern void measure(void);
const char *primitiveimplementation = crypto_box_IMPLEMENTATION;
const char *implementationversion = crypto_box_VERSION;
const char *sizenames[] = { "publickeybytes", "secretkeybytes", "beforenmbytes", "noncebytes", "zerobytes", "boxzerobytes", 0 };
const long long sizes[] = { crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES, crypto_box_BEFORENMBYTES, crypto_box_NONCEBYTES, crypto_box_ZEROBYTES, crypto_box_BOXZEROBYTES };
#define MAXTEST_BYTES 4096
static unsigned char *ska;
static unsigned char *pka;
static unsigned char *skb;
static unsigned char *pkb;
static unsigned char *n;
static unsigned char *m;
static unsigned char *c;
static unsigned char *sa;
static unsigned char *sb;
void preallocate(void)
{
}
void allocate(void)
{
ska = alignedcalloc(crypto_box_SECRETKEYBYTES);
pka = alignedcalloc(crypto_box_PUBLICKEYBYTES);
skb = alignedcalloc(crypto_box_SECRETKEYBYTES);
pkb = alignedcalloc(crypto_box_PUBLICKEYBYTES);
n = alignedcalloc(crypto_box_NONCEBYTES);
m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
sa = alignedcalloc(crypto_box_BEFORENMBYTES);
sb = alignedcalloc(crypto_box_BEFORENMBYTES);
}
#define TIMINGS 15
static long long cycles[TIMINGS + 1];
void measure(void)
{
int i;
int loop;
int mlen;
for (loop = 0;loop < LOOPS;++loop) {
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_keypair(pka,ska);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(-1,"keypair_cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_keypair(pkb,skb);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(-1,"keypair_cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_beforenm(sa,pkb,ska);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(-1,"beforenm_cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_beforenm(sb,pka,skb);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(-1,"beforenm_cycles",cycles,TIMINGS);
for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / 8) {
randombytes(n,crypto_box_NONCEBYTES);
randombytes(m + crypto_box_ZEROBYTES,mlen);
randombytes(c,mlen + crypto_box_ZEROBYTES);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,pka,skb);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_open(m,c,mlen + crypto_box_ZEROBYTES,n,pkb,ska);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"open_cycles",cycles,TIMINGS);
++c[crypto_box_ZEROBYTES];
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_open(m,c,mlen + crypto_box_ZEROBYTES,n,pkb,ska);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"forgery_open_cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_afternm(c,m,mlen + crypto_box_ZEROBYTES,n,sb);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"afternm_cycles",cycles,TIMINGS);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_open_afternm(m,c,mlen + crypto_box_ZEROBYTES,n,sa);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"open_afternm_cycles",cycles,TIMINGS);
++c[crypto_box_ZEROBYTES];
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_box_open_afternm(m,c,mlen + crypto_box_ZEROBYTES,n,sa);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(mlen,"forgery_open_afternm_cycles",cycles,TIMINGS);
}
}
}

View File

@ -0,0 +1,197 @@
/*
* crypto_box/try.c version 20090118
* D. J. Bernstein
* Public domain.
*/
#include <stdlib.h>
#include "crypto_box.h"
#include "windows/windows-quirks.h"
extern unsigned char *alignedcalloc(unsigned long long);
const char *primitiveimplementation = crypto_box_IMPLEMENTATION;
#define MAXTEST_BYTES 10000
#define CHECKSUM_BYTES 4096
#define TUNE_BYTES 1536
static unsigned char *ska;
static unsigned char *pka;
static unsigned char *skb;
static unsigned char *pkb;
static unsigned char *s;
static unsigned char *n;
static unsigned char *m;
static unsigned char *c;
static unsigned char *t;
static unsigned char *ska2;
static unsigned char *pka2;
static unsigned char *skb2;
static unsigned char *pkb2;
static unsigned char *s2;
static unsigned char *n2;
static unsigned char *m2;
static unsigned char *c2;
static unsigned char *t2;
#define sklen crypto_box_SECRETKEYBYTES
#define pklen crypto_box_PUBLICKEYBYTES
#define nlen crypto_box_NONCEBYTES
#define slen crypto_box_BEFORENMBYTES
void preallocate(void)
{
}
void allocate(void)
{
ska = alignedcalloc(sklen);
pka = alignedcalloc(pklen);
skb = alignedcalloc(sklen);
pkb = alignedcalloc(pklen);
n = alignedcalloc(nlen);
m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
t = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
s = alignedcalloc(slen);
ska2 = alignedcalloc(sklen);
pka2 = alignedcalloc(pklen);
skb2 = alignedcalloc(sklen);
pkb2 = alignedcalloc(pklen);
n2 = alignedcalloc(nlen);
m2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
c2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
t2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
s2 = alignedcalloc(slen);
}
void predoit(void)
{
}
void doit(void)
{
crypto_box(c,m,TUNE_BYTES + crypto_box_ZEROBYTES,n,pka,skb);
crypto_box_open(t,c,TUNE_BYTES + crypto_box_ZEROBYTES,n,pkb,ska);
}
char checksum[nlen * 2 + 1];
const char *checksum_compute(void)
{
long long i;
long long j;
if (crypto_box_keypair(pka,ska) != 0) return "crypto_box_keypair returns nonzero";
if (crypto_box_keypair(pkb,skb) != 0) return "crypto_box_keypair returns nonzero";
for (j = 0;j < crypto_box_ZEROBYTES;++j) m[j] = 0;
for (i = 0;i < CHECKSUM_BYTES;++i) {
long long mlen = i + crypto_box_ZEROBYTES;
long long tlen = i + crypto_box_ZEROBYTES;
long long clen = i + crypto_box_ZEROBYTES;
for (j = -16;j < 0;++j) ska[j] = random();
for (j = -16;j < 0;++j) skb[j] = random();
for (j = -16;j < 0;++j) pka[j] = random();
for (j = -16;j < 0;++j) pkb[j] = random();
for (j = -16;j < 0;++j) m[j] = random();
for (j = -16;j < 0;++j) n[j] = random();
for (j = sklen;j < sklen + 16;++j) ska[j] = random();
for (j = sklen;j < sklen + 16;++j) skb[j] = random();
for (j = pklen;j < pklen + 16;++j) pka[j] = random();
for (j = pklen;j < pklen + 16;++j) pkb[j] = random();
for (j = mlen;j < mlen + 16;++j) m[j] = random();
for (j = nlen;j < nlen + 16;++j) n[j] = random();
for (j = -16;j < sklen + 16;++j) ska2[j] = ska[j];
for (j = -16;j < sklen + 16;++j) skb2[j] = skb[j];
for (j = -16;j < pklen + 16;++j) pka2[j] = pka[j];
for (j = -16;j < pklen + 16;++j) pkb2[j] = pkb[j];
for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
for (j = -16;j < clen + 16;++j) c2[j] = c[j] = random();
if (crypto_box(c,m,mlen,n,pkb,ska) != 0) return "crypto_box returns nonzero";
for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box overwrites m";
for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box overwrites n";
for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_box writes before output";
for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box writes after output";
for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
if (c[j] != 0) return "crypto_box does not clear extra bytes";
for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box overwrites ska";
for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box overwrites skb";
for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box overwrites pka";
for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box overwrites pkb";
for (j = -16;j < 0;++j) c[j] = random();
for (j = clen;j < clen + 16;++j) c[j] = random();
for (j = -16;j < clen + 16;++j) c2[j] = c[j];
for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random();
if (crypto_box_open(t,c,clen,n,pka,skb) != 0) return "crypto_box_open returns nonzero";
for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box_open overwrites c";
for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open overwrites n";
for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open writes before output";
for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open writes after output";
for (j = 0;j < crypto_box_ZEROBYTES;++j)
if (t[j] != 0) return "crypto_box_open does not clear extra bytes";
for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box_open overwrites ska";
for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites skb";
for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pka";
for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box_open overwrites pkb";
for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "plaintext does not match";
for (j = -16;j < slen + 16;++j) s2[j] = s[j] = random();
if (crypto_box_beforenm(s,pkb,ska) != 0) return "crypto_box_beforenm returns nonzero";
for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pk";
for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites sk";
for (j = -16;j < 0;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes before output";
for (j = slen;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes after output";
for (j = -16;j < slen + 16;++j) s2[j] = s[j];
for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random();
if (crypto_box_afternm(t,m,mlen,n,s) != 0) return "crypto_box_afternm returns nonzero";
for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_afternm overwrites s";
for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_afternm overwrites m";
for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_afternm overwrites n";
for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes before output";
for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes after output";
for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
if (t[j] != 0) return "crypto_box_afternm does not clear extra bytes";
for (j = 0;j < mlen;++j) if (t[j] != c[j]) return "crypto_box_afternm does not match crypto_box";
if (crypto_box_beforenm(s,pka,skb) != 0) return "crypto_box_beforenm returns nonzero";
for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random();
if (crypto_box_open_afternm(t,c,clen,n,s) != 0) return "crypto_box_open_afternm returns nonzero";
for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_open_afternm overwrites s";
for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_open_afternm overwrites m";
for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open_afternm overwrites n";
for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes before output";
for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes after output";
for (j = 0;j < crypto_box_ZEROBYTES;++j)
if (t[j] != 0) return "crypto_box_open_afternm does not clear extra bytes";
for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "crypto_box_open_afternm does not match crypto_box_open";
for (j = 0;j < i;++j) n[j % nlen] ^= c[j + crypto_box_BOXZEROBYTES];
if (i == 0) m[crypto_box_ZEROBYTES] = 0;
m[i + crypto_box_ZEROBYTES] = m[crypto_box_ZEROBYTES];
for (j = 0;j < i;++j) m[j + crypto_box_ZEROBYTES] ^= c[j + crypto_box_BOXZEROBYTES];
}
for (i = 0;i < nlen;++i) {
checksum[2 * i] = "0123456789abcdef"[15 & (n[i] >> 4)];
checksum[2 * i + 1] = "0123456789abcdef"[15 & n[i]];
}
checksum[2 * i] = 0;
return 0;
}

View File

@ -0,0 +1,24 @@
#include <string>
using std::string;
#include "crypto_box.h"
string crypto_box(const string &m,const string &n,const string &pk,const string &sk)
{
if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length";
if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length";
if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length";
size_t mlen = m.size() + crypto_box_ZEROBYTES;
unsigned char mpad[mlen];
for (int i = 0;i < crypto_box_ZEROBYTES;++i) mpad[i] = 0;
for (int i = crypto_box_ZEROBYTES;i < mlen;++i) mpad[i] = m[i - crypto_box_ZEROBYTES];
unsigned char cpad[mlen];
crypto_box(cpad,mpad,mlen,
(const unsigned char *) n.c_str(),
(const unsigned char *) pk.c_str(),
(const unsigned char *) sk.c_str()
);
return string(
(char *) cpad + crypto_box_BOXZEROBYTES,
mlen - crypto_box_BOXZEROBYTES
);
}

View File

@ -0,0 +1,12 @@
#include <string>
using std::string;
#include "crypto_box.h"
string crypto_box_keypair(string *sk_string)
{
unsigned char pk[crypto_box_PUBLICKEYBYTES];
unsigned char sk[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(pk,sk);
*sk_string = string((char *) sk,sizeof sk);
return string((char *) pk,sizeof pk);
}

View File

@ -0,0 +1,27 @@
#include <string>
using std::string;
#include "crypto_box.h"
string crypto_box_open(const string &c,const string &n,const string &pk,const string &sk)
{
if (pk.size() != crypto_box_PUBLICKEYBYTES) throw "incorrect public-key length";
if (sk.size() != crypto_box_SECRETKEYBYTES) throw "incorrect secret-key length";
if (n.size() != crypto_box_NONCEBYTES) throw "incorrect nonce length";
size_t clen = c.size() + crypto_box_BOXZEROBYTES;
unsigned char cpad[clen];
for (int i = 0;i < crypto_box_BOXZEROBYTES;++i) cpad[i] = 0;
for (int i = crypto_box_BOXZEROBYTES;i < clen;++i) cpad[i] = c[i - crypto_box_BOXZEROBYTES];
unsigned char mpad[clen];
if (crypto_box_open(mpad,cpad,clen,
(const unsigned char *) n.c_str(),
(const unsigned char *) pk.c_str(),
(const unsigned char *) sk.c_str()
) != 0)
throw "ciphertext fails verification";
if (clen < crypto_box_ZEROBYTES)
throw "ciphertext too short"; // should have been caught by _open
return string(
(char *) mpad + crypto_box_ZEROBYTES,
clen - crypto_box_ZEROBYTES
);
}

View File

@ -0,0 +1 @@
28ebe700b5878570702a68740aa131e6fa907e58a3f6915cd183c6db3f7afd7a

Some files were not shown because too many files have changed in this diff Show More