Class: Ccrypto::Java::X25519KeyBundle

Inherits:
Object
  • Object
show all
Includes:
DataConversion, X25519KeyBundle, TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/java/engines/x25519_engine.rb

Overview

X25519PrivateKey

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_b64_mime, #from_hex, included, #logger, #to_b64, #to_b64_mime, #to_bin, #to_hex, #to_java_bytes, #to_str

Constructor Details

#initialize(kp) ⇒ X25519KeyBundle

Returns a new instance of X25519KeyBundle.



103
104
105
# File 'lib/ccrypto/java/engines/x25519_engine.rb', line 103

def initialize(kp)
  @nativeKeypair = kp
end

Class Method Details

.from_storage(bin) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ccrypto/java/engines/x25519_engine.rb', line 170

def self.from_storage(bin)
  case bin
  when Hash
    res = {  }
    JCEProvider.instance.add_bc_provider

    kf = java.security.KeyFactory.getInstance("X25519", JCEProvider::BCProv.name)

    if not_empty?(bin[:private])
      # raw_private = true means the given private key is in its raw data form
      # 5th Sept 22 - Not tested not sure how to generate raw key
      if bin[:raw_private] == true
        # 5th Sept 2022 - untested code
        info = org.bouncycastle.asn1.pkcs.PrivateKeyInfo.new(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.edec.EdECObjectIdentifiers::id_X25519), org.bouncycastle.asn1.DEROctetString.new(bin[:private]))

        spec = java.security.spec.PKCS8EncodedKeySpec.new(info.encoded)
      else
        spec = java.security.spec.PKCS8EncodedKeySpec.new(bin[:private])
      end

      res[:private] = X25519PrivateKey.new(kf.generatePrivate(spec))

      #res[:private] = X25519PrivateKey.new(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters.new(bin[:private],0))
    end

    if not_empty?(bin[:public])
      if bin[:raw_public] == true
        # 5th Sept 2022 - untested code
        #pubRaw = org.bouncycastle.crypto.params.Ed25519PublicKeyParameters.new(bin[:public],0).encoded
        pubRaw = bin[:public]
        info = org.bouncycastle.asn1.x509.SubjectPublicKeyInfo.new(org.bouncycastle.asn1.x509.AlgorithmIdentifier.new(org.bouncycastle.asn1.edec.EdECObjectIdentifiers::id_X25519), bin[:public])

        spec = java.security.spec.X509EncodedKeySpec.new(info.encoded)
      else
        spec = java.security.spec.X509EncodedKeySpec.new(bin[:public])
      end

      res[:public] = X25519PublicKey.new(kf.generatePublic(spec))
    end

    res[:keypair] = X25519KeyBundle.new(java.security.KeyPair.new(res[:public].native_pubKey, res[:private].native_privKey)) if not_empty?(res[:public]) and not_empty?(res[:private])

    res

  else
    raise KeypairEngineException, "No sure how to handle storage input '#{bin.class}'"
  end
end

Instance Method Details

#derive_dh_shared_secret(pubKey, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ccrypto/java/engines/x25519_engine.rb', line 118

def derive_dh_shared_secret(pubKey, &block)
  
  JCEProvider.instance.add_bc_provider

  ka = javax.crypto.KeyAgreement.getInstance("X25519",JCEProvider::BCProv.name)
  ka.init(@nativeKeypair.getPrivate)

  case pubKey
  when X25519PublicKey
    uPubKey = pubKey.native_pubKey
  else 
    raise KeypairEngineException, "Unsupported public key type '#{pubKey.class}'"
  end

  ka.doPhase(uPubKey, true)
  ka.generateSecret()

end

#private_keyObject



114
115
116
# File 'lib/ccrypto/java/engines/x25519_engine.rb', line 114

def private_key
  X25519PrivateKey.new(@nativeKeypair.getPrivate) 
end

#public_keyObject



107
108
109
110
111
112
# File 'lib/ccrypto/java/engines/x25519_engine.rb', line 107

def public_key
  if @pubKey.nil?
    @pubKey = X25519PublicKey.new(@nativeKeypair.getPublic)
  end
  @pubKey
end

#to_storage(eng = :ccrypto, &block) ⇒ Object

should be under keybundle_store but not sure how to do this



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ccrypto/java/engines/x25519_engine.rb', line 138

def to_storage(eng = :ccrypto, &block)
  case eng
  when :ccrypto
    res = {  }

    rawPrivate = false
    rawPublic = false
    if block
      rawPrivate = block.call(:export_raw_private_key)
      rawPublic = block.call(:export_raw_public_key)
    end

    res[:private] = private_key.to_bin
    if rawPrivate == true
      # 5th Sept 2022 - untested code
      res[:private] = org.bouncycastle.crypto.params.X25519PrivateKeyParameters.new(res[:private],0).encoded 
      res[:raw_private] = true
    end

    res[:public] = public_key.to_bin
    if rawPublic == true
      # 5th Sept 2022 - untested code
      res[:public] = org.bouncycastle.crypto.params.X25519PublicKeyParameters.new(res[:public],0).encoded 
      res[:raw_public] = true
    end

    res
  else
    raise KeypairEngineException, "Unsupported storage type '#{eng}'"
  end
end