ccrypto-java 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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.java-version +1 -1
  3. data/.release_history.yml +4 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +1 -1
  6. data/Gemfile.lock +68 -53
  7. data/Rakefile +2 -1
  8. data/bin/console +14 -0
  9. data/jars/bcjmail-jdk18on-172.jar +0 -0
  10. data/jars/bcmail-jdk18on-172.jar +0 -0
  11. data/jars/bcpg-jdk18on-172.1.jar +0 -0
  12. data/jars/bcpkix-jdk18on-172.jar +0 -0
  13. data/jars/bcprov-ext-jdk18on-172.jar +0 -0
  14. data/jars/bcprov-jdk18on-172.jar +0 -0
  15. data/jars/bctls-jdk18on-172.jar +0 -0
  16. data/jars/bcutil-jdk18on-172.jar +0 -0
  17. data/lib/ccrypto/java/bc_const_mapping.rb +42 -0
  18. data/lib/ccrypto/java/data_conversion.rb +23 -2
  19. data/lib/ccrypto/java/engines/argon2_engine.rb +95 -0
  20. data/lib/ccrypto/java/engines/asn1_engine.rb +2 -1
  21. data/lib/ccrypto/java/engines/bcrypt_engine.rb +56 -0
  22. data/lib/ccrypto/java/engines/cipher_engine.rb +462 -130
  23. data/lib/ccrypto/java/engines/compression_engine.rb +7 -28
  24. data/lib/ccrypto/java/engines/crystal_dilithium_engine.rb +226 -0
  25. data/lib/ccrypto/java/engines/crystal_kyber_engine.rb +260 -0
  26. data/lib/ccrypto/java/engines/decompression_engine.rb +5 -4
  27. data/lib/ccrypto/java/engines/digest_engine.rb +221 -139
  28. data/lib/ccrypto/java/engines/ecc_engine.rb +249 -96
  29. data/lib/ccrypto/java/engines/ed25519_engine.rb +211 -0
  30. data/lib/ccrypto/java/engines/hkdf_engine.rb +82 -23
  31. data/lib/ccrypto/java/engines/hmac_engine.rb +98 -23
  32. data/lib/ccrypto/java/engines/pbkdf2_engine.rb +82 -33
  33. data/lib/ccrypto/java/engines/pkcs7_engine.rb +44 -33
  34. data/lib/ccrypto/java/engines/rsa_engine.rb +85 -31
  35. data/lib/ccrypto/java/engines/scrypt_engine.rb +12 -3
  36. data/lib/ccrypto/java/engines/secret_key_engine.rb +77 -12
  37. data/lib/ccrypto/java/engines/secret_sharing_engine.rb +17 -2
  38. data/lib/ccrypto/java/engines/x25519_engine.rb +249 -0
  39. data/lib/ccrypto/java/engines/x509_csr_engine.rb +141 -0
  40. data/lib/ccrypto/java/engines/x509_engine.rb +365 -71
  41. data/lib/ccrypto/java/ext/secret_key.rb +37 -25
  42. data/lib/ccrypto/java/ext/x509_cert.rb +429 -5
  43. data/lib/ccrypto/java/ext/x509_csr.rb +151 -0
  44. data/lib/ccrypto/java/jce_provider.rb +0 -11
  45. data/lib/ccrypto/java/keystore/jce_keystore.rb +205 -0
  46. data/lib/ccrypto/java/keystore/jks_keystore.rb +52 -0
  47. data/lib/ccrypto/java/keystore/keystore.rb +97 -0
  48. data/lib/ccrypto/java/keystore/pem_keystore.rb +147 -0
  49. data/lib/ccrypto/java/keystore/pkcs12_keystore.rb +56 -0
  50. data/lib/ccrypto/java/utils/comparator.rb +25 -2
  51. data/lib/ccrypto/java/version.rb +1 -1
  52. data/lib/ccrypto/java.rb +46 -0
  53. data/lib/ccrypto/provider.rb +139 -3
  54. metadata +40 -24
  55. data/ccrypto-java.gemspec +0 -44
  56. data/jars/bcmail-jdk15on-165.jar +0 -0
  57. data/jars/bcpg-jdk15on-165.jar +0 -0
  58. data/jars/bcpkix-jdk15on-165.jar +0 -0
  59. data/jars/bcprov-ext-jdk15on-165.jar +0 -0
  60. data/jars/bcprov-jdk15on-165.jar +0 -0
  61. data/jars/bctls-jdk15on-165.jar +0 -0
  62. data/lib/ccrypto/java/keybundle_store/pkcs12.rb +0 -125
