BEncryption.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @file BEncryption.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN 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
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Block cipher encryption abstraction.
  25. */
  26. #ifndef BADVPN_SECURITY_BENCRYPTION_H
  27. #define BADVPN_SECURITY_BENCRYPTION_H
  28. #include <stdint.h>
  29. #include <string.h>
  30. #ifdef BADVPN_USE_CRYPTODEV
  31. #include <crypto/cryptodev.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <fcntl.h>
  35. #include <sys/ioctl.h>
  36. #include <string.h>
  37. #include <stdint.h>
  38. #include <unistd.h>
  39. #endif
  40. #include <openssl/blowfish.h>
  41. #include <openssl/aes.h>
  42. #include <misc/debug.h>
  43. #include <system/DebugObject.h>
  44. #define BENCRYPTION_MODE_ENCRYPT 1
  45. #define BENCRYPTION_MODE_DECRYPT 2
  46. #define BENCRYPTION_CIPHER_BLOWFISH 1
  47. #define BENCRYPTION_CIPHER_BLOWFISH_BLOCK_SIZE 8
  48. #define BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE 16
  49. #define BENCRYPTION_CIPHER_AES 2
  50. #define BENCRYPTION_CIPHER_AES_BLOCK_SIZE 16
  51. #define BENCRYPTION_CIPHER_AES_KEY_SIZE 16
  52. /**
  53. * Block cipher encryption abstraction.
  54. */
  55. typedef struct {
  56. DebugObject d_obj;
  57. int mode;
  58. int cipher;
  59. #ifdef BADVPN_USE_CRYPTODEV
  60. int use_cryptodev;
  61. #endif
  62. union {
  63. BF_KEY blowfish;
  64. struct {
  65. AES_KEY encrypt;
  66. AES_KEY decrypt;
  67. } aes;
  68. #ifdef BADVPN_USE_CRYPTODEV
  69. struct {
  70. int fd;
  71. int cfd;
  72. int cipher;
  73. uint32_t ses;
  74. } cryptodev;
  75. #endif
  76. };
  77. } BEncryption;
  78. /**
  79. * Checks if the given cipher number is valid.
  80. *
  81. * @param cipher cipher number
  82. * @return 1 if valid, 0 if not
  83. */
  84. int BEncryption_cipher_valid (int cipher);
  85. /**
  86. * Returns the block size of a cipher.
  87. *
  88. * @param cipher cipher number. Must be valid.
  89. * @return block size in bytes
  90. */
  91. int BEncryption_cipher_block_size (int cipher);
  92. /**
  93. * Returns the key size of a cipher.
  94. *
  95. * @param cipher cipher number. Must be valid.
  96. * @return key size in bytes
  97. */
  98. int BEncryption_cipher_key_size (int cipher);
  99. /**
  100. * Initializes the object.
  101. *
  102. * @param enc the object
  103. * @param mode whether encryption or decryption is to be done, or both.
  104. * Must be a bitwise-OR of at least one of BENCRYPTION_MODE_ENCRYPT
  105. * and BENCRYPTION_MODE_DECRYPT.
  106. * @param cipher cipher number. Must be valid.
  107. * @param key encryption key
  108. */
  109. void BEncryption_Init (BEncryption *enc, int mode, int cipher, uint8_t *key);
  110. /**
  111. * Frees the object.
  112. *
  113. * @param enc the object
  114. */
  115. void BEncryption_Free (BEncryption *enc);
  116. /**
  117. * Encrypts data.
  118. * The object must have been initialized with mode including
  119. * BENCRYPTION_MODE_ENCRYPT.
  120. *
  121. * @param enc the object
  122. * @param in data to encrypt
  123. * @param out ciphertext output
  124. * @param len number of bytes to encrypt. Must be >=0 and a multiple of
  125. * block size.
  126. * @param iv initialization vector. Updated such that continuing a previous encryption
  127. * starting with the updated IV is equivalent to performing just one encryption.
  128. */
  129. void BEncryption_Encrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv);
  130. /**
  131. * Decrypts data.
  132. * The object must have been initialized with mode including
  133. * BENCRYPTION_MODE_DECRYPT.
  134. *
  135. * @param enc the object
  136. * @param in data to decrypt
  137. * @param out plaintext output
  138. * @param len number of bytes to decrypt. Must be >=0 and a multiple of
  139. * block size.
  140. * @param iv initialization vector. Updated such that continuing a previous decryption
  141. * starting with the updated IV is equivalent to performing just one decryption.
  142. */
  143. void BEncryption_Decrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv);
  144. #endif