Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
Classes | Functions | Variables
zeth.core.encryption Namespace Reference

Classes

class  EncryptionKeyPair
 

Functions

EncryptionSecretKey generate_encryption_secret_key ()
 
bytes encode_encryption_secret_key (EncryptionSecretKey sk)
 
EncryptionSecretKey decode_encryption_secret_key (bytes sk_bytes)
 
str encryption_secret_key_as_hex (EncryptionSecretKey sk)
 
EncryptionSecretKey encryption_secret_key_from_hex (str pk_str)
 
EncryptionPublicKey get_encryption_public_key (EncryptionSecretKey enc_secret)
 
bytes encode_encryption_public_key (EncryptionPublicKey pk)
 
EncryptionPublicKey decode_encryption_public_key (bytes pk_data)
 
str encryption_public_key_as_hex (EncryptionPublicKey pk)
 
EncryptionPublicKey encryption_public_key_from_hex (str pk_str)
 
EncryptionKeyPair generate_encryption_keypair ()
 
bytes encrypt (bytes message, EncryptionPublicKey pk_receiver)
 
bytes decrypt (bytes encrypted_message, EncryptionSecretKey sk_receiver)
 

Variables

 int
 
 bytes
 
 InvalidSignature = cryptography_InvalidSignature
 
 EncryptionSecretKey = NewType('EncryptionSecretKey', object)
 
 EncryptionPublicKey = NewType('EncryptionPublicKey', object)
 

Function Documentation

◆ decode_encryption_public_key()

EncryptionPublicKey zeth.core.encryption.decode_encryption_public_key ( bytes  pk_data)

Definition at line 142 of file encryption.py.

142 def decode_encryption_public_key(pk_data: bytes) -> EncryptionPublicKey:
143  return EncryptionPublicKey(X25519PublicKey.from_public_bytes(pk_data))
144 
145 
Here is the caller graph for this function:

◆ decode_encryption_secret_key()

EncryptionSecretKey zeth.core.encryption.decode_encryption_secret_key ( bytes  sk_bytes)

Definition at line 115 of file encryption.py.

115 def decode_encryption_secret_key(sk_bytes: bytes) -> EncryptionSecretKey:
116  return EncryptionSecretKey(
117  X25519PrivateKey.from_private_bytes(sk_bytes))
118 
119 
Here is the caller graph for this function:

◆ decrypt()

bytes zeth.core.encryption.decrypt ( bytes  encrypted_message,
EncryptionSecretKey  sk_receiver 
)
Decrypts a NOTE_LENGTH-byte message by using valid ec25519 private key
objects.  See: https://pynacl.readthedocs.io/en/stable/public/

Definition at line 203 of file encryption.py.

203 def decrypt(
204  encrypted_message: bytes,
205  sk_receiver: EncryptionSecretKey) -> bytes:
206  """
207  Decrypts a NOTE_LENGTH-byte message by using valid ec25519 private key
208  objects. See: https://pynacl.readthedocs.io/en/stable/public/
209  """
210  assert \
211  len(encrypted_message) == ENCRYPTED_NOTE_LENGTH_BYTES, \
212  "encrypted_message byte-length must be: "+str(ENCRYPTED_NOTE_LENGTH_BYTES)
213 
214  assert(isinstance(sk_receiver, X25519PrivateKey)), \
215  f"PrivateKey: {sk_receiver} ({type(sk_receiver)})"
216 
217  # Compute shared secret
218  pk_sender_bytes = encrypted_message[:EC_PUBLIC_KEY_LENGTH_BYTES]
219  pk_sender = decode_encryption_public_key(pk_sender_bytes)
220  shared_key = _exchange(sk_receiver, pk_sender)
221 
222  # Generate key material and recover keys
223  sym_key, mac_key = _kdf(pk_sender_bytes, shared_key)
224 
225  # ct_sym and mac
226  ct_sym = encrypted_message[
227  EC_PUBLIC_KEY_LENGTH_BYTES:
228  EC_PUBLIC_KEY_LENGTH_BYTES + NOTE_LENGTH_BYTES]
229  tag = encrypted_message[
230  EC_PUBLIC_KEY_LENGTH_BYTES + NOTE_LENGTH_BYTES:
231  EC_PUBLIC_KEY_LENGTH_BYTES + NOTE_LENGTH_BYTES + _TAG_LENGTH_BYTES]
232 
233  # Verify the mac
234  mac = poly1305.Poly1305(mac_key)
235  mac.update(ct_sym)
236  mac.verify(tag)
237 
238  # Decrypt sym ciphertext
239  algorithm = algorithms.ChaCha20(sym_key, _SYM_NONCE_VALUE)
240  cipher = Cipher(algorithm, mode=None, backend=default_backend())
241  decryptor = cipher.decryptor()
242  message = decryptor.update(ct_sym)
243 
244  return message
245 
246 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_encryption_public_key()