@@ -7,169 +7,250 @@ module Ccrypto
7
7
  include TR::CondUtils
8
8
  include DataConversion
9
9
 
10
- include TeLogger::TeLogHelper
11
-
12
- teLogger_tag :j_digest
13
-
14
- Potential = [
15
-
16
- Ccrypto::SHA1.provider_info("SHA-1"),
17
- Ccrypto::SHA224.provider_info("SHA-224"),
18
- Ccrypto::SHA256.provider_info("SHA-256"),
19
- Ccrypto::SHA384.provider_info("SHA-384"),
20
- Ccrypto::SHA512.provider_info("SHA-512"),
21
- Ccrypto::SHA512_224.provider_info("SHA-512/224"),
22
- Ccrypto::SHA512_256.provider_info("SHA-512/256"),
23
-
24
- Ccrypto::SHA3_224.provider_info("SHA3-224"),
25
- Ccrypto::SHA3_256.provider_info("SHA3-256"),
26
- Ccrypto::SHA3_384.provider_info("SHA3-384"),
27
- Ccrypto::SHA3_512.provider_info("SHA3-512"),
28
-
29
- Ccrypto::BLAKE2b160.provider_info("BLAKE2B-160"),
30
- Ccrypto::BLAKE2b256.provider_info("BLAKE2B-256"),
31
- Ccrypto::BLAKE2b384.provider_info("BLAKE2B-384"),
32
- Ccrypto::BLAKE2b512.provider_info("BLAKE2B-512"),
33
-
34
- Ccrypto::BLAKE2s128.provider_info("BLAKE2S-128"),
35
- Ccrypto::BLAKE2s160.provider_info("BLAKE2s-160"),
36
- Ccrypto::BLAKE2s224.provider_info("BLAKE2s-224"),
37
- Ccrypto::BLAKE2s256.provider_info("BLAKE2s-256"),
38
-
39
- Ccrypto::HARAKA256.provider_info("HARAKA-256"),
40
- Ccrypto::HARAKA512.provider_info("HARAKA-512"),
41
-
42
- Ccrypto::KECCAK224.provider_info("KECCAK-224"),
43
- Ccrypto::KECCAK256.provider_info("KECCAK-256"),
44
- Ccrypto::KECCAK288.provider_info("KECCAK-288"),
45
- Ccrypto::KECCAK384.provider_info("KECCAK-384"),
46
- Ccrypto::KECCAK512.provider_info("KECCAK-512"),
47
-
48
- Ccrypto::RIPEMD128.provider_info("RIPEMD128"),
49
- Ccrypto::RIPEMD160.provider_info("RIPEMD160"),
50
- Ccrypto::RIPEMD256.provider_info("RIPEMD256"),
51
- Ccrypto::RIPEMD320.provider_info("RIPEMD320"),
52
-
53
- Ccrypto::SHAKE128_256.provider_info("SHAKE128-256"),
54
- Ccrypto::SHAKE256_512.provider_info("SHAKE256-512"),
55
-
56
- Ccrypto::SKEIN1024_1024.provider_info("SKEIN-1024-1024"),
57
- Ccrypto::SKEIN1024_384.provider_info("SKEIN-1024-384"),
58
- Ccrypto::SKEIN1024_512.provider_info("SKEIN-1024-512"),
59
-
60
- Ccrypto::SKEIN256_128.provider_info("SKEIN-256-128"),
61
- Ccrypto::SKEIN256_160.provider_info("SKEIN-256-160"),
62
- Ccrypto::SKEIN256_224.provider_info("SKEIN-256-224"),
63
- Ccrypto::SKEIN256_256.provider_info("SKEIN-256-256"),
64
-
65
- Ccrypto::SKEIN512_128.provider_info("SKEIN-512-128"),
66
- Ccrypto::SKEIN512_160.provider_info("SKEIN-512-160"),
67
- Ccrypto::SKEIN512_224.provider_info("SKEIN-512-224"),
68
- Ccrypto::SKEIN512_256.provider_info("SKEIN-512-256"),
69
- Ccrypto::SKEIN512_384.provider_info("SKEIN-512-384"),
70
- Ccrypto::SKEIN512_512.provider_info("SKEIN-512-512"),
71
-
72
- SM3 = Ccrypto::SM3.provider_info("SM3"),
73
- WHIRLPOOL = Ccrypto::WHIRLPOOL.provider_info("WHIRLPOOL")
74
- ]
10
+ class SupportedDigestEngine
11
+ include InMemoryRecord
75
12
 
