Skip to content

Commit

Permalink
Merge branch 'feature/shared_model-crypto_provider_interfaces' into f…
Browse files Browse the repository at this point in the history
…eature/shared_model
  • Loading branch information
muratovv authored and lebdron committed Dec 19, 2017
2 parents c59fcd9 + b935fa8 commit 6b8253b
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
* limitations under the License.
*/

#include "cryptography/ed25519_sha3_impl/hash_provider.hpp"
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"
#ifndef IROHA_SHARED_MODEL_CRYPTO_DEFAULTS_HPP
#define IROHA_SHARED_MODEL_CRYPTO_DEFAULTS_HPP

#include "cryptography/ed25519_sha3_impl/crypto_provider.hpp"

namespace shared_model {
namespace crypto {
Hash HashProvider::sha3_256(const Blob &blob) const {
return Hash(iroha::sha3_256(blob.blob()).to_string());
}
Hash HashProvider::sha3_512(const Blob &blob) const {
return Hash(iroha::sha3_512(blob.blob()).to_string());
}
/// Default type of crypto algorithm
using DefaultCryptoAlgorithmType = CryptoProviderEd25519Sha3;
} // namespace crypto
} // namespace shared_model
#endif // IROHA_SHARED_MODEL_CRYPTO_DEFAULTS_HPP
51 changes: 51 additions & 0 deletions shared_model/cryptography/crypto_provider/crypto_signer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_CRYPTO_SIGNER_HPP
#define IROHA_CRYPTO_SIGNER_HPP

#include "cryptography/blob.hpp"
#include "cryptography/crypto_provider/crypto_defaults.hpp"
#include "cryptography/keypair.hpp"
#include "cryptography/signed.hpp"

namespace shared_model {
namespace crypto {
/**
* CryptoSigner - wrapper for generalization signing for different
* cryptographic algorithms
* @tparam Algorithm - cryptographic algorithm for singing
*/
template <typename Algorithm = DefaultCryptoAlgorithmType>
class CryptoSigner {
public:
/**
* Generate signature for target data
* @param blob - data for signing
* @param keypair - (public, private) keys for signing
* @return signature's blob
*/
static Signed sign(const Blob &blob, const Keypair &keypair) {
return Algorithm::sign(blob, keypair);
}

/// close constructor for forbidding instantiation
CryptoSigner() = delete;
};
} // namespace crypto
} // namespace shared_model
#endif // IROHA_CRYPTO_SIGNER_HPP
54 changes: 54 additions & 0 deletions shared_model/cryptography/crypto_provider/crypto_verifier.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_CRYPTO_VERIFIER_HPP
#define IROHA_CRYPTO_VERIFIER_HPP

#include "cryptography/blob.hpp"
#include "cryptography/crypto_provider/crypto_defaults.hpp"
#include "cryptography/keypair.hpp"
#include "cryptography/signed.hpp"

namespace shared_model {
namespace crypto {
/**
* CryptoVerifier - wrapper for generalization verification of cryptographic
* signatures
* @tparam Algorithm - cryptographic algorithm for verification
*/
template <typename Algorithm = DefaultCryptoAlgorithmType>
class CryptoVerifier {
public:
/**
* Verify signature attached to source data
* @param signedData - cryptographic signature
* @param source - data that was signed
* @param pubKey - public key of signatory
* @return true if signature correct
*/
static bool verify(const Signed &signedData,
const Blob &source,
const PublicKey &pubKey) {
return Algorithm::verify(signedData, source, pubKey);
}

/// close constructor for forbidding instantiation
CryptoVerifier() = delete;
};
} // namespace crypto
} // namespace shared_model
#endif // IROHA_CRYPTO_VERIFIER_HPP
17 changes: 8 additions & 9 deletions shared_model/cryptography/ed25519_sha3_impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
add_subdirectory(internal)
add_subdirectory(bindings)

add_library(shared_ed25519_sha3
signer.cpp
verifier.cpp
crypto_provider.cpp
hash_provider.cpp
)
add_library(shared_model_ed25519_sha3
signer.cpp
verifier.cpp
crypto_provider.cpp
)

target_link_libraries(shared_ed25519_sha3
cryptography
)
target_link_libraries(shared_model_ed25519_sha3
cryptography
)
25 changes: 14 additions & 11 deletions shared_model/cryptography/ed25519_sha3_impl/crypto_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,37 @@

#include "cryptography/ed25519_sha3_impl/crypto_provider.hpp"
#include "cryptography/ed25519_sha3_impl/internal/ed25519_impl.hpp"
#include "cryptography/ed25519_sha3_impl/signer.hpp"
#include "cryptography/ed25519_sha3_impl/verifier.hpp"

