asfcrypt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ASF decryption
  3. * Copyright (c) 2007 Reimar Doeffinger
  4. * This is a rewrite of code contained in freeme/freeme2
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavutil/common.h"
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/des.h"
  26. #include "libavutil/rc4.h"
  27. #include "asfcrypt.h"
  28. /**
  29. * \brief find multiplicative inverse modulo 2 ^ 32
  30. * \param v number to invert, must be odd!
  31. * \return number so that result * v = 1 (mod 2^32)
  32. */
  33. static uint32_t inverse(uint32_t v) {
  34. // v ^ 3 gives the inverse (mod 16), could also be implemented
  35. // as table etc. (only lowest 4 bits matter!)
  36. uint32_t inverse = v * v * v;
  37. // uses a fixpoint-iteration that doubles the number
  38. // of correct lowest bits each time
  39. inverse *= 2 - v * inverse;
  40. inverse *= 2 - v * inverse;
  41. inverse *= 2 - v * inverse;
  42. return inverse;
  43. }
  44. /**
  45. * \brief read keys from keybuf into keys
  46. * \param keybuf buffer containing the keys
  47. * \param keys output key array containing the keys for encryption in
  48. * native endianness
  49. */
  50. static void multiswap_init(const uint8_t keybuf[48], uint32_t keys[12]) {
  51. int i;
  52. for (i = 0; i < 12; i++)
  53. keys[i] = AV_RL32(keybuf + (i << 2)) | 1;
  54. }
  55. /**
  56. * \brief invert the keys so that encryption become decryption keys and
  57. * the other way round.
  58. * \param keys key array of ints to invert
  59. */
  60. static void multiswap_invert_keys(uint32_t keys[12]) {
  61. int i;
  62. for (i = 0; i < 5; i++)
  63. keys[i] = inverse(keys[i]);
  64. for (i = 6; i < 11; i++)
  65. keys[i] = inverse(keys[i]);
  66. }
  67. static uint32_t multiswap_step(const uint32_t keys[12], uint32_t v) {
  68. int i;
  69. v *= keys[0];
  70. for (i = 1; i < 5; i++) {
  71. v = (v >> 16) | (v << 16);
  72. v *= keys[i];
  73. }
  74. v += keys[5];
  75. return v;
  76. }
  77. static uint32_t multiswap_inv_step(const uint32_t keys[12], uint32_t v) {
  78. int i;
  79. v -= keys[5];
  80. for (i = 4; i > 0; i--) {
  81. v *= keys[i];
  82. v = (v >> 16) | (v << 16);
  83. }
  84. v *= keys[0];
  85. return v;
  86. }
  87. /**
  88. * \brief "MultiSwap" encryption
  89. * \param keys 32 bit numbers in machine endianness,
  90. * 0-4 and 6-10 must be inverted from decryption
  91. * \param key another key, this one must be the same for the decryption
  92. * \param data data to encrypt
  93. * \return encrypted data
  94. */
  95. static uint64_t multiswap_enc(const uint32_t keys[12], uint64_t key, uint64_t data) {
  96. uint32_t a = data;
  97. uint32_t b = data >> 32;
  98. uint32_t c;
  99. uint32_t tmp;
  100. a += key;
  101. tmp = multiswap_step(keys , a);
  102. b += tmp;
  103. c = (key >> 32) + tmp;
  104. tmp = multiswap_step(keys + 6, b);
  105. c += tmp;
  106. return ((uint64_t)c << 32) | tmp;
  107. }
  108. /**
  109. * \brief "MultiSwap" decryption
  110. * \param keys 32 bit numbers in machine endianness,
  111. * 0-4 and 6-10 must be inverted from encryption
  112. * \param key another key, this one must be the same as for the encryption
  113. * \param data data to decrypt
  114. * \return decrypted data
  115. */
  116. static uint64_t multiswap_dec(const uint32_t keys[12], uint64_t key, uint64_t data) {
  117. uint32_t a;
  118. uint32_t b;
  119. uint32_t c = data >> 32;
  120. uint32_t tmp = data;
  121. c -= tmp;
  122. b = multiswap_inv_step(keys + 6, tmp);
  123. tmp = c - (key >> 32);
  124. b -= tmp;
  125. a = multiswap_inv_step(keys , tmp);
  126. a -= key;
  127. return ((uint64_t)b << 32) | a;
  128. }
  129. void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
  130. struct AVDES des;
  131. struct AVRC4 rc4;
  132. int num_qwords = len >> 3;
  133. uint64_t *qwords = (uint64_t *)data;
  134. uint64_t rc4buff[8];
  135. uint64_t packetkey;
  136. uint32_t ms_keys[12];
  137. uint64_t ms_state;
  138. int i;
  139. if (len < 16) {
  140. for (i = 0; i < len; i++)
  141. data[i] ^= key[i];
  142. return;
  143. }
  144. memset(rc4buff, 0, sizeof(rc4buff));
  145. av_rc4_init(&rc4, key, 12 * 8, 1);
  146. av_rc4_crypt(&rc4, (uint8_t *)rc4buff, NULL, sizeof(rc4buff), NULL, 1);
  147. multiswap_init((uint8_t *)rc4buff, ms_keys);
  148. packetkey = qwords[num_qwords - 1];
  149. packetkey ^= rc4buff[7];
  150. av_des_init(&des, key + 12, 64, 1);
  151. av_des_crypt(&des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1, NULL, 1);
  152. packetkey ^= rc4buff[6];
  153. av_rc4_init(&rc4, (uint8_t *)&packetkey, 64, 1);
  154. av_rc4_crypt(&rc4, data, data, len, NULL, 1);
  155. ms_state = 0;
  156. for (i = 0; i < num_qwords - 1; i++, qwords++)
  157. ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords));
  158. multiswap_invert_keys(ms_keys);
  159. packetkey = (packetkey << 32) | (packetkey >> 32);
  160. packetkey = le2me_64(packetkey);
  161. packetkey = multiswap_dec(ms_keys, ms_state, packetkey);
  162. AV_WL64(qwords, packetkey);
  163. }