Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cryptolib: Add selftest-heap #34

Open
wants to merge 1 commit into
base: update-pin-to-3-11-97998
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cryptolib/selftest-heap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin/
*.swp
main
76 changes: 76 additions & 0 deletions cryptolib/selftest-heap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#-------------------------------------------------------------------------
# NMAKE-Makefile
#-------------------------------------------------------------------------
PROJ=dynamic memory tracking example programs
UTIL1=dmt-utils
PROG1=dmt-realloc
PROG2=dmt-access

#-------------------------------------------------------------------------
# Compiler-Flags General
#-------------------------------------------------------------------------
CC := gcc
CFLAGS := -g -O0 -Wall -Werror
LDD := ldd
LIBS :=
INCLUDE := -I.
ifeq ($(MAKECMDGOALS),debug)
CFLAGS += -DDEBUG
endif

#-------------------------------------------------------------------------
# Objects and Output
#-------------------------------------------------------------------------
DIR_OUTPUT := bin
DIR_UTILS := $(DIR_OUTPUT)/utils
DIR_PROGS := $(DIR_OUTPUT)/programs
UTILS := $(DIR_UTILS)/$(UTIL1)
UTILS_OBJ := $(patsubst %, %.o, $(UTILS))
PROGRAMS := $(DIR_PROGS)/$(PROG1) $(DIR_PROGS)/$(PROG2)

#-------------------------------------------------------------------------
# Main-Targets
#-------------------------------------------------------------------------
.PHONY: all help

all: output $(UTILS) $(PROGRAMS)
debug: all

output:
@if [ -d $(DIR_OUTPUT) ]; then \
echo "dir $(DIR_OUTPUT) already exists."; \
else \
mkdir -p $(DIR_UTILS); \
mkdir -p $(DIR_PROGS); \
fi

clean:
-$(RM) -r $(DIR_OUTPUT)

help:
@echo
@echo "$(PROJ)"
@echo
@echo "make [all]"
@echo "make [help|clean]"
@echo
@echo "Targets : help - Shows this text."
@echo " all - Compiles the project."
@echo " debug - Enable debug output."
@echo " clean - Cleans up output files."
@echo

#-------------------------------------------------------------------------
# Sub-Targets
#-------------------------------------------------------------------------

# build utils
$(DIR_UTILS)/%: %.c
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o [email protected]

# build programs
$(DIR_PROGS)/%: %.c
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o [email protected]
$(CC) $(INCLUDE) -o $@ [email protected] $(UTILS_OBJ) $(LIBS)
$(LDD) $@

10 changes: 10 additions & 0 deletions cryptolib/selftest-heap/data_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -e

OPTIONS="--phase1 --phase2 --export --parallel"
export RESULTDIR=results

pushd ${BASH_SOURCE%/*}
./framework.sh ${OPTIONS} basic 8
popd
149 changes: 149 additions & 0 deletions cryptolib/selftest-heap/dmt-access.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/************************************************************************
* Copyright (C) 2017-2018 IAIK TU Graz and Fraunhofer AISEC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
***********************************************************************/

/**
* @file dmt-access.c
* @brief Dynamic memory tracking simple access selftest.
* @license This project is released under the GNU GPLv3+ License.
* @author See AUTHORS file.
* @version 0.3
*/

/***********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include "dmt-utils.h"

/***********************************************************************/

#define LARGE_INIT 524288

/***********************************************************************/

void help();

/***********************************************************************/

uint16_t ALLOC_TYPE = 0;
uint16_t USE_ALTERNATIVE = 0;

/***********************************************************************/

/***
* Main function.
*
* @param argc number of command line arguments
* @param argv command line arguments
* @return 0=success, else=error
*/
int main(int argc, char **argv)
{
/* init */
int err = 1;
uint8_t key = 0;
FILE *kfile = NULL;
unsigned char str[2];
void *obj = NULL;
size_t objsize = 0;
char lookup = 0;

/* check args */
if (argc != 4) {
help();
return (1);
}

/* get cmdline args */
sscanf(argv[1], "%hu", &ALLOC_TYPE);
sscanf(argv[2], "%hu", &USE_ALTERNATIVE);

/* read key */
kfile = fopen(argv[3], "r");
if (!kfile) {
printf("[Error] Unable to open key file!\n");
return (1);
}
if (fread(&str, 1, 2, kfile) != 2) {
printf("[Error] Unable to read key file!\n");
return (1);
}
key = (str[0] % 32 + 9) % 25;
fclose(kfile);

/* ini rnd */
srand(getpid() + time(NULL));

/* allocate object */
objsize = LARGE_INIT;
obj = dmt_allocate(objsize, ALLOC_TYPE, USE_ALTERNATIVE);
if (INVALIDPTR(obj)) {
printf("[Error] Unable to allocate object!\n");
return (1);
}

/* debug info */
#ifdef DEBUG
printf("Max rand() value: %u\n", RAND_MAX);
printf("Allocate type: %s\n", (ALLOC_TYPE ? "mmap" : "malloc"));
printf("Use alternative: %s\n", (USE_ALTERNATIVE ? "yes" : "no"));
printf("Key nibble: %x\n", key);
printf("Object size: %zu\n", objsize);
printf("Object address: %0*"PRIxPTR"\n", (int)(sizeof(void*) * 2), (uintptr_t)obj);
#endif

/* key-dependent data access */
/* triggers H_pos(b), H_addr */
/* expected leak stats: */
/* difference: 1 */
/* data leak generic: 1 */
/* data leak specific: 1 (key byte nibble high) */
lookup = ((uint8_t*)obj)[key];
fprintf(stdin, "%d\n", lookup);

/* key-independent data access */
/* expected leak stats: */
/* difference = 1 */
/* data leak generic = 0 */
/* data leak specific = 0 */
key = rand() % 16;
lookup = ((uint8_t*)obj)[key];
fprintf(stdin, "%d\n", lookup);

/* deallocate */
err = dmt_deallocate(obj, objsize, ALLOC_TYPE);

/* done */
return (err);
}

/***********************************************************************/

/***
* Print help text.
*/
void help()
{
printf("Usage:\n");
printf(" dmt-access <type> <alt> <key>\n\n");
printf(" <type> ....... 0: malloc & co, else: mmap & co\n");
printf(" <alt> ........ 0: standard (malloc,mmap private), else: alternative (calloc,mmap shared)\n");
printf(" <key file> ... file containing secret key byte as hex string\n");
}

Loading