rtmpdh.h 2.9 KB

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