Skip to content

Commit b3eb150

Browse files
committed
cryptolib: Add selftest-heap
1 parent 9143a5c commit b3eb150

File tree

9 files changed

+1195
-0
lines changed

9 files changed

+1195
-0
lines changed

cryptolib/selftest-heap/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
*.swp
3+
main

cryptolib/selftest-heap/Makefile

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#-------------------------------------------------------------------------
2+
# NMAKE-Makefile
3+
#-------------------------------------------------------------------------
4+
PROJ=dynamic memory tracking example programs
5+
UTIL1=dmt-utils
6+
PROG1=dmt-realloc
7+
PROG2=dmt-access
8+
9+
#-------------------------------------------------------------------------
10+
# Compiler-Flags General
11+
#-------------------------------------------------------------------------
12+
CC := gcc
13+
CFLAGS := -g -O0 -Wall -Werror
14+
LDD := ldd
15+
LIBS :=
16+
INCLUDE := -I.
17+
ifeq ($(MAKECMDGOALS),debug)
18+
CFLAGS += -DDEBUG
19+
endif
20+
21+
#-------------------------------------------------------------------------
22+
# Objects and Output
23+
#-------------------------------------------------------------------------
24+
DIR_OUTPUT := bin
25+
DIR_UTILS := $(DIR_OUTPUT)/utils
26+
DIR_PROGS := $(DIR_OUTPUT)/programs
27+
UTILS := $(DIR_UTILS)/$(UTIL1)
28+
UTILS_OBJ := $(patsubst %, %.o, $(UTILS))
29+
PROGRAMS := $(DIR_PROGS)/$(PROG1) $(DIR_PROGS)/$(PROG2)
30+
31+
#-------------------------------------------------------------------------
32+
# Main-Targets
33+
#-------------------------------------------------------------------------
34+
.PHONY: all help
35+
36+
all: output $(UTILS) $(PROGRAMS)
37+
debug: all
38+
39+
output:
40+
@if [ -d $(DIR_OUTPUT) ]; then \
41+
echo "dir $(DIR_OUTPUT) already exists."; \
42+
else \
43+
mkdir -p $(DIR_UTILS); \
44+
mkdir -p $(DIR_PROGS); \
45+
fi
46+
47+
clean:
48+
-$(RM) -r $(DIR_OUTPUT)
49+
50+
help:
51+
@echo
52+
@echo "$(PROJ)"
53+
@echo
54+
@echo "make [all]"
55+
@echo "make [help|clean]"
56+
@echo
57+
@echo "Targets : help - Shows this text."
58+
@echo " all - Compiles the project."
59+
@echo " debug - Enable debug output."
60+
@echo " clean - Cleans up output files."
61+
@echo
62+
63+
#-------------------------------------------------------------------------
64+
# Sub-Targets
65+
#-------------------------------------------------------------------------
66+
67+
# build utils
68+
$(DIR_UTILS)/%: %.c
69+
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@.o
70+
71+
# build programs
72+
$(DIR_PROGS)/%: %.c
73+
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@.o
74+
$(CC) $(INCLUDE) -o $@ $@.o $(UTILS_OBJ) $(LIBS)
75+
$(LDD) $@
76+

