From 4bfe0875834a630e33c0d89054a90183aa47e5b2 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Wed, 19 Dec 2012 15:53:07 +0900 Subject: [PATCH] add test/factory_upgrade.py --- ChangeLog | 4 ++ src/Makefile.in | 2 +- test/factory_upgrade.py | 111 ++++++++++++++++++++++++++++++++++++++++ tool/gnuk_token.py | 13 ++++- 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100755 test/factory_upgrade.py diff --git a/ChangeLog b/ChangeLog index 3f1b2fb..5050e14 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2012-12-19 Niibe Yutaka + * test/factory_upgrade.py: New. + + * src/Makefile.in (USE_OPT): -O3 and -Os (was: -O2). + * tool/gnuk_token.py (gnuk_token.stop_gnuk, gnuk_token.mem_info) (gnuk_token.download, gnuk_token.execute) (gnuk_token.cmd_get_challenge) diff --git a/src/Makefile.in b/src/Makefile.in index 3f73460..3f5a094 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -11,7 +11,7 @@ BOARD_DIR=@BOARD_DIR@ # Compiler options here. ifeq ($(USE_OPT),) - USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16 + USE_OPT = -O3 -Os -ggdb -fomit-frame-pointer -falign-functions=16 endif # C++ specific options here (added to USE_OPT). diff --git a/test/factory_upgrade.py b/test/factory_upgrade.py new file mode 100755 index 0000000..2234a92 --- /dev/null +++ b/test/factory_upgrade.py @@ -0,0 +1,111 @@ +#! /usr/bin/python + +""" +factory_upgrade.py - a tool to install another firmware for Gnuk Token + which is just shipped from factory + +Copyright (C) 2012 Free Software Initiative of Japan +Author: NIIBE Yutaka + +This file is a part of Gnuk, a GnuPG USB Token implementation. + +Gnuk 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. + +Gnuk 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 . +""" + +from gnuk_token import * +import sys, binascii, time, os + +DEFAULT_PW3 = "12345678" +BY_ADMIN = 3 + +KEYNO_FOR_AUTH=2 + +def main(passwd, data_regnual, data_upgrade): + l = len(data_regnual) + if (l & 0x03) != 0: + data_regnual = data_regnual.ljust(l + 4 - (l & 0x03), chr(0)) + crc32code = crc32(data_regnual) + print "CRC32: %04x\n" % crc32code + data_regnual += pack(' 1 and sys.argv[1] == '-p': + from getpass import getpass + passwd = getpass("Admin password: ") + sys.argv.pop(1) + filename_regnual = sys.argv[1] + filename_upgrade = sys.argv[2] + f = open(filename_regnual) + data_regnual = f.read() + f.close() + print "%s: %d" % (filename_regnual, len(data_regnual)) + f = open(filename_upgrade) + data_upgrade = f.read() + f.close() + print "%s: %d" % (filename_upgrade, len(data_upgrade)) + # First 4096-byte in data_upgrade is SYS, so, skip it. + main(passwd, data_regnual, data_upgrade[4096:]) diff --git a/tool/gnuk_token.py b/tool/gnuk_token.py index 78de09d..f60e6aa 100644 --- a/tool/gnuk_token.py +++ b/tool/gnuk_token.py @@ -21,8 +21,8 @@ along with this program. If not, see . """ from struct import * -import string -import usb +import string, binascii +import usb, time # USB class, subclass, protocol CCID_CLASS = 0x0B @@ -606,3 +606,12 @@ def get_gnuk_device(): else: raise ValueError("Unknown ICC status", status) return icc + +SHA256_OID_PREFIX="3031300d060960864801650304020105000420" + +def UNSIGNED(n): + return n & 0xffffffff + +def crc32(bytestr): + crc = binascii.crc32(bytestr) + return UNSIGNED(crc)