76
- def self.supported
77
- if @supported.nil?
78
- @supported = []
79
- probe = java.security.Security.getAlgorithms("MessageDigest").to_a.delete_if { |e| e.include?(".") }
80
- Potential.each do |po|
81
- @supported << po if probe.include?(po.provider_config)
82
- end
13
+ def initialize
14
+ define_search_key(:algo, :outBitLength, :outByteLength)
83
15
  end
84
- @supported
85
16
  end
86
17
 
87
- def self.is_supported?(eng, prov = nil)
88
- if is_empty?(eng)
89
- false
90
- else
18
+ def self.supported
19
+ if @supported.nil?
20
+ if ENV[Java::ENV_PROBE_DIGEST_KEY] == "true"
21
+ @supported = SupportedDigestEngine.new
22
+ else
23
+ @supported = SupportedDigestEngine.load_from_storage("supported_digest")
24
+ end
91
25
 
92
- jceName = algo_jce_map[eng]
93
- begin
94
- if not_empty?(prov)
95
- #java.security.MessageDigest.getInstance(eng.to_s.gsub("_","-"), prov)
96
- java.security.MessageDigest.getInstance(jceName, prov)
97
- else
98
- #java.security.MessageDigest.getInstance(eng.to_s.gsub("_","-"))
99
- java.security.MessageDigest.getInstance(jceName)
26
+ if @supported.empty?
27
+ @supported = SupportedDigestEngine.new
28
+ probe = java.security.Security.getAlgorithms("MessageDigest").to_a.delete_if { |e| e.include?(".") }
29
+
30
+ logger = Ccrypto::Java.logger(:digest_eng)
31
+
32
+ blacklistedDigest = ["MD2","MD4","MD5"]
33
+ probe.sort.each do |found|
34
+ next if blacklistedDigest.include?(found)
35
+ logger.debug "Found digest algo : #{found}"
36
+ begin
37
+ md = java.security.MessageDigest.getInstance(found, JCEProvider::BCProv)
38
+ case found
39
+ when "HARAKA-256"
40
+ conf = { provider_config: { algo_name: found, jceProvider: JCEProvider::BCProv.name }, fixed_input_len_byte: 32 }
41
+ when "HARAKA-512"
42
+ conf = { provider_config: { algo_name: found, jceProvider: JCEProvider::BCProv.name }, fixed_input_len_byte: 64 }
43
+ else
44
+ conf = { provider_config: { algo_name: found, jceProvider: JCEProvider::BCProv.name } }
45
+ end
46
+
47
+ digConf = Ccrypto::DigestConfig.new(found, md.getDigestLength()*8, conf)
48
+
49
+ testDig = Ccrypto::DigestMatcher.generate_digest(digConf)
50
+ digKey = Ccrypto::DigestMatcher.find_digest_key(testDig)
51
+ if not_empty?(digKey)
52
+ # map the algo name to common name
53
+ digConf = Ccrypto::DigestConfig.new(digKey, md.getDigestLength()*8, conf)
54
+ @supported.register(digConf, { tag_under: :jceAlgo, tag_value: found })
55
+
56
+ else
57
+ logger.warn "Digest algo from Java named '#{found}' not listed in the Ccrypto::DigestMatcher. Skip or add the value into the table. [#{testDig}]"
58
+
59
+ end
60
+
61
+ rescue Exception => ex
62
+ logger.error ex.message
63
+ end
100
64
  end
