From bc1592acb465545b56ea57a9d28aaf8ab6cdb0e4 Mon Sep 17 00:00:00 2001 From: Philipp Claves <pclaves@web.de> Date: Sat, 10 Dec 2011 20:09:08 +0100 Subject: [PATCH] ATMegaCard: Move keystore to portal application dir. Its only used there. --- ATMegaCard/Makefile | 2 +- .../{ => applications/portal}/keystore.c | 0 .../{ => applications/portal}/keystore.h | 0 ATMegaCard/eeprom_gen.rb | 74 +++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) rename ATMegaCard/{ => applications/portal}/keystore.c (100%) rename ATMegaCard/{ => applications/portal}/keystore.h (100%) create mode 100755 ATMegaCard/eeprom_gen.rb diff --git a/ATMegaCard/Makefile b/ATMegaCard/Makefile index cb0f900..4288b35 100644 --- a/ATMegaCard/Makefile +++ b/ATMegaCard/Makefile @@ -54,7 +54,7 @@ OPT = s # If there is more than one source file, append them above, or modify and # uncomment the following: -SRC = main.c software_serial.c ISO7816-card.c keystore.c rng.c ../common/sha1/sha1.c applications/portal/portal.c +SRC = main.c software_serial.c ISO7816-card.c rng.c ../common/sha1/sha1.c applications/portal/keystore.c applications/portal/portal.c # List Assembler source files here. # Make them always end in a capital .S. Files ending in a lowercase .s diff --git a/ATMegaCard/keystore.c b/ATMegaCard/applications/portal/keystore.c similarity index 100% rename from ATMegaCard/keystore.c rename to ATMegaCard/applications/portal/keystore.c diff --git a/ATMegaCard/keystore.h b/ATMegaCard/applications/portal/keystore.h similarity index 100% rename from ATMegaCard/keystore.h rename to ATMegaCard/applications/portal/keystore.h diff --git a/ATMegaCard/eeprom_gen.rb b/ATMegaCard/eeprom_gen.rb new file mode 100755 index 0000000..aa0e0db --- /dev/null +++ b/ATMegaCard/eeprom_gen.rb @@ -0,0 +1,74 @@ +#!/usr/bin/ruby + +KEYSTORE_OFFSET=127 +KEY_LENGTH=64 + +class IntelHexWriter + + RECORD_LENGTH=0x20 + + RECORD_TYPE_DATA = 0x00 + + def initialize(filename) + @file = File.open(filename, "w") + @offset = 0 + @record = "" + end + + public; def write_data(data) + while(data && data.length > 0) + bytesleft = RECORD_LENGTH - @record.length + @record += data[0, bytesleft] + data = data[bytesleft..-1] + + write_record() if (data && data.length > 0) + end + end + + private; def write_record() + r = (":%02X%04X%02X" % [@record.length, @offset, RECORD_TYPE_DATA]) + checksum = @record.length + (@offset & 0xFF) + (@offset >> 8) + RECORD_TYPE_DATA + r += @record.bytes.map{|b| checksum=(checksum+b) & 0xFF; "%02X" % b}.join() + r += "%02X" % ((((checksum & 0xFF) ^ 0xFF) + 1) & 0xFF) + r += "\r\n" + @file.write(r) + + @record = "" + @offset += RECORD_LENGTH + end + + public; def close() + write_record() if (@record.length > 0) + @file.write(":00000001FF\n") + @file.close + end + +end + +def hex2bin(hex) + c = hex.length / 2 + bin = ('.'.encode('ASCII-8BIT'))*c + (0...c).each{|i| + bin.setbyte(i,hex[i << 1, 2].hex) + } + return bin +end + +eeprom_file = ARGV[0] +keystring = ARGV[1] + +if ((!eeprom_file.kind_of?(String)) || (!keystring.kind_of?(String)) || keystring.length != (2*KEY_LENGTH)) + puts "Usage: #{$0} hexfilename keystring" + exit 1 +end + +print("Writing EEPROM key to #{eeprom_file}...") + +f = IntelHexWriter.new(eeprom_file) +f.write_data("\xFF"*KEYSTORE_OFFSET) +f.write_data("\x00") +f.write_data(hex2bin(keystring)) +f.write_data("\xFF"*KEY_LENGTH) +f.close + +puts("OK") \ No newline at end of file -- GitLab