]> git.cryptolib.org Git - arm-crypto-lib.git/blobdiff - host/rsa_oaep_check.rb
updated bigint & rsa
[arm-crypto-lib.git] / host / rsa_oaep_check.rb
index 8277c14ac4cee01a7e165ecb8f95445da9fa3dd1..8d8967e8986ab1346957f8b9de7fca9c8838135d 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/ruby
-# nessie_check.rb
+# rsa_oaep_check.rb
 =begin
-    This file is part of the ARM-Crypto-Lib.
+    This file is part of the AVR-Crypto-Lib.
     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
 
     This program is free software: you can redistribute it and/or modify
@@ -25,6 +25,8 @@ require 'getopt/std'
 $buffer_size = 0 # set automatically in init_system
 $conffile_check = Hash.new
 $conffile_check.default = 0
+$debug = false
+$logfile = nil
 
 ################################################################################
 # readconfigfile                                                               #
@@ -38,7 +40,12 @@ def read_line_from_device()
     l = $sp.gets()
     repeat_counter -= 1
   end while !l && repeat_counter > 0
-#  printf("DBG: << %s\n", l.inspect)
+  t = Time.new
+  $logfile.printf("DBG: (%02d:%02d:%02d)<< %s\n", t.hour, t.min, t.sec, l.inspect) if $debug
+  if l && l.include?("AVR-Crypto-Lib")
+    $logfile.printf("DBG: system crashed !!!\n")
+    exit(false)
+  end
   return l
 end
 
@@ -202,24 +209,59 @@ def read_tv(f)
   return h
 end
 
+def wait_for_dot
+  begin
+    s = $sp.gets()
+  end while !s || !s.include?('.')
+end
+
 def load_bigint(d)
   $sp.printf("%d\r", d.length)
   while l = read_line_from_device()
     break if /data:/.match(l)
   end
   printf "ERROR: got no answer from system!" if !l
+  i = 0
   d.each do |e|
-    $sp.printf(" %02x", e)
+    $sp.printf("%02x", e)
+    i += 1
+    if i % 60 == 0
+# we should now wait for incomming dot
+      wait_for_dot()
+      print('.')
+    end
   end
 end
 
+def hexdump(a)
+  i = 0
+  a.each do |e|
+    printf("\n\t") if i % 16 == 0
+    printf('%02x ', e)
+    i += 1
+  end
+  puts('') if i % 16 != 1
+end
+
+def str_hexdump(a)
+  i = 0
+  s = ''
+  a.each do |e|
+    s += "\n\t" if i % 16 == 0
+    s += sprintf('%02x ', e)
+    i += 1
+  end
+  s += "\n" if i % 16 != 1
+  return s
+end
+
 def load_key(k)
   $sp.print("load-key\r")
   sleep 0.1
   v = ['n', 'e', 'p', 'q', 'dP', 'dQ', 'qInv']  
   v.each do |e|
     load_bigint(k[e])
-#    printf("DBG: loaded %s\n", e)
+    $logfile.printf("DBG: loaded %s\n", e) if $debug
   end 
   while l = read_line_from_device()
     break if />/.match(l)
@@ -231,7 +273,7 @@ def check_tv(tv)
   $sp.print("seed-test\r")
   sleep 0.1
   load_bigint(tv['msg'])
-#  printf("DBG: loaded %s\n", 'msg')
+  $logfile.printf("DBG: loaded %s\n", 'msg') if $debug
   sleep 0.1
   tv['seed'].each { |e| $sp.printf(" %02x", e) } 
   while l = read_line_from_device() 
@@ -240,7 +282,7 @@ def check_tv(tv)
   test_enc = ''
   loop do 
     l = read_line_from_device()
-    break if /decrypting/.match(l)
+    break if ! /([0-9A-Fa-f]{2}\s*)+/.match(l)
     test_enc += l if l
   end
   test_enc_a = Array.new
@@ -252,7 +294,7 @@ def check_tv(tv)
   test_enc_a.collect!{ |e| e.to_i(16) }
   enc_ok = (test_enc_a == tv['enc'])
   if !enc_ok
-    printf("DBG: ref = %s test = %s\n", tv['enc'].inspect , test_enc_a.inspect)
+    $logfile.printf("DBG: ref = %s test = %s\n", str_hexdump(tv['enc']) , str_hexdump(test_enc_a))
   end
   m = nil
   loop do 
@@ -264,13 +306,15 @@ def check_tv(tv)
   return false
 end
 
-def run_test(f)
+def run_test(f,skip_key=1,skip_vec=1)
   ok = 0
   fail = 0
+  key_idx = 0
+  vec_idx = 0
   skip_file_header(f)
   loop do
     a,b = goto_next_header(f)
-#    printf("DBG: a=%s b=%s\n", a.inspect, b.inspect)
+    $logfile.printf("DBG: a=%s b=%s\n", a.inspect, b.inspect) if $debug
     return ok,fail if !b
     if a == :mainblock
 # Example 1: A 1024-bit RSA Key Pair
@@ -281,17 +325,24 @@ def run_test(f)
     if a == :subblock
       if b == 'Components of the RSA Key Pair'
         k = read_key(f)
-        load_key(k)
+        key_idx += 1
+        vec_idx = 0
+        load_key(k) if skip_key <= key_idx
       else
         tv = read_tv(f)
-        r = check_tv(tv)
-        if r
-          ok += 1
-          putc('*')
+        vec_idx += 1
+        if (key_idx > skip_key) || (key_idx == skip_key && vec_idx >= skip_vec)
+          r = check_tv(tv)
+          if r
+            ok += 1
+            putc('*')
+          else
+            fail += 1
+            putc('!')
+          end 
         else
-          fail += 1
-          putc('!')
-        end      
+          putc('o')
+        end     
       end
     end
   end
@@ -302,7 +353,7 @@ end
 ########################################
 
 
-opts = Getopt::Std.getopts("c:f:")
+opts = Getopt::Std.getopts("dc:f:il:s:")
 
 conf = Hash.new
 conf = readconfigfile("/etc/testport.conf", conf)
@@ -336,11 +387,27 @@ $sp = SerialPort.new(conf["PORT"]["port"], params)
 $sp.read_timeout=1000; # 5 minutes
 $sp.flow_control = SerialPort::SOFT
 
+$debug = true if opts['d']
+
+if opts['s'] && m = opts['s'].match(/([\d]+\.([\d]+))/)
+  sk = m[1].to_i
+  sv = m[2].to_i
+else
+  sk = 1
+  sv = 1
+end
+
+if opts['l']
+  $logfile = File.open(opts['l'], 'w')
+end
+
+$logfile = STDOUT if ! $logfile
+$logfile.sync = true
 reset_system()
 
 f = File.open(opts['f'], "r")
 exit if !f
-ok,fail = run_test(f)
+ok,fail = run_test(f,sk,sv)
 printf("\nOK: %d FAIL: %d :-%s\n",ok,fail, fail==0 ? ')':'(')