blockwise.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef BLOCKWISE_H
  15. #define BLOCKWISE_H
  16. #include <stdint.h>
  17. #include <stddef.h>
  18. /* Processing function for cf_blockwise_accumulate. */
  19. typedef void (*cf_blockwise_in_fn)(void *ctx, const uint8_t *data);
  20. /* Processing function for cf_blockwise_xor. */
  21. typedef void (*cf_blockwise_out_fn)(void *ctx, uint8_t *data);
  22. /* This function manages the common abstraction of accumulating input in
  23. * a buffer, and processing it when a full block is available.
  24. *
  25. * partial is the buffer (maintained by the caller)
  26. * on entry, npartial is the currently valid count of used bytes on
  27. * the front of partial.
  28. * on exit, npartial is updated to reflect the status of partial.
  29. * nblock is the blocksize to accumulate -- partial must be at least
  30. * this long!
  31. * input is the new data to process, of length nbytes.
  32. * process is the processing function, passed ctx and a pointer
  33. * to the data to process (always exactly nblock bytes long!)
  34. * which may not neccessarily be the same as partial.
  35. */
  36. void cf_blockwise_accumulate(uint8_t *partial, size_t *npartial,
  37. size_t nblock,
  38. const void *input, size_t nbytes,
  39. cf_blockwise_in_fn process,
  40. void *ctx);
  41. /* This function manages the common abstraction of accumulating input in
  42. * a buffer, and processing it when a full block is available.
  43. * This version supports calling a different processing function for
  44. * the last block.
  45. *
  46. * partial is the buffer (maintained by the caller)
  47. * on entry, npartial is the currently valid count of used bytes on
  48. * the front of partial.
  49. * on exit, npartial is updated to reflect the status of partial.
  50. * nblock is the blocksize to accumulate -- partial must be at least
  51. * this long!
  52. * input is the new data to process, of length nbytes.
  53. * process is the processing function, passed ctx and a pointer
  54. * to the data to process (always exactly nblock bytes long!)
  55. * which may not neccessarily be the same as partial.
  56. * process_final is called last (but may not be called at all if
  57. * all input is buffered).
  58. */
  59. void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial,
  60. size_t nblock,
  61. const void *input, size_t nbytes,
  62. cf_blockwise_in_fn process,
  63. cf_blockwise_in_fn process_final,
  64. void *ctx);
  65. /* This function manages XORing an input stream with a keystream
  66. * to produce an output stream. The keystream is produced in blocks
  67. * (ala a block cipher in counter mode).
  68. *
  69. * partial is the keystream buffer (maintained by the caller)
  70. * on entry, *npartial is the currently valid count of bytes in partial:
  71. * unused bytes are at the *end*. So *npartial = 4 means the last four
  72. * bytes of partial are usable as keystream.
  73. * on exit, npartial is updated to reflect the new state of partial.
  74. * nblock is the blocksize to accumulate -- partial must be at least
  75. * this long!
  76. * input is the new data to process, of length nbytes.
  77. * output is where to write input xored with the keystream -- also length
  78. * nbytes.
  79. * process is the processing function, passed ctx and partial which it
  80. * should fill with fresh key stream.
  81. */
  82. void cf_blockwise_xor(uint8_t *partial, size_t *npartial,
  83. size_t nblock,
  84. const void *input, void *output, size_t nbytes,
  85. cf_blockwise_out_fn newblock,
  86. void *ctx);
  87. /* This function processes a single byte a number of times. It's useful
  88. * for padding, and more efficient than calling cf_blockwise_accumulate
  89. * a bunch of times.
  90. *
  91. * partial is the buffer (maintained by the caller)
  92. * on entry, npartial is the currently valid count of used bytes on
  93. * the front of partial.
  94. * on exit, npartial is updated to reflect the status of partial.
  95. * nblock is the blocksize to accumulate -- partial must be at least
  96. * this long!
  97. * process is the processing function, passed ctx and a pointer
  98. * to the data to process (always exactly nblock bytes long!)
  99. * which may not neccessarily be the same as partial.
  100. * byte is the byte to process, nbytes times.
  101. */
  102. void cf_blockwise_acc_byte(uint8_t *partial, size_t *npartial,
  103. size_t nblock,
  104. uint8_t byte, size_t nbytes,
  105. cf_blockwise_in_fn process,
  106. void *ctx);
  107. /* This function attempts to process patterns of bytes common in
  108. * block cipher padding.
  109. *
  110. * This takes three bytes:
  111. * - a first byte, fbyte,
  112. * - a middle byte, mbyte,
  113. * - a last byte, lbyte.
  114. *
  115. * If nbytes is zero, nothing happens.
  116. * If nbytes is one, the byte fbyte ^ lbyte is processed.
  117. * If nbytes is two, the fbyte then lbyte are processed.
  118. * If nbytes is three or more, fbyte, then one or more mbytes, then fbyte
  119. * is processed.
  120. *
  121. * partial is the buffer (maintained by the caller)
  122. * on entry, npartial is the currently valid count of used bytes on
  123. * the front of partial.
  124. * on exit, npartial is updated to reflect the status of partial.
  125. * nblock is the blocksize to accumulate -- partial must be at least
  126. * this long!
  127. * process is the processing function, passed ctx and a pointer
  128. * to the data to process (always exactly nblock bytes long!)
  129. * which may not neccessarily be the same as partial.
  130. */
  131. void cf_blockwise_acc_pad(uint8_t *partial, size_t *npartial,
  132. size_t nblock,
  133. uint8_t fbyte, uint8_t mbyte, uint8_t lbyte,
  134. size_t nbytes,
  135. cf_blockwise_in_fn process,
  136. void *ctx);
  137. #endif