ccm_mode.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* ccm_mode.h - TinyCrypt interface to a CCM mode 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 CCM mode implementation.
  34. *
  35. * Overview: CCM (for "Counter with CBC-MAC") mode is a NIST approved mode of
  36. * operation defined in SP 800-38C.
  37. *
  38. * TinyCrypt CCM implementation accepts:
  39. *
  40. * 1) Both non-empty payload and associated data (it encrypts and
  41. * authenticates the payload and also authenticates the associated
  42. * data);
  43. * 2) Non-empty payload and empty associated data (it encrypts and
  44. * authenticates the payload);
  45. * 3) Non-empty associated data and empty payload (it degenerates to
  46. * an authentication mode on the associated data).
  47. *
  48. * TinyCrypt CCM implementation accepts associated data of any length
  49. * between 0 and (2^16 - 2^8) bytes.
  50. *
  51. * Security: The mac length parameter is an important parameter to estimate the
  52. * security against collision attacks (that aim at finding different
  53. * messages that produce the same authentication tag). TinyCrypt CCM
  54. * implementation accepts any even integer between 4 and 16, as
  55. * suggested in SP 800-38C.
  56. *
  57. * RFC-3610, which also specifies CCM, presents a few relevant
  58. * security suggestions, such as: it is recommended for most
  59. * applications to use a mac length greater than 8. Besides, the
  60. * usage of the same nonce for two different messages which are
  61. * encrypted with the same key destroys the security of CCM mode.
  62. *
  63. * Requires: AES-128
  64. *
  65. * Usage: 1) call tc_ccm_config to configure.
  66. *
  67. * 2) call tc_ccm_mode_encrypt to encrypt data and generate tag.
  68. *
  69. * 3) call tc_ccm_mode_decrypt to decrypt data and verify tag.
  70. */
  71. #ifndef __TC_CCM_MODE_H__
  72. #define __TC_CCM_MODE_H__
  73. #include <tinycrypt/aes.h>
  74. #include <stddef.h>
  75. #ifdef __cplusplus
  76. extern "C" {
  77. #endif
  78. /* max additional authenticated size in bytes: 2^16 - 2^8 = 65280 */
  79. #define TC_CCM_AAD_MAX_BYTES 0xff00
  80. /* max message size in bytes: 2^(8L) = 2^16 = 65536 */
  81. #define TC_CCM_PAYLOAD_MAX_BYTES 0x10000
  82. /* struct tc_ccm_mode_struct represents the state of a CCM computation */
  83. typedef struct tc_ccm_mode_struct {
  84. TCAesKeySched_t sched; /* AES key schedule */
  85. uint8_t *nonce; /* nonce required by CCM */
  86. unsigned int mlen; /* mac length in bytes (parameter t in SP-800 38C) */
  87. } *TCCcmMode_t;
  88. /**
  89. * @brief CCM configuration procedure
  90. * @return returns TC_CRYPTO_SUCCESS (1)
  91. * returns TC_CRYPTO_FAIL (0) if:
  92. * c == NULL or
  93. * sched == NULL or
  94. * nonce == NULL or
  95. * mlen != {4, 6, 8, 10, 12, 16}
  96. * @param c -- CCM state
  97. * @param sched IN -- AES key schedule
  98. * @param nonce IN - nonce
  99. * @param nlen -- nonce length in bytes
  100. * @param mlen -- mac length in bytes (parameter t in SP-800 38C)
  101. */
  102. int tc_ccm_config(TCCcmMode_t c, TCAesKeySched_t sched, uint8_t *nonce,
  103. unsigned int nlen, unsigned int mlen);
  104. /**
  105. * @brief CCM tag generation and encryption procedure
  106. * @return returns TC_CRYPTO_SUCCESS (1)
  107. * returns TC_CRYPTO_FAIL (0) if:
  108. * out == NULL or
  109. * c == NULL or
  110. * ((plen > 0) and (payload == NULL)) or
  111. * ((alen > 0) and (associated_data == NULL)) or
  112. * (alen >= TC_CCM_AAD_MAX_BYTES) or
  113. * (plen >= TC_CCM_PAYLOAD_MAX_BYTES) or
  114. * (olen < plen + maclength)
  115. *
  116. * @param out OUT -- encrypted data
  117. * @param olen IN -- output length in bytes
  118. * @param associated_data IN -- associated data
  119. * @param alen IN -- associated data length in bytes
  120. * @param payload IN -- payload
  121. * @param plen IN -- payload length in bytes
  122. * @param c IN -- CCM state
  123. *
  124. * @note: out buffer should be at least (plen + c->mlen) bytes long.
  125. *
  126. * @note: The sequence b for encryption is formatted as follows:
  127. * b = [FLAGS | nonce | counter ], where:
  128. * FLAGS is 1 byte long
  129. * nonce is 13 bytes long
  130. * counter is 2 bytes long
  131. * The byte FLAGS is composed by the following 8 bits:
  132. * 0-2 bits: used to represent the value of q-1
  133. * 3-7 btis: always 0's
  134. *
  135. * @note: The sequence b for authentication is formatted as follows:
  136. * b = [FLAGS | nonce | length(mac length)], where:
  137. * FLAGS is 1 byte long
  138. * nonce is 13 bytes long
  139. * length(mac length) is 2 bytes long
  140. * The byte FLAGS is composed by the following 8 bits:
  141. * 0-2 bits: used to represent the value of q-1
  142. * 3-5 bits: mac length (encoded as: (mlen-2)/2)
  143. * 6: Adata (0 if alen == 0, and 1 otherwise)
  144. * 7: always 0
  145. */
  146. int tc_ccm_generation_encryption(uint8_t *out, unsigned int olen,
  147. const uint8_t *associated_data,
  148. unsigned int alen, const uint8_t *payload,
  149. unsigned int plen, TCCcmMode_t c);
  150. /**
  151. * @brief CCM decryption and tag verification procedure
  152. * @return returns TC_CRYPTO_SUCCESS (1)
  153. * returns TC_CRYPTO_FAIL (0) if:
  154. * out == NULL or
  155. * c == NULL or
  156. * ((plen > 0) and (payload == NULL)) or
  157. * ((alen > 0) and (associated_data == NULL)) or
  158. * (alen >= TC_CCM_AAD_MAX_BYTES) or
  159. * (plen >= TC_CCM_PAYLOAD_MAX_BYTES) or
  160. * (olen < plen - c->mlen)
  161. *
  162. * @param out OUT -- decrypted data
  163. * @param associated_data IN -- associated data
  164. * @param alen IN -- associated data length in bytes
  165. * @param payload IN -- payload
  166. * @param plen IN -- payload length in bytes
  167. * @param c IN -- CCM state
  168. *
  169. * @note: out buffer should be at least (plen - c->mlen) bytes long.
  170. *
  171. * @note: The sequence b for encryption is formatted as follows:
  172. * b = [FLAGS | nonce | counter ], where:
  173. * FLAGS is 1 byte long
  174. * nonce is 13 bytes long
  175. * counter is 2 bytes long
  176. * The byte FLAGS is composed by the following 8 bits:
  177. * 0-2 bits: used to represent the value of q-1
  178. * 3-7 btis: always 0's
  179. *
  180. * @note: The sequence b for authentication is formatted as follows:
  181. * b = [FLAGS | nonce | length(mac length)], where:
  182. * FLAGS is 1 byte long
  183. * nonce is 13 bytes long
  184. * length(mac length) is 2 bytes long
  185. * The byte FLAGS is composed by the following 8 bits:
  186. * 0-2 bits: used to represent the value of q-1
  187. * 3-5 bits: mac length (encoded as: (mlen-2)/2)
  188. * 6: Adata (0 if alen == 0, and 1 otherwise)
  189. * 7: always 0
  190. */
  191. int tc_ccm_decryption_verification(uint8_t *out, unsigned int olen,
  192. const uint8_t *associated_data,
  193. unsigned int alen, const uint8_t *payload, unsigned int plen,
  194. TCCcmMode_t c);
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif /* __TC_CCM_MODE_H__ */