present-cipher 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a4d550b256ef6e8fa10b22344cf852ebf870cc78e6e56b586dd0c04eb115a2cf
4
- data.tar.gz: 3deea46829f0af9b682ba519b015e9966ea2a93f0b1136099bc8f706f9362a21
3
+ metadata.gz: 36f722bbd51bc59a36df3f047589f45fc0cabec26320e8101da317292615d992
4
+ data.tar.gz: 2b6d314798df04c1e8ed76c87c7ec829992e52aa4b51c26b015c2e566a607663
5
5
  SHA512:
6
- metadata.gz: 6d0766eb00c53da4ec27b2589c285d5ce345f790e688f8331151de56ca2d99ad89ec0fc888762e757eb5893f74985cb8a516b802e055a484d3f49da4a098b80e
7
- data.tar.gz: b78a7a4f805f175ddfda9274fd81aa9c289adf4131e5a1c6b2770ec6b8634466c303d9e7e36c3cf0b87ebe99d5d7cef4cf8f4ff2d9709d0973fb2ea1e7a0e227
6
+ metadata.gz: 29819e47113016e1486b164a519d262632384aa049bd8f77c3df2bd480fc3db073df177c4e95fa14640823a1fd2e42f2764dede07bbfbff11d2392ef72e47056
7
+ data.tar.gz: 025d530a9eb07b8e09b5ecf65dd170b7c0f3312953cdbd58b7ce66c91bf1ae65639ec24523e42f8c731f140f96cb7ec516c88827cea91b1329d52532946c253b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.2.0] - 2022-11-29
4
+
5
+ ### Added
6
+
7
+ - Key and block length validation.
8
+
3
9
  ## [0.1.0] - 2022-11-26
4
10
 
5
11
  ### Added
@@ -0,0 +1,12 @@
1
+ module Present
2
+ class Cipher
3
+ class Error < StandardError
4
+ end
5
+
6
+ class KeyError < Error
7
+ end
8
+
9
+ class BlockError < Error
10
+ end
11
+ end
12
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Present
4
4
  class Cipher
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -1,11 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "cipher/error"
3
4
  require_relative "cipher/version"
4
5
 
5
6
  module Present
6
7
  class Cipher
8
+ KEY_BYTESIZE = 10
9
+ KEY_BITSIZE = KEY_BYTESIZE * 8
10
+
11
+ BLOCK_BYTESIZE = 8
12
+ BLOCK_BITSIZE = BLOCK_BYTESIZE * 8
13
+
7
14
  S_BOX = [
8
- 0x0C, 0x05, 0x06, 0x0b, 0x09, 0x00, 0x0a, 0x0d, 0x03, 0x0e, 0x0f, 0x08, 0x04, 0x07, 0x01, 0x02
15
+ 0x0C, 0x05, 0x06, 0x0b, 0x09, 0x00, 0x0a, 0x0d, 0x03, 0x0e, 0x0f, 0x08, 0x04, 0x07, 0x01, 0x02,
9
16
  ]
10
17
 
11
18
  INVERSE_S_BOX = 0.upto(S_BOX.length - 1).map { |n| S_BOX.index(n) }
@@ -20,10 +27,16 @@ module Present
20
27
  INVERSE_P_BOX = 0.upto(P_BOX.length - 1).map { |n| P_BOX.index(n) }
21
28
 
22
29
  def initialize(key)
30
+ bitsize = key.bytesize * 8
31
+ raise KeyError, "key length is invalid (expected #{KEY_BITSIZE} bits; got #{bitsize})" if bitsize != KEY_BITSIZE
32
+
23
33
  @key = key
24
34
  end
25
35
 
26
36
  def encrypt(bytes)
37
+ bitsize = bytes.bytesize * 8
38
+ raise BlockError, "block length is invalid (expected #{BLOCK_BITSIZE} bits; got #{bitsize})" if bitsize != BLOCK_BITSIZE
39
+
27
40
  bits = bytes.unpack1("Q>")
28
41
 
29
42
  0.upto(30) do |i|
@@ -38,6 +51,9 @@ module Present
38
51
  end
39
52
 
40
53
  def decrypt(bytes)
54
+ bitsize = bytes.bytesize * 8
55
+ raise BlockError, "block length is invalid (expected #{BLOCK_BITSIZE} bits; got #{bitsize})" if bitsize != BLOCK_BITSIZE
56
+
41
57
  bits = bytes.unpack1("Q>")
42
58
 
43
59
  31.downto(1) do |i|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: present-cipher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Phan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-11-26 00:00:00.000000000 Z
11
+ date: 2022-11-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -25,8 +25,8 @@ files:
25
25
  - README.md
26
26
  - Rakefile
27
27
  - lib/present/cipher.rb
28
+ - lib/present/cipher/error.rb
28
29
  - lib/present/cipher/version.rb
29
- - present-cipher.gemspec
30
30
  - sig/present/cipher.rbs
31
31
  homepage: https://codeberg.org/htp/present-cipher
32
32
  licenses:
@@ -1,32 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "lib/present/cipher/version"
4
-
5
- Gem::Specification.new do |spec|
6
- spec.name = "present-cipher"
7
- spec.version = Present::Cipher::VERSION
8
- spec.authors = ["Henry Phan"]
9
- spec.email = ["henry@henryphan.com"]
10
-
11
- spec.summary = "A Ruby implementation of the PRESENT block cipher."
12
- spec.homepage = "https://codeberg.org/htp/present-cipher"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = ">= 2.6.0"
15
-
16
- spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
-
18
- spec.metadata["homepage_uri"] = spec.homepage
19
- spec.metadata["source_code_uri"] = "https://codeberg.org/htp/present-cipher"
20
- spec.metadata["changelog_uri"] = "https://codeberg.org/htp/present-cipher/blob/main/CHANGELOG.md"
21
-
22
- # Specify which files should be added to the gem when it is released.
23
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
- spec.files = Dir.chdir(__dir__) do
25
- `git ls-files -z`.split("\x0").reject do |f|
26
- (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
- end
28
- end
29
- spec.bindir = "exe"
30
- spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
32
- end