crypto.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """A thin ctypes based wrapper for OpenSSL 1.0
  2. """
  3. import sys
  4. from ctypes import cdll, c_char_p, c_int, c_uint, c_void_p, POINTER, create_string_buffer
  5. from ctypes.util import find_library
  6. import struct
  7. libcrypto = cdll.LoadLibrary(find_library('crypto'))
  8. # Default libcrypto on OS X is too old, try the brew version
  9. if not hasattr(libcrypto, 'PKCS5_PBKDF2_HMAC') and sys.platform == 'darwin':
  10. libcrypto = cdll.LoadLibrary('/usr/local/opt/openssl/lib/libcrypto.dylib')
  11. # Default libcrypto on FreeBSD is too old, try the ports version
  12. if not hasattr(libcrypto, 'PKCS5_PBKDF2_HMAC') and sys.platform.startswith('freebsd'):
  13. libcrypto = cdll.LoadLibrary('/usr/local/lib/libcrypto.so')
  14. libcrypto.PKCS5_PBKDF2_HMAC.argtypes = (c_char_p, c_int, c_char_p, c_int, c_int, c_void_p, c_int, c_char_p)
  15. libcrypto.EVP_sha256.restype = c_void_p
  16. libcrypto.AES_set_encrypt_key.argtypes = (c_char_p, c_int, c_char_p)
  17. libcrypto.AES_ctr128_encrypt.argtypes = (c_char_p, c_char_p, c_int, c_char_p, c_char_p, c_char_p, POINTER(c_uint))
  18. libcrypto.RAND_bytes.argtypes = (c_char_p, c_int)
  19. libcrypto.RAND_bytes.restype = c_int
  20. _int = struct.Struct('>I')
  21. _long = struct.Struct('>Q')
  22. bytes_to_int = lambda x, offset=0: _int.unpack_from(x, offset)[0]
  23. bytes_to_long = lambda x, offset=0: _long.unpack_from(x, offset)[0]
  24. long_to_bytes = lambda x: _long.pack(x)
  25. def num_aes_blocks(length):
  26. """Return the number of AES blocks required to encrypt/decrypt *length* bytes of data
  27. """
  28. return (length + 15) // 16
  29. def pbkdf2_sha256(password, salt, iterations, size):
  30. """Password based key derivation function 2 (RFC2898)
  31. """
  32. key = create_string_buffer(size)
  33. rv = libcrypto.PKCS5_PBKDF2_HMAC(password, len(password), salt, len(salt), iterations, libcrypto.EVP_sha256(), size, key)
  34. if not rv:
  35. raise Exception('PKCS5_PBKDF2_HMAC failed')
  36. return key.raw
  37. def get_random_bytes(n):
  38. """Return n cryptographically strong pseudo-random bytes
  39. """
  40. buf = create_string_buffer(n)
  41. if libcrypto.RAND_bytes(buf, n) < 1:
  42. raise Exception('RAND_bytes failed')
  43. return buf.raw
  44. class AES:
  45. """A thin wrapper around the OpenSSL AES CTR_MODE cipher
  46. """
  47. def __init__(self, key, iv=None):
  48. self.key = create_string_buffer(2000)
  49. self.iv = create_string_buffer(16)
  50. self.buf = create_string_buffer(16)
  51. self.num = c_uint()
  52. self.reset(key, iv)
  53. def reset(self, key=None, iv=None):
  54. if key:
  55. libcrypto.AES_set_encrypt_key(key, len(key) * 8, self.key)
  56. if iv:
  57. self.iv.raw = iv
  58. self.num.value = 0
  59. def encrypt(self, data):
  60. out = create_string_buffer(len(data))
  61. libcrypto.AES_ctr128_encrypt(data, out, len(data), self.key, self.iv, self.buf, self.num)
  62. return out.raw
  63. decrypt = encrypt