devel-algos.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. Developing Cipher Algorithms
  2. ============================
  3. Registering And Unregistering Transformation
  4. --------------------------------------------
  5. There are three distinct types of registration functions in the Crypto
  6. API. One is used to register a generic cryptographic transformation,
  7. while the other two are specific to HASH transformations and
  8. COMPRESSion. We will discuss the latter two in a separate chapter, here
  9. we will only look at the generic ones.
  10. Before discussing the register functions, the data structure to be
  11. filled with each, struct crypto_alg, must be considered -- see below
  12. for a description of this data structure.
  13. The generic registration functions can be found in
  14. include/linux/crypto.h and their definition can be seen below. The
  15. former function registers a single transformation, while the latter
  16. works on an array of transformation descriptions. The latter is useful
  17. when registering transformations in bulk, for example when a driver
  18. implements multiple transformations.
  19. ::
  20. int crypto_register_alg(struct crypto_alg *alg);
  21. int crypto_register_algs(struct crypto_alg *algs, int count);
  22. The counterparts to those functions are listed below.
  23. ::
  24. int crypto_unregister_alg(struct crypto_alg *alg);
  25. int crypto_unregister_algs(struct crypto_alg *algs, int count);
  26. Notice that both registration and unregistration functions do return a
  27. value, so make sure to handle errors. A return code of zero implies
  28. success. Any return code < 0 implies an error.
  29. The bulk registration/unregistration functions register/unregister each
  30. transformation in the given array of length count. They handle errors as
  31. follows:
  32. - crypto_register_algs() succeeds if and only if it successfully
  33. registers all the given transformations. If an error occurs partway
  34. through, then it rolls back successful registrations before returning
  35. the error code. Note that if a driver needs to handle registration
  36. errors for individual transformations, then it will need to use the
  37. non-bulk function crypto_register_alg() instead.
  38. - crypto_unregister_algs() tries to unregister all the given
  39. transformations, continuing on error. It logs errors and always
  40. returns zero.
  41. Single-Block Symmetric Ciphers [CIPHER]
  42. ---------------------------------------
  43. Example of transformations: aes, arc4, ...
  44. This section describes the simplest of all transformation
  45. implementations, that being the CIPHER type used for symmetric ciphers.
  46. The CIPHER type is used for transformations which operate on exactly one
  47. block at a time and there are no dependencies between blocks at all.
  48. Registration specifics
  49. ~~~~~~~~~~~~~~~~~~~~~~
  50. The registration of [CIPHER] algorithm is specific in that struct
  51. crypto_alg field .cra_type is empty. The .cra_u.cipher has to be
  52. filled in with proper callbacks to implement this transformation.
  53. See struct cipher_alg below.
  54. Cipher Definition With struct cipher_alg
  55. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. Struct cipher_alg defines a single block cipher.
  57. Here are schematics of how these functions are called when operated from
  58. other part of the kernel. Note that the .cia_setkey() call might happen
  59. before or after any of these schematics happen, but must not happen
  60. during any of these are in-flight.
  61. ::
  62. KEY ---. PLAINTEXT ---.
  63. v v
  64. .cia_setkey() -> .cia_encrypt()
  65. |
  66. '-----> CIPHERTEXT
  67. Please note that a pattern where .cia_setkey() is called multiple times
  68. is also valid:
  69. ::
  70. KEY1 --. PLAINTEXT1 --. KEY2 --. PLAINTEXT2 --.
  71. v v v v
  72. .cia_setkey() -> .cia_encrypt() -> .cia_setkey() -> .cia_encrypt()
  73. | |
  74. '---> CIPHERTEXT1 '---> CIPHERTEXT2
  75. Multi-Block Ciphers
  76. -------------------
  77. Example of transformations: cbc(aes), ecb(arc4), ...
  78. This section describes the multi-block cipher transformation
  79. implementations. The multi-block ciphers are used for transformations
  80. which operate on scatterlists of data supplied to the transformation
  81. functions. They output the result into a scatterlist of data as well.
  82. Registration Specifics
  83. ~~~~~~~~~~~~~~~~~~~~~~
  84. The registration of multi-block cipher algorithms is one of the most
  85. standard procedures throughout the crypto API.
  86. Note, if a cipher implementation requires a proper alignment of data,
  87. the caller should use the functions of crypto_skcipher_alignmask() to
  88. identify a memory alignment mask. The kernel crypto API is able to
  89. process requests that are unaligned. This implies, however, additional
  90. overhead as the kernel crypto API needs to perform the realignment of
  91. the data which may imply moving of data.
  92. Cipher Definition With struct blkcipher_alg and ablkcipher_alg
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. Struct blkcipher_alg defines a synchronous block cipher whereas struct
  95. ablkcipher_alg defines an asynchronous block cipher.
  96. Please refer to the single block cipher description for schematics of
  97. the block cipher usage.
  98. Specifics Of Asynchronous Multi-Block Cipher
  99. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. There are a couple of specifics to the asynchronous interface.
  101. First of all, some of the drivers will want to use the Generic
  102. ScatterWalk in case the hardware needs to be fed separate chunks of the
  103. scatterlist which contains the plaintext and will contain the
  104. ciphertext. Please refer to the ScatterWalk interface offered by the
  105. Linux kernel scatter / gather list implementation.
  106. Hashing [HASH]
  107. --------------
  108. Example of transformations: crc32, md5, sha1, sha256,...
  109. Registering And Unregistering The Transformation
  110. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. There are multiple ways to register a HASH transformation, depending on
  112. whether the transformation is synchronous [SHASH] or asynchronous
  113. [AHASH] and the amount of HASH transformations we are registering. You
  114. can find the prototypes defined in include/crypto/internal/hash.h:
  115. ::
  116. int crypto_register_ahash(struct ahash_alg *alg);
  117. int crypto_register_shash(struct shash_alg *alg);
  118. int crypto_register_shashes(struct shash_alg *algs, int count);
  119. The respective counterparts for unregistering the HASH transformation
  120. are as follows:
  121. ::
  122. int crypto_unregister_ahash(struct ahash_alg *alg);
  123. int crypto_unregister_shash(struct shash_alg *alg);
  124. int crypto_unregister_shashes(struct shash_alg *algs, int count);
  125. Cipher Definition With struct shash_alg and ahash_alg
  126. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. Here are schematics of how these functions are called when operated from
  128. other part of the kernel. Note that the .setkey() call might happen
  129. before or after any of these schematics happen, but must not happen
  130. during any of these are in-flight. Please note that calling .init()
  131. followed immediately by .finish() is also a perfectly valid
  132. transformation.
  133. ::
  134. I) DATA -----------.
  135. v
  136. .init() -> .update() -> .final() ! .update() might not be called
  137. ^ | | at all in this scenario.
  138. '----' '---> HASH
  139. II) DATA -----------.-----------.
  140. v v
  141. .init() -> .update() -> .finup() ! .update() may not be called
  142. ^ | | at all in this scenario.
  143. '----' '---> HASH
  144. III) DATA -----------.
  145. v
  146. .digest() ! The entire process is handled
  147. | by the .digest() call.
  148. '---------------> HASH
  149. Here is a schematic of how the .export()/.import() functions are called
  150. when used from another part of the kernel.
  151. ::
  152. KEY--. DATA--.
  153. v v ! .update() may not be called
  154. .setkey() -> .init() -> .update() -> .export() at all in this scenario.
  155. ^ | |
  156. '-----' '--> PARTIAL_HASH
  157. ----------- other transformations happen here -----------
  158. PARTIAL_HASH--. DATA1--.
  159. v v
  160. .import -> .update() -> .final() ! .update() may not be called
  161. ^ | | at all in this scenario.
  162. '----' '--> HASH1
  163. PARTIAL_HASH--. DATA2-.
  164. v v
  165. .import -> .finup()
  166. |
  167. '---------------> HASH2
  168. Note that it is perfectly legal to "abandon" a request object:
  169. - call .init() and then (as many times) .update()
  170. - _not_ call any of .final(), .finup() or .export() at any point in future
  171. In other words implementations should mind the resource allocation and clean-up.
  172. No resources related to request objects should remain allocated after a call
  173. to .init() or .update(), since there might be no chance to free them.
  174. Specifics Of Asynchronous HASH Transformation
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. Some of the drivers will want to use the Generic ScatterWalk in case the
  177. implementation needs to be fed separate chunks of the scatterlist which
  178. contains the input data. The buffer containing the resulting hash will
  179. always be properly aligned to .cra_alignmask so there is no need to
  180. worry about this.