namespace shared_model {
namespace crypto {

Signed CryptoProvider::sign(const Blob &blob,
const Keypair &keypair) const {
return signer_.sign(blob, keypair);
Signed CryptoProviderEd25519Sha3::sign(const Blob &blob,
const Keypair &keypair) {
return Signer::sign(blob, keypair);
}

bool CryptoProvider::verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey) const {
return verifier_.verify(signedData, orig, publicKey);
bool CryptoProviderEd25519Sha3::verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey) {
return Verifier::verify(signedData, orig, publicKey);
}

Seed CryptoProvider::generateSeed() const {
Seed CryptoProviderEd25519Sha3::generateSeed() {
return Seed(iroha::create_seed().to_string());
}

Seed CryptoProvider::generateSeed(const std::string &passphrase) const {
Seed CryptoProviderEd25519Sha3::generateSeed(
const std::string &passphrase) {
return Seed(iroha::create_seed(passphrase).to_string());
}

Keypair CryptoProvider::generateKeypair() const {
Keypair CryptoProviderEd25519Sha3::generateKeypair() {
return generateKeypair(generateSeed());
}

Keypair CryptoProvider::generateKeypair(const Seed &seed) const {
Keypair CryptoProviderEd25519Sha3::generateKeypair(const Seed &seed) {
auto keypair =
iroha::create_keypair(seed.makeOldModel<Seed::OldSeedType>());
return Keypair(PublicKey(keypair.pubkey.to_string()),
Expand Down
27 changes: 11 additions & 16 deletions shared_model/cryptography/ed25519_sha3_impl/crypto_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
#ifndef IROHA_CRYPTOPROVIDER_HPP
#define IROHA_CRYPTOPROVIDER_HPP

#include "cryptography/ed25519_sha3_impl/signer.hpp"
#include "cryptography/ed25519_sha3_impl/verifier.hpp"
#include "cryptography/keypair.hpp"
#include "cryptography/seed.hpp"
#include "cryptography/signed.hpp"

namespace shared_model {
namespace crypto {
/**
* Wrapper class for signing-related stuff.
*/
class CryptoProvider {
class CryptoProviderEd25519Sha3 {
public:
/**
* Signs the message.
* @param blob - blob to sign
* @param keypair - keypair
* @return Signed object with signed data
*/
Signed sign(const Blob &blob, const Keypair &keypair) const;
static Signed sign(const Blob &blob, const Keypair &keypair);

/**
* Verifies signature.
Expand All @@ -44,39 +44,34 @@ namespace shared_model {
* @param publicKey - public key
* @return true if verify was OK or false otherwise
*/
bool verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey) const;

static bool verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey);
/**
* Generates new seed
* @return Seed generated
*/
Seed generateSeed() const;
static Seed generateSeed();

/**
* Generates new seed from a provided passphrase
* @param passphrase - passphrase to generate seed from
* @return Seed generated
*/
Seed generateSeed(const std::string &passphrase) const;
static Seed generateSeed(const std::string &passphrase);

/**
* Generates new keypair with a default seed
* @return Keypair generated
*/
Keypair generateKeypair() const;
static Keypair generateKeypair();

/**
* Generates new keypair from a provided seed
* @param seed - provided seed
* @return generated keypair
*/
Keypair generateKeypair(const Seed &seed) const;

private:
Signer signer_;
Verifier verifier_;
static Keypair generateKeypair(const Seed &seed);
};
} // namespace crypto
} // namespace shared_model
Expand Down
48 changes: 0 additions & 48 deletions shared_model/cryptography/ed25519_sha3_impl/hash_provider.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion shared_model/cryptography/ed25519_sha3_impl/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace shared_model {
namespace crypto {
Signed Signer::sign(const Blob &blob, const Keypair &keypair) const {
Signed Signer::sign(const Blob &blob, const Keypair &keypair) {
return Signed(
iroha::sign(
blob.blob(),
Expand Down
2 changes: 1 addition & 1 deletion shared_model/cryptography/ed25519_sha3_impl/signer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace shared_model {
* @param keypair - keypair with public and private keys
* @return Signed object with signed data
*/
Signed sign(const Blob &blob, const Keypair &keypair) const;
static Signed sign(const Blob &blob, const Keypair &keypair);
};
} // namespace crypto
} // namespace shared_model
Expand Down
2 changes: 1 addition & 1 deletion shared_model/cryptography/ed25519_sha3_impl/verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace shared_model {
namespace crypto {
bool Verifier::verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey) const {
const PublicKey &publicKey) {
return iroha::verify(
orig.blob(),
publicKey.makeOldModel<PublicKey::OldPublicKeyType>(),
Expand Down
6 changes: 3 additions & 3 deletions shared_model/cryptography/ed25519_sha3_impl/verifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace shared_model {
*/
class Verifier {
public:
bool verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey) const;
static bool verify(const Signed &signedData,
const Blob &orig,
const PublicKey &publicKey);
};

} // namespace crypto
Expand Down
5 changes: 2 additions & 3 deletions shared_model/cryptography/keypair.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ namespace shared_model {
interface::Primitive<Keypair, iroha::keypair_t>::OldModelType *
makeOldModel() const override {
return new iroha::keypair_t{
.pubkey = publicKey().makeOldModel<PublicKey::OldPublicKeyType>(),
.privkey =
privateKey().makeOldModel<PrivateKey::OldPrivateKeyType>()};
publicKey().makeOldModel<PublicKey::OldPublicKeyType>(),
privateKey().makeOldModel<PrivateKey::OldPrivateKeyType>()};
}

Keypair *copy() const override {
Expand Down
2 changes: 1 addition & 1 deletion shared_model/cryptography/seed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ namespace shared_model {
}
};
} // namespace crypto
}; // namespace shared_model
} // namespace shared_model

#endif // IROHA_SEED_HPP
Loading

0 comments on commit 6b8253b

Please sign in to comment.