consttime_memequal 1

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: de7388eb1294d6b13fe781c4ab9ac3b5b25cb240
4
+ data.tar.gz: db1d1daa241d255fc4338a37d1c087f26fba125a
5
+ SHA512:
6
+ metadata.gz: b61060e7f2f889eec00aa0d9f65f59b8c33349db6ab5f21ba64072570be9222ed59433919ffd091e1762c93162da25d5ba446f053d041184eca2cb4705b4a99a
7
+ data.tar.gz: '079833b63df4252ef342fb764ae9708fecd4af253cb278658058739d0ead927cc84e0d9b8e2ea79d9c9dc397c73853af427b033440aedc8f85ac7681baf88cf6'
@@ -0,0 +1,32 @@
1
+ # Copyright (c) 2018 Urabe, Shyouhei
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
11
+ # included in 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 THE
19
+ # SOFTWARE.
20
+
21
+ *.bundle
22
+ .DS_Store
23
+ /.byebug_history
24
+ /.ruby-version
25
+ /.yardoc/
26
+ /_yardoc/
27
+ /coverage/
28
+ /doc/
29
+ /pkg/
30
+ /tmp/
31
+ /vendor/
32
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,28 @@
1
+ #! /your/favourite/path/to/bundler
2
+ # -*- mode: ruby; coding: utf-8; indent-tabs-mode: nil; ruby-indent-level: 2 -*-
3
+ # -*- frozen_string_literal: true -*-
4
+ # -*- warn_indent: true -*-
5
+
6
+ # Copyright (c) 2018 Urabe, Shyouhei
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ source 'https://rubygems.org'
27
+ gemspec
28
+ gem 'openssl'
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 Urabe, Shyouhei
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be
11
+ included in 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 THE
19
+ SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # consttime_memequal? : thin wrapper to OS-provided constant-time memory comparison routine.
2
+
3
+ This kind of routine must be provided a priori but [[Feature
4
+ #10098]](https://bugs.ruby-lang.org/issues/10098) is not yet
5
+ implemented. We have to make glude code for now.
6
+
7
+ ## Provided functionality
8
+
9
+ This library provides one global function named `consttime_memequal?`.
10
+ Which is of course not very ruby-ish, I know, but best describes what
11
+ is going on.
12
+
13
+ ```ruby
14
+ consttime_memequal?(b1, b2, len=b1.bytesize) # => true / false
15
+ ```
16
+
17
+ Compares first _len_ bytes of _b1_ and _b2_. Returns `true` if they
18
+ are identical. Returns `false` if they are distinct.
19
+
20
+ ## Q&As
21
+
22
+ ### Why the name `consttime_memequal?`
23
+
24
+ NetBSD has [consttime_memequal(3)](https://www.freebsd.org/cgi/man.cgi?query=consttime_memequal&manpath=NetBSD+7.0). We followed it.
25
+
26
+ ### This library fails to load on my machine. Why?
27
+
28
+ Install OpenSSL (or LibreSSL).
29
+
30
+ ### I can't install OpenSSL for reasons. What to do?
31
+
32
+ Install OpenBSD instead (or NetBSD).
33
+
34
+ ### I wrote a general implementation! Can I pull request?
35
+
36
+ No you don't. By the nature of its provided functionality, someone
37
+ who implement this have to be very careful about side-channel attacks.
38
+ You definitely shouldn't do it for yourself. Make your OS provide one
39
+ for you.
40
+
41
+ Bug fix etc. are much appreciated!
42
+
43
+ ### Then what can I do?
44
+
45
+ Go to [[Feature #10098]](https://bugs.ruby-lang.org/issues/10098) and
46
+ persuade the core devs to implement that feature. That's the lethal
47
+ solution to the situation.
@@ -0,0 +1,32 @@
1
+ #! /your/favourite/path/to/rake
2
+ # -*- mode: ruby; coding: utf-8; indent-tabs-mode: nil; ruby-indent-level: 2 -*-
3
+ # -*- frozen_string_literal: true -*-
4
+ # -*- warn_indent: true -*-
5
+
6
+ # Copyright (c) 2018 Urabe, Shyouhei
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ require 'bundler/gem_tasks'
27
+ require 'bundler/gem_helper'
28
+
29
+ task :pry do
30
+ sh 'bundle exec ruby bin/console', verbose: true
31
+ end
32
+ task console: :pry
@@ -0,0 +1,29 @@
1
+ #! /your/favourite/path/to/ruby
2
+ # -*- mode: ruby; coding: utf-8; indent-tabs-mode: nil; ruby-indent-level: 2 -*-
3
+ # -*- frozen_string_literal: true -*-
4
+ # -*- warn_indent: true -*-
5
+
6
+ # Copyright (c) 2017 Urabe, Shyouhei
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ require 'bundler/setup'
27
+ require 'consttime_memequal'
28
+ require 'pry'
29
+ Pry.start
@@ -0,0 +1,24 @@
1
+ #!/bin/sh
2
+ # -*- mode: sh; coding: utf-8; indent-tabs-mode: nil -*-
3
+
4
+ # Copyright (c) 2018 Urabe, Shyouhei
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ # SOFTWARE.
23
+
24
+ bundle install
@@ -0,0 +1,45 @@
1
+ #! /your/favourite/path/to/gem
2
+ # -*- mode: ruby; coding: utf-8; indent-tabs-mode: nil; ruby-indent-level: 2 -*-
3
+ # -*- frozen_string_literal: true -*-
4
+ # -*- warn_indent: true -*-
5
+
6
+ # Copyright (c) 2018 Urabe, Shyouhei
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ Gem::Specification.new do |spec|
27
+ spec.name = 'consttime_memequal'
28
+ spec.version = 1
29
+ spec.author = 'Urabe, Shyouhei'
30
+ spec.email = 'shyouhei@ruby-lang.org'
31
+ spec.summary = 'provides consttime_memequal?'
32
+ spec.description = "Wrapper to OS-provided timing-safe memory comparison."
33
+ spec.homepage = 'https://github.com/shyouhei/consttime_memequal'
34
+ spec.license = 'MIT'
35
+ spec.files = `git ls-files -z`.split("\x0").reject { |f|
36
+ f.match(%r'^(test|spec|features|samples)/')
37
+ }
38
+ spec.require_paths = %w'lib'
39
+
40
+ spec.add_development_dependency 'bundler'
41
+ spec.add_development_dependency 'rake'
42
+ spec.add_development_dependency 'pry' # used in bin/console
43
+ spec.required_ruby_version = '>= 2.0.0'
44
+ spec.add_runtime_dependency 'fiddle'
45
+ end
@@ -0,0 +1,88 @@
1
+ #! /your/favourite/path/to/ruby
2
+ # -*- mode: ruby; coding: utf-8; indent-tabs-mode: nil; ruby-indent-level: 2 -*-
3
+ # -*- frozen_string_literal: true -*-
4
+ # -*- warn_indent: true -*-
5
+
6
+ # Copyright (c) 2018 Urabe, Shyouhei
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be
16
+ # included in all copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ dlsym = lambda do |symbol, lib = 'fiddle'|
27
+ begin
28
+ gem lib and require lib
29
+ libc = Fiddle.dlopen nil
30
+ ptr = libc.sym symbol
31
+ func = Fiddle::Function.new ptr, \
32
+ [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_SIZE_T], \
33
+ Fiddle::TYPE_INT,
34
+ name: symbol
35
+ rescue Fiddle::DLError
36
+ return nil
37
+ rescue LoadError
38
+ return nil
39
+ else
40
+ return func
41
+ end
42
+ end
43
+
44
+ case
45
+ when func = dlsym.call('consttime_memequal') then
46
+ define_method :consttime_memequal do |this, that, len = this.bytesize|
47
+ src = Fiddle::Pointer.to_ptr this
48
+ dst = Fiddle::Pointer.to_ptr that
49
+ ret = func.call src, dst, len
50
+ case ret
51
+ when 1 then return true
52
+ when 0 then return false
53
+ else raise RuntimeError, \
54
+ "consttime_memequal(3) returned unknown value: #{ret.inspect}"
55
+ end
56
+ end
57
+
58
+ when func = dlsym.call('timingsafe_memcmp') then
59
+ define_method :consttime_memequal do |this, that, len = this.bytesize|
60
+ src = Fiddle::Pointer.to_ptr this
61
+ dst = Fiddle::Pointer.to_ptr that
62
+ ret = func.call src, dst, len
63
+ return ret == 0
64
+ end
65
+
66
+ when func = dlsym.call('timingsafe_bcmp') then
67
+ define_method :consttime_memequal do |this, that, len = this.bytesize|
68
+ src = Fiddle::Pointer.to_ptr this
69
+ dst = Fiddle::Pointer.to_ptr that
70
+ ret = func.call src, dst, len
71
+ return ret == 0
72
+ end
73
+
74
+ when func = dlsym.call('CRYPTO_memcmp', 'openssl') then
75
+ define_method :consttime_memequal do |this, that, len = this.bytesize|
76
+ src = Fiddle::Pointer.to_ptr this
77
+ dst = Fiddle::Pointer.to_ptr that
78
+ ret = func.call src, dst, len
79
+ return ret == 0
80
+ end
81
+
82
+ else
83
+ raise <<-"end".strip unless func
84
+ No timing-safe memory comparison routine found.
85
+ We don't plan to implement one for you.
86
+ Use other secure OS.
87
+ end
88
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consttime_memequal
3
+ version: !ruby/object:Gem::Version
4
+ version: '1'
5
+ platform: ruby
6
+ authors:
7
+ - Urabe, Shyouhei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-18 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: fiddle
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Wrapper to OS-provided timing-safe memory comparison.
70
+ email: shyouhei@ruby-lang.org
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - bin/console
81
+ - bin/setup
82
+ - consttime_memequal.gemspec
83
+ - lib/consttime_memequal.rb
84
+ homepage: https://github.com/shyouhei/consttime_memequal
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.14
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: provides consttime_memequal?
108
+ test_files: []