added iOS source code
[wl-app.git] / iOS / Pods / SSZipArchive / SSZipArchive / minizip / aes / fileenc.c
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 implements password based file encryption and authentication 
34  using AES in CTR mode, HMAC-SHA1 authentication and RFC2898 password 
35  based key derivation.
36
37 */
38
39 #include <string.h>
40
41 #include "fileenc.h"
42
43 #if defined(__cplusplus)
44 extern "C"
45 {
46 #endif
47
48 /* subroutine for data encryption/decryption    */
49 /* this could be speeded up a lot by aligning   */
50 /* buffers and using 32 bit operations          */
51
52 static void encr_data(unsigned char data[], unsigned long d_len, fcrypt_ctx cx[1])
53 {
54     unsigned int i = 0, pos = cx->encr_pos;
55
56     while (i < d_len)
57     {
58         if (pos == AES_BLOCK_SIZE)
59         {
60             unsigned int j = 0;
61             /* increment encryption nonce   */
62             while (j < 8 && !++cx->nonce[j])
63                 ++j;
64             /* encrypt the nonce to form next xor buffer    */
65             aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx);
66             pos = 0;
67         }
68
69         data[i++] ^= cx->encr_bfr[pos++];
70     }
71
72     cx->encr_pos = pos;
73 }
74
75 int fcrypt_init(
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)    */
82 #endif
83     fcrypt_ctx      cx[1])                  /* the file encryption context (output) */
84 {   unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];
85
86     if (pwd_len > MAX_PWD_LENGTH)
87         return PASSWORD_TOO_LONG;
88
89     if (mode < 1 || mode > 3)
90         return BAD_MODE;
91
92     cx->mode = mode;
93     cx->pwd_len = pwd_len;
94
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);
98
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));
104
105     /* initialise for encryption using key 1            */
106     aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
107
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);
111
112 #ifdef PASSWORD_VERIFIER
113     memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
114 #endif
115
116     return GOOD_RETURN;
117 }
118
119 /* perform 'in place' encryption and authentication */
120
121 void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
122 {
123     encr_data(data, data_len, cx);
124     hmac_sha_data(data, data_len, cx->auth_ctx);
125 }
126
127 /* perform 'in place' authentication and decryption */
128
129 void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
130 {
131     hmac_sha_data(data, data_len, cx->auth_ctx);
132     encr_data(data, data_len, cx);
133 }
134
135 /* close encryption/decryption and return the MAC value */
136
137 int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
138 {
139     hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);
140     return MAC_LENGTH(cx->mode);    /* return MAC length in bytes   */
141 }
142
143 #if defined(__cplusplus)
144 }
145 #endif