Skip to content

Commit

Permalink
Add examples.
Browse files Browse the repository at this point in the history
 Fixes #1
  • Loading branch information
J08nY committed Feb 21, 2024
1 parent 0fc5796 commit 7882ea5
Show file tree
Hide file tree
Showing 13 changed files with 505 additions and 1 deletion.
10 changes: 9 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ collections:
tutorials:
output: true
permalink: /:collection/:name
examples:
output: true
permalink: /:collection/:name
defaults:
- scope:
path: ""
Expand All @@ -20,4 +23,9 @@ defaults:
path: ""
type: "tutorials"
values:
layout: "tutorials"
layout: "tutorials"
- scope:
path: ""
type: "examples"
values:
layout: "examples"
39 changes: 39 additions & 0 deletions _examples/01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
ct: no
---

```c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef uint8_t(* issue_t)(const uint32_t, const uint8_t, const uint8_t);

uint8_t issue(const uint32_t c, const uint8_t a, const uint8_t b) {
if(c) {
return a;
}
else {
return b;
}
}

int main(int argc, char *argv[]) {
uint32_t a = 2, b = 5, c = 0;
// c is our secret value, we read its content from the environment so the
// compiler cannot play tricks on us by inlining
if (argc >= 2) {
c = (uint32_t) strtol(argv[1], NULL, 16);
}
else {
fprintf(stderr, "USAGE: %s hex_value\n", argv[0]);
return 1;
}
// declarations and markup here

volatile issue_t func = issue;
printf("%d\n", func(c, a, b));

return 0;
}
```
35 changes: 35 additions & 0 deletions _examples/02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
ct: yes
---

```c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef uint8_t(* issue_t)(const uint32_t, const uint8_t, const uint8_t);

uint8_t issue(const uint32_t c, const uint8_t a, const uint8_t b) {
int d = !!c;
return d*a + (1-d)*b;
}

int main(int argc, char *argv[]) {
uint32_t a = 2, b = 5, c = 0;
// c is our secret value, we read its content from the environment so the
// compiler cannot play tricks on us by inlining
if (argc >= 2) {
c = (uint32_t) strtol(argv[1], NULL, 16);
}
else {
fprintf(stderr, "USAGE: %s hex_value\n", argv[0]);
return 1;
}
// declarations and markup here

volatile issue_t func = issue;
printf("%d", func(c, a, b));

return 0;
}
```
36 changes: 36 additions & 0 deletions _examples/03.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
ct: no
---

```c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef uint8_t(* issue_t)(const uint8_t *, const uint8_t *);

uint8_t issue(const uint8_t *a, const uint8_t *b) {
return a[b[0]];
}

int main(int argc, char *argv[]) {
uint8_t a[2] ={2, 5}, b[2] = {0};
// b is our secret value containing array, we read its content from the
// environment so the compiler cannot play tricks on us by inlining
if (argc >= 2) {
b[0] = (uint8_t) strtol(argv[1], NULL, 16);
}
else {
fprintf(stderr, "USAGE: %s hex_value\n", argv[0]);
return 1;
}
// make sure b[0] is either 0 or 1
b[0] = !!b[0];
// declarations and markup here

volatile issue_t func = issue;
printf("%d\n", func(a, b));

return 0;
}
```
36 changes: 36 additions & 0 deletions _examples/04.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
ct: yes
---

```c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

typedef uint8_t(* issue_t)(const uint8_t*, const uint8_t*);

uint8_t issue(const uint8_t *a, const uint8_t *b) {
return !b[0]*a[0] + b[0]*a[1];
}

int main(int argc, char *argv[]) {
uint8_t a[2] ={2, 5}, b[2] = {0};
// b is our secret value containing array, we read its content from the
// environment so the compiler cannot play tricks on us by inlining
if (argc >= 2) {
b[0] = (uint8_t) strtol(argv[1], NULL, 16);
}
else {
fprintf(stderr, "USAGE: %s hex_value\n", argv[0]);
return 1;
}
// make sure b[0] is either 0 or 1
b[0] = !!b[0];
// declarations and markup here

volatile issue_t func = issue;
printf("%d\n", func(a, b));

return 0;
}
```
45 changes: 45 additions & 0 deletions _examples/05.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
ct: no
---

