twofish 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e25f6d79c9cb7b9c02f0ea6d36596d1a2bdc2487
4
+ data.tar.gz: a7378fc5f9e8edce9a78a65a8cdd13ffecd18385
5
+ SHA512:
6
+ metadata.gz: 93088141b06171d957c18a1c0ba445c6c97c31dfeff9560c38d6def58ec7289a913b559cbdeffdccf92984704278d4391f1f8cd2fbeacb3de2253e7d4f4ffcdd
7
+ data.tar.gz: 42e5318a058a37cacc94f999e58570cc237d77ea7a4869a8290a25e4a6056cc684e431a2089987a4a30c40e4465bd38ef70ce7c1cd19efa4ab5550867545b146
data/README.rdoc CHANGED
@@ -112,11 +112,14 @@ Ruby >=1.9 introduces string encodings. The current workaround uses
112
112
  #ord and #chr but this is not very satisfactory: it would be preferable
113
113
  to move to byte arrays throughout.
114
114
 
115
- The only padding mechanisms implemented are "none" (don't pad) and "zero
116
- byte" (append zero bytes to make up a full block). Zero byte padding has
117
- a well-known failure mode: if the plaintext terminates in null bytes
115
+ The only padding mechanisms implemented are "none", zero byte, and
116
+ ISO 10126-2. PKCS#5/7 padding is not implemented. Zero byte padding
117
+ has a well-known failure mode: if the plaintext terminates in null bytes
118
118
  then these may be erroneously removed when un-padding is performed.
119
119
 
120
+ Possible implementation-dependent timing attacks (Bignum promotion,
121
+ #pack(), ...).
122
+
120
123
  If no initialization vector is provided for CBC mode then the system
121
124
  random number generator (Kernel#rand) is used to generate one. The system
122
125
  random number generator may be weaker than desired.
data/Rakefile CHANGED
@@ -31,6 +31,7 @@ RDoc::Task.new do |rdoc|
31
31
  rdoc.title = 'twofish.rb'
32
32
  rdoc.options << '--line-numbers'
33
33
  rdoc.options << '--charset' << 'utf-8'
34
+ rdoc.options << '--main' << 'README.rdoc'
34
35
  rdoc.options << '--all'
35
36
  rdoc.rdoc_files.include('README.rdoc')
36
37
  rdoc.rdoc_files.include(Dir[ 'lib/**/*' ])
data/lib/twofish.rb CHANGED
@@ -301,7 +301,7 @@ class Twofish
301
301
  # hash as follows:
302
302
  # :mode => :ecb (default) or :cbc
303
303
  # :iv => optional 16 byte initialization vector (randomly generated if not supplied)
304
- # :padding => :none (default) or :zero_byte
304
+ # :padding => :none (default), :zero_byte or :iso10126_2
305
305
  def initialize(key_string, opts={})
306
306
 
307
307
  self.mode = opts[:mode] # use setter for validation
@@ -455,8 +455,8 @@ class Twofish
455
455
  end
456
456
 
457
457
  # Set the padding scheme for the (CBC mode) cipher
458
- # (Padding::NONE == :none or Padding::ZERO_BYTE ==
459
- # :zero_byte).
458
+ # (Padding::NONE == :none, Padding::ZERO_BYTE ==
459
+ # :zero_byte, Padding::ISO10126_2 == :iso10126_2).
460
460
  def padding=(scheme)
461
461
  @padding = Padding.validate(scheme)
462
462
  end
@@ -17,8 +17,11 @@ class Twofish
17
17
  # Use zero byte padding.
18
18
  ZERO_BYTE = :zero_byte
19
19
 
20
+ # Use ISO 10126-2 padding.
21
+ ISO10126_2 = :iso10126_2
22
+
20
23
  # Array of all known paddings.
21
- ALL = [ NONE, ZERO_BYTE ]
24
+ ALL = [ NONE, ZERO_BYTE, ISO10126_2 ]
22
25
 
23
26
  # Default padding (none).
24
27
  DEFAULT = NONE
@@ -50,6 +53,13 @@ class Twofish
50
53
  plaintext
51
54
  when ZERO_BYTE
52
55
  remainder.zero? ? plaintext : plaintext << "\0" * (block_size - remainder)
56
+ when ISO10126_2
57
+ number_of_pad_bytes = block_size - remainder
58
+ # Create random bytes
59
+ bytes = Array.new(number_of_pad_bytes - 1) {rand(256)}
60
+ # The last byte specify the total pad byte size
61
+ bytes << number_of_pad_bytes
62
+ plaintext << bytes.pack("C*")
53
63
  end
54
64
  end
55
65
 
@@ -66,9 +76,11 @@ class Twofish
66
76
  plaintext.dup
67
77
  when ZERO_BYTE
68
78
  plaintext.sub(/\000+\Z/, '')
79
+ when ISO10126_2
80
+ number_of_pad_bytes = plaintext.bytes.to_a[plaintext.length-1]
81
+ plaintext[0, (plaintext.length - number_of_pad_bytes)]
69
82
  end
70
83
  end
71
-
72
84
  end
73
85
 
74
86
  end
data/test/test_twofish.rb CHANGED
@@ -277,6 +277,17 @@ class TestPadding < TestBasics
277
277
  assert_equal(:zero_byte, tf.padding)
278
278
  end
279
279
 
280
+ def test_cipher_iso10126_2_padding
281
+ tf = Twofish.new(NULL_KEY_16_BYTES)
282
+ tf.padding = :iso10126_2
283
+ assert_equal(:iso10126_2, tf.padding)
284
+ end
285
+
286
+ def test_cipher_iso10126_2_padding_constructor
287
+ tf = Twofish.new(NULL_KEY_16_BYTES, :padding => :iso10126_2)
288
+ assert_equal(:iso10126_2, tf.padding)
289
+ end
290
+
280
291
  def test_cipher_unknown_padding
281
292
  tf = Twofish.new(NULL_KEY_16_BYTES)
282
293
  assert_raise ArgumentError do
@@ -316,4 +327,25 @@ class TestPadding < TestBasics
316
327
  to_pad = TO_PAD * BLOCK_SIZE
317
328
  assert_equal(to_pad, Twofish::Padding::pad(to_pad, BLOCK_SIZE, :zero_byte))
318
329
  end
330
+
331
+ def test_pad_iso10126_2
332
+ padded_text = Twofish::Padding::pad(TO_PAD, BLOCK_SIZE, :iso10126_2)
333
+ assert_match(/\A#{TO_PAD}/, padded_text)
334
+ assert_equal(TO_PAD.length + 10, padded_text.length)
335
+ end
336
+
337
+ def test_unpad_iso10126_2
338
+ bytes = Array.new(10 - 1) {rand(256)}
339
+ bytes << 10
340
+ assert_equal(TO_PAD, Twofish::Padding::unpad(TO_PAD+bytes.pack("C*"), BLOCK_SIZE, :iso10126_2))
341
+ end
342
+
343
+ def test_pad_unpad_block_size_iso10126_2
344
+ to_pad = TO_PAD * BLOCK_SIZE
345
+ padded_text = Twofish::Padding::pad(to_pad, BLOCK_SIZE, :iso10126_2)
346
+ assert_equal(to_pad.length + BLOCK_SIZE, padded_text.length)
347
+ assert_match(/\A#{to_pad}/, padded_text)
348
+ assert_equal(to_pad, Twofish::Padding::unpad(padded_text, BLOCK_SIZE, :iso10126_2))
349
+ end
350
+
319
351
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twofish
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
5
- prerelease:
4
+ version: 1.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Martin Carpenter
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-03-08 00:00:00.000000000 Z
11
+ date: 2013-10-28 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Twofish symmetric cipher in pure Ruby with ECB and CBC cipher modes derived
15
14
  from an original Perl implementation by Guido Flohr
@@ -33,27 +32,26 @@ files:
33
32
  homepage: http://mcarpenter.org/projects/twofish
34
33
  licenses:
35
34
  - BSD
35
+ metadata: {}
36
36
  post_install_message:
37
37
  rdoc_options: []
38
38
  require_paths:
39
39
  - lib
40
40
  required_ruby_version: !ruby/object:Gem::Requirement
41
- none: false
42
41
  requirements:
43
- - - ! '>='
42
+ - - '>='
44
43
  - !ruby/object:Gem::Version
45
44
  version: '0'
46
45
  required_rubygems_version: !ruby/object:Gem::Requirement
47
- none: false
48
46
  requirements:
49
- - - ! '>='
47
+ - - '>='
50
48
  - !ruby/object:Gem::Version
51
49
  version: '0'
52
50
  requirements: []
53
51
  rubyforge_project:
54
- rubygems_version: 1.8.10
52
+ rubygems_version: 2.0.3
55
53
  signing_key:
56
- specification_version: 3
54
+ specification_version: 4
57
55
  summary: Twofish symmetric cipher in pure Ruby
58
56
  test_files:
59
57
  - test/test_twofish.rb