Skip to content

Commit

Permalink
make TrustInSoft entrypoints for other interfaces
Browse files Browse the repository at this point in the history
update parameters in gimli_hash.h and gimli_aead.h

update example variable names to more closely match interfaces
  • Loading branch information
chrisnc committed Oct 18, 2021
1 parent ac58eef commit 02c21a6
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 82 deletions.
10 changes: 10 additions & 0 deletions .trustinsoft/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Import("env")

tis_env = env.Clone()
tis_env.Append(CPPPATH="stubs")

tis_env.Program("hash.c")
tis_env.Program("x25519.c")
tis_env.Program("keygen.c")
tis_env.Program("sign.c")
tis_env.Program("verify.c")
9 changes: 6 additions & 3 deletions .trustinsoft/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"files": [
"../src/fe.c",
"../src/gimli.c",
"../src/gimli_aead.c",
"../src/gimli_common.c",
"../src/gimli_hash.c",
"../src/sign.c",
"../src/x25519.c",
"unknown_random.c"
"../src/x25519.c"
],
"cpp-extra-args": "-I../include -DLITH_VECTORIZE=0 -DLITH_LITTLE_ENDIAN=0 -DLITH_X25519_WBITS=32"
"cpp-extra-args": "-I../include -DLITH_VECTORIZE=0 -DLITH_LITTLE_ENDIAN=0 -DLITH_X25519_WBITS=32",
"val-profile": "analyzer",
"no-results": true,
"slevel": 1024
}
29 changes: 17 additions & 12 deletions .trustinsoft/config.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
[
{
"name": "Hash",
"include": "base.json",
"files": ["hash.c"]
},
{
"name": "X25519",
"include": "base.json",
"files": ["x25519.c"]
},
{
"name": "Keygen",
"include": "base.json",
"files": ["keygen.c"]
},
{
"name": "Sign",
"include": "base.json",
"val-profile": "analyzer",
"no-results": true,
"slevel": 1024,
"files": [
"sign.c"
]
"files": ["sign.c"]
},
{
"name": "Verify",
"include": "base.json",
"val-profile": "analyzer",
"no-results": true,
"slevel": 1024,
"files": [
"verify.c"
]
"files": ["verify.c"]
}
]
12 changes: 12 additions & 0 deletions .trustinsoft/hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <lithium/gimli_hash.h>

#include <tis_builtin.h>

int main(void)
{
unsigned char h[1024], m[1024];
tis_make_unknown(m, sizeof m);
size_t hlen = tis_unsigned_long_interval(0, sizeof h);
size_t mlen = tis_unsigned_long_interval(0, sizeof m);
gimli_hash(h, hlen, m, mlen);
}
15 changes: 15 additions & 0 deletions .trustinsoft/keygen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <lithium/sign.h>

#include <tis_builtin.h>

void lith_random_bytes(unsigned char *buf, size_t len)
{
tis_make_unknown(buf, len);
}

int main(void)
{
unsigned char public_key[LITH_SIGN_PUBLIC_KEY_LEN],
secret_key[LITH_SIGN_SECRET_KEY_LEN];
lith_sign_keygen(public_key, secret_key);
}
9 changes: 4 additions & 5 deletions .trustinsoft/sign.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include <lithium/random.h>
#include <lithium/sign.h>

#include <tis_builtin.h>

int main(void)
{
unsigned char public_key[LITH_SIGN_PUBLIC_KEY_LEN],
secret_key[LITH_SIGN_SECRET_KEY_LEN], sig[LITH_SIGN_LEN], msg[1024];
lith_sign_keygen(public_key, secret_key);
lith_random_bytes(msg, sizeof msg);
unsigned char sig[LITH_SIGN_LEN], msg[1024],
secret_key[LITH_SIGN_SECRET_KEY_LEN];
tis_make_unknown(msg, sizeof msg);
tis_make_unknown(secret_key, sizeof secret_key);
size_t msglen = tis_unsigned_long_interval(0, sizeof msg);
lith_sign_create(sig, msg, msglen, secret_key);
}
27 changes: 27 additions & 0 deletions .trustinsoft/stubs/tis_builtin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef STUBS_TIS_BUILTIN_H
#define STUBS_TIS_BUILTIN_H

#include <lithium/random.h>

/*
* Trivial versions of tis_builtin.h functions that the examples use, that use
* random data rather than data generalization.
*/

static inline int tis_make_unknown(void *p, unsigned long len)
{
lith_random_bytes(p, len);
return 0;
}

static inline unsigned long tis_unsigned_long_interval(unsigned long min_,
unsigned long max_)
{
unsigned long range = max_ - min_;
unsigned long rand;
lith_random_bytes((unsigned char *)&rand, sizeof rand);
rand %= range;
return min_ + range;
}

