cifra_eax_aes.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. /**
  15. * The AES block cipher
  16. * ====================
  17. *
  18. * This is a small, simple implementation of AES. Key expansion is done
  19. * first, filling in a :c:type:`cf_aes_context`. Then encryption and
  20. * decryption can be performed as desired.
  21. *
  22. * Usually you don't want to use AES directly; you should use it via
  23. * a :doc:`block cipher mode <modes>`.
  24. */
  25. #ifndef AES_H
  26. #define AES_H
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include "prp.h"
  30. /* .. c:macro:: AES_BLOCKSZ
  31. * AES has a 128-bit block size. This quantity is in bytes.
  32. */
  33. #define AES_BLOCKSZ 16
  34. /* --- Size configuration --- */
  35. /* .. c:macro:: AES128_ROUNDS
  36. * .. c:macro:: AES192_ROUNDS
  37. * .. c:macro:: AES256_ROUNDS
  38. *
  39. * Round counts for different key sizes.
  40. */
  41. #define AES128_ROUNDS 10
  42. #define AES192_ROUNDS 12
  43. #define AES256_ROUNDS 14
  44. /* .. c:macro:: CF_AES_MAXROUNDS
  45. *
  46. * You can reduce the maximum number of rounds this implementation
  47. * supports. This reduces the storage needed by :c:type:`cf_aes_context`.
  48. *
  49. * The default is :c:macro:`AES256_ROUNDS` and is good for all key
  50. * sizes.
  51. */
  52. #ifndef CF_AES_MAXROUNDS
  53. # define CF_AES_MAXROUNDS AES256_ROUNDS
  54. #endif
  55. /* .. c:macro:: CF_AES_ENCRYPT_ONLY
  56. *
  57. * Define this to 1 if you don't need to decrypt anything.
  58. * This saves space. :c:func:`cf_aes_decrypt` calls `abort(3)`.
  59. */
  60. #ifndef CF_AES_ENCRYPT_ONLY
  61. # define CF_AES_ENCRYPT_ONLY 0
  62. #endif
  63. /* .. c:type:: cf_aes_context
  64. * This type represents an expanded AES key. Create one
  65. * using :c:func:`cf_aes_init`, make use of one using
  66. * :c:func:`cf_aes_encrypt` or :c:func:`cf_aes_decrypt`.
  67. *
  68. * The contents of this structure are equivalent to the
  69. * original key material. You should clean the
  70. * contents of this structure with :c:func:`cf_aes_finish`
  71. * when you're done.
  72. *
  73. * .. c:member:: cf_aes_context.rounds
  74. *
  75. * Number of rounds to use, set by :c:func:`cf_aes_init`.
  76. *
  77. * This depends on the original key size, and will be
  78. * :c:macro:`AES128_ROUNDS`, :c:macro:`AES192_ROUNDS` or
  79. * :c:macro:`AES256_ROUNDS`.
  80. *
  81. * .. c:member:: cf_aes_context.ks
  82. *
  83. * Expanded key material. Filled in by :c:func:`cf_aes_init`.
  84. */
  85. typedef struct
  86. {
  87. uint32_t rounds;
  88. uint32_t ks[AES_BLOCKSZ / 4 * (CF_AES_MAXROUNDS + 1)];
  89. } cf_aes_context;
  90. /* .. c:function:: $DECL
  91. * This function does AES key expansion. It destroys
  92. * existing contents of :c:data:`ctx`.
  93. *
  94. * :param ctx: expanded key context, filled in by this function.
  95. * :param key: pointer to key material, of :c:data:`nkey` bytes.
  96. * :param nkey: length of key material. Must be `16`, `24` or `32`.
  97. */
  98. extern void cf_aes_init(cf_aes_context *ctx,
  99. const uint8_t *key,
  100. size_t nkey);
  101. /* .. c:function:: $DECL
  102. * Encrypts the given block, from :c:data:`in` to :c:data:`out`.
  103. * These may alias.
  104. *
  105. * Fails at runtime if :c:data:`ctx` is invalid.
  106. *
  107. * :param ctx: expanded key context
  108. * :param in: input block (read)
  109. * :param out: output block (written)
  110. */
  111. extern void cf_aes_encrypt(const cf_aes_context *ctx,
  112. const uint8_t in[AES_BLOCKSZ],
  113. uint8_t out[AES_BLOCKSZ]);
  114. /* .. c:function:: $DECL
  115. * Decrypts the given block, from :c:data:`in` to :c:data:`out`.
  116. * These may alias.
  117. *
  118. * Fails at runtime if :c:data:`ctx` is invalid.
  119. *
  120. * :param ctx: expanded key context
  121. * :param in: input block (read)
  122. * :param out: output block (written)
  123. */
  124. extern void cf_aes_decrypt(const cf_aes_context *ctx,
  125. const uint8_t in[AES_BLOCKSZ],
  126. uint8_t out[AES_BLOCKSZ]);
  127. /* .. c:function:: $DECL
  128. * Erase scheduled key material.
  129. *
  130. * Call this when you're done to erase the round keys. */
  131. extern void cf_aes_finish(cf_aes_context *ctx);
  132. /* .. c:var:: const cf_prp cf_aes
  133. * Abstract interface to AES. See :c:type:`cf_prp` for
  134. * more information. */
  135. extern const cf_prp cf_aes;
  136. #endif