CryptState.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #pragma once
  27. #include <openssl/aes.h>
  28. #include <boost/noncopyable.hpp>
  29. namespace mumlib {
  30. class CryptState : boost::noncopyable {
  31. private:
  32. unsigned char raw_key[AES_BLOCK_SIZE];
  33. unsigned char encrypt_iv[AES_BLOCK_SIZE];
  34. unsigned char decrypt_iv[AES_BLOCK_SIZE];
  35. unsigned char decrypt_history[0x100];
  36. unsigned int uiGood;
  37. unsigned int uiLate;
  38. unsigned int uiLost;
  39. unsigned int uiResync;
  40. unsigned int uiRemoteGood;
  41. unsigned int uiRemoteLate;
  42. unsigned int uiRemoteLost;
  43. unsigned int uiRemoteResync;
  44. AES_KEY encrypt_key;
  45. AES_KEY decrypt_key;
  46. bool bInit;
  47. public:
  48. CryptState();
  49. bool isValid() const;
  50. void genKey();
  51. void setKey(const unsigned char *rkey, const unsigned char *eiv, const unsigned char *div);
  52. void setDecryptIV(const unsigned char *iv);
  53. const unsigned char* getEncryptIV() const;
  54. void ocb_encrypt(const unsigned char *plain, unsigned char *encrypted, unsigned int len,
  55. const unsigned char *nonce,
  56. unsigned char *tag);
  57. void ocb_decrypt(const unsigned char *encrypted, unsigned char *plain, unsigned int len,
  58. const unsigned char *nonce,
  59. unsigned char *tag);
  60. bool decrypt(const unsigned char *source, unsigned char *dst, unsigned int crypted_length);
  61. void encrypt(const unsigned char *source, unsigned char *dst, unsigned int plain_length);
  62. };
  63. };