cmac 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e6cc4b7ae080eaa445005b760bb6cd100b892c47
4
+ data.tar.gz: 2483c560fabb23002ac91ffebde110c039b1adb2
5
+ SHA512:
6
+ metadata.gz: 30a9faa3bd1fc82b7dcf15f7a539e982f2eb99fd7a7e4256fe315e538c35fc8147ccbe862dd379f81368119a6f2af29f00d300a20569e6cc66d76e4299c0b5c3
7
+ data.tar.gz: 9a9a6380832361ecaf3e5617a045e3be6add2abd2dff1b5ab8efc175826622880a21aef309d60aaf43cd579fba5ed38b5a1bce3733f6b3c0842eb59b17024285
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 John Downey
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md CHANGED
@@ -29,7 +29,3 @@ cmac = CMAC.new(key)
29
29
  cmac.sign(message)
30
30
  => "\\\x11\x90\xE6\x91\xB2\xC4\x82`\x90\xA6\xEC:\x0E\x1C\xF3"
31
31
  ```
32
-
33
- ## License
34
-
35
- The CMAC gem is released under the [MIT license](http://www.opensource.org/licenses/MIT).
@@ -1,22 +1,26 @@
1
1
  require 'openssl'
2
2
 
3
- require 'cmac/exception'
4
3
  require 'cmac/version'
5
4
 
6
5
  class CMAC
6
+ Exception = Class.new(StandardError)
7
7
  ZeroBlock = "\0" * 16
8
8
  ConstantBlock = ("\0" * 15) + "\x87"
9
9
 
10
10
  def initialize(key)
11
- key.force_encoding('BINARY') if key.respond_to?(:force_encoding)
12
- @key = _derive_key(key)
11
+ @key = _derive_key(key.b)
13
12
  @key1, @key2 = _generate_subkeys(@key)
14
13
  end
15
14
 
15
+ def inspect
16
+ "#<CMAC:0x#{object_id.to_s(16)}>"
17
+ end
18
+
16
19
  def sign(message, truncate = 16)
17
20
  raise CMAC::Exception.new('Tag cannot be greater than maximum (16 bytes)') if truncate > 16
18
21
  raise CMAC::Exception.new('Tag cannot be less than minimum (8 bytes)') if truncate < 8
19
- message.force_encoding('BINARY') if message.respond_to?(:force_encoding)
22
+
23
+ message = message.b
20
24
 
21
25
  if _needs_padding?(message)
22
26
  message = _pad_message(message)
@@ -97,7 +101,7 @@ class CMAC
97
101
 
98
102
  def _pad_message(message)
99
103
  padded_length = message.length + 16 - (message.length % 16)
100
- message = message + "\x80"
104
+ message = message + "\x80".b
101
105
  message.ljust(padded_length, "\0")
102
106
  end
103
107
 
@@ -114,8 +118,8 @@ class CMAC
114
118
  end
115
119
 
116
120
  def _xor(a, b)
117
- a.force_encoding('BINARY') if a.respond_to?(:force_encoding)
118
- b.force_encoding('BINARY') if b.respond_to?(:force_encoding)
121
+ a = a.b
122
+ b = b.b
119
123
 
120
124
  output = ''
121
125
  length = [a.length, b.length].min
@@ -1,3 +1,3 @@
1
1
  class CMAC
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,27 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe CMAC do
4
+ describe 'inspect' do
5
+ it 'does not contain instance variable values' do
6
+ cmac = CMAC.new(TestKey)
7
+ expect(cmac.inspect).to match(/\A#<CMAC:0[xX][0-9a-fA-F]+>\z/)
8
+ end
9
+ end
10
+
4
11
  describe 'sign' do
5
12
  test_vectors.each do |name, options|
6
- it "should match the \"#{name}\" test vector" do
13
+ it "matches the \"#{name}\" test vector" do
7
14
  cmac = CMAC.new(options[:Key])
8
- cmac.sign(options[:Message], options[:Truncate].to_i).should == options[:Tag]
15
+ output = cmac.sign(options[:Message], options[:Truncate].to_i)
16
+ expect(output).to eq(options[:Tag])
9
17
  end
10
18
  end
11
19
 
12
- it 'should give a truncated output if requested' do
20
+ it 'gives a truncated output if requested' do
13
21
  cmac = CMAC.new(TestKey)
14
- cmac.sign('attack at dawn', 12).length.should == 12
22
+ output = cmac.sign('attack at dawn', 12)
23
+ expect(output.length).to eq(12)
15
24
  end
16
25
 
17
- it 'should raise error if truncation request is greater than 16 bytes' do
26
+ it 'raises error if truncation request is greater than 16 bytes' do
18
27
  cmac = CMAC.new(TestKey)
19
28
  expect do
20
29
  cmac.sign('attack at dawn', 17)
21
30
  end.to raise_error(CMAC::Exception, 'Tag cannot be greater than maximum (16 bytes)')
22
31
  end
23
32
 
24
- it 'should raise error if truncation request is less than 8 bytes' do
33
+ it 'raises error if truncation request is less than 8 bytes' do
25
34
  cmac = CMAC.new(TestKey)
26
35
  expect do
27
36
  cmac.sign('attack at dawn', 7)
@@ -30,17 +39,19 @@ describe CMAC do
30
39
  end
31
40
 
32
41
  describe 'valid_message?' do
33
- it 'should be true for matching messages' do
42
+ it 'is true for matching messages' do
34
43
  message = 'attack at dawn'
35
44
  cmac = CMAC.new(TestKey)
36
45
  tag = cmac.sign(message)
37
- cmac.should be_valid_message(tag, message)
46
+ result = cmac.valid_message?(tag, message)
47
+ expect(result).to be_truthy
38
48
  end
39
49
 
40
- it 'should be false for modified messages' do
50
+ it 'is false for modified messages' do
41
51
  cmac = CMAC.new(TestKey)
42
52
  tag = cmac.sign('attack at dawn')
43
- cmac.should_not be_valid_message(tag, 'attack at dusk')
53
+ result = cmac.valid_message?(tag, 'attack at dusk')
54
+ expect(result).to be_falsey
44
55
  end
45
56
  end
46
57
  end
@@ -9,18 +9,16 @@ end
9
9
  def test_vectors
10
10
  test_file = File.expand_path('../test_vectors.txt', __FILE__)
11
11
  test_lines = File.readlines(test_file).map(&:strip).reject(&:empty?)
12
-
13
- vectors = {}
14
- test_lines.each_slice(5) do |lines|
12
+ test_lines.each_slice(5).reduce({}) do |vectors, lines|
15
13
  name = lines.shift
16
- values = lines.inject({}) do |hash, line|
14
+ vector = lines.reduce({}) do |values, line|
17
15
  key, value = line.split('=').map(&:strip)
18
- value = '' unless value
16
+ value ||= ''
19
17
  value = [value.slice(2..-1)].pack('H*') if value.start_with?('0x')
20
- hash[key.to_sym] = value
21
- hash
18
+
19
+ values.merge!(key.to_sym => value)
22
20
  end
23
- vectors[name] = values
21
+
22
+ vectors.merge!(name => vector)
24
23
  end
25
- vectors
26
24
  end
metadata CHANGED
@@ -1,48 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - John Downey
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-23 00:00:00.000000000 Z
11
+ date: 2016-02-10 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '='
20
18
  - !ruby/object:Gem::Version
21
- version: '0'
19
+ version: 10.5.0
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '='
28
25
  - !ruby/object:Gem::Version
29
- version: '0'
26
+ version: 10.5.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - '='
36
32
  - !ruby/object:Gem::Version
37
- version: 2.12.0
33
+ version: 3.4.0
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - '='
44
39
  - !ruby/object:Gem::Version
45
- version: 2.12.0
40
+ version: 3.4.0
46
41
  description: A ruby implementation of RFC4493, RFC4494, and RFC4615. CMAC is a message
47
42
  authentication code (MAC) built using AES-128.
48
43
  email:
@@ -51,42 +46,36 @@ executables: []
51
46
  extensions: []
52
47
  extra_rdoc_files: []
53
48
  files:
54
- - lib/cmac/exception.rb
55
- - lib/cmac/version.rb
49
+ - LICENSE
50
+ - README.md
56
51
  - lib/cmac.rb
52
+ - lib/cmac/version.rb
57
53
  - spec/cmac_spec.rb
58
54
  - spec/spec_helper.rb
59
55
  - spec/test_vectors.txt
60
- - README.md
61
56
  homepage: https://github.com/jtdowney/cmac
62
- licenses: []
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
63
60
  post_install_message:
64
61
  rdoc_options: []
65
62
  require_paths:
66
63
  - lib
67
64
  required_ruby_version: !ruby/object:Gem::Requirement
68
- none: false
69
65
  requirements:
70
- - - ! '>='
66
+ - - ">="
71
67
  - !ruby/object:Gem::Version
72
68
  version: '0'
73
- segments:
74
- - 0
75
- hash: -708155467333762105
76
69
  required_rubygems_version: !ruby/object:Gem::Requirement
77
- none: false
78
70
  requirements:
79
- - - ! '>='
71
+ - - ">="
80
72
  - !ruby/object:Gem::Version
81
73
  version: '0'
82
- segments:
83
- - 0
84
- hash: -708155467333762105
85
74
  requirements: []
86
75
  rubyforge_project:
87
- rubygems_version: 1.8.23
76
+ rubygems_version: 2.5.1
88
77
  signing_key:
89
- specification_version: 3
78
+ specification_version: 4
90
79
  summary: Cipher-based Message Authentication Code
91
80
  test_files:
92
81
  - spec/cmac_spec.rb
@@ -1,4 +0,0 @@
1
- class CMAC
2
- class Exception < StandardError
3
- end
4
- end