crypto.pyx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. pass
  15. ctypedef struct ENGINE:
  16. pass
  17. const EVP_CIPHER *EVP_aes_256_ctr()
  18. EVP_CIPHER_CTX *EVP_CIPHER_CTX_new()
  19. void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *a)
  20. int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
  21. const unsigned char *key, const unsigned char *iv)
  22. int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
  23. const unsigned char *key, const unsigned char *iv)
  24. int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  25. const unsigned char *in_, int inl)
  26. int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
  27. const unsigned char *in_, int inl)
  28. int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  29. int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
  30. import struct
  31. _int = struct.Struct('>I')
  32. _long = struct.Struct('>Q')
  33. _2long = struct.Struct('>QQ')
  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 bytes16_to_int(b, offset=0):
  38. h, l = _2long.unpack_from(b, offset)
  39. return (h << 64) + l
  40. def int_to_bytes16(i):
  41. max_uint64 = 0xffffffffffffffff
  42. l = i & max_uint64
  43. h = (i >> 64) & max_uint64
  44. return _2long.pack(h, l)
  45. def increment_iv(iv, amount=1):
  46. """
  47. Increment the IV by the given amount (default 1).
  48. :param iv: input IV, 16 bytes (128 bit)
  49. :param amount: increment value
  50. :return: input_IV + amount, 16 bytes (128 bit)
  51. """
  52. assert len(iv) == 16
  53. iv = bytes16_to_int(iv)
  54. iv += amount
  55. iv = int_to_bytes16(iv)
  56. return iv
  57. def num_aes_blocks(int length):
  58. """Return the number of AES blocks required to encrypt/decrypt *length* bytes of data.
  59. Note: this is only correct for modes without padding, like AES-CTR.
  60. """
  61. return (length + 15) // 16
  62. cdef class AES:
  63. """A thin wrapper around the OpenSSL EVP cipher API
  64. """
  65. cdef EVP_CIPHER_CTX *ctx
  66. cdef int is_encrypt
  67. cdef unsigned char iv_orig[16]
  68. cdef long long blocks
  69. def __cinit__(self, is_encrypt, key, iv=None):
  70. self.ctx = EVP_CIPHER_CTX_new()
  71. self.is_encrypt = is_encrypt
  72. # Set cipher type and mode
  73. cipher_mode = EVP_aes_256_ctr()
  74. if self.is_encrypt:
  75. if not EVP_EncryptInit_ex(self.ctx, cipher_mode, NULL, NULL, NULL):
  76. raise Exception('EVP_EncryptInit_ex failed')
  77. else: # decrypt
  78. if not EVP_DecryptInit_ex(self.ctx, cipher_mode, NULL, NULL, NULL):
  79. raise Exception('EVP_DecryptInit_ex failed')
  80. self.reset(key, iv)
  81. def __dealloc__(self):
  82. EVP_CIPHER_CTX_free(self.ctx)
  83. def reset(self, key=None, iv=None):
  84. cdef const unsigned char *key2 = NULL
  85. cdef const unsigned char *iv2 = NULL
  86. if key:
  87. key2 = key
  88. if iv:
  89. iv2 = iv
  90. assert isinstance(iv, bytes) and len(iv) == 16
  91. for i in range(16):
  92. self.iv_orig[i] = iv[i]
  93. self.blocks = 0 # number of AES blocks encrypted starting with iv_orig
  94. # Initialise key and IV
  95. if self.is_encrypt:
  96. if not EVP_EncryptInit_ex(self.ctx, NULL, NULL, key2, iv2):
  97. raise Exception('EVP_EncryptInit_ex failed')
  98. else: # decrypt
  99. if not EVP_DecryptInit_ex(self.ctx, NULL, NULL, key2, iv2):
  100. raise Exception('EVP_DecryptInit_ex failed')
  101. @property
  102. def iv(self):
  103. return increment_iv(self.iv_orig[:16], self.blocks)
  104. def encrypt(self, data):
  105. cdef int inl = len(data)
  106. cdef int ctl = 0
  107. cdef int outl = 0
  108. # note: modes that use padding, need up to one extra AES block (16b)
  109. cdef unsigned char *out = <unsigned char *>malloc(inl+16)
  110. if not out:
  111. raise MemoryError
  112. try:
  113. if not EVP_EncryptUpdate(self.ctx, out, &outl, data, inl):
  114. raise Exception('EVP_EncryptUpdate failed')
  115. ctl = outl
  116. if not EVP_EncryptFinal_ex(self.ctx, out+ctl, &outl):
  117. raise Exception('EVP_EncryptFinal failed')
  118. ctl += outl
  119. self.blocks += num_aes_blocks(ctl)
  120. return out[:ctl]
  121. finally:
  122. free(out)
  123. def decrypt(self, data):
  124. cdef int inl = len(data)
  125. cdef int ptl = 0
  126. cdef int outl = 0
  127. # note: modes that use padding, need up to one extra AES block (16b).
  128. # This is what the openssl docs say. I am not sure this is correct,
  129. # but OTOH it will not cause any harm if our buffer is a little bigger.
  130. cdef unsigned char *out = <unsigned char *>malloc(inl+16)
  131. if not out:
  132. raise MemoryError
  133. try:
  134. if not EVP_DecryptUpdate(self.ctx, out, &outl, data, inl):
  135. raise Exception('EVP_DecryptUpdate failed')
  136. ptl = outl
  137. if EVP_DecryptFinal_ex(self.ctx, out+ptl, &outl) <= 0:
  138. # this error check is very important for modes with padding or
  139. # authentication. for them, a failure here means corrupted data.
  140. # CTR mode does not use padding nor authentication.
  141. raise Exception('EVP_DecryptFinal failed')
  142. ptl += outl
  143. self.blocks += num_aes_blocks(inl)
  144. return out[:ptl]
  145. finally:
  146. free(out)