Skip to content

Commit 18f785b

Browse files
committed
porting from neopt
1 parent 62a3979 commit 18f785b

12 files changed

+1070
-0
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "libopenssl"]
2+
path = libopenssl
3+
url = https://github.com/openssl/openssl.git

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
all:
2+
3+
4+
vendor: openssl #cryptopp clang gtests
5+
6+
openssl:
7+
#cd src/core && chmod +x linux_get_build_openssl.sh
8+
#cd src/core && ./linux_get_build_openssl.sh
9+
mkdir -p build/openssl
10+
(cd build/openssl && ../../libopenssl/config && make && make test)
11+
cp -r libopenssl/include build/openssl/ # include files
12+
cp build/openssl/libcrypto.a build/openssl/liblinux-openssl-crypto-x86_64.a
13+
#cp -r build/openssl/include/openssl/* build/openssl/include/openssl/
14+
#mv tmp_build/libcrypto.a crypto/openssl/liblinux-openssl-crypto-x86_64.a
15+
16+

build/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
openssl

libopenssl

Submodule libopenssl added at 50eaac9

src/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cryptopp
2+
cryptopp-2

src/Crypto.h

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#ifndef CRYPTO_H
2+
#define CRYPTO_H
3+
4+
// WARNING: do not include .hpp here, or things may break!
5+
6+
// implementation of Crypto
7+
8+
// system includes
9+
#include <assert.h>
10+
#include <cstring>
11+
#include <string>
12+
13+
/*
14+
// third-party includes
15+
#include <openssl/obj_mac.h> // for NID_secp192k1
16+
17+
#include <openssl/ec.h> // for EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_KEY_new, EC_KEY_set_group, EC_KEY_generate_key, EC_KEY_free
18+
#include <openssl/ecdsa.h> // for ECDSA_do_sign, ECDSA_do_verify
19+
#include <openssl/ripemd.h>
20+
#include <openssl/sha.h>
21+
*/
22+
// core includes
23+
#include <crypto/ICrypto.h>
24+
25+
namespace neopt {
26+
27+
// cryptography for Neo
28+
class Crypto : public ICrypto
29+
{
30+
private:
31+
static ICrypto* _crypto;
32+
33+
public:
34+
static const ICrypto& Default()
35+
{
36+
if (_crypto == nullptr)
37+
_crypto = new Crypto();
38+
return *_crypto;
39+
}
40+
41+
static void Free()
42+
{
43+
if (_crypto != nullptr)
44+
delete _crypto;
45+
_crypto = nullptr;
46+
}
47+
48+
vbyte Hash160(const vbyte& message) const;
49+
50+
vbyte Hash256(const vbyte& message) const;
51+
52+
bool VerifySignature(const vbyte& message, const vbyte& signature, const vbyte& pubkey) const;
53+
54+
// SHA256
55+
vbyte Sha256(const vbyte& message) const;
56+
57+
// RIPEMD160
58+
vbyte RIPEMD160(const vbyte& message) const;
59+
60+
vbyte Sign(const vbyte& message, const vbyte& privkey, const vbyte& pubkey) const
61+
{
62+
return SignData(Sha256(message), privkey, pubkey);
63+
}
64+
/*
65+
public byte[] Sign(byte[] message, byte[] prikey, byte[] pubkey)
66+
{
67+
using (var ecdsa = ECDsa.Create(new ECParameters
68+
{
69+
Curve = ECCurve.NamedCurves.nistP256,
70+
D = prikey,
71+
Q = new ECPoint
72+
{
73+
X = pubkey.Take(32).ToArray(),
74+
Y = pubkey.Skip(32).ToArray()
75+
}
76+
}))
77+
{
78+
return ecdsa.SignData(message, HashAlgorithmName.SHA256);
79+
}
80+
}
81+
*/
82+
83+
// TODO: receive pubkey or already ECPoint(X,Y) ?
84+
vbyte SignData(const vbyte& digest, const vbyte& prikey, const vbyte& pubkey) const;
85+
86+
virtual vbyte GeneratePrivateKey(vbyte& vpubkey) const;
87+
88+
// manually added
89+
static vbyte FromHexString(std::string hex)
90+
{
91+
vbyte bytes(hex.length() / 2);
92+
for (unsigned int i = 0; i < hex.length(); i += 2) {
93+
std::string byteString = hex.substr(i, 2);
94+
byte b = (byte)strtol(byteString.c_str(), NULL, 16);
95+
bytes[i] = b;
96+
}
97+
return bytes;
98+
}
99+
100+
// Sha3 (optional) - implemented via openssl... keccak (older) or NIST SHA-3?
101+
vbyte Sha3NIST(const vbyte& message) const;
102+
};
103+
104+
}
105+
106+
#endif

