chapoly_drv.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2015 Martin Willi
  3. * Copyright (C) 2015 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. /**
  16. * @defgroup chapoly_drv chapoly_drv
  17. * @{ @ingroup chapoly
  18. */
  19. #ifndef CHAPOLY_DRV_H_
  20. #define CHAPOLY_DRV_H_
  21. #include <library.h>
  22. #define CHACHA_BLOCK_SIZE 64
  23. #define CHACHA_IV_SIZE 8
  24. #define CHACHA_SALT_SIZE 4
  25. #define CHACHA_KEY_SIZE 32
  26. #define POLY_BLOCK_SIZE 16
  27. #define POLY_ICV_SIZE 16
  28. typedef struct chapoly_drv_t chapoly_drv_t;
  29. /**
  30. * ChaCha20/Poly1305 backend implementation.
  31. */
  32. struct chapoly_drv_t {
  33. /**
  34. * Set the ChaCha20 encryption key.
  35. *
  36. * @param constant 16 byte key constant to use
  37. * @param key 32 byte encryption key
  38. * @param salt 4 byte nonce salt
  39. * @return TRUE if key set
  40. */
  41. bool (*set_key)(chapoly_drv_t *this, u_char *constant, u_char *key,
  42. u_char *salt);
  43. /**
  44. * Start an AEAD en/decryption session, reset state.
  45. *
  46. * @param iv 8 byte initialization vector for nonce
  47. * @return TRUE if initialized
  48. */
  49. bool (*init)(chapoly_drv_t *this, u_char *iv);
  50. /**
  51. * Poly1305 update multiple blocks.
  52. *
  53. * @param data data to update Poly1305 for
  54. * @param blocks number of 16-byte blocks to process
  55. * @return TRUE if updated
  56. */
  57. bool (*poly)(chapoly_drv_t *this, u_char *data, u_int blocks);
  58. /**
  59. * Create a single ChaCha20 keystream block.
  60. *
  61. * @param stream 64-byte block to write key stream data to
  62. * @return TRUE if keystream returned
  63. */
  64. bool (*chacha)(chapoly_drv_t *this, u_char *stream);
  65. /**
  66. * Encrypt multiple blocks of data inline, update Poly1305.
  67. *
  68. * @param data data to process
  69. * @param blocks number of 64-byte blocks to process
  70. * @return TRUE if encrypted
  71. */
  72. bool (*encrypt)(chapoly_drv_t *this, u_char *data, u_int blocks);
  73. /**
  74. * Decrypt multiple blocks of data inline, update Poly1305.
  75. *
  76. * @param data data to process
  77. * @param blocks number of 64-byte blocks to process
  78. * @return TRUE if decrypted
  79. */
  80. bool (*decrypt)(chapoly_drv_t *this, u_char *data, u_int blocks);
  81. /**
  82. * End a AEAD encryption session, return MAC.
  83. *
  84. * @param mac 16-byte block to write MAC to
  85. * @return TRUE if MAC returned
  86. */
  87. bool (*finish)(chapoly_drv_t *this, u_char *mac);
  88. /**
  89. * Destroy a chapoly_drv_t.
  90. */
  91. void (*destroy)(chapoly_drv_t *this);
  92. };
  93. /**
  94. * Create a chapoly_drv instance.
  95. */
  96. chapoly_drv_t *chapoly_drv_probe();
  97. #endif /** CHAPOLY_DRV_H_ @}*/