You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypto.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef _GPXE_CRYPTO_H
  2. #define _GPXE_CRYPTO_H
  3. /** @file
  4. *
  5. * Cryptographic API
  6. *
  7. */
  8. #include <stdint.h>
  9. #include <stddef.h>
  10. /** A cryptographic algorithm */
  11. struct crypto_algorithm {
  12. /** Algorithm name */
  13. const char *name;
  14. /** Context size */
  15. size_t ctxsize;
  16. /** Block size */
  17. size_t blocksize;
  18. /** Final output size */
  19. size_t digestsize;
  20. /** Initialise algorithm
  21. *
  22. * @v ctx Context
  23. */
  24. void ( * init ) ( void *ctx );
  25. /** Set key
  26. *
  27. * @v ctx Context
  28. * @v key Key
  29. * @v keylen Key length
  30. * @ret rc Return status code
  31. */
  32. int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
  33. /** Set initialisation vector
  34. *
  35. * @v ctx Context
  36. * @v iv Initialisation vector
  37. */
  38. void ( *setiv ) ( void *ctx, const void *iv );
  39. /** Encode data
  40. *
  41. * @v ctx Context
  42. * @v src Data to encode
  43. * @v dst Encoded data, or NULL
  44. * @v len Length of data
  45. * @ret rc Return status code
  46. *
  47. * For a cipher algorithm, the enciphered data should be
  48. * placed in @c dst. For a digest algorithm, only the digest
  49. * state should be updated, and @c dst will be NULL.
  50. *
  51. * @v len is guaranteed to be a multiple of @c blocksize.
  52. */
  53. void ( * encode ) ( void *ctx, const void *src, void *dst,
  54. size_t len );
  55. /** Decode data
  56. *
  57. * @v ctx Context
  58. * @v src Data to decode
  59. * @v dst Decoded data
  60. * @v len Length of data
  61. * @ret rc Return status code
  62. *
  63. * @v len is guaranteed to be a multiple of @c blocksize.
  64. */
  65. void ( * decode ) ( void *ctx, const void *src, void *dst,
  66. size_t len );
  67. /** Finalise algorithm
  68. *
  69. * @v ctx Context
  70. * @v out Algorithm final output
  71. */
  72. void ( * final ) ( void *ctx, void *out );
  73. };
  74. static inline void digest_init ( struct crypto_algorithm *crypto,
  75. void *ctx ) {
  76. crypto->init ( ctx );
  77. }
  78. static inline void digest_update ( struct crypto_algorithm *crypto,
  79. void *ctx, const void *data, size_t len ) {
  80. crypto->encode ( ctx, data, NULL, len );
  81. }
  82. static inline void digest_final ( struct crypto_algorithm *crypto,
  83. void *ctx, void *out ) {
  84. crypto->final ( ctx, out );
  85. }
  86. static inline void cipher_setiv ( struct crypto_algorithm *crypto,
  87. void *ctx, const void *iv ) {
  88. crypto->setiv ( ctx, iv );
  89. }
  90. static inline int cipher_setkey ( struct crypto_algorithm *crypto,
  91. void *ctx, const void *key, size_t keylen ) {
  92. return crypto->setkey ( ctx, key, keylen );
  93. }
  94. static inline int is_stream_cipher ( struct crypto_algorithm *crypto ) {
  95. return ( crypto->blocksize == 1 );
  96. }
  97. extern struct crypto_algorithm crypto_null;
  98. extern int cipher_encrypt ( struct crypto_algorithm *crypto,
  99. void *ctx, const void *src, void *dst,
  100. size_t len );
  101. extern int cipher_decrypt ( struct crypto_algorithm *crypto,
  102. void *ctx, const void *src, void *dst,
  103. size_t len );
  104. #endif /* _GPXE_CRYPTO_H */