bytes zeth.core.encryption.encode_encryption_public_key ( EncryptionPublicKey  pk)

Definition at line 138 of file encryption.py.

138 def encode_encryption_public_key(pk: EncryptionPublicKey) -> bytes:
139  return pk.public_bytes(Encoding.Raw, PublicFormat.Raw) # type: ignore
140 
141 
Here is the caller graph for this function:

◆ encode_encryption_secret_key()

bytes zeth.core.encryption.encode_encryption_secret_key ( EncryptionSecretKey  sk)

Definition at line 110 of file encryption.py.

110 def encode_encryption_secret_key(sk: EncryptionSecretKey) -> bytes:
111  return sk.private_bytes( # type: ignore
112  Encoding.Raw, PrivateFormat.Raw, NoEncryption())
113 
114 
Here is the caller graph for this function:

◆ encrypt()

bytes zeth.core.encryption.encrypt ( bytes  message,
EncryptionPublicKey  pk_receiver 
)
Encrypts a string message under a ec25519 public key by using a custom
dhaes-based scheme.  See: https://eprint.iacr.org/1999/007

Definition at line 168 of file encryption.py.

168 def encrypt(message: bytes, pk_receiver: EncryptionPublicKey) -> bytes:
169  """
170  Encrypts a string message under a ec25519 public key by using a custom
171  dhaes-based scheme. See: https://eprint.iacr.org/1999/007
172  """
173  assert \
174  len(message) == NOTE_LENGTH_BYTES, \
175  f"expected message length {NOTE_LENGTH_BYTES}, saw {len(message)}"
176 
177  # Generate ephemeral keypair
178  eph_keypair = generate_encryption_keypair()
179 
180  # Compute shared secret and eph key
181  shared_key = _exchange(eph_keypair.k_sk, pk_receiver)
182  pk_sender_bytes = encode_encryption_public_key(eph_keypair.k_pk)
183 
184  # Generate key material
185  sym_key, mac_key = _kdf(pk_sender_bytes, shared_key)
186 
187  # Generate symmetric ciphertext
188  # Chacha encryption
189  algorithm = algorithms.ChaCha20(sym_key, _SYM_NONCE_VALUE)
190  cipher = Cipher(algorithm, mode=None, backend=default_backend())
191  encryptor = cipher.encryptor()
192  sym_ciphertext = encryptor.update(message)
193 
194  # Generate mac
195  mac = poly1305.Poly1305(mac_key)
196  mac.update(sym_ciphertext)
197  tag = mac.finalize()
198 
199  # Arrange ciphertext
200  return pk_sender_bytes+sym_ciphertext+tag
201 
202 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encryption_public_key_as_hex()

str zeth.core.encryption.encryption_public_key_as_hex ( EncryptionPublicKey  pk)

Definition at line 146 of file encryption.py.

146 def encryption_public_key_as_hex(pk: EncryptionPublicKey) -> str:
147  return encode_encryption_public_key(pk).hex()
148 
149 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encryption_public_key_from_hex()

EncryptionPublicKey zeth.core.encryption.encryption_public_key_from_hex ( str  pk_str)

Definition at line 150 of file encryption.py.

150 def encryption_public_key_from_hex(pk_str: str) -> EncryptionPublicKey:
151  return decode_encryption_public_key(bytes.fromhex(pk_str))
152 
153 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encryption_secret_key_as_hex()

str zeth.core.encryption.encryption_secret_key_as_hex ( EncryptionSecretKey  sk)

Definition at line 120 of file encryption.py.

120 def encryption_secret_key_as_hex(sk: EncryptionSecretKey) -> str:
121  return encode_encryption_secret_key(sk).hex() # type: ignore
122 
123 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encryption_secret_key_from_hex()

EncryptionSecretKey zeth.core.encryption.encryption_secret_key_from_hex ( str  pk_str)

Definition at line 124 of file encryption.py.

