_mode_ccm.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. """
  31. Counter with CBC-MAC (CCM) mode.
  32. """
  33. __all__ = ['CcmMode']
  34. from Crypto.Util.py3compat import byte_string, b, bchr, bord, unhexlify
  35. from Crypto.Util.strxor import strxor
  36. from Crypto.Util.number import long_to_bytes
  37. from Crypto.Hash import BLAKE2s
  38. from Crypto.Random import get_random_bytes
  39. def enum(**enums):
  40. return type('Enum', (), enums)
  41. MacStatus = enum(NOT_STARTED=0, PROCESSING_AUTH_DATA=1, PROCESSING_PLAINTEXT=2)
  42. class CcmMode(object):
  43. """Counter with CBC-MAC (CCM).
  44. This is an Authenticated Encryption with Associated Data (`AEAD`_) mode.
  45. It provides both confidentiality and authenticity.
  46. The header of the message may be left in the clear, if needed, and it will
  47. still be subject to authentication. The decryption step tells the receiver
  48. if the message comes from a source that really knowns the secret key.
  49. Additionally, decryption detects if any part of the message - including the
  50. header - has been modified or corrupted.
  51. This mode requires a nonce. The nonce shall never repeat for two
  52. different messages encrypted with the same key, but it does not need
  53. to be random.
  54. Note that there is a trade-off between the size of the nonce and the
  55. maximum size of a single message you can encrypt.
  56. It is important to use a large nonce if the key is reused across several
  57. messages and the nonce is chosen randomly.
  58. It is acceptable to us a short nonce if the key is only used a few times or
  59. if the nonce is taken from a counter.
  60. The following table shows the trade-off when the nonce is chosen at
  61. random. The column on the left shows how many messages it takes
  62. for the keystream to repeat **on average**. In practice, you will want to
  63. stop using the key way before that.
  64. +--------------------+---------------+-------------------+
  65. | Avg. # of messages | nonce | Max. message |
  66. | before keystream | size | size |
  67. | repeats | (bytes) | (bytes) |
  68. +====================+===============+===================+
  69. | 2^52 | 13 | 64K |
  70. +--------------------+---------------+-------------------+
  71. | 2^48 | 12 | 16M |
  72. +--------------------+---------------+-------------------+
  73. | 2^44 | 11 | 4G |
  74. +--------------------+---------------+-------------------+
  75. | 2^40 | 10 | 1T |
  76. +--------------------+---------------+-------------------+
  77. | 2^36 | 9 | 64P |
  78. +--------------------+---------------+-------------------+
  79. | 2^32 | 8 | 16E |
  80. +--------------------+---------------+-------------------+
  81. This mode is only available for ciphers that operate on 128 bits blocks
  82. (e.g. AES but not TDES).
  83. See `NIST SP800-38C`_ or RFC3610_.
  84. .. _`NIST SP800-38C`: http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C.pdf
  85. .. _RFC3610: https://tools.ietf.org/html/rfc3610
  86. .. _AEAD: http://blog.cryptographyengineering.com/2012/05/how-to-choose-authenticated-encryption.html
  87. :undocumented: __init__
  88. """
  89. def __init__(self, factory, key, nonce, mac_len, msg_len, assoc_len,
  90. cipher_params):
  91. self.block_size = factory.block_size
  92. """The block size of the underlying cipher, in bytes."""
  93. self.nonce = nonce
  94. """The nonce used for this cipher instance"""
  95. self._factory = factory
  96. self._key = key
  97. self._mac_len = mac_len
  98. self._msg_len = msg_len
  99. self._assoc_len = assoc_len
  100. self._cipher_params = cipher_params
  101. self._mac_tag = None # Cache for MAC tag
  102. if self.block_size != 16:
  103. raise ValueError("CCM mode is only available for ciphers"
  104. " that operate on 128 bits blocks")
  105. # MAC tag length (Tlen)
  106. if mac_len not in (4, 6, 8, 10, 12, 14, 16):
  107. raise ValueError("Parameter 'mac_len' must be even"
  108. " and in the range 4..16 (not %d)" % mac_len)
  109. # Nonce value
  110. if not (nonce and 7 <= len(nonce) <= 13):
  111. raise ValueError("Length of parameter 'nonce' must be"
  112. " in the range 7..13 bytes")
  113. # Create MAC object (the tag will be the last block
  114. # bytes worth of ciphertext)
  115. self._mac = self._factory.new(key,
  116. factory.MODE_CBC,
  117. iv=bchr(0) * 16,
  118. **cipher_params)
  119. self._mac_status = MacStatus.NOT_STARTED
  120. self._t = None
  121. # Allowed transitions after initialization
  122. self._next = [self.update, self.encrypt, self.decrypt,
  123. self.digest, self.verify]
  124. # Cumulative lengths
  125. self._cumul_assoc_len = 0
  126. self._cumul_msg_len = 0
  127. # Cache for unaligned associated data/plaintext.
  128. # This is a list, but when the MAC starts, it will become a binary
  129. # string no longer than the block size.
  130. self._cache = []
  131. # Start CTR cipher, by formatting the counter (A.3)
  132. q = 15 - len(nonce) # length of Q, the encoded message length
  133. self._cipher = self._factory.new(key,
  134. self._factory.MODE_CTR,
  135. nonce=bchr(q - 1) + nonce,
  136. **cipher_params)
  137. # S_0, step 6 in 6.1 for j=0
  138. self._s_0 = self._cipher.encrypt(bchr(0) * 16)
  139. # Try to start the MAC
  140. if None not in (assoc_len, msg_len):
  141. self._start_mac()
  142. def _start_mac(self):
  143. assert(self._mac_status == MacStatus.NOT_STARTED)
  144. assert(None not in (self._assoc_len, self._msg_len))
  145. assert(isinstance(self._cache, list))
  146. # Formatting control information and nonce (A.2.1)
  147. q = 15 - len(self.nonce) # length of Q, the encoded message length
  148. flags = (64 * (self._assoc_len > 0) + 8 * ((self._mac_len - 2) // 2) +
  149. (q - 1))
  150. b_0 = bchr(flags) + self.nonce + long_to_bytes(self._msg_len, q)
  151. # Formatting associated data (A.2.2)
  152. # Encoded 'a' is concatenated with the associated data 'A'
  153. assoc_len_encoded = b('')
  154. if self._assoc_len > 0:
  155. if self._assoc_len < (2 ** 16 - 2 ** 8):
  156. enc_size = 2
  157. elif self._assoc_len < (2L ** 32):
  158. assoc_len_encoded = b('\xFF\xFE')
  159. enc_size = 4
  160. else:
  161. assoc_len_encoded = b('\xFF\xFF')
  162. enc_size = 8
  163. assoc_len_encoded += long_to_bytes(self._assoc_len, enc_size)
  164. # b_0 and assoc_len_encoded must be processed first
  165. self._cache.insert(0, b_0)
  166. self._cache.insert(1, assoc_len_encoded)
  167. # Process all the data cached so far
  168. first_data_to_mac = b("").join(self._cache)
  169. self._cache = b("")
  170. self._mac_status = MacStatus.PROCESSING_AUTH_DATA
  171. self._update(first_data_to_mac)
  172. def _pad_cache_and_update(self):
  173. assert(self._mac_status != MacStatus.NOT_STARTED)
  174. assert(byte_string(self._cache))
  175. assert(len(self._cache) < self.block_size)
  176. # Associated data is concatenated with the least number
  177. # of zero bytes (possibly none) to reach alignment to
  178. # the 16 byte boundary (A.2.3)
  179. len_cache = len(self._cache)
  180. if len_cache > 0:
  181. self._update(bchr(0) * (self.block_size - len_cache))
  182. def update(self, assoc_data):
  183. """Protect associated data
  184. If there is any associated data, the caller has to invoke
  185. this function one or more times, before using
  186. ``decrypt`` or ``encrypt``.
  187. By *associated data* it is meant any data (e.g. packet headers) that
  188. will not be encrypted and will be transmitted in the clear.
  189. However, the receiver is still able to detect any modification to it.
  190. In CCM, the *associated data* is also called
  191. *additional authenticated data* (AAD).
  192. If there is no associated data, this method must not be called.
  193. The caller may split associated data in segments of any size, and
  194. invoke this method multiple times, each time with the next segment.
  195. :Parameters:
  196. assoc_data : byte string
  197. A piece of associated data. There are no restrictions on its size.
  198. """
  199. if self.update not in self._next:
  200. raise TypeError("update() can only be called"
  201. " immediately after initialization")
  202. self._next = [self.update, self.encrypt, self.decrypt,
  203. self.digest, self.verify]
  204. self._cumul_assoc_len += len(assoc_data)
  205. if self._assoc_len is not None and \
  206. self._cumul_assoc_len > self._assoc_len:
  207. raise ValueError("Associated data is too long")
  208. self._update(assoc_data)
  209. return self
  210. def _update(self, assoc_data_pt=b("")):
  211. """Update the MAC with associated data or plaintext
  212. (without FSM checks)"""
  213. if self._mac_status == MacStatus.NOT_STARTED:
  214. self._cache.append(assoc_data_pt)
  215. return
  216. assert(byte_string(self._cache))
  217. assert(len(self._cache) < self.block_size)
  218. if len(self._cache) > 0:
  219. filler = min(self.block_size - len(self._cache),
  220. len(assoc_data_pt))
  221. self._cache += assoc_data_pt[:filler]
  222. assoc_data_pt = assoc_data_pt[filler:]
  223. if len(self._cache) < self.block_size:
  224. return
  225. # The cache is exactly one block
  226. self._t = self._mac.encrypt(self._cache)
  227. self._cache = b("")
  228. update_len = len(assoc_data_pt) // self.block_size * self.block_size
  229. self._cache = assoc_data_pt[update_len:]
  230. if update_len > 0:
  231. self._t = self._mac.encrypt(assoc_data_pt[:update_len])[-16:]
  232. def encrypt(self, plaintext):
  233. """Encrypt data with the key set at initialization.
  234. A cipher object is stateful: once you have encrypted a message
  235. you cannot encrypt (or decrypt) another message using the same
  236. object.
  237. This method can be called only **once** if ``msg_len`` was
  238. not passed at initialization.
  239. If ``msg_len`` was given, the data to encrypt can be broken
  240. up in two or more pieces and `encrypt` can be called
  241. multiple times.
  242. That is, the statement:
  243. >>> c.encrypt(a) + c.encrypt(b)
  244. is equivalent to:
  245. >>> c.encrypt(a+b)
  246. This function does not add any padding to the plaintext.
  247. :Parameters:
  248. plaintext : byte string
  249. The piece of data to encrypt.
  250. It can be of any length.
  251. :Return:
  252. the encrypted data, as a byte string.
  253. It is as long as *plaintext*.
  254. """
  255. if self.encrypt not in self._next:
  256. raise TypeError("encrypt() can only be called after"
  257. " initialization or an update()")
  258. self._next = [self.encrypt, self.digest]
  259. # No more associated data allowed from now
  260. if self._assoc_len is None:
  261. assert(isinstance(self._cache, list))
  262. self._assoc_len = sum([len(x) for x in self._cache])
  263. if self._msg_len is not None:
  264. self._start_mac()
  265. else:
  266. if self._cumul_assoc_len < self._assoc_len:
  267. raise ValueError("Associated data is too short")
  268. # Only once piece of plaintext accepted if message length was
  269. # not declared in advance
  270. if self._msg_len is None:
  271. self._msg_len = len(plaintext)
  272. self._start_mac()
  273. self._next = [self.digest]
  274. self._cumul_msg_len += len(plaintext)
  275. if self._cumul_msg_len > self._msg_len:
  276. raise ValueError("Message is too long")
  277. if self._mac_status == MacStatus.PROCESSING_AUTH_DATA:
  278. # Associated data is concatenated with the least number
  279. # of zero bytes (possibly none) to reach alignment to
  280. # the 16 byte boundary (A.2.3)
  281. self._pad_cache_and_update()
  282. self._mac_status = MacStatus.PROCESSING_PLAINTEXT
  283. self._update(plaintext)
  284. return self._cipher.encrypt(plaintext)
  285. def decrypt(self, ciphertext):
  286. """Decrypt data with the key set at initialization.
  287. A cipher object is stateful: once you have decrypted a message
  288. you cannot decrypt (or encrypt) another message with the same
  289. object.
  290. This method can be called only **once** if ``msg_len`` was
  291. not passed at initialization.
  292. If ``msg_len`` was given, the data to decrypt can be
  293. broken up in two or more pieces and `decrypt` can be
  294. called multiple times.
  295. That is, the statement:
  296. >>> c.decrypt(a) + c.decrypt(b)
  297. is equivalent to:
  298. >>> c.decrypt(a+b)
  299. This function does not remove any padding from the plaintext.
  300. :Parameters:
  301. ciphertext : byte string
  302. The piece of data to decrypt.
  303. It can be of any length.
  304. :Return: the decrypted data (byte string).
  305. """
  306. if self.decrypt not in self._next:
  307. raise TypeError("decrypt() can only be called"
  308. " after initialization or an update()")
  309. self._next = [self.decrypt, self.verify]
  310. # No more associated data allowed from now
  311. if self._assoc_len is None:
  312. assert(isinstance(self._cache, list))
  313. self._assoc_len = sum([len(x) for x in self._cache])
  314. if self._msg_len is not None:
  315. self._start_mac()
  316. else:
  317. if self._cumul_assoc_len < self._assoc_len:
  318. raise ValueError("Associated data is too short")
  319. # Only once piece of ciphertext accepted if message length was
  320. # not declared in advance
  321. if self._msg_len is None:
  322. self._msg_len = len(ciphertext)
  323. self._start_mac()
  324. self._next = [self.verify]
  325. self._cumul_msg_len += len(ciphertext)
  326. if self._cumul_msg_len > self._msg_len:
  327. raise ValueError("Message is too long")
  328. if self._mac_status == MacStatus.PROCESSING_AUTH_DATA:
  329. # Associated data is concatenated with the least number
  330. # of zero bytes (possibly none) to reach alignment to
  331. # the 16 byte boundary (A.2.3)
  332. self._pad_cache_and_update()
  333. self._mac_status = MacStatus.PROCESSING_PLAINTEXT
  334. # Encrypt is equivalent to decrypt with the CTR mode
  335. plaintext = self._cipher.encrypt(ciphertext)
  336. self._update(plaintext)
  337. return plaintext
  338. def digest(self):
  339. """Compute the *binary* MAC tag.
  340. The caller invokes this function at the very end.
  341. This method returns the MAC that shall be sent to the receiver,
  342. together with the ciphertext.
  343. :Return: the MAC, as a byte string.
  344. """
  345. if self.digest not in self._next:
  346. raise TypeError("digest() cannot be called when decrypting"
  347. " or validating a message")
  348. self._next = [self.digest]
  349. return self._digest()
  350. def _digest(self):
  351. if self._mac_tag:
  352. return self._mac_tag
  353. if self._assoc_len is None:
  354. assert(isinstance(self._cache, list))
  355. self._assoc_len = sum([len(x) for x in self._cache])
  356. if self._msg_len is not None:
  357. self._start_mac()
  358. else:
  359. if self._cumul_assoc_len < self._assoc_len:
  360. raise ValueError("Associated data is too short")
  361. if self._msg_len is None:
  362. self._msg_len = 0
  363. self._start_mac()
  364. if self._cumul_msg_len != self._msg_len:
  365. raise ValueError("Message is too short")
  366. # Both associated data and payload are concatenated with the least
  367. # number of zero bytes (possibly none) that align it to the
  368. # 16 byte boundary (A.2.2 and A.2.3)
  369. self._pad_cache_and_update()
  370. # Step 8 in 6.1 (T xor MSB_Tlen(S_0))
  371. self._mac_tag = strxor(self._t, self._s_0)[:self._mac_len]
  372. return self._mac_tag
  373. def hexdigest(self):
  374. """Compute the *printable* MAC tag.
  375. This method is like `digest`.
  376. :Return: the MAC, as a hexadecimal string.
  377. """
  378. return "".join(["%02x" % bord(x) for x in self.digest()])
  379. def verify(self, received_mac_tag):
  380. """Validate the *binary* MAC tag.
  381. The caller invokes this function at the very end.
  382. This method checks if the decrypted message is indeed valid
  383. (that is, if the key is correct) and it has not been
  384. tampered with while in transit.
  385. :Parameters:
  386. received_mac_tag : byte string
  387. This is the *binary* MAC, as received from the sender.
  388. :Raises ValueError:
  389. if the MAC does not match. The message has been tampered with
  390. or the key is incorrect.
  391. """
  392. if self.verify not in self._next:
  393. raise TypeError("verify() cannot be called"
  394. " when encrypting a message")
  395. self._next = [self.verify]
  396. self._digest()
  397. secret = get_random_bytes(16)
  398. mac1 = BLAKE2s.new(digest_bits=160, key=secret, data=self._mac_tag)
  399. mac2 = BLAKE2s.new(digest_bits=160, key=secret, data=received_mac_tag)
  400. if mac1.digest() != mac2.digest():
  401. raise ValueError("MAC check failed")
  402. def hexverify(self, hex_mac_tag):
  403. """Validate the *printable* MAC tag.
  404. This method is like `verify`.
  405. :Parameters:
  406. hex_mac_tag : string
  407. This is the *printable* MAC, as received from the sender.
  408. :Raises ValueError:
  409. if the MAC does not match. The message has been tampered with
  410. or the key is incorrect.
  411. """
  412. self.verify(unhexlify(hex_mac_tag))
  413. def encrypt_and_digest(self, plaintext):
  414. """Perform encrypt() and digest() in one step.
  415. :Parameters:
  416. plaintext : byte string
  417. The piece of data to encrypt.
  418. :Return:
  419. a tuple with two byte strings:
  420. - the encrypted data
  421. - the MAC
  422. """
  423. return self.encrypt(plaintext), self.digest()
  424. def decrypt_and_verify(self, ciphertext, received_mac_tag):
  425. """Perform decrypt() and verify() in one step.
  426. :Parameters:
  427. ciphertext : byte string
  428. The piece of data to decrypt.
  429. received_mac_tag : byte string
  430. This is the *binary* MAC, as received from the sender.
  431. :Return: the decrypted data (byte string).
  432. :Raises ValueError:
  433. if the MAC does not match. The message has been tampered with
  434. or the key is incorrect.
  435. """
  436. plaintext = self.decrypt(ciphertext)
  437. self.verify(received_mac_tag)
  438. return plaintext
  439. def _create_ccm_cipher(factory, **kwargs):
  440. """Create a new block cipher, configured in CCM mode.
  441. :Parameters:
  442. factory : module
  443. A symmetric cipher module from `Crypto.Cipher` (like
  444. `Crypto.Cipher.AES`).
  445. :Keywords:
  446. key : byte string
  447. The secret key to use in the symmetric cipher.
  448. nonce : byte string
  449. A value that must never be reused for any other encryption.
  450. Its length must be in the range ``[7..13]``.
  451. 11 or 12 bytes are reasonable values in general. Bear in
  452. mind that with CCM there is a trade-off between nonce length and
  453. maximum message size.
  454. If not specified, a 11 byte long random string is used.
  455. mac_len : integer
  456. Length of the MAC, in bytes. It must be even and in
  457. the range ``[4..16]``. The default is 16.
  458. msg_len : integer
  459. Length of the message to (de)cipher.
  460. If not specified, ``encrypt`` or ``decrypt`` may only be called once.
  461. assoc_len : integer
  462. Length of the associated data.
  463. If not specified, all data is internally buffered.
  464. """
  465. try:
  466. key = key = kwargs.pop("key")
  467. except KeyError, e:
  468. raise TypeError("Missing parameter: " + str(e))
  469. nonce = kwargs.pop("nonce", None) # N
  470. if nonce is None:
  471. nonce = get_random_bytes(11)
  472. mac_len = kwargs.pop("mac_len", factory.block_size)
  473. msg_len = kwargs.pop("msg_len", None) # p
  474. assoc_len = kwargs.pop("assoc_len", None) # a
  475. cipher_params = dict(kwargs)
  476. return CcmMode(factory, key, nonce, mac_len, msg_len,
  477. assoc_len, cipher_params)