Voice et bot modif
This commit is contained in:
parent
189d56026b
commit
7333a22bcd
10774 changed files with 634644 additions and 933308 deletions
|
|
@ -29,6 +29,7 @@ crypto_box_ZEROBYTES: int = lib.crypto_box_zerobytes()
|
|||
crypto_box_BOXZEROBYTES: int = lib.crypto_box_boxzerobytes()
|
||||
crypto_box_BEFORENMBYTES: int = lib.crypto_box_beforenmbytes()
|
||||
crypto_box_SEALBYTES: int = lib.crypto_box_sealbytes()
|
||||
crypto_box_MACBYTES: int = lib.crypto_box_macbytes()
|
||||
|
||||
|
||||
def crypto_box_keypair() -> Tuple[bytes, bytes]:
|
||||
|
|
@ -51,7 +52,7 @@ def crypto_box_keypair() -> Tuple[bytes, bytes]:
|
|||
|
||||
def crypto_box_seed_keypair(seed: bytes) -> Tuple[bytes, bytes]:
|
||||
"""
|
||||
Returns a (public, secret) keypair deterministically generated
|
||||
Returns a (public, secret) key pair deterministically generated
|
||||
from an input ``seed``.
|
||||
|
||||
.. warning:: The seed **must** be high-entropy; therefore,
|
||||
|
|
@ -227,6 +228,156 @@ def crypto_box_open_afternm(
|
|||
return ffi.buffer(plaintext, len(padded))[crypto_box_ZEROBYTES:]
|
||||
|
||||
|
||||
def crypto_box_easy(
|
||||
message: bytes, nonce: bytes, pk: bytes, sk: bytes
|
||||
) -> bytes:
|
||||
"""
|
||||
Encrypts and returns a message ``message`` using the secret key ``sk``,
|
||||
public key ``pk``, and the nonce ``nonce``.
|
||||
|
||||
:param message: bytes
|
||||
:param nonce: bytes
|
||||
:param pk: bytes
|
||||
:param sk: bytes
|
||||
:rtype: bytes
|
||||
"""
|
||||
if len(nonce) != crypto_box_NONCEBYTES:
|
||||
raise exc.ValueError("Invalid nonce size")
|
||||
|
||||
if len(pk) != crypto_box_PUBLICKEYBYTES:
|
||||
raise exc.ValueError("Invalid public key")
|
||||
|
||||
if len(sk) != crypto_box_SECRETKEYBYTES:
|
||||
raise exc.ValueError("Invalid secret key")
|
||||
|
||||
_mlen = len(message)
|
||||
_clen = crypto_box_MACBYTES + _mlen
|
||||
|
||||
ciphertext = ffi.new("unsigned char[]", _clen)
|
||||
|
||||
rc = lib.crypto_box_easy(ciphertext, message, _mlen, nonce, pk, sk)
|
||||
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
|
||||
|
||||
return ffi.buffer(ciphertext, _clen)[:]
|
||||
|
||||
|
||||
def crypto_box_open_easy(
|
||||
ciphertext: bytes, nonce: bytes, pk: bytes, sk: bytes
|
||||
) -> bytes:
|
||||
"""
|
||||
Decrypts and returns an encrypted message ``ciphertext``, using the secret
|
||||
key ``sk``, public key ``pk``, and the nonce ``nonce``.
|
||||
|
||||
:param ciphertext: bytes
|
||||
:param nonce: bytes
|
||||
:param pk: bytes
|
||||
:param sk: bytes
|
||||
:rtype: bytes
|
||||
"""
|
||||
if len(nonce) != crypto_box_NONCEBYTES:
|
||||
raise exc.ValueError("Invalid nonce size")
|
||||
|
||||
if len(pk) != crypto_box_PUBLICKEYBYTES:
|
||||
raise exc.ValueError("Invalid public key")
|
||||
|
||||
if len(sk) != crypto_box_SECRETKEYBYTES:
|
||||
raise exc.ValueError("Invalid secret key")
|
||||
|
||||
_clen = len(ciphertext)
|
||||
|
||||
ensure(
|
||||
_clen >= crypto_box_MACBYTES,
|
||||
"Input ciphertext must be at least {} long".format(
|
||||
crypto_box_MACBYTES
|
||||
),
|
||||
raising=exc.TypeError,
|
||||
)
|
||||
|
||||
_mlen = _clen - crypto_box_MACBYTES
|
||||
|
||||
plaintext = ffi.new("unsigned char[]", max(1, _mlen))
|
||||
|
||||
res = lib.crypto_box_open_easy(plaintext, ciphertext, _clen, nonce, pk, sk)
|
||||
ensure(
|
||||
res == 0,
|
||||
"An error occurred trying to decrypt the message",
|
||||
raising=exc.CryptoError,
|
||||
)
|
||||
|
||||
return ffi.buffer(plaintext, _mlen)[:]
|
||||
|
||||
|
||||
def crypto_box_easy_afternm(message: bytes, nonce: bytes, k: bytes) -> bytes:
|
||||
"""
|
||||
Encrypts and returns the message ``message`` using the shared key ``k`` and
|
||||
the nonce ``nonce``.
|
||||
|
||||
:param message: bytes
|
||||
:param nonce: bytes
|
||||
:param k: bytes
|
||||
:rtype: bytes
|
||||
"""
|
||||
if len(nonce) != crypto_box_NONCEBYTES:
|
||||
raise exc.ValueError("Invalid nonce")
|
||||
|
||||
if len(k) != crypto_box_BEFORENMBYTES:
|
||||
raise exc.ValueError("Invalid shared key")
|
||||
|
||||
_mlen = len(message)
|
||||
_clen = crypto_box_MACBYTES + _mlen
|
||||
|
||||
ciphertext = ffi.new("unsigned char[]", _clen)
|
||||
|
||||
rc = lib.crypto_box_easy_afternm(ciphertext, message, _mlen, nonce, k)
|
||||
ensure(rc == 0, "Unexpected library error", raising=exc.RuntimeError)
|
||||
|
||||
return ffi.buffer(ciphertext, _clen)[:]
|
||||
|
||||
|
||||
def crypto_box_open_easy_afternm(
|
||||
ciphertext: bytes, nonce: bytes, k: bytes
|
||||
) -> bytes:
|
||||
"""
|
||||
Decrypts and returns the encrypted message ``ciphertext``, using the shared
|
||||
key ``k`` and the nonce ``nonce``.
|
||||
|
||||
:param ciphertext: bytes
|
||||
:param nonce: bytes
|
||||
:param k: bytes
|
||||
:rtype: bytes
|
||||
"""
|
||||
if len(nonce) != crypto_box_NONCEBYTES:
|
||||
raise exc.ValueError("Invalid nonce")
|
||||
|
||||
if len(k) != crypto_box_BEFORENMBYTES:
|
||||
raise exc.ValueError("Invalid shared key")
|
||||
|
||||
_clen = len(ciphertext)
|
||||
|
||||
ensure(
|
||||
_clen >= crypto_box_MACBYTES,
|
||||
"Input ciphertext must be at least {} long".format(
|
||||
crypto_box_MACBYTES
|
||||
),
|
||||
raising=exc.TypeError,
|
||||
)
|
||||
|
||||
_mlen = _clen - crypto_box_MACBYTES
|
||||
|
||||
plaintext = ffi.new("unsigned char[]", max(1, _mlen))
|
||||
|
||||
res = lib.crypto_box_open_easy_afternm(
|
||||
plaintext, ciphertext, _clen, nonce, k
|
||||
)
|
||||
ensure(
|
||||
res == 0,
|
||||
"An error occurred trying to decrypt the message",
|
||||
raising=exc.CryptoError,
|
||||
)
|
||||
|
||||
return ffi.buffer(plaintext, _mlen)[:]
|
||||
|
||||
|
||||
def crypto_box_seal(message: bytes, pk: bytes) -> bytes:
|
||||
"""
|
||||
Encrypts and returns a message ``message`` using an ephemeral secret key
|
||||
|
|
@ -269,7 +420,7 @@ def crypto_box_seal_open(ciphertext: bytes, pk: bytes, sk: bytes) -> bytes:
|
|||
"""
|
||||
Decrypts and returns an encrypted message ``ciphertext``, using the
|
||||
recipent's secret key ``sk`` and the sender's ephemeral public key
|
||||
embedded in the sealed box. The box contruct nonce is derived from
|
||||
embedded in the sealed box. The box construct nonce is derived from
|
||||
the recipient's public key ``pk`` and the sender's public key.
|
||||
|
||||
:param ciphertext: bytes
|
||||
|
|
@ -303,7 +454,7 @@ def crypto_box_seal_open(ciphertext: bytes, pk: bytes, sk: bytes) -> bytes:
|
|||
|
||||
ensure(
|
||||
_clen >= crypto_box_SEALBYTES,
|
||||
("Input cyphertext must be at least {} long").format(
|
||||
("Input ciphertext must be at least {} long").format(
|
||||
crypto_box_SEALBYTES
|
||||
),
|
||||
raising=exc.TypeError,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue