cmac_mode.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* cmac_mode.h -- interface to a CMAC implementation */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @file
  33. * @brief Interface to a CMAC implementation.
  34. *
  35. * Overview: CMAC is defined NIST in SP 800-38B, and is the standard algorithm
  36. * for computing a MAC using a block cipher. It can compute the MAC
  37. * for a byte string of any length. It is distinguished from CBC-MAC
  38. * in the processing of the final message block; CMAC uses a
  39. * different technique to compute the final message block is full
  40. * size or only partial, while CBC-MAC uses the same technique for
  41. * both. This difference permits CMAC to be applied to variable
  42. * length messages, while all messages authenticated by CBC-MAC must
  43. * be the same length.
  44. *
  45. * Security: AES128-CMAC mode of operation offers 64 bits of security against
  46. * collision attacks. Note however that an external attacker cannot
  47. * generate the tags him/herself without knowing the MAC key. In this
  48. * sense, to attack the collision property of AES128-CMAC, an
  49. * external attacker would need the cooperation of the legal user to
  50. * produce an exponentially high number of tags (e.g. 2^64) to
  51. * finally be able to look for collisions and benefit from them. As
  52. * an extra precaution, the current implementation allows to at most
  53. * 2^48 calls to the tc_cmac_update function before re-calling
  54. * tc_cmac_setup (allowing a new key to be set), as suggested in
  55. * Appendix B of SP 800-38B.
  56. *
  57. * Requires: AES-128
  58. *
  59. * Usage: This implementation provides a "scatter-gather" interface, so that
  60. * the CMAC value can be computed incrementally over a message
  61. * scattered in different segments throughout memory. Experience shows
  62. * this style of interface tends to minimize the burden of programming
  63. * correctly. Like all symmetric key operations, it is session
  64. * oriented.
  65. *
  66. * To begin a CMAC session, use tc_cmac_setup to initialize a struct
  67. * tc_cmac_struct with encryption key and buffer. Our implementation
  68. * always assume that the AES key to be the same size as the block
  69. * cipher block size. Once setup, this data structure can be used for
  70. * many CMAC computations.
  71. *
  72. * Once the state has been setup with a key, computing the CMAC of
  73. * some data requires three steps:
  74. *
  75. * (1) first use tc_cmac_init to initialize a new CMAC computation.
  76. * (2) next mix all of the data into the CMAC computation state using
  77. * tc_cmac_update. If all of the data resides in a single data
  78. * segment then only one tc_cmac_update call is needed; if data
  79. * is scattered throughout memory in n data segments, then n calls
  80. * will be needed. CMAC IS ORDER SENSITIVE, to be able to detect
  81. * attacks that swap bytes, so the order in which data is mixed
  82. * into the state is critical!
  83. * (3) Once all of the data for a message has been mixed, use
  84. * tc_cmac_final to compute the CMAC tag value.
  85. *
  86. * Steps (1)-(3) can be repeated as many times as you want to CMAC
  87. * multiple messages. A practical limit is 2^48 1K messages before you
  88. * have to change the key.
  89. *
  90. * Once you are done computing CMAC with a key, it is a good idea to
  91. * destroy the state so an attacker cannot recover the key; use
  92. * tc_cmac_erase to accomplish this.
  93. */
  94. #ifndef __TC_CMAC_MODE_H__
  95. #define __TC_CMAC_MODE_H__
  96. #include <tinycrypt/aes.h>
  97. #include <stddef.h>
  98. #ifdef __cplusplus
  99. extern "C" {
  100. #endif
  101. /* padding for last message block */
  102. #define TC_CMAC_PADDING 0x80
  103. /* struct tc_cmac_struct represents the state of a CMAC computation */
  104. typedef struct tc_cmac_struct {
  105. /* initialization vector */
  106. uint8_t iv[TC_AES_BLOCK_SIZE];
  107. /* used if message length is a multiple of block_size bytes */
  108. uint8_t K1[TC_AES_BLOCK_SIZE];
  109. /* used if message length isn't a multiple block_size bytes */
  110. uint8_t K2[TC_AES_BLOCK_SIZE];
  111. /* where to put bytes that didn't fill a block */
  112. uint8_t leftover[TC_AES_BLOCK_SIZE];
  113. /* identifies the encryption key */
  114. unsigned int keyid;
  115. /* next available leftover location */
  116. unsigned int leftover_offset;
  117. /* AES key schedule */
  118. TCAesKeySched_t sched;
  119. /* calls to tc_cmac_update left before re-key */
  120. uint64_t countdown;
  121. } *TCCmacState_t;
  122. /**
  123. * @brief Configures the CMAC state to use the given AES key
  124. * @return returns TC_CRYPTO_SUCCESS (1) after having configured the CMAC state
  125. * returns TC_CRYPTO_FAIL (0) if:
  126. * s == NULL or
  127. * key == NULL
  128. *
  129. * @param s IN/OUT -- the state to set up
  130. * @param key IN -- the key to use
  131. * @param sched IN -- AES key schedule
  132. */
  133. int tc_cmac_setup(TCCmacState_t s, const uint8_t *key,
  134. TCAesKeySched_t sched);
  135. /**
  136. * @brief Erases the CMAC state
  137. * @return returns TC_CRYPTO_SUCCESS (1) after having configured the CMAC state
  138. * returns TC_CRYPTO_FAIL (0) if:
  139. * s == NULL
  140. *
  141. * @param s IN/OUT -- the state to erase
  142. */
  143. int tc_cmac_erase(TCCmacState_t s);
  144. /**
  145. * @brief Initializes a new CMAC computation
  146. * @return returns TC_CRYPTO_SUCCESS (1) after having initialized the CMAC state
  147. * returns TC_CRYPTO_FAIL (0) if:
  148. * s == NULL
  149. *
  150. * @param s IN/OUT -- the state to initialize
  151. */
  152. int tc_cmac_init(TCCmacState_t s);
  153. /**
  154. * @brief Incrementally computes CMAC over the next data segment
  155. * @return returns TC_CRYPTO_SUCCESS (1) after successfully updating the CMAC state
  156. * returns TC_CRYPTO_FAIL (0) if:
  157. * s == NULL or
  158. * if data == NULL when dlen > 0
  159. *
  160. * @param s IN/OUT -- the CMAC state
  161. * @param data IN -- the next data segment to MAC
  162. * @param dlen IN -- the length of data in bytes
  163. */
  164. int tc_cmac_update(TCCmacState_t s, const uint8_t *data, size_t dlen);
  165. /**
  166. * @brief Generates the tag from the CMAC state
  167. * @return returns TC_CRYPTO_SUCCESS (1) after successfully generating the tag
  168. * returns TC_CRYPTO_FAIL (0) if:
  169. * tag == NULL or
  170. * s == NULL
  171. *
  172. * @param tag OUT -- the CMAC tag
  173. * @param s IN -- CMAC state
  174. */
  175. int tc_cmac_final(uint8_t *tag, TCCmacState_t s);
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* __TC_CMAC_MODE_H__ */