avformat/concatdec: set safe mode to enabled instead of auto
[ffmpeg.git] / libavformat / rtmpdh.c
index c29b563..42ad72c 100644 (file)
@@ -4,20 +4,20 @@
  * Copyright (c) 2009-2010 Howard Chu
  * Copyright (c) 2012 Samuel Pitoiset
  *
- * This file is part of Libav.
+ * This file is part of FFmpeg.
  *
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
  * 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.
  *
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * 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
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
@@ -46,8 +46,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_new(bn)                                              \
+    do {                                                        \
+        if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) { \
+            if (!gcry_check_version("1.5.4"))                   \
+                return AVERROR(EINVAL);                         \
+            gcry_control(GCRYCTL_DISABLE_SECMEM, 0);            \
+            gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);   \
+        }                                                       \
+        bn = gcry_mpi_new(1);                                   \
+    } while (0)
 #define bn_free(bn)                 gcry_mpi_release(bn)
 #define bn_set_word(bn, w)          gcry_mpi_set_ui(bn, w)
 #define bn_cmp(a, b)                gcry_mpi_cmp(a, b)
 #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 +136,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 +153,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)