Compare commits

...

12 Commits

Author SHA1 Message Date
a0d94c680b Remove requires 2024-04-22 13:27:27 +02:00
cb7dff3ffc Update libjade 2024-04-22 13:24:39 +02:00
ff639fdd78 Add imports 2024-04-22 11:20:28 +02:00
52df968d73 Spill variables 2024-04-22 11:13:41 +02:00
dfc00d5bd6 Return pubkey 2024-04-22 11:05:39 +02:00
154c63e7c2 Initialize key_id 2024-04-22 10:58:26 +02:00
00484347f1 Use actual randombytes implementation 2024-04-22 10:56:26 +02:00
ef99cec0b6 Add keygen 2024-04-22 10:48:04 +02:00
c3b45d7f63 Fix futex wake 2024-04-22 09:38:14 +02:00
0a86411e46 Fix bugs 2024-04-22 09:30:14 +02:00
1a378c2e0c Add correct version of libjade 2024-04-19 16:04:30 +02:00
eb1413c398 Remove libjade 2024-04-19 16:04:00 +02:00
4 changed files with 112 additions and 47 deletions

2
.gitmodules vendored
View File

@@ -1,3 +1,3 @@
[submodule "libjade"]
path = libjade
url = https://github.com/Rixxc/libjade.git
url = https://github.com/formosa-crypto/libjade.git

Submodule libjade updated: 43dbe822aa...6755e21624

View File

@@ -1,25 +1,73 @@
from Jade require "crypto_scalarmult/curve25519/amd64/mulx/curve25519.jinc"
from Jade require "crypto_scalarmult/curve25519/amd64/mulx/scalarmult.jazz"
export fn agent_start(reg u64 shared_mem sync_mem) {
inline fn extract_ipc_id(reg u64 sync_mem) -> reg u64 {
reg u64 id;
id = (u64)[sync_mem + 8];
return id;
}
inline fn generate_key(reg u64 shared_mem sync_mem private_mem key_id) -> reg u64 {
inline int i;
stack u8[32] key;
reg u64 addr idptr pkptr;
key = #randombytes(key);
key[0] &= 248;
key[31] &= 127;
key[31] |= 64;
key_id += 1;
addr = key_id * 32;
addr += private_mem;
for i=0 to 4 {
(u64)[private_mem + i * 8] = key[u64 i];
}
idptr = (u64)[sync_mem + 16];
idptr += shared_mem;
(u64)[idptr] = key_id;
pkptr = (u64)[sync_mem + 24];
pkptr += shared_mem;
() = #spill(key_id);
jade_scalarmult_curve25519_amd64_mulx_base(pkptr, private_mem);
() = #unspill(key_id);
return key_id;
}
export fn agent_start(reg u64 shared_mem sync_mem private_mem) {
stack u8[8] unused;
reg u64 outptr;
reg u64 spill_sync_mem spill_shared_mem;
reg u64 key_id;
spill_sync_mem = sync_mem;
spill_shared_mem = shared_mem;
key_id = 0;
() = #spill(shared_mem, sync_mem, private_mem);
while (true) {
unused = unused;
sync_mem = spill_sync_mem;
() = #unspill(sync_mem);
unused = #read(unused, sync_mem);
sync_mem = spill_sync_mem;
shared_mem = spill_shared_mem;
() = #unspill(shared_mem, sync_mem, private_mem);
outptr = (u64)[sync_mem + 8];
outptr = shared_mem + outptr;
reg u64 id;
(u64)[outptr] = 0x1337;
id = extract_ipc_id(sync_mem);
if (id == 0) {
key_id = generate_key(shared_mem, sync_mem, private_mem, key_id);
}
() = #unspill(sync_mem);
unused = #write(unused, sync_mem);
}

View File

@@ -3,33 +3,48 @@
#if defined(__linux__)
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/random.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/futex.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/random.h>
#include <sys/syscall.h>
#include <unistd.h>
// uint8_t *__jasmin_syscall_randombytes__(uint8_t *_x, uint64_t xlen) {
// int i;
// uint8_t *x = _x;
//
// printf("%lx:\n", x);
//
// for (i = 0; i < xlen; i++) {
// printf("%02X", _x[i]);
// }
// printf("\n");
//
// return _x;
// }
//
uint8_t* __jasmin_syscall_randombytes__(uint8_t* _x, uint64_t xlen)
{
int i;
uint8_t* x = _x;
printf("%lx:\n", x);
while (xlen > 0) {
if (xlen < 1048576) i = xlen; else i = 1048576;
for (i = 0; i < xlen; i++)
{
printf("%02X", _x[i]);
i = getrandom(x,i,0);
if (i < 1) {
sleep(1);
continue;
}
x += i;
xlen -= i;
}
printf("\n");
return _x;
}
@@ -54,15 +69,16 @@ uint8_t* __jasmin_syscall_randombytes__(uint8_t* _x, uint64_t xlen)
// }
// }
uint64_t __jasmin_syscall_open__(uint8_t* x, uint64_t xlen)
{
uint8_t* mem = (uint8_t*)mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0);
uint64_t __jasmin_syscall_open__(uint8_t *x, uint64_t xlen) {
uint8_t *mem =
(uint8_t *)mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, 3, 0);
if (mem == NULL) {
puts("mem error");
exit(1);
}
uintptr_t addr = (uintptr_t)(mem - 4096);
void* futex_mem = mmap((void*)addr, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, 4, 0);
void *futex_mem =
mmap((void *)addr, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, 4, 0);
if ((uintptr_t)futex_mem != addr) {
puts("mem error 2");
exit(1);
@@ -88,10 +104,9 @@ uint64_t __jasmin_syscall_open__(uint8_t* x, uint64_t xlen)
// return _x;
// }
uint8_t* __jasmin_syscall_read__(uint8_t* _x, uint64_t xlen, uint64_t fd)
{
uint8_t *__jasmin_syscall_read__(uint8_t *_x, uint64_t xlen, uint64_t fd) {
uint32_t *addr = (uint32_t *)(uintptr_t)fd;
long ret = syscall(SYS_futex, addr, FUTEX_WAIT, 0x1, NULL, NULL, 0);
long ret = syscall(SYS_futex, addr, FUTEX_WAIT, 0, NULL, NULL, 0);
if (ret == -1) {
printf("Agent futex error: %d\n", errno);
perror("futex");
@@ -100,11 +115,12 @@ uint8_t* __jasmin_syscall_read__(uint8_t* _x, uint64_t xlen, uint64_t fd)
return _x;
}
uint8_t* __jasmin_syscall_write__(uint8_t* _x, uint64_t xlen, uint64_t fd)
{
uint8_t *__jasmin_syscall_write__(uint8_t *_x, uint64_t xlen, uint64_t fd) {
uint32_t *addr = (uint32_t *)(uintptr_t)fd;
*addr = 1;
syscall(SYS_futex, addr, FUTEX_WAKE, 1, NULL, NULL, 0);
int woken_up = 0;
while (woken_up == 0) {
woken_up = syscall(SYS_futex, addr, FUTEX_WAKE, 1, NULL, NULL, 0);
}
return _x;
}
@@ -133,7 +149,8 @@ uint8_t* __jasmin_syscall_write__(uint8_t* _x, uint64_t xlen, uint64_t fd)
#include <stdlib.h>
#if !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)
#if !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
__MAC_OS_X_VERSION_MIN_REQUIRED >= 101200)
#error "macOS version not supported (>= 10.12)"
#endif