virgil-crypto 2.3.0 → 3.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -0
- data/.gitmodules +0 -0
- data/.travis.yml +13 -0
- data/Gemfile +0 -0
- data/README.md +113 -9
- data/Rakefile +0 -0
- data/ext/native/src/.clang-format +37 -0
- data/ext/native/src/.gitignore +3 -0
- data/ext/native/src/.travis.yml +2 -1
- data/ext/native/src/CMakeLists.txt +66 -63
- data/ext/native/src/ChangeLog +114 -2
- data/ext/native/src/Dockerfile_Golang +29 -8
- data/ext/native/src/Jenkinsfile +61 -50
- data/ext/native/src/LICENSE +0 -0
- data/ext/native/src/README.md +137 -167
- data/ext/native/src/VERSION +1 -1
- data/ext/native/src/benchmark.md +140 -0
- data/ext/native/src/benchmark/CMakeLists.txt +0 -0
- data/ext/native/src/benchmark/benchmark_cipher.cxx +0 -0
- data/ext/native/src/benchmark/benchmark_hash.cxx +0 -0
- data/ext/native/src/benchmark/benchmark_keys.cxx +0 -0
- data/ext/native/src/benchmark/benchmark_pythia.cxx +61 -0
- data/ext/native/src/benchmark/benchmark_random.cxx +0 -0
- data/ext/native/src/benchmark/benchmark_signer.cxx +0 -0
- data/ext/native/src/ci/install-dependencies.sh +8 -12
- data/ext/native/src/ci/publish-docs.sh +107 -22
- data/ext/native/src/ci/travis-deployment-key.enc +0 -0
- data/ext/native/src/cmake/TransitiveArgs.cmake +78 -0
- data/ext/native/src/cmake/TransitiveToolchainArgs.cmake +105 -0
- data/ext/native/src/cmake/add_mono_metadata.cmake +103 -0
- data/ext/native/src/cmake/apple.cmake +56 -11
- data/ext/native/src/cmake/aux_source_directory_to_file.cmake +0 -0
- data/ext/native/src/cmake/check_pointer_size.cmake +0 -0
- data/ext/native/src/cmake/copy_all_files.cmake +0 -0
- data/ext/native/src/cmake/file_regex_replace.cmake +0 -0
- data/ext/native/src/cmake/find_host_utils.cmake +0 -0
- data/ext/native/src/cmake/uppercase_first_char.cmake +0 -0
- data/ext/native/src/cmake/uppercase_namespaces.cmake +3 -0
- data/ext/native/src/cmake/virgil_depends.cmake +0 -0
- data/ext/native/src/cmake/virgil_depends_local.cmake +2 -0
- data/ext/native/src/docs/.gitignore +0 -0
- data/ext/native/src/lib/CMakeLists.txt +59 -1
- data/ext/native/src/lib/Doxyfile.in +1 -1
- data/ext/native/src/lib/Info.plist.in +0 -0
- data/ext/native/src/lib/cmake/config.cmake.in +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilByteArrayUtils.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilChunkCipher.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilCipherBase.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilContentInfo.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilCrypto.h +13 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilCryptoError.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilCustomParams.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilKeyPair.h +30 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilSignerBase.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilStreamCipher.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilStreamSigner.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilTinyCipher.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/VirgilVersion.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/VirgilAsymmetricCipher.h +17 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/VirgilHKDF.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/VirgilPBKDF.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/VirgilSystemCryptoError.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/asn1/VirgilAsn1Compatible.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/cms/VirgilCMSContent.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/cms/VirgilCMSContentInfo.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/cms/VirgilCMSEncryptedContent.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/cms/VirgilCMSEnvelopedData.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/cms/VirgilCMSKeyTransRecipient.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/foundation/cms/VirgilCMSPasswordRecipient.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFS.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSEncryptedMessage.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSInitiatorPrivateInfo.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSInitiatorPublicInfo.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSPrivateKey.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSPublicKey.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSResponderPrivateInfo.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSResponderPublicInfo.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pfs/VirgilPFSSession.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/primitive/VirgilOperationCipher.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/primitive/VirgilOperationDH.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/primitive/VirgilOperationHash.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/primitive/VirgilOperationKDF.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/primitive/VirgilOperationRandom.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythia.h +181 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythiaBlindResult.h +89 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythiaContext.h +72 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythiaError.h +161 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythiaProveResult.h +89 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythiaTransformResult.h +91 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/VirgilPythiaTransformationKeyPair.h +87 -0
- data/ext/native/src/lib/include/virgil/crypto/pythia/virgil_pythia_c.h +203 -0
- data/ext/native/src/lib/include/virgil/crypto/stream/VirgilBytesDataSink.h +0 -0
- data/ext/native/src/lib/include/virgil/crypto/stream/VirgilBytesDataSource.h +0 -0
- data/ext/native/src/lib/mainpage.dox +6 -0
- data/ext/native/src/lib/module.modulemap +7 -0
- data/ext/native/src/lib/src/VirgilAsn1Alg.cxx +0 -0
- data/ext/native/src/lib/src/VirgilAsn1Alg.h +0 -0
- data/ext/native/src/lib/src/VirgilAsn1Compatible.cxx +0 -0
- data/ext/native/src/lib/src/VirgilAsymmetricCipher.cxx +82 -0
- data/ext/native/src/lib/src/VirgilByteArrayUtils.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCMSContent.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCMSContentInfo.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCMSEncryptedContent.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCMSEnvelopedData.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCMSKeyTransRecipient.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCMSPasswordRecipient.cxx +0 -0
- data/ext/native/src/lib/src/VirgilChunkCipher.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCipherBase.cxx +0 -0
- data/ext/native/src/lib/src/VirgilConfig.cxx +51 -0
- data/ext/native/src/lib/src/VirgilConfig.h.in +43 -1
- data/ext/native/src/lib/src/VirgilContentInfo.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCryptoError.cxx +0 -0
- data/ext/native/src/lib/src/VirgilCustomParams.cxx +0 -0
- data/ext/native/src/lib/src/VirgilDataSink.cxx +0 -0
- data/ext/native/src/lib/src/VirgilHKDF.cxx +0 -0
- data/ext/native/src/lib/src/VirgilKeyPair.cxx +19 -0
- data/ext/native/src/lib/src/VirgilOID.h +0 -0
- data/ext/native/src/lib/src/VirgilPBKDF.cxx +0 -0
- data/ext/native/src/lib/src/VirgilSignerBase.cxx +0 -0
- data/ext/native/src/lib/src/VirgilStreamCipher.cxx +0 -0
- data/ext/native/src/lib/src/VirgilStreamSigner.cxx +0 -0
- data/ext/native/src/lib/src/VirgilSystemCryptoError.cxx +0 -0
- data/ext/native/src/lib/src/VirgilTagFilter.cxx +0 -0
- data/ext/native/src/lib/src/VirgilTagFilter.h +0 -0
- data/ext/native/src/lib/src/VirgilTinyCipher.cxx +0 -0
- data/ext/native/src/lib/src/VirgilVersion.cxx.in +0 -0
- data/ext/native/src/lib/src/mbedtls_context.h +0 -0
- data/ext/native/src/lib/src/mbedtls_context_policy_spec.h +0 -0
- data/ext/native/src/lib/src/mbedtls_type_utils.h +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFS.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSEncryptedMessage.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSInitiatorPrivateInfo.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSInitiatorPublicInfo.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSPrivateKey.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSPublicKey.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSResponderPrivateInfo.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSResponderPublicInfo.cxx +0 -0
- data/ext/native/src/lib/src/pfs/VirgilPFSSession.cxx +0 -0
- data/ext/native/src/lib/src/primitive/VirgilOperationCipher.cxx +0 -0
- data/ext/native/src/lib/src/primitive/VirgilOperationDH.cxx +0 -0
- data/ext/native/src/lib/src/primitive/VirgilOperationHash.cxx +0 -0
- data/ext/native/src/lib/src/primitive/VirgilOperationKDF.cxx +0 -0
- data/ext/native/src/lib/src/primitive/VirgilOperationRandom.cxx +0 -0
- data/ext/native/src/lib/src/pythia/VirgilPythia.cxx +202 -0
- data/ext/native/src/lib/src/pythia/VirgilPythiaContext.cxx +116 -0
- data/ext/native/src/lib/src/pythia/VirgilPythiaError.cxx +58 -0
- data/ext/native/src/lib/src/pythia/virgil_pythia_c.cxx +129 -0
- data/ext/native/src/lib/src/stream/VirgilBytesDataSink.cxx +0 -0
- data/ext/native/src/lib/src/stream/VirgilBytesDataSource.cxx +0 -0
- data/ext/native/src/lib/src/utils.h +3 -2
- data/ext/native/src/libs_ext/mbedtls/configs/config.h +0 -0
- data/ext/native/src/libs_ext/mbedtls/configs/config_desktop.h +0 -0
- data/ext/native/src/libs_ext/mbedtls/mbedtls.cmake +0 -0
- data/ext/native/src/{cmake/cmake_args.cmake → libs_ext/pythia/pythia.cmake} +24 -28
- data/ext/native/src/libs_ext/rapidjson/rapidjson.cmake +0 -0
- data/ext/native/src/libs_ext/tinyformat/cmake/config.cmake.in +0 -0
- data/ext/native/src/libs_ext/tinyformat/tinyformat.cmake +0 -0
- data/ext/native/src/migration-2.0.md +0 -0
- data/ext/native/src/tests/CMakeLists.txt +0 -0
- data/ext/native/src/tests/catch.hpp +0 -0
- data/ext/native/src/tests/data/test_data_pfs_with_otc.json +0 -0
- data/ext/native/src/tests/data/test_data_pfs_without_otc.json +0 -0
- data/ext/native/src/tests/deterministic_keys.h +293 -0
- data/ext/native/src/tests/rsa_keys.h +229 -123
- data/ext/native/src/tests/test_asn1_writer.cxx +0 -0
- data/ext/native/src/tests/test_asymmetric_cipher.cxx +65 -0
- data/ext/native/src/tests/test_base64.cxx +0 -0
- data/ext/native/src/tests/test_byte_array_utils.cxx +0 -0
- data/ext/native/src/tests/test_chunk_cipher.cxx +0 -0
- data/ext/native/src/tests/test_cipher.cxx +8 -4
- data/ext/native/src/tests/test_cipher_base.cxx +0 -0
- data/ext/native/src/tests/test_contract_copy_move.cxx +4 -1
- data/ext/native/src/tests/test_data_pfs.h +0 -0
- data/ext/native/src/tests/test_hash.cxx +0 -0
- data/ext/native/src/tests/test_hkdf.cxx +0 -0
- data/ext/native/src/tests/test_key_pair.cxx +65 -3
- data/ext/native/src/tests/test_pbe.cxx +0 -0
- data/ext/native/src/tests/test_pbkdf.cxx +0 -0
- data/ext/native/src/tests/test_pfs.cxx +0 -0
- data/ext/native/src/tests/test_pythia.cxx +157 -0
- data/ext/native/src/tests/test_random.cxx +0 -0
- data/ext/native/src/tests/test_runner.cxx +0 -0
- data/ext/native/src/tests/test_signer.cxx +0 -0
- data/ext/native/src/tests/test_stream_cipher.cxx +0 -0
- data/ext/native/src/tests/test_stream_data_source.cxx +0 -0
- data/ext/native/src/tests/test_symmetric_cipher.cxx +0 -0
- data/ext/native/src/tests/test_tag_filter.cxx +0 -0
- data/ext/native/src/tests/test_tiny_cipher.cxx +0 -0
- data/ext/native/src/utils/build.bat +0 -0
- data/ext/native/src/utils/build.sh +238 -68
- data/ext/native/src/utils/zip.vbs +0 -0
- data/ext/native/src/virgil-crypto.sublime-project +15 -0
- data/ext/native/src/wrappers/asmjs/CMakeLists.txt +0 -8
- data/ext/native/src/wrappers/emsdk/CMakeLists.txt +2 -2
- data/ext/native/src/wrappers/emsdk/error.js +0 -0
- data/ext/native/src/wrappers/emsdk/example.html +152 -31
- data/ext/native/src/wrappers/emsdk/helpers.js +140 -135
- data/ext/native/src/wrappers/emsdk/patch_embind.pl +0 -0
- data/ext/native/src/wrappers/emsdk/wrapper.cxx +55 -0
- data/ext/native/src/wrappers/go/CMakeLists.txt +6 -5
- data/ext/native/src/wrappers/java/CMakeLists.txt +27 -4
- data/ext/native/src/wrappers/java/src/JniLoader.java +167 -0
- data/ext/native/src/wrappers/java/src/VirgilStreamDataSink.java +0 -0
- data/ext/native/src/wrappers/java/src/VirgilStreamDataSource.java +0 -0
- data/ext/native/src/wrappers/net/CMakeLists.txt +41 -8
- data/ext/native/src/wrappers/net/cmake/UseCSharp.cmake +5 -5
- data/ext/native/src/wrappers/net/src/VirgilStreamDataSink.cs +0 -0
- data/ext/native/src/wrappers/net/src/VirgilStreamDataSource.cs +0 -0
- data/ext/native/src/wrappers/nodejs/checksum.txt +66 -0
- data/ext/native/src/wrappers/php/CMakeLists.txt +2 -5
- data/ext/native/src/wrappers/php/tests/CMakeLists.txt +6 -0
- data/ext/native/src/wrappers/php/tests/VirgilChunkCipher_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilCustomParams_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilKDF_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilPBE_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilPBKDF_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilPythia_Test.php.in +87 -0
- data/ext/native/src/wrappers/php/tests/VirgilStreamCipher_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilStreamSigner_Test.php.in +0 -0
- data/ext/native/src/wrappers/php/tests/VirgilVersion_Test.php.in +0 -0
- data/ext/native/src/wrappers/python/CMakeLists.txt +4 -1
- data/ext/native/src/wrappers/ruby/CMakeLists.txt +4 -1
- data/ext/native/src/wrappers/swig/common.i +0 -0
- data/ext/native/src/wrappers/swig/csharp/FixedArray.i +0 -0
- data/ext/native/src/wrappers/swig/csharp/VirgilByteArray.i +0 -0
- data/ext/native/src/wrappers/swig/csharp/common.i +0 -0
- data/ext/native/src/wrappers/swig/csharp/csharphead.swg.in +0 -0
- data/ext/native/src/wrappers/swig/go/common.i.in +7 -0
- data/ext/native/src/wrappers/swig/java/VirgilByteArray.i +0 -0
- data/ext/native/src/wrappers/swig/java/common.i +6 -0
- data/ext/native/src/wrappers/swig/php/common.i +0 -0
- data/ext/native/src/wrappers/swig/php/php7/VirgilByteArray.i +0 -0
- data/ext/native/src/wrappers/swig/util.i +0 -0
- data/ext/native/src/wrappers/swig/wrapper.i.in +18 -7
- data/ext/native/src/wrappers/webasm/CMakeLists.txt +0 -8
- data/ext/rakefile.rb +77 -56
- data/lib/virgil/crypto.rb +53 -9
- data/lib/virgil/crypto/access_token_signer.rb +79 -0
- data/lib/virgil/crypto/bytes.rb +81 -17
- data/lib/virgil/crypto/card_crypto.rb +115 -0
- data/lib/virgil/crypto/crypto_exception.rb +40 -0
- data/lib/virgil/crypto/hash_algorithm.rb +82 -0
- data/lib/virgil/crypto/key_pair.rb +41 -0
- data/lib/virgil/crypto/key_pair_type.rb +98 -0
- data/lib/virgil/crypto/private_key.rb +40 -0
- data/lib/virgil/crypto/private_key_exporter.rb +67 -0
- data/lib/virgil/crypto/public_key.rb +40 -0
- data/lib/virgil/crypto/{virgil_stream_data_sink.rb → stream_data_sink.rb} +12 -12
- data/lib/virgil/crypto/{virgil_stream_data_source.rb → stream_data_source.rb} +12 -12
- data/lib/virgil/crypto/validation.rb +59 -0
- data/lib/virgil/crypto/version.rb +35 -1
- data/lib/virgil/crypto/virgil_crypto.rb +595 -0
- data/lib/virgil/native_crypto.rb +80 -60
- data/lib/virgil/os.rb +40 -6
- data/secrets.tar.enc +0 -0
- data/virgil-crypto.gemspec +6 -7
- metadata +62 -12
- data/ext/native/src/cmake/android.toolchain.cmake +0 -1697
@@ -1,5 +1,39 @@
|
|
1
|
+
# Copyright (C) 2015-2019 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, bytes, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
|
1
35
|
module Virgil
|
2
36
|
module Crypto
|
3
|
-
VERSION = '
|
37
|
+
VERSION = '3.6.2'
|
4
38
|
end
|
5
39
|
end
|
@@ -0,0 +1,595 @@
|
|
1
|
+
# Copyright (C) 2015-2019 Virgil Security Inc.
|
2
|
+
#
|
3
|
+
# Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are
|
9
|
+
# met:
|
10
|
+
#
|
11
|
+
# (1) Redistributions of source code must retain the above copyright
|
12
|
+
# notice, this list of conditions and the following disclaimer.
|
13
|
+
#
|
14
|
+
# (2) Redistributions in binary form must reproduce the above copyright
|
15
|
+
# notice, this list of conditions and the following disclaimer in
|
16
|
+
# the documentation and/or other materials provided with the
|
17
|
+
# distribution.
|
18
|
+
#
|
19
|
+
# (3) Neither the name of the copyright holder nor the names of its
|
20
|
+
# contributors may be used to endorse or promote products derived from
|
21
|
+
# this software without specific prior written permission.
|
22
|
+
#
|
23
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
|
24
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
25
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
26
|
+
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
27
|
+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
28
|
+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
29
|
+
# SERVICES; LOSS OF USE, bytes, OR PROFITS; OR BUSINESS INTERRUPTION)
|
30
|
+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
31
|
+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
32
|
+
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
33
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
34
|
+
|
35
|
+
module Virgil
|
36
|
+
module Crypto
|
37
|
+
# Wrapper for cryptographic operations.
|
38
|
+
#
|
39
|
+
# Class provides a cryptographic operations in applications, such as
|
40
|
+
# hashing, signature generation and verification, key creation,
|
41
|
+
# import and export key, encryption and decryption.
|
42
|
+
class VirgilCrypto
|
43
|
+
attr_reader :default_key_type
|
44
|
+
attr_accessor :use_SHA256_fingerprints
|
45
|
+
|
46
|
+
def initialize(key_pair_type: KeyPairType::Default)
|
47
|
+
@default_key_type = key_pair_type
|
48
|
+
end
|
49
|
+
|
50
|
+
CUSTOM_PARAM_KEY_SIGNATURE = Bytes.from_string(
|
51
|
+
'VIRGIL-DATA-SIGNATURE'
|
52
|
+
)
|
53
|
+
|
54
|
+
CUSTOM_PARAM_KEY_SIGNER_ID = Bytes.from_string(
|
55
|
+
'VIRGIL-DATA-SIGNER-ID'
|
56
|
+
)
|
57
|
+
|
58
|
+
# Generates asymmetric key pair that is comprised of both public
|
59
|
+
# and private keys by specified type.
|
60
|
+
# @param keys_type [Symbol] type of the generated keys.
|
61
|
+
# The possible values can be found in KeyPairType enum.
|
62
|
+
# @param key_material [Bytes] the only data to be used for
|
63
|
+
# key generation, length must be more than 31.
|
64
|
+
# @return [KeyPair] Generated key pair with the special type.
|
65
|
+
# @example Generated key pair with default type FAST_EC_ED25519
|
66
|
+
# include Virgil::Crypto
|
67
|
+
# crypto = VirgilCrypto.new
|
68
|
+
# alice_keys = crypto.generate_keys
|
69
|
+
# @example Generated key pair with type EC_SECP256R1
|
70
|
+
# include Virgil::Crypto
|
71
|
+
# crypto = VirgilCrypto.new
|
72
|
+
# alice_keys = crypto.generate_keys(key_type: KeyPairType::EC_SECP256R1)
|
73
|
+
def generate_keys(keys_type: @default_key_type, key_material: nil)
|
74
|
+
key_material = Validation.check_filled_array_argument!(key_material) if key_material
|
75
|
+
begin
|
76
|
+
native_type = KeyPairType.convert_to_native(keys_type)
|
77
|
+
native_key_pair = nil
|
78
|
+
native_key_pair = if key_material
|
79
|
+
Core::VirgilKeyPair.generate_from_key_material(
|
80
|
+
native_type,
|
81
|
+
key_material
|
82
|
+
)
|
83
|
+
else
|
84
|
+
Core::VirgilKeyPair.generate(native_type)
|
85
|
+
end
|
86
|
+
key_pair_id = compute_public_key_hash(native_key_pair.public_key)
|
87
|
+
private_key = VirgilPrivateKey.new(
|
88
|
+
key_pair_id,
|
89
|
+
wrap_bytes(
|
90
|
+
Core::VirgilKeyPair.private_key_to_der(native_key_pair.private_key)
|
91
|
+
)
|
92
|
+
)
|
93
|
+
public_key = VirgilPublicKey.new(
|
94
|
+
key_pair_id,
|
95
|
+
wrap_bytes(
|
96
|
+
Core::VirgilKeyPair.public_key_to_der(native_key_pair.public_key)
|
97
|
+
)
|
98
|
+
)
|
99
|
+
return KeyPair.new(private_key, public_key)
|
100
|
+
rescue StandardError => error
|
101
|
+
raise VirgilCryptoException, error.message
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Imports the Private key from material representation.
|
106
|
+
# @param key_bytes [Bytes] private key material
|
107
|
+
# representation bytes.
|
108
|
+
# @param password [String] private key password, nil by default.
|
109
|
+
# @return [VirgilPrivateKey] Imported private key.
|
110
|
+
# @example
|
111
|
+
# private_key = crypto.import_private_key(exported_private_key, 'my_password')
|
112
|
+
# @see #export_private_key How to get exported_private_key
|
113
|
+
def import_private_key(key_bytes, password = nil)
|
114
|
+
key_bytes = Validation.check_filled_array_argument!(key_bytes)
|
115
|
+
|
116
|
+
begin
|
117
|
+
decrypted_private_key = if !password
|
118
|
+
Core::VirgilKeyPair.private_key_to_der(key_bytes)
|
119
|
+
else
|
120
|
+
Core::VirgilKeyPair.decrypt_private_key(
|
121
|
+
key_bytes,
|
122
|
+
Bytes.from_string(password)
|
123
|
+
)
|
124
|
+
end
|
125
|
+
|
126
|
+
public_key_bytes = Core::VirgilKeyPair.extract_public_key(
|
127
|
+
decrypted_private_key, []
|
128
|
+
)
|
129
|
+
key_pair_id = compute_public_key_hash(public_key_bytes)
|
130
|
+
private_key_bytes = Core::VirgilKeyPair.private_key_to_der(
|
131
|
+
decrypted_private_key
|
132
|
+
)
|
133
|
+
return VirgilPrivateKey.new(key_pair_id, wrap_bytes(private_key_bytes))
|
134
|
+
rescue => error
|
135
|
+
raise VirgilCryptoException, error.message
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
# Imports the Public key from material representation.
|
141
|
+
# @param key_bytes [Bytes] public key material
|
142
|
+
# representation bytes.
|
143
|
+
# @return [VirgilPublicKey] Imported public key.
|
144
|
+
# @example
|
145
|
+
# public_key = crypto.import_public_key(exported_public_key)
|
146
|
+
# @see #export_public_key How to get exported_public_key
|
147
|
+
def import_public_key(key_bytes)
|
148
|
+
key_bytes = Validation.check_filled_array_argument!(key_bytes)
|
149
|
+
|
150
|
+
begin
|
151
|
+
key_pair_id = compute_public_key_hash(key_bytes)
|
152
|
+
public_key_bytes = Core::VirgilKeyPair.public_key_to_der(key_bytes)
|
153
|
+
VirgilPublicKey.new(key_pair_id, wrap_bytes(public_key_bytes))
|
154
|
+
rescue StandardError => error
|
155
|
+
raise VirgilCryptoException, error.message
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# Exports the Private key into material representation.
|
160
|
+
# @param private_key [VirgilPrivateKey] private key for export.
|
161
|
+
# @param password [String] private key password, nil by default.
|
162
|
+
# @return [Bytes] Private key material representation bytes.
|
163
|
+
# @example
|
164
|
+
# include Virgil::Crypto
|
165
|
+
# crypto = VirgilCrypto.new
|
166
|
+
# alice_keys = crypto.generate_keys
|
167
|
+
# exported_private_key = crypto.export_private_key(alice_keys.private_key, 'my_password')
|
168
|
+
def export_private_key(private_key, password = nil)
|
169
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
170
|
+
|
171
|
+
begin
|
172
|
+
unless password
|
173
|
+
return Core::VirgilKeyPair.private_key_to_der(
|
174
|
+
private_key.raw_key
|
175
|
+
)
|
176
|
+
end
|
177
|
+
password_bytes = Bytes.from_string(password)
|
178
|
+
private_key_bytes = Core::VirgilKeyPair.encrypt_private_key(
|
179
|
+
private_key.raw_key,
|
180
|
+
password_bytes
|
181
|
+
)
|
182
|
+
wrap_bytes(
|
183
|
+
Core::VirgilKeyPair.private_key_to_der(
|
184
|
+
private_key_bytes,
|
185
|
+
password_bytes
|
186
|
+
)
|
187
|
+
)
|
188
|
+
rescue StandardError => error
|
189
|
+
raise VirgilCryptoException, error.message
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# Exports the Public key into material representation.
|
194
|
+
# @param public_key [VirgilPublicKey] public key for export.
|
195
|
+
# @return [Bytes] Key material representation bytes.
|
196
|
+
# @example
|
197
|
+
# include Virgil::Crypto
|
198
|
+
# crypto = VirgilCrypto.new
|
199
|
+
# alice_keys = crypto.generate_keys
|
200
|
+
# exported_public_key = crypto.export_public_key(alice_keys.public_key)
|
201
|
+
def export_public_key(public_key)
|
202
|
+
public_key = Validation.check_type_argument!(VirgilPublicKey, public_key)
|
203
|
+
|
204
|
+
begin
|
205
|
+
wrap_bytes(
|
206
|
+
Core::VirgilKeyPair.public_key_to_der(public_key.raw_key)
|
207
|
+
)
|
208
|
+
rescue StandardError => error
|
209
|
+
raise VirgilCryptoException, error.message
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Extracts the Public key from Private key.
|
214
|
+
# @param private_key [VirgilPrivateKey] source private
|
215
|
+
# key for extraction.
|
216
|
+
# @return [VirgilPublicKey] Exported public key.
|
217
|
+
def extract_public_key(private_key)
|
218
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
219
|
+
|
220
|
+
begin
|
221
|
+
public_key_bytes = Core::VirgilKeyPair.extract_public_key(
|
222
|
+
private_key.raw_key,
|
223
|
+
[]
|
224
|
+
)
|
225
|
+
VirgilPublicKey.new(
|
226
|
+
private_key.id,
|
227
|
+
wrap_bytes(
|
228
|
+
Core::VirgilKeyPair.public_key_to_der(public_key_bytes)
|
229
|
+
)
|
230
|
+
)
|
231
|
+
rescue StandardError => error
|
232
|
+
raise VirgilCryptoException, error.message
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
# Encrypts the specified data using the specified recipients Public keys.
|
237
|
+
# @param bytes [Virgil::Bytes] raw data bytes for encryption.
|
238
|
+
# @param *public_keys [Array<VirgilPublicKey>] list
|
239
|
+
# of public keys.
|
240
|
+
# @return [Bytes] Encrypted bytes.
|
241
|
+
# @example
|
242
|
+
# include Virgil::Crypto
|
243
|
+
# crypto = VirgilCrypto.new
|
244
|
+
# alice_keys = crypto.generate_keys
|
245
|
+
# plain_data = Bytes.from_string('Hello Bob!')
|
246
|
+
# cipher_data = crypto.encrypt(plain_data, alice_keys.public_key)
|
247
|
+
# @see #generate_keys How to generate keys
|
248
|
+
# @see #decrypt How to decrypt data
|
249
|
+
def encrypt(bytes, *public_keys)
|
250
|
+
bytes = Validation.check_filled_array_argument!(bytes)
|
251
|
+
|
252
|
+
begin
|
253
|
+
encrypt_for_recipients(bytes, Core::VirgilCipher.new, public_keys)
|
254
|
+
rescue StandardError => error
|
255
|
+
raise VirgilCryptoException, error.message
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# Decrypts the specified bytes using Private key.
|
260
|
+
# @param cipher_bytes [Bytes] encrypted data bytes for decryption.
|
261
|
+
# @param private_key [VirgilPrivateKey] private key for decryption.
|
262
|
+
# @return [Bytes] Decrypted data bytes.
|
263
|
+
# @example
|
264
|
+
# # You can decrypt data using your private key
|
265
|
+
# include Virgil::Crypto
|
266
|
+
# crypto = VirgilCrypto.new
|
267
|
+
# alice_keys = crypto.generate_keys
|
268
|
+
# plain_data = crypto.decrypt(cipher_data, alice_keys.private_key)
|
269
|
+
# @see #generate_keys
|
270
|
+
# @see #encrypt How to get cipher_data
|
271
|
+
def decrypt(cipher_bytes, private_key)
|
272
|
+
cipher_bytes = Validation.check_filled_array_argument!(cipher_bytes)
|
273
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
274
|
+
|
275
|
+
begin
|
276
|
+
cipher = Core::VirgilCipher.new
|
277
|
+
decrypted_bytes = cipher.decrypt_with_key(
|
278
|
+
cipher_bytes,
|
279
|
+
private_key.id,
|
280
|
+
private_key.raw_key
|
281
|
+
)
|
282
|
+
wrap_bytes(decrypted_bytes)
|
283
|
+
rescue StandardError => error
|
284
|
+
raise VirgilCryptoException, error.message
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
# Signs and encrypts the data.
|
289
|
+
# @param bytes [Bytes] data bytes for signing and encryption.
|
290
|
+
# @param private_key [VirgilPrivateKey] private key to sign the data.
|
291
|
+
# @param *public_keys [Array<VirgilPublicKey>] list of public keys
|
292
|
+
# to encrypt the data.
|
293
|
+
# @return [Bytes] Signed and encrypted data bytes.
|
294
|
+
# @example
|
295
|
+
# include Virgil::Crypto
|
296
|
+
# crypto = VirgilCrypto.new
|
297
|
+
#
|
298
|
+
# alice = crypto.generate_keys
|
299
|
+
# bob = crypto.generate_keys
|
300
|
+
#
|
301
|
+
# # The data to be signed with alice's Private key
|
302
|
+
# data = Bytes.from_string('Hello Bob, How are you?')
|
303
|
+
# cipher_data = crypto.sign_then_encrypt(
|
304
|
+
# data,
|
305
|
+
# alice.private_key,
|
306
|
+
# bob.public_key
|
307
|
+
# )
|
308
|
+
def sign_then_encrypt(bytes, private_key, *public_keys)
|
309
|
+
bytes = Validation.check_filled_array_argument!(bytes)
|
310
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
311
|
+
|
312
|
+
begin
|
313
|
+
cipher = Core::VirgilCipher.new
|
314
|
+
custom_bytes = cipher.custom_params
|
315
|
+
custom_bytes.set_data(
|
316
|
+
CUSTOM_PARAM_KEY_SIGNATURE,
|
317
|
+
generate_signature(bytes, private_key)
|
318
|
+
)
|
319
|
+
|
320
|
+
public_key = extract_public_key(private_key)
|
321
|
+
custom_bytes.set_data(
|
322
|
+
CUSTOM_PARAM_KEY_SIGNER_ID,
|
323
|
+
wrap_bytes(public_key.id)
|
324
|
+
)
|
325
|
+
encrypt_for_recipients(bytes, cipher, public_keys)
|
326
|
+
rescue StandardError => error
|
327
|
+
raise VirgilCryptoException, error.message
|
328
|
+
end
|
329
|
+
end
|
330
|
+
|
331
|
+
# Decrypts and verifies the data.
|
332
|
+
# @param bytes [Bytes] encrypted data bytes.
|
333
|
+
# @param private_key [VirgilPrivateKey] private key for decryption.
|
334
|
+
# @param *public_keys [Array<VirgilPublicKey>] a list of public keys
|
335
|
+
# for verification,
|
336
|
+
# which can contain signer's public key.
|
337
|
+
# @return [Bytes] Decrypted data bytes.
|
338
|
+
# @raise [VirgilCryptoException] if signature is not verified.
|
339
|
+
# @example
|
340
|
+
# include Virgil::Crypto
|
341
|
+
# crypto = VirgilCrypto.new
|
342
|
+
#
|
343
|
+
# alice = crypto.generate_keys
|
344
|
+
# bob = crypto.generate_keys
|
345
|
+
#
|
346
|
+
# decrypted_data = crypto.decrypt_then_verify(
|
347
|
+
# cipher_data,
|
348
|
+
# bob.private_key,
|
349
|
+
# alice.public_key
|
350
|
+
# )
|
351
|
+
# @see #sign_then_encrypt How to get cipher_data
|
352
|
+
def decrypt_then_verify(bytes, private_key, *public_keys)
|
353
|
+
bytes = Validation.check_filled_array_argument!(bytes)
|
354
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
355
|
+
|
356
|
+
begin
|
357
|
+
cipher = Core::VirgilCipher.new
|
358
|
+
decrypted_bytes = cipher.decrypt_with_key(
|
359
|
+
bytes,
|
360
|
+
private_key.id,
|
361
|
+
private_key.raw_key
|
362
|
+
)
|
363
|
+
signature = cipher.custom_params.get_data(CUSTOM_PARAM_KEY_SIGNATURE)
|
364
|
+
|
365
|
+
signer_public_key = public_keys.first
|
366
|
+
|
367
|
+
if public_keys.count > 1
|
368
|
+
signer_id = cipher.custom_params.get_data(CUSTOM_PARAM_KEY_SIGNER_ID)
|
369
|
+
signer_public_key = public_keys.find {|public_key| public_key.id == signer_id}
|
370
|
+
end
|
371
|
+
|
372
|
+
is_valid = verify_signature(signature, decrypted_bytes, signer_public_key)
|
373
|
+
raise VirgilCryptoException, 'Signature is not valid' unless is_valid
|
374
|
+
|
375
|
+
wrap_bytes(decrypted_bytes)
|
376
|
+
rescue StandardError => error
|
377
|
+
raise VirgilCryptoException, error.message
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
# Signs the specified data using Private key.
|
382
|
+
# @param bytes [Bytes] raw data bytes for signing.
|
383
|
+
# @param private_key [VirgilPrivateKey] private key for signing.
|
384
|
+
# @return [Bytes] Signature data.
|
385
|
+
# @example Sign the fingerprint of bytes using your private key.
|
386
|
+
# include Virgil::Crypto
|
387
|
+
# crypto = VirgilCrypto.new
|
388
|
+
# alice_keys = crypto.generate_keys()
|
389
|
+
# # The data to be signed with alice's Private key
|
390
|
+
# data = Bytes.from_string('Hello Bob, How are you?')
|
391
|
+
# signature = crypto.generate_signature(data, alice.private_key)
|
392
|
+
# @see #generate_keys
|
393
|
+
# @see #verify_signature How to verify signature
|
394
|
+
def generate_signature(bytes, private_key)
|
395
|
+
bytes = Validation.check_filled_array_argument!(bytes)
|
396
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
397
|
+
|
398
|
+
begin
|
399
|
+
native_algorithm = HashAlgorithm.convert_to_native(HashAlgorithm::SHA512)
|
400
|
+
signer = Core::VirgilSigner.new(native_algorithm)
|
401
|
+
wrap_bytes(signer.sign(bytes, private_key.raw_key))
|
402
|
+
rescue StandardError => error
|
403
|
+
raise VirgilCryptoException, error.message
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
# Verifies the specified signature using original data
|
408
|
+
# and signer's public key.
|
409
|
+
# @param bytes [Bytes] original data bytes for verification.
|
410
|
+
# @param signature [Bytes] signature bytes for verification.
|
411
|
+
# @param signer_public_key [VirgilPublicKey] signer public
|
412
|
+
# key for verification.
|
413
|
+
# @return [Boolean] True if signature is valid, False otherwise.
|
414
|
+
# @example Verify the signature of the fingerprint of
|
415
|
+
# bytes using Public key.
|
416
|
+
# include Virgil::Crypto
|
417
|
+
# crypto = VirgilCrypto.new
|
418
|
+
# alice_keys = crypto.generate_keys()
|
419
|
+
# data = Bytes.from_string('Hello Bob, How are you?')
|
420
|
+
# is_valid = crypto.verify_signature(signature, data, alice.public_key)
|
421
|
+
# @see #generate_signature How to get signature
|
422
|
+
def verify_signature(signature, bytes, signer_public_key)
|
423
|
+
signature = Validation.check_filled_array_argument!(signature)
|
424
|
+
bytes = Validation.check_filled_array_argument!(bytes)
|
425
|
+
signer_public_key = Validation.check_type_argument!(VirgilPublicKey, signer_public_key)
|
426
|
+
|
427
|
+
begin
|
428
|
+
native_algorithm = HashAlgorithm.convert_to_native(HashAlgorithm::SHA512)
|
429
|
+
signer = Core::VirgilSigner.new(native_algorithm)
|
430
|
+
signer.verify(bytes, signature, signer_public_key.raw_key)
|
431
|
+
rescue StandardError => error
|
432
|
+
raise VirgilCryptoException, error.message
|
433
|
+
end
|
434
|
+
|
435
|
+
end
|
436
|
+
|
437
|
+
# Encrypts the specified stream using public_keys Public keys.
|
438
|
+
# @param input_stream [IO] readable stream containing input bytes.
|
439
|
+
# @param cipher_stream [IO] writable stream for output.
|
440
|
+
# @param *public_keys [Array<VirgilPublicKey>] list of
|
441
|
+
# public_keys' public keys.
|
442
|
+
# @example
|
443
|
+
# include Virgil::Crypto
|
444
|
+
# crypto = VirgilCrypto.new
|
445
|
+
# alice_keys = crypto.generate_keys()
|
446
|
+
# File.open('[YOUR_FILE_PATH_HERE]', 'r') do |input_stream|
|
447
|
+
# File.open('[YOUR_CIPHER_FILE_PATH_HERE]', 'w') do |cipher_stream|
|
448
|
+
# crypto.encrypt_stream(input_stream,
|
449
|
+
# cipher_stream, alice_keys.public_key)
|
450
|
+
# end
|
451
|
+
# end
|
452
|
+
def encrypt_stream(input_stream, cipher_stream, *public_keys)
|
453
|
+
begin
|
454
|
+
cipher = Core::VirgilChunkCipher.new
|
455
|
+
public_keys.each do |public_key|
|
456
|
+
public_key = Validation.check_type_argument!(VirgilPublicKey, public_key)
|
457
|
+
cipher.add_key_recipient(public_key.id, public_key.raw_key)
|
458
|
+
end
|
459
|
+
source = VirgilStreamDataSource.new(input_stream)
|
460
|
+
sink = VirgilStreamDataSink.new(cipher_stream)
|
461
|
+
cipher.encrypt(source, sink)
|
462
|
+
rescue StandardError => error
|
463
|
+
raise VirgilCryptoException, error.message
|
464
|
+
end
|
465
|
+
end
|
466
|
+
|
467
|
+
# Decrypts the specified stream using Private key.
|
468
|
+
# @param cipher_stream [IO] readable stream containing encrypted data.
|
469
|
+
# @param output_stream [IO] writable stream for output.
|
470
|
+
# @param private_key [VirgilPrivateKey] private key for decryption.
|
471
|
+
# @example
|
472
|
+
# include Virgil::Crypto
|
473
|
+
# crypto = VirgilCrypto.new
|
474
|
+
# File.open('[YOUR_CIPHER_FILE_PATH_HERE]', 'r') do |cipher_stream|
|
475
|
+
# File.open('[YOUR_DECRYPTED_FILE_PATH_HERE]', 'w') do |decrypted_stream|
|
476
|
+
# alice_private_key = crypto.import_private_key(exported_private_key)
|
477
|
+
# crypto.decrypt_stream(cipher_stream, decrypted_stream, alice_private_key)
|
478
|
+
# end
|
479
|
+
# end
|
480
|
+
# @see #encrypt_stream How to get cipher_stream
|
481
|
+
# @see #export_private_key How to get exported_private_key
|
482
|
+
def decrypt_stream(cipher_stream, output_stream, private_key)
|
483
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
484
|
+
begin
|
485
|
+
cipher = Core::VirgilChunkCipher.new
|
486
|
+
source = VirgilStreamDataSource.new(cipher_stream)
|
487
|
+
sink = VirgilStreamDataSink.new(output_stream)
|
488
|
+
cipher.decrypt_with_key(source, sink, private_key.id, private_key.raw_key)
|
489
|
+
rescue StandardError => error
|
490
|
+
raise VirgilCryptoException, error.message
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
# Signs the specified stream using Private key.
|
495
|
+
# @param input_stream [IO] readable stream containing input data.
|
496
|
+
# @param private_key [VirgilPrivateKey] private key for signing.
|
497
|
+
# @return [Bytes] Signature bytes.
|
498
|
+
# @example
|
499
|
+
# include Virgil::Crypto
|
500
|
+
# crypto = VirgilCrypto.new
|
501
|
+
# alice_keys = crypto.generate_keys()
|
502
|
+
# File.open('[YOUR_FILE_PATH_HERE]', 'r') do |input_stream|
|
503
|
+
# signature = crypto.generate_stream_signature(input_stream, alice_keys.private_key)
|
504
|
+
# end
|
505
|
+
# @see #verify_stream_signature How to verify the signature
|
506
|
+
def generate_stream_signature(input_stream, private_key)
|
507
|
+
private_key = Validation.check_type_argument!(VirgilPrivateKey, private_key)
|
508
|
+
|
509
|
+
begin
|
510
|
+
native_algorithm = HashAlgorithm.convert_to_native(HashAlgorithm::SHA512)
|
511
|
+
signer = Core::VirgilStreamSigner.new(native_algorithm)
|
512
|
+
source = VirgilStreamDataSource.new(input_stream)
|
513
|
+
wrap_bytes(signer.sign(source, private_key.raw_key))
|
514
|
+
rescue StandardError => error
|
515
|
+
raise VirgilCryptoException, error.message
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
# Verifies the specified signature using original stream and signer's Public key.
|
520
|
+
# @param input_stream [IO] readable stream containing input data.
|
521
|
+
# @param signature [Bytes] signature bytes for verification.
|
522
|
+
# @param signer_public_key [VirgilPublicKey] signer public key for verification.
|
523
|
+
# @return [Boolean] True if signature is valid, False otherwise.
|
524
|
+
# @example
|
525
|
+
# include Virgil::Crypto
|
526
|
+
# crypto = VirgilCrypto.new
|
527
|
+
# alice_keys = crypto.generate_keys()
|
528
|
+
# File.open('[YOUR_FILE_PATH_HERE]', 'r') do |input_stream|
|
529
|
+
# verified = crypto.verify_stream_signature(signature, input_stream, alice_keys.public_key)
|
530
|
+
# end
|
531
|
+
# @see #generate_stream_signature How to get the signature
|
532
|
+
def verify_stream_signature(signature, input_stream, signer_public_key)
|
533
|
+
signature = Validation.check_filled_array_argument!(signature)
|
534
|
+
signer_public_key = Validation.check_type_argument!(VirgilPublicKey, signer_public_key)
|
535
|
+
|
536
|
+
begin
|
537
|
+
native_algorithm = HashAlgorithm.convert_to_native(HashAlgorithm::SHA512)
|
538
|
+
signer = Core::VirgilStreamSigner.new(native_algorithm)
|
539
|
+
source = VirgilStreamDataSource.new(input_stream)
|
540
|
+
signer.verify(source, signature, signer_public_key.raw_key)
|
541
|
+
rescue StandardError => error
|
542
|
+
raise VirgilCryptoException, error.message
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
# Computes the hash of specified data and the specified [HashAlgorithm]
|
547
|
+
# @param bytes [Bytes] original data bytes to be hashed.
|
548
|
+
# @param algorithm [HashAlgorithm] hashing algorithm.
|
549
|
+
# The possible values can be found in HashAlgorithm enum.
|
550
|
+
# @return [Bytes] Hash bytes.
|
551
|
+
def generate_hash(bytes, algorithm = nil)
|
552
|
+
bytes = Validation.check_filled_array_argument!(bytes)
|
553
|
+
|
554
|
+
alg = algorithm
|
555
|
+
alg ||= use_SHA256_fingerprints ? HashAlgorithm::SHA256 : HashAlgorithm::SHA512
|
556
|
+
|
557
|
+
begin
|
558
|
+
native_algorithm = HashAlgorithm.convert_to_native(alg)
|
559
|
+
native_hasher = Core::VirgilHash.new(native_algorithm)
|
560
|
+
wrap_bytes(native_hasher.hash(bytes))
|
561
|
+
rescue StandardError => error
|
562
|
+
raise VirgilCryptoException, error.message
|
563
|
+
end
|
564
|
+
end
|
565
|
+
|
566
|
+
private
|
567
|
+
|
568
|
+
attr_reader :default_key_type
|
569
|
+
|
570
|
+
def encrypt_for_recipients(bytes, cipher, public_keys)
|
571
|
+
public_keys.each do |public_key|
|
572
|
+
public_key = Validation.check_type_argument!(VirgilPublicKey, public_key)
|
573
|
+
cipher.add_key_recipient(public_key.id, public_key.raw_key)
|
574
|
+
end
|
575
|
+
wrap_bytes(cipher.encrypt(bytes, true))
|
576
|
+
end
|
577
|
+
|
578
|
+
def wrap_bytes(raw_bytes)
|
579
|
+
Bytes.new(raw_bytes)
|
580
|
+
end
|
581
|
+
|
582
|
+
# Computes the hash of specified public key using SHA256 algorithm.
|
583
|
+
# @param public_key [VirgilPublicKey] public key for hashing.
|
584
|
+
# @return [Bytes] Hash bytes.
|
585
|
+
def compute_public_key_hash(public_key)
|
586
|
+
public_key_der = Core::VirgilKeyPair.public_key_to_der(public_key)
|
587
|
+
if use_SHA256_fingerprints
|
588
|
+
return generate_hash(public_key_der, HashAlgorithm::SHA256)
|
589
|
+
end
|
590
|
+
|
591
|
+
generate_hash(public_key_der, HashAlgorithm::SHA512)[0..7]
|
592
|
+
end
|
593
|
+
end
|
594
|
+
end
|
595
|
+
end
|