2 ---------------------------------------------------------------------------
3 Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
8 The free distribution and use of this software in both source and binary
9 form is allowed (with or without changes) provided that:
11 1. distributions of this source code include the above copyright
12 notice, this list of conditions and the following disclaimer;
14 2. distributions in binary form include the above copyright
15 notice, this list of conditions and the following disclaimer
16 in the documentation and/or other associated materials;
18 3. the copyright holder's name is not used to endorse products
19 built using this software without specific written permission.
21 ALTERNATIVELY, provided that this notice is retained in full, this product
22 may be distributed under the terms of the GNU General Public License (GPL),
23 in which case the provisions of the GPL apply INSTEAD OF those given above.
27 This software is provided 'as is' with no explicit or implied warranties
28 in respect of its properties, including, but not limited to, correctness
29 and/or fitness for purpose.
30 -------------------------------------------------------------------------
31 Issue Date: 24/01/2003
33 This file implements password based file encryption and authentication
34 using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password
43 #if defined(__cplusplus)
48 /* subroutine for data encryption/decryption */
49 /* this could be speeded up a lot by aligning */
50 /* buffers and using 32 bit operations */
52 static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1])
54 unsigned int i = 0, pos = cx->encr_pos;
58 if (pos == AES_BLOCK_SIZE)
61 /* increment encryption nonce */
62 while (j < 8 && !++cx->nonce[j])
64 /* encrypt the nonce to form next xor buffer */
65 aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx);
69 data[i++] ^= cx->encr_bfr[pos++];
76 int mode, /* the mode to be used (input) */
77 const unsigned char pwd[], /* the user specified password (input) */
78 unsigned int pwd_len, /* the length of the password (input) */
79 const unsigned char salt[], /* the salt (input) */
80 #ifdef PASSWORD_VERIFIER
81 unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
83 fcrypt_ctx cx[1]) /* the file encryption context (output) */
84 { unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];
86 if (pwd_len > MAX_PWD_LENGTH)
87 return PASSWORD_TOO_LONG;
89 if (mode < 1 || mode > 3)
93 cx->pwd_len = pwd_len;
95 /* derive the encryption and authentication keys and the password verifier */
96 derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,
97 kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);
99 /* initialise the encryption nonce and buffer pos */
100 cx->encr_pos = AES_BLOCK_SIZE;
101 /* if we need a random component in the encryption */
102 /* nonce, this is where it would have to be set */
103 memset(cx->nonce, 0, AES_BLOCK_SIZE * sizeof(unsigned char));
105 /* initialise for encryption using key 1 */
106 aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
108 /* initialise for authentication using key 2 */
109 hmac_sha_begin(HMAC_SHA1, cx->auth_ctx);
110 hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx);
112 #ifdef PASSWORD_VERIFIER
113 memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
119 /* perform 'in place' encryption and authentication */
121 void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
123 encr_data(data, data_len, cx);
124 hmac_sha_data(data, data_len, cx->auth_ctx);
127 /* perform 'in place' authentication and decryption */
129 void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
131 hmac_sha_data(data, data_len, cx->auth_ctx);
132 encr_data(data, data_len, cx);
135 /* close encryption/decryption and return the MAC value */
137 int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
139 hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);
140 return MAC_LENGTH(cx->mode); /* return MAC length in bytes */
143 #if defined(__cplusplus)