101
- true
102
- rescue java.security.NoSuchAlgorithmException => ex
103
- p ex.message
104
- false
105
65
  end
66
+
67
+ @supported.save_to_storage("supported_digest")
68
+
106
69
  end
70
+ @supported
71
+ end
72
+ class << self
73
+ alias_method :supported_digests, :supported
107
74
  end
108
75
 
109
- def self.default_algo
110
- "SHA256"
76
+ def self.is_digest_supported?(key)
77
+ (find_digest_config(key).length > 0)
111
78
  end
112
79
 
113
80
  def self.instance(conf, &block)
114
- if block
115
- prov = block.call(:jce_provider)
116
- if not_empty?(prov)
117
- DigestEngine.new(conf.provider_config, prov, &block)
118
- else
119
- DigestEngine.new(conf.provider_config, &block)
120
- end
81
+
82
+ case conf
83
+ when String, Symbol
84
+ digEng = find_digest_config(conf)
85
+ when Ccrypto::DigestConfig
86
+ digEng = conf
121
87
  else
122
- DigestEngine.new(conf.provider_config, &block)
88
+ raise DigestEngineException, "Unsupported instance type '#{conf}'"
123
89
  end
124
- end
125
90
 
126
- def self.digest(key, &block)
127
- res = engineKeys[key]
128
- if is_empty?(res)
129
- raise DigestEngine, "Not supported digest engine #{key}"
130
- else
131
- if block
132
- digProv = block.call(:digest_jceProvider)
133
- end
91
+ raise DigestEngineException, "Unsupported digest type '#{conf}'" if digEng.nil?
134
92
 
135
- if digProv.nil?
136
- DigestEngine.new(res.provider_config)
137
- else
138
- DigestEngine.new(res.provider_config, digProv)
139
- end
93
+ prov = digEng.provider_config[:jceProvider]
94
+ if not_empty?(prov)
95
+ JCEProvider.instance.add_provider(prov) if not JCEProvider.instance.is_provider_registered?(prov)
96
+ DigestEngine.new(digEng.provider_config[:algo_name], prov, &block)
97
+ else
98
+ DigestEngine.new(digEng.provider_config[:algo_name], &block)
140
99
  end
100
+
141
101
  end
142
102
 
143
- def self.engineKeys
144
- if @engineKeys.nil?
145
- @engineKeys = {}
146
- supported.each do |a|
147
- @engineKeys[a.algo.to_sym] = a
148
- end
149
- end
150
- @engineKeys
103
+ def self.find_digest_config(key)
104
+ res = supported.find(algo: key)
105
+ res.concat(supported.find(jceAlgo: key))
106
+ res.uniq
151
107
  end
152
108
 
