FFmpeg  4.4.9
rtmpdh.c
Go to the documentation of this file.
1 /*
2  * RTMP Diffie-Hellmann utilities
3  * Copyright (c) 2009 Andrej Stepanchuk
4  * Copyright (c) 2009-2010 Howard Chu
5  * Copyright (c) 2012 Samuel Pitoiset
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 /**
25  * @file
26  * RTMP Diffie-Hellmann utilities
27  */
28 
29 #include <stdint.h>
30 #include <string.h>
31 
32 #include "config.h"
33 
34 #include "libavutil/attributes.h"
35 #include "libavutil/error.h"
36 #include "libavutil/mem.h"
37 #include "libavutil/random_seed.h"
38 
39 #include "rtmpdh.h"
40 
41 #if CONFIG_MBEDTLS
42 #include <mbedtls/ctr_drbg.h>
43 #include <mbedtls/entropy.h>
44 #endif
45 
46 #define P1024 \
47  "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
48  "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
49  "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
50  "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
51  "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \
52  "FFFFFFFFFFFFFFFF"
53 
54 #define Q1024 \
55  "7FFFFFFFFFFFFFFFE487ED5110B4611A62633145C06E0E68" \
56  "948127044533E63A0105DF531D89CD9128A5043CC71A026E" \
57  "F7CA8CD9E69D218D98158536F92F8A1BA7F09AB6B6A8E122" \
58  "F242DABB312F3F637A262174D31BF6B585FFAE5B7A035BF6" \
59  "F71C35FDAD44CFD2D74F9208BE258FF324943328F67329C0" \
60  "FFFFFFFFFFFFFFFF"
61 
62 #if CONFIG_GMP
63 #define bn_new(bn) \
64  do { \
65  bn = av_malloc(sizeof(*bn)); \
66  if (bn) \
67  mpz_init2(bn, 1); \
68  } while (0)
69 #define bn_free(bn) \
70  do { \
71  if (bn) \
72  mpz_clear(bn); \
73  av_free(bn); \
74  } while (0)
75 #define bn_set_word(bn, w) mpz_set_ui(bn, w)
76 #define bn_cmp(a, b) mpz_cmp(a, b)
77 #define bn_copy(to, from) mpz_set(to, from)
78 #define bn_sub_word(bn, w) mpz_sub_ui(bn, bn, w)
79 #define bn_cmp_1(bn) mpz_cmp_ui(bn, 1)
80 #define bn_num_bytes(bn) (mpz_sizeinbase(bn, 2) + 7) / 8
81 #define bn_bn2bin(bn, buf, len) \
82  do { \
83  memset(buf, 0, len); \
84  if (bn_num_bytes(bn) <= len) \
85  mpz_export(buf, NULL, 1, 1, 0, 0, bn); \
86  } while (0)
87 #define bn_bin2bn(bn, buf, len) \
88  do { \
89  bn_new(bn); \
90  if (bn) \
91  mpz_import(bn, len, 1, 1, 0, 0, buf); \
92  } while (0)
93 #define bn_hex2bn(bn, buf, ret) \
94  do { \
95  bn_new(bn); \
96  if (bn) \
97  ret = (mpz_set_str(bn, buf, 16) == 0); \
98  else \
99  ret = 0; \
100  } while (0)
101 #define bn_random(bn, num_bits) \
102  do { \
103  int bits = num_bits; \
104  mpz_set_ui(bn, 0); \
105  for (bits = num_bits; bits > 0; bits -= 32) { \
106  mpz_mul_2exp(bn, bn, 32); \
107  mpz_add_ui(bn, bn, av_get_random_seed()); \
108  } \
109  mpz_fdiv_r_2exp(bn, bn, num_bits); \
110  } while (0)
111 static int bn_modexp(FFBigNum bn, FFBigNum y, FFBigNum q, FFBigNum p)
112 {
113  mpz_powm(bn, y, q, p);
114  return 0;
115 }
116 #elif CONFIG_GCRYPT
117 #define bn_new(bn) \
118  do { \
119  if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) { \
120  if (gcry_check_version("1.5.4")) { \
121  gcry_control(GCRYCTL_DISABLE_SECMEM, 0); \
122  gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); \
123  } \
124  } \
125  if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) \
126  bn = gcry_mpi_new(1); \
127  else \
128  bn = NULL; \
129  } while (0)
130 #define bn_free(bn) gcry_mpi_release(bn)
131 #define bn_set_word(bn, w) gcry_mpi_set_ui(bn, w)
132 #define bn_cmp(a, b) gcry_mpi_cmp(a, b)
133 #define bn_copy(to, from) gcry_mpi_set(to, from)
134 #define bn_sub_word(bn, w) gcry_mpi_sub_ui(bn, bn, w)
135 #define bn_cmp_1(bn) gcry_mpi_cmp_ui(bn, 1)
136 #define bn_num_bytes(bn) (gcry_mpi_get_nbits(bn) + 7) / 8
137 #define bn_bn2bin(bn, buf, len) gcry_mpi_print(GCRYMPI_FMT_USG, buf, len, NULL, bn)
138 #define bn_bin2bn(bn, buf, len) gcry_mpi_scan(&bn, GCRYMPI_FMT_USG, buf, len, NULL)
139 #define bn_hex2bn(bn, buf, ret) ret = (gcry_mpi_scan(&bn, GCRYMPI_FMT_HEX, buf, 0, 0) == 0)
140 #define bn_random(bn, num_bits) gcry_mpi_randomize(bn, num_bits, GCRY_WEAK_RANDOM)
141 static int bn_modexp(FFBigNum bn, FFBigNum y, FFBigNum q, FFBigNum p)
142 {
143  gcry_mpi_powm(bn, y, q, p);
144  return 0;
145 }
146 #elif CONFIG_OPENSSL
147 #define bn_new(bn) bn = BN_new()
148 #define bn_free(bn) BN_free(bn)
149 #define bn_set_word(bn, w) BN_set_word(bn, w)
150 #define bn_cmp(a, b) BN_cmp(a, b)
151 #define bn_copy(to, from) BN_copy(to, from)
152 #define bn_sub_word(bn, w) BN_sub_word(bn, w)
153 #define bn_cmp_1(bn) BN_cmp(bn, BN_value_one())
154 #define bn_num_bytes(bn) BN_num_bytes(bn)
155 #define bn_bn2bin(bn, buf, len) BN_bn2bin(bn, buf)
156 #define bn_bin2bn(bn, buf, len) bn = BN_bin2bn(buf, len, 0)
157 #define bn_hex2bn(bn, buf, ret) ret = BN_hex2bn(&bn, buf)
158 #define bn_random(bn, num_bits) BN_rand(bn, num_bits, 0, 0)
159 static int bn_modexp(FFBigNum bn, FFBigNum y, FFBigNum q, FFBigNum p)
160 {
161  BN_CTX *ctx = BN_CTX_new();
162  if (!ctx)
163  return AVERROR(ENOMEM);
164  if (!BN_mod_exp(bn, y, q, p, ctx)) {
165  BN_CTX_free(ctx);
166  return AVERROR(EINVAL);
167  }
168  BN_CTX_free(ctx);
169  return 0;
170 }
171 #elif CONFIG_MBEDTLS
172 #define bn_new(bn) \
173  do { \
174  bn = av_malloc(sizeof(*bn)); \
175  if (bn) \
176  mbedtls_mpi_init(bn); \
177  } while (0)
178 #define bn_free(bn) \
179  do { \
180  mbedtls_mpi_free(bn); \
181  av_free(bn); \
182  } while (0)
183 #define bn_set_word(bn, w) mbedtls_mpi_lset(bn, w)
184 #define bn_cmp(a, b) mbedtls_mpi_cmp_mpi(a, b)
185 #define bn_copy(to, from) mbedtls_mpi_copy(to, from)
186 #define bn_sub_word(bn, w) mbedtls_mpi_sub_int(bn, bn, w)
187 #define bn_cmp_1(bn) mbedtls_mpi_cmp_int(bn, 1)
188 #define bn_num_bytes(bn) (mbedtls_mpi_bitlen(bn) + 7) / 8
189 #define bn_bn2bin(bn, buf, len) mbedtls_mpi_write_binary(bn, buf, len)
190 #define bn_bin2bn(bn, buf, len) \
191  do { \
192  bn_new(bn); \
193  if (bn) \
194  mbedtls_mpi_read_binary(bn, buf, len); \
195  } while (0)
196 #define bn_hex2bn(bn, buf, ret) \
197  do { \
198  bn_new(bn); \
199  if (bn) \
200  ret = (mbedtls_mpi_read_string(bn, 16, buf) == 0); \
201  else \
202  ret = 0; \
203  } while (0)
204 #define bn_random(bn, num_bits) \
205  do { \
206  mbedtls_entropy_context entropy_ctx; \
207  mbedtls_ctr_drbg_context ctr_drbg_ctx; \
208  \
209  mbedtls_entropy_init(&entropy_ctx); \
210  mbedtls_ctr_drbg_init(&ctr_drbg_ctx); \
211  mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, \
212  mbedtls_entropy_func, \
213  &entropy_ctx, \
214  NULL, 0); \
215  mbedtls_mpi_fill_random(bn, (num_bits + 7) / 8, mbedtls_ctr_drbg_random, &ctr_drbg_ctx); \
216  mbedtls_ctr_drbg_free(&ctr_drbg_ctx); \
217  mbedtls_entropy_free(&entropy_ctx); \
218  } while (0)
219 #define bn_modexp(bn, y, q, p) mbedtls_mpi_exp_mod(bn, y, q, p, 0)
220 
221 #endif
222 
223 #define MAX_BYTES 18000
224 
225 #define dh_new() av_mallocz(sizeof(FF_DH))
226 
227 static FFBigNum dh_generate_key(FF_DH *dh)
228 {
229  int num_bytes;
230 
231  num_bytes = bn_num_bytes(dh->p) - 1;
232  if (num_bytes <= 0 || num_bytes > MAX_BYTES)
233  return NULL;
234 
235  bn_new(dh->priv_key);
236  if (!dh->priv_key)
237  return NULL;
238  bn_random(dh->priv_key, 8 * num_bytes);
239 
240  bn_new(dh->pub_key);
241  if (!dh->pub_key)
242  return NULL;
243 
244  if (bn_modexp(dh->pub_key, dh->g, dh->priv_key, dh->p) < 0)
245  return NULL;
246 
247  return dh->pub_key;
248 }
249 
250 static int dh_compute_key(FF_DH *dh, FFBigNum pub_key_bn,
251  uint32_t secret_key_len, uint8_t *secret_key)
252 {
253  FFBigNum k;
254  int ret;
255 
256  bn_new(k);
257  if (!k)
258  return -1;
259 
260  if ((ret = bn_modexp(k, pub_key_bn, dh->priv_key, dh->p)) < 0) {
261  bn_free(k);
262  return ret;
263  }
264  bn_bn2bin(k, secret_key, secret_key_len);
265  bn_free(k);
266 
267  /* return the length of the shared secret key like DH_compute_key */
268  return secret_key_len;
269 }
270 
271 void ff_dh_free(FF_DH *dh)
272 {
273  if (!dh)
274  return;
275  bn_free(dh->p);
276  bn_free(dh->g);
277  bn_free(dh->pub_key);
278  bn_free(dh->priv_key);
279  av_free(dh);
280 }
281 
282 static int dh_is_valid_public_key(FFBigNum y, FFBigNum p, FFBigNum q)
283 {
284  FFBigNum bn = NULL;
285  int ret = AVERROR(EINVAL);
286 
287  bn_new(bn);
288  if (!bn)
289  return AVERROR(ENOMEM);
290 
291  /* y must lie in [2, p - 1] */
292  bn_set_word(bn, 1);
293  if (!bn_cmp(y, bn))
294  goto fail;
295 
296  /* bn = p - 2 */
297  bn_copy(bn, p);
298  bn_sub_word(bn, 1);
299  if (!bn_cmp(y, bn))
300  goto fail;
301 
302  /* Verify with Sophie-Germain prime
303  *
304  * This is a nice test to make sure the public key position is calculated
305  * correctly. This test will fail in about 50% of the cases if applied to
306  * random data.
307  */
308  /* y must fulfill y^q mod p = 1 */
309  if ((ret = bn_modexp(bn, y, q, p)) < 0)
310  goto fail;
311 
312  ret = AVERROR(EINVAL);
313  if (bn_cmp_1(bn))
314  goto fail;
315 
316  ret = 0;
317 fail:
318  bn_free(bn);
319 
320  return ret;
321 }
322 
323 av_cold FF_DH *ff_dh_init(int key_len)
324 {
325  FF_DH *dh;
326  int ret;
327 
328  if (!(dh = dh_new()))
329  return NULL;
330 
331  bn_new(dh->g);
332  if (!dh->g)
333  goto fail;
334 
335  bn_hex2bn(dh->p, P1024, ret);
336  if (!ret)
337  goto fail;
338 
339  bn_set_word(dh->g, 2);
340  dh->length = key_len;
341 
342  return dh;
343 
344 fail:
345  ff_dh_free(dh);
346 
347  return NULL;
348 }
349 
351 {
352  int ret = 0;
353 
354  while (!ret) {
355  FFBigNum q1 = NULL;
356 
357  if (!dh_generate_key(dh))
358  return AVERROR(EINVAL);
359 
360  bn_hex2bn(q1, Q1024, ret);
361  if (!ret)
362  return AVERROR(ENOMEM);
363 
364  ret = dh_is_valid_public_key(dh->pub_key, dh->p, q1);
365  bn_free(q1);
366 
367  if (!ret) {
368  /* the public key is valid */
369  break;
370  }
371  }
372 
373  return ret;
374 }
375 
376 int ff_dh_write_public_key(FF_DH *dh, uint8_t *pub_key, int pub_key_len)
377 {
378  int len;
379 
380  /* compute the length of the public key */
381  len = bn_num_bytes(dh->pub_key);
382  if (len <= 0 || len > pub_key_len)
383  return AVERROR(EINVAL);
384 
385  /* convert the public key value into big-endian form */
386  memset(pub_key, 0, pub_key_len);
387  bn_bn2bin(dh->pub_key, pub_key + pub_key_len - len, len);
388 
389  return 0;
390 }
391 
393  int pub_key_len, uint8_t *secret_key,
394  int secret_key_len)
395 {
396  FFBigNum q1 = NULL, pub_key_bn = NULL;
397  int ret;
398 
399  /* convert the big-endian form of the public key into a bignum */
400  bn_bin2bn(pub_key_bn, pub_key, pub_key_len);
401  if (!pub_key_bn)
402  return AVERROR(ENOMEM);
403 
404  /* convert the string containing a hexadecimal number into a bignum */
405  bn_hex2bn(q1, Q1024, ret);
406  if (!ret) {
407  ret = AVERROR(ENOMEM);
408  goto fail;
409  }
410 
411  /* when the public key is valid we have to compute the shared secret key */
412  if ((ret = dh_is_valid_public_key(pub_key_bn, dh->p, q1)) < 0) {
413  goto fail;
414  } else if ((ret = dh_compute_key(dh, pub_key_bn, secret_key_len,
415  secret_key)) < 0) {
416  ret = AVERROR(EINVAL);
417  goto fail;
418  }
419 
420 fail:
421  bn_free(pub_key_bn);
422  bn_free(q1);
423 
424  return ret;
425 }
Macro definitions for various function/variable attributes.
#define av_cold
Definition: attributes.h:88
uint8_t
#define fail()
Definition: checkasm.h:133
#define NULL
Definition: coverity.c:32
error code definitions
#define AVERROR(e)
Definition: error.h:43
Memory handling functions.
int ff_dh_write_public_key(FF_DH *dh, uint8_t *pub_key, int pub_key_len)
Write the public key into the given buffer.
Definition: rtmpdh.c:376
void ff_dh_free(FF_DH *dh)
Free a Diffie-Hellmann context.
Definition: rtmpdh.c:271
static int dh_compute_key(FF_DH *dh, FFBigNum pub_key_bn, uint32_t secret_key_len, uint8_t *secret_key)
Definition: rtmpdh.c:250
#define MAX_BYTES
Definition: rtmpdh.c:223
static FFBigNum dh_generate_key(FF_DH *dh)
Definition: rtmpdh.c:227
#define P1024
Definition: rtmpdh.c:46
int ff_dh_generate_public_key(FF_DH *dh)
Generate a public key.
Definition: rtmpdh.c:350
int ff_dh_compute_shared_secret_key(FF_DH *dh, const uint8_t *pub_key, int pub_key_len, uint8_t *secret_key, int secret_key_len)
Compute the shared secret key from the private FF_DH value and the other party's public value.
Definition: rtmpdh.c:392
av_cold FF_DH * ff_dh_init(int key_len)
Initialize a Diffie-Hellmann context.
Definition: rtmpdh.c:323
static int dh_is_valid_public_key(FFBigNum y, FFBigNum p, FFBigNum q)
Definition: rtmpdh.c:282
#define dh_new()
Definition: rtmpdh.c:225
#define Q1024
Definition: rtmpdh.c:54
Definition: rtmpdh.h:50
long length
Definition: rtmpdh.h:55
FFBigNum priv_key
Definition: rtmpdh.h:54
FFBigNum pub_key
Definition: rtmpdh.h:53
FFBigNum g
Definition: rtmpdh.h:52
FFBigNum p
Definition: rtmpdh.h:51
#define av_free(p)
AVFormatContext * ctx
Definition: movenc.c:48
static const uint8_t q1[256]
Definition: twofish.c:96
int len