1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 2002, Dr Brian Gladman <                 >, Worcester, UK.
4  All rights reserved.
5 
6  LICENSE TERMS
7 
8  The free distribution and use of this software in both source and binary
9  form is allowed (with or without changes) provided that:
10 
11    1. distributions of this source code include the above copyright
12       notice, this list of conditions and the following disclaimer;
13 
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;
17 
18    3. the copyright holder's name is not used to endorse products
19       built using this software without specific written permission.
20 
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.
24 
25  DISCLAIMER
26 
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
32 
33  This file contains the header file for fileenc.c, which implements password
34  based file encryption and authentication using AES in CTR mode, HMAC-SHA1
35  authentication and RFC2898 password based key derivation.
36 */
37 
38 #ifndef _FENC_H
39 #define _FENC_H
40 
41 #include "aes.h"
42 #include "hmac.h"
43 #include "pwd2key.h"
44 
45 #define	BLOCK_SIZE AES_BLOCK_SIZE
46 #define PASSWORD_VERIFIER
47 
48 #define MAX_KEY_LENGTH        32
49 #define MAX_PWD_LENGTH       128
50 #define MAX_SALT_LENGTH       16
51 #define KEYING_ITERATIONS   1000
52 
53 #ifdef  PASSWORD_VERIFIER
54 #define PWD_VER_LENGTH         2
55 #else
56 #define PWD_VER_LENGTH         0
57 #endif
58 
59 #define GOOD_RETURN            0
60 #define PASSWORD_TOO_LONG   -100
61 #define BAD_MODE            -101
62 
63 /*
64     Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4)
65 
66     Mode Key Salt  MAC Overhead
67        1  16    8   10       18
68        2  24   12   10       22
69        3  32   16   10       26
70 
71    The following macros assume that the mode value is correct.
72 */
73 
74 #define KEY_LENGTH(mode)        (8 * (mode & 3) + 8)
75 #define SALT_LENGTH(mode)       (4 * (mode & 3) + 4)
76 #define MAC_LENGTH(mode)        (10)
77 
78 /* the context for file encryption   */
79 
80 typedef struct
81 {   unsigned char   nonce[BLOCK_SIZE];          /* the CTR nonce          */
82     unsigned char   encr_bfr[BLOCK_SIZE];       /* encrypt buffer         */
83     aes_encrypt_ctx encr_ctx[1];                /* encryption context     */
84     hmac_ctx        auth_ctx[1];                /* authentication context */
85     unsigned int    encr_pos;                   /* block position (enc)   */
86     unsigned int    pwd_len;                    /* password length        */
87     unsigned int    mode;                       /* File encryption mode   */
88 } fcrypt_ctx;
89 
90 /* initialise file encryption or decryption */
91 
92 int fcrypt_init(
93     int mode,                               /* the mode to be used (input)          */
94     const unsigned char pwd[],              /* the user specified password (input)  */
95     unsigned int pwd_len,                   /* the length of the password (input)   */
96     const unsigned char salt[],             /* the salt (input)                     */
97 #ifdef PASSWORD_VERIFIER
98     unsigned char pwd_ver[PWD_VER_LENGTH],  /* 2 byte password verifier (output)    */
99 #endif
100     fcrypt_ctx      cx[1]);                 /* the file encryption context (output) */
101 
102 /* perform 'in place' encryption or decryption and authentication               */
103 
104 void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
105 void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
106 
107 /* close encryption/decryption and return the MAC value */
108 /* the return value is the length of the MAC            */
109 
110 int fcrypt_end(unsigned char mac[],     /* the MAC value (output)   */
111                fcrypt_ctx cx[1]);       /* the context (input)      */
112 
113 #endif
114 
115