153
- def self.algo_jce_map
154
- if @algoMap.nil?
155
- @algoMap = {}
156
- supported.each do |a|
157
- @algoMap[a.algo.to_sym] = a.provider_config
158
- end
109
+ def self.to_bc_digest_inst(conf)
110
+ case conf
111
+ when Ccrypto::DigestConfig
112
+ algo = conf.provider_config[:algo_name]
113
+ when String
114
+ algo = conf.upcase
115
+ when Symbol
116
+ algo = conf.to_s.upcase
117
+ else
118
+ raise DigestEngineException, "Unsupported query type '#{conf.class}'"
119
+ end
120
+
121
+ logger.debug "BC digest matching : #{conf.inspect} ==> #{algo}"
122
+
123
+ case algo
124
+ when "BLAKE2B-160","BLAKE2B_160"
125
+ "org.bouncycastle.crypto.digests.Blake2bDigest.new(160)"
126
+ when "BLAKE2B-256"
127
+ "org.bouncycastle.crypto.digests.Blake2bDigest.new(256)"
128
+ when "BLAKE2B-384"
129
+ "org.bouncycastle.crypto.digests.Blake2bDigest.new(384)"
130
+ when "BLAKE2B-512"
131
+ "org.bouncycastle.crypto.digests.Blake2bDigest.new(512)"
132
+ when "BLAKE2S-128"
133
+ "org.bouncycastle.crypto.digests.Blake2sDigest.new(128)"
134
+ when "BLAKE2S-160"
135
+ "org.bouncycastle.crypto.digests.Blake2sDigest.new(160)"
136
+ when "BLAKE2S-224"
137
+ "org.bouncycastle.crypto.digests.Blake2sDigest.new(224)"
138
+ when "BLAKE2S-256"
139
+ "org.bouncycastle.crypto.digests.Blake2sDigest.new(256)"
140
+ when "BLAKE3-256"
141
+ "org.bouncycastle.crypto.digests.Blake3Digest.new #(256)"
142
+ when "DSTU7564-256"
143
+ "org.bouncycastle.crypto.digests.DSTU7564Digest.new(256)"
144
+ when "DSTU7564-384"
145
+ "org.bouncycastle.crypto.digests.DSTU7564Digest.new(384)"
146
+ when "DSTU7564-512"
147
+ "org.bouncycastle.crypto.digests.DSTU7564Digest.new(512)"
148
+ when "GOST3411"
149
+ "org.bouncycastle.crypto.digests.GOST3411Digest.new"
150
+ when "GOST3411-2012-256"
151
+ "org.bouncycastle.crypto.digests.GOST3411_2012_256Digest.new"
152
+ when "GOST3411-2012-512"
153
+ "org.bouncycastle.crypto.digests.GOST3411_2012_512Digest.new"
154
+ #when "HARAKA-256"
155
+ # org.bouncycastle.crypto.digests.Haraka256Digest.new
156
+ #when "HARAKA-512"
157
+ # org.bouncycastle.crypto.digests.Haraka512Digest.new
158
+ when "KECCAK-224"
159
+ "org.bouncycastle.crypto.digests.KeccakDigest.new(224)"
160
+ when "KECCAK-256"
161
+ "org.bouncycastle.crypto.digests.KeccakDigest.new(256)"
162
+ when "KECCAK-288"
163
+ "org.bouncycastle.crypto.digests.KeccakDigest.new(288)"
164
+ when "KECCAK-384"
165
+ "org.bouncycastle.crypto.digests.KeccakDigest.new(384)"
166
+ when "KECCAK-512"
167
+ "org.bouncycastle.crypto.digests.KeccakDigest.new(512)"
168
+ when "RIPEMD128"
169
+ "org.bouncycastle.crypto.digests.RIPEMD128Digest.new"
170
+ when "RIPEMD160"
171
+ "org.bouncycastle.crypto.digests.RIPEMD160Digest.new"
172
+ when "RIPEMD256"
173
+ "org.bouncycastle.crypto.digests.RIPEMD256Digest.new"
174
+ when "SHA-1", "SHA1"
175
+ "org.bouncycastle.crypto.digests.SHA1Digest.new"
176
+ when "SHA-224", "SHA224"
177
+ "org.bouncycastle.crypto.digests.SHA224Digest.new"
178
+ when "SHA-256", "SHA256"
179
+ "org.bouncycastle.crypto.digests.SHA256Digest.new"
180
+ when "SHA-384", "SHA384"
181
+ "org.bouncycastle.crypto.digests.SHA384Digest.new"
182
+ when "SHA-512", "SHA512"
183
+ "org.bouncycastle.crypto.digests.SHA512Digest.new"
184
+ when "SHA3-224","SHA3_224"
185
+ "org.bouncycastle.crypto.digests.SHA3Digest.new(224)"
186
+ when "SHA3-256","SHA3_256"
187
+ "org.bouncycastle.crypto.digests.SHA3Digest.new(256)"
188
+ when "SHA3-384","SHA3_384"
189
+ "org.bouncycastle.crypto.digests.SHA3Digest.new(384)"
190
+ when "SHA3-512","SHA3_512"
191
+ "org.bouncycastle.crypto.digests.SHA3Digest.new(512)"
192
+ when "SHAKE128-256","SHAKE128_256"
193
+ "org.bouncycastle.crypto.digests.SHAKEDigest.new(128)"
194
+ when "SHAKE256-512","SHAKE256_512"
195
+ "org.bouncycastle.crypto.digests.SHAKEDigest.new(256)"
196
+ when "SKEIN-1024-1024","SKEIN_1024_1024"
197
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(1024,1024)"
198
+ when "SKEIN-1024-384","SKEIN_1024_384"
199
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(1024,384)"
200
+ when "SKEIN-1024-512","SKEIN_1024_512"
201
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(1024,512)"
202
+ when "SKEIN-256-128","SKEIN_256_128"
203
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(256,128)"
204
+ when "SKEIN-256-160","SKEIN_256_160"
205
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(256,160)"
206
+ when "SKEIN-256-224","SKEIN_256_224"
207
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(256,224)"
208
+ when "SKEIN-256-256","SKEIN_256_256"
209
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(256,256)"
210
+ when "SKEIN-512-128","SKEIN_512_128"
211
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(512,128)"
212
+ when "SKEIN-512-160","SKEIN_512_160"
213
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(512,160)"
214
+ when "SKEIN-512-224","SKEIN_512_224"
215
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(512,224)"
216
+ when "SKEIN-512-256","SKEIN_512_256"
217
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(512,256)"
218
+ when "SKEIN-512-384","SKEIN_512_384"
219
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(512,384)"
220
+ when "SKEIN-512-512","SKEIN_512_512"
221
+ "org.bouncycastle.crypto.digests.SkeinDigest.new(512,512)"
222
+ when "SM3"
223
+ "org.bouncycastle.crypto.digests.SM3Digest.new"
224
+ when "TIGER"
225
+ "org.bouncycastle.crypto.digests.TigerDigest.new"
226
+ when "WHIRLPOOL"
227
+ "org.bouncycastle.crypto.digests.WhirlpoolDigest.new"
159
228
  end
