Skip to content

Commit 279192e

Browse files
committed
Updating libtomcrypt to the latest develop branch (for ARM Windows)
1 parent adb2f6b commit 279192e

21 files changed

+221
-121
lines changed

ext/crypto/libtomcrypt/src/ciphers/aes/aes.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static ulong32 setup_mix2(ulong32 temp)
9696
int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
9797
{
9898
int i;
99-
ulong32 temp, *rk;
99+
ulong32 temp, *rk, *K;
100100
#ifndef ENCRYPT_ONLY
101101
ulong32 *rrk;
102102
#endif
@@ -112,6 +112,10 @@ int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *s
112112
}
113113

114114
skey->rijndael.Nr = 10 + ((keylen/8)-2)*2;
115+
K = LTC_ALIGN_BUF(skey->rijndael.K, 16);
116+
skey->rijndael.eK = K;
117+
K += 60;
118+
skey->rijndael.dK = K;
115119

116120
/* setup the forward key */
117121
i = 0;

ext/crypto/libtomcrypt/src/ciphers/aes/aesni.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
4646
{
4747
int i;
4848
__m128i temp;
49-
ulong32 *rk;
49+
ulong32 *rk, *K;
5050
ulong32 *rrk;
5151
LTC_ARGCHK(key != NULL);
5252
LTC_ARGCHK(skey != NULL);
@@ -60,6 +60,10 @@ int aesni_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
6060
}
6161

6262
skey->rijndael.Nr = keylen / 4 + 6;
63+
K = LTC_ALIGN_BUF(skey->rijndael.K, 16);
64+
skey->rijndael.eK = K;
65+
K += 60;
66+
skey->rijndael.dK = K;
6367

6468
/* setup the forward key */
6569
i = 0;

ext/crypto/libtomcrypt/src/encauth/ccm/ccm_memory.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ int ccm_memory(int cipher,
161161
PAD[x++] = 0;
162162
}
163163
for (; y < L; y++) {
164+
if (x >= sizeof(PAD)) {
165+
return CRYPT_INVALID_ARG;
166+
}
164167
PAD[x++] = (unsigned char)((len >> 24) & 255);
165168
len <<= 8;
166169
}

ext/crypto/libtomcrypt/src/encauth/gcm/gcm_memory.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ int gcm_memory( int cipher,
7070
* but again it's only for SSE2 anyways, so who cares?
7171
*/
7272
#ifdef LTC_GCM_TABLES_SSE2
73-
if ((unsigned long)gcm & 15) {
74-
gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15)));
75-
}
73+
gcm = LTC_ALIGN_BUF(gcm, 16);
7674
#endif
7775

7876
if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {

ext/crypto/libtomcrypt/src/headers/tomcrypt_cfg.h

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
7979
* The x86 platforms allow this but some others [ARM for instance] do not. On those platforms you **MUST**
8080
* use the portable [slower] macros.
8181
*/
82-
/* detect x86/i386 32bit */
83-
#if defined(__i386__) || defined(__i386) || defined(_M_IX86)
82+
/* detect x86/i386/ARM 32bit */
83+
#if defined(__i386__) || defined(__i386) || defined(_M_IX86) || defined(_M_ARM)
8484
#define ENDIAN_LITTLE
8585
#define ENDIAN_32BITWORD
8686
#define LTC_FAST
8787
#endif
8888

89-
/* detect amd64/x64 */
90-
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64)
89+
/* detect amd64/x64/arm64 */
90+
#if defined(__x86_64__) || defined(_M_X64) || defined(_M_AMD64) || defined(_M_ARM64)
9191
#define ENDIAN_LITTLE
9292
#define ENDIAN_64BITWORD
9393
#define LTC_FAST
@@ -195,7 +195,8 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
195195
defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ || \
196196
defined(__LITTLE_ENDIAN__) || \
197197
defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
198-
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
198+
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
199+
defined(_M_ARM) || defined(_M_ARM64)
199200
#define ENDIAN_LITTLE
200201
#else
201202
#error Cannot detect endianness
@@ -208,7 +209,7 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
208209
typedef unsigned __int64 ulong64;
209210
typedef __int64 long64;
210211
#else
211-
#define CONST64(n) n ## ULL
212+
#define CONST64(n) n ## uLL
212213
typedef unsigned long long ulong64;
213214
typedef long long long64;
214215
#endif
@@ -219,7 +220,7 @@ LTC_EXPORT int LTC_CALL XSTRCMP(const char *s1, const char *s2);
219220
defined(__s390x__) || defined(__arch64__) || defined(__aarch64__) || \
220221
defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) || \
221222
defined(__ia64) || defined(__ia64__) || defined(__itanium__) || defined(_M_IA64) || \
222-
defined(__LP64__) || defined(_LP64) || defined(__64BIT__)
223+
defined(__LP64__) || defined(_LP64) || defined(__64BIT__) || defined(_M_ARM64)
223224
typedef unsigned ulong32;
224225
#if !defined(ENDIAN_64BITWORD) && !defined(ENDIAN_32BITWORD)
225226
#define ENDIAN_64BITWORD
@@ -300,6 +301,21 @@ typedef unsigned long ltc_mp_digit;
300301
#define LTC_ALIGN(n)
301302
#endif
302303

