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