public.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import, division, print_function
  15. import nacl.bindings
  16. from nacl import encoding
  17. from nacl import exceptions as exc
  18. from nacl.utils import EncryptedMessage, StringFixer, random
  19. class PublicKey(encoding.Encodable, StringFixer, object):
  20. """
  21. The public key counterpart to an Curve25519 :class:`nacl.public.PrivateKey`
  22. for encrypting messages.
  23. :param public_key: [:class:`bytes`] Encoded Curve25519 public key
  24. :param encoder: A class that is able to decode the `public_key`
  25. :cvar SIZE: The size that the public key is required to be
  26. """
  27. SIZE = nacl.bindings.crypto_box_PUBLICKEYBYTES
  28. def __init__(self, public_key, encoder=encoding.RawEncoder):
  29. self._public_key = encoder.decode(public_key)
  30. if not isinstance(self._public_key, bytes):
  31. raise exc.TypeError("PublicKey must be created from 32 bytes")
  32. if len(self._public_key) != self.SIZE:
  33. raise exc.ValueError(
  34. "The public key must be exactly {0} bytes long".format(
  35. self.SIZE)
  36. )
  37. def __bytes__(self):
  38. return self._public_key
  39. def __hash__(self):
  40. return hash(bytes(self))
  41. def __eq__(self, other):
  42. if not isinstance(other, self.__class__):
  43. return False
  44. return nacl.bindings.sodium_memcmp(bytes(self), bytes(other))
  45. def __ne__(self, other):
  46. return not (self == other)
  47. class PrivateKey(encoding.Encodable, StringFixer, object):
  48. """
  49. Private key for decrypting messages using the Curve25519 algorithm.
  50. .. warning:: This **must** be protected and remain secret. Anyone who
  51. knows the value of your :class:`~nacl.public.PrivateKey` can decrypt
  52. any message encrypted by the corresponding
  53. :class:`~nacl.public.PublicKey`
  54. :param private_key: The private key used to decrypt messages
  55. :param encoder: The encoder class used to decode the given keys
  56. :cvar SIZE: The size that the private key is required to be
  57. """
  58. SIZE = nacl.bindings.crypto_box_SECRETKEYBYTES
  59. def __init__(self, private_key, encoder=encoding.RawEncoder):
  60. # Decode the secret_key
  61. private_key = encoder.decode(private_key)
  62. if not isinstance(private_key, bytes):
  63. raise exc.TypeError(
  64. "PrivateKey must be created from a 32 byte seed")
  65. # Verify that our seed is the proper size
  66. if len(private_key) != self.SIZE:
  67. raise exc.ValueError(
  68. "The secret key must be exactly %d bytes long" % self.SIZE)
  69. raw_public_key = nacl.bindings.crypto_scalarmult_base(private_key)
  70. self._private_key = private_key
  71. self.public_key = PublicKey(raw_public_key)
  72. def __bytes__(self):
  73. return self._private_key
  74. def __hash__(self):
  75. return hash(bytes(self))
  76. def __eq__(self, other):
  77. if not isinstance(other, self.__class__):
  78. return False
  79. return nacl.bindings.sodium_memcmp(bytes(self), bytes(other))
  80. def __ne__(self, other):
  81. return not (self == other)
  82. @classmethod
  83. def generate(cls):
  84. """
  85. Generates a random :class:`~nacl.public.PrivateKey` object
  86. :rtype: :class:`~nacl.public.PrivateKey`
  87. """
  88. return cls(random(PrivateKey.SIZE), encoder=encoding.RawEncoder)
  89. class Box(encoding.Encodable, StringFixer, object):
  90. """
  91. The Box class boxes and unboxes messages between a pair of keys
  92. The ciphertexts generated by :class:`~nacl.public.Box` include a 16
  93. byte authenticator which is checked as part of the decryption. An invalid
  94. authenticator will cause the decrypt function to raise an exception. The
  95. authenticator is not a signature. Once you've decrypted the message you've
  96. demonstrated the ability to create arbitrary valid message, so messages you
  97. send are repudiable. For non-repudiable messages, sign them after
  98. encryption.
  99. :param private_key: :class:`~nacl.public.PrivateKey` used to encrypt and
  100. decrypt messages
  101. :param public_key: :class:`~nacl.public.PublicKey` used to encrypt and
  102. decrypt messages
  103. :cvar NONCE_SIZE: The size that the nonce is required to be.
  104. """
  105. NONCE_SIZE = nacl.bindings.crypto_box_NONCEBYTES
  106. def __init__(self, private_key, public_key):
  107. if private_key and public_key:
  108. if ((not isinstance(private_key, PrivateKey) or
  109. not isinstance(public_key, PublicKey))):
  110. raise exc.TypeError("Box must be created from "
  111. "a PrivateKey and a PublicKey")
  112. self._shared_key = nacl.bindings.crypto_box_beforenm(
  113. public_key.encode(encoder=encoding.RawEncoder),
  114. private_key.encode(encoder=encoding.RawEncoder),
  115. )
  116. else:
  117. self._shared_key = None
  118. def __bytes__(self):
  119. return self._shared_key
  120. @classmethod
  121. def decode(cls, encoded, encoder=encoding.RawEncoder):
  122. # Create an empty box
  123. box = cls(None, None)
  124. # Assign our decoded value to the shared key of the box
  125. box._shared_key = encoder.decode(encoded)
  126. return box
  127. def encrypt(self, plaintext, nonce=None, encoder=encoding.RawEncoder):
  128. """
  129. Encrypts the plaintext message using the given `nonce` (or generates
  130. one randomly if omitted) and returns the ciphertext encoded with the
  131. encoder.
  132. .. warning:: It is **VITALLY** important that the nonce is a nonce,
  133. i.e. it is a number used only once for any given key. If you fail
  134. to do this, you compromise the privacy of the messages encrypted.
  135. :param plaintext: [:class:`bytes`] The plaintext message to encrypt
  136. :param nonce: [:class:`bytes`] The nonce to use in the encryption
  137. :param encoder: The encoder to use to encode the ciphertext
  138. :rtype: [:class:`nacl.utils.EncryptedMessage`]
  139. """
  140. if nonce is None:
  141. nonce = random(self.NONCE_SIZE)
  142. if len(nonce) != self.NONCE_SIZE:
  143. raise exc.ValueError("The nonce must be exactly %s bytes long" %
  144. self.NONCE_SIZE)
  145. ciphertext = nacl.bindings.crypto_box_afternm(
  146. plaintext,
  147. nonce,
  148. self._shared_key,
  149. )
  150. encoded_nonce = encoder.encode(nonce)
  151. encoded_ciphertext = encoder.encode(ciphertext)
  152. return EncryptedMessage._from_parts(
  153. encoded_nonce,
  154. encoded_ciphertext,
  155. encoder.encode(nonce + ciphertext),
  156. )
  157. def decrypt(self, ciphertext, nonce=None, encoder=encoding.RawEncoder):
  158. """
  159. Decrypts the ciphertext using the `nonce` (explicitly, when passed as a
  160. parameter or implicitly, when omitted, as part of the ciphertext) and
  161. returns the plaintext message.
  162. :param ciphertext: [:class:`bytes`] The encrypted message to decrypt
  163. :param nonce: [:class:`bytes`] The nonce used when encrypting the
  164. ciphertext
  165. :param encoder: The encoder used to decode the ciphertext.
  166. :rtype: [:class:`bytes`]
  167. """
  168. # Decode our ciphertext
  169. ciphertext = encoder.decode(ciphertext)
  170. if nonce is None:
  171. # If we were given the nonce and ciphertext combined, split them.
  172. nonce = ciphertext[:self.NONCE_SIZE]
  173. ciphertext = ciphertext[self.NONCE_SIZE:]
  174. if len(nonce) != self.NONCE_SIZE:
  175. raise exc.ValueError("The nonce must be exactly %s bytes long" %
  176. self.NONCE_SIZE)
  177. plaintext = nacl.bindings.crypto_box_open_afternm(
  178. ciphertext,
  179. nonce,
  180. self._shared_key,
  181. )
  182. return plaintext
  183. def shared_key(self):
  184. """
  185. Returns the Curve25519 shared secret, that can then be used as a key in
  186. other symmetric ciphers.
  187. .. warning:: It is **VITALLY** important that you use a nonce with your
  188. symmetric cipher. If you fail to do this, you compromise the
  189. privacy of the messages encrypted. Ensure that the key length of
  190. your cipher is 32 bytes.
  191. :rtype: [:class:`bytes`]
  192. """
  193. return self._shared_key