124 def encryption_secret_key_from_hex(pk_str: str) -> EncryptionSecretKey:
125  return EncryptionSecretKey(
126  X25519PrivateKey.from_private_bytes(bytes.fromhex(pk_str)))
127 
128 
129 # Public key for decryption
Here is the caller graph for this function:

◆ generate_encryption_keypair()

EncryptionKeyPair zeth.core.encryption.generate_encryption_keypair ( )

Definition at line 163 of file encryption.py.

163 def generate_encryption_keypair() -> EncryptionKeyPair:
165  return EncryptionKeyPair(sk, get_encryption_public_key(sk))
166 
167 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate_encryption_secret_key()

EncryptionSecretKey zeth.core.encryption.generate_encryption_secret_key ( )

Definition at line 106 of file encryption.py.

106 def generate_encryption_secret_key() -> EncryptionSecretKey:
107  return EncryptionSecretKey(X25519PrivateKey.generate()) # type: ignore
108 
109 
Here is the caller graph for this function:

◆ get_encryption_public_key()

EncryptionPublicKey zeth.core.encryption.get_encryption_public_key ( EncryptionSecretKey  enc_secret)

Definition at line 133 of file encryption.py.

134  enc_secret: EncryptionSecretKey) -> EncryptionPublicKey:
135  return enc_secret.public_key() # type: ignore
136 
137 
Here is the caller graph for this function:

Variable Documentation

◆ bytes

zeth.core.encryption.bytes

Definition at line 87 of file encryption.py.

◆ EncryptionPublicKey

zeth.core.encryption.EncryptionPublicKey = NewType('EncryptionPublicKey', object)

Definition at line 130 of file encryption.py.

◆ EncryptionSecretKey

zeth.core.encryption.EncryptionSecretKey = NewType('EncryptionSecretKey', object)

Definition at line 103 of file encryption.py.

◆ int

zeth.core.encryption.int

Definition at line 72 of file encryption.py.

◆ InvalidSignature

zeth.core.encryption.InvalidSignature = cryptography_InvalidSignature

Definition at line 100 of file encryption.py.

zeth.core.encryption.encode_encryption_public_key
bytes encode_encryption_public_key(EncryptionPublicKey pk)
Definition: encryption.py:138
test_commands.mock.str
str
Definition: mock.py:18
zeth.core.encryption.decode_encryption_secret_key
EncryptionSecretKey decode_encryption_secret_key(bytes sk_bytes)
Definition: encryption.py:115
zeth.core.encryption.encryption_secret_key_from_hex
EncryptionSecretKey encryption_secret_key_from_hex(str pk_str)
Definition: encryption.py:124
zeth.core.encryption.encrypt
bytes encrypt(bytes message, EncryptionPublicKey pk_receiver)
Definition: encryption.py:168
zeth.core.encryption.generate_encryption_secret_key
EncryptionSecretKey generate_encryption_secret_key()
Definition: encryption.py:106
zeth.core.encryption.encode_encryption_secret_key
bytes encode_encryption_secret_key(EncryptionSecretKey sk)
Definition: encryption.py:110
zeth.core.encryption.encryption_public_key_as_hex
str encryption_public_key_as_hex(EncryptionPublicKey pk)
Definition: encryption.py:146
zeth.core.encryption.EncryptionPublicKey
EncryptionPublicKey
Definition: encryption.py:130
zeth.core.encryption.EncryptionSecretKey
EncryptionSecretKey
Definition: encryption.py:103
zeth.core.encryption.decode_encryption_public_key
EncryptionPublicKey decode_encryption_public_key(bytes pk_data)
Definition: encryption.py:142
zeth.core.encryption.get_encryption_public_key
EncryptionPublicKey get_encryption_public_key(EncryptionSecretKey enc_secret)
Definition: encryption.py:133
zeth.core.encryption.decrypt
bytes decrypt(bytes encrypted_message, EncryptionSecretKey sk_receiver)
Definition: encryption.py:203
zeth.core.encryption.generate_encryption_keypair
EncryptionKeyPair generate_encryption_keypair()
Definition: encryption.py:163
zeth.core.encryption.encryption_secret_key_as_hex
str encryption_secret_key_as_hex(EncryptionSecretKey sk)
Definition: encryption.py:120
zeth.core.encryption.encryption_public_key_from_hex
EncryptionPublicKey encryption_public_key_from_hex(str pk_str)
Definition: encryption.py:150