Commit ad7bc99a authored by bernd's avatar bernd

separate wrapper libs for fast/nonfast crypto libraries

parent b789c167
......@@ -20,7 +20,11 @@ GCC = gcc
LIBTOOL = libtool
CFLAGS = -O3 -fomit-frame-pointer
HOST =
FAST = no
FORTHLIB = ed25519-donna.fs keccak.fs threefish.fs
ifeq "$(FAST)" "yes"
FORTHLIB += ed25519-donnafast.fs keccakfast.fs threefishfast.fs
endif
SRC = .
ENGINE = gforth
VERSION = $(shell $(ENGINE) --version | tr ' ' /)
......@@ -36,21 +40,23 @@ datadir =
SOURCES = 64bit.fs alice-test.fs base64.fs base85.fs bob-test.fs \
client-test.fs client-tests.fs crypto-api.fs curve25519.fs \
debugging.fs ed25519-donna.fs ed25519-table-test.fs \
eve-test.fs hash-table.fs keccak.fs keccak-small.fs \
kregion.fs n2o.fs net2o-addr.fs net2o-classes.fs \
net2o-cmd.fs net2o-connected.fs net2o-connect.fs \
net2o-crypt.fs net2o-dht.fs net2o-err.fs net2o-file.fs \
net2o.fs net2o-ip.fs net2o-keys.fs net2o-log.fs \
net2o-msg.fs net2o-qr.fs net2o-template.fs net2o-tools.fs \
net2o-vault.fs rng.fs server-test.fs termclient.fs \
terminal-test.fs test-keys.fs threefish.fs xtype.fs \
tests/alice2-msg.fs tests/alice-msg.fs tests/bernd-msg.fs \
tests/bob-msg.fs tests/copy.fs tests/cryptspeed.fs \
tests/dht.fs tests/dht-pop.fs tests/ed25519.fs \
tests/insdeltest.fs tests/keccak.fs tests/keys.fs \
tests/msg.fs tests/teststat.fs tests/threefish.fs \
tests/vault.fs net2o-dhtroot.n2o
debugging.fs ed25519-table-test.fs eve-test.fs \
hash-table.fs keccak-small.fs kregion.fs n2o.fs \
net2o-addr.fs net2o-classes.fs net2o-cmd.fs \
net2o-connected.fs net2o-connect.fs net2o-crypt.fs \
net2o-dht.fs net2o-err.fs net2o-file.fs net2o.fs \
net2o-ip.fs net2o-keys.fs net2o-log.fs net2o-msg.fs \
net2o-qr.fs net2o-template.fs net2o-tools.fs net2o-vault.fs \
rng.fs server-test.fs termclient.fs terminal-test.fs \
test-keys.fs xtype.fs tests/alice2-msg.fs \
tests/alice-msg.fs tests/bernd-msg.fs tests/bob-msg.fs \
tests/copy.fs tests/cryptspeed.fs tests/dht.fs \
tests/dht-pop.fs tests/ed25519.fs tests/insdeltest.fs \
tests/keccak.fs tests/keys.fs tests/msg.fs \
tests/teststat.fs tests/threefish.fs tests/vault.fs \
net2o-dhtroot.n2o $(FORTHLIB) ed25519-donnalib.fs \
keccaklib.fs threefishlib.fs
SRCDIRS = tests
......
......@@ -19,43 +19,23 @@
\ dummy load for Android
require rec-scope.fs
[IFDEF] android
also android fast-lib previous SDK_INT 10 > and
[IF] s" libed25519primsfast.so" [ELSE] s" libed25519prims.so" [THEN]
also c-lib open-path-lib drop previous
android:fast-lib [IF]
require ed25519-donnafast.fs false
[ELSE]
s" libed25519_donna.so" c-lib:open-path-lib drop true
[THEN]
[ELSE]
true
[THEN]
[IF]
c-library ed25519_donna
"ed25519prims" add-lib
include ed25519-donnalib.fs
end-c-library
[THEN]
c-library ed25519_donna
"ed25519prims" add-lib
\c #include <stdint.h>
\c #include <ed25519-prims.h>
\c int str32eq(long* a, long* b) {
\c long diff=0;
\c switch(sizeof(long)) {
\c case 4:
\c diff|=((a[4]^b[4])|(a[5]^b[5])|(a[6]^b[6])|(a[7]^b[7]));
\c case 8:
\c diff|=((a[0]^b[0])|(a[1]^b[1])|(a[2]^b[2])|(a[3]^b[3]));
\c }
\c return -(diff==0);
\c }
c-function raw>sc25519 expand_raw256_modm a a -- void ( sc char[32] -- )
c-function nb>sc25519 expand256_modm a a n -- void ( sc char[64] n -- )
c-function sc25519>32b contract256_modm a a -- void ( char[32] sc -- )
c-function sc25519* mul256_modm a a a -- void ( r x y -- )
c-function sc25519+ add256_modm a a a -- void ( r x y -- )
c-function ge25519*base ge25519_scalarmult_base a a -- void ( ger x -- )
c-function ge25519-pack ge25519_pack a a -- void ( r ger -- )
c-function ge25519-unpack- ge25519_unpack_negative_vartime a a -- n ( r p -- flag )
c-function ge25519*+ ge25519_double_scalarmult_vartime a a a a -- void ( r p s1 s2 -- )
c-function ge25519*v ge25519_scalarmult_vartime a a a -- void ( r p s -- )
c-function ge25519* ge25519_scalarmult a a a -- void ( r p s -- )
c-function 32b= str32eq a a -- n ( addr1 addr2 -- flag )
c-variable ge25519-basepoint ge25519_basepoint ( -- addr )
c-variable ge25519-niels*[] ge25519_niels_sliding_multiples ( -- addr )
end-c-library
: 32b>sc25519 32 nb>sc25519 ;
: 64b>sc25519 64 nb>sc25519 ;
......
......@@ -15,48 +15,23 @@
\ You should have received a copy of the GNU Affero General Public License
\ along with this program. If not, see <http://www.gnu.org/licenses/>.
require rec-scope.fs
[IFDEF] android
also android fast-lib previous SDK_INT 10 > and
[IF] s" libkeccakfast.so" [ELSE] s" libkeccak.so" [THEN]
also c-lib open-path-lib drop previous
android:fast-lib [IF]
require keccakfast.fs false
[ELSE]
s" libkeccak.so" c-lib:open-path-lib drop true
[THEN]
[ELSE]
true
[THEN]
[IF]
c-library keccak
s" keccak" add-lib
include keccaklib.fs
end-c-library
[THEN]
c-library keccak
s" keccak" add-lib
\c #include <KeccakF-1600.h>
\c UINT64* KeccakEncryptLoop(keccak_state state, UINT64 * data, int n, int rounds)
\c {
\c while(n>0) {
\c unsigned int p = n >= 128 ? 128 : n;
\c KeccakF(state, rounds);
\c KeccakEncrypt(state, data, p);
\c data = (UINT64*)(((char*)data)+p); n-=p;
\c }
\c return data;
\c }
\c UINT64* KeccakDecryptLoop(keccak_state state, UINT64 * data, int n, int rounds)
\c {
\c while(n>0) {
\c unsigned int p = n >= 128 ? 128 : n;
\c KeccakF(state, rounds);
\c KeccakDecrypt(state, data, p);
\c data = (UINT64*)(((char*)data)+p); n-=p;
\c }
\c return data;
\c }
\ ------===< functions >===-------
c-function KeccakInitialize KeccakInitialize -- void
c-function KeccakF KeccakF a n -- void
c-function KeccakInitializeState KeccakInitializeState a -- void
c-function KeccakExtract KeccakExtract a a n -- void
c-function KeccakAbsorb KeccakAbsorb a a n -- void
c-function KeccakEncrypt KeccakEncrypt a a n -- void
c-function KeccakDecrypt KeccakDecrypt a a n -- void
c-function KeccakEncryptLoop KeccakEncryptLoop a a n n -- a
c-function KeccakDecryptLoop KeccakDecryptLoop a a n n -- a
end-c-library
25 8 * Constant keccak#
128 Constant keccak#max
......
......@@ -504,7 +504,7 @@ previous
nat( ." ok from: " ret-addr .addr-path space dup .
dup reply[] 2@ d0= IF ." acked" THEN cr )
0. 2 pick reply[] dup >r 2!
['] drop r> reply-xt !@ execute ; \ clear request
['] drop r> reply-xt !@ ?dup-IF execute THEN ; \ clear request
: net2o:expect-reply ( -- ) o?
timeout( cmd( ." expect: " cmdbuf$ n2o:see ) )
cmdbuf$
......
......@@ -276,6 +276,22 @@ Defer !my-addr
$@ .address
$10 p+ 0 -skip dup IF '|' emit THEN xtype THEN ;
\ Create udp socket
4242 Value net2o-port
0 Value net2o-client-port \ client port by default unassigned
Variable net2o-host "net2o.de" net2o-host $!
: net2o-socket ( port -- ) dup >r
create-udp-server46
[IFDEF] no-hybrid 0 [THEN] to net2o-sock
r> ?dup-0=-IF my-port THEN to my-port#
[IFDEF] no-hybrid
net2o-sock drop my-port# create-udp-server to net2o-sock
[THEN]
!my-addr ;
0 [IF]
Local Variables:
forth-local-words:
......
......@@ -340,4 +340,28 @@ $10 Constant datesize#
: <warn> warn-color attr! ;
: <info> info-color attr! ;
: <err> err-color attr! ;
: <black> black >fg black >bg or attr! ;
\ No newline at end of file
: <black> black >fg black >bg or attr! ;
\ Memory words
\ the policy on allocation and freeing is that both freshly allocated
\ and to-be-freed memory is erased. This makes sure that no unwanted
\ data will be lurking in that memory, waiting to be leaked out
: alloz ( size -- addr )
dup >r allocate throw dup r> erase ;
: freez ( addr size -- )
\G erase and then free - for secret stuff
over swap erase free throw ;
: ?free ( addr size -- ) >r
dup @ IF dup @ r@ freez off ELSE drop THEN rdrop ;
: allo1 ( size -- addr )
dup >r allocate throw dup r> $FF fill ;
: allocate-bits ( size -- addr )
dup >r cell+ allo1 dup r> + off ; \ last cell is off
: ?free+guard ( addr u -- )
over @ IF over @ swap 2dup erase free+guard off
ELSE 2drop THEN ;
......@@ -69,6 +69,17 @@ max-size^2 6 + Value chunk-p2
$10 Constant key-salt#
$10 Constant key-cksum#
\ for bigger blocks, we use use alloc+guard, i.e. mmap with a
\ guard page after the end.
: alloc-buf ( -- addr )
maxpacket-aligned buffers# * alloc+guard ;
: alloc-buf+6 ( -- addr ) alloc-buf 6 + ;
: free-buf ( addr -- )
maxpacket-aligned buffers# * 2dup erase free+guard ;
: free-buf+6 ( addr -- )
6 - free-buf ;
[IFDEF] cygwin
: no-hybrid ; \ cygwin can't deal with hybrid stacks
[THEN]
......@@ -120,22 +131,6 @@ Variable routes
require net2o-ip.fs
\ Create udp socket
4242 Value net2o-port
0 Value net2o-client-port \ client port by default unassigned
Variable net2o-host "net2o.de" net2o-host $!
: net2o-socket ( port -- ) dup >r
create-udp-server46
[IFDEF] no-hybrid 0 [THEN] to net2o-sock
r> ?dup-0=-IF my-port THEN to my-port#
[IFDEF] no-hybrid
net2o-sock drop my-port# create-udp-server to net2o-sock
[THEN]
!my-addr ;
begin-structure reply
field: reply-len
field: reply-offset
......@@ -178,38 +173,6 @@ UValue pollfd# 0 to pollfd#
[IFDEF] no-hybrid POLLIN swap fds!+ [THEN]
pollfds - pollfd / to pollfd# ;
\ the policy on allocation and freeing is that both freshly allocated
\ and to-be-freed memory is erased. This makes sure that no unwanted
\ data will be lurking in that memory, waiting to be leaked out
: alloz ( size -- addr )
dup >r allocate throw dup r> erase ;
: freez ( addr size -- )
\G erase and then free - for secret stuff
over swap erase free throw ;
: ?free ( addr size -- ) >r
dup @ IF dup @ r@ freez off ELSE drop THEN rdrop ;
: allo1 ( size -- addr )
dup >r allocate throw dup r> $FF fill ;
: allocate-bits ( size -- addr )
dup >r cell+ allo1 dup r> + off ; \ last cell is off
\ for bigger blocks, we use use alloc+guard, i.e. mmap with a
\ guard page after the end.
: alloc-buf ( -- addr )
maxpacket-aligned buffers# * alloc+guard ;
: alloc-buf+6 ( -- addr ) alloc-buf 6 + ;
: free-buf ( addr -- )
maxpacket-aligned buffers# * 2dup erase free+guard ;
: free-buf+6 ( addr -- )
6 - free-buf ;
: ?free+guard ( addr u -- )
over @ IF over @ swap 2dup erase free+guard off
ELSE 2drop THEN ;
ustack string-stack
ustack object-stack
ustack t-stack
......
\ threefish wrapper
\ dummy load for Android
\ Copyright (C) 2015 Bernd Paysan
\ This program is free software: you can redistribute it and/or modify
\ it under the terms of the GNU Affero 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 Affero General Public License for more details.
\ You should have received a copy of the GNU Affero General Public License
\ along with this program. If not, see <http://www.gnu.org/licenses/>.
require rec-scope.fs
[IFDEF] android
also android fast-lib previous SDK_INT 10 > and
[IF] s" libthreefishfast.so" [ELSE] s" libthreefish.so" [THEN]
also c-lib open-path-lib drop previous
android:fast-lib [IF]
require threefishfast.fs false
[ELSE]
s" libthreefish.so" c-lib:open-path-lib drop true
[THEN]
[ELSE]
true
[THEN]
[IF]
c-library threefish
s" threefish" add-lib
include threefishlib.fs
end-c-library
[THEN]
c-library threefish
s" threefish" add-lib
\c #include <threefish.h>
\c void tf_encrypt_loop(struct tf_ctx *ctx, uint64_t *p, size_t n,
\c int flags1, int flags2) {
\c int flags=flags1;
\c while(n>=64) {
\c tf_encrypt(ctx, p, p, flags);
\c flags=flags2; p+=8; n-=64;
\c ctx->tweak[1] += !++(ctx->tweak[0]);
\c }
\c }
\c void tf_decrypt_loop(struct tf_ctx *ctx, uint64_t *c, size_t n,
\c int flags1, int flags2) {
\c int flags=flags1;
\c while(n>=64) {
\c tf_decrypt(ctx, c, c, flags);
\c flags=flags2; c+=8; n-=64;
\c ctx->tweak[1] += !++(ctx->tweak[0]);
\c }
\c }
\ -------===< structs >===--------
\ tf_ctx
begin-structure tf_ctx
drop 0 72 +field tf_ctx-key
drop 72 24 +field tf_ctx-tweak
drop 96 end-structure
\ ------===< functions >===-------
c-function tf_encrypt tf_encrypt a a a n -- void
c-function tf_decrypt tf_decrypt a a a n -- void
c-function tf_encrypt_loop tf_encrypt_loop a a n n n -- void
c-function tf_decrypt_loop tf_decrypt_loop a a n n n -- void
end-c-library
UValue @threefish
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment