crypto.pyx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """A thin OpenSSL wrapper
  2. This could be replaced by PyCrypto maybe?
  3. """
  4. from libc.stdlib cimport malloc, free
  5. API_VERSION = 2
  6. cdef extern from "openssl/rand.h":
  7. int RAND_bytes(unsigned char *buf, int num)
  8. cdef extern from "openssl/evp.h":
  9. ctypedef struct EVP_MD:
  10. pass
  11. ctypedef struct EVP_CIPHER:
  12. pass
  13. ctypedef struct EVP_CIPHER_CTX:
  14. unsigned char *iv
  15. pass
  16. ctypedef struct ENGINE:
  17. pass
  18. const EVP_CIPHER *EVP_aes_256_ctr()
  19. void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a)
  20. void EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a)
  21. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
  22. const unsigned char *key, const unsigned char *iv)
  23. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
  24. const unsigned char *key, const unsigned char *iv)
  25. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  26. const unsigned char *in_, int inl)
  27. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  28. const unsigned char *in_, int inl)
  29. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  30. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  31. import struct
  32. _int = struct.Struct('>I')
  33. _long = struct.Struct('>Q')
  34. bytes_to_int = lambda x, offset=0: _int.unpack_from(x, offset)[0]
  35. bytes_to_long = lambda x, offset=0: _long.unpack_from(x, offset)[0]
  36. long_to_bytes = lambda x: _long.pack(x)
  37. def num_aes_blocks(int length):
  38. """Return the number of AES blocks required to encrypt/decrypt *length* bytes of data.
  39. Note: this is only correct for modes without padding, like AES-CTR.
  40. """
  41. return (length + 15) // 16
  42. cdef class AES:
  43. """A thin wrapper around the OpenSSL EVP cipher API
  44. """
  45. cdef EVP_CIPHER_CTX ctx
  46. cdef int is_encrypt
  47. def __cinit__(self, is_encrypt, key, iv=None):
  48. EVP_CIPHER_CTX_init(&self.ctx)
  49. self.is_encrypt = is_encrypt
  50. # Set cipher type and mode
  51. cipher_mode = EVP_aes_256_ctr()
  52. if self.is_encrypt:
  53. if not EVP_EncryptInit_ex(&self.ctx, cipher_mode, NULL, NULL, NULL):
  54. raise Exception('EVP_EncryptInit_ex failed')
  55. else: # decrypt
  56. if not EVP_DecryptInit_ex(&self.ctx, cipher_mode, NULL, NULL, NULL):
  57. raise Exception('EVP_DecryptInit_ex failed')
  58. self.reset(key, iv)
  59. def __dealloc__(self):
  60. EVP_CIPHER_CTX_cleanup(&self.ctx)
  61. def reset(self, key=None, iv=None):
  62. cdef const unsigned char *key2 = NULL
  63. cdef const unsigned char *iv2 = NULL
  64. if key:
  65. key2 = key
  66. if iv:
  67. iv2 = iv
  68. # Initialise key and IV
  69. if self.is_encrypt:
  70. if not EVP_EncryptInit_ex(&self.ctx, NULL, NULL, key2, iv2):
  71. raise Exception('EVP_EncryptInit_ex failed')
  72. else: # decrypt
  73. if not EVP_DecryptInit_ex(&self.ctx, NULL, NULL, key2, iv2):
  74. raise Exception('EVP_DecryptInit_ex failed')
  75. @property
  76. def iv(self):
  77. return self.ctx.iv[:16]
  78. def encrypt(self, data):
  79. cdef int inl = len(data)
  80. cdef int ctl = 0
  81. cdef int outl = 0
  82. # note: modes that use padding, need up to one extra AES block (16b)
  83. cdef unsigned char *out = <unsigned char *>malloc(inl+16)
  84. if not out:
  85. raise MemoryError
  86. try:
  87. if not EVP_EncryptUpdate(&self.ctx, out, &outl, data, inl):
  88. raise Exception('EVP_EncryptUpdate failed')
  89. ctl = outl
  90. if not EVP_EncryptFinal_ex(&self.ctx, out+ctl, &outl):
  91. raise Exception('EVP_EncryptFinal failed')
  92. ctl += outl
  93. return out[:ctl]
  94. finally:
  95. free(out)
  96. def decrypt(self, data):
  97. cdef int inl = len(data)
  98. cdef int ptl = 0
  99. cdef int outl = 0
  100. # note: modes that use padding, need up to one extra AES block (16b).
  101. # This is what the openssl docs say. I am not sure this is correct,
  102. # but OTOH it will not cause any harm if our buffer is a little bigger.
  103. cdef unsigned char *out = <unsigned char *>malloc(inl+16)
  104. if not out:
  105. raise MemoryError
  106. try:
  107. if not EVP_DecryptUpdate(&self.ctx, out, &outl, data, inl):
  108. raise Exception('EVP_DecryptUpdate failed')
  109. ptl = outl
  110. if EVP_DecryptFinal_ex(&self.ctx, out+ptl, &outl) <= 0:
  111. # this error check is very important for modes with padding or
  112. # authentication. for them, a failure here means corrupted data.
  113. # CTR mode does not use padding nor authentication.
  114. raise Exception('EVP_DecryptFinal failed')
  115. ptl += outl
  116. return out[:ptl]
  117. finally:
  118. free(out)