crypto_kernel.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * crypto_kernel.h
  3. *
  4. * header for the cryptographic kernel
  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 CRYPTO_KERNEL
  45. #define CRYPTO_KERNEL
  46. #include "rand_source.h"
  47. #include "prng.h"
  48. #include "cipher.h"
  49. #include "auth.h"
  50. #include "cryptoalg.h"
  51. #include "stat.h"
  52. #include "err.h"
  53. #include "crypto_types.h"
  54. #include "key.h"
  55. #include "crypto.h"
  56. /*
  57. * crypto_kernel_state_t defines the possible states:
  58. *
  59. * insecure - not yet initialized
  60. * secure - initialized and passed self-tests
  61. */
  62. typedef enum {
  63. crypto_kernel_state_insecure,
  64. crypto_kernel_state_secure
  65. } crypto_kernel_state_t;
  66. /*
  67. * linked list of cipher types
  68. */
  69. typedef struct kernel_cipher_type {
  70. cipher_type_id_t id;
  71. cipher_type_t *cipher_type;
  72. struct kernel_cipher_type *next;
  73. } kernel_cipher_type_t;
  74. /*
  75. * linked list of auth types
  76. */
  77. typedef struct kernel_auth_type {
  78. auth_type_id_t id;
  79. auth_type_t *auth_type;
  80. struct kernel_auth_type *next;
  81. } kernel_auth_type_t;
  82. /*
  83. * linked list of debug modules
  84. */
  85. typedef struct kernel_debug_module {
  86. debug_module_t *mod;
  87. struct kernel_debug_module *next;
  88. } kernel_debug_module_t;
  89. /*
  90. * crypto_kernel_t is the data structure for the crypto kernel
  91. *
  92. * note that there is *exactly one* instance of this data type,
  93. * a global variable defined in crypto_kernel.c
  94. */
  95. typedef struct {
  96. crypto_kernel_state_t state; /* current state of kernel */
  97. kernel_cipher_type_t *cipher_type_list; /* list of all cipher types */
  98. kernel_auth_type_t *auth_type_list; /* list of all auth func types */
  99. kernel_debug_module_t *debug_module_list; /* list of all debug modules */
  100. } crypto_kernel_t;
  101. /*
  102. * crypto_kernel_t external api
  103. */
  104. /*
  105. * The function crypto_kernel_init() initialized the crypto kernel and
  106. * runs the self-test operations on the random number generators and
  107. * crypto algorithms. Possible return values are:
  108. *
  109. * err_status_ok initialization successful
  110. * <other> init failure
  111. *
  112. * If any value other than err_status_ok is returned, the
  113. * crypto_kernel MUST NOT be used.
  114. */
  115. err_status_t
  116. crypto_kernel_init(void);
  117. /*
  118. * The function crypto_kernel_shutdown() de-initializes the
  119. * crypto_kernel, zeroizes keys and other cryptographic material, and
  120. * deallocates any dynamically allocated memory. Possible return
  121. * values are:
  122. *
  123. * err_status_ok shutdown successful
  124. * <other> shutdown failure
  125. *
  126. */
  127. err_status_t
  128. crypto_kernel_shutdown(void);
  129. /*
  130. * The function crypto_kernel_stats() checks the the crypto_kernel,
  131. * running tests on the ciphers, auth funcs, and rng, and prints out a
  132. * status report. Possible return values are:
  133. *
  134. * err_status_ok all tests were passed
  135. * <other> a test failed
  136. *
  137. */
  138. err_status_t
  139. crypto_kernel_status(void);
  140. /*
  141. * crypto_kernel_list_debug_modules() outputs a list of debugging modules
  142. *
  143. */
  144. err_status_t
  145. crypto_kernel_list_debug_modules(void);
  146. /*
  147. * crypto_kernel_load_cipher_type()
  148. *
  149. */
  150. err_status_t
  151. crypto_kernel_load_cipher_type(cipher_type_t *ct, cipher_type_id_t id);
  152. err_status_t
  153. crypto_kernel_load_auth_type(auth_type_t *ct, auth_type_id_t id);
  154. /*
  155. * crypto_kernel_replace_cipher_type(ct, id)
  156. *
  157. * replaces the crypto kernel's existing cipher for the cipher_type id
  158. * with a new one passed in externally. The new cipher must pass all the
  159. * existing cipher_type's self tests as well as its own.
  160. */
  161. err_status_t
  162. crypto_kernel_replace_cipher_type(cipher_type_t *ct, cipher_type_id_t id);
  163. /*
  164. * crypto_kernel_replace_auth_type(ct, id)
  165. *
  166. * replaces the crypto kernel's existing cipher for the auth_type id
  167. * with a new one passed in externally. The new auth type must pass all the
  168. * existing auth_type's self tests as well as its own.
  169. */
  170. err_status_t
  171. crypto_kernel_replace_auth_type(auth_type_t *ct, auth_type_id_t id);
  172. err_status_t
  173. crypto_kernel_load_debug_module(debug_module_t *new_dm);
  174. /*
  175. * crypto_kernel_alloc_cipher(id, cp, key_len);
  176. *
  177. * allocates a cipher of type id at location *cp, with key length
  178. * key_len octets. Return values are:
  179. *
  180. * err_status_ok no problems
  181. * err_status_alloc_fail an allocation failure occured
  182. * err_status_fail couldn't find cipher with identifier 'id'
  183. */
  184. err_status_t
  185. crypto_kernel_alloc_cipher(cipher_type_id_t id,
  186. cipher_pointer_t *cp,
  187. int key_len,
  188. int tag_len);
  189. /*
  190. * crypto_kernel_alloc_auth(id, ap, key_len, tag_len);
  191. *
  192. * allocates an auth function of type id at location *ap, with key
  193. * length key_len octets and output tag length of tag_len. Return
  194. * values are:
  195. *
  196. * err_status_ok no problems
  197. * err_status_alloc_fail an allocation failure occured
  198. * err_status_fail couldn't find auth with identifier 'id'
  199. */
  200. err_status_t
  201. crypto_kernel_alloc_auth(auth_type_id_t id,
  202. auth_pointer_t *ap,
  203. int key_len,
  204. int tag_len);
  205. /*
  206. * crypto_kernel_set_debug_module(mod_name, v)
  207. *
  208. * sets dynamic debugging to the value v (0 for off, 1 for on) for the
  209. * debug module with the name mod_name
  210. *
  211. * returns err_status_ok on success, err_status_fail otherwise
  212. */
  213. err_status_t
  214. crypto_kernel_set_debug_module(char *mod_name, int v);
  215. /**
  216. * @brief writes a random octet string.
  217. *
  218. * The function call crypto_get_random(dest, len) writes len octets of
  219. * random data to the location to which dest points, and returns an
  220. * error code. This error code @b must be checked, and if a failure is
  221. * reported, the data in the buffer @b must @b not be used.
  222. *
  223. * @warning If the return code is not checked, then non-random
  224. * data may be in the buffer. This function will fail
  225. * unless it is called after crypto_kernel_init().
  226. *
  227. * @return
  228. * - err_status_ok if no problems occured.
  229. * - [other] a problem occured, and no assumptions should
  230. * be made about the contents of the destination
  231. * buffer.
  232. *
  233. * @ingroup SRTP
  234. */
  235. err_status_t
  236. crypto_get_random(unsigned char *buffer, unsigned int length);
  237. #endif /* CRYPTO_KERNEL */