liboqs 0.1.0

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
+ SHA256:
3
+ metadata.gz: c43a5969249e429523544d9b233f4e8d9a1cbc9996ccfc5eab7abd38cf2b38da
4
+ data.tar.gz: f95847ce06ab42130cda0897d3841e94971c3981ea4381001647c7ebc81dc105
5
+ SHA512:
6
+ metadata.gz: af3ccdcdd6d00dc37285e49e405640aa6240d2e3d189986fd2cb5500a32d1c9a1a7d068c84399d3a3d1327d7f1779d8448b36113a8a98129a5178e93e897be7f
7
+ data.tar.gz: 6739b5aaafe7b1ee875599e1cfb1045f9b2627da8f503c2603d49f41a2d5a5658cab099431a476493e555435a22d8a6e019b90c46e837bcc24fdf684feded801
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ *.log
14
+ *.gem
15
+
16
+
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,9 @@
1
+ image: ruby:3.0.2
2
+
3
+ before_script:
4
+ - gem install bundler -v 2.2.22
5
+ - bundle install
6
+
7
+ example_job:
8
+ script:
9
+ - bundle exec rake
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-08-12
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at chrisliaw@antrapol.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in oqs.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,86 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ liboqs (0.1.0)
5
+ tlogger
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ devops_helper (0.3.1)
12
+ git_cli (>= 0.6.0)
13
+ gvcs (>= 0.1.0)
14
+ tlogger (>= 0.22.0)
15
+ toolrack (>= 0.4.0)
16
+ tty-prompt (= 0.22.0)
17
+ diff-lcs (1.4.4)
18
+ git_cli (0.8.0)
19
+ gvcs (>= 0.1.0)
20
+ ptools (~> 1.4.0)
21
+ tlogger (>= 0.21)
22
+ toolrack (>= 0.4.0)
23
+ gvcs (0.1.0)
24
+ parallel (1.20.1)
25
+ parser (3.0.2.0)
26
+ ast (~> 2.4.1)
27
+ pastel (0.8.0)
28
+ tty-color (~> 0.5)
29
+ ptools (1.4.2)
30
+ rainbow (3.0.0)
31
+ rake (13.0.6)
32
+ regexp_parser (2.1.1)
33
+ rexml (3.2.5)
34
+ rspec (3.10.0)
35
+ rspec-core (~> 3.10.0)
36
+ rspec-expectations (~> 3.10.0)
37
+ rspec-mocks (~> 3.10.0)
38
+ rspec-core (3.10.1)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-expectations (3.10.1)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-mocks (3.10.2)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-support (3.10.2)
47
+ rubocop (1.18.4)
48
+ parallel (~> 1.10)
49
+ parser (>= 3.0.0.0)
50
+ rainbow (>= 2.2.2, < 4.0)
51
+ regexp_parser (>= 1.8, < 3.0)
52
+ rexml
53
+ rubocop-ast (>= 1.8.0, < 2.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 1.4.0, < 3.0)
56
+ rubocop-ast (1.8.0)
57
+ parser (>= 3.0.1.1)
58
+ ruby-progressbar (1.11.0)
59
+ tlogger (0.25.0)
60
+ toolrack (0.6.0)
61
+ tlogger (~> 0.21)
62
+ tty-color (0.6.0)
63
+ tty-cursor (0.7.1)
64
+ tty-prompt (0.22.0)
65
+ pastel (~> 0.8)
66
+ tty-reader (~> 0.8)
67
+ tty-reader (0.9.0)
68
+ tty-cursor (~> 0.7)
69
+ tty-screen (~> 0.8)
70
+ wisper (~> 2.0)
71
+ tty-screen (0.8.1)
72
+ unicode-display_width (2.0.0)
73
+ wisper (2.0.1)
74
+
75
+ PLATFORMS
76
+ x86_64-linux
77
+
78
+ DEPENDENCIES
79
+ devops_helper
80
+ liboqs!
81
+ rake (~> 13.0)
82
+ rspec (~> 3.0)
83
+ rubocop (~> 1.7)
84
+
85
+ BUNDLED WITH
86
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Chris Liaw
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # liboqs-ruby
2
+
3
+ liboqs-ruby is the Ruby wrapper to the [Open Quantum Safe library](https://openquantumsafe.org). The native library was tested against the liboqs at [liboqs](https://github.com/open-quantum-safe/liboqs)
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'oqs'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install liboqs-ruby
21
+
22
+ ## Usage
23
+
24
+ OQS mainly only has two group of functions: Key Encapsulation Mechanism (KEM) and Signature (SIG).
25
+
26
+ Therefore the Ruby wrapper abstraction is following the liboqs C version as baseline.
27
+
28
+ ### Key Encapsulation Mechanism (KEM)
29
+
30
+ For KEM, the API is simple:
31
+
32
+ 1. List all supported KEM PQ algorithms - PQ algorithms can be enable or disabled at compile time so it all depends on the liboqs native library. This API listed down the algorithms which are *supported* as reported by the native library. If you're using your own version of the library, you might have different output.
33
+ ```ruby
34
+ require 'oqs'
35
+
36
+ supported_algo = Oqs::KEM.supported_kem_algo
37
+ supported_algo.each do |al|
38
+ # al is the algorithm name (string) which is required by subsequent API
39
+ ...
40
+ end
41
+ ```
42
+
43
+ 2. Generate keypair
44
+ ```ruby
45
+ require 'oqs'
46
+
47
+ ntru = Oqs::KEM.new('NTRU-HPS-4096-821')
48
+ pubKey, secretKey = ntru.genkeypair
49
+ # note pubKey and secretKey (or private key) is Fiddle::Pointer type and
50
+ # is required to be used by the C API in the subsequent phase.
51
+ # Note that pubKey and secretKey are required to be free manually
52
+ # Refer spec file for usage
53
+ ```
54
+
55
+ 3. Key encapsulation - KEM is meant for key encapsulation which similar with Diffie-Hellman kind of key exchange
56
+ ```ruby
57
+ require 'oqs'
58
+
59
+ sessionKey, cipher = ntru.derive_encapsulation_key(pubKey)
60
+ # cipher is required to be sent to recipient end to re-generate the sessionKey at recipient end.
61
+ # Returned sessionKey is meant to convert into the final AES (or any other symmetric key)
62
+ # for the actual data encryption
63
+ ```
64
+
65
+ 4. Key decapsulation - Re-generate the session key from the private key
66
+ ```ruby
67
+ require 'oqs'
68
+
69
+ sessionKey = ntru.derive_decapsulation_key(cipher, secretKey)
70
+ # cipher is given by sender and privKey is the recipient own private key
71
+ ```
72
+
73
+ The idea is the sessionKey from derive\_encapsulation\_key() shall be same as the sessionKey from derive\_decapsulation\_key(). That session key shall be the AES key (any other symmetric key) for the data encryption.
74
+
75
+
76
+ ### Signature mechanism
77
+
78
+ Signature mechanism is similar with KEM.
79
+
80
+ 1. List all supported Signature PQ algorithms - It is same as KEM as algorithm can be turned on or off during compile time
81
+ ```ruby
82
+ require 'oqs'
83
+
84
+ supported_algo = Oqs::SIG.supported_signature_algo
85
+ supported_algo.each do |al|
86
+ # al is the algorithm name (string) which is required by subsequent API
87
+ ...
88
+ end
89
+ ```
90
+
91
+ 2. Generate keypair
92
+ ```ruby
93
+ require 'oqs'
94
+
95
+ dili = Oqs::SIG.new('Dilithium5')
96
+ pubKey, secretKey = dili.genkeypair
97
+ # note pubKey and secretKey (or private key) is Fiddle::Pointer type and
98
+ # is required to be used by the C API in the subsequent phase.
99
+ # Note that pubKey and secretKey are required to be free manually
100
+ # Refer spec file for usage
101
+ ```
102
+
103
+ 3. Generate data signature
104
+ ```ruby
105
+ require 'oqs'
106
+
107
+ # sign data using sender secretKey/private key
108
+ signature = dili.sign("this is message", secretKey)
109
+ ```
110
+
111
+ 4. Verify data signature
112
+ ```ruby
113
+ require 'oqs'
114
+
115
+ # verify signature with given data using sender public key
116
+ res = dili.verify("this is message", signature, pubKey)
117
+ # res is boolean to indicate the signature verification is passed or failed
118
+ ```
119
+
120
+ spec folder has the necessary API example usage.
121
+
122
+ ## Development Environment
123
+
124
+ The source code was tested on
125
+ * Ruby MRI 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86\_64-linux], Linux Mint 20.2 x86\_64, Kernel 5.4.0-81-generic, CMake version 3.16.3, Ninja 1.10.0
126
+
127
+ ## License
128
+
129
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
130
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ require 'devops_helper'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ require "rubocop/rake_task"
11
+
12
+ RuboCop::RakeTask.new
13
+
14
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "oqs"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/lib/oqs.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "oqs/version"
4
+ require_relative "oqs/struct"
5
+ require_relative "oqs/global"
6
+ require_relative "oqs/kem"
7
+ require_relative "oqs/sig"
8
+
9
+ module Oqs
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+
13
+ # OQS_STATUS
14
+ OQS_ERROR = -1
15
+ OQS_SUCCESS = 0
16
+ OQS_EXTERNAL_LIB_ERROR_OPENSSL = 50
17
+
18
+ end
@@ -0,0 +1,20 @@
1
+
2
+ require 'fiddle'
3
+ require 'fiddle/import'
4
+
5
+ module Oqs
6
+ module CommonWrapper
7
+ extend Fiddle::Importer
8
+ include Oqs::Wrapper
9
+
10
+ #dlload File.join(File.dirname(__FILE__),"..","..","native","linux","x86_64","liboqs.so.0.7.0")
11
+ load_oqs_lib
12
+
13
+ extern 'int OQS_MEM_secure_bcmp(const void *a, const void *b, size_t len)'
14
+ extern 'int OQS_MEM_cleanse(const void *ptr, size_t len)'
15
+ extern 'int OQS_MEM_secure_free(void *ptr, size_t len)'
16
+ extern 'int OQS_MEM_insecure_free(void *ptr)'
17
+
18
+
19
+ end
20
+ end
data/lib/oqs/global.rb ADDED
@@ -0,0 +1,19 @@
1
+
2
+ require 'tlogger'
3
+ require 'singleton'
4
+
5
+ module Oqs
6
+ class Global
7
+ include Singleton
8
+
9
+ attr_accessor :logger
10
+ def initialize
11
+ logPath = ENV['OQS_LOG_PATH']
12
+ if not (logPath.nil? or logPath.empty?)
13
+ @logger = Tlogger.new(logPath,10,1024*1024*10)
14
+ else
15
+ @logger = Tlogger.new
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/oqs/kem.rb ADDED
@@ -0,0 +1,146 @@
1
+
2
+ require_relative 'struct'
3
+ require_relative 'kem_wrapper'
4
+ require_relative 'common_wrapper'
5
+
6
+ module Oqs
7
+ class KEM
8
+
9
+ def self.supported_kem_algo
10
+ ttl = KEMWrapper.OQS_KEM_alg_count
11
+ supported = []
12
+ (0...ttl).each do |i|
13
+ pName = KEMWrapper.OQS_KEM_alg_identifier(i)
14
+ name = pName.to_s
15
+ st = KEMWrapper.OQS_KEM_alg_is_enabled(name)
16
+ if st
17
+ supported << name
18
+ end
19
+ end
20
+
21
+ supported
22
+ end
23
+
24
+ def initialize(name)
25
+ @algo = name
26
+ oqsKem = KEMWrapper.OQS_KEM_new(@algo)
27
+ raise Error, "Unable to create object '#{@algo}'. It is either the algorithm not supported or it is disabled at compile time." if oqsKem.null?
28
+ @struct = OQS_KEM.new(oqsKem)
29
+ end
30
+
31
+ def cleanup
32
+ KEMWrapper.OQS_KEM_free(@struct) if not @struct.nil?
33
+ end
34
+
35
+ def free(obj)
36
+ obj.free if not (obj.nil? and obj.null?)
37
+ end
38
+
39
+ def intrinsic_name
40
+ @struct.intrinsic_name.to_s
41
+ end
42
+
43
+ def algo_version
44
+ @struct.algo_version.to_s
45
+ end
46
+
47
+ def method_missing(mtd, *args, &block)
48
+ @struct.send(mtd) if not @struct.nil? and @struct.respond_to?(mtd)
49
+ end
50
+
51
+ def genkeypair
52
+ pubKey = Fiddle::Pointer.malloc(@struct.length_public_key, Fiddle::RUBY_FREE)
53
+ raise Error, "Unable to allocate memory for public key size #{@struct.length_public_key}" if pubKey.null?
54
+ privKey = Fiddle::Pointer.malloc(@struct.length_secret_key, Fiddle::RUBY_FREE)
55
+ raise Error, "Unable to allocate memory for secret key size #{@struct.length_secret_key}" if privKey.null?
56
+
57
+ rv = KEMWrapper.OQS_KEM_keypair(@struct, pubKey, privKey)
58
+ raise Error, "Error in generation of keypair" if rv != Oqs::OQS_SUCCESS
59
+
60
+ #pubKeyBin = pubKey[0, pubKey.size]
61
+ #privKeyBin = privKey[0, privKey.size]
62
+
63
+ [pubKey, privKey]
64
+ end
65
+
66
+ def derive_encapsulation_key(pubKey)
67
+
68
+ cipher = Fiddle::Pointer.malloc(@struct.length_ciphertext, Fiddle::RUBY_FREE)
69
+ raise Error, "Unable to allocate memory for ciphertext size #{@struct.length_ciphertext}" if cipher.null?
70
+
71
+ encpKey = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
72
+ raise Error, "Unable to allocate memory for shared secret size #{@struct.length_shared_secret}" if encpKey.null?
73
+
74
+ rv = KEMWrapper.OQS_KEM_encaps(@struct, cipher, encpKey, pubKey)
75
+ raise Error, "Error in encapsulation" if rv != Oqs::OQS_SUCCESS
76
+
77
+ encpKeyBin = encpKey[0,encpKey.size]
78
+ cipherBin = cipher[0,cipher.size]
79
+
80
+ cipher.free
81
+ encpKey.free
82
+
83
+ [encpKeyBin, cipherBin]
84
+
85
+ end
86
+
87
+ def derive_decapsulation_key(cipherBin, privKey)
88
+
89
+ raise Error, "Cipher cannot be empty" if cipherBin.nil?
90
+ raise Error, "Private key cannot be nil" if privKey.nil?
91
+
92
+ encpKey = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
93
+ raise Error, "Unable to allocate memory for shared secret size #{@struct.length_shared_secret}" if encpKey.null?
94
+
95
+ rv = KEMWrapper.OQS_KEM_decaps(@struct, encpKey , cipherBin, privKey)
96
+ raise Error, "Error in decapsulation" if rv != Oqs::OQS_SUCCESS
97
+
98
+ encpKeyBin = encpKey[0,encpKey.size]
99
+
100
+ encpKey.free
101
+
102
+ encpKeyBin
103
+
104
+ end
105
+
106
+ #def test
107
+
108
+ # @cipher = Fiddle::Pointer.malloc(@struct.length_ciphertext, Fiddle::RUBY_FREE)
109
+ # raise Error, "Unable to allocate memory for ciphertext size #{@struct.length_ciphertext}" if @cipher.null?
110
+
111
+ # shared_e = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
112
+ # raise Error, "Unable to allocate memory for shared secret size #{@struct.length_shared_secret}" if shared_e.null?
113
+ #
114
+ # shared_d = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
115
+ # raise Error, "Unable to allocate memory for shared secret size #{@struct.length_shared_secret}" if shared_d.null?
116
+
117
+ # shared_x = Fiddle::Pointer.malloc(@struct.length_shared_secret, Fiddle::RUBY_FREE)
118
+ #
119
+ # p shared_e.ptr == shared_d.ptr
120
+
121
+ # rb = shared_e[0, shared_e.size]
122
+ # p rb
123
+
124
+ # rv = KEMWrapper.OQS_KEM_encaps(@struct, @cipher, shared_e, @pubKey)
125
+ # raise Error, "Error in encapsulation" if rv != KEMWrapper::OQS_SUCCESS
126
+
127
+ # rb = shared_e[0, shared_e.size]
128
+ # p rb
129
+
130
+ # p shared_e.ptr == shared_d.ptr
131
+
132
+ # rv = KEMWrapper.OQS_KEM_decaps(@struct, shared_d, @cipher, @privKey)
133
+ # raise Error, "Error in decapsulation" if rv != KEMWrapper::OQS_SUCCESS
134
+
135
+ # p shared_e.ptr == shared_d.ptr
136
+
137
+ # p shared_d.size
138
+ # rb = shared_d[0, shared_d.size]
139
+ # p rb
140
+
141
+
142
+ #end
143
+
144
+ end
145
+
146
+ end
@@ -0,0 +1,28 @@
1
+
2
+ require 'fiddle'
3
+ require 'fiddle/import'
4
+
5
+ require_relative 'wrapper'
6
+
7
+ module Oqs
8
+ module KEMWrapper
9
+ extend Fiddle::Importer
10
+ include Oqs::Wrapper
11
+
12
+ #dlload File.join(File.dirname(__FILE__),"..","..","native","linux","x86_64","liboqs.so.0.7.0")
13
+ load_oqs_lib
14
+
15
+ extern 'const char * OQS_KEM_alg_identifier(size_t i)'
16
+ extern 'int OQS_KEM_alg_count(void)'
17
+ extern 'int OQS_KEM_alg_is_enabled(const char * name)'
18
+
19
+ extern 'OQS_KEM * OQS_KEM_new(const char * algo)'
20
+ extern 'void OQS_KEM_free(OQS_KEM * kem)'
21
+
22
+ extern 'int OQS_KEM_keypair(const OQS_KEM *kem, uint8_t *public_key, uint8_t *secret_key)'
23
+
24
+ extern 'int OQS_KEM_encaps(const OQS_KEM *kem, uint8_t * ciphertext, uint8_t *shared_secret, uint8_t *public_key)'
25
+ extern 'int OQS_KEM_decaps(const OQS_KEM *kem, uint8_t * shared_secret, uint8_t *ciphertext, uint8_t *secret_key)'
26
+
27
+ end
28
+ end
data/lib/oqs/sig.rb ADDED
@@ -0,0 +1,99 @@
1
+
2
+ require_relative 'struct'
3
+ require_relative 'sig_wrapper'
4
+ require_relative 'common_wrapper'
5
+
6
+ module Oqs
7
+ class SIG
8
+
9
+ def self.supported_signature_algo
10
+ ttl = SIGWrapper.OQS_SIG_alg_count
11
+ supported = []
12
+ (0...ttl).each do |i|
13
+ pName = SIGWrapper.OQS_SIG_alg_identifier(i)
14
+ name = pName.to_s
15
+ st = SIGWrapper.OQS_SIG_alg_is_enabled(name)
16
+ if st
17
+ supported << name
18
+ end
19
+ end
20
+
21
+ supported
22
+ end
23
+
24
+ def initialize(name)
25
+ @algo = name
26
+ oqsSig = SIGWrapper.OQS_SIG_new(@algo)
27
+ raise Error, "Unable to create object '#{@algo}'. It is either the algorithm not supported or it is disabled at compile time." if oqsSig.null?
28
+ @struct = OQS_SIG.new(oqsSig)
29
+ end
30
+
31
+ def cleanup
32
+ SIGWrapper.OQS_SIG_free(@struct) if not @struct.nil?
33
+ end
34
+
35
+ def free(obj)
36
+ obj.free if not (obj.nil? and obj.null?)
37
+ end
38
+
39
+ def intrinsic_name
40
+ @struct.intrinsic_name.to_s
41
+ end
42
+
43
+ def algo_version
44
+ @struct.algo_version.to_s
45
+ end
46
+
47
+ def method_missing(mtd, *args, &block)
48
+ @struct.send(mtd) if not @struct.nil? and @struct.respond_to?(mtd)
49
+ end
50
+
51
+ def genkeypair
52
+ pubKey = Fiddle::Pointer.malloc(@struct.length_public_key, Fiddle::RUBY_FREE)
53
+ raise Error, "Unable to allocate memory for public key size #{@struct.length_public_key}" if pubKey.null?
54
+ privKey = Fiddle::Pointer.malloc(@struct.length_secret_key, Fiddle::RUBY_FREE)
55
+ raise Error, "Unable to allocate memory for secret key size #{@struct.length_secret_key}" if privKey.null?
56
+
57
+ rv = SIGWrapper.OQS_SIG_keypair(@struct, pubKey, privKey)
58
+ raise Error, "Error in generation of keypair" if rv != Oqs::OQS_SUCCESS
59
+
60
+ [pubKey, privKey]
61
+ end
62
+
63
+ def verify(message,signature,pubKey)
64
+
65
+ pMessage = Fiddle::Pointer.to_ptr(message)
66
+ pSignature = Fiddle::Pointer.to_ptr(signature)
67
+
68
+ rv = SIGWrapper.OQS_SIG_verify(@struct, pMessage, message.length, pSignature, signature.length, pubKey)
69
+
70
+ rv == Oqs::OQS_SUCCESS
71
+
72
+ end
73
+
74
+ def sign(message, privKey)
75
+
76
+ raise Error, "Private key cannot be nil" if privKey.nil?
77
+
78
+ signature = Fiddle::Pointer.malloc(@struct.length_signature, Fiddle::RUBY_FREE)
79
+ raise Error, "Unable to allocate memory for signature size #{@struct.length_signature}" if signature.null?
80
+ signLen = Fiddle::Pointer.malloc(Fiddle::SIZEOF_SIZE_T, Fiddle::RUBY_FREE)
81
+ raise Error, "Unable to allocate memory for signature length size #{Fiddle::SIZEOF_SIZE_T}" if signLen.null?
82
+
83
+ pMessage = Fiddle::Pointer.to_ptr(message)
84
+
85
+ rv = SIGWrapper.OQS_SIG_sign(@struct, signature, signLen, pMessage, message.length, privKey)
86
+ raise Error, "Error in signing" if rv != Oqs::OQS_SUCCESS
87
+
88
+ signBin = signature[0, signLen.ptr.to_i]
89
+
90
+ signLen.free
91
+ signature.free
92
+
93
+ signBin
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
@@ -0,0 +1,31 @@
1
+
2
+ require 'fiddle'
3
+ require 'fiddle/import'
4
+
5
+ module Oqs
6
+ module SIGWrapper
7
+ extend Fiddle::Importer
8
+ include Oqs::Wrapper
9
+
10
+ ## OQS_STATUS
11
+ #OQS_ERROR = -1
12
+ #OQS_SUCCESS = 0
13
+ #OQS_EXTERNAL_LIB_ERROR_OPENSSL = 50
14
+
15
+ #dlload File.join(File.dirname(__FILE__),"..","..","native","linux","x86_64","liboqs.so.0.7.0")
16
+ load_oqs_lib
17
+
18
+ extern 'const char * OQS_SIG_alg_identifier(size_t i)'
19
+ extern 'int OQS_SIG_alg_count(void)'
20
+ extern 'int OQS_SIG_alg_is_enabled(const char * name)'
21
+
22
+ extern 'OQS_SIG * OQS_SIG_new(const char * algo)'
23
+ extern 'void OQS_SIG_free(OQS_SIG * sig)'
24
+
25
+ extern 'int OQS_SIG_keypair(const OQS_SIG *sig, uint8_t *public_key, uint8_t *secret_key)'
26
+
27
+ extern 'int OQS_SIG_sign(const OQS_SIG *sig, uint8_t * signature, size_t *signature_len, uint8_t *message, size_t message_len, uint8_t *secret_key)'
28
+ extern 'int OQS_SIG_verify(const OQS_SIG *sig, uint8_t * message, size_t message_len, uint8_t *signature, size_t signature_len, uint8_t *public_key)'
29
+
30
+ end
31
+ end
data/lib/oqs/struct.rb ADDED
@@ -0,0 +1,35 @@
1
+
2
+ require 'fiddle'
3
+ require 'fiddle/import'
4
+
5
+ module Oqs
6
+ extend Fiddle::Importer
7
+
8
+ OQS_KEM = struct [
9
+ "const char * intrinsic_name",
10
+ "const char * algo_version",
11
+ "uint8_t claimed_nist_level",
12
+ "int ind_cca",
13
+ "size_t length_public_key",
14
+ "size_t length_secret_key",
15
+ "size_t length_ciphertext",
16
+ "size_t length_shared_secret",
17
+ "int (*keypair)(uint8_t *pubKey, uint8_t* secretKey)",
18
+ "int (*encaps)(uint8_t *cipher_text, uint8_t* shared_secret, const unit8_t * pubKey)",
19
+ "int (*decaps)(uint8_t *shared_secret, uint8_t* cipher_text, const unit8_t * secretKey)"
20
+ ]
21
+
22
+ OQS_SIG = struct [
23
+ "const char * intrinsic_name",
24
+ "const char * algo_version",
25
+ "uint8_t claimed_nist_level",
26
+ "int euf_cma",
27
+ "size_t length_public_key",
28
+ "size_t length_secret_key",
29
+ "size_t length_signature",
30
+ "int (*keypair)(uint8_t *pubKey, uint8_t* secretKey)",
31
+ "int (*sign)(uint8_t *signature, size_t signature_len, const uint8_t* message, size_t message_len, const unit8_t * secretKey)",
32
+ "int (*verify)(uint8_t *message, size_t message_len, const uint8_t* signature, size_t signature_len, const unit8_t * pubKey)"
33
+ ]
34
+
35
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Oqs
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,58 @@
1
+
2
+ require 'fiddle'
3
+
4
+ require_relative "global"
5
+
6
+ module Oqs
7
+ module Wrapper
8
+ class WrapperError < StandardError; end
9
+
10
+ module ClassMethods
11
+ def load_oqs_lib
12
+ os = detect_os
13
+ logger.tdebug :wrapper, "Found OS #{os}"
14
+ load_arch_lib(os)
15
+ end
16
+
17
+ def detect_os
18
+ plat = RUBY_PLATFORM
19
+ if plat =~ /linux/
20
+ :linux
21
+ elsif plat =~ /darwin/
22
+ :macos
23
+ elsif plat =~ /mingw/
24
+ :windows
25
+ else
26
+ raise WrapperError, "Unknown platform detected. [#{plat}]"
27
+ end
28
+ end
29
+
30
+ def load_arch_lib(os)
31
+ plat = RUBY_PLATFORM
32
+ pplat = plat.split('-')[0]
33
+ logger.tdebug :wrapper, "OS architecture is #{pplat}"
34
+ drvDir = File.join(File.dirname(__FILE__),"..","..","native","#{os}",pplat)
35
+ if File.exist?(drvDir)
36
+ Dir.glob(File.join(drvDir,"liboqs*")) do |f|
37
+ logger.tdebug :wrapper, "Loading #{f} from system"
38
+ dlload f
39
+ end
40
+ else
41
+ raise WrapperError, "Directory '#{pplat}' not found at #{drvDir}"
42
+ end
43
+ end
44
+
45
+ def logger
46
+ Oqs::Global.instance.logger
47
+ end
48
+ end
49
+ def self.included(klass)
50
+ klass.extend(ClassMethods)
51
+ end
52
+
53
+ def logger
54
+ self.class.logger
55
+ end
56
+
57
+ end
58
+ end
Binary file
Binary file
data/oqs.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/oqs/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "liboqs"
7
+ spec.version = Oqs::VERSION
8
+ spec.authors = ["Chris Liaw"]
9
+ spec.email = ["chrisliaw@antrapol.com"]
10
+
11
+ spec.summary = "Ruby wrapper for Open Quantum Safe library"
12
+ spec.description = "Ruby wrapper for liboqs from Open Quantum Safe library. This version included platform binary of Linux, MacOS and Windows based on git commit b803b54179c1cea9091d9331cc8085fc235e1be4"
13
+ spec.homepage = "https://github.com/chrisliaw/liboqs-ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/chrisliaw/liboqs-ruby.git"
21
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.files += Dir.glob(File.join(__dir__,"native","**/*oqs*"))
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency 'tlogger'
34
+
35
+ spec.add_development_dependency "devops_helper"
36
+
37
+ # Uncomment to register a new dependency of your gem
38
+ # spec.add_dependency "example-gem", "~> 1.0"
39
+
40
+ # For more information and examples about making a new gem, checkout our
41
+ # guide at: https://bundler.io/guides/creating_gem.html
42
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liboqs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Liaw
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tlogger
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: devops_helper
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
+ description: Ruby wrapper for liboqs from Open Quantum Safe library. This version
42
+ included platform binary of Linux, MacOS and Windows based on git commit b803b54179c1cea9091d9331cc8085fc235e1be4
43
+ email:
44
+ - chrisliaw@antrapol.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".gitlab-ci.yml"
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - "/mnt/Vault/08.Dev/01.Workspaces/cryptolib.rebooted/liboqs/oqs/native/linux/x86_64/liboqs.so.0.7.0"
54
+ - "/mnt/Vault/08.Dev/01.Workspaces/cryptolib.rebooted/liboqs/oqs/native/macos/x86_64/liboqs.0.7.0.dylib"
55
+ - "/mnt/Vault/08.Dev/01.Workspaces/cryptolib.rebooted/liboqs/oqs/native/windows/x64/liboqs.dll"
56
+ - CHANGELOG.md
57
+ - CODE_OF_CONDUCT.md
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE.txt
61
+ - README.md
62
+ - Rakefile
63
+ - bin/console
64
+ - bin/setup
65
+ - lib/oqs.rb
66
+ - lib/oqs/common_wrapper.rb
67
+ - lib/oqs/global.rb
68
+ - lib/oqs/kem.rb
69
+ - lib/oqs/kem_wrapper.rb
70
+ - lib/oqs/sig.rb
71
+ - lib/oqs/sig_wrapper.rb
72
+ - lib/oqs/struct.rb
73
+ - lib/oqs/version.rb
74
+ - lib/oqs/wrapper.rb
75
+ - native/linux/x86_64/liboqs.so.0.7.0
76
+ - native/macos/x86_64/liboqs.0.7.0.dylib
77
+ - native/windows/x64/liboqs.dll
78
+ - oqs.gemspec
79
+ homepage: https://github.com/chrisliaw/liboqs-ruby
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://github.com/chrisliaw/liboqs-ruby
84
+ source_code_uri: https://github.com/chrisliaw/liboqs-ruby.git
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.4.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.2.22
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Ruby wrapper for Open Quantum Safe library
104
+ test_files: []