00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include "config.h"
00030 #include "rtmpdh.h"
00031 #include "libavutil/random_seed.h"
00032
00033 #define P1024 \
00034 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
00035 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
00036 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
00037 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
00038 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \
00039 "FFFFFFFFFFFFFFFF"
00040
00041 #define Q1024 \
00042 "7FFFFFFFFFFFFFFFE487ED5110B4611A62633145C06E0E68" \
00043 "948127044533E63A0105DF531D89CD9128A5043CC71A026E" \
00044 "F7CA8CD9E69D218D98158536F92F8A1BA7F09AB6B6A8E122" \
00045 "F242DABB312F3F637A262174D31BF6B585FFAE5B7A035BF6" \
00046 "F71C35FDAD44CFD2D74F9208BE258FF324943328F67329C0" \
00047 "FFFFFFFFFFFFFFFF"
00048
00049 #if CONFIG_NETTLE || CONFIG_GCRYPT
00050 #if CONFIG_NETTLE
00051 #define bn_new(bn) \
00052 do { \
00053 bn = av_malloc(sizeof(*bn)); \
00054 if (bn) \
00055 mpz_init2(bn, 1); \
00056 } while (0)
00057 #define bn_free(bn) \
00058 do { \
00059 mpz_clear(bn); \
00060 av_free(bn); \
00061 } while (0)
00062 #define bn_set_word(bn, w) mpz_set_ui(bn, w)
00063 #define bn_cmp(a, b) mpz_cmp(a, b)
00064 #define bn_copy(to, from) mpz_set(to, from)
00065 #define bn_sub_word(bn, w) mpz_sub_ui(bn, bn, w)
00066 #define bn_cmp_1(bn) mpz_cmp_ui(bn, 1)
00067 #define bn_num_bytes(bn) (mpz_sizeinbase(bn, 2) + 7) / 8
00068 #define bn_bn2bin(bn, buf, len) nettle_mpz_get_str_256(len, buf, bn)
00069 #define bn_bin2bn(bn, buf, len) \
00070 do { \
00071 bn_new(bn); \
00072 if (bn) \
00073 nettle_mpz_set_str_256_u(bn, len, buf); \
00074 } while (0)
00075 #define bn_hex2bn(bn, buf, ret) \
00076 do { \
00077 bn_new(bn); \
00078 if (bn) \
00079 ret = (mpz_set_str(bn, buf, 16) == 0); \
00080 } while (0)
00081 #define bn_modexp(bn, y, q, p) mpz_powm(bn, y, q, p)
00082 #define bn_random(bn, num_bytes) \
00083 do { \
00084 gmp_randstate_t rs; \
00085 gmp_randinit_mt(rs); \
00086 gmp_randseed_ui(rs, av_get_random_seed()); \
00087 mpz_urandomb(bn, rs, num_bytes); \
00088 gmp_randclear(rs); \
00089 } while (0)
00090 #elif CONFIG_GCRYPT
00091 #define bn_new(bn) bn = gcry_mpi_new(1)
00092 #define bn_free(bn) gcry_mpi_release(bn)
00093 #define bn_set_word(bn, w) gcry_mpi_set_ui(bn, w)
00094 #define bn_cmp(a, b) gcry_mpi_cmp(a, b)
00095 #define bn_copy(to, from) gcry_mpi_set(to, from)
00096 #define bn_sub_word(bn, w) gcry_mpi_sub_ui(bn, bn, w)
00097 #define bn_cmp_1(bn) gcry_mpi_cmp_ui(bn, 1)
00098 #define bn_num_bytes(bn) (gcry_mpi_get_nbits(bn) + 7) / 8
00099 #define bn_bn2bin(bn, buf, len) gcry_mpi_print(GCRYMPI_FMT_USG, buf, len, NULL, bn)
00100 #define bn_bin2bn(bn, buf, len) gcry_mpi_scan(&bn, GCRYMPI_FMT_USG, buf, len, NULL)
00101 #define bn_hex2bn(bn, buf, ret) ret = (gcry_mpi_scan(&bn, GCRYMPI_FMT_HEX, buf, 0, 0) == 0)
00102 #define bn_modexp(bn, y, q, p) gcry_mpi_powm(bn, y, q, p)
00103 #define bn_random(bn, num_bytes) gcry_mpi_randomize(bn, num_bytes, GCRY_WEAK_RANDOM)
00104 #endif
00105
00106 #define MAX_BYTES 18000
00107
00108 #define dh_new() av_malloc(sizeof(FF_DH))
00109
00110 static FFBigNum dh_generate_key(FF_DH *dh)
00111 {
00112 int num_bytes;
00113
00114 num_bytes = bn_num_bytes(dh->p) - 1;
00115 if (num_bytes <= 0 || num_bytes > MAX_BYTES)
00116 return NULL;
00117
00118 bn_new(dh->priv_key);
00119 if (!dh->priv_key)
00120 return NULL;
00121 bn_random(dh->priv_key, num_bytes);
00122
00123 bn_new(dh->pub_key);
00124 if (!dh->pub_key) {
00125 bn_free(dh->priv_key);
00126 return NULL;
00127 }
00128
00129 bn_modexp(dh->pub_key, dh->g, dh->priv_key, dh->p);
00130
00131 return dh->pub_key;
00132 }
00133
00134 static int dh_compute_key(FF_DH *dh, FFBigNum pub_key_bn,
00135 uint32_t pub_key_len, uint8_t *secret_key)
00136 {
00137 FFBigNum k;
00138 int num_bytes;
00139
00140 num_bytes = bn_num_bytes(dh->p);
00141 if (num_bytes <= 0 || num_bytes > MAX_BYTES)
00142 return -1;
00143
00144 bn_new(k);
00145 if (!k)
00146 return -1;
00147
00148 bn_modexp(k, pub_key_bn, dh->priv_key, dh->p);
00149 bn_bn2bin(k, secret_key, pub_key_len);
00150 bn_free(k);
00151
00152
00153 return pub_key_len;
00154 }
00155
00156 void ff_dh_free(FF_DH *dh)
00157 {
00158 bn_free(dh->p);
00159 bn_free(dh->g);
00160 bn_free(dh->pub_key);
00161 bn_free(dh->priv_key);
00162 av_free(dh);
00163 }
00164 #elif CONFIG_OPENSSL
00165 #define bn_new(bn) bn = BN_new()
00166 #define bn_free(bn) BN_free(bn)
00167 #define bn_set_word(bn, w) BN_set_word(bn, w)
00168 #define bn_cmp(a, b) BN_cmp(a, b)
00169 #define bn_copy(to, from) BN_copy(to, from)
00170 #define bn_sub_word(bn, w) BN_sub_word(bn, w)
00171 #define bn_cmp_1(bn) BN_cmp(bn, BN_value_one())
00172 #define bn_num_bytes(bn) BN_num_bytes(bn)
00173 #define bn_bn2bin(bn, buf, len) BN_bn2bin(bn, buf)
00174 #define bn_bin2bn(bn, buf, len) bn = BN_bin2bn(buf, len, 0)
00175 #define bn_hex2bn(bn, buf, ret) ret = BN_hex2bn(&bn, buf)
00176 #define bn_modexp(bn, y, q, p) \
00177 do { \
00178 BN_CTX *ctx = BN_CTX_new(); \
00179 if (!ctx) \
00180 return AVERROR(ENOMEM); \
00181 if (!BN_mod_exp(bn, y, q, p, ctx)) { \
00182 BN_CTX_free(ctx); \
00183 return AVERROR(EINVAL); \
00184 } \
00185 BN_CTX_free(ctx); \
00186 } while (0)
00187
00188 #define dh_new() DH_new()
00189 #define dh_generate_key(dh) DH_generate_key(dh)
00190 #define dh_compute_key(dh, pub, len, secret) DH_compute_key(secret, pub, dh)
00191
00192 void ff_dh_free(FF_DH *dh)
00193 {
00194 DH_free(dh);
00195 }
00196 #endif
00197
00198 static int dh_is_valid_public_key(FFBigNum y, FFBigNum p, FFBigNum q)
00199 {
00200 FFBigNum bn = NULL;
00201 int ret = AVERROR(EINVAL);
00202
00203 bn_new(bn);
00204 if (!bn)
00205 return AVERROR(ENOMEM);
00206
00207
00208 bn_set_word(bn, 1);
00209 if (!bn_cmp(y, bn))
00210 goto fail;
00211
00212
00213 bn_copy(bn, p);
00214 bn_sub_word(bn, 1);
00215 if (!bn_cmp(y, bn))
00216 goto fail;
00217
00218
00219
00220
00221
00222
00223
00224
00225 bn_modexp(bn, y, q, p);
00226
00227 if (bn_cmp_1(bn))
00228 goto fail;
00229
00230 ret = 0;
00231 fail:
00232 bn_free(bn);
00233
00234 return ret;
00235 }
00236
00237 av_cold FF_DH *ff_dh_init(int key_len)
00238 {
00239 FF_DH *dh;
00240 int ret;
00241
00242 if (!(dh = dh_new()))
00243 return NULL;
00244
00245 bn_new(dh->g);
00246 if (!dh->g)
00247 goto fail;
00248
00249 bn_hex2bn(dh->p, P1024, ret);
00250 if (!ret)
00251 goto fail;
00252
00253 bn_set_word(dh->g, 2);
00254 dh->length = key_len;
00255
00256 return dh;
00257
00258 fail:
00259 ff_dh_free(dh);
00260
00261 return NULL;
00262 }
00263
00264 int ff_dh_generate_public_key(FF_DH *dh)
00265 {
00266 int ret = 0;
00267
00268 while (!ret) {
00269 FFBigNum q1 = NULL;
00270
00271 if (!dh_generate_key(dh))
00272 return AVERROR(EINVAL);
00273
00274 bn_hex2bn(q1, Q1024, ret);
00275 if (!ret)
00276 return AVERROR(ENOMEM);
00277
00278 ret = dh_is_valid_public_key(dh->pub_key, dh->p, q1);
00279 bn_free(q1);
00280
00281 if (!ret) {
00282
00283 break;
00284 }
00285 }
00286
00287 return ret;
00288 }
00289
00290 int ff_dh_write_public_key(FF_DH *dh, uint8_t *pub_key, int pub_key_len)
00291 {
00292 int len;
00293
00294
00295 len = bn_num_bytes(dh->pub_key);
00296 if (len <= 0 || len > pub_key_len)
00297 return AVERROR(EINVAL);
00298
00299
00300 memset(pub_key, 0, pub_key_len);
00301 bn_bn2bin(dh->pub_key, pub_key + pub_key_len - len, len);
00302
00303 return 0;
00304 }
00305
00306 int ff_dh_compute_shared_secret_key(FF_DH *dh, const uint8_t *pub_key,
00307 int pub_key_len, uint8_t *secret_key)
00308 {
00309 FFBigNum q1 = NULL, pub_key_bn = NULL;
00310 int ret;
00311
00312
00313 bn_bin2bn(pub_key_bn, pub_key, pub_key_len);
00314 if (!pub_key_bn)
00315 return AVERROR(ENOMEM);
00316
00317
00318 bn_hex2bn(q1, Q1024, ret);
00319 if (!ret) {
00320 ret = AVERROR(ENOMEM);
00321 goto fail;
00322 }
00323
00324
00325 if ((ret = dh_is_valid_public_key(pub_key_bn, dh->p, q1)) < 0) {
00326 goto fail;
00327 } else if ((ret = dh_compute_key(dh, pub_key_bn, pub_key_len,
00328 secret_key)) < 0) {
00329 ret = AVERROR(EINVAL);
00330 goto fail;
00331 }
00332
00333 fail:
00334 bn_free(pub_key_bn);
00335 bn_free(q1);
00336
00337 return ret;
00338 }
00339