├── .DS_Store
├── Info.plist
├── README.md
├── SSZipArchive.h
├── SSZipArchive.m
├── SSZipCommon.h
├── ZipArchive.h
├── aes
├── aes.h
├── aes_via_ace.h
├── aescrypt.m
├── aeskey.m
├── aesopt.h
├── aestab.h
├── aestab.m
├── brg_endian.h
├── brg_types.h
├── entropy.h
├── entropy.m
├── fileenc.h
├── fileenc.m
├── hmac.h
├── hmac.m
├── prng.h
├── prng.m
├── pwd2key.h
├── pwd2key.m
├── sha1.h
└── sha1.m
└── minizip
├── crypt.h
├── ioapi.h
├── ioapi.m
├── mztools.h
├── mztools.m
├── unzip.h
├── unzip.m
├── zip.h
└── zip.m
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Feijunjie/SSZipArchive/b256ea4f637b22b5aed0f65a4a84dcb55fffc46f/.DS_Store
--------------------------------------------------------------------------------
/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | FMWK
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | $(CURRENT_PROJECT_VERSION)
23 | NSPrincipalClass
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SSZipArchive
2 | 解压的一个工具
3 |
--------------------------------------------------------------------------------
/SSZipArchive.h:
--------------------------------------------------------------------------------
1 | //
2 | // SSZipArchive.h
3 | // SSZipArchive
4 | //
5 | // Created by Sam Soffes on 7/21/10.
6 | // Copyright (c) Sam Soffes 2010-2015. All rights reserved.
7 | //
8 |
9 | #ifndef _SSZIPARCHIVE_H
10 | #define _SSZIPARCHIVE_H
11 |
12 | #import
13 | #include "SSZipCommon.h"
14 |
15 | NS_ASSUME_NONNULL_BEGIN
16 |
17 | @protocol SSZipArchiveDelegate;
18 |
19 | @interface SSZipArchive : NSObject
20 |
21 | // Password check
22 | + (BOOL)isFilePasswordProtectedAtPath:(NSString *)path;
23 |
24 | // Unzip
25 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination;
26 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination delegate:(nullable id)delegate;
27 |
28 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(nullable NSString *)password error:(NSError * *)error;
29 | + (BOOL)unzipFileAtPath:(NSString *)path toDestination:(NSString *)destination overwrite:(BOOL)overwrite password:(nullable NSString *)password error:(NSError * *)error delegate:(nullable id)delegate NS_REFINED_FOR_SWIFT;
30 |
31 | + (BOOL)unzipFileAtPath:(NSString *)path
32 | toDestination:(NSString *)destination
33 | preserveAttributes:(BOOL)preserveAttributes
34 | overwrite:(BOOL)overwrite
35 | password:(nullable NSString *)password
36 | error:(NSError * *)error
37 | delegate:(nullable id)delegate;
38 |
39 | + (BOOL)unzipFileAtPath:(NSString *)path
40 | toDestination:(NSString *)destination
41 | progressHandler:(void (^)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
42 | completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError *error))completionHandler;
43 |
44 | + (BOOL)unzipFileAtPath:(NSString *)path
45 | toDestination:(NSString *)destination
46 | overwrite:(BOOL)overwrite
47 | password:(nullable NSString *)password
48 | progressHandler:(void (^)(NSString *entry, unz_file_info zipInfo, long entryNumber, long total))progressHandler
49 | completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError *error))completionHandler;
50 |
51 | // Zip
52 |
53 | // without password
54 | + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths;
55 | + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath;
56 |
57 | + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory;
58 |
59 | // with password, password could be nil
60 | + (BOOL)createZipFileAtPath:(NSString *)path withFilesAtPaths:(NSArray *)paths withPassword:(nullable NSString *)password;
61 | + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath withPassword:(nullable NSString *)password;
62 | + (BOOL)createZipFileAtPath:(NSString *)path withContentsOfDirectory:(NSString *)directoryPath keepParentDirectory:(BOOL)keepParentDirectory withPassword:(nullable NSString *)password;
63 |
64 | - (instancetype)initWithPath:(NSString *)path;
65 | @property (NS_NONATOMIC_IOSONLY, readonly) BOOL open;
66 | - (BOOL)writeFile:(NSString *)path withPassword:(nullable NSString *)password;
67 | - (BOOL)writeFolderAtPath:(NSString *)path withFolderName:(NSString *)folderName withPassword:(nullable NSString *)password;
68 | - (BOOL)writeFileAtPath:(NSString *)path withFileName:(nullable NSString *)fileName withPassword:(nullable NSString *)password;
69 | - (BOOL)writeData:(NSData *)data filename:(nullable NSString *)filename withPassword:(nullable NSString *)password;
70 | @property (NS_NONATOMIC_IOSONLY, readonly) BOOL close;
71 |
72 | @end
73 |
74 | @protocol SSZipArchiveDelegate
75 |
76 | @optional
77 |
78 | - (void)zipArchiveWillUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo;
79 | - (void)zipArchiveDidUnzipArchiveAtPath:(NSString *)path zipInfo:(unz_global_info)zipInfo unzippedPath:(NSString *)unzippedPath;
80 |
81 | - (BOOL)zipArchiveShouldUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
82 | - (void)zipArchiveWillUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
83 | - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath fileInfo:(unz_file_info)fileInfo;
84 | - (void)zipArchiveDidUnzipFileAtIndex:(NSInteger)fileIndex totalFiles:(NSInteger)totalFiles archivePath:(NSString *)archivePath unzippedFilePath:(NSString *)unzippedFilePath;
85 |
86 | - (void)zipArchiveProgressEvent:(unsigned long long)loaded total:(unsigned long long)total;
87 | - (void)zipArchiveDidUnzipArchiveFile:(NSString *)zipFile entryPath:(NSString *)entryPath destPath:(NSString *)destPath;
88 |
89 | @end
90 |
91 | NS_ASSUME_NONNULL_END
92 |
93 | #endif /* _SSZIPARCHIVE_H */
94 |
--------------------------------------------------------------------------------
/SSZipCommon.h:
--------------------------------------------------------------------------------
1 | #ifndef SSZipCommon
2 | #define SSZipCommon
3 |
4 | /* tm_unz contain date/time info */
5 | typedef struct tm_unz_s
6 | {
7 | unsigned int tm_sec; /* seconds after the minute - [0,59] */
8 | unsigned int tm_min; /* minutes after the hour - [0,59] */
9 | unsigned int tm_hour; /* hours since midnight - [0,23] */
10 | unsigned int tm_mday; /* day of the month - [1,31] */
11 | unsigned int tm_mon; /* months since January - [0,11] */
12 | unsigned int tm_year; /* years - [1980..2044] */
13 | } tm_unz;
14 |
15 | typedef struct unz_file_info_s
16 | {
17 | unsigned long version; /* version made by 2 bytes */
18 | unsigned long version_needed; /* version needed to extract 2 bytes */
19 | unsigned long flag; /* general purpose bit flag 2 bytes */
20 | unsigned long compression_method; /* compression method 2 bytes */
21 | unsigned long dosDate; /* last mod file date in Dos fmt 4 bytes */
22 | unsigned long crc; /* crc-32 4 bytes */
23 | unsigned long compressed_size; /* compressed size 4 bytes */
24 | unsigned long uncompressed_size; /* uncompressed size 4 bytes */
25 | unsigned long size_filename; /* filename length 2 bytes */
26 | unsigned long size_file_extra; /* extra field length 2 bytes */
27 | unsigned long size_file_comment; /* file comment length 2 bytes */
28 |
29 | unsigned long disk_num_start; /* disk number start 2 bytes */
30 | unsigned long internal_fa; /* internal file attributes 2 bytes */
31 | unsigned long external_fa; /* external file attributes 4 bytes */
32 |
33 | tm_unz tmu_date;
34 | } unz_file_info;
35 |
36 | /* unz_file_info contain information about a file in the zipfile */
37 | typedef struct unz_file_info64_s
38 | {
39 | unsigned long version; /* version made by 2 bytes */
40 | unsigned long version_needed; /* version needed to extract 2 bytes */
41 | unsigned long flag; /* general purpose bit flag 2 bytes */
42 | unsigned long compression_method; /* compression method 2 bytes */
43 | unsigned long dosDate; /* last mod file date in Dos fmt 4 bytes */
44 | unsigned long crc; /* crc-32 4 bytes */
45 | unsigned long long compressed_size; /* compressed size 8 bytes */
46 | unsigned long long uncompressed_size; /* uncompressed size 8 bytes */
47 | unsigned long size_filename; /* filename length 2 bytes */
48 | unsigned long size_file_extra; /* extra field length 2 bytes */
49 | unsigned long size_file_comment; /* file comment length 2 bytes */
50 |
51 | unsigned long disk_num_start; /* disk number start 2 bytes */
52 | unsigned long internal_fa; /* internal file attributes 2 bytes */
53 | unsigned long external_fa; /* external file attributes 4 bytes */
54 |
55 | tm_unz tmu_date;
56 | unsigned long long disk_offset;
57 | unsigned long size_file_extra_internal;
58 | } unz_file_info64;
59 |
60 | typedef struct unz_global_info_s
61 | {
62 | unsigned long number_entry; /* total number of entries in
63 | the central dir on this disk */
64 |
65 | unsigned long number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
66 |
67 |
68 | unsigned long size_comment; /* size of the global comment of the zipfile */
69 | } unz_global_info;
70 |
71 | typedef struct unz_global_info64
72 | {
73 | unsigned long long number_entry; /* total number of entries in
74 | the central dir on this disk */
75 |
76 | unsigned long number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP*/
77 |
78 | unsigned long size_comment; /* size of the global comment of the zipfile */
79 | } unz_global_info64;
80 |
81 | #endif
82 |
--------------------------------------------------------------------------------
/ZipArchive.h:
--------------------------------------------------------------------------------
1 | //
2 | // ZipArchive.h
3 | // ZipArchive
4 | //
5 | // Created by Serhii Mumriak on 12/1/15.
6 | // Copyright © 2015 smumryak. All rights reserved.
7 | //
8 |
9 | #import
10 |
11 | //! Project version number for ZipArchive.
12 | FOUNDATION_EXPORT double ZipArchiveVersionNumber;
13 |
14 | //! Project version string for ZipArchive.
15 | FOUNDATION_EXPORT const unsigned char ZipArchiveVersionString[];
16 |
17 | // In this header, you should import all the public headers of your framework using statements like #import
18 |
19 | #import "SSZipArchive.h"
20 |
--------------------------------------------------------------------------------
/aes/aes.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 |
20 | This file contains the definitions required to use AES in C. See aesopt.h
21 | for optimisation details.
22 | */
23 |
24 | #ifndef _AES_H
25 | #define _AES_H
26 |
27 | #include
28 |
29 | /* This include is used to find 8 & 32 bit unsigned integer types */
30 | #include "brg_types.h"
31 |
32 | #if defined(__cplusplus)
33 | extern "C"
34 | {
35 | #endif
36 |
37 | #define AES_128 /* if a fast 128 bit key scheduler is needed */
38 | #define AES_192 /* if a fast 192 bit key scheduler is needed */
39 | #define AES_256 /* if a fast 256 bit key scheduler is needed */
40 | #define AES_VAR /* if variable key size scheduler is needed */
41 | #define AES_MODES /* if support is needed for modes */
42 |
43 | /* The following must also be set in assembler files if being used */
44 |
45 | #define AES_ENCRYPT /* if support for encryption is needed */
46 | #define AES_DECRYPT /* if support for decryption is needed */
47 | #define AES_REV_DKS /* define to reverse decryption key schedule */
48 |
49 | #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
50 | #define N_COLS 4 /* the number of columns in the state */
51 |
52 | /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
53 | /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
54 | /* or 44, 52 or 60 32-bit words. */
55 |
56 | #if defined( AES_VAR ) || defined( AES_256 )
57 | #define KS_LENGTH 60
58 | #elif defined( AES_192 )
59 | #define KS_LENGTH 52
60 | #else
61 | #define KS_LENGTH 44
62 | #endif
63 |
64 | #define AES_RETURN INT_RETURN
65 |
66 | /* the character array 'inf' in the following structures is used */
67 | /* to hold AES context information. This AES code uses cx->inf.b[0] */
68 | /* to hold the number of rounds multiplied by 16. The other three */
69 | /* elements can be used by code that implements additional modes */
70 |
71 | typedef union
72 | { uint_32t l;
73 | uint_8t b[4];
74 | } aes_inf;
75 |
76 | typedef struct
77 | { uint_32t ks[KS_LENGTH];
78 | aes_inf inf;
79 | } aes_encrypt_ctx;
80 |
81 | typedef struct
82 | { uint_32t ks[KS_LENGTH];
83 | aes_inf inf;
84 | } aes_decrypt_ctx;
85 |
86 | /* This routine must be called before first use if non-static */
87 | /* tables are being used */
88 |
89 | AES_RETURN aes_init(void);
90 |
91 | /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
92 | /* those in the range 128 <= key_len <= 256 are given in bits */
93 |
94 | #if defined( AES_ENCRYPT )
95 |
96 | #if defined( AES_128 ) || defined( AES_VAR)
97 | AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
98 | #endif
99 |
100 | #if defined( AES_192 ) || defined( AES_VAR)
101 | AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
102 | #endif
103 |
104 | #if defined( AES_256 ) || defined( AES_VAR)
105 | AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
106 | #endif
107 |
108 | #if defined( AES_VAR )
109 | AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
110 | #endif
111 |
112 | AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
113 |
114 | #endif
115 |
116 | #if defined( AES_DECRYPT )
117 |
118 | #if defined( AES_128 ) || defined( AES_VAR)
119 | AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
120 | #endif
121 |
122 | #if defined( AES_192 ) || defined( AES_VAR)
123 | AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
124 | #endif
125 |
126 | #if defined( AES_256 ) || defined( AES_VAR)
127 | AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
128 | #endif
129 |
130 | #if defined( AES_VAR )
131 | AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
132 | #endif
133 |
134 | AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
135 |
136 | #endif
137 |
138 | #if defined( AES_MODES )
139 |
140 | /* Multiple calls to the following subroutines for multiple block */
141 | /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
142 | /* long messages incremantally provided that the context AND the iv */
143 | /* are preserved between all such calls. For the ECB and CBC modes */
144 | /* each individual call within a series of incremental calls must */
145 | /* process only full blocks (i.e. len must be a multiple of 16) but */
146 | /* the CFB, OFB and CTR mode calls can handle multiple incremental */
147 | /* calls of any length. Each mode is reset when a new AES key is */
148 | /* set but ECB and CBC operations can be reset without setting a */
149 | /* new key by setting a new IV value. To reset CFB, OFB and CTR */
150 | /* without setting the key, aes_mode_reset() must be called and the */
151 | /* IV must be set. NOTE: All these calls update the IV on exit so */
152 | /* this has to be reset if a new operation with the same IV as the */
153 | /* previous one is required (or decryption follows encryption with */
154 | /* the same IV array). */
155 |
156 | AES_RETURN aes_test_alignment_detection(unsigned int n);
157 |
158 | AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
159 | int len, const aes_encrypt_ctx cx[1]);
160 |
161 | AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
162 | int len, const aes_decrypt_ctx cx[1]);
163 |
164 | AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
165 | int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
166 |
167 | AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
168 | int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
169 |
170 | AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);
171 |
172 | AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
173 | int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
174 |
175 | AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
176 | int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
177 |
178 | #define aes_ofb_encrypt aes_ofb_crypt
179 | #define aes_ofb_decrypt aes_ofb_crypt
180 |
181 | AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
182 | int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
183 |
184 | typedef void cbuf_inc(unsigned char *cbuf);
185 |
186 | #define aes_ctr_encrypt aes_ctr_crypt
187 | #define aes_ctr_decrypt aes_ctr_crypt
188 |
189 | AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
190 | int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
191 |
192 | #endif
193 |
194 | #if defined(__cplusplus)
195 | }
196 | #endif
197 |
198 | #endif
199 |
--------------------------------------------------------------------------------
/aes/aes_via_ace.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
3 |
4 | The redistribution and use of this software (with or without changes)
5 | is allowed without the payment of fees or royalties provided that:
6 |
7 | source code distributions include the above copyright notice, this
8 | list of conditions and the following disclaimer;
9 |
10 | binary distributions include the above copyright notice, this list
11 | of conditions and the following disclaimer in their documentation.
12 |
13 | This software is provided 'as is' with no explicit or implied warranties
14 | in respect of its operation, including, but not limited to, correctness
15 | and fitness for purpose.
16 | ---------------------------------------------------------------------------
17 | Issue Date: 20/12/2007
18 | */
19 |
20 | #ifndef AES_VIA_ACE_H
21 | #define AES_VIA_ACE_H
22 |
23 | #if defined( _MSC_VER )
24 | # define INLINE __inline
25 | #elif defined( __GNUC__ )
26 | # define INLINE static inline
27 | #else
28 | # error VIA ACE requires Microsoft or GNU C
29 | #endif
30 |
31 | #define NEH_GENERATE 1
32 | #define NEH_LOAD 2
33 | #define NEH_HYBRID 3
34 |
35 | #define MAX_READ_ATTEMPTS 1000
36 |
37 | /* VIA Nehemiah RNG and ACE Feature Mask Values */
38 |
39 | #define NEH_CPU_IS_VIA 0x00000001
40 | #define NEH_CPU_READ 0x00000010
41 | #define NEH_CPU_MASK 0x00000011
42 |
43 | #define NEH_RNG_PRESENT 0x00000004
44 | #define NEH_RNG_ENABLED 0x00000008
45 | #define NEH_ACE_PRESENT 0x00000040
46 | #define NEH_ACE_ENABLED 0x00000080
47 | #define NEH_RNG_FLAGS (NEH_RNG_PRESENT | NEH_RNG_ENABLED)
48 | #define NEH_ACE_FLAGS (NEH_ACE_PRESENT | NEH_ACE_ENABLED)
49 | #define NEH_FLAGS_MASK (NEH_RNG_FLAGS | NEH_ACE_FLAGS)
50 |
51 | /* VIA Nehemiah Advanced Cryptography Engine (ACE) Control Word Values */
52 |
53 | #define NEH_GEN_KEY 0x00000000 /* generate key schedule */
54 | #define NEH_LOAD_KEY 0x00000080 /* load schedule from memory */
55 | #define NEH_ENCRYPT 0x00000000 /* encryption */
56 | #define NEH_DECRYPT 0x00000200 /* decryption */
57 | #define NEH_KEY128 0x00000000+0x0a /* 128 bit key */
58 | #define NEH_KEY192 0x00000400+0x0c /* 192 bit key */
59 | #define NEH_KEY256 0x00000800+0x0e /* 256 bit key */
60 |
61 | #define NEH_ENC_GEN (NEH_ENCRYPT | NEH_GEN_KEY)
62 | #define NEH_DEC_GEN (NEH_DECRYPT | NEH_GEN_KEY)
63 | #define NEH_ENC_LOAD (NEH_ENCRYPT | NEH_LOAD_KEY)
64 | #define NEH_DEC_LOAD (NEH_DECRYPT | NEH_LOAD_KEY)
65 |
66 | #define NEH_ENC_GEN_DATA {\
67 | NEH_ENC_GEN | NEH_KEY128, 0, 0, 0,\
68 | NEH_ENC_GEN | NEH_KEY192, 0, 0, 0,\
69 | NEH_ENC_GEN | NEH_KEY256, 0, 0, 0 }
70 |
71 | #define NEH_ENC_LOAD_DATA {\
72 | NEH_ENC_LOAD | NEH_KEY128, 0, 0, 0,\
73 | NEH_ENC_LOAD | NEH_KEY192, 0, 0, 0,\
74 | NEH_ENC_LOAD | NEH_KEY256, 0, 0, 0 }
75 |
76 | #define NEH_ENC_HYBRID_DATA {\
77 | NEH_ENC_GEN | NEH_KEY128, 0, 0, 0,\
78 | NEH_ENC_LOAD | NEH_KEY192, 0, 0, 0,\
79 | NEH_ENC_LOAD | NEH_KEY256, 0, 0, 0 }
80 |
81 | #define NEH_DEC_GEN_DATA {\
82 | NEH_DEC_GEN | NEH_KEY128, 0, 0, 0,\
83 | NEH_DEC_GEN | NEH_KEY192, 0, 0, 0,\
84 | NEH_DEC_GEN | NEH_KEY256, 0, 0, 0 }
85 |
86 | #define NEH_DEC_LOAD_DATA {\
87 | NEH_DEC_LOAD | NEH_KEY128, 0, 0, 0,\
88 | NEH_DEC_LOAD | NEH_KEY192, 0, 0, 0,\
89 | NEH_DEC_LOAD | NEH_KEY256, 0, 0, 0 }
90 |
91 | #define NEH_DEC_HYBRID_DATA {\
92 | NEH_DEC_GEN | NEH_KEY128, 0, 0, 0,\
93 | NEH_DEC_LOAD | NEH_KEY192, 0, 0, 0,\
94 | NEH_DEC_LOAD | NEH_KEY256, 0, 0, 0 }
95 |
96 | #define neh_enc_gen_key(x) ((x) == 128 ? (NEH_ENC_GEN | NEH_KEY128) : \
97 | (x) == 192 ? (NEH_ENC_GEN | NEH_KEY192) : (NEH_ENC_GEN | NEH_KEY256))
98 |
99 | #define neh_enc_load_key(x) ((x) == 128 ? (NEH_ENC_LOAD | NEH_KEY128) : \
100 | (x) == 192 ? (NEH_ENC_LOAD | NEH_KEY192) : (NEH_ENC_LOAD | NEH_KEY256))
101 |
102 | #define neh_enc_hybrid_key(x) ((x) == 128 ? (NEH_ENC_GEN | NEH_KEY128) : \
103 | (x) == 192 ? (NEH_ENC_LOAD | NEH_KEY192) : (NEH_ENC_LOAD | NEH_KEY256))
104 |
105 | #define neh_dec_gen_key(x) ((x) == 128 ? (NEH_DEC_GEN | NEH_KEY128) : \
106 | (x) == 192 ? (NEH_DEC_GEN | NEH_KEY192) : (NEH_DEC_GEN | NEH_KEY256))
107 |
108 | #define neh_dec_load_key(x) ((x) == 128 ? (NEH_DEC_LOAD | NEH_KEY128) : \
109 | (x) == 192 ? (NEH_DEC_LOAD | NEH_KEY192) : (NEH_DEC_LOAD | NEH_KEY256))
110 |
111 | #define neh_dec_hybrid_key(x) ((x) == 128 ? (NEH_DEC_GEN | NEH_KEY128) : \
112 | (x) == 192 ? (NEH_DEC_LOAD | NEH_KEY192) : (NEH_DEC_LOAD | NEH_KEY256))
113 |
114 | #if defined( _MSC_VER ) && ( _MSC_VER > 1200 )
115 | #define aligned_auto(type, name, no, stride) __declspec(align(stride)) type name[no]
116 | #else
117 | #define aligned_auto(type, name, no, stride) \
118 | unsigned char _##name[no * sizeof(type) + stride]; \
119 | type *name = (type*)(16 * ((((unsigned long)(_##name)) + stride - 1) / stride))
120 | #endif
121 |
122 | #if defined( _MSC_VER ) && ( _MSC_VER > 1200 )
123 | #define aligned_array(type, name, no, stride) __declspec(align(stride)) type name[no]
124 | #elif defined( __GNUC__ )
125 | #define aligned_array(type, name, no, stride) type name[no] __attribute__ ((aligned(stride)))
126 | #else
127 | #define aligned_array(type, name, no, stride) type name[no]
128 | #endif
129 |
130 | /* VIA ACE codeword */
131 |
132 | static unsigned char via_flags = 0;
133 |
134 | #if defined ( _MSC_VER ) && ( _MSC_VER > 800 )
135 |
136 | #define NEH_REKEY __asm pushfd __asm popfd
137 | #define NEH_AES __asm _emit 0xf3 __asm _emit 0x0f __asm _emit 0xa7
138 | #define NEH_ECB NEH_AES __asm _emit 0xc8
139 | #define NEH_CBC NEH_AES __asm _emit 0xd0
140 | #define NEH_CFB NEH_AES __asm _emit 0xe0
141 | #define NEH_OFB NEH_AES __asm _emit 0xe8
142 | #define NEH_RNG __asm _emit 0x0f __asm _emit 0xa7 __asm _emit 0xc0
143 |
144 | INLINE int has_cpuid(void)
145 | { char ret_value;
146 | __asm
147 | { pushfd /* save EFLAGS register */
148 | mov eax,[esp] /* copy it to eax */
149 | mov edx,0x00200000 /* CPUID bit position */
150 | xor eax,edx /* toggle the CPUID bit */
151 | push eax /* attempt to set EFLAGS to */
152 | popfd /* the new value */
153 | pushfd /* get the new EFLAGS value */
154 | pop eax /* into eax */
155 | xor eax,[esp] /* xor with original value */
156 | and eax,edx /* has CPUID bit changed? */
157 | setne al /* set to 1 if we have been */
158 | mov ret_value,al /* able to change it */
159 | popfd /* restore original EFLAGS */
160 | }
161 | return (int)ret_value;
162 | }
163 |
164 | INLINE int is_via_cpu(void)
165 | { char ret_value;
166 | __asm
167 | { push ebx
168 | xor eax,eax /* use CPUID to get vendor */
169 | cpuid /* identity string */
170 | xor eax,eax /* is it "CentaurHauls" ? */
171 | sub ebx,0x746e6543 /* 'Cent' */
172 | or eax,ebx
173 | sub edx,0x48727561 /* 'aurH' */
174 | or eax,edx
175 | sub ecx,0x736c7561 /* 'auls' */
176 | or eax,ecx
177 | sete al /* set to 1 if it is VIA ID */
178 | mov dl,NEH_CPU_READ /* mark CPU type as read */
179 | or dl,al /* & store result in flags */
180 | mov [via_flags],dl /* set VIA detected flag */
181 | mov ret_value,al /* able to change it */
182 | pop ebx
183 | }
184 | return (int)ret_value;
185 | }
186 |
187 | INLINE int read_via_flags(void)
188 | { char ret_value = 0;
189 | __asm
190 | { mov eax,0xC0000000 /* Centaur extended CPUID */
191 | cpuid
192 | mov edx,0xc0000001 /* >= 0xc0000001 if support */
193 | cmp eax,edx /* for VIA extended feature */
194 | jnae no_rng /* flags is available */
195 | mov eax,edx /* read Centaur extended */
196 | cpuid /* feature flags */
197 | mov eax,NEH_FLAGS_MASK /* mask out and save */
198 | and eax,edx /* the RNG and ACE flags */
199 | or [via_flags],al /* present & enabled flags */
200 | mov ret_value,al /* able to change it */
201 | no_rng:
202 | }
203 | return (int)ret_value;
204 | }
205 |
206 | INLINE unsigned int via_rng_in(void *buf)
207 | { char ret_value = 0x1f;
208 | __asm
209 | { push edi
210 | mov edi,buf /* input buffer address */
211 | xor edx,edx /* try to fetch 8 bytes */
212 | NEH_RNG /* do RNG read operation */
213 | and ret_value,al /* count of bytes returned */
214 | pop edi
215 | }
216 | return (int)ret_value;
217 | }
218 |
219 | INLINE void via_ecb_op5(
220 | const void *k, const void *c, const void *s, void *d, int l)
221 | { __asm
222 | { push ebx
223 | NEH_REKEY
224 | mov ebx, (k)
225 | mov edx, (c)
226 | mov esi, (s)
227 | mov edi, (d)
228 | mov ecx, (l)
229 | NEH_ECB
230 | pop ebx
231 | }
232 | }
233 |
234 | INLINE void via_cbc_op6(
235 | const void *k, const void *c, const void *s, void *d, int l, void *v)
236 | { __asm
237 | { push ebx
238 | NEH_REKEY
239 | mov ebx, (k)
240 | mov edx, (c)
241 | mov esi, (s)
242 | mov edi, (d)
243 | mov ecx, (l)
244 | mov eax, (v)
245 | NEH_CBC
246 | pop ebx
247 | }
248 | }
249 |
250 | INLINE void via_cbc_op7(
251 | const void *k, const void *c, const void *s, void *d, int l, void *v, void *w)
252 | { __asm
253 | { push ebx
254 | NEH_REKEY
255 | mov ebx, (k)
256 | mov edx, (c)
257 | mov esi, (s)
258 | mov edi, (d)
259 | mov ecx, (l)
260 | mov eax, (v)
261 | NEH_CBC
262 | mov esi, eax
263 | mov edi, (w)
264 | movsd
265 | movsd
266 | movsd
267 | movsd
268 | pop ebx
269 | }
270 | }
271 |
272 | INLINE void via_cfb_op6(
273 | const void *k, const void *c, const void *s, void *d, int l, void *v)
274 | { __asm
275 | { push ebx
276 | NEH_REKEY
277 | mov ebx, (k)
278 | mov edx, (c)
279 | mov esi, (s)
280 | mov edi, (d)
281 | mov ecx, (l)
282 | mov eax, (v)
283 | NEH_CFB
284 | pop ebx
285 | }
286 | }
287 |
288 | INLINE void via_cfb_op7(
289 | const void *k, const void *c, const void *s, void *d, int l, void *v, void *w)
290 | { __asm
291 | { push ebx
292 | NEH_REKEY
293 | mov ebx, (k)
294 | mov edx, (c)
295 | mov esi, (s)
296 | mov edi, (d)
297 | mov ecx, (l)
298 | mov eax, (v)
299 | NEH_CFB
300 | mov esi, eax
301 | mov edi, (w)
302 | movsd
303 | movsd
304 | movsd
305 | movsd
306 | pop ebx
307 | }
308 | }
309 |
310 | INLINE void via_ofb_op6(
311 | const void *k, const void *c, const void *s, void *d, int l, void *v)
312 | { __asm
313 | { push ebx
314 | NEH_REKEY
315 | mov ebx, (k)
316 | mov edx, (c)
317 | mov esi, (s)
318 | mov edi, (d)
319 | mov ecx, (l)
320 | mov eax, (v)
321 | NEH_OFB
322 | pop ebx
323 | }
324 | }
325 |
326 | #elif defined( __GNUC__ )
327 |
328 | #define NEH_REKEY asm("pushfl\n popfl\n\t")
329 | #define NEH_ECB asm(".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t")
330 | #define NEH_CBC asm(".byte 0xf3, 0x0f, 0xa7, 0xd0\n\t")
331 | #define NEH_CFB asm(".byte 0xf3, 0x0f, 0xa7, 0xe0\n\t")
332 | #define NEH_OFB asm(".byte 0xf3, 0x0f, 0xa7, 0xe8\n\t")
333 | #define NEH_RNG asm(".byte 0x0f, 0xa7, 0xc0\n\t");
334 |
335 | INLINE int has_cpuid(void)
336 | { int val;
337 | asm("pushfl\n\t");
338 | asm("movl 0(%esp),%eax\n\t");
339 | asm("xor $0x00200000,%eax\n\t");
340 | asm("pushl %eax\n\t");
341 | asm("popfl\n\t");
342 | asm("pushfl\n\t");
343 | asm("popl %eax\n\t");
344 | asm("xorl 0(%esp),%edx\n\t");
345 | asm("andl $0x00200000,%eax\n\t");
346 | asm("movl %%eax,%0\n\t" : "=m" (val));
347 | asm("popfl\n\t");
348 | return val ? 1 : 0;
349 | }
350 |
351 | INLINE int is_via_cpu(void)
352 | { int val;
353 | asm("pushl %ebx\n\t");
354 | asm("xorl %eax,%eax\n\t");
355 | asm("cpuid\n\t");
356 | asm("xorl %eax,%eax\n\t");
357 | asm("subl $0x746e6543,%ebx\n\t");
358 | asm("orl %ebx,%eax\n\t");
359 | asm("subl $0x48727561,%edx\n\t");
360 | asm("orl %edx,%eax\n\t");
361 | asm("subl $0x736c7561,%ecx\n\t");
362 | asm("orl %ecx,%eax\n\t");
363 | asm("movl %%eax,%0\n\t" : "=m" (val));
364 | asm("popl %ebx\n\t");
365 | val = (val ? 0 : 1);
366 | via_flags = (val | NEH_CPU_READ);
367 | return val;
368 | }
369 |
370 | INLINE int read_via_flags(void)
371 | { unsigned char val;
372 | asm("movl $0xc0000000,%eax\n\t");
373 | asm("cpuid\n\t");
374 | asm("movl $0xc0000001,%edx\n\t");
375 | asm("cmpl %edx,%eax\n\t");
376 | asm("setae %al\n\t");
377 | asm("movb %%al,%0\n\t" : "=m" (val));
378 | if(!val) return 0;
379 | asm("movl $0xc0000001,%eax\n\t");
380 | asm("cpuid\n\t");
381 | asm("movb %%dl,%0\n\t" : "=m" (val));
382 | val &= NEH_FLAGS_MASK;
383 | via_flags |= val;
384 | return (int) val;
385 | }
386 |
387 | INLINE int via_rng_in(void *buf)
388 | { int val;
389 | asm("pushl %edi\n\t");
390 | asm("movl %0,%%edi\n\t" : : "m" (buf));
391 | asm("xorl %edx,%edx\n\t");
392 | NEH_RNG
393 | asm("andl $0x0000001f,%eax\n\t");
394 | asm("movl %%eax,%0\n\t" : "=m" (val));
395 | asm("popl %edi\n\t");
396 | return val;
397 | }
398 |
399 | INLINE volatile void via_ecb_op5(
400 | const void *k, const void *c, const void *s, void *d, int l)
401 | {
402 | asm("pushl %ebx\n\t");
403 | NEH_REKEY;
404 | asm("movl %0, %%ebx\n\t" : : "m" (k));
405 | asm("movl %0, %%edx\n\t" : : "m" (c));
406 | asm("movl %0, %%esi\n\t" : : "m" (s));
407 | asm("movl %0, %%edi\n\t" : : "m" (d));
408 | asm("movl %0, %%ecx\n\t" : : "m" (l));
409 | NEH_ECB;
410 | asm("popl %ebx\n\t");
411 | }
412 |
413 | INLINE volatile void via_cbc_op6(
414 | const void *k, const void *c, const void *s, void *d, int l, void *v)
415 | {
416 | asm("pushl %ebx\n\t");
417 | NEH_REKEY;
418 | asm("movl %0, %%ebx\n\t" : : "m" (k));
419 | asm("movl %0, %%edx\n\t" : : "m" (c));
420 | asm("movl %0, %%esi\n\t" : : "m" (s));
421 | asm("movl %0, %%edi\n\t" : : "m" (d));
422 | asm("movl %0, %%ecx\n\t" : : "m" (l));
423 | asm("movl %0, %%eax\n\t" : : "m" (v));
424 | NEH_CBC;
425 | asm("popl %ebx\n\t");
426 | }
427 |
428 | INLINE volatile void via_cbc_op7(
429 | const void *k, const void *c, const void *s, void *d, int l, void *v, void *w)
430 | {
431 | asm("pushl %ebx\n\t");
432 | NEH_REKEY;
433 | asm("movl %0, %%ebx\n\t" : : "m" (k));
434 | asm("movl %0, %%edx\n\t" : : "m" (c));
435 | asm("movl %0, %%esi\n\t" : : "m" (s));
436 | asm("movl %0, %%edi\n\t" : : "m" (d));
437 | asm("movl %0, %%ecx\n\t" : : "m" (l));
438 | asm("movl %0, %%eax\n\t" : : "m" (v));
439 | NEH_CBC;
440 | asm("movl %eax,%esi\n\t");
441 | asm("movl %0, %%edi\n\t" : : "m" (w));
442 | asm("movsl; movsl; movsl; movsl\n\t");
443 | asm("popl %ebx\n\t");
444 | }
445 |
446 | INLINE volatile void via_cfb_op6(
447 | const void *k, const void *c, const void *s, void *d, int l, void *v)
448 | {
449 | asm("pushl %ebx\n\t");
450 | NEH_REKEY;
451 | asm("movl %0, %%ebx\n\t" : : "m" (k));
452 | asm("movl %0, %%edx\n\t" : : "m" (c));
453 | asm("movl %0, %%esi\n\t" : : "m" (s));
454 | asm("movl %0, %%edi\n\t" : : "m" (d));
455 | asm("movl %0, %%ecx\n\t" : : "m" (l));
456 | asm("movl %0, %%eax\n\t" : : "m" (v));
457 | NEH_CFB;
458 | asm("popl %ebx\n\t");
459 | }
460 |
461 | INLINE volatile void via_cfb_op7(
462 | const void *k, const void *c, const void *s, void *d, int l, void *v, void *w)
463 | {
464 | asm("pushl %ebx\n\t");
465 | NEH_REKEY;
466 | asm("movl %0, %%ebx\n\t" : : "m" (k));
467 | asm("movl %0, %%edx\n\t" : : "m" (c));
468 | asm("movl %0, %%esi\n\t" : : "m" (s));
469 | asm("movl %0, %%edi\n\t" : : "m" (d));
470 | asm("movl %0, %%ecx\n\t" : : "m" (l));
471 | asm("movl %0, %%eax\n\t" : : "m" (v));
472 | NEH_CFB;
473 | asm("movl %eax,%esi\n\t");
474 | asm("movl %0, %%edi\n\t" : : "m" (w));
475 | asm("movsl; movsl; movsl; movsl\n\t");
476 | asm("popl %ebx\n\t");
477 | }
478 |
479 | INLINE volatile void via_ofb_op6(
480 | const void *k, const void *c, const void *s, void *d, int l, void *v)
481 | {
482 | asm("pushl %ebx\n\t");
483 | NEH_REKEY;
484 | asm("movl %0, %%ebx\n\t" : : "m" (k));
485 | asm("movl %0, %%edx\n\t" : : "m" (c));
486 | asm("movl %0, %%esi\n\t" : : "m" (s));
487 | asm("movl %0, %%edi\n\t" : : "m" (d));
488 | asm("movl %0, %%ecx\n\t" : : "m" (l));
489 | asm("movl %0, %%eax\n\t" : : "m" (v));
490 | NEH_OFB;
491 | asm("popl %ebx\n\t");
492 | }
493 |
494 | #else
495 | #error VIA ACE is not available with this compiler
496 | #endif
497 |
498 | INLINE int via_ace_test(void)
499 | {
500 | return has_cpuid() && is_via_cpu() && ((read_via_flags() & NEH_ACE_FLAGS) == NEH_ACE_FLAGS);
501 | }
502 |
503 | #define VIA_ACE_AVAILABLE (((via_flags & NEH_ACE_FLAGS) == NEH_ACE_FLAGS) \
504 | || (via_flags & NEH_CPU_READ) && (via_flags & NEH_CPU_IS_VIA) || via_ace_test())
505 |
506 | INLINE int via_rng_test(void)
507 | {
508 | return has_cpuid() && is_via_cpu() && ((read_via_flags() & NEH_RNG_FLAGS) == NEH_RNG_FLAGS);
509 | }
510 |
511 | #define VIA_RNG_AVAILABLE (((via_flags & NEH_RNG_FLAGS) == NEH_RNG_FLAGS) \
512 | || (via_flags & NEH_CPU_READ) && (via_flags & NEH_CPU_IS_VIA) || via_rng_test())
513 |
514 | INLINE int read_via_rng(void *buf, int count)
515 | { int nbr, max_reads, lcnt = count;
516 | unsigned char *p, *q;
517 | aligned_auto(unsigned char, bp, 64, 16);
518 |
519 | if(!VIA_RNG_AVAILABLE)
520 | return 0;
521 |
522 | do
523 | {
524 | max_reads = MAX_READ_ATTEMPTS;
525 | do
526 | nbr = via_rng_in(bp);
527 | while
528 | (nbr == 0 && --max_reads);
529 |
530 | lcnt -= nbr;
531 | p = (unsigned char*)buf; q = bp;
532 | while(nbr--)
533 | *p++ = *q++;
534 | }
535 | while
536 | (lcnt && max_reads);
537 |
538 | return count - lcnt;
539 | }
540 |
541 | #endif
542 |
--------------------------------------------------------------------------------
/aes/aescrypt.m:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 | */
20 |
21 | #include "aesopt.h"
22 | #include "aestab.h"
23 |
24 | #if defined(__cplusplus)
25 | extern "C"
26 | {
27 | #endif
28 |
29 | #define si(y,x,k,c) (s(y,c) = word_in(x, c) ^ (k)[c])
30 | #define so(y,x,c) word_out(y, c, s(x,c))
31 |
32 | #if defined(ARRAYS)
33 | #define locals(y,x) x[4],y[4]
34 | #else
35 | #define locals(y,x) x##0,x##1,x##2,x##3,y##0,y##1,y##2,y##3
36 | #endif
37 |
38 | #define l_copy(y, x) s(y,0) = s(x,0); s(y,1) = s(x,1); \
39 | s(y,2) = s(x,2); s(y,3) = s(x,3);
40 | #define state_in(y,x,k) si(y,x,k,0); si(y,x,k,1); si(y,x,k,2); si(y,x,k,3)
41 | #define state_out(y,x) so(y,x,0); so(y,x,1); so(y,x,2); so(y,x,3)
42 | #define round(rm,y,x,k) rm(y,x,k,0); rm(y,x,k,1); rm(y,x,k,2); rm(y,x,k,3)
43 |
44 | #if ( FUNCS_IN_C & ENCRYPTION_IN_C )
45 |
46 | /* Visual C++ .Net v7.1 provides the fastest encryption code when using
47 | Pentium optimiation with small code but this is poor for decryption
48 | so we need to control this with the following VC++ pragmas
49 | */
50 |
51 | #if defined( _MSC_VER ) && !defined( _WIN64 )
52 | #pragma optimize( "s", on )
53 | #endif
54 |
55 | /* Given the column (c) of the output state variable, the following
56 | macros give the input state variables which are needed in its
57 | computation for each row (r) of the state. All the alternative
58 | macros give the same end values but expand into different ways
59 | of calculating these values. In particular the complex macro
60 | used for dynamically variable block sizes is designed to expand
61 | to a compile time constant whenever possible but will expand to
62 | conditional clauses on some branches (I am grateful to Frank
63 | Yellin for this construction)
64 | */
65 |
66 | #define fwd_var(x,r,c)\
67 | ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
68 | : r == 1 ? ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0))\
69 | : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
70 | : ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2)))
71 |
72 | #if defined(FT4_SET)
73 | #undef dec_fmvars
74 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,n),fwd_var,rf1,c))
75 | #elif defined(FT1_SET)
76 | #undef dec_fmvars
77 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(f,n),fwd_var,rf1,c))
78 | #else
79 | #define fwd_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ fwd_mcol(no_table(x,t_use(s,box),fwd_var,rf1,c)))
80 | #endif
81 |
82 | #if defined(FL4_SET)
83 | #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(f,l),fwd_var,rf1,c))
84 | #elif defined(FL1_SET)
85 | #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(f,l),fwd_var,rf1,c))
86 | #else
87 | #define fwd_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(s,box),fwd_var,rf1,c))
88 | #endif
89 |
90 | AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1])
91 | { uint_32t locals(b0, b1);
92 | const uint_32t *kp;
93 | #if defined( dec_fmvars )
94 | dec_fmvars; /* declare variables for fwd_mcol() if needed */
95 | #endif
96 |
97 | if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )
98 | return EXIT_FAILURE;
99 |
100 | kp = cx->ks;
101 | state_in(b0, in, kp);
102 |
103 | #if (ENC_UNROLL == FULL)
104 |
105 | switch(cx->inf.b[0])
106 | {
107 | case 14 * 16:
108 | round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
109 | round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
110 | kp += 2 * N_COLS;
111 | case 12 * 16:
112 | round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
113 | round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
114 | kp += 2 * N_COLS;
115 | case 10 * 16:
116 | round(fwd_rnd, b1, b0, kp + 1 * N_COLS);
117 | round(fwd_rnd, b0, b1, kp + 2 * N_COLS);
118 | round(fwd_rnd, b1, b0, kp + 3 * N_COLS);
119 | round(fwd_rnd, b0, b1, kp + 4 * N_COLS);
120 | round(fwd_rnd, b1, b0, kp + 5 * N_COLS);
121 | round(fwd_rnd, b0, b1, kp + 6 * N_COLS);
122 | round(fwd_rnd, b1, b0, kp + 7 * N_COLS);
123 | round(fwd_rnd, b0, b1, kp + 8 * N_COLS);
124 | round(fwd_rnd, b1, b0, kp + 9 * N_COLS);
125 | round(fwd_lrnd, b0, b1, kp +10 * N_COLS);
126 | }
127 |
128 | #else
129 |
130 | #if (ENC_UNROLL == PARTIAL)
131 | { uint_32t rnd;
132 | for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)
133 | {
134 | kp += N_COLS;
135 | round(fwd_rnd, b1, b0, kp);
136 | kp += N_COLS;
137 | round(fwd_rnd, b0, b1, kp);
138 | }
139 | kp += N_COLS;
140 | round(fwd_rnd, b1, b0, kp);
141 | #else
142 | { uint_32t rnd;
143 | for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)
144 | {
145 | kp += N_COLS;
146 | round(fwd_rnd, b1, b0, kp);
147 | l_copy(b0, b1);
148 | }
149 | #endif
150 | kp += N_COLS;
151 | round(fwd_lrnd, b0, b1, kp);
152 | }
153 | #endif
154 |
155 | state_out(out, b0);
156 | return EXIT_SUCCESS;
157 | }
158 |
159 | #endif
160 |
161 | #if ( FUNCS_IN_C & DECRYPTION_IN_C)
162 |
163 | /* Visual C++ .Net v7.1 provides the fastest encryption code when using
164 | Pentium optimiation with small code but this is poor for decryption
165 | so we need to control this with the following VC++ pragmas
166 | */
167 |
168 | #if defined( _MSC_VER ) && !defined( _WIN64 )
169 | #pragma optimize( "t", on )
170 | #endif
171 |
172 | /* Given the column (c) of the output state variable, the following
173 | macros give the input state variables which are needed in its
174 | computation for each row (r) of the state. All the alternative
175 | macros give the same end values but expand into different ways
176 | of calculating these values. In particular the complex macro
177 | used for dynamically variable block sizes is designed to expand
178 | to a compile time constant whenever possible but will expand to
179 | conditional clauses on some branches (I am grateful to Frank
180 | Yellin for this construction)
181 | */
182 |
183 | #define inv_var(x,r,c)\
184 | ( r == 0 ? ( c == 0 ? s(x,0) : c == 1 ? s(x,1) : c == 2 ? s(x,2) : s(x,3))\
185 | : r == 1 ? ( c == 0 ? s(x,3) : c == 1 ? s(x,0) : c == 2 ? s(x,1) : s(x,2))\
186 | : r == 2 ? ( c == 0 ? s(x,2) : c == 1 ? s(x,3) : c == 2 ? s(x,0) : s(x,1))\
187 | : ( c == 0 ? s(x,1) : c == 1 ? s(x,2) : c == 2 ? s(x,3) : s(x,0)))
188 |
189 | #if defined(IT4_SET)
190 | #undef dec_imvars
191 | #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,n),inv_var,rf1,c))
192 | #elif defined(IT1_SET)
193 | #undef dec_imvars
194 | #define inv_rnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,upr,t_use(i,n),inv_var,rf1,c))
195 | #else
196 | #define inv_rnd(y,x,k,c) (s(y,c) = inv_mcol((k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c)))
197 | #endif
198 |
199 | #if defined(IL4_SET)
200 | #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ four_tables(x,t_use(i,l),inv_var,rf1,c))
201 | #elif defined(IL1_SET)
202 | #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ one_table(x,ups,t_use(i,l),inv_var,rf1,c))
203 | #else
204 | #define inv_lrnd(y,x,k,c) (s(y,c) = (k)[c] ^ no_table(x,t_use(i,box),inv_var,rf1,c))
205 | #endif
206 |
207 | /* This code can work with the decryption key schedule in the */
208 | /* order that is used for encrytpion (where the 1st decryption */
209 | /* round key is at the high end ot the schedule) or with a key */
210 | /* schedule that has been reversed to put the 1st decryption */
211 | /* round key at the low end of the schedule in memory (when */
212 | /* AES_REV_DKS is defined) */
213 |
214 | #ifdef AES_REV_DKS
215 | #define key_ofs 0
216 | #define rnd_key(n) (kp + n * N_COLS)
217 | #else
218 | #define key_ofs 1
219 | #define rnd_key(n) (kp - n * N_COLS)
220 | #endif
221 |
222 | AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1])
223 | { uint_32t locals(b0, b1);
224 | #if defined( dec_imvars )
225 | dec_imvars; /* declare variables for inv_mcol() if needed */
226 | #endif
227 | const uint_32t *kp;
228 |
229 | if( cx->inf.b[0] != 10 * 16 && cx->inf.b[0] != 12 * 16 && cx->inf.b[0] != 14 * 16 )
230 | return EXIT_FAILURE;
231 |
232 | kp = cx->ks + (key_ofs ? (cx->inf.b[0] >> 2) : 0);
233 | state_in(b0, in, kp);
234 |
235 | #if (DEC_UNROLL == FULL)
236 |
237 | kp = cx->ks + (key_ofs ? 0 : (cx->inf.b[0] >> 2));
238 | switch(cx->inf.b[0])
239 | {
240 | case 14 * 16:
241 | round(inv_rnd, b1, b0, rnd_key(-13));
242 | round(inv_rnd, b0, b1, rnd_key(-12));
243 | case 12 * 16:
244 | round(inv_rnd, b1, b0, rnd_key(-11));
245 | round(inv_rnd, b0, b1, rnd_key(-10));
246 | case 10 * 16:
247 | round(inv_rnd, b1, b0, rnd_key(-9));
248 | round(inv_rnd, b0, b1, rnd_key(-8));
249 | round(inv_rnd, b1, b0, rnd_key(-7));
250 | round(inv_rnd, b0, b1, rnd_key(-6));
251 | round(inv_rnd, b1, b0, rnd_key(-5));
252 | round(inv_rnd, b0, b1, rnd_key(-4));
253 | round(inv_rnd, b1, b0, rnd_key(-3));
254 | round(inv_rnd, b0, b1, rnd_key(-2));
255 | round(inv_rnd, b1, b0, rnd_key(-1));
256 | round(inv_lrnd, b0, b1, rnd_key( 0));
257 | }
258 |
259 | #else
260 |
261 | #if (DEC_UNROLL == PARTIAL)
262 | { uint_32t rnd;
263 | for(rnd = 0; rnd < (cx->inf.b[0] >> 5) - 1; ++rnd)
264 | {
265 | kp = rnd_key(1);
266 | round(inv_rnd, b1, b0, kp);
267 | kp = rnd_key(1);
268 | round(inv_rnd, b0, b1, kp);
269 | }
270 | kp = rnd_key(1);
271 | round(inv_rnd, b1, b0, kp);
272 | #else
273 | { uint_32t rnd;
274 | for(rnd = 0; rnd < (cx->inf.b[0] >> 4) - 1; ++rnd)
275 | {
276 | kp = rnd_key(1);
277 | round(inv_rnd, b1, b0, kp);
278 | l_copy(b0, b1);
279 | }
280 | #endif
281 | kp = rnd_key(1);
282 | round(inv_lrnd, b0, b1, kp);
283 | }
284 | #endif
285 |
286 | state_out(out, b0);
287 | return EXIT_SUCCESS;
288 | }
289 |
290 | #endif
291 |
292 | #if defined(__cplusplus)
293 | }
294 | #endif
295 |
--------------------------------------------------------------------------------
/aes/aeskey.m:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 | */
20 |
21 | #include "aesopt.h"
22 | #include "aestab.h"
23 |
24 | #ifdef USE_VIA_ACE_IF_PRESENT
25 | # include "aes_via_ace.h"
26 | #endif
27 |
28 | #if defined(__cplusplus)
29 | extern "C"
30 | {
31 | #endif
32 |
33 | /* Initialise the key schedule from the user supplied key. The key
34 | length can be specified in bytes, with legal values of 16, 24
35 | and 32, or in bits, with legal values of 128, 192 and 256. These
36 | values correspond with Nk values of 4, 6 and 8 respectively.
37 |
38 | The following macros implement a single cycle in the key
39 | schedule generation process. The number of cycles needed
40 | for each cx->n_col and nk value is:
41 |
42 | nk = 4 5 6 7 8
43 | ------------------------------
44 | cx->n_col = 4 10 9 8 7 7
45 | cx->n_col = 5 14 11 10 9 9
46 | cx->n_col = 6 19 15 12 11 11
47 | cx->n_col = 7 21 19 16 13 14
48 | cx->n_col = 8 29 23 19 17 14
49 | */
50 |
51 | #if defined( REDUCE_CODE_SIZE )
52 | # define ls_box ls_sub
53 | uint_32t ls_sub(const uint_32t t, const uint_32t n);
54 | # define inv_mcol im_sub
55 | uint_32t im_sub(const uint_32t x);
56 | # ifdef ENC_KS_UNROLL
57 | # undef ENC_KS_UNROLL
58 | # endif
59 | # ifdef DEC_KS_UNROLL
60 | # undef DEC_KS_UNROLL
61 | # endif
62 | #endif
63 |
64 | #if (FUNCS_IN_C & ENC_KEYING_IN_C)
65 |
66 | #if defined(AES_128) || defined( AES_VAR )
67 |
68 | #define ke4(k,i) \
69 | { k[4*(i)+4] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \
70 | k[4*(i)+5] = ss[1] ^= ss[0]; \
71 | k[4*(i)+6] = ss[2] ^= ss[1]; \
72 | k[4*(i)+7] = ss[3] ^= ss[2]; \
73 | }
74 |
75 | AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1])
76 | { uint_32t ss[4];
77 |
78 | cx->ks[0] = ss[0] = word_in(key, 0);
79 | cx->ks[1] = ss[1] = word_in(key, 1);
80 | cx->ks[2] = ss[2] = word_in(key, 2);
81 | cx->ks[3] = ss[3] = word_in(key, 3);
82 |
83 | #ifdef ENC_KS_UNROLL
84 | ke4(cx->ks, 0); ke4(cx->ks, 1);
85 | ke4(cx->ks, 2); ke4(cx->ks, 3);
86 | ke4(cx->ks, 4); ke4(cx->ks, 5);
87 | ke4(cx->ks, 6); ke4(cx->ks, 7);
88 | ke4(cx->ks, 8);
89 | #else
90 | { uint_32t i;
91 | for(i = 0; i < 9; ++i)
92 | ke4(cx->ks, i);
93 | }
94 | #endif
95 | ke4(cx->ks, 9);
96 | cx->inf.l = 0;
97 | cx->inf.b[0] = 10 * 16;
98 |
99 | #ifdef USE_VIA_ACE_IF_PRESENT
100 | if(VIA_ACE_AVAILABLE)
101 | cx->inf.b[1] = 0xff;
102 | #endif
103 | return EXIT_SUCCESS;
104 | }
105 |
106 | #endif
107 |
108 | #if defined(AES_192) || defined( AES_VAR )
109 |
110 | #define kef6(k,i) \
111 | { k[6*(i)+ 6] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \
112 | k[6*(i)+ 7] = ss[1] ^= ss[0]; \
113 | k[6*(i)+ 8] = ss[2] ^= ss[1]; \
114 | k[6*(i)+ 9] = ss[3] ^= ss[2]; \
115 | }
116 |
117 | #define ke6(k,i) \
118 | { kef6(k,i); \
119 | k[6*(i)+10] = ss[4] ^= ss[3]; \
120 | k[6*(i)+11] = ss[5] ^= ss[4]; \
121 | }
122 |
123 | AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1])
124 | { uint_32t ss[6];
125 |
126 | cx->ks[0] = ss[0] = word_in(key, 0);
127 | cx->ks[1] = ss[1] = word_in(key, 1);
128 | cx->ks[2] = ss[2] = word_in(key, 2);
129 | cx->ks[3] = ss[3] = word_in(key, 3);
130 | cx->ks[4] = ss[4] = word_in(key, 4);
131 | cx->ks[5] = ss[5] = word_in(key, 5);
132 |
133 | #ifdef ENC_KS_UNROLL
134 | ke6(cx->ks, 0); ke6(cx->ks, 1);
135 | ke6(cx->ks, 2); ke6(cx->ks, 3);
136 | ke6(cx->ks, 4); ke6(cx->ks, 5);
137 | ke6(cx->ks, 6);
138 | #else
139 | { uint_32t i;
140 | for(i = 0; i < 7; ++i)
141 | ke6(cx->ks, i);
142 | }
143 | #endif
144 | kef6(cx->ks, 7);
145 | cx->inf.l = 0;
146 | cx->inf.b[0] = 12 * 16;
147 |
148 | #ifdef USE_VIA_ACE_IF_PRESENT
149 | if(VIA_ACE_AVAILABLE)
150 | cx->inf.b[1] = 0xff;
151 | #endif
152 | return EXIT_SUCCESS;
153 | }
154 |
155 | #endif
156 |
157 | #if defined(AES_256) || defined( AES_VAR )
158 |
159 | #define kef8(k,i) \
160 | { k[8*(i)+ 8] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \
161 | k[8*(i)+ 9] = ss[1] ^= ss[0]; \
162 | k[8*(i)+10] = ss[2] ^= ss[1]; \
163 | k[8*(i)+11] = ss[3] ^= ss[2]; \
164 | }
165 |
166 | #define ke8(k,i) \
167 | { kef8(k,i); \
168 | k[8*(i)+12] = ss[4] ^= ls_box(ss[3],0); \
169 | k[8*(i)+13] = ss[5] ^= ss[4]; \
170 | k[8*(i)+14] = ss[6] ^= ss[5]; \
171 | k[8*(i)+15] = ss[7] ^= ss[6]; \
172 | }
173 |
174 | AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1])
175 | { uint_32t ss[8];
176 |
177 | cx->ks[0] = ss[0] = word_in(key, 0);
178 | cx->ks[1] = ss[1] = word_in(key, 1);
179 | cx->ks[2] = ss[2] = word_in(key, 2);
180 | cx->ks[3] = ss[3] = word_in(key, 3);
181 | cx->ks[4] = ss[4] = word_in(key, 4);
182 | cx->ks[5] = ss[5] = word_in(key, 5);
183 | cx->ks[6] = ss[6] = word_in(key, 6);
184 | cx->ks[7] = ss[7] = word_in(key, 7);
185 |
186 | #ifdef ENC_KS_UNROLL
187 | ke8(cx->ks, 0); ke8(cx->ks, 1);
188 | ke8(cx->ks, 2); ke8(cx->ks, 3);
189 | ke8(cx->ks, 4); ke8(cx->ks, 5);
190 | #else
191 | { uint_32t i;
192 | for(i = 0; i < 6; ++i)
193 | ke8(cx->ks, i);
194 | }
195 | #endif
196 | kef8(cx->ks, 6);
197 | cx->inf.l = 0;
198 | cx->inf.b[0] = 14 * 16;
199 |
200 | #ifdef USE_VIA_ACE_IF_PRESENT
201 | if(VIA_ACE_AVAILABLE)
202 | cx->inf.b[1] = 0xff;
203 | #endif
204 | return EXIT_SUCCESS;
205 | }
206 |
207 | #endif
208 |
209 | #if defined( AES_VAR )
210 |
211 | AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1])
212 | {
213 | switch(key_len)
214 | {
215 | case 16: case 128: return aes_encrypt_key128(key, cx);
216 | case 24: case 192: return aes_encrypt_key192(key, cx);
217 | case 32: case 256: return aes_encrypt_key256(key, cx);
218 | default: return EXIT_FAILURE;
219 | }
220 | }
221 |
222 | #endif
223 |
224 | #endif
225 |
226 | #if (FUNCS_IN_C & DEC_KEYING_IN_C)
227 |
228 | /* this is used to store the decryption round keys */
229 | /* in forward or reverse order */
230 |
231 | #ifdef AES_REV_DKS
232 | #define v(n,i) ((n) - (i) + 2 * ((i) & 3))
233 | #else
234 | #define v(n,i) (i)
235 | #endif
236 |
237 | #if DEC_ROUND == NO_TABLES
238 | #define ff(x) (x)
239 | #else
240 | #define ff(x) inv_mcol(x)
241 | #if defined( dec_imvars )
242 | #define d_vars dec_imvars
243 | #endif
244 | #endif
245 |
246 | #if defined(AES_128) || defined( AES_VAR )
247 |
248 | #define k4e(k,i) \
249 | { k[v(40,(4*(i))+4)] = ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; \
250 | k[v(40,(4*(i))+5)] = ss[1] ^= ss[0]; \
251 | k[v(40,(4*(i))+6)] = ss[2] ^= ss[1]; \
252 | k[v(40,(4*(i))+7)] = ss[3] ^= ss[2]; \
253 | }
254 |
255 | #if 1
256 |
257 | #define kdf4(k,i) \
258 | { ss[0] = ss[0] ^ ss[2] ^ ss[1] ^ ss[3]; \
259 | ss[1] = ss[1] ^ ss[3]; \
260 | ss[2] = ss[2] ^ ss[3]; \
261 | ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \
262 | ss[i % 4] ^= ss[4]; \
263 | ss[4] ^= k[v(40,(4*(i)))]; k[v(40,(4*(i))+4)] = ff(ss[4]); \
264 | ss[4] ^= k[v(40,(4*(i))+1)]; k[v(40,(4*(i))+5)] = ff(ss[4]); \
265 | ss[4] ^= k[v(40,(4*(i))+2)]; k[v(40,(4*(i))+6)] = ff(ss[4]); \
266 | ss[4] ^= k[v(40,(4*(i))+3)]; k[v(40,(4*(i))+7)] = ff(ss[4]); \
267 | }
268 |
269 | #define kd4(k,i) \
270 | { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; \
271 | ss[i % 4] ^= ss[4]; ss[4] = ff(ss[4]); \
272 | k[v(40,(4*(i))+4)] = ss[4] ^= k[v(40,(4*(i)))]; \
273 | k[v(40,(4*(i))+5)] = ss[4] ^= k[v(40,(4*(i))+1)]; \
274 | k[v(40,(4*(i))+6)] = ss[4] ^= k[v(40,(4*(i))+2)]; \
275 | k[v(40,(4*(i))+7)] = ss[4] ^= k[v(40,(4*(i))+3)]; \
276 | }
277 |
278 | #define kdl4(k,i) \
279 | { ss[4] = ls_box(ss[(i+3) % 4], 3) ^ t_use(r,c)[i]; ss[i % 4] ^= ss[4]; \
280 | k[v(40,(4*(i))+4)] = (ss[0] ^= ss[1]) ^ ss[2] ^ ss[3]; \
281 | k[v(40,(4*(i))+5)] = ss[1] ^ ss[3]; \
282 | k[v(40,(4*(i))+6)] = ss[0]; \
283 | k[v(40,(4*(i))+7)] = ss[1]; \
284 | }
285 |
286 | #else
287 |
288 | #define kdf4(k,i) \
289 | { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ff(ss[0]); \
290 | ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ff(ss[1]); \
291 | ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ff(ss[2]); \
292 | ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ff(ss[3]); \
293 | }
294 |
295 | #define kd4(k,i) \
296 | { ss[4] = ls_box(ss[3],3) ^ t_use(r,c)[i]; \
297 | ss[0] ^= ss[4]; ss[4] = ff(ss[4]); k[v(40,(4*(i))+ 4)] = ss[4] ^= k[v(40,(4*(i)))]; \
298 | ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[4] ^= k[v(40,(4*(i))+ 1)]; \
299 | ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[4] ^= k[v(40,(4*(i))+ 2)]; \
300 | ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[4] ^= k[v(40,(4*(i))+ 3)]; \
301 | }
302 |
303 | #define kdl4(k,i) \
304 | { ss[0] ^= ls_box(ss[3],3) ^ t_use(r,c)[i]; k[v(40,(4*(i))+ 4)] = ss[0]; \
305 | ss[1] ^= ss[0]; k[v(40,(4*(i))+ 5)] = ss[1]; \
306 | ss[2] ^= ss[1]; k[v(40,(4*(i))+ 6)] = ss[2]; \
307 | ss[3] ^= ss[2]; k[v(40,(4*(i))+ 7)] = ss[3]; \
308 | }
309 |
310 | #endif
311 |
312 | AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1])
313 | { uint_32t ss[5];
314 | #if defined( d_vars )
315 | d_vars;
316 | #endif
317 | cx->ks[v(40,(0))] = ss[0] = word_in(key, 0);
318 | cx->ks[v(40,(1))] = ss[1] = word_in(key, 1);
319 | cx->ks[v(40,(2))] = ss[2] = word_in(key, 2);
320 | cx->ks[v(40,(3))] = ss[3] = word_in(key, 3);
321 |
322 | #ifdef DEC_KS_UNROLL
323 | kdf4(cx->ks, 0); kd4(cx->ks, 1);
324 | kd4(cx->ks, 2); kd4(cx->ks, 3);
325 | kd4(cx->ks, 4); kd4(cx->ks, 5);
326 | kd4(cx->ks, 6); kd4(cx->ks, 7);
327 | kd4(cx->ks, 8); kdl4(cx->ks, 9);
328 | #else
329 | { uint_32t i;
330 | for(i = 0; i < 10; ++i)
331 | k4e(cx->ks, i);
332 | #if !(DEC_ROUND == NO_TABLES)
333 | for(i = N_COLS; i < 10 * N_COLS; ++i)
334 | cx->ks[i] = inv_mcol(cx->ks[i]);
335 | #endif
336 | }
337 | #endif
338 | cx->inf.l = 0;
339 | cx->inf.b[0] = 10 * 16;
340 |
341 | #ifdef USE_VIA_ACE_IF_PRESENT
342 | if(VIA_ACE_AVAILABLE)
343 | cx->inf.b[1] = 0xff;
344 | #endif
345 | return EXIT_SUCCESS;
346 | }
347 |
348 | #endif
349 |
350 | #if defined(AES_192) || defined( AES_VAR )
351 |
352 | #define k6ef(k,i) \
353 | { k[v(48,(6*(i))+ 6)] = ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; \
354 | k[v(48,(6*(i))+ 7)] = ss[1] ^= ss[0]; \
355 | k[v(48,(6*(i))+ 8)] = ss[2] ^= ss[1]; \
356 | k[v(48,(6*(i))+ 9)] = ss[3] ^= ss[2]; \
357 | }
358 |
359 | #define k6e(k,i) \
360 | { k6ef(k,i); \
361 | k[v(48,(6*(i))+10)] = ss[4] ^= ss[3]; \
362 | k[v(48,(6*(i))+11)] = ss[5] ^= ss[4]; \
363 | }
364 |
365 | #define kdf6(k,i) \
366 | { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ff(ss[0]); \
367 | ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ff(ss[1]); \
368 | ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ff(ss[2]); \
369 | ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ff(ss[3]); \
370 | ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ff(ss[4]); \
371 | ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ff(ss[5]); \
372 | }
373 |
374 | #define kd6(k,i) \
375 | { ss[6] = ls_box(ss[5],3) ^ t_use(r,c)[i]; \
376 | ss[0] ^= ss[6]; ss[6] = ff(ss[6]); k[v(48,(6*(i))+ 6)] = ss[6] ^= k[v(48,(6*(i)))]; \
377 | ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[6] ^= k[v(48,(6*(i))+ 1)]; \
378 | ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[6] ^= k[v(48,(6*(i))+ 2)]; \
379 | ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[6] ^= k[v(48,(6*(i))+ 3)]; \
380 | ss[4] ^= ss[3]; k[v(48,(6*(i))+10)] = ss[6] ^= k[v(48,(6*(i))+ 4)]; \
381 | ss[5] ^= ss[4]; k[v(48,(6*(i))+11)] = ss[6] ^= k[v(48,(6*(i))+ 5)]; \
382 | }
383 |
384 | #define kdl6(k,i) \
385 | { ss[0] ^= ls_box(ss[5],3) ^ t_use(r,c)[i]; k[v(48,(6*(i))+ 6)] = ss[0]; \
386 | ss[1] ^= ss[0]; k[v(48,(6*(i))+ 7)] = ss[1]; \
387 | ss[2] ^= ss[1]; k[v(48,(6*(i))+ 8)] = ss[2]; \
388 | ss[3] ^= ss[2]; k[v(48,(6*(i))+ 9)] = ss[3]; \
389 | }
390 |
391 | AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1])
392 | { uint_32t ss[7];
393 | #if defined( d_vars )
394 | d_vars;
395 | #endif
396 | cx->ks[v(48,(0))] = ss[0] = word_in(key, 0);
397 | cx->ks[v(48,(1))] = ss[1] = word_in(key, 1);
398 | cx->ks[v(48,(2))] = ss[2] = word_in(key, 2);
399 | cx->ks[v(48,(3))] = ss[3] = word_in(key, 3);
400 |
401 | #ifdef DEC_KS_UNROLL
402 | cx->ks[v(48,(4))] = ff(ss[4] = word_in(key, 4));
403 | cx->ks[v(48,(5))] = ff(ss[5] = word_in(key, 5));
404 | kdf6(cx->ks, 0); kd6(cx->ks, 1);
405 | kd6(cx->ks, 2); kd6(cx->ks, 3);
406 | kd6(cx->ks, 4); kd6(cx->ks, 5);
407 | kd6(cx->ks, 6); kdl6(cx->ks, 7);
408 | #else
409 | cx->ks[v(48,(4))] = ss[4] = word_in(key, 4);
410 | cx->ks[v(48,(5))] = ss[5] = word_in(key, 5);
411 | { uint_32t i;
412 |
413 | for(i = 0; i < 7; ++i)
414 | k6e(cx->ks, i);
415 | k6ef(cx->ks, 7);
416 | #if !(DEC_ROUND == NO_TABLES)
417 | for(i = N_COLS; i < 12 * N_COLS; ++i)
418 | cx->ks[i] = inv_mcol(cx->ks[i]);
419 | #endif
420 | }
421 | #endif
422 | cx->inf.l = 0;
423 | cx->inf.b[0] = 12 * 16;
424 |
425 | #ifdef USE_VIA_ACE_IF_PRESENT
426 | if(VIA_ACE_AVAILABLE)
427 | cx->inf.b[1] = 0xff;
428 | #endif
429 | return EXIT_SUCCESS;
430 | }
431 |
432 | #endif
433 |
434 | #if defined(AES_256) || defined( AES_VAR )
435 |
436 | #define k8ef(k,i) \
437 | { k[v(56,(8*(i))+ 8)] = ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; \
438 | k[v(56,(8*(i))+ 9)] = ss[1] ^= ss[0]; \
439 | k[v(56,(8*(i))+10)] = ss[2] ^= ss[1]; \
440 | k[v(56,(8*(i))+11)] = ss[3] ^= ss[2]; \
441 | }
442 |
443 | #define k8e(k,i) \
444 | { k8ef(k,i); \
445 | k[v(56,(8*(i))+12)] = ss[4] ^= ls_box(ss[3],0); \
446 | k[v(56,(8*(i))+13)] = ss[5] ^= ss[4]; \
447 | k[v(56,(8*(i))+14)] = ss[6] ^= ss[5]; \
448 | k[v(56,(8*(i))+15)] = ss[7] ^= ss[6]; \
449 | }
450 |
451 | #define kdf8(k,i) \
452 | { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ff(ss[0]); \
453 | ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ff(ss[1]); \
454 | ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ff(ss[2]); \
455 | ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ff(ss[3]); \
456 | ss[4] ^= ls_box(ss[3],0); k[v(56,(8*(i))+12)] = ff(ss[4]); \
457 | ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ff(ss[5]); \
458 | ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ff(ss[6]); \
459 | ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ff(ss[7]); \
460 | }
461 |
462 | #define kd8(k,i) \
463 | { ss[8] = ls_box(ss[7],3) ^ t_use(r,c)[i]; \
464 | ss[0] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+ 8)] = ss[8] ^= k[v(56,(8*(i)))]; \
465 | ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[8] ^= k[v(56,(8*(i))+ 1)]; \
466 | ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[8] ^= k[v(56,(8*(i))+ 2)]; \
467 | ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[8] ^= k[v(56,(8*(i))+ 3)]; \
468 | ss[8] = ls_box(ss[3],0); \
469 | ss[4] ^= ss[8]; ss[8] = ff(ss[8]); k[v(56,(8*(i))+12)] = ss[8] ^= k[v(56,(8*(i))+ 4)]; \
470 | ss[5] ^= ss[4]; k[v(56,(8*(i))+13)] = ss[8] ^= k[v(56,(8*(i))+ 5)]; \
471 | ss[6] ^= ss[5]; k[v(56,(8*(i))+14)] = ss[8] ^= k[v(56,(8*(i))+ 6)]; \
472 | ss[7] ^= ss[6]; k[v(56,(8*(i))+15)] = ss[8] ^= k[v(56,(8*(i))+ 7)]; \
473 | }
474 |
475 | #define kdl8(k,i) \
476 | { ss[0] ^= ls_box(ss[7],3) ^ t_use(r,c)[i]; k[v(56,(8*(i))+ 8)] = ss[0]; \
477 | ss[1] ^= ss[0]; k[v(56,(8*(i))+ 9)] = ss[1]; \
478 | ss[2] ^= ss[1]; k[v(56,(8*(i))+10)] = ss[2]; \
479 | ss[3] ^= ss[2]; k[v(56,(8*(i))+11)] = ss[3]; \
480 | }
481 |
482 | AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1])
483 | { uint_32t ss[9];
484 | #if defined( d_vars )
485 | d_vars;
486 | #endif
487 | cx->ks[v(56,(0))] = ss[0] = word_in(key, 0);
488 | cx->ks[v(56,(1))] = ss[1] = word_in(key, 1);
489 | cx->ks[v(56,(2))] = ss[2] = word_in(key, 2);
490 | cx->ks[v(56,(3))] = ss[3] = word_in(key, 3);
491 |
492 | #ifdef DEC_KS_UNROLL
493 | cx->ks[v(56,(4))] = ff(ss[4] = word_in(key, 4));
494 | cx->ks[v(56,(5))] = ff(ss[5] = word_in(key, 5));
495 | cx->ks[v(56,(6))] = ff(ss[6] = word_in(key, 6));
496 | cx->ks[v(56,(7))] = ff(ss[7] = word_in(key, 7));
497 | kdf8(cx->ks, 0); kd8(cx->ks, 1);
498 | kd8(cx->ks, 2); kd8(cx->ks, 3);
499 | kd8(cx->ks, 4); kd8(cx->ks, 5);
500 | kdl8(cx->ks, 6);
501 | #else
502 | cx->ks[v(56,(4))] = ss[4] = word_in(key, 4);
503 | cx->ks[v(56,(5))] = ss[5] = word_in(key, 5);
504 | cx->ks[v(56,(6))] = ss[6] = word_in(key, 6);
505 | cx->ks[v(56,(7))] = ss[7] = word_in(key, 7);
506 | { uint_32t i;
507 |
508 | for(i = 0; i < 6; ++i)
509 | k8e(cx->ks, i);
510 | k8ef(cx->ks, 6);
511 | #if !(DEC_ROUND == NO_TABLES)
512 | for(i = N_COLS; i < 14 * N_COLS; ++i)
513 | cx->ks[i] = inv_mcol(cx->ks[i]);
514 | #endif
515 | }
516 | #endif
517 | cx->inf.l = 0;
518 | cx->inf.b[0] = 14 * 16;
519 |
520 | #ifdef USE_VIA_ACE_IF_PRESENT
521 | if(VIA_ACE_AVAILABLE)
522 | cx->inf.b[1] = 0xff;
523 | #endif
524 | return EXIT_SUCCESS;
525 | }
526 |
527 | #endif
528 |
529 | #if defined( AES_VAR )
530 |
531 | AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1])
532 | {
533 | switch(key_len)
534 | {
535 | case 16: case 128: return aes_decrypt_key128(key, cx);
536 | case 24: case 192: return aes_decrypt_key192(key, cx);
537 | case 32: case 256: return aes_decrypt_key256(key, cx);
538 | default: return EXIT_FAILURE;
539 | }
540 | }
541 |
542 | #endif
543 |
544 | #endif
545 |
546 | #if defined(__cplusplus)
547 | }
548 | #endif
549 |
--------------------------------------------------------------------------------
/aes/aestab.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 |
20 | This file contains the code for declaring the tables needed to implement
21 | AES. The file aesopt.h is assumed to be included before this header file.
22 | If there are no global variables, the definitions here can be used to put
23 | the AES tables in a structure so that a pointer can then be added to the
24 | AES context to pass them to the AES routines that need them. If this
25 | facility is used, the calling program has to ensure that this pointer is
26 | managed appropriately. In particular, the value of the t_dec(in,it) item
27 | in the table structure must be set to zero in order to ensure that the
28 | tables are initialised. In practice the three code sequences in aeskey.c
29 | that control the calls to aes_init() and the aes_init() routine itself will
30 | have to be changed for a specific implementation. If global variables are
31 | available it will generally be preferable to use them with the precomputed
32 | FIXED_TABLES option that uses static global tables.
33 |
34 | The following defines can be used to control the way the tables
35 | are defined, initialised and used in embedded environments that
36 | require special features for these purposes
37 |
38 | the 't_dec' construction is used to declare fixed table arrays
39 | the 't_set' construction is used to set fixed table values
40 | the 't_use' construction is used to access fixed table values
41 |
42 | 256 byte tables:
43 |
44 | t_xxx(s,box) => forward S box
45 | t_xxx(i,box) => inverse S box
46 |
47 | 256 32-bit word OR 4 x 256 32-bit word tables:
48 |
49 | t_xxx(f,n) => forward normal round
50 | t_xxx(f,l) => forward last round
51 | t_xxx(i,n) => inverse normal round
52 | t_xxx(i,l) => inverse last round
53 | t_xxx(l,s) => key schedule table
54 | t_xxx(i,m) => key schedule table
55 |
56 | Other variables and tables:
57 |
58 | t_xxx(r,c) => the rcon table
59 | */
60 |
61 | #if !defined( _AESTAB_H )
62 | #define _AESTAB_H
63 |
64 | #if defined(__cplusplus)
65 | extern "C" {
66 | #endif
67 |
68 | #define t_dec(m,n) t_##m##n
69 | #define t_set(m,n) t_##m##n
70 | #define t_use(m,n) t_##m##n
71 |
72 | #if defined(FIXED_TABLES)
73 | # if !defined( __GNUC__ ) && (defined( __MSDOS__ ) || defined( __WIN16__ ))
74 | /* make tables far data to avoid using too much DGROUP space (PG) */
75 | # define CONST const far
76 | # else
77 | # define CONST const
78 | # endif
79 | #else
80 | # define CONST
81 | #endif
82 |
83 | #if defined(DO_TABLES)
84 | # define EXTERN
85 | #else
86 | # define EXTERN extern
87 | #endif
88 |
89 | #if defined(_MSC_VER) && defined(TABLE_ALIGN)
90 | #define ALIGN __declspec(align(TABLE_ALIGN))
91 | #else
92 | #define ALIGN
93 | #endif
94 |
95 | #if defined( __WATCOMC__ ) && ( __WATCOMC__ >= 1100 )
96 | # define XP_DIR __cdecl
97 | #else
98 | # define XP_DIR
99 | #endif
100 |
101 | #if defined(DO_TABLES) && defined(FIXED_TABLES)
102 | #define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256] = b(e)
103 | #define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256] = { b(e), b(f), b(g), b(h) }
104 | EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH] = rc_data(w0);
105 | #else
106 | #define d_1(t,n,b,e) EXTERN ALIGN CONST XP_DIR t n[256]
107 | #define d_4(t,n,b,e,f,g,h) EXTERN ALIGN CONST XP_DIR t n[4][256]
108 | EXTERN ALIGN CONST uint_32t t_dec(r,c)[RC_LENGTH];
109 | #endif
110 |
111 | #if defined( SBX_SET )
112 | d_1(uint_8t, t_dec(s,box), sb_data, h0);
113 | #endif
114 | #if defined( ISB_SET )
115 | d_1(uint_8t, t_dec(i,box), isb_data, h0);
116 | #endif
117 |
118 | #if defined( FT1_SET )
119 | d_1(uint_32t, t_dec(f,n), sb_data, u0);
120 | #endif
121 | #if defined( FT4_SET )
122 | d_4(uint_32t, t_dec(f,n), sb_data, u0, u1, u2, u3);
123 | #endif
124 |
125 | #if defined( FL1_SET )
126 | d_1(uint_32t, t_dec(f,l), sb_data, w0);
127 | #endif
128 | #if defined( FL4_SET )
129 | d_4(uint_32t, t_dec(f,l), sb_data, w0, w1, w2, w3);
130 | #endif
131 |
132 | #if defined( IT1_SET )
133 | d_1(uint_32t, t_dec(i,n), isb_data, v0);
134 | #endif
135 | #if defined( IT4_SET )
136 | d_4(uint_32t, t_dec(i,n), isb_data, v0, v1, v2, v3);
137 | #endif
138 |
139 | #if defined( IL1_SET )
140 | d_1(uint_32t, t_dec(i,l), isb_data, w0);
141 | #endif
142 | #if defined( IL4_SET )
143 | d_4(uint_32t, t_dec(i,l), isb_data, w0, w1, w2, w3);
144 | #endif
145 |
146 | #if defined( LS1_SET )
147 | #if defined( FL1_SET )
148 | #undef LS1_SET
149 | #else
150 | d_1(uint_32t, t_dec(l,s), sb_data, w0);
151 | #endif
152 | #endif
153 |
154 | #if defined( LS4_SET )
155 | #if defined( FL4_SET )
156 | #undef LS4_SET
157 | #else
158 | d_4(uint_32t, t_dec(l,s), sb_data, w0, w1, w2, w3);
159 | #endif
160 | #endif
161 |
162 | #if defined( IM1_SET )
163 | d_1(uint_32t, t_dec(i,m), mm_data, v0);
164 | #endif
165 | #if defined( IM4_SET )
166 | d_4(uint_32t, t_dec(i,m), mm_data, v0, v1, v2, v3);
167 | #endif
168 |
169 | #if defined(__cplusplus)
170 | }
171 | #endif
172 |
173 | #endif
174 |
--------------------------------------------------------------------------------
/aes/aestab.m:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 | */
20 |
21 | #define DO_TABLES
22 |
23 | #include "aes.h"
24 | #include "aesopt.h"
25 |
26 | #if defined(FIXED_TABLES)
27 |
28 | #define sb_data(w) {\
29 | w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\
30 | w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\
31 | w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\
32 | w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\
33 | w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\
34 | w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\
35 | w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\
36 | w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\
37 | w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\
38 | w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\
39 | w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\
40 | w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\
41 | w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\
42 | w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\
43 | w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\
44 | w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\
45 | w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\
46 | w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\
47 | w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\
48 | w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\
49 | w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\
50 | w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\
51 | w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\
52 | w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\
53 | w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\
54 | w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\
55 | w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\
56 | w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\
57 | w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\
58 | w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\
59 | w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\
60 | w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) }
61 |
62 | #define isb_data(w) {\
63 | w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\
64 | w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\
65 | w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\
66 | w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\
67 | w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\
68 | w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\
69 | w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\
70 | w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\
71 | w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\
72 | w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\
73 | w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\
74 | w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\
75 | w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\
76 | w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\
77 | w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\
78 | w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\
79 | w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\
80 | w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\
81 | w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\
82 | w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\
83 | w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\
84 | w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\
85 | w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\
86 | w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\
87 | w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\
88 | w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\
89 | w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\
90 | w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\
91 | w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\
92 | w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\
93 | w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\
94 | w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d) }
95 |
96 | #define mm_data(w) {\
97 | w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\
98 | w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\
99 | w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\
100 | w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\
101 | w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\
102 | w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\
103 | w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\
104 | w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\
105 | w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\
106 | w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\
107 | w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\
108 | w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\
109 | w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\
110 | w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\
111 | w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\
112 | w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\
113 | w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\
114 | w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\
115 | w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\
116 | w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\
117 | w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\
118 | w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\
119 | w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\
120 | w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\
121 | w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\
122 | w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\
123 | w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\
124 | w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\
125 | w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\
126 | w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\
127 | w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\
128 | w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) }
129 |
130 | #define rc_data(w) {\
131 | w(0x01), w(0x02), w(0x04), w(0x08), w(0x10),w(0x20), w(0x40), w(0x80),\
132 | w(0x1b), w(0x36) }
133 |
134 | #define h0(x) (x)
135 |
136 | #define w0(p) bytes2word(p, 0, 0, 0)
137 | #define w1(p) bytes2word(0, p, 0, 0)
138 | #define w2(p) bytes2word(0, 0, p, 0)
139 | #define w3(p) bytes2word(0, 0, 0, p)
140 |
141 | #define u0(p) bytes2word(f2(p), p, p, f3(p))
142 | #define u1(p) bytes2word(f3(p), f2(p), p, p)
143 | #define u2(p) bytes2word(p, f3(p), f2(p), p)
144 | #define u3(p) bytes2word(p, p, f3(p), f2(p))
145 |
146 | #define v0(p) bytes2word(fe(p), f9(p), fd(p), fb(p))
147 | #define v1(p) bytes2word(fb(p), fe(p), f9(p), fd(p))
148 | #define v2(p) bytes2word(fd(p), fb(p), fe(p), f9(p))
149 | #define v3(p) bytes2word(f9(p), fd(p), fb(p), fe(p))
150 |
151 | #endif
152 |
153 | #if defined(FIXED_TABLES) || !defined(FF_TABLES)
154 |
155 | #define f2(x) ((x<<1) ^ (((x>>7) & 1) * WPOLY))
156 | #define f4(x) ((x<<2) ^ (((x>>6) & 1) * WPOLY) ^ (((x>>6) & 2) * WPOLY))
157 | #define f8(x) ((x<<3) ^ (((x>>5) & 1) * WPOLY) ^ (((x>>5) & 2) * WPOLY) \
158 | ^ (((x>>5) & 4) * WPOLY))
159 | #define f3(x) (f2(x) ^ x)
160 | #define f9(x) (f8(x) ^ x)
161 | #define fb(x) (f8(x) ^ f2(x) ^ x)
162 | #define fd(x) (f8(x) ^ f4(x) ^ x)
163 | #define fe(x) (f8(x) ^ f4(x) ^ f2(x))
164 |
165 | #else
166 |
167 | #define f2(x) ((x) ? pow[log[x] + 0x19] : 0)
168 | #define f3(x) ((x) ? pow[log[x] + 0x01] : 0)
169 | #define f9(x) ((x) ? pow[log[x] + 0xc7] : 0)
170 | #define fb(x) ((x) ? pow[log[x] + 0x68] : 0)
171 | #define fd(x) ((x) ? pow[log[x] + 0xee] : 0)
172 | #define fe(x) ((x) ? pow[log[x] + 0xdf] : 0)
173 |
174 | #endif
175 |
176 | #include "aestab.h"
177 |
178 | #if defined(__cplusplus)
179 | extern "C"
180 | {
181 | #endif
182 |
183 | #if defined(FIXED_TABLES)
184 |
185 | /* implemented in case of wrong call for fixed tables */
186 |
187 | AES_RETURN aes_init(void)
188 | {
189 | return EXIT_SUCCESS;
190 | }
191 |
192 | #else /* Generate the tables for the dynamic table option */
193 |
194 | #if defined(FF_TABLES)
195 |
196 | #define gf_inv(x) ((x) ? pow[ 255 - log[x]] : 0)
197 |
198 | #else
199 |
200 | /* It will generally be sensible to use tables to compute finite
201 | field multiplies and inverses but where memory is scarse this
202 | code might sometimes be better. But it only has effect during
203 | initialisation so its pretty unimportant in overall terms.
204 | */
205 |
206 | /* return 2 ^ (n - 1) where n is the bit number of the highest bit
207 | set in x with x in the range 1 < x < 0x00000200. This form is
208 | used so that locals within fi can be bytes rather than words
209 | */
210 |
211 | static uint_8t hibit(const uint_32t x)
212 | { uint_8t r = (uint_8t)((x >> 1) | (x >> 2));
213 |
214 | r |= (r >> 2);
215 | r |= (r >> 4);
216 | return (r + 1) >> 1;
217 | }
218 |
219 | /* return the inverse of the finite field element x */
220 |
221 | static uint_8t gf_inv(const uint_8t x)
222 | { uint_8t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
223 |
224 | if(x < 2)
225 | return x;
226 |
227 | for( ; ; )
228 | {
229 | if(n1)
230 | while(n2 >= n1) /* divide polynomial p2 by p1 */
231 | {
232 | n2 /= n1; /* shift smaller polynomial left */
233 | p2 ^= (p1 * n2) & 0xff; /* and remove from larger one */
234 | v2 ^= v1 * n2; /* shift accumulated value and */
235 | n2 = hibit(p2); /* add into result */
236 | }
237 | else
238 | return v1;
239 |
240 | if(n2) /* repeat with values swapped */
241 | while(n1 >= n2)
242 | {
243 | n1 /= n2;
244 | p1 ^= p2 * n1;
245 | v1 ^= v2 * n1;
246 | n1 = hibit(p1);
247 | }
248 | else
249 | return v2;
250 | }
251 | }
252 |
253 | #endif
254 |
255 | /* The forward and inverse affine transformations used in the S-box */
256 | uint_8t fwd_affine(const uint_8t x)
257 | { uint_32t w = x;
258 | w ^= (w << 1) ^ (w << 2) ^ (w << 3) ^ (w << 4);
259 | return 0x63 ^ ((w ^ (w >> 8)) & 0xff);
260 | }
261 |
262 | uint_8t inv_affine(const uint_8t x)
263 | { uint_32t w = x;
264 | w = (w << 1) ^ (w << 3) ^ (w << 6);
265 | return 0x05 ^ ((w ^ (w >> 8)) & 0xff);
266 | }
267 |
268 | static int init = 0;
269 |
270 | AES_RETURN aes_init(void)
271 | { uint_32t i, w;
272 |
273 | #if defined(FF_TABLES)
274 |
275 | uint_8t pow[512], log[256];
276 |
277 | if(init)
278 | return EXIT_SUCCESS;
279 | /* log and power tables for GF(2^8) finite field with
280 | WPOLY as modular polynomial - the simplest primitive
281 | root is 0x03, used here to generate the tables
282 | */
283 |
284 | i = 0; w = 1;
285 | do
286 | {
287 | pow[i] = (uint_8t)w;
288 | pow[i + 255] = (uint_8t)w;
289 | log[w] = (uint_8t)i++;
290 | w ^= (w << 1) ^ (w & 0x80 ? WPOLY : 0);
291 | }
292 | while (w != 1);
293 |
294 | #else
295 | if(init)
296 | return EXIT_SUCCESS;
297 | #endif
298 |
299 | for(i = 0, w = 1; i < RC_LENGTH; ++i)
300 | {
301 | t_set(r,c)[i] = bytes2word(w, 0, 0, 0);
302 | w = f2(w);
303 | }
304 |
305 | for(i = 0; i < 256; ++i)
306 | { uint_8t b;
307 |
308 | b = fwd_affine(gf_inv((uint_8t)i));
309 | w = bytes2word(f2(b), b, b, f3(b));
310 |
311 | #if defined( SBX_SET )
312 | t_set(s,box)[i] = b;
313 | #endif
314 |
315 | #if defined( FT1_SET ) /* tables for a normal encryption round */
316 | t_set(f,n)[i] = w;
317 | #endif
318 | #if defined( FT4_SET )
319 | t_set(f,n)[0][i] = w;
320 | t_set(f,n)[1][i] = upr(w,1);
321 | t_set(f,n)[2][i] = upr(w,2);
322 | t_set(f,n)[3][i] = upr(w,3);
323 | #endif
324 | w = bytes2word(b, 0, 0, 0);
325 |
326 | #if defined( FL1_SET ) /* tables for last encryption round (may also */
327 | t_set(f,l)[i] = w; /* be used in the key schedule) */
328 | #endif
329 | #if defined( FL4_SET )
330 | t_set(f,l)[0][i] = w;
331 | t_set(f,l)[1][i] = upr(w,1);
332 | t_set(f,l)[2][i] = upr(w,2);
333 | t_set(f,l)[3][i] = upr(w,3);
334 | #endif
335 |
336 | #if defined( LS1_SET ) /* table for key schedule if t_set(f,l) above is*/
337 | t_set(l,s)[i] = w; /* not of the required form */
338 | #endif
339 | #if defined( LS4_SET )
340 | t_set(l,s)[0][i] = w;
341 | t_set(l,s)[1][i] = upr(w,1);
342 | t_set(l,s)[2][i] = upr(w,2);
343 | t_set(l,s)[3][i] = upr(w,3);
344 | #endif
345 |
346 | b = gf_inv(inv_affine((uint_8t)i));
347 | w = bytes2word(fe(b), f9(b), fd(b), fb(b));
348 |
349 | #if defined( IM1_SET ) /* tables for the inverse mix column operation */
350 | t_set(i,m)[b] = w;
351 | #endif
352 | #if defined( IM4_SET )
353 | t_set(i,m)[0][b] = w;
354 | t_set(i,m)[1][b] = upr(w,1);
355 | t_set(i,m)[2][b] = upr(w,2);
356 | t_set(i,m)[3][b] = upr(w,3);
357 | #endif
358 |
359 | #if defined( ISB_SET )
360 | t_set(i,box)[i] = b;
361 | #endif
362 | #if defined( IT1_SET ) /* tables for a normal decryption round */
363 | t_set(i,n)[i] = w;
364 | #endif
365 | #if defined( IT4_SET )
366 | t_set(i,n)[0][i] = w;
367 | t_set(i,n)[1][i] = upr(w,1);
368 | t_set(i,n)[2][i] = upr(w,2);
369 | t_set(i,n)[3][i] = upr(w,3);
370 | #endif
371 | w = bytes2word(b, 0, 0, 0);
372 | #if defined( IL1_SET ) /* tables for last decryption round */
373 | t_set(i,l)[i] = w;
374 | #endif
375 | #if defined( IL4_SET )
376 | t_set(i,l)[0][i] = w;
377 | t_set(i,l)[1][i] = upr(w,1);
378 | t_set(i,l)[2][i] = upr(w,2);
379 | t_set(i,l)[3][i] = upr(w,3);
380 | #endif
381 | }
382 | init = 1;
383 | return EXIT_SUCCESS;
384 | }
385 |
386 | #endif
387 |
388 | #if defined(__cplusplus)
389 | }
390 | #endif
391 |
392 |
--------------------------------------------------------------------------------
/aes/brg_endian.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 | */
20 |
21 | #ifndef _BRG_ENDIAN_H
22 | #define _BRG_ENDIAN_H
23 |
24 | #define IS_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
25 | #define IS_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
26 |
27 | /* Include files where endian defines and byteswap functions may reside */
28 | #if defined( __sun )
29 | # include
30 | #elif defined( __FreeBSD__ ) || defined( __OpenBSD__ ) || defined( __NetBSD__ )
31 | # include
32 | #elif defined( BSD ) && ( BSD >= 199103 ) || defined( __APPLE__ ) || \
33 | defined( __CYGWIN32__ ) || defined( __DJGPP__ ) || defined( __osf__ )
34 | # include
35 | #elif defined( __linux__ ) || defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
36 | # if !defined( __MINGW32__ ) && !defined( _AIX )
37 | # include
38 | # if !defined( __BEOS__ )
39 | # include
40 | # endif
41 | # endif
42 | #endif
43 |
44 | /* Now attempt to set the define for platform byte order using any */
45 | /* of the four forms SYMBOL, _SYMBOL, __SYMBOL & __SYMBOL__, which */
46 | /* seem to encompass most endian symbol definitions */
47 |
48 | #if defined( BIG_ENDIAN ) && defined( LITTLE_ENDIAN )
49 | # if defined( BYTE_ORDER ) && BYTE_ORDER == BIG_ENDIAN
50 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
51 | # elif defined( BYTE_ORDER ) && BYTE_ORDER == LITTLE_ENDIAN
52 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
53 | # endif
54 | #elif defined( BIG_ENDIAN )
55 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
56 | #elif defined( LITTLE_ENDIAN )
57 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
58 | #endif
59 |
60 | #if defined( _BIG_ENDIAN ) && defined( _LITTLE_ENDIAN )
61 | # if defined( _BYTE_ORDER ) && _BYTE_ORDER == _BIG_ENDIAN
62 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
63 | # elif defined( _BYTE_ORDER ) && _BYTE_ORDER == _LITTLE_ENDIAN
64 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
65 | # endif
66 | #elif defined( _BIG_ENDIAN )
67 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
68 | #elif defined( _LITTLE_ENDIAN )
69 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
70 | #endif
71 |
72 | #if defined( __BIG_ENDIAN ) && defined( __LITTLE_ENDIAN )
73 | # if defined( __BYTE_ORDER ) && __BYTE_ORDER == __BIG_ENDIAN
74 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
75 | # elif defined( __BYTE_ORDER ) && __BYTE_ORDER == __LITTLE_ENDIAN
76 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
77 | # endif
78 | #elif defined( __BIG_ENDIAN )
79 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
80 | #elif defined( __LITTLE_ENDIAN )
81 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
82 | #endif
83 |
84 | #if defined( __BIG_ENDIAN__ ) && defined( __LITTLE_ENDIAN__ )
85 | # if defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __BIG_ENDIAN__
86 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
87 | # elif defined( __BYTE_ORDER__ ) && __BYTE_ORDER__ == __LITTLE_ENDIAN__
88 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
89 | # endif
90 | #elif defined( __BIG_ENDIAN__ )
91 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
92 | #elif defined( __LITTLE_ENDIAN__ )
93 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
94 | #endif
95 |
96 | /* if the platform byte order could not be determined, then try to */
97 | /* set this define using common machine defines */
98 | #if !defined(PLATFORM_BYTE_ORDER)
99 |
100 | #if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \
101 | defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \
102 | defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \
103 | defined( vax ) || defined( vms ) || defined( VMS ) || \
104 | defined( __VMS ) || defined( _M_X64 )
105 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
106 |
107 | #elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
108 | defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
109 | defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
110 | defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
111 | defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
112 | defined( __VOS__ ) || defined( __TIGCC__ ) || defined( __TANDEM ) || \
113 | defined( THINK_C ) || defined( __VMCMS__ ) || defined( _AIX )
114 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
115 |
116 | #elif 0 /* **** EDIT HERE IF NECESSARY **** */
117 | # define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
118 | #elif 0 /* **** EDIT HERE IF NECESSARY **** */
119 | # define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
120 | #else
121 | # error Please edit lines 126 or 128 in brg_endian.h to set the platform byte order
122 | #endif
123 |
124 | #endif
125 |
126 | #endif
127 |
--------------------------------------------------------------------------------
/aes/brg_types.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | The redistribution and use of this software (with or without changes)
6 | is allowed without the payment of fees or royalties provided that:
7 |
8 | source code distributions include the above copyright notice, this
9 | list of conditions and the following disclaimer;
10 |
11 | binary distributions include the above copyright notice, this list
12 | of conditions and the following disclaimer in their documentation.
13 |
14 | This software is provided 'as is' with no explicit or implied warranties
15 | in respect of its operation, including, but not limited to, correctness
16 | and fitness for purpose.
17 | ---------------------------------------------------------------------------
18 | Issue Date: 20/12/2007
19 |
20 | The unsigned integer types defined here are of the form uint_t where
21 | is the length of the type; for example, the unsigned 32-bit type is
22 | 'uint_32t'. These are NOT the same as the 'C99 integer types' that are
23 | defined in the inttypes.h and stdint.h headers since attempts to use these
24 | types have shown that support for them is still highly variable. However,
25 | since the latter are of the form uint_t, a regular expression search
26 | and replace (in VC++ search on 'uint_{:z}t' and replace with 'uint\1_t')
27 | can be used to convert the types used here to the C99 standard types.
28 | */
29 |
30 | #ifndef _BRG_TYPES_H
31 | #define _BRG_TYPES_H
32 |
33 | #if defined(__cplusplus)
34 | extern "C" {
35 | #endif
36 |
37 | #include
38 |
39 | #if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
40 | # include
41 | # define ptrint_t intptr_t
42 | #elif defined( __ECOS__ )
43 | # define intptr_t unsigned int
44 | # define ptrint_t intptr_t
45 | #elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
46 | # include
47 | # define ptrint_t intptr_t
48 | #else
49 | # define ptrint_t int
50 | #endif
51 |
52 | #ifndef BRG_UI8
53 | # define BRG_UI8
54 | # if UCHAR_MAX == 255u
55 | typedef unsigned char uint_8t;
56 | # else
57 | # error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h
58 | # endif
59 | #endif
60 |
61 | #ifndef BRG_UI16
62 | # define BRG_UI16
63 | # if USHRT_MAX == 65535u
64 | typedef unsigned short uint_16t;
65 | # else
66 | # error Please define uint_16t as a 16-bit unsigned short type in brg_types.h
67 | # endif
68 | #endif
69 |
70 | #ifndef BRG_UI32
71 | # define BRG_UI32
72 | # if UINT_MAX == 4294967295u
73 | # define li_32(h) 0x##h##u
74 | typedef unsigned int uint_32t;
75 | # elif ULONG_MAX == 4294967295u
76 | # define li_32(h) 0x##h##ul
77 | typedef unsigned long uint_32t;
78 | # elif defined( _CRAY )
79 | # error This code needs 32-bit data types, which Cray machines do not provide
80 | # else
81 | # error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h
82 | # endif
83 | #endif
84 |
85 | #ifndef BRG_UI64
86 | # if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
87 | # define BRG_UI64
88 | # define li_64(h) 0x##h##ui64
89 | typedef unsigned __int64 uint_64t;
90 | # elif defined( _MSC_VER ) && ( _MSC_VER < 1300 ) /* 1300 == VC++ 7.0 */
91 | # define BRG_UI64
92 | # define li_64(h) 0x##h##ui64
93 | typedef unsigned __int64 uint_64t;
94 | # elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
95 | # define BRG_UI64
96 | # define li_64(h) 0x##h##ull
97 | typedef unsigned long long uint_64t;
98 | # elif defined( __MVS__ )
99 | # define BRG_UI64
100 | # define li_64(h) 0x##h##ull
101 | typedef unsigned int long long uint_64t;
102 | # elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
103 | # if UINT_MAX == 18446744073709551615u
104 | # define BRG_UI64
105 | # define li_64(h) 0x##h##u
106 | typedef unsigned int uint_64t;
107 | # endif
108 | # elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
109 | # if ULONG_MAX == 18446744073709551615ul
110 | # define BRG_UI64
111 | # define li_64(h) 0x##h##ul
112 | typedef unsigned long uint_64t;
113 | # endif
114 | # elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
115 | # if ULLONG_MAX == 18446744073709551615ull
116 | # define BRG_UI64
117 | # define li_64(h) 0x##h##ull
118 | typedef unsigned long long uint_64t;
119 | # endif
120 | # elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
121 | # if ULONG_LONG_MAX == 18446744073709551615ull
122 | # define BRG_UI64
123 | # define li_64(h) 0x##h##ull
124 | typedef unsigned long long uint_64t;
125 | # endif
126 | # endif
127 | #endif
128 |
129 | #if !defined( BRG_UI64 )
130 | # if defined( NEED_UINT_64T )
131 | # error Please define uint_64t as an unsigned 64 bit type in brg_types.h
132 | # endif
133 | #endif
134 |
135 | #ifndef RETURN_VALUES
136 | # define RETURN_VALUES
137 | # if defined( DLL_EXPORT )
138 | # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
139 | # define VOID_RETURN __declspec( dllexport ) void __stdcall
140 | # define INT_RETURN __declspec( dllexport ) int __stdcall
141 | # elif defined( __GNUC__ )
142 | # define VOID_RETURN __declspec( __dllexport__ ) void
143 | # define INT_RETURN __declspec( __dllexport__ ) int
144 | # else
145 | # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
146 | # endif
147 | # elif defined( DLL_IMPORT )
148 | # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
149 | # define VOID_RETURN __declspec( dllimport ) void __stdcall
150 | # define INT_RETURN __declspec( dllimport ) int __stdcall
151 | # elif defined( __GNUC__ )
152 | # define VOID_RETURN __declspec( __dllimport__ ) void
153 | # define INT_RETURN __declspec( __dllimport__ ) int
154 | # else
155 | # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
156 | # endif
157 | # elif defined( __WATCOMC__ )
158 | # define VOID_RETURN void __cdecl
159 | # define INT_RETURN int __cdecl
160 | # else
161 | # define VOID_RETURN void
162 | # define INT_RETURN int
163 | # endif
164 | #endif
165 |
166 | /* These defines are used to detect and set the memory alignment of pointers.
167 | Note that offsets are in bytes.
168 |
169 | ALIGN_OFFSET(x,n) return the positive or zero offset of
170 | the memory addressed by the pointer 'x'
171 | from an address that is aligned on an
172 | 'n' byte boundary ('n' is a power of 2)
173 |
174 | ALIGN_FLOOR(x,n) return a pointer that points to memory
175 | that is aligned on an 'n' byte boundary
176 | and is not higher than the memory address
177 | pointed to by 'x' ('n' is a power of 2)
178 |
179 | ALIGN_CEIL(x,n) return a pointer that points to memory
180 | that is aligned on an 'n' byte boundary
181 | and is not lower than the memory address
182 | pointed to by 'x' ('n' is a power of 2)
183 | */
184 |
185 | #define ALIGN_OFFSET(x,n) (((ptrint_t)(x)) & ((n) - 1))
186 | #define ALIGN_FLOOR(x,n) ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
187 | #define ALIGN_CEIL(x,n) ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))
188 |
189 | /* These defines are used to declare buffers in a way that allows
190 | faster operations on longer variables to be used. In all these
191 | defines 'size' must be a power of 2 and >= 8. NOTE that the
192 | buffer size is in bytes but the type length is in bits
193 |
194 | UNIT_TYPEDEF(x,size) declares a variable 'x' of length
195 | 'size' bits
196 |
197 | BUFR_TYPEDEF(x,size,bsize) declares a buffer 'x' of length 'bsize'
198 | bytes defined as an array of variables
199 | each of 'size' bits (bsize must be a
200 | multiple of size / 8)
201 |
202 | UNIT_CAST(x,size) casts a variable to a type of
203 | length 'size' bits
204 |
205 | UPTR_CAST(x,size) casts a pointer to a pointer to a
206 | varaiable of length 'size' bits
207 | */
208 |
209 | #define UI_TYPE(size) uint_##size##t
210 | #define UNIT_TYPEDEF(x,size) typedef UI_TYPE(size) x
211 | #define BUFR_TYPEDEF(x,size,bsize) typedef UI_TYPE(size) x[bsize / (size >> 3)]
212 | #define UNIT_CAST(x,size) ((UI_TYPE(size) )(x))
213 | #define UPTR_CAST(x,size) ((UI_TYPE(size)*)(x))
214 |
215 | #if defined(__cplusplus)
216 | }
217 | #endif
218 |
219 | #endif
220 |
--------------------------------------------------------------------------------
/aes/entropy.h:
--------------------------------------------------------------------------------
1 |
2 | #ifndef _ENTROPY_FUN_H
3 | #define _ENTROPY_FUN_H
4 |
5 | #if defined(__cplusplus)
6 | extern "C"
7 | {
8 | #endif
9 |
10 | int entropy_fun(unsigned char buf[], unsigned int len);
11 |
12 | #if defined(__cplusplus)
13 | }
14 | #endif
15 |
16 | #endif
17 |
--------------------------------------------------------------------------------
/aes/entropy.m:
--------------------------------------------------------------------------------
1 | #ifdef _WIN32
2 | #include
3 | #else
4 | #include
5 | #include
6 | #include
7 | #endif
8 |
9 | #if defined(__cplusplus)
10 | extern "C"
11 | {
12 | #endif
13 |
14 | #ifdef _WIN32
15 | int entropy_fun(unsigned char buf[], unsigned int len)
16 | {
17 | HCRYPTPROV provider;
18 | unsigned __int64 pentium_tsc[1];
19 | unsigned int i;
20 | int result = 0;
21 |
22 |
23 | if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
24 | {
25 | result = CryptGenRandom(provider, len, buf);
26 | CryptReleaseContext(provider, 0);
27 | if (result)
28 | return len;
29 | }
30 |
31 | QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);
32 |
33 | for(i = 0; i < 8 && i < len; ++i)
34 | buf[i] = ((unsigned char*)pentium_tsc)[i];
35 |
36 | return i;
37 | }
38 | #else
39 | int entropy_fun(unsigned char buf[], unsigned int len)
40 | {
41 | int frand = open("/dev/random", O_RDONLY);
42 | int rlen = 0;
43 | if (frand != -1)
44 | {
45 | rlen = (int)read(frand, buf, len);
46 | close(frand);
47 | }
48 | return rlen;
49 | }
50 | #endif
51 |
52 | #if defined(__cplusplus)
53 | }
54 | #endif
55 |
--------------------------------------------------------------------------------
/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
122 |
--------------------------------------------------------------------------------
/aes/fileenc.m:
--------------------------------------------------------------------------------
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
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 long i = 0, pos = cx->encr_pos;
55 |
56 | while (i < d_len) {
57 | if (pos == AES_BLOCK_SIZE) {
58 | unsigned int j = 0;
59 | /* increment encryption nonce */
60 | while (j < 8 && !++cx->nonce[j])
61 | ++j;
62 | /* encrypt the nonce to form next xor buffer */
63 | aes_encrypt(cx->nonce, cx->encr_bfr, cx->encr_ctx);
64 | pos = 0;
65 | }
66 |
67 | data[i++] ^= cx->encr_bfr[pos++];
68 | }
69 |
70 | cx->encr_pos = (unsigned int)pos;
71 | }
72 |
73 | int fcrypt_init(
74 | int mode, /* the mode to be used (input) */
75 | const unsigned char pwd[], /* the user specified password (input) */
76 | unsigned int pwd_len, /* the length of the password (input) */
77 | const unsigned char salt[], /* the salt (input) */
78 | #ifdef PASSWORD_VERIFIER
79 | unsigned char pwd_ver[PWD_VER_LENGTH], /* 2 byte password verifier (output) */
80 | #endif
81 | fcrypt_ctx cx[1]) /* the file encryption context (output) */
82 | {
83 | unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];
84 |
85 | if (pwd_len > MAX_PWD_LENGTH)
86 | return PASSWORD_TOO_LONG;
87 |
88 | if (mode < 1 || mode > 3)
89 | return BAD_MODE;
90 |
91 | cx->mode = mode;
92 | cx->pwd_len = pwd_len;
93 |
94 | /* derive the encryption and authentication keys and the password verifier */
95 | derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,
96 | kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);
97 |
98 | /* initialise the encryption nonce and buffer pos */
99 | cx->encr_pos = AES_BLOCK_SIZE;
100 | /* if we need a random component in the encryption */
101 | /* nonce, this is where it would have to be set */
102 | memset(cx->nonce, 0, AES_BLOCK_SIZE * sizeof(unsigned char));
103 |
104 | /* initialise for encryption using key 1 */
105 | aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
106 |
107 | /* initialise for authentication using key 2 */
108 | hmac_sha_begin(cx->auth_ctx);
109 | hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx);
110 |
111 | #ifdef PASSWORD_VERIFIER
112 | memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
113 | #endif
114 |
115 | return GOOD_RETURN;
116 | }
117 |
118 | /* perform 'in place' encryption and authentication */
119 |
120 | void fcrypt_encrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
121 | {
122 | encr_data(data, data_len, cx);
123 | hmac_sha_data(data, data_len, cx->auth_ctx);
124 | }
125 |
126 | /* perform 'in place' authentication and decryption */
127 |
128 | void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
129 | {
130 | hmac_sha_data(data, data_len, cx->auth_ctx);
131 | encr_data(data, data_len, cx);
132 | }
133 |
134 | /* close encryption/decryption and return the MAC value */
135 |
136 | int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
137 | {
138 | hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);
139 | return MAC_LENGTH(cx->mode); /* return MAC length in bytes */
140 | }
141 |
142 | #if defined(__cplusplus)
143 | }
144 | #endif
145 |
--------------------------------------------------------------------------------
/aes/hmac.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | LICENSE TERMS
6 |
7 | The free distribution and use of this software in both source and binary
8 | form is allowed (with or without changes) provided that:
9 |
10 | 1. distributions of this source code include the above copyright
11 | notice, this list of conditions and the following disclaimer;
12 |
13 | 2. distributions in binary form include the above copyright
14 | notice, this list of conditions and the following disclaimer
15 | in the documentation and/or other associated materials;
16 |
17 | 3. the copyright holder's name is not used to endorse products
18 | built using this software without specific written permission.
19 |
20 | ALTERNATIVELY, provided that this notice is retained in full, this product
21 | may be distributed under the terms of the GNU General Public License (GPL),
22 | in which case the provisions of the GPL apply INSTEAD OF those given above.
23 |
24 | DISCLAIMER
25 |
26 | This software is provided 'as is' with no explicit or implied warranties
27 | in respect of its properties, including, but not limited to, correctness
28 | and/or fitness for purpose.
29 | ---------------------------------------------------------------------------
30 | Issue Date: 26/08/2003
31 |
32 | This is an implementation of HMAC, the FIPS standard keyed hash function
33 | */
34 |
35 | #ifndef _HMAC_H
36 | #define _HMAC_H
37 |
38 | #include
39 |
40 | #if defined(__cplusplus)
41 | extern "C"
42 | {
43 | #endif
44 |
45 | #define USE_SHA1
46 |
47 | #if !defined(USE_SHA1) && !defined(USE_SHA256)
48 | #error define USE_SHA1 or USE_SHA256 to set the HMAC hash algorithm
49 | #endif
50 |
51 | #ifdef USE_SHA1
52 |
53 | #include "sha1.h"
54 |
55 | #define HASH_INPUT_SIZE SHA1_BLOCK_SIZE
56 | #define HASH_OUTPUT_SIZE SHA1_DIGEST_SIZE
57 | #define sha_ctx sha1_ctx
58 | #define sha_begin sha1_begin
59 | #define sha_hash sha1_hash
60 | #define sha_end sha1_end
61 |
62 | #endif
63 |
64 | #ifdef USE_SHA256
65 |
66 | #include "sha2.h"
67 |
68 | #define HASH_INPUT_SIZE SHA256_BLOCK_SIZE
69 | #define HASH_OUTPUT_SIZE SHA256_DIGEST_SIZE
70 | #define sha_ctx sha256_ctx
71 | #define sha_begin sha256_begin
72 | #define sha_hash sha256_hash
73 | #define sha_end sha256_end
74 |
75 | #endif
76 |
77 | #define HMAC_OK 0
78 | #define HMAC_BAD_MODE -1
79 | #define HMAC_IN_DATA 0xffffffff
80 |
81 | typedef struct
82 | { unsigned char key[HASH_INPUT_SIZE];
83 | sha_ctx ctx[1];
84 | unsigned long klen;
85 | } hmac_ctx;
86 |
87 | void hmac_sha_begin(hmac_ctx cx[1]);
88 |
89 | int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]);
90 |
91 | void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]);
92 |
93 | void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]);
94 |
95 | void hmac_sha(const unsigned char key[], unsigned long key_len,
96 | const unsigned char data[], unsigned long data_len,
97 | unsigned char mac[], unsigned long mac_len);
98 |
99 | #if defined(__cplusplus)
100 | }
101 | #endif
102 |
103 | #endif
104 |
--------------------------------------------------------------------------------
/aes/hmac.m:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | LICENSE TERMS
6 |
7 | The free distribution and use of this software in both source and binary
8 | form is allowed (with or without changes) provided that:
9 |
10 | 1. distributions of this source code include the above copyright
11 | notice, this list of conditions and the following disclaimer;
12 |
13 | 2. distributions in binary form include the above copyright
14 | notice, this list of conditions and the following disclaimer
15 | in the documentation and/or other associated materials;
16 |
17 | 3. the copyright holder's name is not used to endorse products
18 | built using this software without specific written permission.
19 |
20 | ALTERNATIVELY, provided that this notice is retained in full, this product
21 | may be distributed under the terms of the GNU General Public License (GPL),
22 | in which case the provisions of the GPL apply INSTEAD OF those given above.
23 |
24 | DISCLAIMER
25 |
26 | This software is provided 'as is' with no explicit or implied warranties
27 | in respect of its properties, including, but not limited to, correctness
28 | and/or fitness for purpose.
29 | ---------------------------------------------------------------------------
30 | Issue Date: 26/08/2003
31 |
32 | This is an implementation of HMAC, the FIPS standard keyed hash function
33 | */
34 |
35 | #include "hmac.h"
36 | #include "brg_types.h"
37 |
38 | #if defined(__cplusplus)
39 | extern "C"
40 | {
41 | #endif
42 |
43 | /* initialise the HMAC context to zero */
44 | void hmac_sha_begin(hmac_ctx cx[1])
45 | {
46 | memset(cx, 0, sizeof(hmac_ctx));
47 | }
48 |
49 | /* input the HMAC key (can be called multiple times) */
50 | int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1])
51 | {
52 | if(cx->klen == HMAC_IN_DATA) /* error if further key input */
53 | return HMAC_BAD_MODE; /* is attempted in data mode */
54 |
55 | if(cx->klen + key_len > HASH_INPUT_SIZE) /* if the key has to be hashed */
56 | {
57 | if(cx->klen <= HASH_INPUT_SIZE) /* if the hash has not yet been */
58 | { /* started, initialise it and */
59 | sha_begin(cx->ctx); /* hash stored key characters */
60 | sha_hash(cx->key, cx->klen, cx->ctx);
61 | }
62 |
63 | sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */
64 | }
65 | else /* otherwise store key data */
66 | memcpy(cx->key + cx->klen, key, key_len);
67 |
68 | cx->klen += key_len; /* update the key length count */
69 | return HMAC_OK;
70 | }
71 |
72 | /* input the HMAC data (can be called multiple times) - */
73 | /* note that this call terminates the key input phase */
74 | void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1])
75 | { unsigned int i;
76 |
77 | if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */
78 | {
79 | if(cx->klen > HASH_INPUT_SIZE) /* if key is being hashed */
80 | { /* complete the hash and */
81 | sha_end(cx->key, cx->ctx); /* store the result as the */
82 | cx->klen = HASH_OUTPUT_SIZE; /* key and set new length */
83 | }
84 |
85 | /* pad the key if necessary */
86 | memset(cx->key + cx->klen, 0, HASH_INPUT_SIZE - cx->klen);
87 |
88 | /* xor ipad into key value */
89 | for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i)
90 | ((uint_32t*)cx->key)[i] ^= 0x36363636;
91 |
92 | /* and start hash operation */
93 | sha_begin(cx->ctx);
94 | sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx);
95 |
96 | /* mark as now in data mode */
97 | cx->klen = HMAC_IN_DATA;
98 | }
99 |
100 | /* hash the data (if any) */
101 | if(data_len)
102 | sha_hash(data, data_len, cx->ctx);
103 | }
104 |
105 | /* compute and output the MAC value */
106 | void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1])
107 | { unsigned char dig[HASH_OUTPUT_SIZE];
108 | unsigned int i;
109 |
110 | /* if no data has been entered perform a null data phase */
111 | if(cx->klen != HMAC_IN_DATA)
112 | hmac_sha_data((const unsigned char*)0, 0, cx);
113 |
114 | sha_end(dig, cx->ctx); /* complete the inner hash */
115 |
116 | /* set outer key value using opad and removing ipad */
117 | for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i)
118 | ((uint_32t*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c;
119 |
120 | /* perform the outer hash operation */
121 | sha_begin(cx->ctx);
122 | sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx);
123 | sha_hash(dig, HASH_OUTPUT_SIZE, cx->ctx);
124 | sha_end(dig, cx->ctx);
125 |
126 | /* output the hash value */
127 | for(i = 0; i < mac_len; ++i)
128 | mac[i] = dig[i];
129 | }
130 |
131 | /* 'do it all in one go' subroutine */
132 | void hmac_sha(const unsigned char key[], unsigned long key_len,
133 | const unsigned char data[], unsigned long data_len,
134 | unsigned char mac[], unsigned long mac_len)
135 | { hmac_ctx cx[1];
136 |
137 | hmac_sha_begin(cx);
138 | hmac_sha_key(key, key_len, cx);
139 | hmac_sha_data(data, data_len, cx);
140 | hmac_sha_end(mac, mac_len, cx);
141 | }
142 |
143 | #if defined(__cplusplus)
144 | }
145 | #endif
146 |
--------------------------------------------------------------------------------
/aes/prng.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 is the header file for an implementation of a random data pool based on
34 | the use of an external entropy function (inspired by Peter Gutmann's work).
35 | */
36 |
37 | #ifndef _PRNG_H
38 | #define _PRNG_H
39 |
40 | #include "sha1.h"
41 |
42 | #define PRNG_POOL_LEN 256 /* minimum random pool size */
43 | #define PRNG_MIN_MIX 20 /* min initial pool mixing iterations */
44 |
45 | /* ensure that pool length is a multiple of the SHA1 digest size */
46 |
47 | #define PRNG_POOL_SIZE (SHA1_DIGEST_SIZE * (1 + (PRNG_POOL_LEN - 1) / SHA1_DIGEST_SIZE))
48 |
49 | #if defined(__cplusplus)
50 | extern "C"
51 | {
52 | #endif
53 |
54 | /* A function for providing entropy is a parameter in the prng_init() */
55 | /* call. This function has the following form and returns a maximum */
56 | /* of 'len' bytes of pseudo random data in the buffer 'buf'. It can */
57 | /* return less than 'len' bytes but will be repeatedly called for more */
58 | /* data in this case. */
59 |
60 | typedef int (*prng_entropy_fn)(unsigned char buf[], unsigned int len);
61 |
62 | typedef struct
63 | { unsigned char rbuf[PRNG_POOL_SIZE]; /* the random pool */
64 | unsigned char obuf[PRNG_POOL_SIZE]; /* pool output buffer */
65 | unsigned int pos; /* output buffer position */
66 | prng_entropy_fn entropy; /* entropy function pointer */
67 | } prng_ctx;
68 |
69 | /* initialise the random stream generator */
70 | void prng_init(prng_entropy_fn fun, prng_ctx ctx[1]);
71 |
72 | /* obtain random bytes from the generator */
73 | void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1]);
74 |
75 | /* close the random stream generator */
76 | void prng_end(prng_ctx ctx[1]);
77 |
78 | #if defined(__cplusplus)
79 | }
80 | #endif
81 |
82 | #endif
83 |
--------------------------------------------------------------------------------
/aes/prng.m:
--------------------------------------------------------------------------------
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 a random data pool based on the use of an external
34 | entropy function. It is based on the ideas advocated by Peter Gutmann in
35 | his work on pseudo random sequence generators. It is not a 'paranoid'
36 | random sequence generator and no attempt is made to protect the pool
37 | from prying eyes either by memory locking or by techniques to obscure
38 | its location in memory.
39 | */
40 |
41 | #include
42 | #include "prng.h"
43 |
44 | #if defined(__cplusplus)
45 | extern "C"
46 | {
47 | #endif
48 |
49 | /* mix a random data pool using the SHA1 compression function (as */
50 | /* suggested by Peter Gutmann in his paper on random pools) */
51 |
52 | static void prng_mix(unsigned char buf[])
53 | { unsigned int i, len;
54 | sha1_ctx ctx[1];
55 |
56 | /*lint -e{663} unusual array to pointer conversion */
57 | for(i = 0; i < PRNG_POOL_SIZE; i += SHA1_DIGEST_SIZE)
58 | {
59 | /* copy digest size pool block into SHA1 hash block */
60 | memcpy(ctx->hash, buf + (i ? i : PRNG_POOL_SIZE)
61 | - SHA1_DIGEST_SIZE, SHA1_DIGEST_SIZE);
62 |
63 | /* copy data from pool into the SHA1 data buffer */
64 | len = PRNG_POOL_SIZE - i;
65 | memcpy(ctx->wbuf, buf + i, (len > SHA1_BLOCK_SIZE ? SHA1_BLOCK_SIZE : len));
66 |
67 | if(len < SHA1_BLOCK_SIZE)
68 | memcpy(((char*)ctx->wbuf) + len, buf, SHA1_BLOCK_SIZE - len);
69 |
70 | /* compress using the SHA1 compression function */
71 | sha1_compile(ctx);
72 |
73 | /* put digest size block back into the random pool */
74 | memcpy(buf + i, ctx->hash, SHA1_DIGEST_SIZE);
75 | }
76 | }
77 |
78 | /* refresh the output buffer and update the random pool by adding */
79 | /* entropy and remixing */
80 |
81 | static void update_pool(prng_ctx ctx[1])
82 | { unsigned int i = 0;
83 |
84 | /* transfer random pool data to the output buffer */
85 | memcpy(ctx->obuf, ctx->rbuf, PRNG_POOL_SIZE);
86 |
87 | /* enter entropy data into the pool */
88 | while(i < PRNG_POOL_SIZE)
89 | i += ctx->entropy(ctx->rbuf + i, PRNG_POOL_SIZE - i);
90 |
91 | /* invert and xor the original pool data into the pool */
92 | for(i = 0; i < PRNG_POOL_SIZE; ++i)
93 | ctx->rbuf[i] ^= ~ctx->obuf[i];
94 |
95 | /* mix the pool and the output buffer */
96 | prng_mix(ctx->rbuf);
97 | prng_mix(ctx->obuf);
98 | }
99 |
100 | void prng_init(prng_entropy_fn fun, prng_ctx ctx[1])
101 | { int i;
102 |
103 | /* clear the buffers and the counter in the context */
104 | memset(ctx, 0, sizeof(prng_ctx));
105 |
106 | /* set the pointer to the entropy collection function */
107 | ctx->entropy = fun;
108 |
109 | /* initialise the random data pool */
110 | update_pool(ctx);
111 |
112 | /* mix the pool a minimum number of times */
113 | for(i = 0; i < PRNG_MIN_MIX; ++i)
114 | prng_mix(ctx->rbuf);
115 |
116 | /* update the pool to prime the pool output buffer */
117 | update_pool(ctx);
118 | }
119 |
120 | /* provide random bytes from the random data pool */
121 |
122 | void prng_rand(unsigned char data[], unsigned int data_len, prng_ctx ctx[1])
123 | { unsigned char *rp = data;
124 | unsigned int len, pos = ctx->pos;
125 |
126 | while(data_len)
127 | {
128 | /* transfer 'data_len' bytes (or the number of bytes remaining */
129 | /* the pool output buffer if less) into the output */
130 | len = (data_len < PRNG_POOL_SIZE - pos ? data_len : PRNG_POOL_SIZE - pos);
131 | memcpy(rp, ctx->obuf + pos, len);
132 | rp += len; /* update ouput buffer position pointer */
133 | pos += len; /* update pool output buffer pointer */
134 | data_len -= len; /* update the remaining data count */
135 |
136 | /* refresh the random pool if necessary */
137 | if(pos == PRNG_POOL_SIZE)
138 | {
139 | update_pool(ctx); pos = 0;
140 | }
141 | }
142 |
143 | ctx->pos = pos;
144 | }
145 |
146 | void prng_end(prng_ctx ctx[1])
147 | {
148 | /* ensure the data in the context is destroyed */
149 | memset(ctx, 0, sizeof(prng_ctx));
150 | }
151 |
152 | #if defined(__cplusplus)
153 | }
154 | #endif
155 |
156 |
--------------------------------------------------------------------------------
/aes/pwd2key.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | LICENSE TERMS
6 |
7 | The free distribution and use of this software in both source and binary
8 | form is allowed (with or without changes) provided that:
9 |
10 | 1. distributions of this source code include the above copyright
11 | notice, this list of conditions and the following disclaimer;
12 |
13 | 2. distributions in binary form include the above copyright
14 | notice, this list of conditions and the following disclaimer
15 | in the documentation and/or other associated materials;
16 |
17 | 3. the copyright holder's name is not used to endorse products
18 | built using this software without specific written permission.
19 |
20 | ALTERNATIVELY, provided that this notice is retained in full, this product
21 | may be distributed under the terms of the GNU General Public License (GPL),
22 | in which case the provisions of the GPL apply INSTEAD OF those given above.
23 |
24 | DISCLAIMER
25 |
26 | This software is provided 'as is' with no explicit or implied warranties
27 | in respect of its properties, including, but not limited to, correctness
28 | and/or fitness for purpose.
29 | ---------------------------------------------------------------------------
30 | Issue Date: 26/08/2003
31 |
32 | This is an implementation of RFC2898, which specifies key derivation from
33 | a password and a salt value.
34 | */
35 |
36 | #ifndef PWD2KEY_H
37 | #define PWD2KEY_H
38 |
39 | #if defined(__cplusplus)
40 | extern "C"
41 | {
42 | #endif
43 |
44 | void derive_key(
45 | const unsigned char pwd[], /* the PASSWORD, and */
46 | unsigned int pwd_len, /* its length */
47 | const unsigned char salt[], /* the SALT and its */
48 | unsigned int salt_len, /* length */
49 | unsigned int iter, /* the number of iterations */
50 | unsigned char key[], /* space for the output key */
51 | unsigned int key_len); /* and its required length */
52 |
53 | #if defined(__cplusplus)
54 | }
55 | #endif
56 |
57 | #endif
58 |
--------------------------------------------------------------------------------
/aes/pwd2key.m:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | LICENSE TERMS
6 |
7 | The free distribution and use of this software in both source and binary
8 | form is allowed (with or without changes) provided that:
9 |
10 | 1. distributions of this source code include the above copyright
11 | notice, this list of conditions and the following disclaimer;
12 |
13 | 2. distributions in binary form include the above copyright
14 | notice, this list of conditions and the following disclaimer
15 | in the documentation and/or other associated materials;
16 |
17 | 3. the copyright holder's name is not used to endorse products
18 | built using this software without specific written permission.
19 |
20 | ALTERNATIVELY, provided that this notice is retained in full, this product
21 | may be distributed under the terms of the GNU General Public License (GPL),
22 | in which case the provisions of the GPL apply INSTEAD OF those given above.
23 |
24 | DISCLAIMER
25 |
26 | This software is provided 'as is' with no explicit or implied warranties
27 | in respect of its properties, including, but not limited to, correctness
28 | and/or fitness for purpose.
29 | ---------------------------------------------------------------------------
30 | Issue Date: 26/08/2003
31 |
32 | This is an implementation of RFC2898, which specifies key derivation from
33 | a password and a salt value.
34 | */
35 |
36 | #include
37 | #include "hmac.h"
38 |
39 | #if defined(__cplusplus)
40 | extern "C"
41 | {
42 | #endif
43 |
44 | void derive_key(const unsigned char pwd[], /* the PASSWORD */
45 | unsigned int pwd_len, /* and its length */
46 | const unsigned char salt[], /* the SALT and its */
47 | unsigned int salt_len, /* length */
48 | unsigned int iter, /* the number of iterations */
49 | unsigned char key[], /* space for the output key */
50 | unsigned int key_len)/* and its required length */
51 | {
52 | unsigned int i, j, k, n_blk;
53 | unsigned char uu[HASH_OUTPUT_SIZE], ux[HASH_OUTPUT_SIZE];
54 | hmac_ctx c1[1], c2[1], c3[1];
55 |
56 | /* set HMAC context (c1) for password */
57 | hmac_sha_begin(c1);
58 | hmac_sha_key(pwd, pwd_len, c1);
59 |
60 | /* set HMAC context (c2) for password and salt */
61 | memcpy(c2, c1, sizeof(hmac_ctx));
62 | hmac_sha_data(salt, salt_len, c2);
63 |
64 | /* find the number of SHA blocks in the key */
65 | n_blk = 1 + (key_len - 1) / HASH_OUTPUT_SIZE;
66 |
67 | for(i = 0; i < n_blk; ++i) /* for each block in key */
68 | {
69 | /* ux[] holds the running xor value */
70 | memset(ux, 0, HASH_OUTPUT_SIZE);
71 |
72 | /* set HMAC context (c3) for password and salt */
73 | memcpy(c3, c2, sizeof(hmac_ctx));
74 |
75 | /* enter additional data for 1st block into uu */
76 | uu[0] = (unsigned char)((i + 1) >> 24);
77 | uu[1] = (unsigned char)((i + 1) >> 16);
78 | uu[2] = (unsigned char)((i + 1) >> 8);
79 | uu[3] = (unsigned char)(i + 1);
80 |
81 | /* this is the key mixing iteration */
82 | for(j = 0, k = 4; j < iter; ++j)
83 | {
84 | /* add previous round data to HMAC */
85 | hmac_sha_data(uu, k, c3);
86 |
87 | /* obtain HMAC for uu[] */
88 | hmac_sha_end(uu, HASH_OUTPUT_SIZE, c3);
89 |
90 | /* xor into the running xor block */
91 | for(k = 0; k < HASH_OUTPUT_SIZE; ++k)
92 | ux[k] ^= uu[k];
93 |
94 | /* set HMAC context (c3) for password */
95 | memcpy(c3, c1, sizeof(hmac_ctx));
96 | }
97 |
98 | /* compile key blocks into the key output */
99 | j = 0; k = i * HASH_OUTPUT_SIZE;
100 | while(j < HASH_OUTPUT_SIZE && k < key_len)
101 | key[k++] = ux[j++];
102 | }
103 | }
104 |
105 | #ifdef TEST
106 |
107 | #include
108 |
109 | struct
110 | { unsigned int pwd_len;
111 | unsigned int salt_len;
112 | unsigned int it_count;
113 | unsigned char *pwd;
114 | unsigned char salt[32];
115 | unsigned char key[32];
116 | } tests[] =
117 | {
118 | { 8, 4, 5, (unsigned char*)"password",
119 | {
120 | 0x12, 0x34, 0x56, 0x78
121 | },
122 | {
123 | 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7,
124 | 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5
125 | }
126 | },
127 | { 8, 8, 5, (unsigned char*)"password",
128 | {
129 | 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12
130 | },
131 | {
132 | 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6,
133 | 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49
134 | }
135 | },
136 | { 8, 21, 1, (unsigned char*)"password",
137 | {
138 | "ATHENA.MIT.EDUraeburn"
139 | },
140 | {
141 | 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01,
142 | 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15
143 | }
144 | },
145 | { 8, 21, 2, (unsigned char*)"password",
146 | {
147 | "ATHENA.MIT.EDUraeburn"
148 | },
149 | {
150 | 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e,
151 | 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d
152 | }
153 | },
154 | { 8, 21, 1200, (unsigned char*)"password",
155 | {
156 | "ATHENA.MIT.EDUraeburn"
157 | },
158 | {
159 | 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e,
160 | 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b
161 | }
162 | }
163 | };
164 |
165 | int main()
166 | { unsigned int i, j, key_len = 256;
167 | unsigned char key[256];
168 |
169 | printf("\nTest of RFC2898 Password Based Key Derivation");
170 | for(i = 0; i < 5; ++i)
171 | {
172 | derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt,
173 | tests[i].salt_len, tests[i].it_count, key, key_len);
174 |
175 | printf("\ntest %i: ", i + 1);
176 | printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good");
177 | for(j = 0; j < key_len && j < 64; j += 4)
178 | {
179 | if(j % 16 == 0)
180 | printf("\n");
181 | printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]);
182 | }
183 | printf(j < key_len ? " ... \n" : "\n");
184 | }
185 | printf("\n");
186 | return 0;
187 | }
188 |
189 | #if defined(__cplusplus)
190 | }
191 | #endif
192 |
193 | #endif
194 |
--------------------------------------------------------------------------------
/aes/sha1.h:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | LICENSE TERMS
6 |
7 | The free distribution and use of this software in both source and binary
8 | form is allowed (with or without changes) provided that:
9 |
10 | 1. distributions of this source code include the above copyright
11 | notice, this list of conditions and the following disclaimer;
12 |
13 | 2. distributions in binary form include the above copyright
14 | notice, this list of conditions and the following disclaimer
15 | in the documentation and/or other associated materials;
16 |
17 | 3. the copyright holder's name is not used to endorse products
18 | built using this software without specific written permission.
19 |
20 | ALTERNATIVELY, provided that this notice is retained in full, this product
21 | may be distributed under the terms of the GNU General Public License (GPL),
22 | in which case the provisions of the GPL apply INSTEAD OF those given above.
23 |
24 | DISCLAIMER
25 |
26 | This software is provided 'as is' with no explicit or implied warranties
27 | in respect of its properties, including, but not limited to, correctness
28 | and/or fitness for purpose.
29 | ---------------------------------------------------------------------------
30 | Issue Date: 01/08/2005
31 | */
32 |
33 | #ifndef _SHA1_H
34 | #define _SHA1_H
35 |
36 | #include
37 | #include "brg_types.h"
38 |
39 | #define SHA1_BLOCK_SIZE 64
40 | #define SHA1_DIGEST_SIZE 20
41 |
42 | #if defined(__cplusplus)
43 | extern "C"
44 | {
45 | #endif
46 |
47 | /* type to hold the SHA256 context */
48 |
49 | typedef struct
50 | { uint_32t count[2];
51 | uint_32t hash[5];
52 | uint_32t wbuf[16];
53 | } sha1_ctx;
54 |
55 | /* Note that these prototypes are the same for both bit and */
56 | /* byte oriented implementations. However the length fields */
57 | /* are in bytes or bits as appropriate for the version used */
58 | /* and bit sequences are input as arrays of bytes in which */
59 | /* bit sequences run from the most to the least significant */
60 | /* end of each byte */
61 |
62 | VOID_RETURN sha1_compile(sha1_ctx ctx[1]);
63 |
64 | VOID_RETURN sha1_begin(sha1_ctx ctx[1]);
65 | VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1]);
66 | VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1]);
67 | VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len);
68 |
69 | #if defined(__cplusplus)
70 | }
71 | #endif
72 |
73 | #endif
74 |
--------------------------------------------------------------------------------
/aes/sha1.m:
--------------------------------------------------------------------------------
1 | /*
2 | ---------------------------------------------------------------------------
3 | Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 |
5 | LICENSE TERMS
6 |
7 | The free distribution and use of this software in both source and binary
8 | form is allowed (with or without changes) provided that:
9 |
10 | 1. distributions of this source code include the above copyright
11 | notice, this list of conditions and the following disclaimer;
12 |
13 | 2. distributions in binary form include the above copyright
14 | notice, this list of conditions and the following disclaimer
15 | in the documentation and/or other associated materials;
16 |
17 | 3. the copyright holder's name is not used to endorse products
18 | built using this software without specific written permission.
19 |
20 | ALTERNATIVELY, provided that this notice is retained in full, this product
21 | may be distributed under the terms of the GNU General Public License (GPL),
22 | in which case the provisions of the GPL apply INSTEAD OF those given above.
23 |
24 | DISCLAIMER
25 |
26 | This software is provided 'as is' with no explicit or implied warranties
27 | in respect of its properties, including, but not limited to, correctness
28 | and/or fitness for purpose.
29 | ---------------------------------------------------------------------------
30 | Issue Date: 01/08/2005
31 |
32 | This is a byte oriented version of SHA1 that operates on arrays of bytes
33 | stored in memory.
34 | */
35 |
36 | #include /* for memcpy() etc. */
37 |
38 | #include "sha1.h"
39 | #include "brg_endian.h"
40 |
41 | #if defined(__cplusplus)
42 | extern "C"
43 | {
44 | #endif
45 |
46 | #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
47 | #pragma intrinsic(memcpy)
48 | #endif
49 |
50 | #if 0 && defined(_MSC_VER)
51 | #define rotl32 _lrotl
52 | #define rotr32 _lrotr
53 | #else
54 | #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
55 | #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
56 | #endif
57 |
58 | #if !defined(bswap_32)
59 | #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
60 | #endif
61 |
62 | #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
63 | #define SWAP_BYTES
64 | #else
65 | #undef SWAP_BYTES
66 | #endif
67 |
68 | #if defined(SWAP_BYTES)
69 | #define bsw_32(p,n) \
70 | { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); }
71 | #else
72 | #define bsw_32(p,n)
73 | #endif
74 |
75 | #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
76 |
77 | #if 0
78 |
79 | #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
80 | #define parity(x,y,z) ((x) ^ (y) ^ (z))
81 | #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
82 |
83 | #else /* Discovered by Rich Schroeppel and Colin Plumb */
84 |
85 | #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
86 | #define parity(x,y,z) ((x) ^ (y) ^ (z))
87 | #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
88 |
89 | #endif
90 |
91 | /* Compile 64 bytes of hash data into SHA1 context. Note */
92 | /* that this routine assumes that the byte order in the */
93 | /* ctx->wbuf[] at this point is in such an order that low */
94 | /* address bytes in the ORIGINAL byte stream will go in */
95 | /* this buffer to the high end of 32-bit words on BOTH big */
96 | /* and little endian systems */
97 |
98 | #ifdef ARRAY
99 | #define q(v,n) v[n]
100 | #else
101 | #define q(v,n) v##n
102 | #endif
103 |
104 | #define one_cycle(v,a,b,c,d,e,f,k,h) \
105 | q(v,e) += rotr32(q(v,a),27) + \
106 | f(q(v,b),q(v,c),q(v,d)) + k + h; \
107 | q(v,b) = rotr32(q(v,b), 2)
108 |
109 | #define five_cycle(v,f,k,i) \
110 | one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \
111 | one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \
112 | one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \
113 | one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \
114 | one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
115 |
116 | VOID_RETURN sha1_compile(sha1_ctx ctx[1])
117 | { uint_32t *w = ctx->wbuf;
118 |
119 | #ifdef ARRAY
120 | uint_32t v[5];
121 | memcpy(v, ctx->hash, 5 * sizeof(uint_32t));
122 | #else
123 | uint_32t v0, v1, v2, v3, v4;
124 | v0 = ctx->hash[0]; v1 = ctx->hash[1];
125 | v2 = ctx->hash[2]; v3 = ctx->hash[3];
126 | v4 = ctx->hash[4];
127 | #endif
128 |
129 | #define hf(i) w[i]
130 |
131 | five_cycle(v, ch, 0x5a827999, 0);
132 | five_cycle(v, ch, 0x5a827999, 5);
133 | five_cycle(v, ch, 0x5a827999, 10);
134 | one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
135 |
136 | #undef hf
137 | #define hf(i) (w[(i) & 15] = rotl32( \
138 | w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
139 | ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1))
140 |
141 | one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
142 | one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
143 | one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
144 | one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
145 |
146 | five_cycle(v, parity, 0x6ed9eba1, 20);
147 | five_cycle(v, parity, 0x6ed9eba1, 25);
148 | five_cycle(v, parity, 0x6ed9eba1, 30);
149 | five_cycle(v, parity, 0x6ed9eba1, 35);
150 |
151 | five_cycle(v, maj, 0x8f1bbcdc, 40);
152 | five_cycle(v, maj, 0x8f1bbcdc, 45);
153 | five_cycle(v, maj, 0x8f1bbcdc, 50);
154 | five_cycle(v, maj, 0x8f1bbcdc, 55);
155 |
156 | five_cycle(v, parity, 0xca62c1d6, 60);
157 | five_cycle(v, parity, 0xca62c1d6, 65);
158 | five_cycle(v, parity, 0xca62c1d6, 70);
159 | five_cycle(v, parity, 0xca62c1d6, 75);
160 |
161 | #ifdef ARRAY
162 | ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
163 | ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
164 | ctx->hash[4] += v[4];
165 | #else
166 | ctx->hash[0] += v0; ctx->hash[1] += v1;
167 | ctx->hash[2] += v2; ctx->hash[3] += v3;
168 | ctx->hash[4] += v4;
169 | #endif
170 | }
171 |
172 | VOID_RETURN sha1_begin(sha1_ctx ctx[1])
173 | {
174 | ctx->count[0] = ctx->count[1] = 0;
175 | ctx->hash[0] = 0x67452301;
176 | ctx->hash[1] = 0xefcdab89;
177 | ctx->hash[2] = 0x98badcfe;
178 | ctx->hash[3] = 0x10325476;
179 | ctx->hash[4] = 0xc3d2e1f0;
180 | }
181 |
182 | /* SHA1 hash data in an array of bytes into hash buffer and */
183 | /* call the hash_compile function as required. */
184 |
185 | VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])
186 | { uint_32t pos = (uint_32t)(ctx->count[0] & SHA1_MASK),
187 | space = SHA1_BLOCK_SIZE - pos;
188 | const unsigned char *sp = data;
189 |
190 | if((ctx->count[0] += len) < len)
191 | ++(ctx->count[1]);
192 |
193 | while(len >= space) /* tranfer whole blocks if possible */
194 | {
195 | memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
196 | sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
197 | bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
198 | sha1_compile(ctx);
199 | }
200 |
201 | memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
202 | }
203 |
204 | /* SHA1 final padding and digest calculation */
205 |
206 | VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1])
207 | { uint_32t i = (uint_32t)(ctx->count[0] & SHA1_MASK);
208 |
209 | /* put bytes in the buffer in an order in which references to */
210 | /* 32-bit words will put bytes with lower addresses into the */
211 | /* top of 32 bit words on BOTH big and little endian machines */
212 | bsw_32(ctx->wbuf, (i + 3) >> 2);
213 |
214 | /* we now need to mask valid bytes and add the padding which is */
215 | /* a single 1 bit and as many zero bits as necessary. Note that */
216 | /* we can always add the first padding byte here because the */
217 | /* buffer always has at least one empty slot */
218 | ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
219 | ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
220 |
221 | /* we need 9 or more empty positions, one for the padding byte */
222 | /* (above) and eight for the length count. If there is not */
223 | /* enough space, pad and empty the buffer */
224 | if(i > SHA1_BLOCK_SIZE - 9)
225 | {
226 | if(i < 60) ctx->wbuf[15] = 0;
227 | sha1_compile(ctx);
228 | i = 0;
229 | }
230 | else /* compute a word index for the empty buffer positions */
231 | i = (i >> 2) + 1;
232 |
233 | while(i < 14) /* and zero pad all but last two positions */
234 | ctx->wbuf[i++] = 0;
235 |
236 | /* the following 32-bit length fields are assembled in the */
237 | /* wrong byte order on little endian machines but this is */
238 | /* corrected later since they are only ever used as 32-bit */
239 | /* word values. */
240 | ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
241 | ctx->wbuf[15] = ctx->count[0] << 3;
242 | sha1_compile(ctx);
243 |
244 | /* extract the hash value as bytes in case the hash buffer is */
245 | /* misaligned for 32-bit words */
246 | for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
247 | hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
248 | }
249 |
250 | VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len)
251 | { sha1_ctx cx[1];
252 |
253 | sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
254 | }
255 |
256 | #if defined(__cplusplus)
257 | }
258 | #endif
259 |
--------------------------------------------------------------------------------
/minizip/crypt.h:
--------------------------------------------------------------------------------
1 | /* crypt.h -- base code for traditional PKWARE encryption
2 | Version 1.01e, February 12th, 2005
3 |
4 | Copyright (C) 1998-2005 Gilles Vollant
5 | Modifications for Info-ZIP crypting
6 | Copyright (C) 2003 Terry Thorsen
7 |
8 | This code is a modified version of crypting code in Info-ZIP distribution
9 |
10 | Copyright (C) 1990-2000 Info-ZIP. All rights reserved.
11 |
12 | See the Info-ZIP LICENSE file version 2000-Apr-09 or later for terms of use
13 | which also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
14 |
15 | The encryption/decryption parts of this source code (as opposed to the
16 | non-echoing password parts) were originally written in Europe. The
17 | whole source package can be freely distributed, including from the USA.
18 | (Prior to January 2000, re-export from the US was a violation of US law.)
19 |
20 | This encryption code is a direct transcription of the algorithm from
21 | Roger Schlafly, described by Phil Katz in the file appnote.txt. This
22 | file (appnote.txt) is distributed with the PKZIP program (even in the
23 | version without encryption capabilities).
24 |
25 | If you don't need crypting in your application, just define symbols
26 | NOCRYPT and NOUNCRYPT.
27 | */
28 |
29 | #define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8))
30 |
31 | /***********************************************************************
32 | * Return the next byte in the pseudo-random sequence
33 | */
34 | static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab)
35 | {
36 | unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an
37 | * unpredictable manner on 16-bit systems; not a problem
38 | * with any known compiler so far, though */
39 |
40 | temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2;
41 | return (int)(((temp * (temp ^ 1)) >> 8) & 0xff);
42 | }
43 |
44 | /***********************************************************************
45 | * Update the encryption keys with the next byte of plain text
46 | */
47 | static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c)
48 | {
49 | (*(pkeys+0)) = CRC32((*(pkeys+0)), c);
50 | (*(pkeys+1)) += (*(pkeys+0)) & 0xff;
51 | (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1;
52 | {
53 | register int keyshift = (int)((*(pkeys+1)) >> 24);
54 | (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift);
55 | }
56 | return c;
57 | }
58 |
59 |
60 | /***********************************************************************
61 | * Initialize the encryption keys and the random header according to
62 | * the given password.
63 | */
64 | static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab)
65 | {
66 | *(pkeys+0) = 305419896L;
67 | *(pkeys+1) = 591751049L;
68 | *(pkeys+2) = 878082192L;
69 | while (*passwd != 0) {
70 | update_keys(pkeys,pcrc_32_tab,(int)*passwd);
71 | passwd++;
72 | }
73 | }
74 |
75 | #define zdecode(pkeys,pcrc_32_tab,c) \
76 | (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab)))
77 |
78 | #define zencode(pkeys,pcrc_32_tab,c,t) \
79 | (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c))
80 |
81 | #ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED
82 |
83 | #define RAND_HEAD_LEN 12
84 | /* "last resort" source for second part of crypt seed pattern */
85 | # ifndef ZCR_SEED2
86 | # define ZCR_SEED2 3141592654UL /* use PI as default pattern */
87 | # endif
88 |
89 | static int crypthead(const char* passwd, /* password string */
90 | unsigned char* buf, /* where to write header */
91 | int bufSize,
92 | unsigned long* pkeys,
93 | const unsigned long* pcrc_32_tab,
94 | unsigned long crcForCrypting)
95 | {
96 | int n; /* index in random header */
97 | int t; /* temporary */
98 | int c; /* random byte */
99 | unsigned char header[RAND_HEAD_LEN-2]; /* random header */
100 | static unsigned calls = 0; /* ensure different random header each time */
101 |
102 | if (bufSize> 7) & 0xff;
117 | header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t);
118 | }
119 | /* Encrypt random header (last two bytes is high word of crc) */
120 | init_keys(passwd, pkeys, pcrc_32_tab);
121 | for (n = 0; n < RAND_HEAD_LEN-2; n++)
122 | {
123 | buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t);
124 | }
125 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t);
126 | buf[n++] = (unsigned char)zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t);
127 | return n;
128 | }
129 |
130 | #endif
131 |
--------------------------------------------------------------------------------
/minizip/ioapi.h:
--------------------------------------------------------------------------------
1 | /* ioapi.h -- IO base function header for compress/uncompress .zip
2 | part of the MiniZip project
3 |
4 | Copyright (C) 1998-2010 Gilles Vollant
5 | http://www.winimage.com/zLibDll/minizip.html
6 | Modifications for Zip64 support
7 | Copyright (C) 2009-2010 Mathias Svensson
8 | http://result42.com
9 |
10 | This program is distributed under the terms of the same license as zlib.
11 | See the accompanying LICENSE file for the full text of the license.
12 | */
13 |
14 | #ifndef _ZLIBIOAPI64_H
15 | #define _ZLIBIOAPI64_H
16 |
17 | #if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
18 | # ifndef __USE_FILE_OFFSET64
19 | # define __USE_FILE_OFFSET64
20 | # endif
21 | # ifndef __USE_LARGEFILE64
22 | # define __USE_LARGEFILE64
23 | # endif
24 | # ifndef _LARGEFILE64_SOURCE
25 | # define _LARGEFILE64_SOURCE
26 | # endif
27 | # ifndef _FILE_OFFSET_BIT
28 | # define _FILE_OFFSET_BIT 64
29 | # endif
30 | #endif
31 |
32 | #include
33 | #include
34 | #include "zlib.h"
35 |
36 | #if defined(USE_FILE32API)
37 | # define fopen64 fopen
38 | # define ftello64 ftell
39 | # define fseeko64 fseek
40 | #else
41 | # if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
42 | # define fopen64 fopen
43 | # define ftello64 ftello
44 | # define fseeko64 fseeko
45 | # endif
46 | # ifdef _MSC_VER
47 | # define fopen64 fopen
48 | # if (_MSC_VER >= 1400) && (!(defined(NO_MSCVER_FILE64_FUNC)))
49 | # define ftello64 _ftelli64
50 | # define fseeko64 _fseeki64
51 | # else /* old MSC */
52 | # define ftello64 ftell
53 | # define fseeko64 fseek
54 | # endif
55 | # endif
56 | #endif
57 |
58 | /* a type choosen by DEFINE */
59 | #ifdef HAVE_64BIT_INT_CUSTOM
60 | typedef 64BIT_INT_CUSTOM_TYPE ZPOS64_T;
61 | #else
62 | # ifdef HAS_STDINT_H
63 | # include "stdint.h"
64 | typedef uint64_t ZPOS64_T;
65 | # else
66 | # if defined(_MSC_VER) || defined(__BORLANDC__)
67 | typedef unsigned __int64 ZPOS64_T;
68 | # else
69 | typedef unsigned long long int ZPOS64_T;
70 | # endif
71 | # endif
72 | #endif
73 |
74 | #ifdef __cplusplus
75 | extern "C" {
76 | #endif
77 |
78 | #define ZLIB_FILEFUNC_SEEK_CUR (1)
79 | #define ZLIB_FILEFUNC_SEEK_END (2)
80 | #define ZLIB_FILEFUNC_SEEK_SET (0)
81 |
82 | #define ZLIB_FILEFUNC_MODE_READ (1)
83 | #define ZLIB_FILEFUNC_MODE_WRITE (2)
84 | #define ZLIB_FILEFUNC_MODE_READWRITEFILTER (3)
85 | #define ZLIB_FILEFUNC_MODE_EXISTING (4)
86 | #define ZLIB_FILEFUNC_MODE_CREATE (8)
87 |
88 | #ifndef ZCALLBACK
89 | # if (defined(WIN32) || defined(_WIN32) || defined (WINDOWS) || \
90 | defined (_WINDOWS)) && defined(CALLBACK) && defined (USEWINDOWS_CALLBACK)
91 | # define ZCALLBACK CALLBACK
92 | # else
93 | # define ZCALLBACK
94 | # endif
95 | #endif
96 |
97 | typedef voidpf (ZCALLBACK *open_file_func) OF((voidpf opaque, const char* filename, int mode));
98 | typedef voidpf (ZCALLBACK *opendisk_file_func) OF((voidpf opaque, voidpf stream, int number_disk, int mode));
99 | typedef uLong (ZCALLBACK *read_file_func) OF((voidpf opaque, voidpf stream, void* buf, uLong size));
100 | typedef uLong (ZCALLBACK *write_file_func) OF((voidpf opaque, voidpf stream, const void* buf, uLong size));
101 | typedef int (ZCALLBACK *close_file_func) OF((voidpf opaque, voidpf stream));
102 | typedef int (ZCALLBACK *testerror_file_func) OF((voidpf opaque, voidpf stream));
103 |
104 | typedef long (ZCALLBACK *tell_file_func) OF((voidpf opaque, voidpf stream));
105 | typedef long (ZCALLBACK *seek_file_func) OF((voidpf opaque, voidpf stream, uLong offset, int origin));
106 |
107 | /* here is the "old" 32 bits structure structure */
108 | typedef struct zlib_filefunc_def_s
109 | {
110 | open_file_func zopen_file;
111 | opendisk_file_func zopendisk_file;
112 | read_file_func zread_file;
113 | write_file_func zwrite_file;
114 | tell_file_func ztell_file;
115 | seek_file_func zseek_file;
116 | close_file_func zclose_file;
117 | testerror_file_func zerror_file;
118 | voidpf opaque;
119 | } zlib_filefunc_def;
120 |
121 | typedef ZPOS64_T (ZCALLBACK *tell64_file_func) OF((voidpf opaque, voidpf stream));
122 | typedef long (ZCALLBACK *seek64_file_func) OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
123 | typedef voidpf (ZCALLBACK *open64_file_func) OF((voidpf opaque, const void* filename, int mode));
124 | typedef voidpf (ZCALLBACK *opendisk64_file_func)OF((voidpf opaque, voidpf stream, int number_disk, int mode));
125 |
126 | typedef struct zlib_filefunc64_def_s
127 | {
128 | open64_file_func zopen64_file;
129 | opendisk64_file_func zopendisk64_file;
130 | read_file_func zread_file;
131 | write_file_func zwrite_file;
132 | tell64_file_func ztell64_file;
133 | seek64_file_func zseek64_file;
134 | close_file_func zclose_file;
135 | testerror_file_func zerror_file;
136 | voidpf opaque;
137 | } zlib_filefunc64_def;
138 |
139 | void fill_fopen_filefunc OF((zlib_filefunc_def* pzlib_filefunc_def));
140 | void fill_fopen64_filefunc OF((zlib_filefunc64_def* pzlib_filefunc_def));
141 |
142 | /* now internal definition, only for zip.c and unzip.h */
143 | typedef struct zlib_filefunc64_32_def_s
144 | {
145 | zlib_filefunc64_def zfile_func64;
146 | open_file_func zopen32_file;
147 | opendisk_file_func zopendisk32_file;
148 | tell_file_func ztell32_file;
149 | seek_file_func zseek32_file;
150 | } zlib_filefunc64_32_def;
151 |
152 | #define ZREAD64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zread_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size))
153 | #define ZWRITE64(filefunc,filestream,buf,size) ((*((filefunc).zfile_func64.zwrite_file)) ((filefunc).zfile_func64.opaque,filestream,buf,size))
154 | /*#define ZTELL64(filefunc,filestream) ((*((filefunc).ztell64_file)) ((filefunc).opaque,filestream))*/
155 | /*#define ZSEEK64(filefunc,filestream,pos,mode) ((*((filefunc).zseek64_file)) ((filefunc).opaque,filestream,pos,mode))*/
156 | #define ZCLOSE64(filefunc,filestream) ((*((filefunc).zfile_func64.zclose_file)) ((filefunc).zfile_func64.opaque,filestream))
157 | #define ZERROR64(filefunc,filestream) ((*((filefunc).zfile_func64.zerror_file)) ((filefunc).zfile_func64.opaque,filestream))
158 |
159 | voidpf call_zopen64 OF((const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode));
160 | voidpf call_zopendisk64 OF((const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, int number_disk, int mode));
161 | long call_zseek64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin));
162 | ZPOS64_T call_ztell64 OF((const zlib_filefunc64_32_def* pfilefunc,voidpf filestream));
163 |
164 | void fill_zlib_filefunc64_32_def_from_filefunc32 OF((zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32));
165 |
166 | #define ZOPEN64(filefunc,filename,mode) (call_zopen64((&(filefunc)),(filename),(mode)))
167 | #define ZOPENDISK64(filefunc,filestream,diskn,mode) (call_zopendisk64((&(filefunc)),(filestream),(diskn),(mode)))
168 | #define ZTELL64(filefunc,filestream) (call_ztell64((&(filefunc)),(filestream)))
169 | #define ZSEEK64(filefunc,filestream,pos,mode) (call_zseek64((&(filefunc)),(filestream),(pos),(mode)))
170 |
171 | #ifdef __cplusplus
172 | }
173 | #endif
174 |
175 | #endif
176 |
--------------------------------------------------------------------------------
/minizip/ioapi.m:
--------------------------------------------------------------------------------
1 | /* ioapi.h -- IO base function header for compress/uncompress .zip
2 | part of the MiniZip project
3 |
4 | Copyright (C) 1998-2010 Gilles Vollant
5 | http://www.winimage.com/zLibDll/minizip.html
6 | Modifications for Zip64 support
7 | Copyright (C) 2009-2010 Mathias Svensson
8 | http://result42.com
9 |
10 | This program is distributed under the terms of the same license as zlib.
11 | See the accompanying LICENSE file for the full text of the license.
12 | */
13 |
14 | #include
15 | #include
16 |
17 | #include "ioapi.h"
18 |
19 | #if defined(_WIN32)
20 | # define snprintf _snprintf
21 | #endif
22 |
23 | #ifdef __APPLE__
24 | /* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */
25 | # define FOPEN_FUNC(filename, mode) fopen(filename, mode)
26 | # define FTELLO_FUNC(stream) ftello(stream)
27 | # define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
28 | #else
29 | # define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
30 | # define FTELLO_FUNC(stream) ftello64(stream)
31 | # define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
32 | #endif
33 |
34 | /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
35 | #ifndef SEEK_CUR
36 | # define SEEK_CUR 1
37 | #endif
38 | #ifndef SEEK_END
39 | # define SEEK_END 2
40 | #endif
41 | #ifndef SEEK_SET
42 | # define SEEK_SET 0
43 | #endif
44 |
45 | voidpf call_zopen64 (const zlib_filefunc64_32_def* pfilefunc,const void*filename,int mode)
46 | {
47 | if (pfilefunc->zfile_func64.zopen64_file != NULL)
48 | return (*(pfilefunc->zfile_func64.zopen64_file)) (pfilefunc->zfile_func64.opaque,filename,mode);
49 | return (*(pfilefunc->zopen32_file))(pfilefunc->zfile_func64.opaque,(const char*)filename,mode);
50 | }
51 |
52 | voidpf call_zopendisk64 OF((const zlib_filefunc64_32_def* pfilefunc, voidpf filestream, int number_disk, int mode))
53 | {
54 | if (pfilefunc->zfile_func64.zopendisk64_file != NULL)
55 | return (*(pfilefunc->zfile_func64.zopendisk64_file)) (pfilefunc->zfile_func64.opaque,filestream,number_disk,mode);
56 | return (*(pfilefunc->zopendisk32_file))(pfilefunc->zfile_func64.opaque,filestream,number_disk,mode);
57 | }
58 |
59 | long call_zseek64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream, ZPOS64_T offset, int origin)
60 | {
61 | uLong offsetTruncated;
62 | if (pfilefunc->zfile_func64.zseek64_file != NULL)
63 | return (*(pfilefunc->zfile_func64.zseek64_file)) (pfilefunc->zfile_func64.opaque,filestream,offset,origin);
64 | offsetTruncated = (uLong)offset;
65 | if (offsetTruncated != offset)
66 | return -1;
67 | return (*(pfilefunc->zseek32_file))(pfilefunc->zfile_func64.opaque,filestream,offsetTruncated,origin);
68 | }
69 |
70 | ZPOS64_T call_ztell64 (const zlib_filefunc64_32_def* pfilefunc,voidpf filestream)
71 | {
72 | uLong tell_uLong;
73 | if (pfilefunc->zfile_func64.zseek64_file != NULL)
74 | return (*(pfilefunc->zfile_func64.ztell64_file)) (pfilefunc->zfile_func64.opaque,filestream);
75 | tell_uLong = (*(pfilefunc->ztell32_file))(pfilefunc->zfile_func64.opaque,filestream);
76 | if ((tell_uLong) == 0xffffffff)
77 | return (ZPOS64_T)-1;
78 | return tell_uLong;
79 | }
80 |
81 | void fill_zlib_filefunc64_32_def_from_filefunc32(zlib_filefunc64_32_def* p_filefunc64_32,const zlib_filefunc_def* p_filefunc32)
82 | {
83 | p_filefunc64_32->zfile_func64.zopen64_file = NULL;
84 | p_filefunc64_32->zfile_func64.zopendisk64_file = NULL;
85 | p_filefunc64_32->zopen32_file = p_filefunc32->zopen_file;
86 | p_filefunc64_32->zopendisk32_file = p_filefunc32->zopendisk_file;
87 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
88 | p_filefunc64_32->zfile_func64.zread_file = p_filefunc32->zread_file;
89 | p_filefunc64_32->zfile_func64.zwrite_file = p_filefunc32->zwrite_file;
90 | p_filefunc64_32->zfile_func64.ztell64_file = NULL;
91 | p_filefunc64_32->zfile_func64.zseek64_file = NULL;
92 | p_filefunc64_32->zfile_func64.zclose_file = p_filefunc32->zclose_file;
93 | p_filefunc64_32->zfile_func64.zerror_file = p_filefunc32->zerror_file;
94 | p_filefunc64_32->zfile_func64.opaque = p_filefunc32->opaque;
95 | p_filefunc64_32->zseek32_file = p_filefunc32->zseek_file;
96 | p_filefunc64_32->ztell32_file = p_filefunc32->ztell_file;
97 | }
98 |
99 | static voidpf ZCALLBACK fopen_file_func OF((voidpf opaque, const char* filename, int mode));
100 | static uLong ZCALLBACK fread_file_func OF((voidpf opaque, voidpf stream, void* buf, uLong size));
101 | static uLong ZCALLBACK fwrite_file_func OF((voidpf opaque, voidpf stream, const void* buf,uLong size));
102 | static ZPOS64_T ZCALLBACK ftell64_file_func OF((voidpf opaque, voidpf stream));
103 | static long ZCALLBACK fseek64_file_func OF((voidpf opaque, voidpf stream, ZPOS64_T offset, int origin));
104 | static int ZCALLBACK fclose_file_func OF((voidpf opaque, voidpf stream));
105 | static int ZCALLBACK ferror_file_func OF((voidpf opaque, voidpf stream));
106 |
107 | typedef struct
108 | {
109 | FILE *file;
110 | int filenameLength;
111 | void *filename;
112 | } FILE_IOPOSIX;
113 |
114 | static voidpf file_build_ioposix(FILE *file, const char *filename)
115 | {
116 | FILE_IOPOSIX *ioposix = NULL;
117 | if (file == NULL)
118 | return NULL;
119 | ioposix = (FILE_IOPOSIX*)malloc(sizeof(FILE_IOPOSIX));
120 | ioposix->file = file;
121 | ioposix->filenameLength = (int)strlen(filename) + 1;
122 | ioposix->filename = (char*)malloc(ioposix->filenameLength * sizeof(char));
123 | strncpy(ioposix->filename, filename, ioposix->filenameLength);
124 | return (voidpf)ioposix;
125 | }
126 |
127 | static voidpf ZCALLBACK fopen_file_func (voidpf opaque, const char* filename, int mode)
128 | {
129 | FILE* file = NULL;
130 | const char* mode_fopen = NULL;
131 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
132 | mode_fopen = "rb";
133 | else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
134 | mode_fopen = "r+b";
135 | else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
136 | mode_fopen = "wb";
137 |
138 | if ((filename != NULL) && (mode_fopen != NULL))
139 | {
140 | file = fopen(filename, mode_fopen);
141 | return file_build_ioposix(file, filename);
142 | }
143 | return file;
144 | }
145 |
146 | static voidpf ZCALLBACK fopen64_file_func (voidpf opaque, const void* filename, int mode)
147 | {
148 | FILE* file = NULL;
149 | const char* mode_fopen = NULL;
150 | if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
151 | mode_fopen = "rb";
152 | else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
153 | mode_fopen = "r+b";
154 | else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
155 | mode_fopen = "wb";
156 |
157 | if ((filename != NULL) && (mode_fopen != NULL))
158 | {
159 | file = FOPEN_FUNC((const char*)filename, mode_fopen);
160 | return file_build_ioposix(file, (const char*)filename);
161 | }
162 | return file;
163 | }
164 |
165 | static voidpf ZCALLBACK fopendisk64_file_func (voidpf opaque, voidpf stream, int number_disk, int mode)
166 | {
167 | FILE_IOPOSIX *ioposix = NULL;
168 | char *diskFilename = NULL;
169 | voidpf ret = NULL;
170 | int i = 0;
171 |
172 | if (stream == NULL)
173 | return NULL;
174 | ioposix = (FILE_IOPOSIX*)stream;
175 | diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
176 | strncpy(diskFilename, ioposix->filename, ioposix->filenameLength);
177 | for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
178 | {
179 | if (diskFilename[i] != '.')
180 | continue;
181 | snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1);
182 | break;
183 | }
184 | if (i >= 0)
185 | ret = fopen64_file_func(opaque, diskFilename, mode);
186 | free(diskFilename);
187 | return ret;
188 | }
189 |
190 | static voidpf ZCALLBACK fopendisk_file_func (voidpf opaque, voidpf stream, int number_disk, int mode)
191 | {
192 | FILE_IOPOSIX *ioposix = NULL;
193 | char *diskFilename = NULL;
194 | voidpf ret = NULL;
195 | int i = 0;
196 |
197 | if (stream == NULL)
198 | return NULL;
199 | ioposix = (FILE_IOPOSIX*)stream;
200 | diskFilename = (char*)malloc(ioposix->filenameLength * sizeof(char));
201 | strncpy(diskFilename, ioposix->filename, ioposix->filenameLength);
202 | for (i = ioposix->filenameLength - 1; i >= 0; i -= 1)
203 | {
204 | if (diskFilename[i] != '.')
205 | continue;
206 | snprintf(&diskFilename[i], ioposix->filenameLength - i, ".z%02d", number_disk + 1);
207 | break;
208 | }
209 | if (i >= 0)
210 | ret = fopen_file_func(opaque, diskFilename, mode);
211 | free(diskFilename);
212 | return ret;
213 | }
214 |
215 | static uLong ZCALLBACK fread_file_func (voidpf opaque, voidpf stream, void* buf, uLong size)
216 | {
217 | FILE_IOPOSIX *ioposix = NULL;
218 | uLong ret;
219 | if (stream == NULL)
220 | return -1;
221 | ioposix = (FILE_IOPOSIX*)stream;
222 | ret = (uLong)fread(buf, 1, (size_t)size, ioposix->file);
223 | return ret;
224 | }
225 |
226 | static uLong ZCALLBACK fwrite_file_func (voidpf opaque, voidpf stream, const void* buf, uLong size)
227 | {
228 | FILE_IOPOSIX *ioposix = NULL;
229 | uLong ret;
230 | if (stream == NULL)
231 | return -1;
232 | ioposix = (FILE_IOPOSIX*)stream;
233 | ret = (uLong)fwrite(buf, 1, (size_t)size, ioposix->file);
234 | return ret;
235 | }
236 |
237 | static long ZCALLBACK ftell_file_func (voidpf opaque, voidpf stream)
238 | {
239 | FILE_IOPOSIX *ioposix = NULL;
240 | long ret = -1;
241 | if (stream == NULL)
242 | return ret;
243 | ioposix = (FILE_IOPOSIX*)stream;
244 | ret = ftell(ioposix->file);
245 | return ret;
246 | }
247 |
248 | static ZPOS64_T ZCALLBACK ftell64_file_func (voidpf opaque, voidpf stream)
249 | {
250 | FILE_IOPOSIX *ioposix = NULL;
251 | ZPOS64_T ret = -1;
252 | if (stream == NULL)
253 | return ret;
254 | ioposix = (FILE_IOPOSIX*)stream;
255 | ret = FTELLO_FUNC(ioposix->file);
256 | return ret;
257 | }
258 |
259 | static long ZCALLBACK fseek_file_func (voidpf opaque, voidpf stream, uLong offset, int origin)
260 | {
261 | FILE_IOPOSIX *ioposix = NULL;
262 | int fseek_origin = 0;
263 | long ret = 0;
264 |
265 | if (stream == NULL)
266 | return -1;
267 | ioposix = (FILE_IOPOSIX*)stream;
268 |
269 | switch (origin)
270 | {
271 | case ZLIB_FILEFUNC_SEEK_CUR:
272 | fseek_origin = SEEK_CUR;
273 | break;
274 | case ZLIB_FILEFUNC_SEEK_END:
275 | fseek_origin = SEEK_END;
276 | break;
277 | case ZLIB_FILEFUNC_SEEK_SET:
278 | fseek_origin = SEEK_SET;
279 | break;
280 | default:
281 | return -1;
282 | }
283 | if (fseek(ioposix->file, offset, fseek_origin) != 0)
284 | ret = -1;
285 | return ret;
286 | }
287 |
288 | static long ZCALLBACK fseek64_file_func (voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
289 | {
290 | FILE_IOPOSIX *ioposix = NULL;
291 | int fseek_origin = 0;
292 | long ret = 0;
293 |
294 | if (stream == NULL)
295 | return -1;
296 | ioposix = (FILE_IOPOSIX*)stream;
297 |
298 | switch (origin)
299 | {
300 | case ZLIB_FILEFUNC_SEEK_CUR:
301 | fseek_origin = SEEK_CUR;
302 | break;
303 | case ZLIB_FILEFUNC_SEEK_END:
304 | fseek_origin = SEEK_END;
305 | break;
306 | case ZLIB_FILEFUNC_SEEK_SET:
307 | fseek_origin = SEEK_SET;
308 | break;
309 | default:
310 | return -1;
311 | }
312 |
313 | if(FSEEKO_FUNC(ioposix->file, offset, fseek_origin) != 0)
314 | ret = -1;
315 |
316 | return ret;
317 | }
318 |
319 |
320 | static int ZCALLBACK fclose_file_func (voidpf opaque, voidpf stream)
321 | {
322 | FILE_IOPOSIX *ioposix = NULL;
323 | int ret = -1;
324 | if (stream == NULL)
325 | return ret;
326 | ioposix = (FILE_IOPOSIX*)stream;
327 | if (ioposix->filename != NULL)
328 | free(ioposix->filename);
329 | ret = fclose(ioposix->file);
330 | free(ioposix);
331 | return ret;
332 | }
333 |
334 | static int ZCALLBACK ferror_file_func (voidpf opaque, voidpf stream)
335 | {
336 | FILE_IOPOSIX *ioposix = NULL;
337 | int ret = -1;
338 | if (stream == NULL)
339 | return ret;
340 | ioposix = (FILE_IOPOSIX*)stream;
341 | ret = ferror(ioposix->file);
342 | return ret;
343 | }
344 |
345 | void fill_fopen_filefunc (zlib_filefunc_def* pzlib_filefunc_def)
346 | {
347 | pzlib_filefunc_def->zopen_file = fopen_file_func;
348 | pzlib_filefunc_def->zopendisk_file = fopendisk_file_func;
349 | pzlib_filefunc_def->zread_file = fread_file_func;
350 | pzlib_filefunc_def->zwrite_file = fwrite_file_func;
351 | pzlib_filefunc_def->ztell_file = ftell_file_func;
352 | pzlib_filefunc_def->zseek_file = fseek_file_func;
353 | pzlib_filefunc_def->zclose_file = fclose_file_func;
354 | pzlib_filefunc_def->zerror_file = ferror_file_func;
355 | pzlib_filefunc_def->opaque = NULL;
356 | }
357 |
358 | void fill_fopen64_filefunc (zlib_filefunc64_def* pzlib_filefunc_def)
359 | {
360 | pzlib_filefunc_def->zopen64_file = fopen64_file_func;
361 | pzlib_filefunc_def->zopendisk64_file = fopendisk64_file_func;
362 | pzlib_filefunc_def->zread_file = fread_file_func;
363 | pzlib_filefunc_def->zwrite_file = fwrite_file_func;
364 | pzlib_filefunc_def->ztell64_file = ftell64_file_func;
365 | pzlib_filefunc_def->zseek64_file = fseek64_file_func;
366 | pzlib_filefunc_def->zclose_file = fclose_file_func;
367 | pzlib_filefunc_def->zerror_file = ferror_file_func;
368 | pzlib_filefunc_def->opaque = NULL;
369 | }
370 |
--------------------------------------------------------------------------------
/minizip/mztools.h:
--------------------------------------------------------------------------------
1 | /*
2 | Additional tools for Minizip
3 | Code: Xavier Roche '2004
4 | License: Same as ZLIB (www.gzip.org)
5 | */
6 |
7 | #ifndef _zip_tools_H
8 | #define _zip_tools_H
9 |
10 | #ifdef __cplusplus
11 | extern "C" {
12 | #endif
13 |
14 | #ifndef _ZLIB_H
15 | #include "zlib.h"
16 | #endif
17 |
18 | #include "unzip.h"
19 |
20 | /* Repair a ZIP file (missing central directory)
21 | file: file to recover
22 | fileOut: output file after recovery
23 | fileOutTmp: temporary file name used for recovery
24 | */
25 | extern int ZEXPORT unzRepair(const char* file,
26 | const char* fileOut,
27 | const char* fileOutTmp,
28 | uLong* nRecovered,
29 | uLong* bytesRecovered);
30 |
31 | #endif
32 |
--------------------------------------------------------------------------------
/minizip/mztools.m:
--------------------------------------------------------------------------------
1 | /*
2 | Additional tools for Minizip
3 | Code: Xavier Roche '2004
4 | License: Same as ZLIB (www.gzip.org)
5 | */
6 |
7 | /* Code */
8 | #include
9 | #include
10 | #include
11 | #include "zlib.h"
12 | #include "unzip.h"
13 | #include "mztools.h"
14 |
15 | #define READ_8(adr) ((unsigned char)*(adr))
16 | #define READ_16(adr) ( READ_8(adr) | (READ_8(adr+1) << 8) )
17 | #define READ_32(adr) ( READ_16(adr) | (READ_16((adr)+2) << 16) )
18 |
19 | #define WRITE_8(buff, n) do { \
20 | *((unsigned char*)(buff)) = (unsigned char) ((n) & 0xff); \
21 | } while(0)
22 | #define WRITE_16(buff, n) do { \
23 | WRITE_8((unsigned char*)(buff), n); \
24 | WRITE_8(((unsigned char*)(buff)) + 1, (n) >> 8); \
25 | } while(0)
26 | #define WRITE_32(buff, n) do { \
27 | WRITE_16((unsigned char*)(buff), (n) & 0xffff); \
28 | WRITE_16((unsigned char*)(buff) + 2, (n) >> 16); \
29 | } while(0)
30 |
31 | extern int ZEXPORT unzRepair(file, fileOut, fileOutTmp, nRecovered, bytesRecovered)
32 | const char* file;
33 | const char* fileOut;
34 | const char* fileOutTmp;
35 | uLong* nRecovered;
36 | uLong* bytesRecovered;
37 | {
38 | int err = Z_OK;
39 | FILE* fpZip = fopen(file, "rb");
40 | FILE* fpOut = fopen(fileOut, "wb");
41 | FILE* fpOutCD = fopen(fileOutTmp, "wb");
42 | if (fpZip != NULL && fpOut != NULL) {
43 | int entries = 0;
44 | uLong totalBytes = 0;
45 | char header[30];
46 | char filename[256];
47 | char extra[1024];
48 | int offset = 0;
49 | int offsetCD = 0;
50 | while ( fread(header, 1, 30, fpZip) == 30 ) {
51 | int currentOffset = offset;
52 |
53 | /* File entry */
54 | if (READ_32(header) == 0x04034b50) {
55 | unsigned int version = READ_16(header + 4);
56 | unsigned int gpflag = READ_16(header + 6);
57 | unsigned int method = READ_16(header + 8);
58 | unsigned int filetime = READ_16(header + 10);
59 | unsigned int filedate = READ_16(header + 12);
60 | unsigned int crc = READ_32(header + 14); /* crc */
61 | unsigned int cpsize = READ_32(header + 18); /* compressed size */
62 | unsigned int uncpsize = READ_32(header + 22); /* uncompressed sz */
63 | unsigned int fnsize = READ_16(header + 26); /* file name length */
64 | unsigned int extsize = READ_16(header + 28); /* extra field length */
65 | filename[0] = extra[0] = '\0';
66 |
67 | /* Header */
68 | if (fwrite(header, 1, 30, fpOut) == 30) {
69 | offset += 30;
70 | } else {
71 | err = Z_ERRNO;
72 | break;
73 | }
74 |
75 | /* Filename */
76 | if (fnsize > 0) {
77 | if (fread(filename, 1, fnsize, fpZip) == fnsize) {
78 | if (fwrite(filename, 1, fnsize, fpOut) == fnsize) {
79 | offset += fnsize;
80 | } else {
81 | err = Z_ERRNO;
82 | break;
83 | }
84 | } else {
85 | err = Z_ERRNO;
86 | break;
87 | }
88 | } else {
89 | err = Z_STREAM_ERROR;
90 | break;
91 | }
92 |
93 | /* Extra field */
94 | if (extsize > 0) {
95 | if (fread(extra, 1, extsize, fpZip) == extsize) {
96 | if (fwrite(extra, 1, extsize, fpOut) == extsize) {
97 | offset += extsize;
98 | } else {
99 | err = Z_ERRNO;
100 | break;
101 | }
102 | } else {
103 | err = Z_ERRNO;
104 | break;
105 | }
106 | }
107 |
108 | /* Data */
109 | {
110 | int dataSize = cpsize;
111 | if (dataSize == 0) {
112 | dataSize = uncpsize;
113 | }
114 | if (dataSize > 0) {
115 | char* data = malloc(dataSize);
116 | if (data != NULL) {
117 | if ((int)fread(data, 1, dataSize, fpZip) == dataSize) {
118 | if ((int)fwrite(data, 1, dataSize, fpOut) == dataSize) {
119 | offset += dataSize;
120 | totalBytes += dataSize;
121 | } else {
122 | err = Z_ERRNO;
123 | }
124 | } else {
125 | err = Z_ERRNO;
126 | }
127 | free(data);
128 | if (err != Z_OK) {
129 | break;
130 | }
131 | } else {
132 | err = Z_MEM_ERROR;
133 | break;
134 | }
135 | }
136 | }
137 |
138 | /* Central directory entry */
139 | {
140 | char centralDirectoryEntryHeader[46];
141 | //char* comment = "";
142 | //int comsize = (int) strlen(comment);
143 | WRITE_32(centralDirectoryEntryHeader, 0x02014b50);
144 | WRITE_16(centralDirectoryEntryHeader + 4, version);
145 | WRITE_16(centralDirectoryEntryHeader + 6, version);
146 | WRITE_16(centralDirectoryEntryHeader + 8, gpflag);
147 | WRITE_16(centralDirectoryEntryHeader + 10, method);
148 | WRITE_16(centralDirectoryEntryHeader + 12, filetime);
149 | WRITE_16(centralDirectoryEntryHeader + 14, filedate);
150 | WRITE_32(centralDirectoryEntryHeader + 16, crc);
151 | WRITE_32(centralDirectoryEntryHeader + 20, cpsize);
152 | WRITE_32(centralDirectoryEntryHeader + 24, uncpsize);
153 | WRITE_16(centralDirectoryEntryHeader + 28, fnsize);
154 | WRITE_16(centralDirectoryEntryHeader + 30, extsize);
155 | WRITE_16(centralDirectoryEntryHeader + 32, 0 /*comsize*/);
156 | WRITE_16(centralDirectoryEntryHeader + 34, 0); /* disk # */
157 | WRITE_16(centralDirectoryEntryHeader + 36, 0); /* int attrb */
158 | WRITE_32(centralDirectoryEntryHeader + 38, 0); /* ext attrb */
159 | WRITE_32(centralDirectoryEntryHeader + 42, currentOffset);
160 | /* Header */
161 | if (fwrite(centralDirectoryEntryHeader, 1, 46, fpOutCD) == 46) {
162 | offsetCD += 46;
163 |
164 | /* Filename */
165 | if (fnsize > 0) {
166 | if (fwrite(filename, 1, fnsize, fpOutCD) == fnsize) {
167 | offsetCD += fnsize;
168 | } else {
169 | err = Z_ERRNO;
170 | break;
171 | }
172 | } else {
173 | err = Z_STREAM_ERROR;
174 | break;
175 | }
176 |
177 | /* Extra field */
178 | if (extsize > 0) {
179 | if (fwrite(extra, 1, extsize, fpOutCD) == extsize) {
180 | offsetCD += extsize;
181 | } else {
182 | err = Z_ERRNO;
183 | break;
184 | }
185 | }
186 |
187 | /* Comment field */
188 | /*
189 | if (comsize > 0) {
190 | if ((int)fwrite(comment, 1, comsize, fpOutCD) == comsize) {
191 | offsetCD += comsize;
192 | } else {
193 | err = Z_ERRNO;
194 | break;
195 | }
196 | }
197 | */
198 |
199 | } else {
200 | err = Z_ERRNO;
201 | break;
202 | }
203 | }
204 |
205 | /* Success */
206 | entries++;
207 |
208 | } else {
209 | break;
210 | }
211 | }
212 |
213 | /* Final central directory */
214 | {
215 | int entriesZip = entries;
216 | char finalCentralDirectoryHeader[22];
217 | //char* comment = ""; // "ZIP File recovered by zlib/minizip/mztools";
218 | //int comsize = (int) strlen(comment);
219 | if (entriesZip > 0xffff) {
220 | entriesZip = 0xffff;
221 | }
222 | WRITE_32(finalCentralDirectoryHeader, 0x06054b50);
223 | WRITE_16(finalCentralDirectoryHeader + 4, 0); /* disk # */
224 | WRITE_16(finalCentralDirectoryHeader + 6, 0); /* disk # */
225 | WRITE_16(finalCentralDirectoryHeader + 8, entriesZip); /* hack */
226 | WRITE_16(finalCentralDirectoryHeader + 10, entriesZip); /* hack */
227 | WRITE_32(finalCentralDirectoryHeader + 12, offsetCD); /* size of CD */
228 | WRITE_32(finalCentralDirectoryHeader + 16, offset); /* offset to CD */
229 | WRITE_16(finalCentralDirectoryHeader + 20, 0 /*comsize*/); /* comment */
230 |
231 | /* Header */
232 | if (fwrite(finalCentralDirectoryHeader, 1, 22, fpOutCD) == 22) {
233 |
234 | /* Comment field */
235 | /*
236 | if (comsize > 0) {
237 | if ((int)fwrite(comment, 1, comsize, fpOutCD) != comsize) {
238 | err = Z_ERRNO;
239 | }
240 | }
241 | */
242 | } else {
243 | err = Z_ERRNO;
244 | }
245 | }
246 |
247 | /* Final merge (file + central directory) */
248 | fclose(fpOutCD);
249 | if (err == Z_OK) {
250 | fpOutCD = fopen(fileOutTmp, "rb");
251 | if (fpOutCD != NULL) {
252 | int nRead;
253 | char buffer[8192];
254 | while ( (nRead = (int)fread(buffer, 1, sizeof(buffer), fpOutCD)) > 0) {
255 | if ((int)fwrite(buffer, 1, nRead, fpOut) != nRead) {
256 | err = Z_ERRNO;
257 | break;
258 | }
259 | }
260 | fclose(fpOutCD);
261 | }
262 | }
263 |
264 | /* Close */
265 | fclose(fpZip);
266 | fclose(fpOut);
267 |
268 | /* Wipe temporary file */
269 | (void)remove(fileOutTmp);
270 |
271 | /* Number of recovered entries */
272 | if (err == Z_OK) {
273 | if (nRecovered != NULL) {
274 | *nRecovered = entries;
275 | }
276 | if (bytesRecovered != NULL) {
277 | *bytesRecovered = totalBytes;
278 | }
279 | }
280 | } else {
281 | err = Z_STREAM_ERROR;
282 | }
283 | return err;
284 | }
285 |
--------------------------------------------------------------------------------
/minizip/unzip.h:
--------------------------------------------------------------------------------
1 | /* unzip.h -- IO for uncompress .zip files using zlib
2 | Version 1.1, February 14h, 2010
3 | part of the MiniZip project
4 |
5 | Copyright (C) 1998-2010 Gilles Vollant
6 | http://www.winimage.com/zLibDll/minizip.html
7 | Modifications of Unzip for Zip64
8 | Copyright (C) 2007-2008 Even Rouault
9 | Modifications for Zip64 support on both zip and unzip
10 | Copyright (C) 2009-2010 Mathias Svensson
11 | http://result42.com
12 |
13 | This program is distributed under the terms of the same license as zlib.
14 | See the accompanying LICENSE file for the full text of the license.
15 | */
16 |
17 | #include "SSZipCommon.h"
18 |
19 | #ifndef _UNZ_H
20 | #define _UNZ_H
21 |
22 | #define HAVE_AES
23 |
24 | #ifdef __cplusplus
25 | extern "C" {
26 | #endif
27 |
28 | #ifndef _ZLIB_H
29 | #include "zlib.h"
30 | #endif
31 |
32 | #ifndef _ZLIBIOAPI_H
33 | #include "ioapi.h"
34 | #endif
35 |
36 | #ifdef HAVE_BZIP2
37 | #include "bzlib.h"
38 | #endif
39 |
40 | #define Z_BZIP2ED 12
41 |
42 | #if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
43 | /* like the STRICT of WIN32, we define a pointer that cannot be converted
44 | from (void*) without cast */
45 | typedef struct TagunzFile__ { int unused; } unzFile__;
46 | typedef unzFile__ *unzFile;
47 | #else
48 | typedef voidp unzFile;
49 | #endif
50 |
51 |
52 | #define UNZ_OK (0)
53 | #define UNZ_END_OF_LIST_OF_FILE (-100)
54 | #define UNZ_ERRNO (Z_ERRNO)
55 | #define UNZ_EOF (0)
56 | #define UNZ_PARAMERROR (-102)
57 | #define UNZ_BADZIPFILE (-103)
58 | #define UNZ_INTERNALERROR (-104)
59 | #define UNZ_CRCERROR (-105)
60 |
61 |
62 | /***************************************************************************/
63 | /* Opening and close a zip file */
64 |
65 | extern unzFile ZEXPORT unzOpen OF((const char *path));
66 | extern unzFile ZEXPORT unzOpen64 OF((const void *path));
67 | /* Open a Zip file.
68 |
69 | path should contain the full pathname (by example, on a Windows XP computer
70 | "c:\\zlib\\zlib113.zip" or on an Unix computer "zlib/zlib113.zip".
71 | return NULL if zipfile cannot be opened or doesn't exist
72 | return unzFile handle if no error
73 |
74 | NOTE: The "64" function take a const void* pointer, because the path is just the value passed to the
75 | open64_file_func callback. Under Windows, if UNICODE is defined, using fill_fopen64_filefunc, the path
76 | is a pointer to a wide unicode string (LPCTSTR is LPCWSTR), so const char* does not describe the reality */
77 |
78 | extern unzFile ZEXPORT unzOpen2 OF((const char *path, zlib_filefunc_def* pzlib_filefunc_def));
79 | /* Open a Zip file, like unzOpen, but provide a set of file low level API for read/write operations */
80 | extern unzFile ZEXPORT unzOpen2_64 OF((const void *path, zlib_filefunc64_def* pzlib_filefunc_def));
81 | /* Open a Zip file, like unz64Open, but provide a set of file low level API for read/write 64-bit operations */
82 |
83 | extern int ZEXPORT unzClose OF((unzFile file));
84 | /* Close a ZipFile opened with unzipOpen. If there is files inside the .Zip opened with unzOpenCurrentFile,
85 | these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
86 |
87 | return UNZ_OK if there is no error */
88 |
89 | extern int ZEXPORT unzGetGlobalInfo OF((unzFile file, unz_global_info *pglobal_info));
90 | extern int ZEXPORT unzGetGlobalInfo64 OF((unzFile file, unz_global_info64 *pglobal_info));
91 | /* Write info about the ZipFile in the *pglobal_info structure.
92 |
93 | return UNZ_OK if no error */
94 |
95 | extern int ZEXPORT unzGetGlobalComment OF((unzFile file, char *comment, uLong comment_size));
96 | /* Get the global comment string of the ZipFile, in the comment buffer.
97 |
98 | uSizeBuf is the size of the szComment buffer.
99 | return the number of byte copied or an error code <0 */
100 |
101 | /***************************************************************************/
102 | /* Reading the content of the current zipfile, you can open it, read data from it, and close it
103 | (you can close it before reading all the file) */
104 |
105 | extern int ZEXPORT unzOpenCurrentFile OF((unzFile file));
106 | /* Open for reading data the current file in the zipfile.
107 |
108 | return UNZ_OK if no error */
109 |
110 | extern int ZEXPORT unzOpenCurrentFilePassword OF((unzFile file, const char* password));
111 | /* Open for reading data the current file in the zipfile.
112 | password is a crypting password
113 |
114 | return UNZ_OK if no error */
115 |
116 | extern int ZEXPORT unzOpenCurrentFile2 OF((unzFile file, int* method, int* level, int raw));
117 | /* Same as unzOpenCurrentFile, but open for read raw the file (not uncompress)
118 | if raw==1 *method will receive method of compression, *level will receive level of compression
119 |
120 | NOTE: you can set level parameter as NULL (if you did not want known level,
121 | but you CANNOT set method parameter as NULL */
122 |
123 | extern int ZEXPORT unzOpenCurrentFile3 OF((unzFile file, int* method, int* level, int raw, const char* password));
124 | /* Same as unzOpenCurrentFile, but takes extra parameter password for encrypted files */
125 |
126 | extern int ZEXPORT unzReadCurrentFile OF((unzFile file, voidp buf, unsigned len));
127 | /* Read bytes from the current file (opened by unzOpenCurrentFile)
128 | buf contain buffer where data must be copied
129 | len the size of buf.
130 |
131 | return the number of byte copied if somes bytes are copied
132 | return 0 if the end of file was reached
133 | return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
134 |
135 | extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file, unz_file_info *pfile_info, char *filename,
136 | uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size));
137 | extern int ZEXPORT unzGetCurrentFileInfo64 OF((unzFile file, unz_file_info64 *pfile_info, char *filename,
138 | uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size));
139 | /* Get Info about the current file
140 |
141 | pfile_info if != NULL, the *pfile_info structure will contain somes info about the current file
142 | filename if != NULL, the file name string will be copied in filename
143 | filename_size is the size of the filename buffer
144 | extrafield if != NULL, the extra field information from the central header will be copied in to
145 | extrafield_size is the size of the extraField buffer
146 | comment if != NULL, the comment string of the file will be copied in to
147 | comment_size is the size of the comment buffer */
148 |
149 | extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64 OF((unzFile file));
150 |
151 | extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file, voidp buf, unsigned len));
152 | /* Read extra field from the current file (opened by unzOpenCurrentFile)
153 | This is the local-header version of the extra field (sometimes, there is
154 | more info in the local-header version than in the central-header)
155 |
156 | if buf == NULL, it return the size of the local extra field
157 | if buf != NULL, len is the size of the buffer, the extra header is copied in buf.
158 |
159 | return number of bytes copied in buf, or (if <0) the error code */
160 |
161 | extern int ZEXPORT unzCloseCurrentFile OF((unzFile file));
162 | /* Close the file in zip opened with unzOpenCurrentFile
163 |
164 | return UNZ_CRCERROR if all the file was read but the CRC is not good */
165 |
166 | /***************************************************************************/
167 | /* Browse the directory of the zipfile */
168 |
169 | typedef int (*unzFileNameComparer)(unzFile file, const char *filename1, const char *filename2);
170 | typedef int (*unzIteratorFunction)(unzFile file);
171 | typedef int (*unzIteratorFunction2)(unzFile file, unz_file_info64 *pfile_info, char *filename,
172 | uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size);
173 |
174 | extern int ZEXPORT unzGoToFirstFile OF((unzFile file));
175 | /* Set the current file of the zipfile to the first file.
176 |
177 | return UNZ_OK if no error */
178 |
179 | extern int ZEXPORT unzGoToFirstFile2 OF((unzFile file, unz_file_info64 *pfile_info, char *filename,
180 | uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size));
181 | /* Set the current file of the zipfile to the first file and retrieves the current info on success.
182 | Not as seek intensive as unzGoToFirstFile + unzGetCurrentFileInfo.
183 |
184 | return UNZ_OK if no error */
185 |
186 | extern int ZEXPORT unzGoToNextFile OF((unzFile file));
187 | /* Set the current file of the zipfile to the next file.
188 |
189 | return UNZ_OK if no error
190 | return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
191 |
192 | extern int ZEXPORT unzGoToNextFile2 OF((unzFile file, unz_file_info64 *pfile_info, char *filename,
193 | uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size));
194 | /* Set the current file of the zipfile to the next file and retrieves the current
195 | info on success. Does less seeking around than unzGotoNextFile + unzGetCurrentFileInfo.
196 |
197 | return UNZ_OK if no error
198 | return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest */
199 |
200 | extern int ZEXPORT unzLocateFile OF((unzFile file, const char *filename, unzFileNameComparer filename_compare_func));
201 | /* Try locate the file szFileName in the zipfile. For custom filename comparison pass in comparison function.
202 |
203 | return UNZ_OK if the file is found (it becomes the current file)
204 | return UNZ_END_OF_LIST_OF_FILE if the file is not found */
205 |
206 | /***************************************************************************/
207 | /* Raw access to zip file */
208 |
209 | typedef struct unz_file_pos_s
210 | {
211 | uLong pos_in_zip_directory; /* offset in zip file directory */
212 | uLong num_of_file; /* # of file */
213 | } unz_file_pos;
214 |
215 | extern int ZEXPORT unzGetFilePos OF((unzFile file, unz_file_pos* file_pos));
216 | extern int ZEXPORT unzGoToFilePos OF((unzFile file, unz_file_pos* file_pos));
217 |
218 | typedef struct unz64_file_pos_s
219 | {
220 | ZPOS64_T pos_in_zip_directory; /* offset in zip file directory */
221 | ZPOS64_T num_of_file; /* # of file */
222 | } unz64_file_pos;
223 |
224 | extern int ZEXPORT unzGetFilePos64 OF((unzFile file, unz64_file_pos* file_pos));
225 | extern int ZEXPORT unzGoToFilePos64 OF((unzFile file, const unz64_file_pos* file_pos));
226 |
227 | extern uLong ZEXPORT unzGetOffset OF((unzFile file));
228 | extern ZPOS64_T ZEXPORT unzGetOffset64 OF((unzFile file));
229 | /* Get the current file offset */
230 |
231 | extern int ZEXPORT unzSetOffset OF((unzFile file, uLong pos));
232 | extern int ZEXPORT unzSetOffset64 OF((unzFile file, ZPOS64_T pos));
233 | /* Set the current file offset */
234 |
235 | extern z_off_t ZEXPORT unztell OF((unzFile file));
236 | extern ZPOS64_T ZEXPORT unztell64 OF((unzFile file));
237 | /* return current position in uncompressed data */
238 |
239 | extern int ZEXPORT unzeof OF((unzFile file));
240 | /* return 1 if the end of file was reached, 0 elsewhere */
241 |
242 | /***************************************************************************/
243 |
244 | #ifdef __cplusplus
245 | }
246 | #endif
247 |
248 | #endif /* _UNZ_H */
249 |
--------------------------------------------------------------------------------
/minizip/zip.h:
--------------------------------------------------------------------------------
1 | /* zip.h -- IO on .zip files using zlib
2 | Version 1.1, February 14h, 2010
3 | part of the MiniZip project
4 |
5 | Copyright (C) 1998-2010 Gilles Vollant
6 | http://www.winimage.com/zLibDll/minizip.html
7 | Modifications for Zip64 support
8 | Copyright (C) 2009-2010 Mathias Svensson
9 | http://result42.com
10 |
11 | This program is distributed under the terms of the same license as zlib.
12 | See the accompanying LICENSE file for the full text of the license.
13 | */
14 |
15 | #ifndef _ZIP_H
16 | #define _ZIP_H
17 |
18 | #define HAVE_AES
19 |
20 | #ifdef __cplusplus
21 | extern "C" {
22 | #endif
23 |
24 | #ifndef _ZLIB_H
25 | # include "zlib.h"
26 | #endif
27 |
28 | #ifndef _ZLIBIOAPI_H
29 | # include "ioapi.h"
30 | #endif
31 |
32 | #ifdef HAVE_BZIP2
33 | # include "bzlib.h"
34 | #endif
35 |
36 | #define Z_BZIP2ED 12
37 |
38 | #if defined(STRICTZIP) || defined(STRICTZIPUNZIP)
39 | /* like the STRICT of WIN32, we define a pointer that cannot be converted
40 | from (void*) without cast */
41 | typedef struct TagzipFile__ { int unused; } zipFile__;
42 | typedef zipFile__ *zipFile;
43 | #else
44 | typedef voidp zipFile;
45 | #endif
46 |
47 | #define ZIP_OK (0)
48 | #define ZIP_EOF (0)
49 | #define ZIP_ERRNO (Z_ERRNO)
50 | #define ZIP_PARAMERROR (-102)
51 | #define ZIP_BADZIPFILE (-103)
52 | #define ZIP_INTERNALERROR (-104)
53 |
54 | #ifndef DEF_MEM_LEVEL
55 | # if MAX_MEM_LEVEL >= 8
56 | # define DEF_MEM_LEVEL 8
57 | # else
58 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL
59 | # endif
60 | #endif
61 | /* default memLevel */
62 |
63 | /* tm_zip contain date/time info */
64 | typedef struct tm_zip_s
65 | {
66 | uInt tm_sec; /* seconds after the minute - [0,59] */
67 | uInt tm_min; /* minutes after the hour - [0,59] */
68 | uInt tm_hour; /* hours since midnight - [0,23] */
69 | uInt tm_mday; /* day of the month - [1,31] */
70 | uInt tm_mon; /* months since January - [0,11] */
71 | uInt tm_year; /* years - [1980..2044] */
72 | } tm_zip;
73 |
74 | typedef struct
75 | {
76 | tm_zip tmz_date; /* date in understandable format */
77 | uLong dosDate; /* if dos_date == 0, tmu_date is used */
78 | uLong internal_fa; /* internal file attributes 2 bytes */
79 | uLong external_fa; /* external file attributes 4 bytes */
80 | } zip_fileinfo;
81 |
82 | typedef const char* zipcharpc;
83 |
84 | #define APPEND_STATUS_CREATE (0)
85 | #define APPEND_STATUS_CREATEAFTER (1)
86 | #define APPEND_STATUS_ADDINZIP (2)
87 |
88 | /***************************************************************************/
89 | /* Writing a zip file */
90 |
91 | extern zipFile ZEXPORT zipOpen OF((const char *pathname, int append));
92 | extern zipFile ZEXPORT zipOpen64 OF((const void *pathname, int append));
93 | /* Create a zipfile.
94 |
95 | pathname should contain the full pathname (by example, on a Windows XP computer
96 | "c:\\zlib\\zlib113.zip" or on an Unix computer "zlib/zlib113.zip".
97 |
98 | return NULL if zipfile cannot be opened
99 | return zipFile handle if no error
100 |
101 | If the file pathname exist and append == APPEND_STATUS_CREATEAFTER, the zip
102 | will be created at the end of the file. (useful if the file contain a self extractor code)
103 | If the file pathname exist and append == APPEND_STATUS_ADDINZIP, we will add files in existing
104 | zip (be sure you don't add file that doesn't exist)
105 |
106 | NOTE: There is no delete function into a zipfile. If you want delete file into a zipfile,
107 | you must open a zipfile, and create another. Of course, you can use RAW reading and writing to copy
108 | the file you did not want delete. */
109 |
110 | extern zipFile ZEXPORT zipOpen2 OF((const char *pathname, int append, zipcharpc* globalcomment,
111 | zlib_filefunc_def* pzlib_filefunc_def));
112 |
113 | extern zipFile ZEXPORT zipOpen2_64 OF((const void *pathname, int append, zipcharpc* globalcomment,
114 | zlib_filefunc64_def* pzlib_filefunc_def));
115 |
116 | extern zipFile ZEXPORT zipOpen3 OF((const char *pathname, int append, ZPOS64_T disk_size,
117 | zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc_def));
118 | /* Same as zipOpen2 but allows specification of spanned zip size */
119 |
120 | extern zipFile ZEXPORT zipOpen3_64 OF((const void *pathname, int append, ZPOS64_T disk_size,
121 | zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def));
122 |
123 | extern int ZEXPORT zipOpenNewFileInZip OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
124 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
125 | uInt size_extrafield_global, const char* comment, int method, int level));
126 | /* Open a file in the ZIP for writing.
127 |
128 | filename : the filename in zip (if NULL, '-' without quote will be used
129 | *zipfi contain supplemental information
130 | extrafield_local buffer to store the local header extra field data, can be NULL
131 | size_extrafield_local size of extrafield_local buffer
132 | extrafield_global buffer to store the global header extra field data, can be NULL
133 | size_extrafield_global size of extrafield_local buffer
134 | comment buffer for comment string
135 | method contain the compression method (0 for store, Z_DEFLATED for deflate)
136 | level contain the level of compression (can be Z_DEFAULT_COMPRESSION)
137 | zip64 is set to 1 if a zip64 extended information block should be added to the local file header.
138 | this MUST be '1' if the uncompressed size is >= 0xffffffff. */
139 |
140 | extern int ZEXPORT zipOpenNewFileInZip64 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
141 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
142 | uInt size_extrafield_global, const char* comment, int method, int level, int zip64));
143 | /* Same as zipOpenNewFileInZip with zip64 support */
144 |
145 | extern int ZEXPORT zipOpenNewFileInZip2 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
146 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
147 | uInt size_extrafield_global, const char* comment, int method, int level, int raw));
148 | /* Same as zipOpenNewFileInZip, except if raw=1, we write raw file */
149 |
150 | extern int ZEXPORT zipOpenNewFileInZip2_64 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
151 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
152 | uInt size_extrafield_global, const char* comment, int method, int level, int raw, int zip64));
153 | /* Same as zipOpenNewFileInZip3 with zip64 support */
154 |
155 | extern int ZEXPORT zipOpenNewFileInZip3 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
156 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
157 | uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, int memLevel,
158 | int strategy, const char* password, uLong crcForCrypting));
159 | /* Same as zipOpenNewFileInZip2, except
160 | windowBits, memLevel, strategy : see parameter strategy in deflateInit2
161 | password : crypting password (NULL for no crypting)
162 | crcForCrypting : crc of file to compress (needed for crypting) */
163 |
164 | extern int ZEXPORT zipOpenNewFileInZip3_64 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
165 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
166 | uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, int memLevel,
167 | int strategy, const char* password, uLong crcForCrypting, int zip64));
168 | /* Same as zipOpenNewFileInZip3 with zip64 support */
169 |
170 | extern int ZEXPORT zipOpenNewFileInZip4 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
171 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
172 | uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, int memLevel,
173 | int strategy, const char* password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase));
174 | /* Same as zipOpenNewFileInZip3 except versionMadeBy & flag fields */
175 |
176 | extern int ZEXPORT zipOpenNewFileInZip4_64 OF((zipFile file, const char* filename, const zip_fileinfo* zipfi,
177 | const void* extrafield_local, uInt size_extrafield_local, const void* extrafield_global,
178 | uInt size_extrafield_global, const char* comment, int method, int level, int raw, int windowBits, int memLevel,
179 | int strategy, const char* password, uLong crcForCrypting, uLong versionMadeBy, uLong flagBase, int zip64));
180 | /* Same as zipOpenNewFileInZip4 with zip64 support */
181 |
182 | extern int ZEXPORT zipWriteInFileInZip OF((zipFile file, const void* buf, unsigned len));
183 | /* Write data in the zipfile */
184 |
185 | extern int ZEXPORT zipCloseFileInZip OF((zipFile file));
186 | /* Close the current file in the zipfile */
187 |
188 | extern int ZEXPORT zipCloseFileInZipRaw OF((zipFile file, uLong uncompressed_size, uLong crc32));
189 | extern int ZEXPORT zipCloseFileInZipRaw64 OF((zipFile file, ZPOS64_T uncompressed_size, uLong crc32));
190 | /* Close the current file in the zipfile, for file opened with parameter raw=1 in zipOpenNewFileInZip2
191 | uncompressed_size and crc32 are value for the uncompressed size */
192 |
193 | extern int ZEXPORT zipClose OF((zipFile file, const char* global_comment));
194 | /* Close the zipfile */
195 |
196 | /***************************************************************************/
197 |
198 | #ifdef __cplusplus
199 | }
200 | #endif
201 |
202 | #endif /* _ZIP_H */
203 |
--------------------------------------------------------------------------------