Tuesday, January 22, 2019

Matlab Coder from Windows to *unix

One of the strange things about a platform is that people tend to support "the platform". If you ever get a look at a MATLAB Coder Makefile, you'll immediately see things like PERL is included due to the lack of grep on Windows.

I asked Mathworks if they could export both a Windows and *unix Makefile, but it's not supported: https://www.mathworks.com/matlabcentral/answers/440297-coder-create-a-makefile-for-linux-from-windows

The following script won't work for everyone, but it is a good start. Run "script.sh > Makefile" in the top directory of the Coder output and you'll compile to a static library.
Surprisingly (don't know why I am), Coder did a really good job of making C code that compiled on BSD-Lite, Solaris and AIX. I'm sure it'll work on Linux.


## change OUTPUT to be the proper name
echo '
#       User-modifiable options

ANSI_OPTS = -ansi -pedantic -Wno-long-long -fwrapv

#       User-modifiable options

UNIXTIME=$(shell date +%s)
WRTSIGNAL = ./signals/
WRTPARSERS = ./parsers/

# Compile for all memories available on the board (this sets $(MEMORIES))
# moved down to tools.

# Output directories
BIN = bin
OBJ = obj
SRC = src

echo '
#       Tools

# This is to generate SVN information into the binary.  0 is default.

# Tool suffix when cross-compiling
#CROSS_COMPILE = arm-elf-
#CROSS_COMPILE =arm-none-eabi-
# Compilation tools
SVNREV := $(shell svnversion -n . | sed -e "s/.*://" -e "s/\([0-9]*\).*/\1/" | grep "[0-9]" )
#degs compile target
MEMORIES = flash

ARFLAGS  = ruvs
CFLAGS  += -c $(ANSI_OPTS) -O0

#       Files

#VPATH is important for file location, which is basically the "INCLUDES"
VPATH += ./io
VPATH += ./examples

#C_OBJECTS = main.o

shopt -s nullglob
for file in *.cdo
    echo "C_source+=$file"

shopt -s nullglob
for file in *.cdo
    basefilename=$(echo "$file" | sed -e "s/c$//")
    #objectname = "$basefilename""o"
    #echo "$objectname"
    objectname="$basefilename""o" #create the .o list
    echo "C_OBJECTS+=$objectname"

echo '

all: clean $(BIN) $(OBJ) $(SRC) $(MEMORIES) 

$(BIN) $(OBJ) $(SRC):
    mkdir $@

define RULES
C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))

$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))

$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)
    $(CC) $(CFLAGS) -D$(1) -c -o $$@ $$<

$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile $(OBJ) $(BIN)
    $(CC) $(ASFLAGS) -D$(1) -c -o $$@ $$<


$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))
    -rm -f $(SRC)/*.s $(SRC)/*.i $(OBJ)/* $(BIN)/*
    -rm -f *.s
    -rm -f *.i
    -rm -f *.bc
    -rm -f a.out $(SRC)/*.out $(SRC)/*.out $(OBJ)/*.out $(BIN)/*.out $(BIN)/*.out $(WRTSIGNAL)/*.out $(WRTPARSERS)/*.out


Sunday, January 13, 2019

BASH script to update all SVN archives

I have a base root for my SVN repositories, such as:
In order to update everything, I made a script that will execute from any SVN repository and update all of the unrelated repos.

#Before anything else, set the PATH_SCRIPT variable
    pushd `dirname $0` > /dev/nullPATH_SCRIPT=`pwd -P`popd > /dev/null
    PROGNAME=${0##*/}; PROGVERSION=0.1.0 
## This program attempts to do a SVN update on all of the directories off an SVN_ROOT

SVN_ROOT=$(svn info | awk -F'/Root Path: (.*)/ { print $2 }' | xargs)
SVN_ROOT=$(dirname $SVN_ROOT)
echo "$SVN_ROOT"