#endif // STUBS_TIS_BUILTIN_H
8 changes: 0 additions & 8 deletions .trustinsoft/unknown_random.c

This file was deleted.

11 changes: 5 additions & 6 deletions .trustinsoft/verify.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include <lithium/random.h>
#include <lithium/sign.h>

#include <tis_builtin.h>

int main(void)
{
unsigned char public_key[LITH_SIGN_PUBLIC_KEY_LEN],
secret_key[LITH_SIGN_SECRET_KEY_LEN], sig[LITH_SIGN_LEN], msg[1024];
unsigned char sig[LITH_SIGN_LEN], msg[1024],
public_key[LITH_SIGN_PUBLIC_KEY_LEN];
tis_make_unknown(sig, sizeof sig);
tis_make_unknown(msg, sizeof msg);
tis_make_unknown(public_key, sizeof public_key);
size_t msglen = tis_unsigned_long_interval(0, sizeof msg);
lith_sign_keygen(public_key, secret_key);
lith_random_bytes(msg, sizeof msg);
lith_random_bytes(sig, sizeof sig);
return !lith_sign_verify(sig, msg, msglen, public_key);
}
11 changes: 11 additions & 0 deletions .trustinsoft/x25519.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <lithium/x25519.h>

#include <tis_builtin.h>

int main(void)
{
unsigned char out[X25519_LEN], scalar[X25519_LEN], point[X25519_LEN];
tis_make_unknown(scalar, sizeof scalar);
tis_make_unknown(point, sizeof point);
x25519(out, scalar, point);
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![TrustInSoft CI](https://ci.trust-in-soft.com/projects/teslamotors/liblithium.svg?branch=main)](https://ci.trust-in-soft.com/projects/teslamotors/liblithium)

![Lithium](lithium.svg)

# liblithium
Expand Down
9 changes: 8 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def build_with_env(path, env, test=True, measure_size=False):
"$SIZE $SOURCES",
)
)
return lith_env


if platform.system() == "Windows":
Expand Down Expand Up @@ -233,7 +234,13 @@ else:

host_env.Append(CCFLAGS=arch_flag, LINKFLAGS=arch_flag)

build_with_env("dist", host_env)
lith_env = build_with_env("dist", host_env)
SConscript(
dirs=".trustinsoft",
variant_dir="dist/trustinsoft",
exports={"env": lith_env},
duplicate=False,
)

env16 = host_env.Clone()
env16.Append(CPPDEFINES={"LITH_X25519_WBITS": 16})
Expand Down
14 changes: 7 additions & 7 deletions examples/gimli-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ static ssize_t hash_fd(int fd)
gimli_hash_state state;
gimli_hash_init(&state);

static unsigned char buf[4096];
static unsigned char m[4096];
ssize_t nread;

while ((nread = read(fd, buf, sizeof buf)) > 0)
while ((nread = read(fd, m, sizeof m)) > 0)
{
gimli_hash_update(&state, buf, (size_t)nread);
gimli_hash_update(&state, m, (size_t)nread);
}

