added iOS source code
[wl-app.git] / iOS / Pods / SSZipArchive / SSZipArchive / minizip / aes / fileenc.h
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 PASSWORD_VERIFIER
46
47 #define MAX_KEY_LENGTH        32
48 #define MAX_PWD_LENGTH       128
49 #define MAX_SALT_LENGTH       16
50 #define KEYING_ITERATIONS   1000
51
52 #ifdef  PASSWORD_VERIFIER
53 #define PWD_VER_LENGTH         2
54 #else
55 #define PWD_VER_LENGTH         0
56 #endif
57
58 #define GOOD_RETURN            0
59 #define PASSWORD_TOO_LONG   -100
60 #define BAD_MODE            -101
61
62 /*
63     Field lengths (in bytes) versus File Encryption Mode (0 < mode < 4)
64
65     Mode Key Salt  MAC Overhead
66        1  16    8   10       18
67        2  24   12   10       22
68        3  32   16   10       26
69
70    The following macros assume that the mode value is correct.
71 */
72
73 #define KEY_LENGTH(mode)        (8 * (mode & 3) + 8)
74 #define SALT_LENGTH(mode)       (4 * (mode & 3) + 4)
75 #define MAC_LENGTH(mode)        (10)
76
77 /* the context for file encryption   */
78
79 #if defined(__cplusplus)
80 extern "C"
81 {
82 #endif
83
84 typedef struct
85 {   unsigned char   nonce[AES_BLOCK_SIZE];      /* the CTR nonce          */
86     unsigned char   encr_bfr[AES_BLOCK_SIZE];   /* encrypt buffer         */
87     aes_encrypt_ctx encr_ctx[1];                /* encryption context     */
88     hmac_ctx        auth_ctx[1];                /* authentication context */
89     unsigned int    encr_pos;                   /* block position (enc)   */
90     unsigned int    pwd_len;                    /* password length        */
91     unsigned int    mode;                       /* File encryption mode   */
92 } fcrypt_ctx;
93
94 /* initialise file encryption or decryption */
95
96 int fcrypt_init(
97     int mode,                               /* the mode to be used (input)          */
98     const unsigned char pwd[],              /* the user specified password (input)  */
99     unsigned int pwd_len,                   /* the length of the password (input)   */
100     const unsigned char salt[],             /* the salt (input)                     */
101 #ifdef PASSWORD_VERIFIER
102     unsigned char pwd_ver[PWD_VER_LENGTH],  /* 2 byte password verifier (output)    */
103 #endif
104     fcrypt_ctx      cx[1]);                 /* the file encryption context (output) */
105
106 /* perform 'in place' encryption or decryption and authentication               */
107
108 void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
109 void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1]);
110
111 /* close encryption/decryption and return the MAC value */
112 /* the return value is the length of the MAC            */
113
114 int fcrypt_end(unsigned char mac[],     /* the MAC value (output)   */
115                fcrypt_ctx cx[1]);       /* the context (input)      */
116
117 #if defined(__cplusplus)
118 }
119 #endif
120
121 #endif