a bf(@svUddlmZddlmZddlmZmZddlm Z e Z e e d<edZe e d<eZe e d<eZe e d <eZe e d <eZe e d <eeefd d dZeeeefdddZeeedddZeeedddZeedddZeedddZeedddZeedd d!ZGd"d#d#Z e ed$d%d&d'Z!e eed(d)d*Z"e eee#d+d,d-Z$d$S).)Tuple) exceptions)ffilib)ensurecrypto_sign_BYTEScrypto_sign_SEEDBYTEScrypto_sign_PUBLICKEYBYTEScrypto_sign_SECRETKEYBYTEScrypto_sign_curve25519_BYTES crypto_sign_ed25519ph_STATEBYTESreturncCs`tdt}tdt}t||}t|dkdtjdt |tddt |tddfS)zu Returns a randomly generated public key and secret key. :rtype: (bytes(public_key), bytes(secret_key)) unsigned char[]rUnexpected library errorZraisingN) rnewr r rcrypto_sign_keypairrexc RuntimeErrorbuffer)pkskrcr@/tmp/pip-target-98j97qn4/lib/python/nacl/bindings/crypto_sign.pyr s   r)seedrcCsxt|tkrtdtdt}tdt}t |||}t |dkdtj dt |tddt |tddfS)z Computes and returns the public key and secret key using the seed ``seed``. :param seed: bytes :rtype: (bytes(public_key), bytes(secret_key)) z Invalid seedrrrrN) lenr r ValueErrorrrr r rcrypto_sign_seed_keypairrrr)rrrrrrrr 2s    r )messagerrcCs`tdt|t}td}t|||t||}t|dkdtjdt ||dddS)z Signs the message ``message`` using the secret key ``sk`` and returns the signed message. :param message: bytes :param sk: bytes :rtype: bytes runsigned long long *rrrN) rrrrr crypto_signrrrr)r!rsignedZ signed_lenrrrrr#Hs  r#)r$rrcCsVtdt|}td}t|||t||dkr>tdt||dddS)z Verifies the signature of the signed message ``signed`` using the public key ``pk`` and returns the unsigned message. :param signed: bytes :param pk: bytes :rtype: bytes rr"rSignature was forged or corruptN)rrrrcrypto_sign_openrBadSignatureErrorr)r$rr!Z message_lenrrrr&Zs   r&)public_key_bytesrcCsZt|tkrtdt}td|}t||}t |dkdtj dt ||ddS)a Converts a public Ed25519 key (encoded as bytes ``public_key_bytes``) to a public Curve25519 key as bytes. Raises a ValueError if ``public_key_bytes`` is not of length ``crypto_sign_PUBLICKEYBYTES`` :param public_key_bytes: bytes :rtype: bytes zInvalid curve public keyrrrrN) rr rrr rrr$crypto_sign_ed25519_pk_to_curve25519rrr)r(Zcurve_public_key_lenZcurve_public_keyrrrrr)os   r))secret_key_bytesrcCsZt|tkrtdt}td|}t||}t |dkdtj dt ||ddS)a Converts a secret Ed25519 key (encoded as bytes ``secret_key_bytes``) to a secret Curve25519 key as bytes. Raises a ValueError if ``secret_key_bytes``is not of length ``crypto_sign_SECRETKEYBYTES`` :param secret_key_bytes: bytes :rtype: bytes zInvalid curve secret keyrrrrN) rr rrr rrr$crypto_sign_ed25519_sk_to_curve25519rrr)r*Zcurve_secret_key_lenZcurve_secret_keyrrrrr+s   r+cCs"t|tkrtd|tdS)a  Extract the public Ed25519 key from a secret Ed25519 key (encoded as bytes ``secret_key_bytes``). Raises a ValueError if ``secret_key_bytes``is not of length ``crypto_sign_SECRETKEYBYTES`` :param secret_key_bytes: bytes :rtype: bytes Invalid secret keyNrr rrr r*rrrcrypto_sign_ed25519_sk_to_pks  r/cCs"t|tkrtd|dtS)z Extract the seed from a secret Ed25519 key (encoded as bytes ``secret_key_bytes``). Raises a ValueError if ``secret_key_bytes``is not of length ``crypto_sign_SECRETKEYBYTES`` :param secret_key_bytes: bytes :rtype: bytes r,Nr-r.rrrcrypto_sign_ed25519_sk_to_seeds  r0c@s$eZdZdZdgZddddZdS)crypto_sign_ed25519ph_statezO State object wrapping the sha-512 state used in ed25519ph computation stateNrcCs2tdt|_t|j}t|dkdtjddS)Nrrrr) rrr r2rZcrypto_sign_ed25519ph_initrrr)selfrrrr__init__s  z$crypto_sign_ed25519ph_state.__init__)__name__ __module__ __qualname____doc__ __slots__r4rrrrr1sr1N)edphpmsgrcCsXtt|tdtjdtt|tdtjdt|j|t |}t|dkdtj ddS)z Update the hash state wrapped in edph :param edph: the ed25519ph state being updated :type edph: crypto_sign_ed25519ph_state :param pmsg: the partial message :type pmsg: bytes :rtype: None /edph parameter must be a ed25519ph_state objectrz%pmsg parameter must be a bytes objectrrN) r isinstancer1r TypeErrorbytesrcrypto_sign_ed25519ph_updater2rr)r:r;rrrrr@s r@)r:rrcCstt|tdtjdtt|tdtjdtt|tkdttjdt dt }t |j|t j|}t|dkdtjdt |t ddS) aR Create a signature for the data hashed in edph using the secret key sk :param edph: the ed25519ph state for the data being signed :type edph: crypto_sign_ed25519ph_state :param sk: the ed25519 secret part of the signing key :type sk: bytes :return: ed25519ph signature :rtype: bytes r<rz+secret key parameter must be a bytes objectz secret key must be {} bytes longrrrN)rr=r1rr>r?rr formatrrrr"crypto_sign_ed25519ph_final_creater2ZNULLrr)r:r signaturerrrrrBs.   rB)r:rCrrcCstt|tdtjdtt|tdtjdtt|tkdttjdtt|tdtjdtt|t kdt tjdt |j ||}|dkrt dd S) a Verify a prehashed signature using the public key pk :param edph: the ed25519ph state for the data being verified :type edph: crypto_sign_ed25519ph_state :param signature: the signature being verified :type signature: bytes :param pk: the ed25519 public part of the signing key :type pk: bytes :return: True if the signature is valid :rtype: boolean :raises exc.BadSignatureError: if the signature is not valid r<rz*signature parameter must be a bytes objectzsignature must be {} bytes longz+public key parameter must be a bytes objectz public key must be {} bytes longrr%T)rr=r1rr>r?rrrAr r"crypto_sign_ed25519ph_final_verifyr2r')r:rCrrrrrrDs>   rD)%typingrZnaclrrZ nacl._sodiumrrZnacl.exceptionsrZcrypto_sign_bytesrint__annotations__Zcrypto_sign_secretkeybytesr Zcrypto_sign_publickeybytesr r Zcrypto_box_secretkeybytesr Z crypto_sign_ed25519ph_statebytesr r?rr r#r&r)r+r/r0r1r@rBboolrDrrrrs6    *