Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

crypto.h 3.7KB

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