rtmpdh.h 2.91 KB
Newer Older
Samuel Pitoiset's avatar
Samuel Pitoiset committed
1 2 3 4
/*
 * RTMP Diffie-Hellmann utilities
 * Copyright (c) 2012 Samuel Pitoiset
 *
5
 * This file is part of FFmpeg.
Samuel Pitoiset's avatar
Samuel Pitoiset committed
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
Samuel Pitoiset's avatar
Samuel Pitoiset committed
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Samuel Pitoiset's avatar
Samuel Pitoiset committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
Samuel Pitoiset's avatar
Samuel Pitoiset committed
19 20 21 22 23 24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVFORMAT_RTMPDH_H
#define AVFORMAT_RTMPDH_H

25 26
#include <stdint.h>

Samuel Pitoiset's avatar
Samuel Pitoiset committed
27 28
#include "config.h"

29 30
#if CONFIG_GMP || CONFIG_GCRYPT
#if CONFIG_GMP
Samuel Pitoiset's avatar
Samuel Pitoiset committed
31 32 33 34 35 36 37 38 39 40
#include <gmp.h>

typedef mpz_ptr FFBigNum;
#elif CONFIG_GCRYPT
#include <gcrypt.h>

typedef gcry_mpi_t FFBigNum;
#endif

typedef struct FF_DH {
41 42 43 44 45
    FFBigNum p;
    FFBigNum g;
    FFBigNum pub_key;
    FFBigNum priv_key;
    long length;
Samuel Pitoiset's avatar
Samuel Pitoiset committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
} FF_DH;

#elif CONFIG_OPENSSL
#include <openssl/bn.h>
#include <openssl/dh.h>

typedef BIGNUM *FFBigNum;
typedef DH FF_DH;
#endif

/**
 * Initialize a Diffie-Hellmann context.
 *
 * @param key_len length of the key
 * @return a new Diffie-Hellmann context on success, NULL otherwise
 */
FF_DH *ff_dh_init(int key_len);

/**
 * Free a Diffie-Hellmann context.
 *
 * @param dh a Diffie-Hellmann context to free
 */
void ff_dh_free(FF_DH *dh);

/**
 * Generate a public key.
 *
 * @param dh a Diffie-Hellmann context
 * @return zero on success, negative value otherwise
 */
int ff_dh_generate_public_key(FF_DH *dh);

/**
 * Write the public key into the given buffer.
 *
 * @param dh            a Diffie-Hellmann context, containing the public key to write
 * @param pub_key       the buffer where the public key is written
 * @param pub_key_len   the length of the buffer
 * @return zero on success, negative value otherwise
 */
int ff_dh_write_public_key(FF_DH *dh, uint8_t *pub_key, int pub_key_len);

/**
 * Compute the shared secret key from the private FF_DH value and the
 * other party's public value.
 *
 * @param dh            a Diffie-Hellmann context, containing the private key
 * @param pub_key       the buffer containing the public key
95
 * @param pub_key_len   the length of the public key buffer
Samuel Pitoiset's avatar
Samuel Pitoiset committed
96
 * @param secret_key    the buffer where the secret key is written
97
 * @param secret_key_len the length of the secret key buffer
Samuel Pitoiset's avatar
Samuel Pitoiset committed
98 99 100
 * @return length of the shared secret key on success, negative value otherwise
 */
int ff_dh_compute_shared_secret_key(FF_DH *dh, const uint8_t *pub_key,
101 102
                                    int pub_key_len, uint8_t *secret_key,
                                    int secret_key_len);
Samuel Pitoiset's avatar
Samuel Pitoiset committed
103 104

#endif /* AVFORMAT_RTMPDH_H */