ctr_mode.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* ctr_mode.h - TinyCrypt interface to CTR mode */
  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 CTR mode.
  34. *
  35. * Overview: CTR (pronounced "counter") mode is a NIST approved mode of
  36. * operation defined in SP 800-38a. It can be used with any
  37. * block cipher to provide confidentiality of strings of any
  38. * length. TinyCrypt hard codes AES128 as the block cipher.
  39. *
  40. * Security: CTR mode achieves confidentiality only if the counter value is
  41. * never reused with a same encryption key. If the counter is
  42. * repeated, than an adversary might be able to defeat the scheme.
  43. *
  44. * A usual method to ensure different counter values refers to
  45. * initialize the counter in a given value (0, for example) and
  46. * increases it every time a new block is enciphered. This naturally
  47. * leaves to a limitation on the number q of blocks that can be
  48. * enciphered using a same key: q < 2^(counter size).
  49. *
  50. * TinyCrypt uses a counter of 32 bits. This means that after 2^32
  51. * block encryptions, the counter will be reused (thus losing CBC
  52. * security). 2^32 block encryptions should be enough for most of
  53. * applications targeting constrained devices. Applications intended
  54. * to encrypt a larger number of blocks must replace the key after
  55. * 2^32 block encryptions.
  56. *
  57. * CTR mode provides NO data integrity.
  58. *
  59. * Requires: AES-128
  60. *
  61. * Usage: 1) call tc_ctr_mode to process the data to encrypt/decrypt.
  62. *
  63. */
  64. #ifndef __TC_CTR_MODE_H__
  65. #define __TC_CTR_MODE_H__
  66. #include <tinycrypt/aes.h>
  67. #include <tinycrypt/constants.h>
  68. #ifdef __cplusplus
  69. extern "C" {
  70. #endif
  71. /**
  72. * @brief CTR mode encryption/decryption procedure.
  73. * CTR mode encrypts (or decrypts) inlen bytes from in buffer into out buffer
  74. * @return returns TC_CRYPTO_SUCCESS (1)
  75. * returns TC_CRYPTO_FAIL (0) if:
  76. * out == NULL or
  77. * in == NULL or
  78. * ctr == NULL or
  79. * sched == NULL or
  80. * inlen == 0 or
  81. * outlen == 0 or
  82. * inlen != outlen
  83. * @note Assumes:- The current value in ctr has NOT been used with sched
  84. * - out points to inlen bytes
  85. * - in points to inlen bytes
  86. * - ctr is an integer counter in littleEndian format
  87. * - sched was initialized by aes_set_encrypt_key
  88. * @param out OUT -- produced ciphertext (plaintext)
  89. * @param outlen IN -- length of ciphertext buffer in bytes
  90. * @param in IN -- data to encrypt (or decrypt)
  91. * @param inlen IN -- length of input data in bytes
  92. * @param ctr IN/OUT -- the current counter value
  93. * @param sched IN -- an initialized AES key schedule
  94. */
  95. int tc_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
  96. unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif /* __TC_CTR_MODE_H__ */