for svndir in $SVN_ROOT/* ; do
    BRANCH=`svn info ${svndir} 2> /dev/null | grep "^URL:" | cut -d" " -f2 `
     if [ -z "${BRANCH}" ]
        #echo "Error: directory ${svndir} exists and is not part of a working copy."
        echo "skipping: ${svndir}"
        #echo "$svndir"
        svn update $svndir

Tuesday, August 28, 2018

Multiple timers in MacOS/FreeBSD without timer_t.

POSIX extensions strike again, as I did not have timer_t. dispatch functions allow multiple timers in C under MacOS.

int i = 0;
dispatch_queue_t queue;
dispatch_source_t timer1;
dispatch_source_t timer2;

void sigtrap(int sig)
    printf("CTRL-C received, exiting program\n");

void vector1(dispatch_source_t timer)
  printf("a: %d\n", i);
  if (i >= 20) 
void vector2(dispatch_source_t timer)
  printf("b: %d\n", i);
  if (i >= 20)  //at 20 count cancel the 

int main(int argc, const char* argv[]) {

 signal(SIGINT, &sigtrap);   //catch the cntl-c
 queue = dispatch_queue_create("timerQueue", 0);

 // Create dispatch timer source
 timer1 = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
 timer2 = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
 // Set block for dispatch source when catched events
 dispatch_source_set_event_handler(timer1, ^{vector1(timer1);});
 dispatch_source_set_event_handler(timer2, ^{vector2(timer2);});

 // Set block for dispatch source when cancel source
 dispatch_source_set_cancel_handler(timer1, ^{
 dispatch_source_set_cancel_handler(timer2, ^{

 dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC); // after 1 sec

 // Set timer
 dispatch_source_set_timer(timer1, start, NSEC_PER_SEC / 5, 0);  // 0.2 sec
 dispatch_source_set_timer(timer2, start, NSEC_PER_SEC / 2, 0);  // 0.5 sec

 return 0;

Monday, June 11, 2018

ISO rejection of Simon

I am a circuit designer by training, and not a cryptographer. One of the thing that I've found from working with cryptographers, is that I find them to be odd to work with by nature. It's most like because their products are seldom tangible, and therefore there is a lot of infighting.

In this article on the ISO rejection of SIMON, there's a quote regarding the NSA from Tomer Ashur: "They refused to motivate design choices they made such as the choice of matrices U, V, and W in Simon’s key schedule. Instead, they chose to personally attack some of the experts (including @hashbreaker, Orr Dunkelman and myself) as incompetent."

Well, I know the design choices behind U, W, V, which were related to slide attacks for some internal tool they had based on the rounds. I do not know anything about the tool, but Shor's mentioned this when I asked when I was writing my Simontool paper. The matrices in question with circuit implementation are in my simontool.supplemental.pdf. The question at hand is how U, W, and V where decided; however, I cannot answer that as I do not have their internal tools. Having said that, the circuits are beautiful, and perhaps someone will do a detailed analysis of the tradeoffs between each matrix for rounds.

Thursday, April 12, 2018

Two random words under MacOS

As MacOS doesn't have sort -R, if you want to randomize a list, it gets a bit more complicated.
In order to generate two random words, I'm currently using:
cat /usr/share/dict/words | awk 'BEGIN{srand();}{print rand()"\t"$0}' \
| sort -k1 -n | cut -f2- | awk 'NR <= 2 { print $1 }'

Wednesday, March 28, 2018

AES S-Box reference implementation.

One of the things that has always bothered me about academia is the "minimally publishable item". I was trying to find a reference AES circuit implementation, and I never found one, so I started writing one for me to use internally. I found the document to be so useful, I sent it out to see if anyone was interested in the tech report. Of course, no one was, so what I have is a very good text book chapter or a nice tech report that everyone seems to want, but no one wants to publish. I used to publish things on ece.gatech.edu, but they decided grad student work was not important enough to be persistent. Once I was postdoc, the same thing happened. I'm hedging now that github will be around long enough for this work to be useful. I now present:

A reference implementation of the AES S-Box: