cryptoalg.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * cryptoalg.h
  3. *
  4. * API for authenticated encryption crypto algorithms
  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 CRYPTOALG_H
  45. #define CRYPTOALG_H
  46. #include "err.h"
  47. /**
  48. * @defgroup Crypto Cryptography
  49. *
  50. * Zed uses a simple interface to a cryptographic transform.
  51. *
  52. * @{
  53. */
  54. /**
  55. * @brief applies a crypto algorithm
  56. *
  57. * The function pointer cryptoalg_func_t points to a function that
  58. * implements a crypto transform, and provides a uniform API for
  59. * accessing crypto mechanisms.
  60. *
  61. * @param key location of secret key
  62. *
  63. * @param clear data to be authenticated but not encrypted
  64. *
  65. * @param clear_len length of data to be authenticated but not encrypted
  66. *
  67. * @param iv location to write the Initialization Vector (IV)
  68. *
  69. * @param protect location of the data to be encrypted and
  70. * authenticated (before the function call), and the ciphertext
  71. * and authentication tag (after the call)
  72. *
  73. * @param protected_len location of the length of the data to be
  74. * encrypted and authenticated (before the function call), and the
  75. * length of the ciphertext (after the call)
  76. *
  77. */
  78. typedef err_status_t (*cryptoalg_func_t)
  79. (void *key,
  80. const void *clear,
  81. unsigned clear_len,
  82. void *iv,
  83. void *protect,
  84. unsigned *protected_len);
  85. typedef
  86. err_status_t (*cryptoalg_inv_t)
  87. (void *key, /* location of secret key */
  88. const void *clear, /* data to be authenticated only */
  89. unsigned clear_len, /* length of data to be authenticated only */
  90. void *iv, /* location of iv */
  91. void *opaque, /* data to be decrypted and authenticated */
  92. unsigned *opaque_len /* location of the length of data to be
  93. * decrypted and authd (before and after)
  94. */
  95. );
  96. typedef struct cryptoalg_ctx_t {
  97. cryptoalg_func_t enc;
  98. cryptoalg_inv_t dec;
  99. unsigned key_len;
  100. unsigned iv_len;
  101. unsigned auth_tag_len;
  102. unsigned max_expansion;
  103. } cryptoalg_ctx_t;
  104. typedef cryptoalg_ctx_t *cryptoalg_t;
  105. #define cryptoalg_get_key_len(cryptoalg) ((cryptoalg)->key_len)
  106. #define cryptoalg_get_iv_len(cryptoalg) ((cryptoalg)->iv_len)
  107. #define cryptoalg_get_auth_tag_len(cryptoalg) ((cryptoalg)->auth_tag_len)
  108. int
  109. cryptoalg_get_id(cryptoalg_t c);
  110. cryptoalg_t
  111. cryptoalg_find_by_id(int id);
  112. /**
  113. * @}
  114. */
  115. #endif /* CRYPTOALG_H */