tinycrypt.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. TinyCrypt Cryptographic Library
  2. ###############################
  3. Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. Overview
  5. ********
  6. The TinyCrypt Library provides an implementation for targeting constrained devices
  7. with a minimal set of standard cryptography primitives, as listed below. To better
  8. serve applications targeting constrained devices, TinyCrypt implementations differ
  9. from the standard specifications (see the Important Remarks section for some
  10. important differences). Certain cryptographic primitives depend on other
  11. primitives, as mentioned in the list below.
  12. Aside from the Important Remarks section below, valuable information on the usage,
  13. security and technicalities of each cryptographic primitive are found in the
  14. corresponding header file.
  15. * SHA-256:
  16. * Type of primitive: Hash function.
  17. * Standard Specification: NIST FIPS PUB 180-4.
  18. * Requires: --
  19. * HMAC-SHA256:
  20. * Type of primitive: Message authentication code.
  21. * Standard Specification: RFC 2104.
  22. * Requires: SHA-256
  23. * HMAC-PRNG:
  24. * Type of primitive: Pseudo-random number generator (256-bit strength).
  25. * Standard Specification: NIST SP 800-90A.
  26. * Requires: SHA-256 and HMAC-SHA256.
  27. * AES-128:
  28. * Type of primitive: Block cipher.
  29. * Standard Specification: NIST FIPS PUB 197.
  30. * Requires: --
  31. * AES-CBC mode:
  32. * Type of primitive: Encryption mode of operation.
  33. * Standard Specification: NIST SP 800-38A.
  34. * Requires: AES-128.
  35. * AES-CTR mode:
  36. * Type of primitive: Encryption mode of operation.
  37. * Standard Specification: NIST SP 800-38A.
  38. * Requires: AES-128.
  39. * AES-CMAC mode:
  40. * Type of primitive: Message authentication code.
  41. * Standard Specification: NIST SP 800-38B.
  42. * Requires: AES-128.
  43. * AES-CCM mode:
  44. * Type of primitive: Authenticated encryption.
  45. * Standard Specification: NIST SP 800-38C.
  46. * Requires: AES-128.
  47. * CTR-PRNG:
  48. * Type of primitive: Pseudo-random number generator (128-bit strength).
  49. * Standard Specification: NIST SP 800-90A.
  50. * Requires: AES-128.
  51. * ECC-DH:
  52. * Type of primitive: Key exchange based on curve NIST p-256.
  53. * Standard Specification: RFC 6090.
  54. * Requires: ECC auxiliary functions (ecc.h/c).
  55. * ECC-DSA:
  56. * Type of primitive: Digital signature based on curve NIST p-256.
  57. * Standard Specification: RFC 6090.
  58. * Requires: ECC auxiliary functions (ecc.h/c).
  59. Design Goals
  60. ************
  61. * Minimize the code size of each cryptographic primitive. This means minimize
  62. the size of a platform-independent implementation, as presented in TinyCrypt.
  63. Note that various applications may require further features, optimizations with
  64. respect to other metrics and countermeasures for particular threats. These
  65. peculiarities would increase the code size and thus are not considered here.
  66. * Minimize the dependencies among the cryptographic primitives. This means
  67. that it is unnecessary to build and allocate object code for more primitives
  68. than the ones strictly required by the intended application. In other words,
  69. one can select and compile only the primitives required by the application.
  70. Important Remarks
  71. *****************
  72. The cryptographic implementations in TinyCrypt library have some limitations.
  73. Some of these limitations are inherent to the cryptographic primitives
  74. themselves, while others are specific to TinyCrypt. These limitations were accepted
  75. in order to meet its design goals (in special, minimal code size) and to better
  76. serve applications targeting constrained devices in general. Some of these
  77. limitations are discussed in-depth below.
  78. General Remarks
  79. ***************
  80. * TinyCrypt does **not** intend to be fully side-channel resistant. Due to the
  81. variety of side-channel attacks, many of them only relevant to certain
  82. platforms. In this sense, instead of penalizing all library users with
  83. side-channel countermeasures such as increasing the overall code size,
  84. TinyCrypt only implements certain generic timing-attack countermeasures.
  85. Specific Remarks
  86. ****************
  87. * SHA-256:
  88. * The number of bits_hashed in the state is not checked for overflow. Note
  89. however that this will only be a problem if you intend to hash more than
  90. 2^64 bits, which is an extremely large window.
  91. * HMAC:
  92. * The HMAC verification process is assumed to be performed by the application.
  93. This compares the computed tag with some given tag.
  94. Note that conventional memory-comparison methods (such as memcmp function)
  95. might be vulnerable to timing attacks; thus be sure to use a constant-time
  96. memory comparison function (such as compare_constant_time
  97. function provided in lib/utils.c).
  98. * The tc_hmac_final function, responsible for computing the message tag,
  99. cleans the state context before exiting. Thus, applications do not need to
  100. clean the TCHmacState_t ctx after calling tc_hmac_final. This should not
  101. be changed in future versions of the library as there are applications
  102. currently relying on this good-practice/feature of TinyCrypt.
  103. * HMAC-PRNG:
  104. * Before using HMAC-PRNG, you *must* find an entropy source to produce a seed.
  105. PRNGs only stretch the seed into a seemingly random output of arbitrary
  106. length. The security of the output is exactly equal to the
  107. unpredictability of the seed.
  108. * NIST SP 800-90A requires three items as seed material in the initialization
  109. step: entropy seed, personalization and a nonce (which is not implemented).
  110. TinyCrypt requires the personalization byte array and automatically creates
  111. the entropy seed using a mandatory call to the re-seed function.
  112. * AES-128:
  113. * The current implementation does not support other key-lengths (such as 256
  114. bits). Note that if you need AES-256, it doesn't sound as though your
  115. application is running in a constrained environment. AES-256 requires keys
  116. twice the size as for AES-128, and the key schedule is 40% larger.
  117. * CTR mode:
  118. * The AES-CTR mode limits the size of a data message they encrypt to 2^32
  119. blocks. If you need to encrypt larger data sets, your application would
  120. need to replace the key after 2^32 block encryptions.
  121. * CTR-PRNG:
  122. * Before using CTR-PRNG, you *must* find an entropy source to produce a seed.
  123. PRNGs only stretch the seed into a seemingly random output of arbitrary
  124. length. The security of the output is exactly equal to the
  125. unpredictability of the seed.
  126. * CBC mode:
  127. * TinyCrypt CBC decryption assumes that the iv and the ciphertext are
  128. contiguous (as produced by TinyCrypt CBC encryption). This allows for a
  129. very efficient decryption algorithm that would not otherwise be possible.
  130. * CMAC mode:
  131. * AES128-CMAC mode of operation offers 64 bits of security against collision
  132. attacks. Note however that an external attacker cannot generate the tags
  133. him/herself without knowing the MAC key. In this sense, to attack the
  134. collision property of AES128-CMAC, an external attacker would need the
  135. cooperation of the legal user to produce an exponentially high number of
  136. tags (e.g. 2^64) to finally be able to look for collisions and benefit
  137. from them. As an extra precaution, the current implementation allows to at
  138. most 2^48 calls to tc_cmac_update function before re-calling tc_cmac_setup
  139. (allowing a new key to be set), as suggested in Appendix B of SP 800-38B.
  140. * CCM mode:
  141. * There are a few tradeoffs for the selection of the parameters of CCM mode.
  142. In special, there is a tradeoff between the maximum number of invocations
  143. of CCM under a given key and the maximum payload length for those
  144. invocations. Both things are related to the parameter 'q' of CCM mode. The
  145. maximum number of invocations of CCM under a given key is determined by
  146. the nonce size, which is: 15-q bytes. The maximum payload length for those
  147. invocations is defined as 2^(8q) bytes.
  148. To achieve minimal code size, TinyCrypt CCM implementation fixes q = 2,
  149. which is a quite reasonable choice for constrained applications. The
  150. implications of this choice are:
  151. The nonce size is: 13 bytes.
  152. The maximum payload length is: 2^16 bytes = 65 KB.
  153. The mac size parameter is an important parameter to estimate the security
  154. against collision attacks (that aim at finding different messages that
  155. produce the same authentication tag). TinyCrypt CCM implementation
  156. accepts any even integer between 4 and 16, as suggested in SP 800-38C.
  157. * TinyCrypt CCM implementation accepts associated data of any length between
  158. 0 and (2^16 - 2^8) = 65280 bytes.
  159. * TinyCrypt CCM implementation accepts:
  160. * Both non-empty payload and associated data (it encrypts and
  161. authenticates the payload and only authenticates the associated data);
  162. * Non-empty payload and empty associated data (it encrypts and
  163. authenticates the payload);
  164. * Non-empty associated data and empty payload (it degenerates to an
  165. authentication-only mode on the associated data).
  166. * RFC-3610, which also specifies CCM, presents a few relevant security
  167. suggestions, such as: it is recommended for most applications to use a
  168. mac size greater than 8. Besides, it is emphasized that the usage of the
  169. same nonce for two different messages which are encrypted with the same
  170. key obviously destroys the security properties of CCM mode.
  171. * ECC-DH and ECC-DSA:
  172. * TinyCrypt ECC implementation is based on micro-ecc (see
  173. https://github.com/kmackay/micro-ecc). In the original micro-ecc
  174. documentation, there is an important remark about the way integers are
  175. represented:
  176. "Integer representation: To reduce code size, all large integers are
  177. represented using little-endian words - so the least significant word is
  178. first. You can use the 'ecc_bytes2native()' and 'ecc_native2bytes()'
  179. functions to convert between the native integer representation and the
  180. standardized octet representation."
  181. Note that the assumed bit layout is: {31, 30, ..., 0}, {63, 62, ..., 32},
  182. {95, 94, ..., 64}, {127, 126, ..., 96} for a very-long-integer (vli)
  183. consisting of 4 unsigned integers (as an example).
  184. * A cryptographically-secure PRNG function must be set (using uECC_set_rng())
  185. before calling uECC_make_key() or uECC_sign().
  186. Examples of Applications
  187. ************************
  188. It is possible to do useful cryptography with only the given small set of
  189. primitives. With this list of primitives it becomes feasible to support a range
  190. of cryptography usages:
  191. * Measurement of code, data structures, and other digital artifacts (SHA256);
  192. * Generate commitments (SHA256);
  193. * Construct keys (HMAC-SHA256);
  194. * Extract entropy from strings containing some randomness (HMAC-SHA256);
  195. * Construct random mappings (HMAC-SHA256);
  196. * Construct nonces and challenges (HMAC-PRNG, CTR-PRNG);
  197. * Authenticate using a shared secret (HMAC-SHA256);
  198. * Create an authenticated, replay-protected session (HMAC-SHA256 + HMAC-PRNG);
  199. * Authenticated encryption (AES-128 + AES-CCM);
  200. * Key-exchange (EC-DH);
  201. * Digital signature (EC-DSA);
  202. Test Vectors
  203. ************
  204. The library provides a test program for each cryptographic primitive (see 'test'
  205. folder). Besides illustrating how to use the primitives, these tests evaluate
  206. the correctness of the implementations by checking the results against
  207. well-known publicly validated test vectors.
  208. For the case of the HMAC-PRNG, due to the necessity of performing an extensive
  209. battery test to produce meaningful conclusions, we suggest the user to evaluate
  210. the unpredictability of the implementation by using the NIST Statistical Test
  211. Suite (see References).
  212. For the case of the EC-DH and EC-DSA implementations, most of the test vectors
  213. were obtained from the site of the NIST Cryptographic Algorithm Validation
  214. Program (CAVP), see References.
  215. References
  216. **********
  217. * `NIST FIPS PUB 180-4 (SHA-256)`_
  218. .. _NIST FIPS PUB 180-4 (SHA-256):
  219. http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  220. * `NIST FIPS PUB 197 (AES-128)`_
  221. .. _NIST FIPS PUB 197 (AES-128):
  222. http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  223. * `NIST SP800-90A (HMAC-PRNG)`_
  224. .. _NIST SP800-90A (HMAC-PRNG):
  225. http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  226. * `NIST SP 800-38A (AES-CBC and AES-CTR)`_
  227. .. _NIST SP 800-38A (AES-CBC and AES-CTR):
  228. http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  229. * `NIST SP 800-38B (AES-CMAC)`_
  230. .. _NIST SP 800-38B (AES-CMAC):
  231. http://csrc.nist.gov/publications/nistpubs/800-38B/SP_800-38B.pdf
  232. * `NIST SP 800-38C (AES-CCM)`_
  233. .. _NIST SP 800-38C (AES-CCM):
  234. http://csrc.nist.gov/publications/nistpubs/800-38C/SP800-38C_updated-July20_2007.pdf
  235. * `NIST Statistical Test Suite (useful for testing HMAC-PRNG)`_
  236. .. _NIST Statistical Test Suite (useful for testing HMAC-PRNG):
  237. http://csrc.nist.gov/groups/ST/toolkit/rng/documentation_software.html
  238. * `NIST Cryptographic Algorithm Validation Program (CAVP) site`_
  239. .. _NIST Cryptographic Algorithm Validation Program (CAVP) site:
  240. http://csrc.nist.gov/groups/STM/cavp/
  241. * `RFC 2104 (HMAC-SHA256)`_
  242. .. _RFC 2104 (HMAC-SHA256):
  243. https://www.ietf.org/rfc/rfc2104.txt
  244. * `RFC 6090 (ECC-DH and ECC-DSA)`_
  245. .. _RFC 6090 (ECC-DH and ECC-DSA):
  246. https://www.ietf.org/rfc/rfc6090.txt