304+
/* Choose Windows Vista as minimum Version if we're compiling with at least VS2019
305+
* This is done in order to test the bcrypt RNG and can still be overridden by the user. */
306+
#if defined(_MSC_VER) && _MSC_VER >= 1920
307+
# ifndef _WIN32_WINNT
308+
# define _WIN32_WINNT 0x0600
309+
# endif
310+
# ifndef WINVER
311+
# define WINVER 0x0600
312+
# endif
313+
#endif
314+
315+
#if defined(_MSC_VER) && defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0600 && !defined(LTC_WIN32_BCRYPT)
316+
# define LTC_WIN32_BCRYPT
317+
#endif
318+
303319
/* Define `LTC_NO_NULL_TERMINATION_CHECK` in the user code
304320
* before including `tomcrypt.h` to disable this functionality.
305321
*/

ext/crypto/libtomcrypt/src/headers/tomcrypt_cipher.h

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ struct saferp_key {
3535

3636
#ifdef LTC_RIJNDAEL
3737
struct rijndael_key {
38-
ulong32 eK[60] LTC_ALIGN(16);
39-
ulong32 dK[60] LTC_ALIGN(16);
38+
unsigned char K[(60 + 60 + 4) * sizeof(ulong32)];
39+
ulong32 *eK;
40+
ulong32 *dK;
4041
int Nr;
4142
};
4243
#endif
@@ -128,24 +129,24 @@ struct khazad_key {
128129

129130
#ifdef LTC_ANUBIS
130131
struct anubis_key {
131-
int keyBits;
132-
int R;
133132
ulong32 roundKeyEnc[18 + 1][4];
134133
ulong32 roundKeyDec[18 + 1][4];
134+
int keyBits;
135+
int R;
135136
};
136137
#endif
137138

138139
#ifdef LTC_MULTI2
139140
struct multi2_key {
140-
int N;
141141
ulong32 uk[8];
142+
int N;
142143
};
143144
#endif
144145

145146
#ifdef LTC_CAMELLIA
146147
struct camellia_key {
147-
int R;
148148
ulong64 kw[4], k[24], kl[6];
149+
int R;
149150
};
150151
#endif
151152

@@ -246,67 +247,74 @@ typedef union Symmetric_key {
246247
#ifdef LTC_ECB_MODE
247248
/** A block cipher ECB structure */
248249
typedef struct {
250+
/** The scheduled key */
251+
symmetric_key key;
249252
/** The index of the cipher chosen */
250253
int cipher,
251254
/** The block size of the given cipher */
252255
blocklen;
253-
/** The scheduled key */
254-
symmetric_key key;
255256
} symmetric_ECB;
256257
#endif
257258

258259
#ifdef LTC_CFB_MODE
259260
/** A block cipher CFB structure */
260261
typedef struct {
261-
/** The index of the cipher chosen */
262-
int cipher,
263-
/** The block size of the given cipher */
264-
blocklen,
265-
/** The padding offset */
266-
padlen;
267262
/** The current IV */
268263
unsigned char IV[MAXBLOCKSIZE],
269264
/** The pad used to encrypt/decrypt */
270265
pad[MAXBLOCKSIZE];
271266
/** The scheduled key */
272267
symmetric_key key;
268+
/** The index of the cipher chosen */
269+
int cipher,
270+
/** The block size of the given cipher */
271+
blocklen,
272+
/** The padding offset */
273+
padlen;
273274
} symmetric_CFB;
274275
#endif
275276

276277
#ifdef LTC_OFB_MODE
277278
/** A block cipher OFB structure */
278279
typedef struct {
280+
/** The current IV */
281+
unsigned char IV[MAXBLOCKSIZE];
282+
/** The scheduled key */
283+
symmetric_key key;
279284
/** The index of the cipher chosen */
280285
int cipher,
281286
/** The block size of the given cipher */
282287
blocklen,
283288
/** The padding offset */
284289
padlen;
285-
/** The current IV */
286-
unsigned char IV[MAXBLOCKSIZE];
287-
/** The scheduled key */
288-
symmetric_key key;
289290
} symmetric_OFB;
290291
#endif
291292

292293
#ifdef LTC_CBC_MODE
293294
/** A block cipher CBC structure */
294295
typedef struct {
295-
/** The index of the cipher chosen */
296-
int cipher,
297-
/** The block size of the given cipher */
298-
blocklen;
299296
/** The current IV */
300297
unsigned char IV[MAXBLOCKSIZE];
301298
/** The scheduled key */
302299
symmetric_key key;
300+
/** The index of the cipher chosen */
301+
int cipher,
302+
/** The block size of the given cipher */
303+
blocklen;
303304
} symmetric_CBC;
304305
#endif
305306

306307

307308
#ifdef LTC_CTR_MODE
308309
/** A block cipher CTR structure */
309310
typedef struct {
311+
/** The counter */
312+
unsigned char ctr[MAXBLOCKSIZE];
313+
/** The pad used to encrypt/decrypt */
314+
unsigned char pad[MAXBLOCKSIZE];
315+
/** The scheduled key */
316+
symmetric_key key;
317+
310318
/** The index of the cipher chosen */
311319
int cipher,
312320
/** The block size of the given cipher */
@@ -317,23 +325,13 @@ typedef struct {
317325
mode,
318326
/** counter width */
319327
ctrlen;
320-
321-
/** The counter */
322-
unsigned char ctr[MAXBLOCKSIZE];
323-
/** The pad used to encrypt/decrypt */
324-
unsigned char pad[MAXBLOCKSIZE] LTC_ALIGN(16);
325-
/** The scheduled key */
326-
symmetric_key key;
327328
} symmetric_CTR;
328329
#endif
329330

330331

331332
#ifdef LTC_LRW_MODE
332333
/** A LRW structure */
333334
typedef struct {
334-
/** The index of the cipher chosen (must be a 128-bit block cipher) */
335-
int cipher;
336-
337335
/** The current IV */
338336
unsigned char IV[16],
339337

@@ -350,25 +348,28 @@ typedef struct {
350348
/** The pre-computed multiplication table */
351349
unsigned char PC[16][256][16];
352350
#endif
351+
352+
/** The index of the cipher chosen (must be a 128-bit block cipher) */
353+
int cipher;
353354
} symmetric_LRW;
354355
#endif
355356

356357
#ifdef LTC_F8_MODE
357358
/** A block cipher F8 structure */
358359
typedef struct {
360+
/** The current IV */
361+
unsigned char IV[MAXBLOCKSIZE],
362+
MIV[MAXBLOCKSIZE];
363+
/** The scheduled key */
364+
symmetric_key key;
359365
/** The index of the cipher chosen */
360366
int cipher,
361367
/** The block size of the given cipher */
362368
blocklen,
363369
/** The padding offset */
364370
padlen;
365-
/** The current IV */
366-
unsigned char IV[MAXBLOCKSIZE],
367-
MIV[MAXBLOCKSIZE];
368371
/** Current block count */
369372
ulong32 blockcnt;
370-
/** The scheduled key */
371-
symmetric_key key;
372373
} symmetric_F8;
373374
#endif
374375

ext/crypto/libtomcrypt/src/headers/tomcrypt_custom.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@
179179
#define LTC_RC6
180180
#define LTC_SAFERP
181181
#define LTC_RIJNDAEL
182-
#define LTC_AES_NI
182+
#ifndef LTC_NO_AES_NI
183+
#define LTC_AES_NI
184+
#endif
183185
#define LTC_XTEA
184186
/* _TABLES tells it to use tables during setup, _SMALL means to use the smaller scheduled key format
185187
* (saves 4KB of ram), _ALL_TABLES enables all tables during setup */
@@ -333,11 +335,14 @@
333335
/* Greg's SOBER128 stream cipher based PRNG */
334336
#define LTC_SOBER128
335337

338+
#if !defined(_WIN32) && !defined(_WIN32_WCE)
336339
/* the *nix style /dev/random device */
337340
#define LTC_DEVRANDOM
338341
/* try /dev/urandom before trying /dev/random
339342
* are you sure you want to disable this? http://www.2uo.de/myths-about-urandom/ */
340343
#define LTC_TRY_URANDOM_FIRST
344+
#endif /* not Windows */
345+
341346
/* rng_get_bytes() */
342347
#define LTC_RNG_GET_BYTES
343348
/* rng_make_prng() */

0 commit comments

Comments
 (0)