src/CryptoExtra.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <crypto/CryptoExtra.h>
2+
3+
#include <crypto/cryptopp/keccak.h> // sha-3 keccak (not NIST SHA-3)
4+
5+
#include<iostream>
6+
7+
using namespace neopt;
8+
9+
void
10+
lComputeKeccak(const unsigned char *message, size_t message_len, vbyte& digest);
11+
12+
13+
vbyte CryptoExtra::Sha3NIST(const vbyte& message) const
14+
{
15+
NEOPT_EXCEPTION("NOT IMPLEMENTED SHA3 NIST");
16+
vbyte voutput(0);
17+
return voutput;
18+
}
19+
20+
vbyte CryptoExtra::Sha3Keccak(const vbyte& message) const
21+
{
22+
vbyte digest;
23+
lComputeKeccak(message.data(), message.size(), digest);
24+
return digest;
25+
}
26+
27+
void
28+
lComputeSHA3(const unsigned char *message, size_t message_len, unsigned char **digest, unsigned int *digest_len)
29+
{
30+
}
31+
32+
// "official" keccak via cryptopp
33+
void
34+
lComputeKeccak(const unsigned char *message, size_t message_len, vbyte& digest)
35+
{
36+
std::cout << "Creating Keccak" << std::endl;
37+
CryptoPP::Keccak_256 hash;
38+
std::cout << "Update hash Keccak" << std::endl;
39+
hash.Update(message, message_len);
40+
41+
digest.resize(hash.DigestSize());
42+
hash.Final((byte*)&digest[0]);
43+
/*
44+
bool verified = hash.Verify((const byte*)digest.data());
45+
46+
if (verified == true)
47+
std::cout << "Verified hash over message" << std::endl;
48+
else
49+
std::cout << "Failed to verify hash over message" << std::endl;
50+
*/
51+
52+
std::cout << "Finished Keccak" << std::endl;
53+
}
54+

src/CryptoExtra.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef CRYPTO_EXTRA_H
2+
#define CRYPTO_EXTRA_H
3+
4+
// WARNING: do not include .hpp here, or things may break!
5+
6+
// implementation of Crypto Extra (using cryptopp lib)
7+
8+
// system includes
9+
#include <assert.h>
10+
#include <cstring>
11+
#include <string>
12+
13+
/*
14+
// third-party includes
15+
#include <openssl/obj_mac.h> // for NID_secp192k1
16+
17+
#include <openssl/ec.h> // for EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_KEY_new, EC_KEY_set_group, EC_KEY_generate_key, EC_KEY_free
18+
#include <openssl/ecdsa.h> // for ECDSA_do_sign, ECDSA_do_verify
19+
#include <openssl/ripemd.h>
20+
#include <openssl/sha.h>
21+
*/
22+
// core includes
23+
#include <crypto/ICrypto.h>
24+
25+
namespace neopt {
26+
27+
class CryptoExtra
28+
{
29+
public:
30+
31+
// Sha3 (optional) - implemented via openssl... keccak (older) or NIST SHA-3?
32+
vbyte Sha3NIST(const vbyte& message) const;
33+
34+
// Keccak "official" (not new NIST SHA-3)
35+
vbyte Sha3Keccak(const vbyte& message) const;
36+
};
37+
38+
}
39+
40+
#endif

0 commit comments

Comments
 (0)