3 L]%@sddlmZmZmZddlmZddlmZm Z ddl m Z e j Z e jdZe jZe jZe jZe jZddZdd Zd d Zd d ZddZddZGdddeZddZddZ ddZ!dS))absolute_importdivisionprint_function) exceptions)ffilib)ensurecCs`tjdt}tjdt}tj||}t|dkdtjdtj |tddtj |tddfS)zu Returns a randomly generated public key and secret key. :rtype: (bytes(public_key), bytes(secret_key)) zunsigned char[]rzUnexpected library error)raisingN) rnewcrypto_sign_PUBLICKEYBYTEScrypto_sign_SECRETKEYBYTESrcrypto_sign_keypairrexc RuntimeErrorbuffer)pkskrcr=/tmp/pip-install-wfra5znf/pynacl/nacl/bindings/crypto_sign.pyr!s    rcCsxt|tkrtjdtjdt}tjdt}tj |||}t |dkdtj dtj |tddtj |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 seedzunsigned char[]rzUnexpected library error)r N) lencrypto_sign_SEEDBYTESr ValueErrorrr r r rcrypto_sign_seed_keypairrrr)seedrrrrrrr5s     rcCs`tjdt|t}tjd}tj|||t||}t|dkdtjdtj ||dddS)z Signs the message ``message`` using the secret key ``sk`` and returns the signed message. :param message: bytes :param sk: bytes :rtype: bytes zunsigned char[]zunsigned long long *rzUnexpected library error)r N) rr rcrypto_sign_BYTESr crypto_signrrrr)messagersignedZ signed_lenrrrrrMs   rcCsVtjdt|}tjd}tj|||t||dkr>tjdtj||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 zunsigned char[]zunsigned long long *rzSignature was forged or corruptN)rr rrcrypto_sign_openrBadSignatureErrorr)rrrZ message_lenrrrr as   r cCsZt|tkrtjdt}tjd|}tj||}t |dkdtj dtj ||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 keyzunsigned char[]rzUnexpected library error)r N) rr rrcrypto_sign_curve25519_BYTESrr r$crypto_sign_ed25519_pk_to_curve25519rrr)Zpublic_key_bytesZcurve_public_key_lenZcurve_public_keyrrrrr#ts    r#cCsZt|tkrtjdt}tjd|}tj||}t |dkdtj dtj ||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 public_key_bytes: bytes :rtype: bytes zInvalid curve public keyzunsigned char[]rzUnexpected library error)r N) rr rrr"rr r$crypto_sign_ed25519_sk_to_curve25519rrr)Zsecret_key_bytesZcurve_secret_key_lenZcurve_secret_keyrrrrr$s    r$c@seZdZdZdgZddZdS)crypto_sign_ed25519ph_statezO State object wrapping the sha-512 state used in ed25519ph computation statecCs2tjdt|_tj|j}t|dkdtjddS)Nzunsigned char[]rzUnexpected library error)r ) rr crypto_sign_ed25519ph_STATEBYTESr&rZcrypto_sign_ed25519ph_initrrr)selfrrrr__init__s  z$crypto_sign_ed25519ph_state.__init__N)__name__ __module__ __qualname____doc__ __slots__r)rrrrr%sr%cCsXtt|tdtjdtt|tdtjdtj|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 z/edph parameter must be a ed25519ph_state object)r z%pmsg parameter must be a bytes objectrzUnexpected library errorN) r isinstancer%r TypeErrorbytesrcrypto_sign_ed25519ph_updater&rr)edphZpmsgrrrrr2s     r2cCstt|tdtjdtt|tdtjdtt|tkdjttjdt j dt }t j |j|t j|}t|dkdtjdt j|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 z/edph parameter must be a ed25519ph_state object)r z+secret key parameter must be a bytes objectz!secret key must be {0} bytes longzunsigned char[]rzUnexpected library errorN)rr/r%rr0r1rr formatrr rr"crypto_sign_ed25519ph_final_creater&ZNULLrr)r3r signaturerrrrr5s&        r5cCstt|tdtjdtt|tdtjdtt|tkdjttjdtt|tdtjdtt|t kdjt tjdt j |j ||}|dkrtj 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 z/edph parameter must be a ed25519ph_state object)r z*signature parameter must be a bytes objectz signature must be {0} bytes longz+public key parameter must be a bytes objectz!public key must be {0} bytes longrzSignature was forged or corruptT)rr/r%rr0r1rrr4r r"crypto_sign_ed25519ph_final_verifyr&r!)r3r6rrrrrr7s.           r7N)" __future__rrrZnaclrrZ nacl._sodiumrrZnacl.exceptionsrZcrypto_sign_bytesrZcrypto_sign_secretkeybytesrZcrypto_sign_publickeybytesr r Zcrypto_box_secretkeybytesr"Z crypto_sign_ed25519ph_statebytesr'rrrr r#r$objectr%r2r5r7rrrrs&   $