if (nread == 0)
{
unsigned char output[GIMLI_HASH_DEFAULT_LEN];
gimli_hash_final(&state, output, sizeof output);
unsigned char h[GIMLI_HASH_DEFAULT_LEN];
gimli_hash_final(&state, h, sizeof h);

for (size_t i = 0; i < sizeof output; ++i)
for (size_t i = 0; i < sizeof h; ++i)
{
printf("%02hhx", output[i]);
printf("%02hhx", h[i]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/lith-sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ int main(int argc, char **argv)
goto cleanup;
}

static unsigned char buf[4096];
static unsigned char msg[4096];
ssize_t nread;

while ((nread = read(msgfd, buf, sizeof buf)) > 0)
while ((nread = read(msgfd, msg, sizeof msg)) > 0)
{
lith_sign_update(&state, buf, (size_t)nread);
lith_sign_update(&state, msg, (size_t)nread);
}

if (nread < 0)
Expand Down
6 changes: 3 additions & 3 deletions examples/lith-verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ int main(int argc, char **argv)
goto cleanup;
}

static unsigned char buf[4096];
static unsigned char msg[4096];
ssize_t nread;

while ((nread = read(msgfd, buf, sizeof buf)) > 0)
while ((nread = read(msgfd, msg, sizeof msg)) > 0)
{
lith_sign_update(&state, buf, (size_t)nread);
lith_sign_update(&state, msg, (size_t)nread);
}

if (nread < 0)
Expand Down
14 changes: 7 additions & 7 deletions hydro/examples/hydro-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ static ssize_t hash_fd(int fd)
static const char ctx[hydro_hash_CONTEXTBYTES] = {0};
hydro_hash_init(&state, ctx, NULL);

static unsigned char buf[4096];
static unsigned char m[4096];
ssize_t nread;

while ((nread = read(fd, buf, sizeof buf)) > 0)
while ((nread = read(fd, m, sizeof m)) > 0)
{
hydro_hash_update(&state, buf, (size_t)nread);
hydro_hash_update(&state, m, (size_t)nread);
}

if (nread == 0)
{
unsigned char output[hydro_hash_BYTES];
hydro_hash_final(&state, output, sizeof output);
unsigned char h[hydro_hash_BYTES];
hydro_hash_final(&state, h, sizeof h);

for (size_t i = 0; i < sizeof output; ++i)
for (size_t i = 0; i < sizeof h; ++i)
{
printf("%02hhx", output[i]);
printf("%02hhx", h[i]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions hydro/examples/hydro-sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ int main(int argc, char **argv)
goto cleanup;
}

static unsigned char buf[4096];
static unsigned char msg[4096];
ssize_t nread;

while ((nread = read(msgfd, buf, sizeof buf)) > 0)
while ((nread = read(msgfd, msg, sizeof msg)) > 0)
{
hydro_sign_update(&state, buf, (size_t)nread);
hydro_sign_update(&state, msg, (size_t)nread);
}

if (nread < 0)
Expand Down
6 changes: 3 additions & 3 deletions hydro/examples/hydro-verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ int main(int argc, char **argv)
goto cleanup;
}

static unsigned char buf[4096];
static unsigned char msg[4096];
ssize_t nread;

while ((nread = read(msgfd, buf, sizeof buf)) > 0)
while ((nread = read(msgfd, msg, sizeof msg)) > 0)
{
hydro_sign_update(&state, buf, (size_t)nread);
hydro_sign_update(&state, msg, (size_t)nread);
}

if (nread < 0)
Expand Down
11 changes: 6 additions & 5 deletions include/lithium/gimli_aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,29 @@ void gimli_aead_init(gimli_state *g,
const unsigned char n[GIMLI_AEAD_NONCE_LEN],
const unsigned char k[GIMLI_AEAD_KEY_LEN]);

void gimli_aead_update_ad(gimli_state *g, const unsigned char *ad, size_t len);
void gimli_aead_update_ad(gimli_state *g, const unsigned char *ad,
size_t adlen);

void gimli_aead_final_ad(gimli_state *g);

void gimli_aead_encrypt_update(gimli_state *g, unsigned char *c,
const unsigned char *m, size_t len);

void gimli_aead_encrypt_final(gimli_state *g, unsigned char *t, size_t len);
void gimli_aead_encrypt_final(gimli_state *g, unsigned char *t, size_t tlen);

void gimli_aead_decrypt_update(gimli_state *g, unsigned char *m,
const unsigned char *c, size_t len);

bool gimli_aead_decrypt_final(gimli_state *g, const unsigned char *t,
size_t len);
size_t tlen);

void gimli_aead_encrypt(unsigned char *c, unsigned char *t, size_t tlen,
const unsigned char *m, size_t mlen,
const unsigned char *m, size_t len,
const unsigned char *ad, size_t adlen,
const unsigned char n[GIMLI_AEAD_NONCE_LEN],
const unsigned char k[GIMLI_AEAD_KEY_LEN]);

bool gimli_aead_decrypt(unsigned char *m, const unsigned char *c, size_t clen,
bool gimli_aead_decrypt(unsigned char *m, const unsigned char *c, size_t len,
const unsigned char *t, size_t tlen,
const unsigned char *ad, size_t adlen,
const unsigned char n[GIMLI_AEAD_NONCE_LEN],
Expand Down
9 changes: 4 additions & 5 deletions include/lithium/gimli_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ typedef gimli_state gimli_hash_state;

void gimli_hash_init(gimli_hash_state *g);

void gimli_hash_update(gimli_hash_state *g, const unsigned char *input,
size_t len);
void gimli_hash_update(gimli_hash_state *g, const unsigned char *m, size_t len);

void gimli_hash_final(gimli_hash_state *g, unsigned char *output, size_t len);
void gimli_hash_final(gimli_hash_state *g, unsigned char *h, size_t len);

void gimli_hash(unsigned char *output, size_t output_len,
const unsigned char *input, size_t input_len);
void gimli_hash(unsigned char *h, size_t hlen, const unsigned char *m,
size_t mlen);

/* cffi:end */

Expand Down
Loading

0 comments on commit 02c21a6

Please sign in to comment.