fileenc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 implements password based file encryption and authentication
  25. using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password
  26. based key derivation.
  27. */
  28. #include <memory.h>
  29. #include "fileenc.h"
  30. #if defined(__cplusplus)
  31. extern "C"
  32. {
  33. #endif
  34. /* subroutine for data encryption/decryption */
  35. /* this could be speeded up a lot by aligning */
  36. /* buffers and using 32 bit operations */
  37. static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1])
  38. {
  39. unsigned long i = 0, pos = cx->encr_pos;
  40. while (i < d_len) {
  41. if (pos == AES_BLOCK_SIZE) {
  42. unsigned int j = 0;
  43. /* increment encryption nonce */
  44. while (j < 8 && !++cx->nonce[j])
  45. ++j;
  46. /* encrypt the nonce to form next xor buffer */
  47. aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx);
  48. pos = 0;
  49. }
  50. data[i++] ^= cx->encr_bfr[pos++];
  51. }
  52. cx->encr_pos = (unsigned int)pos;
  53. }
  54. int fcrypt_init(
  55. int mode, /* the mode to be used (input) */
  56. const unsigned char pwd[], /* the user specified password (input) */
  57. unsigned int pwd_len, /* the length of the password (input) */
  58. const unsigned char salt[], /* the salt (input) */
  59. #ifdef PASSWORD_VERIFIER
  60. unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
  61. #endif
  62. fcrypt_ctx cx[1]) /* the file encryption context (output) */
  63. {
  64. unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];
  65. if (pwd_len > MAX_PWD_LENGTH)
  66. return PASSWORD_TOO_LONG;
  67. if (mode < 1 || mode > 3)
  68. return BAD_MODE;
  69. cx->mode = mode;
  70. cx->pwd_len = pwd_len;
  71. /* derive the encryption and authentication keys and the password verifier */
  72. derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,
  73. kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);
  74. /* initialise the encryption nonce and buffer pos */
  75. cx->encr_pos = AES_BLOCK_SIZE;
  76. /* if we need a random component in the encryption */
  77. /* nonce, this is where it would have to be set */
  78. memset(cx->nonce, 0, AES_BLOCK_SIZE * sizeof(unsigned char));
  79. /* initialise for encryption using key 1 */
  80. aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
  81. /* initialise for authentication using key 2 */
  82. hmac_sha_begin(cx->auth_ctx);
  83. hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx);
  84. #ifdef PASSWORD_VERIFIER
  85. memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
  86. #endif
  87. return GOOD_RETURN;
  88. }
  89. /* perform 'in place' encryption and authentication */
  90. void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
  91. {
  92. encr_data(data, data_len, cx);
  93. hmac_sha_data(data, data_len, cx->auth_ctx);
  94. }
  95. /* perform 'in place' authentication and decryption */
  96. void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
  97. {
  98. hmac_sha_data(data, data_len, cx->auth_ctx);
  99. encr_data(data, data_len, cx);
  100. }
  101. /* close encryption/decryption and return the MAC value */
  102. int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
  103. {
  104. hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);
  105. return MAC_LENGTH(cx->mode); /* return MAC length in bytes */
  106. }
  107. #if defined(__cplusplus)
  108. }
  109. #endif