sig.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * DOC: Generic Public Key Signature API
  21. *
  22. * The Public Key Signature API is used with the algorithms of type
  23. * CRYPTO_ALG_TYPE_SIG (listed as type "sig" in /proc/crypto)
  24. */
  25. /**
  26. * crypto_alloc_sig() - allocate signature tfm handle
  27. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  28. * signing algorithm e.g. "ecdsa"
  29. * @type: specifies the type of the algorithm
  30. * @mask: specifies the mask for the algorithm
  31. *
  32. * Allocate a handle for public key signature algorithm. The returned struct
  33. * crypto_sig is the handle that is required for any subsequent
  34. * API invocation for signature operations.
  35. *
  36. * Return: allocated handle in case of success; IS_ERR() is true in case
  37. * of an error, PTR_ERR() returns the error code.
  38. */
  39. struct crypto_sig *crypto_alloc_sig(const char *alg_name, u32 type, u32 mask);
  40. static inline struct crypto_tfm *crypto_sig_tfm(struct crypto_sig *tfm)
  41. {
  42. return &tfm->base;
  43. }
  44. /**
  45. * crypto_free_sig() - free signature tfm handle
  46. *
  47. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  48. *
  49. * If @tfm is a NULL or error pointer, this function does nothing.
  50. */
  51. static inline void crypto_free_sig(struct crypto_sig *tfm)
  52. {
  53. crypto_destroy_tfm(tfm, crypto_sig_tfm(tfm));
  54. }
  55. /**
  56. * crypto_sig_maxsize() - Get len for output buffer
  57. *
  58. * Function returns the dest buffer size required for a given key.
  59. * Function assumes that the key is already set in the transformation. If this
  60. * function is called without a setkey or with a failed setkey, you will end up
  61. * in a NULL dereference.
  62. *
  63. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  64. */
  65. int crypto_sig_maxsize(struct crypto_sig *tfm);
  66. /**
  67. * crypto_sig_sign() - Invoke signing operation
  68. *
  69. * Function invokes the specific signing operation for a given algorithm
  70. *
  71. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  72. * @src: source buffer
  73. * @slen: source length
  74. * @dst: destination obuffer
  75. * @dlen: destination length
  76. *
  77. * Return: zero on success; error code in case of error
  78. */
  79. int crypto_sig_sign(struct crypto_sig *tfm,
  80. const void *src, unsigned int slen,
  81. void *dst, unsigned int dlen);
  82. /**
  83. * crypto_sig_verify() - Invoke signature verification
  84. *
  85. * Function invokes the specific signature verification operation
  86. * for a given algorithm.
  87. *
  88. * @tfm: signature tfm handle allocated with crypto_alloc_sig()
  89. * @src: source buffer
  90. * @slen: source length
  91. * @digest: digest
  92. * @dlen: digest length
  93. *
  94. * Return: zero on verification success; error code in case of error.
  95. */
  96. int crypto_sig_verify(struct crypto_sig *tfm,
  97. const void *src, unsigned int slen,
  98. const void *digest, unsigned int dlen);
  99. /**
  100. * crypto_sig_set_pubkey() - Invoke set public key operation
  101. *
  102. * Function invokes the algorithm specific set key function, which knows
  103. * how to decode and interpret the encoded key and parameters
  104. *
  105. * @tfm: tfm handle
  106. * @key: BER encoded public key, algo OID, paramlen, BER encoded
  107. * parameters
  108. * @keylen: length of the key (not including other data)
  109. *
  110. * Return: zero on success; error code in case of error
  111. */
  112. int crypto_sig_set_pubkey(struct crypto_sig *tfm,
  113. const void *key, unsigned int keylen);
  114. /**
  115. * crypto_sig_set_privkey() - Invoke set private key operation
  116. *
  117. * Function invokes the algorithm specific set key function, which knows
  118. * how to decode and interpret the encoded key and parameters
  119. *
  120. * @tfm: tfm handle
  121. * @key: BER encoded private key, algo OID, paramlen, BER encoded
  122. * parameters
  123. * @keylen: length of the key (not including other data)
  124. *
  125. * Return: zero on success; error code in case of error
  126. */
  127. int crypto_sig_set_privkey(struct crypto_sig *tfm,
  128. const void *key, unsigned int keylen);
  129. #endif