CryptState.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Copyright (C) 2005-2011, Thorvald Natvig <thorvald@natvig.com>
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. - Neither the name of the Mumble Developers nor the names of its
  12. contributors may be used to endorse or promote products derived from this
  13. software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. * This code implements OCB-AES128.
  28. * In the US, OCB is covered by patents. The inventor has given a license
  29. * to all programs distributed under the GPL.
  30. * Mumble is BSD (revised) licensed, meaning you can use the code in a
  31. * closed-source program. If you do, you'll have to either replace
  32. * OCB with something else or get yourself a license.
  33. */
  34. #include "mumlib/CryptState.hpp"
  35. #include <openssl/rand.h>
  36. #include <cstring>
  37. #include <cstdint>
  38. using namespace std;
  39. mumlib::CryptState::CryptState() {
  40. for (int i = 0; i < 0x100; i++)
  41. decrypt_history[i] = 0;
  42. bInit = false;
  43. uiGood = uiLate = uiLost = uiResync = 0;
  44. uiRemoteGood = uiRemoteLate = uiRemoteLost = uiRemoteResync = 0;
  45. }
  46. bool mumlib::CryptState::isValid() const {
  47. return bInit;
  48. }
  49. void mumlib::CryptState::genKey() {
  50. RAND_bytes(raw_key, AES_BLOCK_SIZE);
  51. RAND_bytes(encrypt_iv, AES_BLOCK_SIZE);
  52. RAND_bytes(decrypt_iv, AES_BLOCK_SIZE);
  53. AES_set_encrypt_key(raw_key, 128, &encrypt_key);
  54. AES_set_decrypt_key(raw_key, 128, &decrypt_key);
  55. bInit = true;
  56. }
  57. void mumlib::CryptState::setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div) {
  58. memcpy(raw_key, rkey, AES_BLOCK_SIZE);
  59. memcpy(encrypt_iv, eiv, AES_BLOCK_SIZE);
  60. memcpy(decrypt_iv, div, AES_BLOCK_SIZE);
  61. AES_set_encrypt_key(raw_key, 128, &encrypt_key);
  62. AES_set_decrypt_key(raw_key, 128, &decrypt_key);
  63. bInit = true;
  64. }
  65. void mumlib::CryptState::setDecryptIV(const unsigned char *iv) {
  66. memcpy(decrypt_iv, iv, AES_BLOCK_SIZE);
  67. }
  68. const unsigned char* mumlib::CryptState::getEncryptIV() const {
  69. return encrypt_iv;
  70. }
  71. void mumlib::CryptState::encrypt(const unsigned char *source, unsigned char *dst, unsigned int plain_length) {
  72. unsigned char tag[AES_BLOCK_SIZE];
  73. // First, increase our IV.
  74. for (int i = 0; i < AES_BLOCK_SIZE; i++)
  75. if (++encrypt_iv[i])
  76. break;
  77. ocb_encrypt(source, dst + 4, plain_length, encrypt_iv, tag);
  78. dst[0] = encrypt_iv[0];
  79. dst[1] = tag[0];
  80. dst[2] = tag[1];
  81. dst[3] = tag[2];
  82. }
  83. bool mumlib::CryptState::decrypt(const unsigned char *source, unsigned char *dst, unsigned int crypted_length) {
  84. if (crypted_length < 4)
  85. return false;
  86. unsigned int plain_length = crypted_length - 4;
  87. unsigned char saveiv[AES_BLOCK_SIZE];
  88. unsigned char ivbyte = source[0];
  89. bool restore = false;
  90. unsigned char tag[AES_BLOCK_SIZE];
  91. int lost = 0;
  92. int late = 0;
  93. memcpy(saveiv, decrypt_iv, AES_BLOCK_SIZE);
  94. if (((decrypt_iv[0] + 1) & 0xFF) == ivbyte) {
  95. // In order as expected.
  96. if (ivbyte > decrypt_iv[0]) {
  97. decrypt_iv[0] = ivbyte;
  98. } else if (ivbyte < decrypt_iv[0]) {
  99. decrypt_iv[0] = ivbyte;
  100. for (int i = 1; i < AES_BLOCK_SIZE; i++)
  101. if (++decrypt_iv[i])
  102. break;
  103. } else {
  104. return false;
  105. }
  106. } else {
  107. // This is either out of order or a repeat.
  108. int diff = ivbyte - decrypt_iv[0];
  109. if (diff > 128)
  110. diff = diff - 256;
  111. else if (diff < -128)
  112. diff = diff + 256;
  113. if ((ivbyte < decrypt_iv[0]) && (diff > -30) && (diff < 0)) {
  114. // Late packet, but no wraparound.
  115. late = 1;
  116. lost = -1;
  117. decrypt_iv[0] = ivbyte;
  118. restore = true;
  119. } else if ((ivbyte > decrypt_iv[0]) && (diff > -30) && (diff < 0)) {
  120. // Last was 0x02, here comes 0xff from last round
  121. late = 1;
  122. lost = -1;
  123. decrypt_iv[0] = ivbyte;
  124. for (int i = 1; i < AES_BLOCK_SIZE; i++)
  125. if (decrypt_iv[i]--)
  126. break;
  127. restore = true;
  128. } else if ((ivbyte > decrypt_iv[0]) && (diff > 0)) {
  129. // Lost a few packets, but beyond that we're good.
  130. lost = ivbyte - decrypt_iv[0] - 1;
  131. decrypt_iv[0] = ivbyte;
  132. } else if ((ivbyte < decrypt_iv[0]) && (diff > 0)) {
  133. // Lost a few packets, and wrapped around
  134. lost = 256 - decrypt_iv[0] + ivbyte - 1;
  135. decrypt_iv[0] = ivbyte;
  136. for (int i = 1; i < AES_BLOCK_SIZE; i++)
  137. if (++decrypt_iv[i])
  138. break;
  139. } else {
  140. return false;
  141. }
  142. if (decrypt_history[decrypt_iv[0]] == decrypt_iv[1]) {
  143. memcpy(decrypt_iv, saveiv, AES_BLOCK_SIZE);
  144. return false;
  145. }
  146. }
  147. ocb_decrypt(source + 4, dst, plain_length, decrypt_iv, tag);
  148. if (memcmp(tag, source + 1, 3) != 0) {
  149. memcpy(decrypt_iv, saveiv, AES_BLOCK_SIZE);
  150. return false;
  151. }
  152. decrypt_history[decrypt_iv[0]] = decrypt_iv[1];
  153. if (restore)
  154. memcpy(decrypt_iv, saveiv, AES_BLOCK_SIZE);
  155. uiGood++;
  156. uiLate += late;
  157. uiLost += lost;
  158. return true;
  159. }
  160. #define BLOCKSIZE 2
  161. #define SHIFTBITS 63
  162. typedef uint64_t subblock;
  163. #define SWAP64(x) (__builtin_bswap64(x))
  164. #define SWAPPED(x) SWAP64(x)
  165. typedef subblock keyblock[BLOCKSIZE];
  166. static void inline XOR(subblock *dst, const subblock *a, const subblock *b) {
  167. for (int i = 0; i < BLOCKSIZE; i++) {
  168. dst[i] = a[i] ^ b[i];
  169. }
  170. }
  171. static void inline S2(subblock *block) {
  172. subblock carry = SWAPPED(block[0]) >> SHIFTBITS;
  173. for (int i = 0; i < BLOCKSIZE - 1; i++)
  174. block[i] = SWAPPED((SWAPPED(block[i]) << 1) | (SWAPPED(block[i + 1]) >> SHIFTBITS));
  175. block[BLOCKSIZE - 1] = SWAPPED((SWAPPED(block[BLOCKSIZE - 1]) << 1) ^ (carry * 0x87));
  176. }
  177. static void inline S3(subblock *block) {
  178. subblock carry = SWAPPED(block[0]) >> SHIFTBITS;
  179. for (int i = 0; i < BLOCKSIZE - 1; i++)
  180. block[i] ^= SWAPPED((SWAPPED(block[i]) << 1) | (SWAPPED(block[i + 1]) >> SHIFTBITS));
  181. block[BLOCKSIZE - 1] ^= SWAPPED((SWAPPED(block[BLOCKSIZE - 1]) << 1) ^ (carry * 0x87));
  182. }
  183. static void inline ZERO(keyblock &block) {
  184. for (int i = 0; i < BLOCKSIZE; i++)
  185. block[i] = 0;
  186. }
  187. #define AESencrypt(src, dst, key) AES_encrypt(reinterpret_cast<const unsigned char *>(src),reinterpret_cast<unsigned char *>(dst), key);
  188. #define AESdecrypt(src, dst, key) AES_decrypt(reinterpret_cast<const unsigned char *>(src),reinterpret_cast<unsigned char *>(dst), key);
  189. void mumlib::CryptState::ocb_encrypt(const unsigned char *plain, unsigned char *encrypted, unsigned int len,
  190. const unsigned char *nonce, unsigned char *tag) {
  191. keyblock checksum, delta, tmp, pad;
  192. // Initialize
  193. AESencrypt(nonce, delta, &encrypt_key);
  194. ZERO(checksum);
  195. while (len > AES_BLOCK_SIZE) {
  196. S2(delta);
  197. XOR(tmp, delta, reinterpret_cast<const subblock *>(plain));
  198. AESencrypt(tmp, tmp, &encrypt_key);
  199. XOR(reinterpret_cast<subblock *>(encrypted), delta, tmp);
  200. XOR(checksum, checksum, reinterpret_cast<const subblock *>(plain));
  201. len -= AES_BLOCK_SIZE;
  202. plain += AES_BLOCK_SIZE;
  203. encrypted += AES_BLOCK_SIZE;
  204. }
  205. S2(delta);
  206. ZERO(tmp);
  207. tmp[BLOCKSIZE - 1] = SWAPPED(len * 8);
  208. XOR(tmp, tmp, delta);
  209. AESencrypt(tmp, pad, &encrypt_key);
  210. memcpy(tmp, plain, len);
  211. memcpy(reinterpret_cast<unsigned char *>(tmp) + len, reinterpret_cast<const unsigned char *>(pad) + len,
  212. AES_BLOCK_SIZE - len);
  213. XOR(checksum, checksum, tmp);
  214. XOR(tmp, pad, tmp);
  215. memcpy(encrypted, tmp, len);
  216. S3(delta);
  217. XOR(tmp, delta, checksum);
  218. AESencrypt(tmp, tag, &encrypt_key);
  219. }
  220. void mumlib::CryptState::ocb_decrypt(const unsigned char *encrypted, unsigned char *plain, unsigned int len,
  221. const unsigned char *nonce, unsigned char *tag) {
  222. keyblock checksum, delta, tmp, pad;
  223. // Initialize
  224. AESencrypt(nonce, delta, &encrypt_key);
  225. ZERO(checksum);
  226. while (len > AES_BLOCK_SIZE) {
  227. S2(delta);
  228. XOR(tmp, delta, reinterpret_cast<const subblock *>(encrypted));
  229. AESdecrypt(tmp, tmp, &decrypt_key);
  230. XOR(reinterpret_cast<subblock *>(plain), delta, tmp);
  231. XOR(checksum, checksum, reinterpret_cast<const subblock *>(plain));
  232. len -= AES_BLOCK_SIZE;
  233. plain += AES_BLOCK_SIZE;
  234. encrypted += AES_BLOCK_SIZE;
  235. }
  236. S2(delta);
  237. ZERO(tmp);
  238. tmp[BLOCKSIZE - 1] = SWAPPED(len * 8);
  239. XOR(tmp, tmp, delta);
  240. AESencrypt(tmp, pad, &encrypt_key);
  241. memset(tmp, 0, AES_BLOCK_SIZE);
  242. memcpy(tmp, encrypted, len);
  243. XOR(tmp, tmp, pad);
  244. XOR(checksum, checksum, tmp);
  245. memcpy(plain, tmp, len);
  246. S3(delta);
  247. XOR(tmp, delta, checksum);
  248. AESencrypt(tmp, tag, &encrypt_key);
  249. }