Commit 6d738803 authored by ale's avatar ale

C implementation of the core SSO library, using tweetnacl

parent 366f0ae5
# Note: this should match what's in lib/nacl-20110221/do.
short_hostname := $(shell hostname | sed 's/\..*//' | tr -cd '[a-z][A-Z][0-9]')
NACL_ABI := $(shell $(top_srcdir)/lib/nacl-20110221/printabi.debian)
NACL_DIR = $(top_builddir)/lib/nacl-20110221/build/$(short_hostname)
NACL_LIB_DIR = $(NACL_DIR)/lib/$(NACL_ABI)
AM_CPPFLAGS = \
-I$(top_srcdir)/src \
-I$(NACL_DIR)/include/$(NACL_ABI)
-D_GNU_SOURCE -Wall -Werror \
-I$(top_srcdir)/src
AM_LDFLAGS = \
-L$(top_builddir)/src \
-L$(NACL_LIB_DIR)
NACL_LIBS = $(NACL_LIB_DIR)/libnacl.a $(NACL_LIB_DIR)/librandombytes.a
SSO_LIBS = $(top_builddir)/src/sso/libsso.la $(NACL_LIBS)
LDADD = $(SSO_LIBS)
-L$(top_builddir)/src
AC_INIT([sso], [2.0], [info@autistici.org])
AC_CONFIG_SRCDIR([src/sso/ticket.cc])
AC_CONFIG_SRCDIR([src/sso/sso.c])
AC_LANG(C++)
AM_INIT_AUTOMAKE([dist-bzip2 foreign])
......@@ -16,10 +16,11 @@ AC_PROG_MAKE_SET
AC_PROG_LIBTOOL
AC_SUBST(LIBTOOL_DEPS)
CFLAGS="$CFLAGS -fPIC -std=c99 -pedantic"
CXXFLAGS="$CXXFLAGS -fPIC"
dnl Checks for libraries.
ACX_PTHREAD([have_pthread=yes], [heve_pthread=no])
ACX_PTHREAD([have_pthread=yes], [have_pthread=no])
CXXFLAGS="$CXXFLAGS $PTHREAD_CFLAGS"
LIBS="$PTHREAD_LIBS $LIBS"
......@@ -128,7 +129,6 @@ dnl Final stage
AC_OUTPUT(
Makefile
lib/Makefile
lib/nacl-20110221/Makefile
lib/gtest/Makefile
src/Makefile
src/sso/Makefile
......
SUBDIRS = nacl-20110221 gtest
SUBDIRS = gtest
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
include $(top_srcdir)/Makefile.defs
all: .build-stamp .librandombytes-stamp
.build-stamp:
$(srcdir)/do
-touch $@
.librandombytes-stamp: .build-stamp
$(AR) cru $(NACL_LIB_DIR)/librandombytes.a \
$(NACL_LIB_DIR)/randombytes.o
$(RANLIB) $(NACL_LIB_DIR)/librandombytes.a
-touch $@
clean:
-rm -f .build-stamp
crypto_verify
crypto_core
crypto_hashblocks
crypto_hash
crypto_stream
crypto_onetimeauth
crypto_auth
crypto_secretbox
crypto_scalarmult
crypto_box
crypto_sign
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 *);
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 *);
/*
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;
}
/*
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;
}
/*
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;
}
/*
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
#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();
}
/*
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