xfm.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * xfm.h
  3. *
  4. * interface for abstract crypto transform
  5. *
  6. * David A. McGrew
  7. * Cisco Systems, Inc.
  8. */
  9. /*
  10. *
  11. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * Neither the name of the Cisco Systems, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  35. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  37. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  41. * OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. */
  44. #ifndef XFM_H
  45. #define XFM_H
  46. #include "crypto_kernel.h"
  47. #include "err.h"
  48. /**
  49. * @defgroup Crypto Cryptography
  50. *
  51. * A simple interface to an abstract cryptographic transform that
  52. * provides both confidentiality and message authentication.
  53. *
  54. * @{
  55. */
  56. /**
  57. * @brief applies a crypto transform
  58. *
  59. * The function pointer xfm_func_t points to a function that
  60. * implements a crypto transform, and provides a uniform API for
  61. * accessing crypto mechanisms.
  62. *
  63. * @param key location of secret key
  64. *
  65. * @param clear data to be authenticated only
  66. *
  67. * @param clear_len length of data to be authenticated only
  68. *
  69. * @param iv location to write the Initialization Vector (IV)
  70. *
  71. * @param protect location of the data to be encrypted and
  72. * authenticated (before the function call), and the ciphertext
  73. * and authentication tag (after the call)
  74. *
  75. * @param protected_len location of the length of the data to be
  76. * encrypted and authenticated (before the function call), and the
  77. * length of the ciphertext (after the call)
  78. *
  79. * @param auth_tag location to write auth tag
  80. */
  81. typedef err_status_t (*xfm_func_t)
  82. (void *key,
  83. void *clear,
  84. unsigned clear_len,
  85. void *iv,
  86. void *protect,
  87. unsigned *protected_len,
  88. void *auth_tag
  89. );
  90. typedef
  91. err_status_t (*xfm_inv_t)
  92. (void *key, /* location of secret key */
  93. void *clear, /* data to be authenticated only */
  94. unsigned clear_len, /* length of data to be authenticated only */
  95. void *iv, /* location of iv */
  96. void *opaque, /* data to be decrypted and authenticated */
  97. unsigned *opaque_len, /* location of the length of data to be
  98. * decrypted and authd (before and after)
  99. */
  100. void *auth_tag /* location of auth tag */
  101. );
  102. typedef struct xfm_ctx_t {
  103. xfm_func_t func;
  104. xfm_inv_t inv;
  105. unsigned key_len;
  106. unsigned iv_len;
  107. unsigned auth_tag_len;
  108. } xfm_ctx_t;
  109. typedef xfm_ctx_t *xfm_t;
  110. #define xfm_get_key_len(xfm) ((xfm)->key_len)
  111. #define xfm_get_iv_len(xfm) ((xfm)->iv_len)
  112. #define xfm_get_auth_tag_len(xfm) ((xfm)->auth_tag_len)
  113. /* cryptoalgo - 5/28 */
  114. typedef err_status_t (*cryptoalg_func_t)
  115. (void *key,
  116. void *clear,
  117. unsigned clear_len,
  118. void *iv,
  119. void *opaque,
  120. unsigned *opaque_len
  121. );
  122. typedef
  123. err_status_t (*cryptoalg_inv_t)
  124. (void *key, /* location of secret key */
  125. void *clear, /* data to be authenticated only */
  126. unsigned clear_len, /* length of data to be authenticated only */
  127. void *iv, /* location of iv */
  128. void *opaque, /* data to be decrypted and authenticated */
  129. unsigned *opaque_len /* location of the length of data to be
  130. * decrypted and authd (before and after)
  131. */
  132. );
  133. typedef struct cryptoalg_ctx_t {
  134. cryptoalg_func_t enc;
  135. cryptoalg_inv_t dec;
  136. unsigned key_len;
  137. unsigned iv_len;
  138. unsigned auth_tag_len;
  139. unsigned max_expansion;
  140. } cryptoalg_ctx_t;
  141. typedef cryptoalg_ctx_t *cryptoalg_t;
  142. #define cryptoalg_get_key_len(cryptoalg) ((cryptoalg)->key_len)
  143. #define cryptoalg_get_iv_len(cryptoalg) ((cryptoalg)->iv_len)
  144. #define cryptoalg_get_auth_tag_len(cryptoalg) ((cryptoalg)->auth_tag_len)
  145. /**
  146. * @}
  147. */
  148. #endif /* XFM_H */