Debian Codegraph @

Doc/src/signature/eddsa.rst

raw
1 Edwards-curve Digital Signature Algorithm (EdDSA)
2 =================================================
3
4 EdDSA is a deterministic digital signature scheme
5 based on twisted Edwards elliptic curves (Ed25519 and Ed448).
6 It is specified in `RFC8032 <https://datatracker.ietf.org/doc/html/rfc8032>`_,
7 as two variants:
8
9 * *PureEdDSA*, where the message is signed directly.
10 * *HashEdDSA*, where the message is first hashed, and only the resulting digest is signed.
11 This should only be used by streaming applications because it avoids double passes
12 on messages, at the cost of reduced collision resistance.
13
14 This module supports signatures for both variants (*PureEdDSA* and *HashEdDSA*),
15 on the Ed25519 curve (with a 128-bit security level), and
16 on the Ed448 curve (with a 224-bit security level).
17
18 For *HashEdDSA*, the hash function must be SHA-512 in case of Ed25519,
19 and SHAKE256 for Ed448.
20
21 A sender can use a *private* key (loaded from a file) to sign a message::
22
23 from Crypto.PublicKey import ECC
24 from Signature import eddsa
25
26 message = b'I give my permission to order #4355'
27 key = ECC.import_key(open("private_ed25519.pem").read()))
28 signer = eddsa.new(key, 'rfc8032')
29 signature = signer.sign(message)
30
31 The receiver can use the matching *public* key to verify authenticity of the received message::
32
33 from Crypto.PublicKey import ECC
34 from Signature import eddsa
35
36 message = b'I give my permission to order #4355'
37 key = ECC.import_key(open("public_ed25519.pem").read()))
38 verifier = eddsa.new(key, 'rfc8032')
39 try:
40 verifier.verify(message, signature)
41 print("The message is authentic")
42 except ValueError:
43 print("The message is not authentic")
44
45 Alternatively the *HashedEdDSA* variant can be used to sign a message with Ed25519::
46
47 from Crypto.PublicKey import ECC
48 from Signature import eddsa
49 from Crypto.Hash import SHA512
50
51 message = b'I give my permission to order #4355'
52 prehashed_message = SHA512.new(message)
53 key = ECC.import_key(open("private_ed25519.pem").read()))
54 signer = eddsa.new(key, 'rfc8032')
55 signature = signer.sign(prehashed_message)
56
57 *HashedEdDSA* also exists for Ed448::
58
59 from Crypto.PublicKey import ECC
60 from Signature import eddsa
61 from Crypto.Hash import SHAKE256
62
63 message = b'I give my permission to order #4355'
64 prehashed_message = SHAKE256.new(message)
65 key = ECC.import_key(open("private_ed448.pem").read()))
66 signer = eddsa.new(key, 'rfc8032')
67 signature = signer.sign(prehashed_message)
68
69
70 .. automodule:: Crypto.Signature.eddsa
71 :members: