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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 message digest algorithm */
  11. struct digest_algorithm {
  12. /** Algorithm name */
  13. const char *name;
  14. /** Context size */
  15. size_t ctxsize;
  16. /** Block size */
  17. size_t blocksize;
  18. /** Digest size */
  19. size_t digestsize;
  20. /** Initialise digest
  21. *
  22. * @v ctx Context
  23. */
  24. void ( * init ) ( void *ctx );
  25. /** Update digest with new data
  26. *
  27. * @v ctx Context
  28. * @v src Data to digest
  29. * @v len Length of data
  30. *
  31. * @v len is not necessarily a multiple of @c blocksize.
  32. */
  33. void ( * update ) ( void *ctx, const void *src, size_t len );
  34. /** Finalise digest
  35. *
  36. * @v ctx Context
  37. * @v out Buffer for digest output
  38. */
  39. void ( * final ) ( void *ctx, void *out );
  40. };
  41. /** A cipher algorithm */
  42. struct cipher_algorithm {
  43. /** Algorithm name */
  44. const char *name;
  45. /** Context size */
  46. size_t ctxsize;
  47. /** Block size */
  48. size_t blocksize;
  49. /** Set key
  50. *
  51. * @v ctx Context
  52. * @v key Key
  53. * @v keylen Key length
  54. * @ret rc Return status code
  55. */
  56. int ( * setkey ) ( void *ctx, const void *key, size_t keylen );
  57. /** Set initialisation vector
  58. *
  59. * @v ctx Context
  60. * @v iv Initialisation vector
  61. */
  62. void ( * setiv ) ( void *ctx, const void *iv );
  63. /** Encrypt data
  64. *
  65. * @v ctx Context
  66. * @v src Data to encrypt
  67. * @v dst Buffer for encrypted data
  68. * @v len Length of data
  69. *
  70. * @v len is guaranteed to be a multiple of @c blocksize.
  71. */
  72. void ( * encrypt ) ( void *ctx, const void *src, void *dst,
  73. size_t len );
  74. /** Decrypt data
  75. *
  76. * @v ctx Context
  77. * @v src Data to decrypt
  78. * @v dst Buffer for decrypted data
  79. * @v len Length of data
  80. *
  81. * @v len is guaranteed to be a multiple of @c blocksize.
  82. */
  83. void ( * decrypt ) ( void *ctx, const void *src, void *dst,
  84. size_t len );
  85. };
  86. /** A public key algorithm */
  87. struct pubkey_algorithm {
  88. /** Algorithm name */
  89. const char *name;
  90. /** Context size */
  91. size_t ctxsize;
  92. };
  93. static inline void digest_init ( struct digest_algorithm *digest,
  94. void *ctx ) {
  95. digest->init ( ctx );
  96. }
  97. static inline void digest_update ( struct digest_algorithm *digest,
  98. void *ctx, const void *data, size_t len ) {
  99. digest->update ( ctx, data, len );
  100. }
  101. static inline void digest_final ( struct digest_algorithm *digest,
  102. void *ctx, void *out ) {
  103. digest->final ( ctx, out );
  104. }
  105. static inline int cipher_setkey ( struct cipher_algorithm *cipher,
  106. void *ctx, const void *key, size_t keylen ) {
  107. return cipher->setkey ( ctx, key, keylen );
  108. }
  109. static inline void cipher_setiv ( struct cipher_algorithm *cipher,
  110. void *ctx, const void *iv ) {
  111. cipher->setiv ( ctx, iv );
  112. }
  113. static inline void cipher_encrypt ( struct cipher_algorithm *cipher,
  114. void *ctx, const void *src, void *dst,
  115. size_t len ) {
  116. cipher->encrypt ( ctx, src, dst, len );
  117. }
  118. #define cipher_encrypt( cipher, ctx, src, dst, len ) do { \
  119. assert ( ( len & ( (cipher)->blocksize - 1 ) ) == 0 ); \
  120. cipher_encrypt ( (cipher), (ctx), (src), (dst), (len) ); \
  121. } while ( 0 )
  122. static inline void cipher_decrypt ( struct cipher_algorithm *cipher,
  123. void *ctx, const void *src, void *dst,
  124. size_t len ) {
  125. cipher->decrypt ( ctx, src, dst, len );
  126. }
  127. #define cipher_decrypt( cipher, ctx, src, dst, len ) do { \
  128. assert ( ( len & ( (cipher)->blocksize - 1 ) ) == 0 ); \
  129. cipher_decrypt ( (cipher), (ctx), (src), (dst), (len) ); \
  130. } while ( 0 )
  131. static inline int is_stream_cipher ( struct cipher_algorithm *cipher ) {
  132. return ( cipher->blocksize == 1 );
  133. }
  134. extern struct digest_algorithm digest_null;
  135. extern struct cipher_algorithm cipher_null;
  136. extern struct pubkey_algorithm pubkey_null;
  137. #endif /* _GPXE_CRYPTO_H */