crypto_aesctr.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <assert.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include "cpusupport.h"
  5. #include "crypto_aes.h"
  6. #include "crypto_aesctr_aesni.h"
  7. #include "crypto_aesctr_arm.h"
  8. #include "insecure_memzero.h"
  9. #include "sysendian.h"
  10. #include "crypto_aesctr.h"
  11. /**
  12. * In order to optimize AES-CTR, it is desirable to separate out the handling
  13. * of individual bytes of data vs. the handling of complete (16 byte) blocks.
  14. * The handling of blocks in turn can be optimized further using CPU
  15. * intrinsics, e.g. SSE2 on x86 CPUs; however while the byte-at-once code
  16. * remains the same across platforms it should be inlined into the same (CPU
  17. * feature specific) routines for performance reasons.
  18. *
  19. * In order to allow those generic functions to be inlined into multiple
  20. * functions in separate translation units, we place them into a "shared" C
  21. * file which is included in each of the platform-specific variants.
  22. */
  23. #include "crypto_aesctr_shared.c"
  24. #if defined(CPUSUPPORT_X86_AESNI) || defined(CPUSUPPORT_ARM_AES)
  25. #define HWACCEL
  26. static enum {
  27. HW_SOFTWARE = 0,
  28. #if defined(CPUSUPPORT_X86_AESNI)
  29. HW_X86_AESNI,
  30. #endif
  31. #if defined(CPUSUPPORT_ARM_AES)
  32. HW_ARM_AES,
  33. #endif
  34. HW_UNSET
  35. } hwaccel = HW_UNSET;
  36. #endif
  37. #ifdef HWACCEL
  38. /* Which type of hardware acceleration should we use, if any? */
  39. static void
  40. hwaccel_init(void)
  41. {
  42. /* If we've already set hwaccel, we're finished. */
  43. if (hwaccel != HW_UNSET)
  44. return;
  45. /* Default to software. */
  46. hwaccel = HW_SOFTWARE;
  47. /* Can we use AESNI? */
  48. switch (crypto_aes_can_use_intrinsics()) {
  49. #ifdef CPUSUPPORT_X86_AESNI
  50. case 1:
  51. hwaccel = HW_X86_AESNI;
  52. break;
  53. #endif
  54. #ifdef CPUSUPPORT_ARM_AES
  55. case 2:
  56. hwaccel = HW_ARM_AES;
  57. break;
  58. #endif
  59. case 0:
  60. break;
  61. default:
  62. /* Should never happen. */
  63. assert(0);
  64. }
  65. }
  66. #endif /* HWACCEL */
  67. /**
  68. * crypto_aesctr_alloc(void):
  69. * Allocate an object for performing AES in CTR code. This must be followed
  70. * by calling _init2().
  71. */
  72. struct crypto_aesctr *
  73. crypto_aesctr_alloc(void)
  74. {
  75. struct crypto_aesctr * stream;
  76. /* Allocate memory. */
  77. if ((stream = malloc(sizeof(struct crypto_aesctr))) == NULL)
  78. goto err0;
  79. /* Success! */
  80. return (stream);
  81. err0:
  82. /* Failure! */
  83. return (NULL);
  84. }
  85. /**
  86. * crypto_aesctr_init2(stream, key, nonce):
  87. * Reset the AES-CTR stream ${stream}, using the ${key} and ${nonce}. If ${key}
  88. * is NULL, retain the previous AES key.
  89. */
  90. void
  91. crypto_aesctr_init2(struct crypto_aesctr * stream,
  92. const struct crypto_aes_key * key, uint64_t nonce)
  93. {
  94. /* If the key is NULL, retain the previous AES key. */
  95. if (key != NULL)
  96. stream->key = key;
  97. /* Set nonce as provided and reset bytectr. */
  98. be64enc(stream->pblk, nonce);
  99. stream->bytectr = 0;
  100. /*
  101. * Set the counter such that the least significant byte will wrap once
  102. * incremented.
  103. */
  104. stream->pblk[15] = 0xff;
  105. #ifdef HWACCEL
  106. hwaccel_init();
  107. #endif
  108. /* Sanity check. */
  109. assert(stream->key != NULL);
  110. }
  111. /**
  112. * crypto_aesctr_init(key, nonce):
  113. * Prepare to encrypt/decrypt data with AES in CTR mode, using the provided
  114. * expanded ${key} and ${nonce}. The key provided must remain valid for the
  115. * lifetime of the stream. This is the same as calling _alloc() followed by
  116. * _init2().
  117. */
  118. struct crypto_aesctr *
  119. crypto_aesctr_init(const struct crypto_aes_key * key, uint64_t nonce)
  120. {
  121. struct crypto_aesctr * stream;
  122. /* Sanity check. */
  123. assert(key != NULL);
  124. /* Allocate memory. */
  125. if ((stream = crypto_aesctr_alloc()) == NULL)
  126. goto err0;
  127. /* Initialize values. */
  128. crypto_aesctr_init2(stream, key, nonce);
  129. /* Success! */
  130. return (stream);
  131. err0:
  132. /* Failure! */
  133. return (NULL);
  134. }
  135. /**
  136. * crypto_aesctr_stream(stream, inbuf, outbuf, buflen):
  137. * Generate the next ${buflen} bytes of the AES-CTR stream ${stream} and xor
  138. * them with bytes from ${inbuf}, writing the result into ${outbuf}. If the
  139. * buffers ${inbuf} and ${outbuf} overlap, they must be identical.
  140. */
  141. void
  142. crypto_aesctr_stream(struct crypto_aesctr * stream, const uint8_t * inbuf,
  143. uint8_t * outbuf, size_t buflen)
  144. {
  145. #if defined(HWACCEL)
  146. #if defined(CPUSUPPORT_X86_AESNI)
  147. if ((buflen >= 16) && (hwaccel == HW_X86_AESNI)) {
  148. crypto_aesctr_aesni_stream(stream, inbuf, outbuf, buflen);
  149. return;
  150. }
  151. #endif
  152. #if defined(CPUSUPPORT_ARM_AES)
  153. if ((buflen >= 16) && (hwaccel == HW_ARM_AES)) {
  154. crypto_aesctr_arm_stream(stream, inbuf, outbuf, buflen);
  155. return;
  156. }
  157. #endif
  158. #endif /* HWACCEL */
  159. /* Process any bytes before we can process a whole block. */
  160. if (crypto_aesctr_stream_pre_wholeblock(stream, &inbuf, &outbuf,
  161. &buflen))
  162. return;
  163. /* Process whole blocks of 16 bytes. */
  164. while (buflen >= 16) {
  165. /* Generate a block of cipherstream. */
  166. crypto_aesctr_stream_cipherblock_generate(stream);
  167. /* Encrypt the bytes and update the positions. */
  168. crypto_aesctr_stream_cipherblock_use(stream, &inbuf, &outbuf,
  169. &buflen, 16, 0);
  170. }
  171. /* Process any final bytes after finishing all whole blocks. */
  172. crypto_aesctr_stream_post_wholeblock(stream, &inbuf, &outbuf, &buflen);
  173. }
  174. /**
  175. * crypto_aesctr_free(stream):
  176. * Free the AES-CTR stream ${stream}.
  177. */
  178. void
  179. crypto_aesctr_free(struct crypto_aesctr * stream)
  180. {
  181. /* Behave consistently with free(NULL). */
  182. if (stream == NULL)
  183. return;
  184. /* Zero potentially sensitive information. */
  185. insecure_memzero(stream, sizeof(struct crypto_aesctr));
  186. /* Free the stream. */
  187. free(stream);
  188. }
  189. /**
  190. * crypto_aesctr_buf(key, nonce, inbuf, outbuf, buflen):
  191. * Equivalent to _init(key, nonce); _stream(inbuf, outbuf, buflen); _free().
  192. */
  193. void
  194. crypto_aesctr_buf(const struct crypto_aes_key * key, uint64_t nonce,
  195. const uint8_t * inbuf, uint8_t * outbuf, size_t buflen)
  196. {
  197. struct crypto_aesctr stream_rec;
  198. struct crypto_aesctr * stream = &stream_rec;
  199. /* Sanity check. */
  200. assert(key != NULL);
  201. /* Initialize values. */
  202. crypto_aesctr_init2(stream, key, nonce);
  203. /* Perform the encryption. */
  204. crypto_aesctr_stream(stream, inbuf, outbuf, buflen);
  205. /* Zero potentially sensitive information. */
  206. insecure_memzero(stream, sizeof(struct crypto_aesctr));
  207. }