key.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifndef __ELL_KEY_H
  23. #define __ELL_KEY_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stddef.h>
  28. #include <stdbool.h>
  29. #include <ell/checksum.h>
  30. struct l_key;
  31. struct l_keyring;
  32. enum l_key_feature {
  33. L_KEY_FEATURE_DH = 1 << 0,
  34. L_KEY_FEATURE_RESTRICT = 1 << 1,
  35. L_KEY_FEATURE_CRYPTO = 1 << 2,
  36. };
  37. enum l_key_type {
  38. L_KEY_RAW = 0,
  39. L_KEY_RSA,
  40. };
  41. enum l_keyring_restriction {
  42. L_KEYRING_RESTRICT_ASYM = 0,
  43. L_KEYRING_RESTRICT_ASYM_CHAIN,
  44. };
  45. enum l_key_cipher_type {
  46. L_KEY_RSA_PKCS1_V1_5,
  47. L_KEY_RSA_RAW,
  48. };
  49. struct l_key *l_key_new(enum l_key_type type, const void *payload,
  50. size_t payload_length);
  51. void l_key_free(struct l_key *key);
  52. void l_key_free_norevoke(struct l_key *key);
  53. bool l_key_update(struct l_key *key, const void *payload, size_t len);
  54. bool l_key_extract(struct l_key *key, void *payload, size_t *len);
  55. ssize_t l_key_get_payload_size(struct l_key *key);
  56. bool l_key_get_info(struct l_key *key, enum l_key_cipher_type cipher,
  57. enum l_checksum_type checksum, size_t *bits,
  58. bool *out_public);
  59. struct l_key *l_key_generate_dh_private(const void *prime_buf,
  60. size_t prime_len);
  61. bool l_key_compute_dh_public(struct l_key *generator, struct l_key *private_key,
  62. struct l_key *prime,
  63. void *payload, size_t *len);
  64. bool l_key_compute_dh_secret(struct l_key *other_public, struct l_key *private_key,
  65. struct l_key *prime,
  66. void *payload, size_t *len);
  67. bool l_key_validate_dh_payload(const void *payload, size_t len,
  68. const void *prime_buf, size_t prime_len);
  69. ssize_t l_key_encrypt(struct l_key *key, enum l_key_cipher_type cipher,
  70. enum l_checksum_type checksum, const void *in,
  71. void *out, size_t len_in, size_t len_out);
  72. ssize_t l_key_decrypt(struct l_key *key, enum l_key_cipher_type cipher,
  73. enum l_checksum_type checksum, const void *in,
  74. void *out, size_t len_in, size_t len_out);
  75. ssize_t l_key_sign(struct l_key *key, enum l_key_cipher_type cipher,
  76. enum l_checksum_type checksum, const void *in,
  77. void *out, size_t len_in, size_t len_out);
  78. bool l_key_verify(struct l_key *key, enum l_key_cipher_type cipher,
  79. enum l_checksum_type checksum, const void *data,
  80. const void *sig, size_t len_data, size_t len_sig);
  81. struct l_keyring *l_keyring_new(void);
  82. bool l_keyring_restrict(struct l_keyring *keyring, enum l_keyring_restriction res,
  83. const struct l_keyring *trust);
  84. void l_keyring_free(struct l_keyring *keyring);
  85. void l_keyring_free_norevoke(struct l_keyring *keyring);
  86. bool l_keyring_link(struct l_keyring *keyring, const struct l_key *key);
  87. bool l_keyring_unlink(struct l_keyring *keyring, const struct l_key *key);
  88. bool l_keyring_link_nested(struct l_keyring *keyring,
  89. const struct l_keyring *nested);
  90. bool l_keyring_unlink_nested(struct l_keyring *keyring,
  91. const struct l_keyring *nested);
  92. bool l_key_is_supported(uint32_t features);
  93. #ifdef __cplusplus
  94. }
  95. #endif
  96. #endif /* __ELL_KEY_H */