From 52a93d3bf785ada75b2aea500a081b5ac1a00b12 Mon Sep 17 00:00:00 2001 From: Rich Zhao Date: Thu, 27 Oct 2022 21:10:09 +0800 Subject: [PATCH] crypt --- crypto/crypto.go | 80 +++++++++++++++++++++++++ crypto/ed29919csp.go | 122 ++++++++++++++++++++++++++++++++++++++ crypto/ed29919csp_test.go | 1 + crypto/opts.go | 24 ++++++++ crypto/rsacsp.go | 72 ++++++++++++++++++++++ node/config.go | 24 ++++++++ node/config.yaml | 12 ++++ node/node.go | 2 +- 8 files changed, 336 insertions(+), 1 deletion(-) create mode 100644 crypto/crypto.go create mode 100644 crypto/ed29919csp.go create mode 100644 crypto/ed29919csp_test.go create mode 100644 crypto/opts.go create mode 100644 crypto/rsacsp.go create mode 100644 node/config.go create mode 100644 node/config.yaml diff --git a/crypto/crypto.go b/crypto/crypto.go new file mode 100644 index 0000000..9045313 --- /dev/null +++ b/crypto/crypto.go @@ -0,0 +1,80 @@ +// The AGPLv3 License (AGPLv3) + +// Copyright (c) 2022 ZHAO Zhenhua + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package crypto + +import ( + gocrypto "crypto" + "hash" +) + +type Key interface { + //the key's raw byte + Bytes() ([]byte, error) + //PrivateKey returns true is this is a asymmetric private key or symmetric security key + PrivateKey() bool + //symmetric returns true if this key is symmetric, otherwise false + Symmetric() bool + //if this is a asymmetric key, returns the corresponding Public key, otherwise error + PublicKey() (Key, error) +} + +// Key generation options for BHPCSP +type KeyGenOpts interface { + Algorithem() string +} + +// HashOpts contains hash options for BHPCSP +type HashOpts interface { + Algorithem() string +} + +// EncrypterOpts contains encrypting options +type EncrypterOpts interface { +} + +// DecrypterOpts contains decrypting options +type DecrypterOpts interface { +} + +// SignerOpts contain signing options +type SignerOpts interface { + gocrypto.SignerOpts +} + +// bytehub+ crytograhic service provider +type BHPCSP interface { + //KeyGen generates a new key + KeyGen(opts KeyGenOpts) (Key, error) + + //GetKey returns the key + GetKey(keyInstance []byte) (Key, error) + + //Hash hashes a message + Hash(msg []byte, opts HashOpts) ([]byte, error) + + //GetHash returns the instance of hash function + GetHash(opt HashOpts) (hash.Hash, error) + + Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error) + Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error) + + //Sign signs a message's hash + Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error) + //Verify verifies a signature + Verify(k Key, signature, digest []byte, opts SignerOpts) (bool, error) +} diff --git a/crypto/ed29919csp.go b/crypto/ed29919csp.go new file mode 100644 index 0000000..ed28f21 --- /dev/null +++ b/crypto/ed29919csp.go @@ -0,0 +1,122 @@ +// The AGPLv3 License (AGPLv3) + +// Copyright (c) 2022 ZHAO Zhenhua + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package crypto + +import ( + "crypto/ed25519" + "crypto/rand" + "errors" + "hash" +) + +type Ed25519PrivateKey struct { + csp []byte + pub Ed25519PublicKey +} + +// KeyGen generates a new key +func (e *Ed25519PrivateKey) KeyGen(opts KeyGenOpts) (Key, error) { + return e.generateEd25519Key() +} + +func (k *Ed25519PrivateKey) generateEd25519Key() (Key, error) { + pub, ski, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + return nil, errors.New("failed to genenate ed25519 key") + } + key := &Ed25519PrivateKey{ski, Ed25519PublicKey{ski, &pub}} + return key, nil +} + +// GetKey returns the key +func (e *Ed25519PrivateKey) GetKey(keyInstance []byte) (Key, error) { + panic("not implemented") // TODO: Implement +} + +// Hash hashes a message +func (e *Ed25519PrivateKey) Hash(msg []byte, opts HashOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// GetHash returns the instance of hash function +func (e *Ed25519PrivateKey) GetHash(opt HashOpts) (hash.Hash, error) { + panic("not implemented") // TODO: Implement +} + +func (e *Ed25519PrivateKey) Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +func (e *Ed25519PrivateKey) Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// Sign signs a message's hash +func (e *Ed25519PrivateKey) Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// Verify verifies a signature +func (e *Ed25519PrivateKey) Verify(k Key, signature []byte, digest []byte, opts SignerOpts) (bool, error) { + panic("not implemented") // TODO: Implement +} + +// the key's raw byte +func (e *Ed25519PrivateKey) Bytes() ([]byte, error) { + return e.csp, nil +} + +// PrivateKey returns true is this is a asymmetric private key or symmetric security key +func (e *Ed25519PrivateKey) PrivateKey() bool { + return true +} + +// symmetric returns true if this key is symmetric, otherwise false +func (e *Ed25519PrivateKey) Symmetric() bool { + return false +} + +// if this is a asymmetric key, returns the corresponding Public key, otherwise false +func (e *Ed25519PrivateKey) PublicKey() (Key, error) { + return &e.pub, nil +} + +type Ed25519PublicKey struct { + csi []byte + pub *ed25519.PublicKey +} + +// the key's raw byte +func (e *Ed25519PublicKey) Bytes() ([]byte, error) { + return e.csi, nil +} + +// PrivateKey returns true is this is a asymmetric private key or symmetric security key +func (e *Ed25519PublicKey) PrivateKey() bool { + return false +} + +// symmetric returns true if this key is symmetric, otherwise false +func (e *Ed25519PublicKey) Symmetric() bool { + return false +} + +// if this is a asymmetric key, returns the corresponding Public key, otherwise false +func (e *Ed25519PublicKey) PublicKey() (Key, error) { + return e, nil +} diff --git a/crypto/ed29919csp_test.go b/crypto/ed29919csp_test.go new file mode 100644 index 0000000..5871506 --- /dev/null +++ b/crypto/ed29919csp_test.go @@ -0,0 +1 @@ +package crypto diff --git a/crypto/opts.go b/crypto/opts.go new file mode 100644 index 0000000..b11b5f3 --- /dev/null +++ b/crypto/opts.go @@ -0,0 +1,24 @@ +// The AGPLv3 License (AGPLv3) + +// Copyright (c) 2022 ZHAO Zhenhua + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package crypto + +const ( + ED25519 = "ED25519" + SHA2 = "SHA2" + SHA3 = "SHA3" +) diff --git a/crypto/rsacsp.go b/crypto/rsacsp.go new file mode 100644 index 0000000..f423a22 --- /dev/null +++ b/crypto/rsacsp.go @@ -0,0 +1,72 @@ +package crypto + +import "hash" + +type RSAPrivateKey struct { + csi []byte + // pub RSAPublicKey + +} + +// KeyGen generates a new key +func (r *RSAPrivateKey) KeyGen(opts KeyGenOpts) (Key, error) { + panic("not implemented") // TODO: Implement +} + +// GetKey returns the key +func (r *RSAPrivateKey) GetKey(keyInstance []byte) (Key, error) { + panic("not implemented") // TODO: Implement +} + +// Hash hashes a message +func (r *RSAPrivateKey) Hash(msg []byte, opts HashOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// GetHash returns the instance of hash function +func (r *RSAPrivateKey) GetHash(opt HashOpts) (hash.Hash, error) { + panic("not implemented") // TODO: Implement +} + +func (r *RSAPrivateKey) Encrypt(k Key, plaintext []byte, opts EncrypterOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +func (r *RSAPrivateKey) Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// Sign signs a message's hash +func (r *RSAPrivateKey) Sign(k Key, digest []byte, opts SignerOpts) ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// Verify verifies a signature +func (r *RSAPrivateKey) Verify(k Key, signature []byte, digest []byte, opts SignerOpts) (bool, error) { + panic("not implemented") // TODO: Implement +} + +// the key's raw byte +func (r *RSAPrivateKey) Bytes() ([]byte, error) { + panic("not implemented") // TODO: Implement +} + +// PrivateKey returns true is this is a asymmetric private key or symmetric security key +func (r *RSAPrivateKey) PrivateKey() bool { + panic("not implemented") // TODO: Implement +} + +// symmetric returns true if this key is symmetric, otherwise false +func (r *RSAPrivateKey) Symmetric() bool { + panic("not implemented") // TODO: Implement +} + +// if this is a asymmetric key, returns the corresponding Public key, otherwise error +func (r *RSAPrivateKey) PublicKey() (Key, error) { + panic("not implemented") // TODO: Implement +} + +// type RSAPublicKey struct { +// csi []byte +// pub xrsa.XRsa. +// } diff --git a/node/config.go b/node/config.go new file mode 100644 index 0000000..d43ab39 --- /dev/null +++ b/node/config.go @@ -0,0 +1,24 @@ +// The AGPLv3 License (AGPLv3) + +// Copyright (c) 2022 ZHAO Zhenhua + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +// this file define/implement node configuration + +package node + +type NodeConfig struct { + SignKey []byte +} diff --git a/node/config.yaml b/node/config.yaml new file mode 100644 index 0000000..3c25f87 --- /dev/null +++ b/node/config.yaml @@ -0,0 +1,12 @@ +nodes: + node1: 192.168.0.2:3000 + node2: 192.168.0.3:3000 + node3: 192.168.0.4:3000 + node4: 192.168.0.5:3000 + node5: 192.168.0.6:3000 + node6: 192.168.0.7:3000 +vaultIndex: + path: ./data +vaultPath: /data/vault/ +TLS: + tls: tls.key diff --git a/node/node.go b/node/node.go index 06c6558..ed562e9 100644 --- a/node/node.go +++ b/node/node.go @@ -1,4 +1,4 @@ -package main +package node import ( "crypto"