Hazmat EC Signatures (Ed25519)
[Hazmat Home][Home]
With Ed25519 we use a private key to sign data, and then the public key can prove it. We use Curve 25519 for the generation of the public key and for the signing process. Curve 25519 uses a Montgomery curve of the form of \(y^2 = x^3+a x^2+x \pmod p\). If we have a point \(P\), we then find a point \(nP\) - where \(n\) is the number of times we add \(P\)). Curve 25519 takes the form of \(y^2 = x^3+486662 x^2+x \pmod p\) and a base point at \(x=9\). This gives a base point (\(G\)), and where we find the point \(nG\) for a given \(n\) value, and where \(n\) is the private key value, and \(nG\) is the public key point. Normally in Curve 25519 we only focus on the x-point, and do not need the \((x,y)\) value.
|
Coding
The following is the code:
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey from cryptography import exceptions from cryptography.hazmat.primitives import serialization import binascii import sys data=b"Test" if (len(sys.argv)>1): data=str(sys.argv[1]).encode() private_key = Ed25519PrivateKey.generate() public_key = private_key.public_key() public_key = private_key.public_key() print ("Message: ",data.decode()) try: signature = private_key.sign(data) print ("Good Signature: ",binascii.b2a_hex(signature).decode()) public_key.verify(signature, data) except exceptions.InvalidSignature: print("A bad signature failed") else: print("Good signature verified") try: signature = private_key.sign(b"Bad data") print ("Bad Signature: ",binascii.b2a_hex(signature).decode()) public_key.verify(signature, data) except exceptions.InvalidSignature: print("A bad signature failed") else: print("Good signature verified") pem = private_key.private_bytes(encoding=serialization.Encoding.PEM,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption()) der = private_key.private_bytes(encoding=serialization.Encoding.DER,format=serialization.PrivateFormat.PKCS8,encryption_algorithm=serialization.NoEncryption()) print ("\nPrivate key (PEM):\n",pem.decode()) print ("Private key (DER):\n",binascii.b2a_hex(der)) pem = public_key.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo) der = public_key.public_bytes(encoding=serialization.Encoding.DER,format=serialization.PublicFormat.SubjectPublicKeyInfo) print ("\nPublic key (PEM):\n",pem.decode()) print ("Public key (DER):\n",binascii.b2a_hex(der))
The following is a sample run:
Message: Test Good Signature: 919fadd1ef6596e13f95a0f333ec8b904f69e3a0492f84dd7d810bd2c43dab9096cdc87f992366e3c6887e831b5876ee4d1f37e943bd0bacf3cc1009a2008609 Good signature verified Bad Signature: 0bd83f3fc3544637612763dbf62cccc7569cd4fbb227d055b8ac0a29b8a3ca97058a169598f74c0ad7df6f7ecb1627e71b357d9b43a3db1649daa8f8f3f2790c A bad signature failed Private key (PEM): -----BEGIN PRIVATE KEY----- MC4CAQAwBQYDK2VwBCIEICU0Z+Uvma7PKzCtQtU1SVKEtNAy9zSw/PbX8c7di795 -----END PRIVATE KEY----- Private key (DER): b'302e020100300506032b657004220420253467e52f99aecf2b30ad42d535495284b4d032f734b0fcf6d7f1cedd8bbf79' Public key (PEM): -----BEGIN PUBLIC KEY----- MCowBQYDK2VwAyEAFCKJFCIwhgkngFF3Mm1TGXcFB8flAGDNvG54bsnaMF0= -----END PUBLIC KEY----- Public key (DER): b'302a300506032b6570032100142289142230860927805177326d5319770507c7e50060cdbc6e786ec9da305d'