digest-cmac 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5fe90c5b5f913dab5c89f32635ce3fdd74b1e206802aec12b0c6e43d7bcec822
4
+ data.tar.gz: 125dab2006677c9de6ffe258a7b68580544513a9c9277b4b2bc588963669dadd
5
+ SHA512:
6
+ metadata.gz: f5d4a1a61059286cf911198c8b8a2c3f6fbf3928ac49c5cfecff478d792b39bc142c9a1a8c00e18beb36e83019744d3e9fbc29becfff6cbe33dd6dc5fac06a87
7
+ data.tar.gz: 9f53b561b1dee62b03cdc2307a8e85cfb4a95a271132affbd1e49f3618f8ef43f6b6e5d1a7b469f0167720ab777762ea9a3049aad516dee22d205727a29b4e99
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in digest-cmac.gemspec
4
+ gemspec
@@ -0,0 +1,36 @@
1
+ # Digest::Cmac
2
+
3
+ Copied from https://github.com/quadule/digest-cmac since it's not a gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'digest-cmac'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install digest-cmac
20
+
21
+ ## Usage
22
+
23
+ A Ruby implementation of the CMAC / OMAC1 digest algorithm, based on RFC 4493:
24
+ http://tools.ietf.org/html/rfc4493
25
+
26
+ Here's an example using a test vector from the RFC:
27
+
28
+ # key is 128 bits
29
+ key = ["2b7e151628aed2a6abf7158809cf4f3c"].pack('H*')
30
+
31
+ cmac = Digest::CMAC.new(OpenSSL::Cipher::Cipher.new('aes-128-cbc'), key)
32
+ cmac.update(["6bc1bee22e409f96e93d7e117393172a"].pack('H*'))
33
+ digest = cmac.digest
34
+
35
+ # unpack it into hex
36
+ digest.unpack('H*')[0] # => '070a16b46b4d4144f79bdd9dd04a287c'
@@ -0,0 +1,3 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'digest/cmac'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'digest/cmac/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'digest-cmac'
7
+ spec.version = Digest::CMAC::VERSION
8
+ spec.authors = ['eMerchantPay']
9
+ spec.email = ['rnd@emerchantpay.com']
10
+
11
+ spec.summary = 'CMAC algorithm'
12
+ spec.description = 'CMAC algorithm'
13
+ spec.homepage = 'https://emp-sof-github01.emp.internal.com/eMerchantPay/digest-cmac'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.15'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.7'
25
+ end
@@ -0,0 +1,82 @@
1
+ require 'digest/cmac/version'
2
+
3
+ module Digest
4
+ class CMAC
5
+
6
+ BLOCK_SIZE = 16.freeze
7
+
8
+ # Constant defined in the RFC for 16-byte block sizes
9
+ RB = "\0" * (BLOCK_SIZE - 1) + "\x87"
10
+
11
+ # Constructs an object to calculate CMACs for data
12
+ def initialize(cipher, key)
13
+ raise "Cipher block size must be #{BLOCK_SIZE}" unless cipher.block_size == BLOCK_SIZE
14
+
15
+ @cipher = cipher
16
+ @cipher.encrypt
17
+ @cipher.key = @key = key
18
+
19
+ generate_subkeys
20
+ reset
21
+ end
22
+
23
+ def reset
24
+ @data = ''
25
+ @tag = "\0" * BLOCK_SIZE
26
+ end
27
+
28
+ def update(data)
29
+ @data += data
30
+ complete_block_count = (@data.length / BLOCK_SIZE).floor
31
+
32
+ if @data.length > BLOCK_SIZE
33
+ 0.upto(complete_block_count - 1) do |_i|
34
+ break if @data.length == BLOCK_SIZE
35
+ block = @data[0..(BLOCK_SIZE - 1)]
36
+ @data = @data[BLOCK_SIZE..@data.length]
37
+ raise 'Bad block length' if block.length != BLOCK_SIZE
38
+ @tag = xor(@tag, block)
39
+ @tag = encrypt_block(@tag)
40
+ end
41
+ end
42
+ end
43
+
44
+ def digest
45
+ raise 'Bad data length' if @data.length > BLOCK_SIZE
46
+
47
+ if @data.length == BLOCK_SIZE
48
+ @data = xor(@data, @lu)
49
+ else
50
+ @data << "\200".force_encoding('ASCII-8BIT') + ("\000".force_encoding('ASCII-8BIT') * (BLOCK_SIZE - @data.length - 1))
51
+ @data = xor(@data, @lu2)
52
+ end
53
+
54
+ @tag = xor(@tag, @data)
55
+ @tag = encrypt_block(@tag)
56
+ end
57
+
58
+ private
59
+
60
+ def encrypt_block(block)
61
+ @cipher.reset
62
+ @cipher.update(block)
63
+ end
64
+
65
+ def generate_subkeys
66
+ @l = encrypt_block("\0" * BLOCK_SIZE)
67
+ @lu = subkey_shift(@l)
68
+ @lu2 = subkey_shift(@lu)
69
+ end
70
+
71
+ def subkey_shift(subkey)
72
+ msb, tail = subkey.unpack('B*').first.unpack('a a*')
73
+ left_shift = [tail, '0'].pack('B*')
74
+ msb == '1' ? xor(left_shift, RB) : left_shift
75
+ end
76
+
77
+ def xor(a, b)
78
+ a.bytes.zip(b.bytes).map { |x, y| (x ^ y).chr }.join
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,5 @@
1
+ module Digest
2
+ class CMAC
3
+ VERSION = '1.0.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digest-cmac
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - eMerchantPay
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
55
+ description: CMAC algorithm
56
+ email:
57
+ - rnd@emerchantpay.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rspec_status"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - digest-cmac.gemspec
71
+ - lib/digest/cmac.rb
72
+ - lib/digest/cmac/version.rb
73
+ homepage: https://emp-sof-github01.emp.internal.com/eMerchantPay/digest-cmac
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.7
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: CMAC algorithm
96
+ test_files: []