Exemple #1
0
    sys.exit('Error: Unable to import public key, aborting.')

if clargs.file is None:
    message = sys.stdin.read().encode()
else:
    with open(clargs.file, 'r') as msgfile:
        message = msgfile.read().encode()

if (message is None) or (len(message) == 0):
    sys.exit('Error: Plaintext length 0, aborting.')

# generate a random (ephemeral) private key
eprivkey = FieldElement.urandom(curve.p)
SharedPt = Pubkey * eprivkey
sbytes = SharedPt.compressed()
key = sha256(sbytes).digest()

nonce = pysodium.randombytes(pysodium.crypto_stream_NONCEBYTES)
assert pysodium.crypto_stream_NONCEBYTES == 24
assert pysodium.crypto_stream_KEYBYTES == 32

ctext = pysodium.crypto_stream_xor(message, len(message), nonce, key)

# public key point for ephemeral key
Gpt = ECPoint(curve, curve.G)
ePubkey = Gpt * eprivkey

DERmsg = der_encode_message(ePubkey, nonce, ctext)

print(pem_wrap(DERmsg, 'ECDHE_XSALSA20 ENCRYPTED MESSAGE'))
Exemple #2
0
parser = ArgumentParser(description=desc)
parser.add_argument('-f',
                    '--file',
                    default=None,
                    help='read private key from file instead of stdin')
clargs = parser.parse_args()

if clargs.file is None:
    Inkey = sys.stdin.read()
else:
    with open(clargs.file, 'r') as keyfile:
        Inkey = keyfile.read()

DERkey = pem_unwrap(Inkey, 'ECDH PRIVATE KEY')

try:
    (privkey, curve) = der_decode_privkey(DERkey)
except ValueError:
    sys.exit('Error: Unable to import private key, aborting.')

if privkey is None:
    sys.exit('Error: Unable to import private key, aborting.')

Gpt = ECPoint(curve, curve.G)
Pubkey = Gpt * privkey

DERkey = der_encode_pubkey(Pubkey, curve)

print(pem_wrap(DERkey, 'ECDH PUBLIC KEY'))
Exemple #3
0
with open(clargs.privkey, 'r') as keyfile:
    Inkey = keyfile.read()

DERkey = pem_unwrap(Inkey, 'ECDH PRIVATE KEY')
if DERkey == None:
    sys.exit('unable to decode ECDH PRIVATE KEY in base64 PEM format')

try:
    (Privkey, curve) = der_decode_privkey(DERkey)
except ValueError:
    sys.exit('Error: Unable to import private key, aborting.')

if Privkey is None:
    sys.exit('Error: Unable to import public key, aborting.')

if clargs.file is None:
    message = sys.stdin.read().encode()
else:
    with open(clargs.file, 'r') as msgfile:
        message = msgfile.read().encode()

if (message is None) or (len(message) == 0):
    sys.exit('Error: Plaintext length 0, aborting.')

ss = ECDSASignatureScheme(curve, SHA512)
sig = ss.Sign(Privkey, message)
sigbytes = sig.AsBytes()

print(pem_wrap(message, 'ECDSA SIGNED MESSAGE'))
print(pem_wrap(sigbytes, 'ECDSA SIGNATURE'))
    sys.exit('Error: Unable to import public key, aborting.')

if clargs.file is None:
    ptxt_wrapped = sys.stdin.read()
else:
    with open(clargs.file, 'r') as msgfile:
        ptxt_wrapped=msgfile.read()

if (ptxt_wrapped is None) or (len(ptxt_wrapped) == 0):
    sys.exit('Error: Plaintext length 0, aborting.')

DERptxt = pem_unwrap(ptxt_wrapped, 'ECDH PUBLIC KEY')
if DERptxt == None:
    sys.exit('unable to decode plaintext (ECDH PUBLIC KEY) in base64 PEM format')

try:
    (Plaintext, curve2) = der_decode_pubkey(DERptxt)
except ValueError:
    sys.exit('Error: Unable to import private key, aborting.')

if Plaintext is None:
    sys.exit('Error: Unable to import public key, aborting.')

if curve != curve2:
    sys.exit('Error: key and plaintext must be on the same curve, aborting.')

ctext = ECElgamalCiphertext.encrypt(Pubkey, Plaintext)
DERmsg = der_encode_ecelgamal_ctxt(ctext, curve)

print(pem_wrap(DERmsg, 'ECELGAMAL ENCRYPTED MESSAGE'))
Exemple #5
0
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from ECC import FieldElement, ECurve
from example_der import der_encode_privkey
from example_pem import pem_wrap
from argparse import ArgumentParser
import base64
import sys

desc = ('ecdh-gen generates a private key for elliptic curve cryptography '
        'based on a specific curve. The private key value is simply a '
        'random number ')

parser = ArgumentParser(description=desc)
parser.add_argument('-l', '--list_curves', action='store_true', help='list supported curves')
parser.add_argument('-c', '--curve', default='secp256k1', help='choose standard curve')
clargs = parser.parse_args()

if clargs.list_curves:
    clist = ECurve._CurveNames()
    for c in clist:
        print(c)
    sys.exit(0)

curve = ECurve(clargs.curve)
privkey = int(FieldElement.urandom(curve.p))

DERkey = der_encode_privkey(privkey, curve)

print(pem_wrap(DERkey, 'ECDH PRIVATE KEY'))