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 3.0KB

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