sig.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Public Key Signature Algorithm
  4. *
  5. * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_SIG_H
  8. #define _CRYPTO_SIG_H
  9. #include <linux/crypto.h>
  10. /**
  11. * struct crypto_sig - user-instantiated objects which encapsulate
  12. * algorithms and core processing logic
  13. *
  14. * @base: Common crypto API algorithm data structure
  15. */
  16. struct crypto_sig {
  17. struct crypto_tfm base;
  18. };
  19. /**
  20. * struct sig_alg - generic public key signature algorithm
  21. *
  22. * @sign: Function performs a sign operation as defined by public key
  23. * algorithm. On success, the signature size is returned.
  24. * Optional.
  25. * @verify: Function performs a complete verify operation as defined by
  26. * public key algorithm, returning verification status. Optional.
  27. * @set_pub_key: Function invokes the algorithm specific set public key
  28. * function, which knows how to decode and interpret
  29. * the BER encoded public key and parameters. Mandatory.
  30. * @set_priv_key: Function invokes the algorithm specific set private key
  31. * function, which knows how to decode and interpret
  32. * the BER encoded private key and parameters. Optional.
  33. * @key_size: Function returns key size. Mandatory.
  34. * @digest_size: Function returns maximum digest size. Optional.
  35. * @max_size: Function returns maximum signature size. Optional.
  36. * @init: Initialize the cryptographic transformation object.
  37. * This function is used to initialize the cryptographic
  38. * transformation object. This function is called only once at
  39. * the instantiation time, right after the transformation context
  40. * was allocated. In case the cryptographic hardware has some
  41. * special requirements which need to be handled by software, this
  42. * function shall check for the precise requirement of the
  43. * transformation and put any software fallbacks in place.
  44. * @exit: Deinitialize the cryptographic transformation object. This is a
  45. * counterpart to @init, used to remove various changes set in
  46. * @init.
  47. *
  48. * @base: Common crypto API algorithm data structure
  49. */
  50. struct sig_alg {
  51. int (*sign)(struct crypto_sig *tfm,
  52. const void *src, unsigned int slen,
  53. void *dst, unsigned int dlen);
  54. int (*verify)(struct crypto_sig *tfm,
  55. const void *src, unsigned int slen,
  56. const void *digest, unsigned int dlen);
  57. int (*set_pub_key)(struct crypto_sig *tfm,
  58. const void *key, unsigned int keylen);
  59. int (*set_priv_key)(struct crypto_sig *tfm,
  60. const void *key, unsigned int keylen);
  61. unsigned int (*key_size)(struct crypto_sig *tfm);
  62. unsigned int (*digest_size)(struct crypto_sig *tfm);
  63. unsigned int (*max_size)(struct crypto_sig *tfm);
  64. int (*init)(struct crypto_sig *tfm);
  65. void (*exit)(struct crypto_sig *tfm);
  66. struct crypto_alg base;
  67. };
  68. /**
  69. * DOC: Generic Public Key Signature API
  70. *
  71. * The Public Key Signature API is used with the algorithms of type
  72. * CRYPTO_ALG_TYPE_SIG (listed as type "sig" in /proc/crypto)
  73. */
  74. /**
  75. * crypto_alloc_sig() - allocate signature tfm handle
  76. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  77. * signing algorithm e.g. "ecdsa"
  78. * @type: specifies the type of the algorithm
  79. * @mask: specifies the mask for the algorithm
  80. *
  81. * Allocate a handle for public key signature algorithm. The returned struct
  82. * crypto_sig is the handle that is required for any subsequent
  83. * API invocation for signature operations.
  84. *
  85. * Return: allocated handle in case of success; IS_ERR() is true in case
  86. * of an error, PTR_ERR() returns the error code.
  87. */
  88. struct crypto_sig *crypto_alloc_sig(const char *alg_name, u32 type, u32 mask);
  89. static inline struct crypto_tfm *crypto_sig_tfm(struct crypto_sig *tfm)
  90. {
  91. return &tfm->base;
  92. }
  93. static inline struct crypto_sig *__crypto_sig_tfm(struct crypto_tfm *tfm)
  94. {
  95. return container_of(tfm, struct crypto_sig, base);
  96. }
  97. static inline struct sig_alg *__crypto_sig_alg(struct crypto_alg *alg)
  98. {
  99. return container_of(alg, struct sig_alg, base);
  100. }
  101. static inline struct sig_alg *crypto_sig_alg(struct crypto_sig *tfm)
  102. {
  103. return __crypto_sig_alg(crypto_sig_tfm(tfm)->__crt_alg);
  104. }
  105. /**
  106. * crypto_free_sig() - free signature tfm handle
  107. *
  108. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  109. *
  110. * If @tfm is a NULL or error pointer, this function does nothing.
  111. */
  112. static inline void crypto_free_sig(struct crypto_sig *tfm)
  113. {
  114. crypto_destroy_tfm(tfm, crypto_sig_tfm(tfm));
  115. }
  116. /**
  117. * crypto_sig_keysize() - Get key size
  118. *
  119. * Function returns the key size in bits.
  120. * Function assumes that the key is already set in the transformation. If this
  121. * function is called without a setkey or with a failed setkey, you may end up
  122. * in a NULL dereference.
  123. *
  124. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  125. */
  126. static inline unsigned int crypto_sig_keysize(struct crypto_sig *tfm)
  127. {
  128. struct sig_alg *alg = crypto_sig_alg(tfm);
  129. return alg->key_size(tfm);
  130. }
  131. /**
  132. * crypto_sig_digestsize() - Get maximum digest size
  133. *
  134. * Function returns the maximum digest size in bytes.
  135. * Function assumes that the key is already set in the transformation. If this
  136. * function is called without a setkey or with a failed setkey, you may end up
  137. * in a NULL dereference.
  138. *
  139. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  140. */
  141. static inline unsigned int crypto_sig_digestsize(struct crypto_sig *tfm)
  142. {
  143. struct sig_alg *alg = crypto_sig_alg(tfm);
  144. return alg->digest_size(tfm);
  145. }
  146. /**
  147. * crypto_sig_maxsize() - Get maximum signature size
  148. *
  149. * Function returns the maximum signature size in bytes.
  150. * Function assumes that the key is already set in the transformation. If this
  151. * function is called without a setkey or with a failed setkey, you may end up
  152. * in a NULL dereference.
  153. *
  154. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  155. */
  156. static inline unsigned int crypto_sig_maxsize(struct crypto_sig *tfm)
  157. {
  158. struct sig_alg *alg = crypto_sig_alg(tfm);
  159. return alg->max_size(tfm);
  160. }
  161. /**
  162. * crypto_sig_sign() - Invoke signing operation
  163. *
  164. * Function invokes the specific signing operation for a given algorithm
  165. *
  166. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  167. * @src: source buffer
  168. * @slen: source length
  169. * @dst: destination obuffer
  170. * @dlen: destination length
  171. *
  172. * Return: signature size on success; error code in case of error
  173. */
  174. static inline int crypto_sig_sign(struct crypto_sig *tfm,
  175. const void *src, unsigned int slen,
  176. void *dst, unsigned int dlen)
  177. {
  178. struct sig_alg *alg = crypto_sig_alg(tfm);
  179. return alg->sign(tfm, src, slen, dst, dlen);
  180. }
  181. /**
  182. * crypto_sig_verify() - Invoke signature verification
  183. *
  184. * Function invokes the specific signature verification operation
  185. * for a given algorithm.
  186. *
  187. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  188. * @src: source buffer
  189. * @slen: source length
  190. * @digest: digest
  191. * @dlen: digest length
  192. *
  193. * Return: zero on verification success; error code in case of error.
  194. */
  195. static inline int crypto_sig_verify(struct crypto_sig *tfm,
  196. const void *src, unsigned int slen,
  197. const void *digest, unsigned int dlen)
  198. {
  199. struct sig_alg *alg = crypto_sig_alg(tfm);
  200. return alg->verify(tfm, src, slen, digest, dlen);
  201. }
  202. /**
  203. * crypto_sig_set_pubkey() - Invoke set public key operation
  204. *
  205. * Function invokes the algorithm specific set key function, which knows
  206. * how to decode and interpret the encoded key and parameters
  207. *
  208. * @tfm: tfm handle
  209. * @key: BER encoded public key, algo OID, paramlen, BER encoded
  210. * parameters
  211. * @keylen: length of the key (not including other data)
  212. *
  213. * Return: zero on success; error code in case of error
  214. */
  215. static inline int crypto_sig_set_pubkey(struct crypto_sig *tfm,
  216. const void *key, unsigned int keylen)
  217. {
  218. struct sig_alg *alg = crypto_sig_alg(tfm);
  219. return alg->set_pub_key(tfm, key, keylen);
  220. }
  221. /**
  222. * crypto_sig_set_privkey() - Invoke set private key operation
  223. *
  224. * Function invokes the algorithm specific set key function, which knows
  225. * how to decode and interpret the encoded key and parameters
  226. *
  227. * @tfm: tfm handle
  228. * @key: BER encoded private key, algo OID, paramlen, BER encoded
  229. * parameters
  230. * @keylen: length of the key (not including other data)
  231. *
  232. * Return: zero on success; error code in case of error
  233. */
  234. static inline int crypto_sig_set_privkey(struct crypto_sig *tfm,
  235. const void *key, unsigned int keylen)
  236. {
  237. struct sig_alg *alg = crypto_sig_alg(tfm);
  238. return alg->set_priv_key(tfm, key, keylen);
  239. }
  240. #endif