crypto.pyx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """A thin OpenSSL wrapper
  2. This could be replaced by PyCrypto or something similar when the performance
  3. of their PBKDF2 implementation is comparable to the OpenSSL version.
  4. """
  5. from libc.string cimport memcpy
  6. from libc.stdlib cimport malloc, free
  7. cdef extern from "openssl/rand.h":
  8. int RAND_bytes(unsigned char *buf,int num)
  9. cdef extern from "openssl/aes.h":
  10. ctypedef struct AES_KEY:
  11. pass
  12. int AES_set_encrypt_key(const unsigned char *userKey, const int bits, AES_KEY *key)
  13. void AES_ctr128_encrypt(const unsigned char *in_, unsigned char *out,
  14. size_t length, const AES_KEY *key,
  15. unsigned char *ivec,
  16. unsigned char *ecount_buf,
  17. unsigned int *num)
  18. cdef extern from "openssl/evp.h":
  19. ctypedef struct EVP_MD:
  20. pass
  21. const EVP_MD *EVP_sha256()
  22. int PKCS5_PBKDF2_HMAC(const char *password, int passwordlen,
  23. const unsigned char *salt, int saltlen, int iter,
  24. const EVP_MD *digest,
  25. int keylen, unsigned char *out)
  26. import struct
  27. _int = struct.Struct('>I')
  28. _long = struct.Struct('>Q')
  29. bytes_to_int = lambda x, offset=0: _int.unpack_from(x, offset)[0]
  30. bytes_to_long = lambda x, offset=0: _long.unpack_from(x, offset)[0]
  31. long_to_bytes = lambda x: _long.pack(x)
  32. def num_aes_blocks(length):
  33. """Return the number of AES blocks required to encrypt/decrypt *length* bytes of data
  34. """
  35. return (length + 15) // 16
  36. def pbkdf2_sha256(password, salt, iterations, size):
  37. """Password based key derivation function 2 (RFC2898)
  38. """
  39. cdef unsigned char *key = <unsigned char *>malloc(size)
  40. if not key:
  41. raise MemoryError
  42. try:
  43. rv = PKCS5_PBKDF2_HMAC(password, len(password), salt, len(salt), iterations, EVP_sha256(), size, key)
  44. if not rv:
  45. raise Exception('PKCS5_PBKDF2_HMAC failed')
  46. return key[:size]
  47. finally:
  48. free(key)
  49. def get_random_bytes(n):
  50. """Return n cryptographically strong pseudo-random bytes
  51. """
  52. cdef unsigned char *buf = <unsigned char *>malloc(n)
  53. if not buf:
  54. raise MemoryError
  55. try:
  56. if RAND_bytes(buf, n) < 1:
  57. raise Exception('RAND_bytes failed')
  58. return buf[:n]
  59. finally:
  60. free(buf)
  61. cdef class AES:
  62. """A thin wrapper around the OpenSSL AES CTR_MODE cipher
  63. """
  64. cdef AES_KEY key
  65. cdef unsigned char _iv[16]
  66. cdef unsigned char buf[16]
  67. cdef unsigned int num
  68. def __cinit__(self, key, iv=None):
  69. self.reset(key, iv)
  70. def reset(self, key=None, iv=None):
  71. if key:
  72. AES_set_encrypt_key(key, len(key) * 8, &self.key)
  73. if iv:
  74. memcpy(self._iv, <unsigned char *>iv, 16)
  75. self.num = 0
  76. @property
  77. def iv(self):
  78. return self._iv[:16]
  79. def encrypt(self, data):
  80. cdef int n = len(data)
  81. cdef unsigned char *out = <unsigned char *>malloc(n)
  82. if not out:
  83. raise MemoryError
  84. try:
  85. AES_ctr128_encrypt(data, out, len(data), &self.key, self._iv, self.buf, &self.num)
  86. return out[:n]
  87. finally:
  88. free(out)
  89. decrypt = encrypt