ccipher_factory 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/.rspec +3 -0
- data/Gemfile +30 -0
- data/Gemfile.lock-java +65 -0
- data/Gemfile.lock-ruby +67 -0
- data/README.md +80 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/ccipher_factory.gemspec +46 -0
- data/lib/ccipher_factory/asymkey/asymkey.rb +16 -0
- data/lib/ccipher_factory/asymkey/asymkey_generator.rb +87 -0
- data/lib/ccipher_factory/asymkey/ecc_keypair.rb +56 -0
- data/lib/ccipher_factory/asymkey_cipher/asymkey_cipher.rb +63 -0
- data/lib/ccipher_factory/asymkey_cipher/asymkey_signer.rb +44 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_decrypt.rb +55 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_encrypt.rb +70 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_signer.rb +88 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_verifier.rb +100 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_decrypt.rb +80 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_encrypt.rb +101 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_signer.rb +80 -0
- data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_verifier.rb +56 -0
- data/lib/ccipher_factory/composite_cipher/composite_cipher.rb +28 -0
- data/lib/ccipher_factory/composite_cipher/decrypt_verifier.rb +116 -0
- data/lib/ccipher_factory/composite_cipher/sign_encryptor.rb +100 -0
- data/lib/ccipher_factory/compression/compression_helper.rb +103 -0
- data/lib/ccipher_factory/compression/compressor.rb +55 -0
- data/lib/ccipher_factory/compression/zlib_compressor.rb +48 -0
- data/lib/ccipher_factory/compression/zlib_decompressor.rb +67 -0
- data/lib/ccipher_factory/digest/digest.rb +180 -0
- data/lib/ccipher_factory/digest/supported_digest.rb +47 -0
- data/lib/ccipher_factory/encoding/asn1.rb +43 -0
- data/lib/ccipher_factory/encoding/bin_struct.rb +207 -0
- data/lib/ccipher_factory/encoding/binenc_constant.rb +149 -0
- data/lib/ccipher_factory/helpers/common.rb +124 -0
- data/lib/ccipher_factory/kcv/kcv.rb +89 -0
- data/lib/ccipher_factory/kdf/hkdf.rb +114 -0
- data/lib/ccipher_factory/kdf/kdf.rb +73 -0
- data/lib/ccipher_factory/kdf/pbkdf2.rb +82 -0
- data/lib/ccipher_factory/kdf/scrypt.rb +105 -0
- data/lib/ccipher_factory/shamir/shamir_sharing.rb +293 -0
- data/lib/ccipher_factory/shamir/shamir_sharing_helper.rb +88 -0
- data/lib/ccipher_factory/symkey/derived_symkey.rb +110 -0
- data/lib/ccipher_factory/symkey/hardware_symkey.rb +0 -0
- data/lib/ccipher_factory/symkey/soft_symkey.rb +63 -0
- data/lib/ccipher_factory/symkey/symkey.rb +122 -0
- data/lib/ccipher_factory/symkey/symkey_generator.rb +70 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_att_decrypt.rb +64 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_att_encrypt.rb +65 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_att_sign.rb +84 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_att_verify.rb +85 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_cipher.rb +101 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb +144 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb +164 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_sign.rb +70 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_signer.rb +59 -0
- data/lib/ccipher_factory/symkey_cipher/symkey_verify.rb +76 -0
- data/lib/ccipher_factory/version.rb +5 -0
- data/lib/ccipher_factory.rb +52 -0
- data/run_test.rb +27 -0
- metadata +172 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module CcipherFactory
|
4
|
+
module Encoding
|
5
|
+
class EncoderError < StandardError; end
|
6
|
+
|
7
|
+
class InsufficientData < StandardError; end
|
8
|
+
|
9
|
+
def self.extract_meta(buf, &block)
|
10
|
+
|
11
|
+
cpos = buf.pos
|
12
|
+
|
13
|
+
begin
|
14
|
+
|
15
|
+
#len = find_asn1_length(buf.string)
|
16
|
+
len = Ccrypto::ASN1.engine.asn1_length(buf.bytes)
|
17
|
+
#logger.debug "Found meta length : #{len}" if not logger.nil?
|
18
|
+
raise InsufficientData if len == 0
|
19
|
+
|
20
|
+
buf.rewind
|
21
|
+
meta = buf.read(len)
|
22
|
+
|
23
|
+
if block
|
24
|
+
block.call(meta, buf.read(cpos-len))
|
25
|
+
else
|
26
|
+
meta
|
27
|
+
end
|
28
|
+
|
29
|
+
#rescue OpenSSL::ASN1::ASN1Error => ex
|
30
|
+
rescue Ccrypto::ASN1EngineException => ex
|
31
|
+
logger.error ex
|
32
|
+
buf.seek(cpos)
|
33
|
+
raise InsufficientData
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#require_relative 'asn1_encoder'
|
42
|
+
#require_relative 'asn1_decoder'
|
43
|
+
|
@@ -0,0 +1,207 @@
|
|
1
|
+
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
module CcipherFactory
|
5
|
+
class BinStruct
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def struct(key, ver = "1.0")
|
9
|
+
st = structure(ver)[key]
|
10
|
+
st.clone if not st.nil?
|
11
|
+
end
|
12
|
+
|
13
|
+
def struct_from_bin(bin)
|
14
|
+
bs = Binenc::EngineFactory.instance(:bin_struct)
|
15
|
+
type, ver = bs.value_from_bin_struct(bin, 0, 1)
|
16
|
+
c = BTag.value_constant(type)
|
17
|
+
st = struct(c, translate_version(ver))
|
18
|
+
st.from_bin(bin) if not st.nil?
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def logger
|
23
|
+
if @logger.nil?
|
24
|
+
@logger = TeLogger::Tlogger.new
|
25
|
+
@logger.tag = :binstruct
|
26
|
+
end
|
27
|
+
@logger
|
28
|
+
end
|
29
|
+
|
30
|
+
def structure(ver = "1.0")
|
31
|
+
|
32
|
+
if @struct.nil?
|
33
|
+
@struct = { }
|
34
|
+
|
35
|
+
@struct["1.0"] = {
|
36
|
+
|
37
|
+
compression_none: Binenc::EngineFactory.instance(:bin_struct).define do
|
38
|
+
oid :oid, BTag.constant_value(:compression_none)
|
39
|
+
int :version, 0x0100
|
40
|
+
end,
|
41
|
+
|
42
|
+
compression_zlib: Binenc::EngineFactory.instance(:bin_struct).define do
|
43
|
+
oid :oid, BTag.constant_value(:compression_zlib)
|
44
|
+
int :version, 0x0100
|
45
|
+
end,
|
46
|
+
|
47
|
+
digest: Binenc::EngineFactory.instance(:bin_struct).define do
|
48
|
+
oid :oid, BTag.constant_value(:digest)
|
49
|
+
int :version, 0x0100
|
50
|
+
int :digest_algo
|
51
|
+
bin :salt
|
52
|
+
end,
|
53
|
+
|
54
|
+
digest_attached: Binenc::EngineFactory.instance(:bin_struct).define do
|
55
|
+
oid :oid, BTag.constant_value(:digest_attached)
|
56
|
+
int :version, 0x0100
|
57
|
+
bin :digest_config
|
58
|
+
bin :digest_value
|
59
|
+
end,
|
60
|
+
|
61
|
+
ecc_att_sign: Binenc::EngineFactory.instance(:bin_struct).define do
|
62
|
+
oid :oid, BTag.constant_value(:ecc_att_sign)
|
63
|
+
int :version, 0x0100
|
64
|
+
bin :ecc_signature
|
65
|
+
bin :compression
|
66
|
+
end,
|
67
|
+
|
68
|
+
ecc_cipher: Binenc::EngineFactory.instance(:bin_struct).define do
|
69
|
+
oid :oid, BTag.constant_value(:ecc_cipher)
|
70
|
+
int :version, 0x0100
|
71
|
+
bin :sender_public
|
72
|
+
bin :cipher_config
|
73
|
+
bin :key_config
|
74
|
+
end,
|
75
|
+
|
76
|
+
ecc_signature: Binenc::EngineFactory.instance(:bin_struct).define do
|
77
|
+
oid :oid, BTag.constant_value(:ecc_signature)
|
78
|
+
int :version, 0x0100
|
79
|
+
bin :digest_info
|
80
|
+
bin :signer_info
|
81
|
+
bin :signature
|
82
|
+
end,
|
83
|
+
|
84
|
+
ecc_signer_info: Binenc::EngineFactory.instance(:bin_struct).define do
|
85
|
+
oid :oid, BTag.constant_value(:ecc_signer_info)
|
86
|
+
int :version, 0x0100
|
87
|
+
int :signer_info_type, BTag.constant_value(:public_key)
|
88
|
+
bin :signer_info_value
|
89
|
+
end,
|
90
|
+
|
91
|
+
kcv: Binenc::EngineFactory.instance(:bin_struct).define do
|
92
|
+
oid :oid, BTag.constant_value(:kcv)
|
93
|
+
int :version, 0x0100
|
94
|
+
int :mode
|
95
|
+
bin :iv
|
96
|
+
bin :nonce
|
97
|
+
bin :check_value
|
98
|
+
end,
|
99
|
+
|
100
|
+
kdf_hkdf: Binenc::EngineFactory.instance(:bin_struct).define do
|
101
|
+
oid :oid, BTag.constant_value(:kdf_hkdf)
|
102
|
+
int :version, 0x0100
|
103
|
+
int :digest
|
104
|
+
int :outByteLength
|
105
|
+
bin :salt
|
106
|
+
end,
|
107
|
+
|
108
|
+
kdf_scrypt: Binenc::EngineFactory.instance(:bin_struct).define do
|
109
|
+
oid :oid, BTag.constant_value(:kdf_scrypt)
|
110
|
+
int :version, 0x0100
|
111
|
+
bin :digest
|
112
|
+
bin :salt
|
113
|
+
int :cost
|
114
|
+
int :blocksize
|
115
|
+
int :parallel
|
116
|
+
int :outByteLength
|
117
|
+
end,
|
118
|
+
|
119
|
+
|
120
|
+
kdf_pbkdf2: Binenc::EngineFactory.instance(:bin_struct).define do
|
121
|
+
oid :oid, BTag.constant_value(:kdf_pbkdf2)
|
122
|
+
int :version, 0x0100
|
123
|
+
int :digest
|
124
|
+
bin :salt
|
125
|
+
int :iterations
|
126
|
+
int :outByteLength
|
127
|
+
end,
|
128
|
+
|
129
|
+
|
130
|
+
shared_secret: Binenc::EngineFactory.instance(:bin_struct).define do
|
131
|
+
oid :oid, BTag.constant_value(:shared_secret)
|
132
|
+
int :version, 0x0100
|
133
|
+
int :req_share
|
134
|
+
int :share_id
|
135
|
+
bin :serial
|
136
|
+
bin :shared_value
|
137
|
+
end,
|
138
|
+
|
139
|
+
sign_encrypt_cipher: Binenc::EngineFactory.instance(:bin_struct).define do
|
140
|
+
oid :oid, BTag.constant_value(:sign_encrypt_cipher)
|
141
|
+
int :version, 0x0100
|
142
|
+
bin :signer_config
|
143
|
+
bin :cipher_config
|
144
|
+
end,
|
145
|
+
|
146
|
+
symkey: Binenc::EngineFactory.instance(:bin_struct).define do
|
147
|
+
oid :oid, BTag.constant_value(:symkey)
|
148
|
+
int :version, 0x0100
|
149
|
+
int :keytype
|
150
|
+
int :keysize
|
151
|
+
bin :key
|
152
|
+
end,
|
153
|
+
|
154
|
+
symkey_att_sign: Binenc::EngineFactory.instance(:bin_struct).define do
|
155
|
+
oid :oid, BTag.constant_value(:symkey_att_sign)
|
156
|
+
int :version, 0x0100
|
157
|
+
bin :symkey_signature
|
158
|
+
bin :compression
|
159
|
+
end,
|
160
|
+
|
161
|
+
symkey_cipher: Binenc::EngineFactory.instance(:bin_struct).define do
|
162
|
+
oid :oid, BTag.constant_value(:symkey_cipher)
|
163
|
+
int :version, 0x0100
|
164
|
+
|
165
|
+
int :mode
|
166
|
+
bin :iv
|
167
|
+
bin :compression
|
168
|
+
bin :auth_tag
|
169
|
+
end,
|
170
|
+
|
171
|
+
symkey_derived: Binenc::EngineFactory.instance(:bin_struct).define do
|
172
|
+
oid :oid, BTag.constant_value(:symkey_derived)
|
173
|
+
int :version, 0x0100
|
174
|
+
|
175
|
+
int :keytype
|
176
|
+
int :keysize
|
177
|
+
bin :kdf_config
|
178
|
+
bin :kcv
|
179
|
+
end,
|
180
|
+
|
181
|
+
symkey_signature: Binenc::EngineFactory.instance(:bin_struct).define do
|
182
|
+
oid :oid, BTag.constant_value(:symkey_signature)
|
183
|
+
int :version, 0x0100
|
184
|
+
|
185
|
+
int :digest_algo
|
186
|
+
bin :signature
|
187
|
+
end,
|
188
|
+
|
189
|
+
}
|
190
|
+
end
|
191
|
+
|
192
|
+
@struct[ver]
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
def translate_version(ver)
|
197
|
+
case ver.to_i
|
198
|
+
when 0x0100
|
199
|
+
"1.0"
|
200
|
+
else
|
201
|
+
raise Exception, "Version #{ver} is unknown"
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
end
|
207
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
|
2
|
+
if not defined?(BTag)
|
3
|
+
|
4
|
+
BTag = Binenc::BinTag.new
|
5
|
+
|
6
|
+
BTag.load do
|
7
|
+
|
8
|
+
# hierarchy
|
9
|
+
define_constant(:root, '2.8.8') do
|
10
|
+
|
11
|
+
define_constant(:kdf, "#.10") do
|
12
|
+
define_constant(:kdf_scrypt, "#.0")
|
13
|
+
define_constant(:kdf_hkdf, "#.1")
|
14
|
+
define_constant(:kdf_pbkdf2, "#.2")
|
15
|
+
end
|
16
|
+
|
17
|
+
define_constant(:digest, "#.20")
|
18
|
+
define_constant(:digest_attached, "#.21")
|
19
|
+
|
20
|
+
define_constant(:symkey, "#.30") do
|
21
|
+
define_constant(:symkey_derived, "#.0")
|
22
|
+
define_constant(:plain_symkey, "#.1")
|
23
|
+
define_constant(:symkey_vtype_kdf, "#.2")
|
24
|
+
|
25
|
+
define_constant(:symkey_cipher, "#.20")
|
26
|
+
define_constant(:symkey_signature, "#.21")
|
27
|
+
define_constant(:symkey_att_sign, "#.22")
|
28
|
+
|
29
|
+
define_constant(:kcv, "#.30")
|
30
|
+
end
|
31
|
+
|
32
|
+
define_constant(:compression, "#.40") do
|
33
|
+
define_constant(:compression_none, "#.0")
|
34
|
+
define_constant(:compression_zlib, "#.1")
|
35
|
+
define_constant(:compression_zlib_attached, "#.2")
|
36
|
+
end
|
37
|
+
|
38
|
+
define_constant(:asymkey, "#.50") do
|
39
|
+
define_constant(:ecc, "#.1") do
|
40
|
+
define_constant(:ecc_cipher, "#.10")
|
41
|
+
define_constant(:ecc_signature, "#.11") do
|
42
|
+
define_constant(:ecc_signer_info, "#.1")
|
43
|
+
end
|
44
|
+
|
45
|
+
define_constant(:ecc_att_sign, "#.12")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
define_constant(:composite, "#.60") do
|
50
|
+
define_constant(:sign_encrypt_cipher, "#.1")
|
51
|
+
end
|
52
|
+
|
53
|
+
define_constant(:shared_secret, "#.70")
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
# constant
|
60
|
+
define_constant(:sha1, 0x0101)
|
61
|
+
define_constant(:sha256, 0x0102)
|
62
|
+
define_constant(:sha384, 0x0103)
|
63
|
+
define_constant(:sha224, 0x0104)
|
64
|
+
define_constant(:sha512, 0x0105)
|
65
|
+
define_constant(:sha512_224, 0x0106)
|
66
|
+
define_constant(:sha512_256, 0x0107)
|
67
|
+
|
68
|
+
define_constant(:sha3_256, 0x0110)
|
69
|
+
define_constant(:sha3_224, 0x0111)
|
70
|
+
define_constant(:sha3_384, 0x0112)
|
71
|
+
define_constant(:sha3_512, 0x0113)
|
72
|
+
|
73
|
+
define_constant(:shake128, 0x0120)
|
74
|
+
define_constant(:shake256, 0x0121)
|
75
|
+
|
76
|
+
define_constant(:blake2b160, 0x0130)
|
77
|
+
define_constant(:blake2b256, 0x0131)
|
78
|
+
define_constant(:blake2b384, 0x0132)
|
79
|
+
define_constant(:blake2b512, 0x0133)
|
80
|
+
|
81
|
+
define_constant(:blake2s128, 0x0134)
|
82
|
+
define_constant(:blake2s160, 0x0135)
|
83
|
+
define_constant(:blake2s224, 0x0136)
|
84
|
+
define_constant(:blake2s256, 0x0137)
|
85
|
+
|
86
|
+
define_constant(:haraka256, 0x0140)
|
87
|
+
define_constant(:haraka512, 0x0141)
|
88
|
+
|
89
|
+
define_constant(:shake128_256, 0x0142)
|
90
|
+
define_constant(:shake256_512, 0x0143)
|
91
|
+
|
92
|
+
define_constant(:sm3, 0x0144)
|
93
|
+
define_constant(:whirlpool, 0x0145)
|
94
|
+
|
95
|
+
define_constant(:keccak224, 0x0150)
|
96
|
+
define_constant(:keccak256, 0x0151)
|
97
|
+
define_constant(:keccak288, 0x0152)
|
98
|
+
define_constant(:keccak384, 0x0153)
|
99
|
+
define_constant(:keccak512, 0x0154)
|
100
|
+
|
101
|
+
define_constant(:ripemd128, 0x0160)
|
102
|
+
define_constant(:ripemd160, 0x0161)
|
103
|
+
define_constant(:ripemd256, 0x0162)
|
104
|
+
define_constant(:ripemd320, 0x0163)
|
105
|
+
|
106
|
+
define_constant(:skein1024_1024, 0x0170)
|
107
|
+
define_constant(:skein1024_384, 0x0171)
|
108
|
+
define_constant(:skein1024_512, 0x0172)
|
109
|
+
|
110
|
+
define_constant(:skein256_128, 0x0173)
|
111
|
+
define_constant(:skein256_160, 0x0174)
|
112
|
+
define_constant(:skein256_224, 0x0175)
|
113
|
+
define_constant(:skein256_256, 0x0176)
|
114
|
+
|
115
|
+
define_constant(:skein512_128, 0x0177)
|
116
|
+
define_constant(:skein512_160, 0x0178)
|
117
|
+
define_constant(:skein512_224, 0x0179)
|
118
|
+
define_constant(:skein512_256, 0x0180)
|
119
|
+
define_constant(:skein512_384, 0x0181)
|
120
|
+
define_constant(:skein512_512, 0x0182)
|
121
|
+
|
122
|
+
|
123
|
+
define_constant(:aes, 0x0201)
|
124
|
+
define_constant(:chacha20, 0x0202)
|
125
|
+
define_constant(:chacha20_poly1305, 0x0203)
|
126
|
+
define_constant(:blowfish, 0x0204)
|
127
|
+
define_constant(:camellia, 0x0205)
|
128
|
+
define_constant(:aria, 0x0206)
|
129
|
+
|
130
|
+
define_constant(:gcm, 0x0220)
|
131
|
+
define_constant(:cbc, 0x0221)
|
132
|
+
define_constant(:cfb, 0x0222)
|
133
|
+
define_constant(:ctr, 0x0223)
|
134
|
+
define_constant(:ccm, 0x0224)
|
135
|
+
|
136
|
+
define_constant(:ecb, 0x0225)
|
137
|
+
|
138
|
+
define_constant(:ofb, 0x0226)
|
139
|
+
define_constant(:ocb, 0x0227)
|
140
|
+
|
141
|
+
define_constant(:poly1305, 0x0228)
|
142
|
+
|
143
|
+
define_constant(:public_key, 0x0230)
|
144
|
+
define_constant(:x509_cert, 0x0231)
|
145
|
+
define_constant(:cf_cert, 0x0232)
|
146
|
+
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
module CcipherFactory
|
5
|
+
module Common
|
6
|
+
include TR::CondUtils
|
7
|
+
|
8
|
+
#
|
9
|
+
# output section
|
10
|
+
#
|
11
|
+
def output(output)
|
12
|
+
raise CcipherFactory::Error, "Output requires to support write(). StringIO is a good example." if output.nil? or not output.respond_to?(:write)
|
13
|
+
@output = output
|
14
|
+
end
|
15
|
+
|
16
|
+
def output_obj
|
17
|
+
@output
|
18
|
+
end
|
19
|
+
|
20
|
+
def write_to_output(val)
|
21
|
+
@output.write(val) if not @output.nil? and not_empty?(val)
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_output_given?
|
25
|
+
not @output.nil?
|
26
|
+
end
|
27
|
+
|
28
|
+
def intOutputBuf
|
29
|
+
if @intOutputBuf.nil?
|
30
|
+
@intOutputBuf = MemBuf.new
|
31
|
+
end
|
32
|
+
@intOutputBuf
|
33
|
+
end
|
34
|
+
|
35
|
+
def cleanup_intOutputBuf
|
36
|
+
if not @intOutputBuf.nil?
|
37
|
+
@intOutputBuf = nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def intOutputFile
|
42
|
+
if @intOutputFile.nil?
|
43
|
+
@intOutputFile = Tempfile.new
|
44
|
+
end
|
45
|
+
@intOutputFile
|
46
|
+
end
|
47
|
+
|
48
|
+
def cleanup_intOutputFile
|
49
|
+
if not @intOutputFile.nil?
|
50
|
+
@intOutputFile.close!
|
51
|
+
@intOutputFile = nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def disposeOutput(obj)
|
56
|
+
case obj
|
57
|
+
when intOutputBuf
|
58
|
+
cnt = 0
|
59
|
+
len = @intOutputBuf.length
|
60
|
+
loop do
|
61
|
+
@intOutputBuf.rewind
|
62
|
+
@intOutputBuf.write(SecureRandom.random_bytes(len))
|
63
|
+
cnt += 1
|
64
|
+
break if cnt >= 16
|
65
|
+
end
|
66
|
+
@intOutputBuf.rewind
|
67
|
+
@intOutputBuf = nil
|
68
|
+
when intOutputFile
|
69
|
+
@intOutputFile.close!
|
70
|
+
end
|
71
|
+
end
|
72
|
+
#
|
73
|
+
# end output section
|
74
|
+
#
|
75
|
+
|
76
|
+
#
|
77
|
+
# attached mode
|
78
|
+
# Flag to indicate if the result of the operation
|
79
|
+
# should be part of the header/meta data directly
|
80
|
+
# return from the API
|
81
|
+
#
|
82
|
+
def attach_mode
|
83
|
+
@attachMode = true
|
84
|
+
end
|
85
|
+
|
86
|
+
def detach_mode
|
87
|
+
@attachMode = false
|
88
|
+
end
|
89
|
+
|
90
|
+
def is_attach_mode?
|
91
|
+
if @attachMode.nil? or not is_bool?(@attachMode)
|
92
|
+
# default detach
|
93
|
+
# The impact is mainly on huge data encrypt/decrypt
|
94
|
+
# Returning the huge data through the API returned might
|
95
|
+
# have undetermined behaviour
|
96
|
+
false
|
97
|
+
else
|
98
|
+
@attachMode
|
99
|
+
end
|
100
|
+
end
|
101
|
+
#
|
102
|
+
# End attach mode section
|
103
|
+
#
|
104
|
+
|
105
|
+
|
106
|
+
def sanitize_symbol(sym, conv = :downcase)
|
107
|
+
if not_empty?(sym)
|
108
|
+
case conv
|
109
|
+
when :downcase
|
110
|
+
sym.to_s.downcase.to_sym
|
111
|
+
when :upcase
|
112
|
+
sym.to_s.upcase.to_sym
|
113
|
+
when :capitalize
|
114
|
+
sym.to_s.capitalize.to_sym
|
115
|
+
else
|
116
|
+
sym
|
117
|
+
end
|
118
|
+
else
|
119
|
+
sym
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
|
2
|
+
require_relative '../symkey_cipher/symkey_cipher'
|
3
|
+
require_relative '../symkey_cipher/symkey_encrypt'
|
4
|
+
|
5
|
+
module CcipherFactory
|
6
|
+
class KCV
|
7
|
+
include TR::CondUtils
|
8
|
+
include SymKeyCipher::SymKeyEncrypt
|
9
|
+
|
10
|
+
class KCVError < StandardError; end
|
11
|
+
|
12
|
+
attr_accessor :nonce, :check_value
|
13
|
+
|
14
|
+
def self.from_encoded(bin)
|
15
|
+
|
16
|
+
ts = BinStruct.instance.struct_from_bin(bin)
|
17
|
+
kcv = KCV.new
|
18
|
+
kcv.mode = BTag.constant_value(ts.mode)
|
19
|
+
kcv.iv = ts.iv
|
20
|
+
kcv.nonce = ts.nonce
|
21
|
+
kcv.check_value = ts.check_value
|
22
|
+
|
23
|
+
kcv
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.converter
|
28
|
+
if @conv.nil?
|
29
|
+
@conv = Ccrypto::UtilFactory.instance(:data_converter)
|
30
|
+
end
|
31
|
+
@conv
|
32
|
+
end
|
33
|
+
|
34
|
+
def is_matched?
|
35
|
+
logger.tdebug :kcv_match, "Check if KCV matched"
|
36
|
+
encoded
|
37
|
+
res = intOutputBuf.bytes
|
38
|
+
comp = Ccrypto::UtilFactory.instance(:comparator)
|
39
|
+
comp.is_equal?(@check_value, res)
|
40
|
+
end
|
41
|
+
|
42
|
+
def encoded(&block)
|
43
|
+
|
44
|
+
raise KCVError, "Key must be given" if is_empty?(@key)
|
45
|
+
|
46
|
+
logger.debug "Generating KCV"
|
47
|
+
compression_off
|
48
|
+
output(intOutputBuf)
|
49
|
+
|
50
|
+
if block
|
51
|
+
logger.debug "Block given"
|
52
|
+
@iv = block.call(:kcv_cipher_iv)
|
53
|
+
end
|
54
|
+
|
55
|
+
encrypt_init(@key)
|
56
|
+
|
57
|
+
if is_empty?(@nonce)
|
58
|
+
logger.debug "Random nounce"
|
59
|
+
@nonce = SecureRandom.random_bytes(@key.keysize)
|
60
|
+
else
|
61
|
+
logger.debug "Nounce is given"
|
62
|
+
end
|
63
|
+
|
64
|
+
encrypt_update(@nonce)
|
65
|
+
encrypt_final
|
66
|
+
|
67
|
+
ts = BinStruct.instance.struct(:kcv)
|
68
|
+
ts.mode = BTag.constant_value(@mode)
|
69
|
+
ts.iv = @iv
|
70
|
+
ts.nonce = @nonce
|
71
|
+
ts.check_value = intOutputBuf.bytes
|
72
|
+
|
73
|
+
ts.encoded
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.logger
|
78
|
+
if @logger.nil?
|
79
|
+
@logger = Tlogger.new
|
80
|
+
@logger.tag = :kcv
|
81
|
+
end
|
82
|
+
@logger
|
83
|
+
end
|
84
|
+
def logger
|
85
|
+
self.class.logger
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|