160
- @algoMap
229
+
161
230
  end
162
231
 
232
+
233
+ private
234
+ def self.logger
235
+ Ccrypto::Java.logger(:cj_digest_eng_c)
236
+ end
237
+
238
+ public
239
+ ##
240
+ # Instance method
241
+ ##
242
+ attr_reader :native_instance
163
243
  def initialize(algo, prov = nil, &block)
164
- teLogger.debug "Algo : #{algo}"
165
- @algo = algo #algo.to_s.gsub("_","-")
244
+
245
+ logger.debug "Algo : #{algo}"
246
+
247
+ @algo = algo
166
248
  begin
167
249
  if not_empty?(prov)
168
- @inst = java.security.MessageDigest.getInstance(@algo, prov)
250
+ @native_instance = java.security.MessageDigest.getInstance(@algo, prov)
169
251
  else
170
- @inst = java.security.MessageDigest.getInstance(@algo)
252
+ @native_instance = java.security.MessageDigest.getInstance(@algo)
171
253
  end
172
- #rescue java.security.NoSuchAlgorithmException => ex
173
254
  rescue Exception => ex
174
255
  raise DigestEngineException, ex
175
256
  end
@@ -180,15 +261,15 @@ module Ccrypto
180
261
  end
181
262
 
182
263
  def digest_update(val)
183
- @inst.update(to_java_bytes(val))
264
+ @native_instance.update(to_java_bytes(val))
184
265
  end
185
266
 
186
267
  def digest_final(val = nil, output = :binary)
187
268
  if not_empty?(val)
188
- @inst.update(to_java_bytes(val))
269
+ @native_instance.update(to_java_bytes(val))
189
270
  end
190
- res = @inst.digest
191
- @inst.reset
271
+ res = @native_instance.digest
272
+ @native_instance.reset
192
273
  case output
193
274
  when :hex
194
275
  to_hex(res)
@@ -199,8 +280,9 @@ module Ccrypto
199
280
  end
200
281
  end
201
282
 
202
- def reset
203
- @inst.reset
283
+ private
284
+ def logger
285
+ Ccrypto::Java.logger(:cj_digest_eng)
204
286
  end
205
287
 
206
288
  end