ctr_mode.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* ctr_mode.c - TinyCrypt CTR 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. #include <tinycrypt/constants.h>
  32. #include <tinycrypt/ctr_mode.h>
  33. #include <tinycrypt/utils.h>
  34. int tc_ctr_mode(uint8_t *out, unsigned int outlen, const uint8_t *in,
  35. unsigned int inlen, uint8_t *ctr, const TCAesKeySched_t sched)
  36. {
  37. uint8_t buffer[TC_AES_BLOCK_SIZE];
  38. uint8_t nonce[TC_AES_BLOCK_SIZE];
  39. unsigned int block_num;
  40. unsigned int i;
  41. /* input sanity check: */
  42. if (out == (uint8_t *) 0 ||
  43. in == (uint8_t *) 0 ||
  44. ctr == (uint8_t *) 0 ||
  45. sched == (TCAesKeySched_t) 0 ||
  46. inlen == 0 ||
  47. outlen == 0 ||
  48. outlen != inlen) {
  49. return TC_CRYPTO_FAIL;
  50. }
  51. /* copy the ctr to the nonce */
  52. (void)_copy(nonce, sizeof(nonce), ctr, sizeof(nonce));
  53. /* select the last 4 bytes of the nonce to be incremented */
  54. block_num = (nonce[12] << 24) | (nonce[13] << 16) |
  55. (nonce[14] << 8) | (nonce[15]);
  56. for (i = 0; i < inlen; ++i) {
  57. if ((i % (TC_AES_BLOCK_SIZE)) == 0) {
  58. /* encrypt data using the current nonce */
  59. if (tc_aes_encrypt(buffer, nonce, sched)) {
  60. block_num++;
  61. nonce[12] = (uint8_t)(block_num >> 24);
  62. nonce[13] = (uint8_t)(block_num >> 16);
  63. nonce[14] = (uint8_t)(block_num >> 8);
  64. nonce[15] = (uint8_t)(block_num);
  65. } else {
  66. return TC_CRYPTO_FAIL;
  67. }
  68. }
  69. /* update the output */
  70. *out++ = buffer[i%(TC_AES_BLOCK_SIZE)] ^ *in++;
  71. }
  72. /* update the counter */
  73. ctr[12] = nonce[12]; ctr[13] = nonce[13];
  74. ctr[14] = nonce[14]; ctr[15] = nonce[15];
  75. return TC_CRYPTO_SUCCESS;
  76. }