fileenc.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
  4. All rights reserved.
  5. LICENSE TERMS
  6. The free distribution and use of this software in both source and binary
  7. form is allowed (with or without changes) provided that:
  8. 1. distributions of this source code include the above copyright
  9. notice, this list of conditions and the following disclaimer;
  10. 2. distributions in binary form include the above copyright
  11. notice, this list of conditions and the following disclaimer
  12. in the documentation and/or other associated materials;
  13. 3. the copyright holder's name is not used to endorse products
  14. built using this software without specific written permission.
  15. ALTERNATIVELY, provided that this notice is retained in full, this product
  16. may be distributed under the terms of the GNU General Public License (GPL),
  17. in which case the provisions of the GPL apply INSTEAD OF those given above.
  18. DISCLAIMER
  19. This software is provided 'as is' with no explicit or implied warranties
  20. in respect of its properties, including, but not limited to, correctness
  21. and/or fitness for purpose.
  22. ---------------------------------------------------------------------------
  23. Issue Date: 24/01/2003
  24. This file contains the header file for fileenc.c, which implements password
  25. based file encryption and authentication using AES in CTR mode, HMAC-SHA1
  26. authentication and RFC2898 password based key derivation.
  27. */
  28. #ifndef _FENC_H
  29. #define _FENC_H
  30. #include "aes.h"
  31. #include "hmac.h"
  32. #include "pwd2key.h"
  33. #define PASSWORD_VERIFIER
  34. #define MAX_KEY_LENGTH 32
  35. #define MAX_PWD_LENGTH 128
  36. #define MAX_SALT_LENGTH 16
  37. #define KEYING_ITERATIONS 1000
  38. #ifdef PASSWORD_VERIFIER
  39. #define PWD_VER_LENGTH 2
  40. #else
  41. #define PWD_VER_LENGTH 0
  42. #endif
  43. #define GOOD_RETURN 0
  44. #define PASSWORD_TOO_LONG -100
  45. #define BAD_MODE -101
  46. /*
  47. Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4)
  48. Mode Key Salt MAC Overhead
  49. 1 16 8 10 18
  50. 2 24 12 10 22
  51. 3 32 16 10 26
  52. The following macros assume that the mode value is correct.
  53. */
  54. #define KEY_LENGTH(mode) (8 * (mode & 3) + 8)
  55. #define SALT_LENGTH(mode) (4 * (mode & 3) + 4)
  56. #define MAC_LENGTH(mode) (10)
  57. /* the context for file encryption */
  58. #if defined(__cplusplus)
  59. extern "C"
  60. {
  61. #endif
  62. typedef struct
  63. { unsigned char nonce[AES_BLOCK_SIZE]; /* the CTR nonce */
  64. unsigned char encr_bfr[AES_BLOCK_SIZE]; /* encrypt buffer */
  65. aes_encrypt_ctx encr_ctx[1]; /* encryption context */
  66. hmac_ctx auth_ctx[1]; /* authentication context */
  67. unsigned int encr_pos; /* block position (enc) */
  68. unsigned int pwd_len; /* password length */
  69. unsigned int mode; /* File encryption mode */
  70. } fcrypt_ctx;
  71. /* initialise file encryption or decryption */
  72. int fcrypt_init(
  73. int mode, /* the mode to be used (input) */
  74. const unsigned char pwd[], /* the user specified password (input) */
  75. unsigned int pwd_len, /* the length of the password (input) */
  76. const unsigned char salt[], /* the salt (input) */
  77. #ifdef PASSWORD_VERIFIER
  78. unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
  79. #endif
  80. fcrypt_ctx cx[1]); /* the file encryption context (output) */
  81. /* perform 'in place' encryption or decryption and authentication */
  82. void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
  83. void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
  84. /* close encryption/decryption and return the MAC value */
  85. /* the return value is the length of the MAC */
  86. int fcrypt_end(unsigned char mac[], /* the MAC value (output) */
  87. fcrypt_ctx cx[1]); /* the context (input) */
  88. #if defined(__cplusplus)
  89. }
  90. #endif
  91. #endif