You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.9KB

  1. /*
  2. * RTMP Diffie-Hellmann utilities
  3. * Copyright (c) 2012 Samuel Pitoiset
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVFORMAT_RTMPDH_H
  22. #define AVFORMAT_RTMPDH_H
  23. #include <stdint.h>
  24. #include "config.h"
  25. #if CONFIG_GMP
  26. #include <gmp.h>
  27. typedef mpz_ptr FFBigNum;
  28. #elif CONFIG_OPENSSL
  29. #include <openssl/bn.h>
  30. #include <openssl/dh.h>
  31. typedef BIGNUM *FFBigNum;
  32. #elif CONFIG_MBEDTLS
  33. #include <mbedtls/bignum.h>
  34. #include <mbedtls/ctr_drbg.h>
  35. #include <mbedtls/entropy.h>
  36. typedef mbedtls_mpi *FFBigNum;
  37. #endif
  38. typedef struct FF_DH {
  39. FFBigNum p;
  40. FFBigNum g;
  41. FFBigNum pub_key;
  42. FFBigNum priv_key;
  43. long length;
  44. } FF_DH;
  45. /**
  46. * Initialize a Diffie-Hellmann context.
  47. *
  48. * @param key_len length of the key
  49. * @return a new Diffie-Hellmann context on success, NULL otherwise
  50. */
  51. FF_DH *ff_dh_init(int key_len);
  52. /**
  53. * Free a Diffie-Hellmann context.
  54. *
  55. * @param dh a Diffie-Hellmann context to free
  56. */
  57. void ff_dh_free(FF_DH *dh);
  58. /**
  59. * Generate a public key.
  60. *
  61. * @param dh a Diffie-Hellmann context
  62. * @return zero on success, negative value otherwise
  63. */
  64. int ff_dh_generate_public_key(FF_DH *dh);
  65. /**
  66. * Write the public key into the given buffer.
  67. *
  68. * @param dh a Diffie-Hellmann context, containing the public key to write
  69. * @param pub_key the buffer where the public key is written
  70. * @param pub_key_len the length of the buffer
  71. * @return zero on success, negative value otherwise
  72. */
  73. int ff_dh_write_public_key(FF_DH *dh, uint8_t *pub_key, int pub_key_len);
  74. /**
  75. * Compute the shared secret key from the private FF_DH value and the
  76. * other party's public value.
  77. *
  78. * @param dh a Diffie-Hellmann context, containing the private key
  79. * @param pub_key the buffer containing the public key
  80. * @param pub_key_len the length of the public key buffer
  81. * @param secret_key the buffer where the secret key is written
  82. * @param secret_key_len the length of the secret key buffer
  83. * @return length of the shared secret key on success, negative value otherwise
  84. */
  85. int ff_dh_compute_shared_secret_key(FF_DH *dh, const uint8_t *pub_key,
  86. int pub_key_len, uint8_t *secret_key,
  87. int secret_key_len);
  88. #endif /* AVFORMAT_RTMPDH_H */