BEncryption.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * @file BEncryption.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Block cipher encryption abstraction.
  32. */
  33. #ifndef BADVPN_SECURITY_BENCRYPTION_H
  34. #define BADVPN_SECURITY_BENCRYPTION_H
  35. #include <stdint.h>
  36. #include <string.h>
  37. #ifdef BADVPN_USE_CRYPTODEV
  38. #include <crypto/cryptodev.h>
  39. #include <sys/types.h>
  40. #include <sys/stat.h>
  41. #include <fcntl.h>
  42. #include <sys/ioctl.h>
  43. #include <string.h>
  44. #include <stdint.h>
  45. #include <unistd.h>
  46. #endif
  47. #include <openssl/blowfish.h>
  48. #include <openssl/aes.h>
  49. #include <misc/debug.h>
  50. #include <base/DebugObject.h>
  51. #define BENCRYPTION_MODE_ENCRYPT 1
  52. #define BENCRYPTION_MODE_DECRYPT 2
  53. #define BENCRYPTION_MAX_BLOCK_SIZE 16
  54. #define BENCRYPTION_MAX_KEY_SIZE 16
  55. #define BENCRYPTION_CIPHER_BLOWFISH 1
  56. #define BENCRYPTION_CIPHER_BLOWFISH_BLOCK_SIZE 8
  57. #define BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE 16
  58. #define BENCRYPTION_CIPHER_AES 2
  59. #define BENCRYPTION_CIPHER_AES_BLOCK_SIZE 16
  60. #define BENCRYPTION_CIPHER_AES_KEY_SIZE 16
  61. // NOTE: update the maximums above when adding a cipher!
  62. /**
  63. * Block cipher encryption abstraction.
  64. */
  65. typedef struct {
  66. DebugObject d_obj;
  67. int mode;
  68. int cipher;
  69. #ifdef BADVPN_USE_CRYPTODEV
  70. int use_cryptodev;
  71. #endif
  72. union {
  73. BF_KEY blowfish;
  74. struct {
  75. AES_KEY encrypt;
  76. AES_KEY decrypt;
  77. } aes;
  78. #ifdef BADVPN_USE_CRYPTODEV
  79. struct {
  80. int fd;
  81. int cfd;
  82. int cipher;
  83. uint32_t ses;
  84. } cryptodev;
  85. #endif
  86. };
  87. } BEncryption;
  88. /**
  89. * Checks if the given cipher number is valid.
  90. *
  91. * @param cipher cipher number
  92. * @return 1 if valid, 0 if not
  93. */
  94. int BEncryption_cipher_valid (int cipher);
  95. /**
  96. * Returns the block size of a cipher.
  97. *
  98. * @param cipher cipher number. Must be valid.
  99. * @return block size in bytes
  100. */
  101. int BEncryption_cipher_block_size (int cipher);
  102. /**
  103. * Returns the key size of a cipher.
  104. *
  105. * @param cipher cipher number. Must be valid.
  106. * @return key size in bytes
  107. */
  108. int BEncryption_cipher_key_size (int cipher);
  109. /**
  110. * Initializes the object.
  111. * {@link BSecurity_GlobalInitThreadSafe} must have been done if this object
  112. * will be used from a non-main thread.
  113. *
  114. * @param enc the object
  115. * @param mode whether encryption or decryption is to be done, or both.
  116. * Must be a bitwise-OR of at least one of BENCRYPTION_MODE_ENCRYPT
  117. * and BENCRYPTION_MODE_DECRYPT.
  118. * @param cipher cipher number. Must be valid.
  119. * @param key encryption key
  120. */
  121. void BEncryption_Init (BEncryption *enc, int mode, int cipher, uint8_t *key);
  122. /**
  123. * Frees the object.
  124. *
  125. * @param enc the object
  126. */
  127. void BEncryption_Free (BEncryption *enc);
  128. /**
  129. * Encrypts data.
  130. * The object must have been initialized with mode including
  131. * BENCRYPTION_MODE_ENCRYPT.
  132. *
  133. * @param enc the object
  134. * @param in data to encrypt
  135. * @param out ciphertext output
  136. * @param len number of bytes to encrypt. Must be >=0 and a multiple of
  137. * block size.
  138. * @param iv initialization vector. Updated such that continuing a previous encryption
  139. * starting with the updated IV is equivalent to performing just one encryption.
  140. */
  141. void BEncryption_Encrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv);
  142. /**
  143. * Decrypts data.
  144. * The object must have been initialized with mode including
  145. * BENCRYPTION_MODE_DECRYPT.
  146. *
  147. * @param enc the object
  148. * @param in data to decrypt
  149. * @param out plaintext output
  150. * @param len number of bytes to decrypt. Must be >=0 and a multiple of
  151. * block size.
  152. * @param iv initialization vector. Updated such that continuing a previous decryption
  153. * starting with the updated IV is equivalent to performing just one decryption.
  154. */
  155. void BEncryption_Decrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv);
  156. #endif