```c
#include <stdint.h>
#include <stdio.h>
#include <string.h>

typedef int(* issue_t)(const uint64_t*, const uint64_t*);

#define THE_SIZE 9

int comparator(const uint64_t *a, const uint64_t *b) {
return (memcmp(a, b, sizeof(uint64_t) * THE_SIZE) == 0);
}

int issue(const uint64_t *a, const uint64_t *b) {
int ret = 0;
for (int i = 2;i < 3;i += 2) {
ret |= comparator(a,b);
}
return ret;
}

int main(int argc, char *argv[]){
uint64_t a[THE_SIZE] = {0}, b[THE_SIZE] = {0}, c[THE_SIZE] = {0};
// c is our secret value containing array, we read its content from the
// environment so the compiler cannot play tricks on us by inlining
if (argc >= 2) {
memset(c, (uint64_t) argv[1][0], THE_SIZE*sizeof(uint64_t));
}
else {
fprintf(stderr, "USAGE: %s hex_value\n", argv[0]);
return 1;
}
// declarations and markup here

volatile issue_t func = issue;
printf("%d\n", func(a, b));
printf("%d\n", func(a, c));

return 0;
}
```
79 changes: 79 additions & 0 deletions _examples/06.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
ct: yes
---

```c
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>

typedef void(* issue_t)(uint8_t*, size_t);

#if defined(__linux__) && defined(SYS_getrandom)
void issue(uint8_t *out, size_t outlen) {
while(outlen > 0) {
ssize_t ret = syscall(SYS_getrandom, out, outlen, 0);
if((ret == -1) && (errno == EINTR)) {
continue;
}
else if(ret == -1) {
fprintf(stderr, "Error while executing syscall getrandom\n");
abort();
}
out += ret;
outlen -= ret;
}
}
#else
void issue(uint8_t *out, size_t outlen) {
int fd = -1;
while(fd == -1) {
fd = open("/dev/urandom", O_RDONLY);
if((fd == -1) && (errno == EINTR)) {
continue;
}
else if(fd == -1) {
fprintf(stderr, "Error while opening /dev/urandom\n");
abort();
}
}

while(outlen > 0) {
ssize_t ret = read(fd, out, outlen);
if((ret == -1) && (errno == EINTR)) {
continue;
}
else if(ret == -1) {
close(fd);
fprintf(stderr, "Error while reading from /dev/urandom\n");
abort();
}

out += ret;
outlen -= ret;
}
close(fd);
}
#endif

int main(void) {
uint8_t out[1] = {0};
size_t size = 1;
// this test has no secret variables from the start, but the randomness from
// /dev/urandom or SYS_getrandom may be the input to some function generating
// a secret

// declarations and markup here

volatile issue_t func = issue;
func(out, size);

return 0;
}
```
64 changes: 64 additions & 0 deletions _examples/07.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
ct: no
---

```c
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef uint8_t(* issue_t)(const uint8_t, const uint8_t, const uint8_t, const uint8_t);

uint8_t issue(const uint8_t c, const uint8_t a, const uint8_t b, const uint8_t len) {
uint8_t d = !!c;
if(len <= 254) {
return d*a + (1-d)*b;
}
else {
if(c) {
return a;
}
else {
return b;
}
}
}

int main(int argc, char *argv[]) {
uint8_t a = 2, b = 5, c = 0;
// c is our secret value, we read its content from the environment so the
// compiler cannot play tricks on us by inlining
if (argc >= 2) {
c = (uint8_t) strtol(argv[1], NULL, 16);
}
else {
fprintf(stderr, "USAGE: %s hex_value\n", argv[0]);
return 1;
}
// declarations and markup here

uint8_t len = 1;

int fd = open("/dev/urandom", O_RDONLY);
if(fd != -1) {
ssize_t ret = read(fd, &len, 1);
close(fd);
if(ret == -1) {
fprintf(stderr, "Error while reading from /dev/urandom\n");
abort();
}
}
else {
fprintf(stderr, "Error while opening /dev/urandom\n");
abort();
}


volatile issue_t func = issue;
printf("%d\n", func(c, a, b, len));

return 0;
}
```
Loading

0 comments on commit 7882ea5

Please sign in to comment.