Merge branches 'jb/rnv-restage' and 'jb/rnv-fixes', remote-tracking branches 'frodwith/keccak' and 'botter-nidnul/release/next-vere' into jb/rnv-merge

* jb/rnv-restage:
  build: patch windows uv_pipe impl to allow null writes
  build: update pmnsh to support cachix text/x-nix-narinfo responses
  vere: renames terminal streams for clarity, removes unused uv_tcp_t
  vere: suppress null writes in term.c
  Revert "Merge pull request #4463 from urbit/m/next-gen-term"

* jb/rnv-fixes:
  vere: bumps versions
  vere: fixes crash (null deref) in comet booting
  u3: fixes printf in c3_assert()

* frodwith/keccak:
  jets: ice keccak
  working keccak jets
  urcrypt: fixing some typos - now builds
  Squashed 'pkg/urcrypt/keccak-tiny/' content from commit 9b438dc0f7
  urcrypt,jets: initial commit of keccak material

* botter-nidnul/release/next-vere:
  urcrypt: argon2 only optimize for x86_64 on x86_64
This commit is contained in:
Joe Bryan 2021-09-24 18:05:37 -04:00
commit b1bc4b54f3
22 changed files with 830 additions and 13 deletions

View File

@ -1587,14 +1587,15 @@
:::: ++keccak:crypto :: (2b7) keccak family
:: ::::
++ keccak
~% %kecc ..part ~
|%
::
:: keccak
::
++ keccak-224 |=(a=octs (keccak 1.152 448 224 a))
++ keccak-256 |=(a=octs (keccak 1.088 512 256 a))
++ keccak-384 |=(a=octs (keccak 832 768 384 a))
++ keccak-512 |=(a=octs (keccak 576 1.024 512 a))
++ keccak-224 ~/ %k224 |=(a=octs (keccak 1.152 448 224 a))
++ keccak-256 ~/ %k256 |=(a=octs (keccak 1.088 512 256 a))
++ keccak-384 ~/ %k384 |=(a=octs (keccak 832 768 384 a))
++ keccak-512 ~/ %k512 |=(a=octs (keccak 576 1.024 512 a))
::
++ keccak (cury (cury hash keccak-f) padding-keccak)
::

View File

@ -1,5 +1,5 @@
name: urbit-king
version: 1.5
version: 1.6
license: MIT
license-file: LICENSE
data-files:

View File

