aez 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +37 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +13 -0
- data/aez.gemspec +33 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/aezv5/encrypt.c +943 -0
- data/ext/aezv5/extconf.rb +8 -0
- data/lib/aez.rb +76 -0
- data/lib/aez/version.rb +3 -0
- metadata +131 -0
data/lib/aez.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aez/version'
|
4
|
+
require 'ffi'
|
5
|
+
|
6
|
+
# AEZv5 ruby binding.
|
7
|
+
# [AEZv5](https://web.cs.ucdavis.edu/~rogaway/aez)
|
8
|
+
module AEZ
|
9
|
+
|
10
|
+
class Error; end
|
11
|
+
|
12
|
+
MAX_CIPHER_TXT_LENGTH = 2**32 - 1
|
13
|
+
|
14
|
+
extend FFI::Library
|
15
|
+
|
16
|
+
ffi_lib 'lib/aez/aezv5.so'
|
17
|
+
|
18
|
+
attach_function :aez_setup, [:pointer, :ulong_long, :pointer], :int
|
19
|
+
attach_function :aez_encrypt, [:pointer, :pointer, :uint, :pointer, :uint, :uint, :pointer, :uint, :pointer], :int
|
20
|
+
attach_function :aez_decrypt, [:pointer, :pointer, :uint, :pointer, :uint, :uint, :pointer, :uint, :pointer], :int
|
21
|
+
|
22
|
+
module_function
|
23
|
+
|
24
|
+
# Encrypt a message.
|
25
|
+
# @param [String] key key with binary format.
|
26
|
+
# @param [String] message message with binary format.
|
27
|
+
# @param [String] ad ad with binary format.
|
28
|
+
# @param [String] nonce nonce with binary format. The nonce length must be `1..=16`
|
29
|
+
# @param [Integer] abyte authenticator length which determines how much longer a ciphertext is than its plaintext.
|
30
|
+
# @return [String] cipher text with binary format. The ciphertext may be up to 16 bytes larger than the message,
|
31
|
+
# these extra bytes add authentication.
|
32
|
+
def encrypt(key, message, ad, nonce, abyte)
|
33
|
+
raise Error, 'invalid nonce.' if nonce.empty? || nonce.bytesize > 16
|
34
|
+
|
35
|
+
with_context(key) do |context|
|
36
|
+
message_m = message.empty? ? nil : FFI::MemoryPointer.new(:uchar, message.bytesize).put_bytes(0, message)
|
37
|
+
ad_m = ad.empty? ? nil : FFI::MemoryPointer.new(:char, ad.bytesize).put_bytes(0, ad)
|
38
|
+
nonce_m = FFI::MemoryPointer.new(:char, nonce.bytesize).put_bytes(0, nonce)
|
39
|
+
dest = FFI::MemoryPointer.new(:char, message.bytesize + abyte)
|
40
|
+
|
41
|
+
aez_encrypt(context, nonce_m, nonce.bytesize, ad_m, ad.bytesize, abyte, message_m, message.bytesize, dest)
|
42
|
+
dest.read_string(message.bytesize + abyte)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Decrypt a message.
|
47
|
+
# @param [String] key key with binary format.
|
48
|
+
# @param [String] ciphertxt cipher text with binary format. the ciphertext must not be larger than `2^32 - 1`.
|
49
|
+
# @param [String] ad ad with binary format.
|
50
|
+
# @param [String] nonce nonce with binary format. The nonce length must be `1..=16`.
|
51
|
+
# @param [Integer] abyte authenticator length which determines how much longer a ciphertext is than its plaintext.
|
52
|
+
# @return [String] plain text with binary format.
|
53
|
+
def decrypt(key, ciphertxt, ad, nonce, abyte)
|
54
|
+
raise Error, 'invalid nonce.' if nonce.empty? || nonce.bytesize > 16
|
55
|
+
raise Error, 'ciphertxt length too long.' unless ciphertxt.bytesize < MAX_CIPHER_TXT_LENGTH
|
56
|
+
|
57
|
+
with_context(key) do |context|
|
58
|
+
ciphertxt_m = FFI::MemoryPointer.new(:uchar, ciphertxt.bytesize).put_bytes(0, ciphertxt)
|
59
|
+
ad_m = ad.empty? ? nil : FFI::MemoryPointer.new(:char, ad.bytesize).put_bytes(0, ad)
|
60
|
+
nonce_m = FFI::MemoryPointer.new(:char, nonce.bytesize).put_bytes(0, nonce)
|
61
|
+
dest = FFI::MemoryPointer.new(:char, ciphertxt.bytesize - abyte)
|
62
|
+
result = aez_decrypt(context, nonce_m, nonce.bytesize, ad_m, ad.bytesize, abyte, ciphertxt_m, ciphertxt.bytesize, dest)
|
63
|
+
raise Error, 'decrypt failure.' unless result == 0
|
64
|
+
|
65
|
+
dest.read_string(ciphertxt.bytesize - abyte)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def with_context(key)
|
70
|
+
context = FFI::MemoryPointer.new(144)
|
71
|
+
key_m = FFI::MemoryPointer.new(:uchar, key.bytesize).put_bytes(0, key)
|
72
|
+
aez_setup(key_m, key.bytesize, context)
|
73
|
+
yield(context) if block_given?
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/aez/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aez
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shigeyuki Azuchi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ffi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.15.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.15.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
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: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 12.3.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.3.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.1.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 1.1.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: AEZ binding for ruby.
|
84
|
+
email:
|
85
|
+
- azuchi@chaintope.com
|
86
|
+
executables: []
|
87
|
+
extensions:
|
88
|
+
- ext/aezv5/extconf.rb
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".github/workflows/ruby.yml"
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".ruby-gemset"
|
95
|
+
- ".ruby-version"
|
96
|
+
- CODE_OF_CONDUCT.md
|
97
|
+
- Gemfile
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- aez.gemspec
|
102
|
+
- bin/console
|
103
|
+
- bin/setup
|
104
|
+
- ext/aezv5/encrypt.c
|
105
|
+
- ext/aezv5/extconf.rb
|
106
|
+
- lib/aez.rb
|
107
|
+
- lib/aez/version.rb
|
108
|
+
homepage: https://github.com/azuchi/aez
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
requirements: []
|
127
|
+
rubygems_version: 3.2.3
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: AEZ binding for ruby.
|
131
|
+
test_files: []
|