summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoro <o@immerda.ch>2011-06-30 23:46:04 +0200
committero <o@immerda.ch>2011-06-30 23:46:04 +0200
commite75df8ab3488ff751ec233ca8f052fa4e8238c28 (patch)
tree127167326a3ec86f85ba6d0708c4e01cc59328f7
parent883ab23d7fbb3d8d32cee39ef46b10b44c36faa8 (diff)
tried to implement s2k. but ruby is tooo slow!HEADmaster
i guess the next thing will be to write a wrapper for libgcrypt
-rw-r--r--lib/fpg/algos/cast5.rb3
-rw-r--r--lib/fpg/algos/hash.rb1
-rw-r--r--lib/fpg/algos/sha1.rb10
-rw-r--r--lib/fpg/fields/rsa_secret.rb3
-rw-r--r--lib/fpg/fields/s2k_specifier.rb38
-rw-r--r--lib/fpg/packets/secret_key.rb10
-rw-r--r--lib/fpg/parse/parser.rb14
-rw-r--r--spec/secret_key_spec.rb6
8 files changed, 74 insertions, 11 deletions
diff --git a/lib/fpg/algos/cast5.rb b/lib/fpg/algos/cast5.rb
index d07712f..58a0c87 100644
--- a/lib/fpg/algos/cast5.rb
+++ b/lib/fpg/algos/cast5.rb
@@ -4,6 +4,9 @@ module FPG
def block_size
8
end
+ def key_size
+ 128
+ end
def self.tag
3
end
diff --git a/lib/fpg/algos/hash.rb b/lib/fpg/algos/hash.rb
index 1388124..a954818 100644
--- a/lib/fpg/algos/hash.rb
+++ b/lib/fpg/algos/hash.rb
@@ -1,6 +1,7 @@
module FPG
module Algos
class Hash
+ require 'forwardable'
include TaggedSubclasses
end
end
diff --git a/lib/fpg/algos/sha1.rb b/lib/fpg/algos/sha1.rb
index a47ce87..113f43d 100644
--- a/lib/fpg/algos/sha1.rb
+++ b/lib/fpg/algos/sha1.rb
@@ -1,6 +1,16 @@
module FPG
module Algos
class Sha1 < Hash
+ require 'digest/sha1'
+
+ extend Forwardable
+ def_delegator :@digest, :digest, :digest
+ def_delegator :@digest, :digest_length, :digest_length
+
+ def initialize
+ @digest = Digest::SHA1.new
+ end
+
def self.tag
2
end
diff --git a/lib/fpg/fields/rsa_secret.rb b/lib/fpg/fields/rsa_secret.rb
index 9ecb175..75114e4 100644
--- a/lib/fpg/fields/rsa_secret.rb
+++ b/lib/fpg/fields/rsa_secret.rb
@@ -5,6 +5,9 @@ module FPG
multi_precision_integer :p
multi_precision_integer :q
multi_precision_integer :u
+ def empty?
+ d.val.empty?
+ end
end
end
end
diff --git a/lib/fpg/fields/s2k_specifier.rb b/lib/fpg/fields/s2k_specifier.rb
index 9b3a00c..8740568 100644
--- a/lib/fpg/fields/s2k_specifier.rb
+++ b/lib/fpg/fields/s2k_specifier.rb
@@ -4,12 +4,46 @@ module FPG
endian :big
uint8 :id
uint8 :h_algo
- uint64 :salt, :onlyif => lambda { id > 0 }
- uint8 :iterations, :onlyif => lambda { id > 2 }
+ string :salt, :onlyif => lambda { id > 0 }, :read_length => 8
+ uint8 :octet_ct, :onlyif => lambda { id > 2 }
+ def octet_count
+ (16+(octet_ct&15))<<((octet_ct>>4)+6)
+ end
def hash_algo
Algos::Hash.with_tag(h_algo)
end
+ def get_key(passphrase, key_length)
+ if id == 0 then
+ digest(passphrase, key_length)
+ elsif id == 1 then
+ digest(salt+passphrase, key_length)
+ elsif id == 3 then
+ pass = salt+passphrase
+ hashed = 0
+ while hashed < octet_count do
+ pass = digest(pass, key_length)
+ hashed += pass.length
+ end
+ pass
+ else
+ fail "#{id}"
+ end
+ end
+ def digest( passphrase, key_length )
+ if hash_algo.digest_length >= key_length then
+ hash_algo.digest( passphrase )[0..(key_length-hash_algo.digest_length)]
+ else
+ instances = (key_length / hash_algo.digest_length)
+ res = ""
+ (1..instances).each do |i|
+ preload = ""
+ i.times { preload << "\0" }
+ res << hash_algo.digest( preload+passphrase )
+ end
+ res[0..(key_length-(hash_algo.digest_length*instances))]
+ end
+ end
end
end
end
diff --git a/lib/fpg/packets/secret_key.rb b/lib/fpg/packets/secret_key.rb
index 16d834f..61f8a6d 100644
--- a/lib/fpg/packets/secret_key.rb
+++ b/lib/fpg/packets/secret_key.rb
@@ -18,7 +18,15 @@ module FPG
def hash_algo
self.s2k_specifier.hash_algo
end
-
+ def open!
+ return unless secret_key_material.empty?
+ key = get_key("test")
+ raw_key = s2k_algo.decrypt(encrypted_secret_key_material,key)
+ end
+ def get_key(passphrase)
+ return Digest::MD5.digest(passphrase) unless s2k_specifier_given?
+ s2k_specifier.get_key(passphrase,s2k_algo.key_size)
+ end
def s2k_specifier_given?
s2k >= 254
end
diff --git a/lib/fpg/parse/parser.rb b/lib/fpg/parse/parser.rb
index 76ba23e..5aed1e5 100644
--- a/lib/fpg/parse/parser.rb
+++ b/lib/fpg/parse/parser.rb
@@ -16,14 +16,14 @@ module FPG
num = packet_number(stream.getbyte)
stream.seek(-1,IO::SEEK_CUR)
packet = Packets::Packet.with_tag(num)
- if packet.is_a? Signature then
- puts
- BinData::trace_reading do
- packet.read(stream)
- end
- else
+# if packet.is_a? Signature then
+# puts
+# BinData::trace_reading do
+# packet.read(stream)
+# end
+# else
packet.read(stream)
- end
+# end
packet
end
def self.packet_number(byte)
diff --git a/spec/secret_key_spec.rb b/spec/secret_key_spec.rb
index 4268829..fd778f2 100644
--- a/spec/secret_key_spec.rb
+++ b/spec/secret_key_spec.rb
@@ -17,7 +17,11 @@ describe SecretKey do
pk.algo.is_a?( Rsa ).should be_true
pk.key_material.n.bit_len.should == 4096
pk.hash_algo.is_a?( Sha1 ).should be_true
-
+ end
+ it "should be able to open a key" do
+ parsed = Parser.parse(@binary_sec)
+ pk = parsed.select{|packet| packet.is_a? SecretKey}.first
+ puts pk.open!
end
end