@ -32,7 +32,7 @@
if (!(x)) { \
fflush(stderr); \
fprintf(stderr, "\rAssertion '%s' " \
"failed in %s:%d\n", \
"failed in %s:%d\r\n", \
#x, __FILE__, __LINE__); \
c3_cooked(); \
assert(x); \

View File

@ -192,6 +192,10 @@
u3_noun u3we_hmac(u3_noun);
u3_noun u3we_kecc224(u3_noun);
u3_noun u3we_kecc256(u3_noun);
u3_noun u3we_kecc384(u3_noun);
u3_noun u3we_kecc512(u3_noun);
u3_noun u3we_argon2(u3_noun);

39
pkg/urbit/jets/e/keccak.c Normal file
View File

@ -0,0 +1,39 @@
/* jets/e/keccak.c
*/
#include "all.h"
#include <urcrypt.h>
#define defw(bits,byts) \
u3_atom \
_kecc_##bits(c3_w len_w, u3_atom a) \
{ \
c3_y out[byts]; \
c3_y* buf_y = u3r_bytes_alloc(0, len_w, a); \
if ( 0 != urcrypt_keccak_##bits(buf_y, len_w, out) ) { \
/* urcrypt_keccac_##bits always succeeds when called correctly */ \
return u3m_bail(c3__oops); \
} \
else { \
u3_atom pro = u3i_bytes(byts, out); \
u3a_free(buf_y); \
return pro; \
} \
} \
\
u3_weak \
u3we_kecc##bits(u3_noun cor) \
{ \
c3_w len_w; \
u3_noun len, tom; \
u3x_mean(cor, u3x_sam_2, &len, u3x_sam_3, &tom, 0); \
return ( (c3n == u3ud(len)) || (c3n == u3ud(tom)) ) \
? u3m_bail(c3__exit) \
: (!u3r_word_fit(&len_w, len)) \
? u3m_bail(c3__fail) \
: _kecc_##bits(len_w, tom); \
}
defw(224, 28)
defw(256, 32)
defw(384, 48)
defw(512, 64)

View File

@ -15,6 +15,8 @@
#include "all.h"
static c3_c* no_hashes[] = { 0 };
static u3j_harm _140_hex_mimes_base16_en_a[] = {{".2", u3we_en_base16}, {}};
static c3_c* _140_hex_mimes_base16_en_ha[] = {
"669807766b6802719769fcbfe149d77fb352fcf0922afaf35dc4ab8c201d84e5",
@ -435,6 +437,26 @@ static c3_c* _140_hex_blake_ha[] = {
0
};
static u3j_harm _140_hex_kecc_k224_a[] =
{{".2", u3we_kecc224, c3y, c3y, c3y}, {}};
static u3j_harm _140_hex_kecc_k256_a[] =
{{".2", u3we_kecc256, c3y, c3y, c3y}, {}};
static u3j_harm _140_hex_kecc_k384_a[] =
{{".2", u3we_kecc384, c3y, c3y, c3y}, {}};
static u3j_harm _140_hex_kecc_k512_a[] =
{{".2", u3we_kecc512, c3y, c3y, c3y}, {}};
static u3j_core _140_hex_kecc_d[] =
{ { "k224", 7, _140_hex_kecc_k224_a, 0, no_hashes },
{ "k256", 7, _140_hex_kecc_k256_a, 0, no_hashes },
{ "k384", 7, _140_hex_kecc_k384_a, 0, no_hashes },
{ "k512", 7, _140_hex_kecc_k512_a, 0, no_hashes },
{}
};
static u3j_harm _140_hex_ripemd_160_a[] = {{".2", u3we_ripe, c3y}, {}};
static c3_c* _140_hex_ripemd_160_ha[] = {
"176684b29926a01f5c60fa584e4691b0cbdc9b93608dcbe7d0cf3585683fa42f",
@ -462,6 +484,7 @@ static u3j_core _140_hex_d[] =
{ "hmac", 63, 0, _140_hex_hmac_d, _140_hex_hmac_ha },
{ "argon", 31, 0, _140_hex_argon_d, _140_hex_argon_ha },
{ "blake", 31, 0, _140_hex_blake_d, _140_hex_blake_ha },
{ "kecc", 31, 0, _140_hex_kecc_d, no_hashes },
{ "ripemd", 31, 0, _140_hex_ripe_d, _140_hex_ripe_ha },
{ "scr", 31, 0, _140_hex_scr_d, _140_hex_scr_ha },
{ "secp", 6, 0, _140_hex_secp_d, _140_hex_secp_ha },

View File

@ -186,12 +186,13 @@ _king_dawn(u3_noun feed, u3_noun pill, u3_noun path)
//
u3C.slog_f = _king_slog;
u3_noun ship = ( c3y == u3a_is_cell(u3h(feed)) )
? u3h(u3t(feed))
: u3h(feed);
u3_noun vent = u3_dawn_vent(u3k(ship), u3k(feed));
// XX link properly
//
//NOTE +slav is safe because _boothack_key already verified it
u3_noun ship = u3dc("slav", 'p', u3i_string(u3_Host.ops_u.who_c));
u3_noun vent = u3_dawn_vent(u3k(ship), u3k(feed));
u3K.pir_u = u3_pier_boot(sag_w, ship, vent, pill, path, feed);
u3K.pir_u = u3_pier_boot(sag_w, u3k(ship), vent, pill, path, feed);
// disable ivory slog printfs
//

View File

@ -1 +1 @@
1.5
1.6

View File

@ -6,6 +6,7 @@ lib_LTLIBRARIES = liburcrypt.la
noinst_LTLIBRARIES = libed25519.la \
libge_additions.la \
libargon2.la \
libkeccak_tiny.la \
libscrypt.la
include_HEADERS = urcrypt/urcrypt.h
@ -26,6 +27,7 @@ liburcrypt_la_CPPFLAGS = -I$(srcdir)/ed25519/src \
-I$(srcdir)/ge-additions \
-I$(srcdir)/argon2/include \
-I$(srcdir)/argon2/src/blake2 \
-I$(srcdir)/keccak-tiny \
-I$(srcdir)/scrypt
liburcrypt_la_LIBADD = $(LIBCRYPTO_LIBS) \
$(LIBSECP256K1_LIBS) \
@ -33,6 +35,7 @@ liburcrypt_la_LIBADD = $(LIBCRYPTO_LIBS) \
libed25519.la \
libge_additions.la \
libargon2.la \
libkeccak_tiny.la \
libscrypt.la
liburcrypt_la_CFLAGS = $(LIBCRYPTO_CFLAGS) \
$(LIBSECP256K1_CFLAGS) \
@ -48,6 +51,7 @@ liburcrypt_la_SOURCES = urcrypt/aes_cbc.c \
urcrypt/ge_additions.c \
urcrypt/ripemd.c \
urcrypt/scrypt.c \
urcrypt/keccak.c \
urcrypt/secp256k1.c \
urcrypt/sha.c \
urcrypt/util.c \
@ -89,8 +93,18 @@ libargon2_la_SOURCES = argon2/src/core.h \
argon2/src/core.c \
argon2/src/blake2/blake2b.c \
argon2/src/thread.c \
argon2/src/encoding.c \
argon2/src/opt.c
argon2/src/encoding.c
# argon2 different sources for different CPU architectures
# opt.c requires SSE instructions and won't work on AArch64 et al.
if ARCH_X86_64
libargon2_la_SOURCES += \
argon2/src/opt.c
endif
if ARCH_GENERIC
libargon2_la_SOURCES += \
argon2/src/ref.c
endif
# scrypt
libscrypt_la_CPPFLAGS = -D_FORTIFY_SOURCE=2
@ -108,3 +122,9 @@ libscrypt_la_SOURCES = scrypt/b64.c \
scrypt/crypto_scrypt-hexconvert.h \
scrypt/slowequals.h \
scrypt/sysendian.h
# keccak-tiny
libkeccak_tiny_la_CFLAGS = -march=native -std=c11 -Wextra -Wpedantic -Wall
libkeccak_tiny_la_SOURCES = keccak-tiny/keccak-tiny.c \
keccak-tiny/define-macros.h \
keccak-tiny/keccak-tiny.h

View File

@ -101,6 +101,17 @@ AC_TYPE_UINT8_T
# Checks for library functions.
AC_CHECK_FUNCS([memset])
# Checks for CPU architecture, uses SSE instructions if on X86_64
AS_CASE([$host_cpu],
[x86_64], [ARCH=x86_64
AC_MSG_WARN("Architecture x86_64: Building libargon2 with optimizations")],
[ARCH=generic
AC_MSG_WARN("Architecture $host_cpu is not x86_64: Building libargon2 without optimizations")]
)
AC_SUBST([ARCH])
AM_CONDITIONAL([ARCH_X86_64], [test "$ARCH" = 'x86_64'])
AM_CONDITIONAL([ARCH_GENERIC], [test "$ARCH" = 'generic'])
# Finish and output
AC_CONFIG_FILES([Makefile liburcrypt-$URCRYPT_API_VERSION.pc:liburcrypt.pc.in])
AC_OUTPUT

View File

@ -0,0 +1,54 @@
---
Language: Cpp
# BasedOnStyle: Chromium
AccessModifierOffset: -1
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
AlwaysBreakTemplateDeclarations: true
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: false
ColumnLimit: 90
ConstructorInitializerAllOnOneLineOrOnePerLine: false
DerivePointerBinding: false
ExperimentalAutoDetectBinPacking: false
IndentCaseLabels: true
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerBindsToType: true
SpacesBeforeTrailingComments: 2
Cpp11BracedListStyle: true
Standard: Cpp11
IndentWidth: 2
TabWidth: 8
UseTab: Never
BreakBeforeBraces: Attach
IndentFunctionDeclarationAfterType: true
SpacesInParentheses: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
...

139
pkg/urcrypt/keccak-tiny/.gitignore vendored Normal file
View File

@ -0,0 +1,139 @@
### https://raw.github.com/github/gitignore/master/Global/OSX.gitignore
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### https://raw.github.com/github/gitignore/master/Global/vim.gitignore
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~
### https://raw.github.com/github/gitignore/master/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml
# Translations
*.mo
*.pot
# Django stuff:
*.log
# Sphinx documentation
docs/_build/
### https://raw.github.com/github/gitignore/master/C.gitignore
# Object files
*.o
*.ko
*.obj
*.elf
# Libraries
*.lib
*.a
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
### https://raw.github.com/github/gitignore/master/Global/Xcode.gitignore
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.xcuserstate
### https://raw.github.com/github/gitignore/master/Global/Linux.gitignore
*~
# KDE directory preferences
.directory
kcksum
.ninja_deps
.ninja_log
JUNK/

View File

@ -0,0 +1,82 @@
# libkeccak-tiny
An implementation of the FIPS-202-defined SHA-3 and SHAKE functions
in 120 cloc (156 lines). One C file, one header.
The `Keccak-f[1600]` permutation is fully unrolled; it's nearly as fast
as the Keccak team's optimized permutation.
## Building
> clang -O3 -march=native -std=c11 -Wextra -dynamic -shared keccak-tiny.c -o libkeccak-tiny.dylib
If you don't have a modern libc that includes the `memset_s` function,
you can just add `-D"memset_s(W,WL,V,OL)=memset(W,V,OL)` to the command
line.
## Using
Build the library, include the header, and do, e.g.,
shake256(out, 256, in, inlen);
That's it.
(Note: You can request less output from the fixed-output-length
functions, but not more.)
## TweetShake
The relevant tweets:
```C
// @hashbreaker Inspired by TweetNaCl!
// Keccak and SHA-3 are supposedly hard to implement. So, how many tweets does it take to get to the center of a sponge...?
#define decshake(bits) int shake##bits(unsigned char* o, unsigned long, unsigned char*, unsigned long); /*begin keccak.h*/
#define decsha3(bits) int sha3_##bits(unsigned char*,unsigned long,unsigned char*,unsigned long);
decshake(128) decshake(256) decsha3(224) decsha3(256) decsha3(384) decsha3(512) /*end keccak.h*/
#define K static const /* Keccak constants: rho rotations, pi lanes, and iota RCs */ /*begin keccak.c*/
typedef unsigned char byte;typedef byte*bytes;typedef unsigned long z;typedef unsigned long long u8;K u8 V=1ULL<<63;K u8 W=1ULL<<31;/*!gcc*/
#define V (1ULL<<63)
#define W (1ULL<31)
K byte rho[24]={1,3,6,10,15,21,28,36,45,55,2,14,27,41,56,8,25,43,62,18,39,61,20,44};K u8 RC[24]={1,0x8082,V|0x808a,V|W|0x8000,0x808b,W|1,V|W
|0x8081,V|0x8009,138,136,W|0x8009,W|10,W|0x808b,V|0x8b,V|0x8089,V|0x8003,V|0x8002,V|0x80,0x800a,V|W|0xa,V|W|0x8081,V|0x8080,W|1,V|W|0x8008};
K byte pi[25]={10,7,11,17,18,3,5,16,8,21,24,4,15,23,19,13,12,2,20,14,22,9,6,1}; /**helpers:*/static inline z min(z a,z b){return (a<b)?a:b;}
#define ROL(x, s) /* rotary shift */ (((x) << s) | ((x) >> (64-s))) /**macros to fully unroll the Keccak-f[1600] permutation:*/
#define R24(e) /* repeat 24 times */ e e e e e e e e e e e e e e e e e e e e e e e e
#define L5(v,s,e) /* 5-unroll a loop */ v=0; e; v+=s; e; v+=s; e; v+=s; e; v+=s; e; v+=s; /**the permutation:*/
static inline void keccakf(u8* a){u8 b[5]={0};u8 t=0;byte x,y,i=0; /*24 rounds:*/R24( L5(x,1,b[x]=0;L5(y,5, /*parity*/ b[x] ^= a[x+y]))
L5(x,1,L5(y,5,/*theta*/a[y+x] ^= b[(x+4)%5] ^ ROL(b[(x+1)%5],1))) t=a[1];x=0;R24(b[0]=a[pi[x]];/*rho*/a[pi[x]]=ROL(t, rho[x]);t=b[0];x++;)
L5(y,5,L5(x,1, /*chi*/ b[x] = a[y+x]) L5(x,1, a[y+x] = b[x] ^ ~b[(x+1)%5] & b[(x+2)%5])) /*iota*/ a[0] ^= RC[i]; i++; )} /**keccak-f!**/
#define FOR(i, ST, L, S) /*obvious*/ do { for (z i = 0; i < L; i += ST) { S; } } while (0) /**now, the sponge construction in hash mode**/
#define appl(NAME, S) /*macro to define array comprehensions*/ static inline void NAME(bytes dst, bytes src, z len) { FOR(i, 1, len, S); }
/*helpers:*/ static inline void clear(bytes a) { FOR(i,1,200,a[i]=0); } appl(xorin, dst[i] ^= src[i]) appl(set, src[i] = dst[i])
#define foldP(I, L, F) /* macro to fold app P F */ while (L >= r) { /*apply F*/ F(a, I, r); /*permute*/ keccakf(A); I += r; L -= r; }
static inline int hash(bytes o,z olen,bytes in,z ilen,z r,byte D){ if((o == (void*)0)||((in == (void*)0)&&ilen != 0)||(r >= 200))return -1;
/*absorb*/u8 A[25]={0};bytes a=(bytes)A;/*full blocks*/foldP(in,ilen,xorin);/*last block*/xorin(a,in,ilen);/**ds+padstart*/a[ilen]^=D;
/*padend:*/a[r-1]^=0x80; /**permute**/keccakf(A); /**squeeze:**/foldP(o,olen,set);/*last bytes*/set(a,o,olen);/*done!*/clear(a);return 0;}
#define defshake(bits) int shake##bits(bytes o, z olen, bytes in, z ilen) {return hash(o,olen,in,ilen,200-(bits/4),0x1f);}
#define defsha3(bits) int sha3_##bits(bytes o,z olen,bytes in,z ilen) {return hash(o,min(olen,200-(bits/4)),in,ilen,200-(bits/4),0x06);}
/*define the SHA3 and SHAKE instances:*/defshake(128) defshake(256) defsha3(224) defsha3(256) defsha3(384) defsha3(512)/*end keccak.c*/
// ...chomp. 24 kinda legible tweets (3232 bytes). And a simple interface: shake256(digest, digestlen, in, inlen)
// Clang recommended. GCC users will need to insert "#define V (1ULL<<63)" and "#define W (1ULL<31)" at the point marked "/*!gcc*/"
// If you're using as a prefix MAC, you MUST replace the body of "clear" with "memset_s(a, 200, 0, 200)" to avoid misoptimization.
// @everyone_who_is_still_using_sha1 Please stop using SHA-1.
// Oh, one more thing: a C11-threaded, memmapped shake256sum in 10 tweets. (Your libc may need a shim for C11 thread support.)
// echo -n string stdio stdint fcntl sys/mman sys/stat sys/types unistd threads|tr ' ' \\n|xargs -n1 -I_ echo '#include <_.h>'
#include "kcksum_tweet.h"
#define E(LABEL, MSG) if (err != 0) { strerror_r(err, serr, 1024); fprintf(stderr, "%s: '%s' %s\n", serr, fn, MSG); goto LABEL;}
static mtx_t iomtx;void h(void* v);void h(void* v){char* fn=(char*)v;int err=0;char serr[1024]={0};/*open file*/int fd=open(fn, O_RDONLY);
err=!fd;E(ret,"couldn't be opened.");/*stat it*/struct stat stat;err=fstat(fd,&stat);E(close,"doesn't exist.");err=!!(stat.st_mode&S_IFDIR);
E(close,"not a regular file.");z length=(size_t)stat.st_size;/*mmap the file*/bytes in=length?mmap(0,length,PROT_READ,MAP_SHARED,fd,0):NULL;
if(length&&(in==MAP_FAILED)){E(close,"mmap-ing failed.");}byte out[64]={0};/*hash it*/shake256(out,64,in,length);length&&munmap(in,length);
/*lock io*/mtx_lock(&iomtx);printf("SHAKE256('%s') = ", fn);FOR(i,1,64,printf("%02x",out[i]));printf("\n");mtx_unlock(&iomtx);/*unlock io*/
close:close(fd);ret:thrd_exit(err);}int main(int argc,char** argv){int err=0; mtx_init(&iomtx, mtx_plain); thrd_t t[4]; int res[4],i,j,k;
for(i=1;i<argc;i+=4){for(j=0;j<4;j++){if((j+i)==argc){/*out of files*/goto join;} /*spawn*/ thrd_create(t + j,h,argv[i + j]);}
join: for (k = 0; k < j; k++) { /*wait*/ err |= thrd_join(t[k], res + k); err |= res[k];} } mtx_destroy(&iomtx); return err; } /* done! */
```
## License
[CC0](http://creativecommons.org/publicdomain/zero/1.0/)

View File

@ -0,0 +1,44 @@
#ifndef DEFINEMACROS_H
#define DEFINEMACROS_H
/*** Helper macros to define SHA3 and SHAKE instances. ***/
#define defshake(bits) \
int shake##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \
}
#define defsha3(bits) \
int sha3_##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \
}
#define defkeccak(bits) \
int keccak_##bits(uint8_t* out, size_t outlen, \
const uint8_t* in, size_t inlen) { \
if (outlen > (bits/8)) { \
return -1; \
} \
return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \
}
/*** FIPS202 SHAKE VOFs ***/
defshake(128)
defshake(256)
/*** FIPS202 SHA3 FOFs ***/
defsha3(224)
defsha3(256)
defsha3(384)
defsha3(512)
/*** Non FIP202 SHA3 (KECCAK) FOFs ***/
defkeccak(224)
defkeccak(256)
defkeccak(384)
defkeccak(512)
#endif // DEFINEMACROS_H

5
pkg/urcrypt/keccak-tiny/do.sh Executable file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env sh
cc=$(which clang-3.6||which gcc-4.9||which clang||which gcc)
so=$(test -f /etc/asl.conf && printf dylib|| printf so)
$cc "-Dinline=__attribute__((__always_inline__))" -O3 -march=native -std=c11 -Wextra -Wpedantic -Wall -dynamic -shared keccak-tiny.c -o libkeccak-tiny.$so
$cc -Os -march=native -std=c11 -Wextra -Wpedantic -Wall -dynamic -shared keccak-tiny.c -o libkeccak-tiny-small.$so

View File

@ -0,0 +1,145 @@
/** libkeccak-tiny
*
* A single-file implementation of SHA-3 and SHAKE.
*
* Implementor: David Leon Gil
* License: CC0, attribution kindly requested. Blame taken too,
* but not liability.
*/
#include "keccak-tiny.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******** The Keccak-f[1600] permutation ********/
/*** Constants. ***/
static const uint8_t rho[24] = \
{ 1, 3, 6, 10, 15, 21,
28, 36, 45, 55, 2, 14,
27, 41, 56, 8, 25, 43,
62, 18, 39, 61, 20, 44};
static const uint8_t pi[24] = \
{10, 7, 11, 17, 18, 3,
5, 16, 8, 21, 24, 4,
15, 23, 19, 13, 12, 2,
20, 14, 22, 9, 6, 1};
static const uint64_t RC[24] = \
{1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
/*** Helper macros to unroll the permutation. ***/
#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
#define REPEAT6(e) e e e e e e
#define REPEAT24(e) REPEAT6(e e e e)
#define REPEAT5(e) e e e e e
#define FOR5(v, s, e) \
v = 0; \
REPEAT5(e; v += s;)
/*** Keccak-f[1600] ***/
static inline void keccakf(void* state) {
uint64_t* a = (uint64_t*)state;
uint64_t b[5] = {0};
uint64_t t = 0;
uint8_t x, y, i = 0;
REPEAT24(
// Theta
FOR5(x, 1,
b[x] = 0;
FOR5(y, 5,
b[x] ^= a[x + y]; ))
FOR5(x, 1,
FOR5(y, 5,
a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
// Rho and pi
t = a[1];
x = 0;
REPEAT24(b[0] = a[pi[x]];
a[pi[x]] = rol(t, rho[x]);
t = b[0];
x++; )
// Chi
FOR5(y,
5,
FOR5(x, 1,
b[x] = a[y + x];)
FOR5(x, 1,
a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
// Iota
a[0] ^= RC[i];
i++; )
}
/******** The FIPS202-defined functions. ********/
/*** Some helper macros. ***/
#define _(S) do { S } while (0)
#define FOR(i, ST, L, S) \
_(for (size_t i = 0; i < L; i += ST) { S; })
#define mkapply_ds(NAME, S) \
static inline void NAME(uint8_t* dst, \
const uint8_t* src, \
size_t len) { \
FOR(i, 1, len, S); \
}
#define mkapply_sd(NAME, S) \
static inline void NAME(const uint8_t* src, \
uint8_t* dst, \
size_t len) { \
FOR(i, 1, len, S); \
}
mkapply_ds(xorin, dst[i] ^= src[i]) // xorin
mkapply_sd(setout, dst[i] = src[i]) // setout
#define P keccakf
#define Plen 200
// Fold P*F over the full blocks of an input.
#define foldP(I, L, F) \
while (L >= rate) { \
F(a, I, rate); \
P(a); \
I += rate; \
L -= rate; \
}
/** The sponge-based hash construction. **/
static inline int hash(uint8_t* out, size_t outlen,
const uint8_t* in, size_t inlen,
size_t rate, uint8_t delim)
{
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen))
{
return -1;
}
uint8_t a[Plen] = {0};
// Absorb input.
foldP(in, inlen, xorin);
// Xor in the DS and pad frame.
a[inlen] ^= delim;
a[rate - 1] ^= 0x80;
// Xor in the last block.
xorin(a, in, inlen);
// Apply P
P(a);
// Squeeze output.
foldP(out, outlen, setout);
setout(a, out, outlen);
//TODO: c11 problem: replaced
//memset_s(a, 200, 0, 200);
//Reference: http://en.cppreference.com/w/c/string/byte/memset
memset(a, 0, 200);
return 0;
}
#include "define-macros.h"

View File

@ -0,0 +1,145 @@
/** libkeccak-tiny
*
* A single-file implementation of SHA-3 and SHAKE.
*
* Implementor: David Leon Gil
* License: CC0, attribution kindly requested. Blame taken too,
* but not liability.
*/
#include "keccak-tiny.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******** The Keccak-f[1600] permutation ********/
/*** Constants. ***/
static const uint8_t rho[24] = \
{ 1, 3, 6, 10, 15, 21,
28, 36, 45, 55, 2, 14,
27, 41, 56, 8, 25, 43,
62, 18, 39, 61, 20, 44};
static const uint8_t pi[24] = \
{10, 7, 11, 17, 18, 3,
5, 16, 8, 21, 24, 4,
15, 23, 19, 13, 12, 2,
20, 14, 22, 9, 6, 1};
static const uint64_t RC[24] = \
{1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
/*** Helper macros to unroll the permutation. ***/
#define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
#define REPEAT6(e) e e e e e e
#define REPEAT24(e) REPEAT6(e e e e)
#define REPEAT5(e) e e e e e
#define FOR5(v, s, e) \
v = 0; \
REPEAT5(e; v += s;)
/*** Keccak-f[1600] ***/
static inline void keccakf(void* state) {
uint64_t* a = (uint64_t*)state;
uint64_t b[5] = {0};
uint64_t t = 0;
uint8_t x, y;
for (int i = 0; i < 24; i++) {
// Theta
FOR5(x, 1,
b[x] = 0;
FOR5(y, 5,
b[x] ^= a[x + y]; ))
FOR5(x, 1,
FOR5(y, 5,
a[y + x] ^= b[(x + 4) % 5] ^ rol(b[(x + 1) % 5], 1); ))
// Rho and pi
t = a[1];
x = 0;
REPEAT24(b[0] = a[pi[x]];
a[pi[x]] = rol(t, rho[x]);
t = b[0];
x++; )
// Chi
FOR5(y,
5,
FOR5(x, 1,
b[x] = a[y + x];)
FOR5(x, 1,
a[y + x] = b[x] ^ ((~b[(x + 1) % 5]) & b[(x + 2) % 5]); ))
// Iota
a[0] ^= RC[i];
}
}
/******** The FIPS202-defined functions. ********/
/*** Some helper macros. ***/
#define _(S) do { S } while (0)
#define FOR(i, ST, L, S) \
_(for (size_t i = 0; i < L; i += ST) { S; })
#define mkapply_ds(NAME, S) \
static inline void NAME(uint8_t* dst, \
const uint8_t* src, \
size_t len) { \
FOR(i, 1, len, S); \
}
#define mkapply_sd(NAME, S) \
static inline void NAME(const uint8_t* src, \
uint8_t* dst, \
size_t len) { \
FOR(i, 1, len, S); \
}
mkapply_ds(xorin, dst[i] ^= src[i]) // xorin
mkapply_sd(setout, dst[i] = src[i]) // setout
#define P keccakf
#define Plen 200
// Fold P*F over the full blocks of an input.
#define foldP(I, L, F) \
while (L >= rate) { \
F(a, I, rate); \
P(a); \
I += rate; \
L -= rate; \
}
/** The sponge-based hash construction. **/
static inline int hash(uint8_t* out, size_t outlen,
const uint8_t* in, size_t inlen,
size_t rate, uint8_t delim)
{
if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen))
{
return -1;
}
uint8_t a[Plen] = {0};
// Absorb input.
foldP(in, inlen, xorin);
// Xor in the DS and pad frame.
a[inlen] ^= delim;
a[rate - 1] ^= 0x80;
// Xor in the last block.
xorin(a, in, inlen);
// Apply P
P(a);
// Squeeze output.
foldP(out, outlen, setout);
setout(a, out, outlen);
//TODO: c11 problem: replaced
//memset_s(a, 200, 0, 200);
//Reference: http://en.cppreference.com/w/c/string/byte/memset
memset(a, 0, 200);
return 0;
}
#include "define-macros.h"

View File

@ -0,0 +1,34 @@
#ifndef KECCAK_FIPS202_H
#define KECCAK_FIPS202_H
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdint.h>
#include <stdlib.h>
#define HASH224_SIZE 28
#define HASH256_SIZE 32
#define HASH384_SIZE 48
#define HASH512_SIZE 64
#define decshake(bits) \
int shake##bits(uint8_t*, size_t, const uint8_t*, size_t);
#define decsha3(bits) \
int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t);
#define deckeccak(bits) \
int keccak_##bits(uint8_t*, size_t, const uint8_t*, size_t);
decshake(128)
decshake(256)
decsha3(224)
decsha3(256)
decsha3(384)
decsha3(512)
deckeccak(224)
deckeccak(256)
deckeccak(384)
deckeccak(512)
#endif

View File

@ -0,0 +1,13 @@
# Keccak-tiny Library
# Options:
# * KECCAK_UNROLLED - Enable unrolled implementation (but disable main one).
HEADERS += \
$$PWD/keccak-tiny.h \
$$PWD/define-macros.h
contains(DEFINES, KECCAK_UNROLLED) {
SOURCES += $$PWD/keccak-tiny-unrolled.c
} else {
SOURCES += $$PWD/keccak-tiny.c
}

View File

@ -0,0 +1,23 @@
#!/usr/bin/env sh
cc=$(which clang-3.6||which gcc-4.9||which clang||which gcc)
case $(uname) in
Linux)
so=$(printf so)
ARFLAGS='rvs -o'
;;
Darwin)
so=$(printf dylib)
ARFLAGS=rcs
;;
MINGW*)
so=$(printf lib)
ARFLAGS='rcs -o'
;;
*)
so=$(printf so)
ARFLAGS='rvs -o'
;;
esac
# TODO: Fix the compiler to work with any of the above compilers $cc
gcc -c keccak-tiny.c
ar ${ARFLAGS} libkeccak-tiny.$so keccak-tiny.o

View File

@ -0,0 +1,21 @@
#include "urcrypt.h"
#include "util.h"
#include <keccak-tiny.h>
#define defkec(bits,byts) \
int urcrypt_keccak_##bits(const uint8_t *message, size_t length, \
uint8_t out[byts]) \
{ \
if ( 0 == keccak_##bits(out, byts, message, length) ) { \
urcrypt__reverse(byts, out); \
return 0; \
} \
else { \
return -1; \
}\
}
defkec(224, 28)
defkec(256, 32)
defkec(384, 48)
defkec(512, 64)

View File

@ -220,4 +220,17 @@ int urcrypt_scrypt(const uint8_t *passwd,
uint32_t p,
size_t outlen,
uint8_t *out);
int urcrypt_keccak_224(const uint8_t *message,
size_t length,
uint8_t out[28]);
int urcrypt_keccak_256(const uint8_t *message,
size_t length,
uint8_t out[32]);
int urcrypt_keccak_384(const uint8_t *message,
size_t length,
uint8_t out[48]);
int urcrypt_keccak_512(const uint8_t *message,
size_t length,
uint8_t out[64]);
#endif