aes.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* aes.h - TinyCrypt interface to an AES-128 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 an AES-128 implementation.
  34. *
  35. * Overview: AES-128 is a NIST approved block cipher specified in
  36. * FIPS 197. Block ciphers are deterministic algorithms that
  37. * perform a transformation specified by a symmetric key in fixed-
  38. * length data sets, also called blocks.
  39. *
  40. * Security: AES-128 provides approximately 128 bits of security.
  41. *
  42. * Usage: 1) call tc_aes128_set_encrypt/decrypt_key to set the key.
  43. *
  44. * 2) call tc_aes_encrypt/decrypt to process the data.
  45. */
  46. #ifndef __TC_AES_H__
  47. #define __TC_AES_H__
  48. #include <stdint.h>
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. #define Nb (4) /* number of columns (32-bit words) comprising the state */
  53. #define Nk (4) /* number of 32-bit words comprising the key */
  54. #define Nr (10) /* number of rounds */
  55. #define TC_AES_BLOCK_SIZE (Nb*Nk)
  56. #define TC_AES_KEY_SIZE (Nb*Nk)
  57. typedef struct tc_aes_key_sched_struct {
  58. unsigned int words[Nb*(Nr+1)];
  59. } *TCAesKeySched_t;
  60. /**
  61. * @brief Set AES-128 encryption key
  62. * Uses key k to initialize s
  63. * @return returns TC_CRYPTO_SUCCESS (1)
  64. * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
  65. * @note This implementation skips the additional steps required for keys
  66. * larger than 128 bits, and must not be used for AES-192 or
  67. * AES-256 key schedule -- see FIPS 197 for details
  68. * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
  69. * @param k IN -- points to the AES key
  70. */
  71. int tc_aes128_set_encrypt_key(TCAesKeySched_t s, const uint8_t *k);
  72. /**
  73. * @brief AES-128 Encryption procedure
  74. * Encrypts contents of in buffer into out buffer under key;
  75. * schedule s
  76. * @note Assumes s was initialized by aes_set_encrypt_key;
  77. * out and in point to 16 byte buffers
  78. * @return returns TC_CRYPTO_SUCCESS (1)
  79. * returns TC_CRYPTO_FAIL (0) if: out == NULL or in == NULL or s == NULL
  80. * @param out IN/OUT -- buffer to receive ciphertext block
  81. * @param in IN -- a plaintext block to encrypt
  82. * @param s IN -- initialized AES key schedule
  83. */
  84. int tc_aes_encrypt(uint8_t *out, const uint8_t *in,
  85. const TCAesKeySched_t s);
  86. /**
  87. * @brief Set the AES-128 decryption key
  88. * Uses key k to initialize s
  89. * @return returns TC_CRYPTO_SUCCESS (1)
  90. * returns TC_CRYPTO_FAIL (0) if: s == NULL or k == NULL
  91. * @note This is the implementation of the straightforward inverse cipher
  92. * using the cipher documented in FIPS-197 figure 12, not the
  93. * equivalent inverse cipher presented in Figure 15
  94. * @warning This routine skips the additional steps required for keys larger
  95. * than 128, and must not be used for AES-192 or AES-256 key
  96. * schedule -- see FIPS 197 for details
  97. * @param s IN/OUT -- initialized struct tc_aes_key_sched_struct
  98. * @param k IN -- points to the AES key
  99. */
  100. int tc_aes128_set_decrypt_key(TCAesKeySched_t s, const uint8_t *k);
  101. /**
  102. * @brief AES-128 Encryption procedure
  103. * Decrypts in buffer into out buffer under key schedule s
  104. * @return returns TC_CRYPTO_SUCCESS (1)
  105. * returns TC_CRYPTO_FAIL (0) if: out is NULL or in is NULL or s is NULL
  106. * @note Assumes s was initialized by aes_set_encrypt_key
  107. * out and in point to 16 byte buffers
  108. * @param out IN/OUT -- buffer to receive ciphertext block
  109. * @param in IN -- a plaintext block to encrypt
  110. * @param s IN -- initialized AES key schedule
  111. */
  112. int tc_aes_decrypt(uint8_t *out, const uint8_t *in,
  113. const TCAesKeySched_t s);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* __TC_AES_H__ */