]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/rtmpdh.c
mp3dec: read the initial/trailing padding from the LAME tag
[ffmpeg] / libavformat / rtmpdh.c
index f328e68fbf992d47e0a80b3c09c8bf1e3c51d203..e7a83e173f713038678d7dcfa5000ceb75bbd2c9 100644 (file)
  * RTMP Diffie-Hellmann utilities
  */
 
+#include <stdint.h>
+#include <string.h>
+
 #include "config.h"
-#include "rtmpdh.h"
+
+#include "libavutil/attributes.h"
+#include "libavutil/error.h"
+#include "libavutil/mem.h"
 #include "libavutil/random_seed.h"
 
+#include "rtmpdh.h"
+
 #define P1024                                          \
     "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
     "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
@@ -46,8 +54,8 @@
     "F71C35FDAD44CFD2D74F9208BE258FF324943328F67329C0" \
     "FFFFFFFFFFFFFFFF"
 
-#if CONFIG_NETTLE || CONFIG_GCRYPT
-#if CONFIG_NETTLE
+#if CONFIG_GMP || CONFIG_GCRYPT
+#if CONFIG_GMP
 #define bn_new(bn)                      \
     do {                                \
         bn = av_malloc(sizeof(*bn));    \
 #define bn_sub_word(bn, w)          mpz_sub_ui(bn, bn, w)
 #define bn_cmp_1(bn)                mpz_cmp_ui(bn, 1)
 #define bn_num_bytes(bn)            (mpz_sizeinbase(bn, 2) + 7) / 8
-#define bn_bn2bin(bn, buf, len)     nettle_mpz_get_str_256(len, buf, bn)
+#define bn_bn2bin(bn, buf, len)                     \
+    do {                                            \
+        memset(buf, 0, len);                        \
+        if (bn_num_bytes(bn) <= len)                \
+            mpz_export(buf, NULL, 1, 1, 0, 0, bn);  \
+    } while (0)
 #define bn_bin2bn(bn, buf, len)                     \
     do {                                            \
         bn_new(bn);                                 \
         if (bn)                                     \
-            nettle_mpz_set_str_256_u(bn, len, buf); \
+            mpz_import(bn, len, 1, 1, 0, 0, buf);   \
     } while (0)
 #define bn_hex2bn(bn, buf, ret)                     \
     do {                                            \
             ret = 1;                                \
     } while (0)
 #define bn_modexp(bn, y, q, p)      mpz_powm(bn, y, q, p)
-#define bn_random(bn, num_bytes)                    \
-    do {                                            \
-        gmp_randstate_t rs;                         \
-        gmp_randinit_mt(rs);                        \
-        gmp_randseed_ui(rs, av_get_random_seed());  \
-        mpz_urandomb(bn, rs, num_bytes);            \
-        gmp_randclear(rs);                          \
+#define bn_random(bn, num_bits)                       \
+    do {                                              \
+        int bits = num_bits;                          \
+        mpz_set_ui(bn, 0);                            \
+        for (bits = num_bits; bits > 0; bits -= 32) { \
+            mpz_mul_2exp(bn, bn, 32);                 \
+            mpz_add_ui(bn, bn, av_get_random_seed()); \
+        }                                             \
+        mpz_fdiv_r_2exp(bn, bn, num_bits);            \
     } while (0)
 #elif CONFIG_GCRYPT
 #define bn_new(bn)                  bn = gcry_mpi_new(1)
 #define bn_bin2bn(bn, buf, len)     gcry_mpi_scan(&bn, GCRYMPI_FMT_USG, buf, len, NULL)
 #define bn_hex2bn(bn, buf, ret)     ret = (gcry_mpi_scan(&bn, GCRYMPI_FMT_HEX, buf, 0, 0) == 0)
 #define bn_modexp(bn, y, q, p)      gcry_mpi_powm(bn, y, q, p)
-#define bn_random(bn, num_bytes)    gcry_mpi_randomize(bn, num_bytes, GCRY_WEAK_RANDOM)
+#define bn_random(bn, num_bits)     gcry_mpi_randomize(bn, num_bits, GCRY_WEAK_RANDOM)
 #endif
 
 #define MAX_BYTES 18000
@@ -120,7 +135,7 @@ static FFBigNum dh_generate_key(FF_DH *dh)
     bn_new(dh->priv_key);
     if (!dh->priv_key)
         return NULL;
-    bn_random(dh->priv_key, num_bytes);
+    bn_random(dh->priv_key, 8 * num_bytes);
 
     bn_new(dh->pub_key);
     if (!dh->pub_key) {
@@ -137,11 +152,6 @@ static int dh_compute_key(FF_DH *dh, FFBigNum pub_key_bn,
                           uint32_t secret_key_len, uint8_t *secret_key)
 {
     FFBigNum k;
-    int num_bytes;
-
-    num_bytes = bn_num_bytes(dh->p);
-    if (num_bytes <= 0 || num_bytes > MAX_BYTES)
-        return -1;
 
     bn_new(k);
     if (!k)
@@ -157,6 +167,8 @@ static int dh_compute_key(FF_DH *dh, FFBigNum pub_key_bn,
 
 void ff_dh_free(FF_DH *dh)
 {
+    if (!dh)
+        return;
     bn_free(dh->p);
     bn_free(dh->g);
     bn_free(dh->pub_key);
@@ -200,6 +212,8 @@ static int dh_compute_key(FF_DH *dh, FFBigNum pub_key_bn,
 
 void ff_dh_free(FF_DH *dh)
 {
+    if (!dh)
+        return;
     DH_free(dh);
 }
 #endif
@@ -346,4 +360,3 @@ fail:
 
     return ret;
 }
-