VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Crypto/Twofish.h
blob: cec99c7c48bef03726fe444cfc1ee9fcfc29a92d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef TWOFISH_H
#define TWOFISH_H

#include "Common/Tcdefs.h"
#include "config.h"

#if defined(__cplusplus)
extern "C"
{
#endif

#ifndef u4byte
#define u4byte	unsigned __int32
#endif
#ifndef u1byte
#define u1byte	unsigned char
#endif

#ifndef extract_byte
#define extract_byte(x,n)   ((u1byte)((x) >> (8 * n)))
#endif

#ifndef rotl

#ifdef _WIN32
#include <stdlib.h>
#pragma intrinsic(_lrotr,_lrotl)
#define rotr(x,n) _lrotr(x,n)
#define rotl(x,n) _lrotl(x,n)
#else
#define rotr(x,n) (((x)>>(n))|((x)<<(32-(n))))
#define rotl(x,n) (((x)<<(n))|((x)>>(32-(n))))
#endif

#endif
typedef struct
{
#if CRYPTOPP_BOOL_X64
   u4byte mk_tab[4][256], w[8], k[32];
#else
	u4byte l_key[40];
#ifdef TC_MINIMIZE_CODE_SIZE
	u4byte s_key[4];
#ifdef TC_WINDOWS_BOOT_TWOFISH
	u4byte mk_tab[4 * 256];
#endif
#else
   u4byte mk_tab[4][256];
#endif
#endif
} TwofishInstance;

#define TWOFISH_KS		sizeof(TwofishInstance)

/* in_key must be 32-bytes long */
void twofish_set_key(TwofishInstance *instance, const u4byte in_key[]);
#if CRYPTOPP_BOOL_X64
void twofish_encrypt_blocks(TwofishInstance *instance, const byte* in_blk, byte* out_blk, uint32 blockCount);
void twofish_decrypt_blocks(TwofishInstance *instance, const byte* in_blk, byte* out_blk, uint32 blockCount);
#define twofish_encrypt(instance,in_blk,out_blk)   twofish_encrypt_blocks(instance, (const byte*) in_blk, (byte*) out_blk, 1)
#define twofish_decrypt(instance,in_blk,out_blk)   twofish_decrypt_blocks(instance, (const byte*) in_blk, (byte*) out_blk, 1)
#else
void twofish_encrypt(TwofishInstance *instance, const u4byte in_blk[4], u4byte out_blk[4]);
void twofish_decrypt(TwofishInstance *instance, const u4byte in_blk[4], u4byte out_blk[4]);
#endif

#if defined(__cplusplus)
}
#endif

#endif // TWOFISH_H
f">"zipint.h" zip_source_t * _zip_source_zip_new(zip_t *za, zip_t *srcza, zip_uint64_t srcidx, zip_flags_t flags, zip_uint64_t start, zip_uint64_t len, const char *password) { zip_source_t *src, *s2; struct zip_stat st; bool partial_data, needs_crc, needs_decrypt, needs_decompress; if (za == NULL) return NULL; if (srcza == NULL || srcidx >= srcza->nentry) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return NULL; } if ((flags & ZIP_FL_UNCHANGED) == 0 && (ZIP_ENTRY_DATA_CHANGED(srcza->entry + srcidx) || srcza->entry[srcidx].deleted)) { zip_error_set(&za->error, ZIP_ER_CHANGED, 0); return NULL; } if (zip_stat_index(srcza, srcidx, flags | ZIP_FL_UNCHANGED, &st) < 0) { zip_error_set(&za->error, ZIP_ER_INTERNAL, 0); return NULL; } if (flags & ZIP_FL_ENCRYPTED) flags |= ZIP_FL_COMPRESSED; if ((start > 0 || len > 0) && (flags & ZIP_FL_COMPRESSED)) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return NULL; } /* overflow or past end of file */ if ((start > 0 || len > 0) && (start + len < start || start + len > st.size)) { zip_error_set(&za->error, ZIP_ER_INVAL, 0); return NULL; } if (len == 0) { len = st.size - start; } partial_data = len < st.size; needs_decrypt = ((flags & ZIP_FL_ENCRYPTED) == 0) && (st.encryption_method != ZIP_EM_NONE); needs_decompress = ((flags & ZIP_FL_COMPRESSED) == 0) && (st.comp_method != ZIP_CM_STORE); /* when reading the whole file, check for CRC errors */ needs_crc = ((flags & ZIP_FL_COMPRESSED) == 0 || st.comp_method == ZIP_CM_STORE) && !partial_data; if (needs_decrypt) { if (password == NULL) { password = za->default_password; } if (password == NULL) { zip_error_set(&za->error, ZIP_ER_NOPASSWD, 0); return NULL; } } if (st.comp_size == 0) { return zip_source_buffer(za, NULL, 0, 0); } if (partial_data && !needs_decrypt && !needs_decompress) { struct zip_stat st2; st2.size = len; st2.comp_size = len; st2.comp_method = ZIP_CM_STORE; st2.mtime = st.mtime; st2.valid = ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_MTIME; if ((src = _zip_source_window_new(srcza->src, start, len, &st2, 0, srcza, srcidx, &za->error)) == NULL) { return NULL; } } else { zip_dirent_t *de; if ((de = _zip_get_dirent(srcza, srcidx, flags, &za->error)) == NULL) { return NULL; } if ((src = _zip_source_window_new(srcza->src, 0, st.comp_size, &st, (de->bitflags >> 1) & 3, srcza, srcidx, &za->error)) == NULL) { return NULL; } } if (_zip_source_set_source_archive(src, srcza) < 0) { zip_source_free(src); return NULL; } /* creating a layered source calls zip_keep() on the lower layer, so we free it */ if (needs_decrypt) { zip_encryption_implementation enc_impl; if ((enc_impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) { zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0); return NULL; } s2 = enc_impl(za, src, st.encryption_method, 0, password); zip_source_free(src); if (s2 == NULL) { return NULL; } src = s2; } if (needs_decompress) { s2 = zip_source_decompress(za, src, st.comp_method); zip_source_free(src); if (s2 == NULL) { return NULL; } src = s2; } if (needs_crc) { s2 = zip_source_crc(za, src, 1); zip_source_free(src); if (s2 == NULL) { return NULL; } src = s2; } if (partial_data && (needs_decrypt || needs_decompress)) { s2 = zip_source_window(za, src, start, len); zip_source_free(src); if (s2 == NULL) { return NULL; } src = s2; } return src; }