cbc_mode.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* cbc_mode.h - TinyCrypt interface to a CBC 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 CBC mode implementation.
  34. *
  35. * Overview: CBC (for "cipher block chaining") mode is a NIST approved mode of
  36. * operation defined in SP 800-38a. It can be used with any block
  37. * cipher to provide confidentiality of strings whose lengths are
  38. * multiples of the block_size of the underlying block cipher.
  39. * TinyCrypt hard codes AES as the block cipher.
  40. *
  41. * Security: CBC mode provides data confidentiality given that the maximum
  42. * number q of blocks encrypted under a single key satisfies
  43. * q < 2^63, which is not a practical constraint (it is considered a
  44. * good practice to replace the encryption when q == 2^56). CBC mode
  45. * provides NO data integrity.
  46. *
  47. * CBC mode assumes that the IV value input into the
  48. * tc_cbc_mode_encrypt is randomly generated. The TinyCrypt library
  49. * provides HMAC-PRNG module, which generates suitable IVs. Other
  50. * methods for generating IVs are acceptable, provided that the
  51. * values of the IVs generated appear random to any adversary,
  52. * including someone with complete knowledge of the system design.
  53. *
  54. * The randomness property on which CBC mode's security depends is
  55. * the unpredictability of the IV. Since it is unpredictable, this
  56. * means in practice that CBC mode requires that the IV is stored
  57. * somehow with the ciphertext in order to recover the plaintext.
  58. *
  59. * TinyCrypt CBC encryption prepends the IV to the ciphertext,
  60. * because this affords a more efficient (few buffers) decryption.
  61. * Hence tc_cbc_mode_encrypt assumes the ciphertext buffer is always
  62. * 16 bytes larger than the plaintext buffer.
  63. *
  64. * Requires: AES-128
  65. *
  66. * Usage: 1) call tc_cbc_mode_encrypt to encrypt data.
  67. *
  68. * 2) call tc_cbc_mode_decrypt to decrypt data.
  69. *
  70. */
  71. #ifndef __TC_CBC_MODE_H__
  72. #define __TC_CBC_MODE_H__
  73. #include <tinycrypt/aes.h>
  74. #ifdef __cplusplus
  75. extern "C" {
  76. #endif
  77. /**
  78. * @brief CBC encryption procedure
  79. * CBC encrypts inlen bytes of the in buffer into the out buffer
  80. * using the encryption key schedule provided, prepends iv to out
  81. * @return returns TC_CRYPTO_SUCCESS (1)
  82. * returns TC_CRYPTO_FAIL (0) if:
  83. * out == NULL or
  84. * in == NULL or
  85. * ctr == NULL or
  86. * sched == NULL or
  87. * inlen == 0 or
  88. * (inlen % TC_AES_BLOCK_SIZE) != 0 or
  89. * (outlen % TC_AES_BLOCK_SIZE) != 0 or
  90. * outlen != inlen + TC_AES_BLOCK_SIZE
  91. * @note Assumes: - sched has been configured by aes_set_encrypt_key
  92. * - iv contains a 16 byte random string
  93. * - out buffer is large enough to hold the ciphertext + iv
  94. * - out buffer is a contiguous buffer
  95. * - in holds the plaintext and is a contiguous buffer
  96. * - inlen gives the number of bytes in the in buffer
  97. * @param out IN/OUT -- buffer to receive the ciphertext
  98. * @param outlen IN -- length of ciphertext buffer in bytes
  99. * @param in IN -- plaintext to encrypt
  100. * @param inlen IN -- length of plaintext buffer in bytes
  101. * @param iv IN -- the IV for the this encrypt/decrypt
  102. * @param sched IN -- AES key schedule for this encrypt
  103. */
  104. int tc_cbc_mode_encrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
  105. unsigned int inlen, const uint8_t *iv,
  106. const TCAesKeySched_t sched);
  107. /**
  108. * @brief CBC decryption procedure
  109. * CBC decrypts inlen bytes of the in buffer into the out buffer
  110. * using the provided encryption key schedule
  111. * @return returns TC_CRYPTO_SUCCESS (1)
  112. * returns TC_CRYPTO_FAIL (0) if:
  113. * out == NULL or
  114. * in == NULL or
  115. * sched == NULL or
  116. * inlen == 0 or
  117. * outlen == 0 or
  118. * (inlen % TC_AES_BLOCK_SIZE) != 0 or
  119. * (outlen % TC_AES_BLOCK_SIZE) != 0 or
  120. * outlen != inlen + TC_AES_BLOCK_SIZE
  121. * @note Assumes:- in == iv + ciphertext, i.e. the iv and the ciphertext are
  122. * contiguous. This allows for a very efficient decryption
  123. * algorithm that would not otherwise be possible
  124. * - sched was configured by aes_set_decrypt_key
  125. * - out buffer is large enough to hold the decrypted plaintext
  126. * and is a contiguous buffer
  127. * - inlen gives the number of bytes in the in buffer
  128. * @param out IN/OUT -- buffer to receive decrypted data
  129. * @param outlen IN -- length of plaintext buffer in bytes
  130. * @param in IN -- ciphertext to decrypt, including IV
  131. * @param inlen IN -- length of ciphertext buffer in bytes
  132. * @param iv IN -- the IV for the this encrypt/decrypt
  133. * @param sched IN -- AES key schedule for this decrypt
  134. *
  135. */
  136. int tc_cbc_mode_decrypt(uint8_t *out, unsigned int outlen, const uint8_t *in,
  137. unsigned int inlen, const uint8_t *iv,
  138. const TCAesKeySched_t sched);
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif /* __TC_CBC_MODE_H__ */