cryptolib/selftest-heap/dmt-access.c

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/************************************************************************
2+
* Copyright (C) 2017-2018 IAIK TU Graz and Fraunhofer AISEC
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
***********************************************************************/
17+
18+
/**
19+
* @file dmt-access.c
20+
* @brief Dynamic memory tracking simple access selftest.
21+
* @license This project is released under the GNU GPLv3+ License.
22+
* @author See AUTHORS file.
23+
* @version 0.3
24+
*/
25+
26+
/***********************************************************************/
27+
28+
#include <stdlib.h>
29+
#include <stdio.h>
30+
#include <unistd.h>
31+
#include <time.h>
32+
#include "dmt-utils.h"
33+
34+
/***********************************************************************/
35+
36+
#define LARGE_INIT 524288
37+
38+
/***********************************************************************/
39+
40+
void help();
41+
42+
/***********************************************************************/
43+
44+
uint16_t ALLOC_TYPE = 0;
45+
uint16_t USE_ALTERNATIVE = 0;
46+
47+
/***********************************************************************/
48+
49+
/***
50+
* Main function.
51+
*
52+
* @param argc number of command line arguments
53+
* @param argv command line arguments
54+
* @return 0=success, else=error
55+
*/
56+
int main(int argc, char **argv)
57+
{
58+
/* init */
59+
int err = 1;
60+
uint8_t key = 0;
61+
FILE *kfile = NULL;
62+
unsigned char str[2];
63+
void *obj = NULL;
64+
size_t objsize = 0;
65+
char lookup = 0;
66+
67+
/* check args */
68+
if (argc != 4) {
69+
help();
70+
return (1);
71+
}
72+
73+
/* get cmdline args */
74+
sscanf(argv[1], "%hu", &ALLOC_TYPE);
75+
sscanf(argv[2], "%hu", &USE_ALTERNATIVE);
76+
77+
/* read key */
78+
kfile = fopen(argv[3], "r");
79+
if (!kfile) {
80+
printf("[Error] Unable to open key file!\n");
81+
return (1);
82+
}
83+
if (fread(&str, 1, 2, kfile) != 2) {
84+
printf("[Error] Unable to read key file!\n");
85+
return (1);
86+
}
87+
key = (str[0] % 32 + 9) % 25;
88+
fclose(kfile);
89+
90+
/* ini rnd */
91+
srand(getpid() + time(NULL));
92+
93+
/* allocate object */
94+
objsize = LARGE_INIT;
95+
obj = dmt_allocate(objsize, ALLOC_TYPE, USE_ALTERNATIVE);
96+
if (INVALIDPTR(obj)) {
97+
printf("[Error] Unable to allocate object!\n");
98+
return (1);
99+
}
100+
101+
/* debug info */
102+
#ifdef DEBUG
103+
printf("Max rand() value: %u\n", RAND_MAX);
104+
printf("Allocate type: %s\n", (ALLOC_TYPE ? "mmap" : "malloc"));
105+
printf("Use alternative: %s\n", (USE_ALTERNATIVE ? "yes" : "no"));
106+
printf("Key nibble: %x\n", key);
107+
printf("Object size: %zu\n", objsize);
108+
printf("Object address: %0*"PRIxPTR"\n", (int)(sizeof(void*) * 2), (uintptr_t)obj);
109+
#endif
110+
111+
/* key-dependent data access */
112+
/* triggers H_pos(b), H_addr */
113+
/* expected leak stats: */
114+
/* difference: 1 */
115+
/* data leak generic: 1 */
116+
/* data leak specific: 1 (key byte nibble high) */
117+
lookup = ((uint8_t*)obj)[key];
118+
fprintf(stdin, "%d\n", lookup);
119+
120+
/* key-independent data access */
121+
/* expected leak stats: */
122+
/* difference = 1 */
123+
/* data leak generic = 0 */
124+
/* data leak specific = 0 */
125+
key = rand() % 16;
126+
lookup = ((uint8_t*)obj)[key];
127+
fprintf(stdin, "%d\n", lookup);
128+
129+
/* deallocate */
130+
err = dmt_deallocate(obj, objsize, ALLOC_TYPE);
131+
132+
/* done */
133+
return (err);
134+
}
135+
136+
/***********************************************************************/
137+
138+
/***
139+
* Print help text.
140+
*/
141+
void help()
142+
{
143+
printf("Usage:\n");
144+
printf(" dmt-access <type> <alt> <key>\n\n");
145+
printf(" <type> ....... 0: malloc & co, else: mmap & co\n");
146+
printf(" <alt> ........ 0: standard (malloc,mmap private), else: alternative (calloc,mmap shared)\n");
147+
printf(" <key file> ... file containing secret key byte as hex string\n");
148+
}
149+

0 commit comments

Comments
 (0)