Merge branch 'develop'

This commit is contained in:
2025-10-24 19:35:52 +03:00
159 changed files with 4272 additions and 2564 deletions

View File

@@ -669,7 +669,7 @@ int ECB_TEST(void)
unsigned char tmp[2][16];
int i, y;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
zeromem(&key, sizeof(key));
if ((err = rijndael_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
return err;

View File

@@ -63,10 +63,19 @@ static LTC_INLINE int s_aesni_is_supported(void)
a = 1;
c = 0;
#if defined(_MSC_VER) && !defined(__clang__)
int arr[4];
__cpuidex(arr, a, c);
a = arr[0];
b = arr[1];
c = arr[2];
d = arr[3];
#else
__asm__ volatile ("cpuid"
:"=a"(a), "=b"(b), "=c"(c), "=d"(d)
:"a"(a), "c"(c)
);
#endif
is_supported = ((c >> 19) & 1) && ((c >> 25) & 1);
initialized = 1;
@@ -189,7 +198,7 @@ int AES_TEST(void)
int y;
#endif
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
zeromem(&key, sizeof(key));
if ((err = AES_SETUP(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
return err;

View File

@@ -313,7 +313,7 @@ int aesni_test(void)
unsigned char tmp[2][16];
int i, y;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
zeromem(&key, sizeof(key));
if ((err = aesni_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
return err;

View File

@@ -1498,7 +1498,7 @@ int anubis_test(void)
unsigned char buf[2][16];
symmetric_key skey;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
anubis_setup(tests[x].key, tests[x].keylen, 0, &skey);
anubis_ecb_encrypt(tests[x].pt, buf[0], &skey);
anubis_ecb_decrypt(buf[0], buf[1], &skey);

View File

@@ -680,7 +680,7 @@ int camellia_test(void)
int err;
unsigned int x;
for (x = 0; x < sizeof(tests)/sizeof(tests[0]); x++) {
for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) {
zeromem(&skey, sizeof(skey));
if ((err = camellia_setup(tests[x].key, tests[x].keylen, 0, &skey)) != CRYPT_OK) {
return err;

View File

@@ -492,7 +492,7 @@ int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_
#endif
#define FI cast5_FI
LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
static LTC_INLINE ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
{
ulong32 I;
I = (Km + R);
@@ -500,7 +500,7 @@ LTC_INLINE static ulong32 FI(ulong32 R, ulong32 Km, ulong32 Kr)
return ((S1[LTC_BYTE(I, 3)] ^ S2[LTC_BYTE(I,2)]) - S3[LTC_BYTE(I,1)]) + S4[LTC_BYTE(I,0)];
}
LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
static LTC_INLINE ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
{
ulong32 I;
I = (Km ^ R);
@@ -508,7 +508,7 @@ LTC_INLINE static ulong32 FII(ulong32 R, ulong32 Km, ulong32 Kr)
return ((S1[LTC_BYTE(I, 3)] - S2[LTC_BYTE(I,2)]) + S3[LTC_BYTE(I,1)]) ^ S4[LTC_BYTE(I,0)];
}
LTC_INLINE static ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
static LTC_INLINE ulong32 FIII(ulong32 R, ulong32 Km, ulong32 Kr)
{
ulong32 I;
I = (Km - R);

View File

@@ -2,6 +2,10 @@
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
/**
@file des.c
DES code submitted by Dobes Vandermeer
@@ -1381,14 +1385,14 @@ static void cookey(const ulong32 *raw1, ulong32 *keyout)
for(i=0; i < 16; i++, raw1++)
{
raw0 = raw1++;
*cook = (ulong32)((*raw0 & 0x00fc0000L) << 6);
*cook |= (ulong32)((*raw0 & 0x00000fc0L) << 10);
*cook |= (ulong32)((*raw1 & 0x00fc0000L) >> 10);
*cook++ |= (ulong32)((*raw1 & 0x00000fc0L) >> 6);
*cook = (ulong32)((*raw0 & 0x0003f000L) << 12);
*cook |= (ulong32)((*raw0 & 0x0000003fL) << 16);
*cook |= (ulong32)((*raw1 & 0x0003f000L) >> 4);
*cook++ |= (ulong32) (*raw1 & 0x0000003fL);
*cook = (*raw0 & 0x00fc0000L) << 6;
*cook |= (*raw0 & 0x00000fc0L) << 10;
*cook |= (*raw1 & 0x00fc0000L) >> 10;
*cook++ |= (*raw1 & 0x00000fc0L) >> 6;
*cook = (*raw0 & 0x0003f000L) << 12;
*cook |= (*raw0 & 0x0000003fL) << 16;
*cook |= (*raw1 & 0x0003f000L) >> 4;
*cook++ |= (*raw1 & 0x0000003fL);
}
XMEMCPY(keyout, dough, sizeof(dough));
@@ -2018,7 +2022,7 @@ int des_test(void)
symmetric_key skey;
int i, err;
for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++)
for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++)
{
if ((err = des_setup(cases[i].key, 8, 0, &skey)) != CRYPT_OK) {
return err;
@@ -2125,7 +2129,7 @@ int des3_test(void)
return err;
}
for (i = 0; i < (int)(sizeof(cases)/sizeof(cases[0])); i++)
for (i = 0; i < (int)LTC_ARRAY_SIZE(cases); i++)
{
if ((err = des3_setup(cases[i].key, 16, 0, &skey)) != CRYPT_OK) {
return err;
@@ -2243,3 +2247,4 @@ int des3_keysize(int *keysize)
#endif
#pragma clang diagnostic pop

View File

@@ -226,7 +226,7 @@ int idea_test(void)
return CRYPT_FAIL_TESTVECTOR;
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
if ((err = idea_setup(tests[x].key, 16, 8, &key)) != CRYPT_OK) {
return err;
}

View File

@@ -286,7 +286,7 @@ int kasumi_test(void)
symmetric_key key;
int err, x;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
if ((err = kasumi_setup(tests[x].key, 16, 0, &key)) != CRYPT_OK) {
return err;
}

View File

@@ -794,7 +794,7 @@ int khazad_test(void)
unsigned char buf[2][8];
symmetric_key skey;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
khazad_setup(tests[x].key, 16, 0, &skey);
khazad_ecb_encrypt(tests[x].pt, buf[0], &skey);
khazad_ecb_decrypt(buf[0], buf[1], &skey);

View File

@@ -334,7 +334,7 @@ int kseed_test(void)
unsigned char buf[2][16];
symmetric_key skey;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
kseed_setup(tests[x].key, 16, 0, &skey);
kseed_ecb_encrypt(tests[x].pt, buf[0], &skey);
kseed_ecb_decrypt(buf[0], buf[1], &skey);

View File

@@ -242,7 +242,7 @@ int multi2_test(void)
symmetric_key skey;
int err, x;
for (x = 1; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 1; x < (int)LTC_ARRAY_SIZE(tests); x++) {
if ((err = multi2_setup(tests[x].key, 40, tests[x].rounds, &skey)) != CRYPT_OK) {
return err;
}

View File

@@ -267,7 +267,7 @@ int noekeon_test(void)
unsigned char tmp[2][16];
int err, i, y;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
zeromem(&key, sizeof(key));
if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
return err;

View File

@@ -693,7 +693,7 @@ int serpent_test(void)
symmetric_key key;
int err, x;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
if ((err = serpent_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) {
return err;
}

View File

@@ -67,7 +67,7 @@ static const sm4_u8_t sm4_sbox_table[16][16] = {
* S-box
* defined in section 2.6 S-box
*/
LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a)
static LTC_INLINE sm4_u8_t s_sm4_sbox(sm4_u8_t a)
{
return sm4_sbox_table[(a >> 4) & 0x0f][a & 0x0f];
}
@@ -80,7 +80,7 @@ LTC_INLINE static sm4_u8_t s_sm4_sbox(sm4_u8_t a)
* But we just convert a 32bit word byte by byte.
* So it's OK if we don't convert the endian order
*/
LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A)
static LTC_INLINE sm4_u32_t s_sm4_t(sm4_u32_t A)
{
sm4_u8_t a[4];
sm4_u8_t b[4];
@@ -98,7 +98,7 @@ LTC_INLINE static sm4_u32_t s_sm4_t(sm4_u32_t A)
/*
* defined in section 6.2 (2) Linear transformation L
*/
LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B)
static LTC_INLINE sm4_u32_t s_sm4_L62(sm4_u32_t B)
{
return B ^ ROLc(B, 2) ^ ROLc(B, 10) ^ ROLc(B, 18) ^ ROLc(B, 24);
}
@@ -106,7 +106,7 @@ LTC_INLINE static sm4_u32_t s_sm4_L62(sm4_u32_t B)
/*
* defined in section 6.2 Permutation T
*/
LTC_INLINE static sm4_u32_t s_sm4_T62(sm4_u32_t Z)
static LTC_INLINE sm4_u32_t s_sm4_T62(sm4_u32_t Z)
{
return s_sm4_L62(s_sm4_t(Z));
}
@@ -137,7 +137,7 @@ static const sm4_u32_t sm4_CK[32] =
/*
* defined in section 7.3 (1) L'
*/
LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B)
static LTC_INLINE sm4_u32_t s_sm4_L73(sm4_u32_t B)
{
return B ^ ROLc(B, 13) ^ ROLc(B, 23);
}
@@ -145,7 +145,7 @@ LTC_INLINE static sm4_u32_t s_sm4_L73(sm4_u32_t B)
/*
* defined in section 7.3 (1) T'
*/
LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z)
static LTC_INLINE sm4_u32_t s_sm4_T73(sm4_u32_t Z)
{
return s_sm4_L73(s_sm4_t(Z));
}
@@ -153,7 +153,7 @@ LTC_INLINE static sm4_u32_t s_sm4_T73(sm4_u32_t Z)
/*
* defined in section 7.3 Key Expansion
*/
LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
static LTC_INLINE void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
{
sm4_u32_t MK[4] = { 0 };
sm4_u32_t K[4+32] = { 0 };
@@ -175,7 +175,7 @@ LTC_INLINE static void s_sm4_mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
/*
* defined in section 6 Round Function F
*/
LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
static LTC_INLINE sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
{
return X[0] ^ s_sm4_T62(X[1] ^ X[2] ^ X[3] ^ rk);
}
@@ -183,7 +183,7 @@ LTC_INLINE static sm4_u32_t s_sm4_F(sm4_u32_t X[4], sm4_u32_t rk)
/*
* defined in section 7.1 (2) The reverse transformation
*/
LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
static LTC_INLINE void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
{
Y[0] = X[35];
Y[1] = X[34];
@@ -194,7 +194,7 @@ LTC_INLINE static void s_sm4_R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
/*
* defined in section 7.1 (En)cryption
*/
LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
static LTC_INLINE void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
{
int i;
@@ -203,7 +203,7 @@ LTC_INLINE static void s_sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_
s_sm4_R(Y, X);
}
LTC_INLINE static void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
static LTC_INLINE void s_sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
{
int i;
@@ -229,7 +229,7 @@ int sm4_setup(const unsigned char *key, int keylen,
/*
* SM4 encryption.
*/
LTC_INLINE static void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
static LTC_INLINE void s_sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
{
sm4_u32_t Y[4];
sm4_u32_t X[32+4];

View File

@@ -150,7 +150,7 @@ int tea_test(void)
symmetric_key skey;
size_t i;
int err, y;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
for (i = 0; i < LTC_ARRAY_SIZE(tests); i++) {
zeromem(&skey, sizeof(skey));
l = sizeof(key);
@@ -166,8 +166,8 @@ int tea_test(void)
tea_ecb_encrypt(ptct[0], tmp[0], &skey);
tea_ecb_decrypt(tmp[0], tmp[1], &skey);
if (compare_testvector(tmp[0], 8, ptct[1], 8, "TEA Encrypt", (int)i) != 0 ||
compare_testvector(tmp[1], 8, ptct[0], 8, "TEA Decrypt", (int)i) != 0) {
if (compare_testvector(tmp[0], 8, ptct[1], 8, "TEA Encrypt", i) != 0 ||
compare_testvector(tmp[1], 8, ptct[0], 8, "TEA Decrypt", i) != 0) {
return CRYPT_FAIL_TESTVECTOR;
}

View File

@@ -651,7 +651,7 @@ int twofish_test(void)
unsigned char tmp[2][16];
int err, i, y;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
if ((err = twofish_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
return err;
}

View File

@@ -197,7 +197,7 @@ int xtea_test(void)
unsigned char tmp[2][8];
symmetric_key skey;
int i, err, y;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
zeromem(&skey, sizeof(skey));
if ((err = xtea_setup(tests[i].key, 16, 0, &skey)) != CRYPT_OK) {
return err;

View File

@@ -29,7 +29,7 @@ int ccm_add_aad(ccm_state *ccm,
for (y = 0; y < adatalen; y++) {
if (ccm->x == 16) {
/* full block so let's encrypt it */
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->x = 0;
@@ -40,7 +40,7 @@ int ccm_add_aad(ccm_state *ccm,
/* remainder? */
if (ccm->aadlen == ccm->current_aadlen) {
if (ccm->x != 0) {
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}
}

View File

@@ -66,7 +66,7 @@ int ccm_add_nonce(ccm_state *ccm,
}
/* encrypt PAD */
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}

View File

@@ -28,7 +28,7 @@ int ccm_done(ccm_state *ccm,
LTC_ARGCHK(taglen != NULL);
if (ccm->x != 0) {
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}
}
@@ -37,11 +37,11 @@ int ccm_done(ccm_state *ccm,
for (y = 15; y > 15 - ccm->L; y--) {
ccm->ctr[y] = 0x00;
}
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
return err;
}
cipher_descriptor[ccm->cipher].done(&ccm->K);
ecb_done(&ccm->K);
/* store the TAG */
for (x = 0; x < 16 && x < *taglen; x++) {

View File

@@ -41,10 +41,9 @@ int ccm_init(ccm_state *ccm, int cipher,
ccm->taglen = taglen;
/* schedule key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->cipher = cipher;
/* let's get the L value */
ccm->ptlen = ptlen;

View File

@@ -32,7 +32,7 @@
*/
int ccm_memory(int cipher,
const unsigned char *key, unsigned long keylen,
symmetric_key *uskey,
symmetric_ECB *uskey,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
unsigned char *pt, unsigned long ptlen,
@@ -42,7 +42,7 @@ int ccm_memory(int cipher,
{
unsigned char PAD[16], ctr[16], CTRPAD[16], ptTag[16], b, *pt_real;
unsigned char *pt_work = NULL;
symmetric_key *skey;
symmetric_ECB *skey;
int err;
unsigned long len, L, x, y, z, CTRlen;
@@ -78,12 +78,15 @@ int ccm_memory(int cipher,
if (*taglen < 4 || *taglen > 16 || (*taglen % 2) == 1 || headerlen > 0x7fffffffu) {
return CRYPT_INVALID_ARG;
}
if (noncelen < 7) {
return CRYPT_INVALID_ARG;
}
/* is there an accelerator? */
if (cipher_descriptor[cipher].accel_ccm_memory != NULL) {
return cipher_descriptor[cipher].accel_ccm_memory(
key, keylen,
uskey,
&uskey->key,
nonce, noncelen,
header, headerlen,
pt, ptlen,
@@ -120,7 +123,7 @@ int ccm_memory(int cipher,
}
/* initialize the cipher */
if ((err = cipher_descriptor[cipher].setup(key, (int)keylen, 0, skey)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, skey)) != CRYPT_OK) {
XFREE(skey);
return err;
}
@@ -144,7 +147,7 @@ int ccm_memory(int cipher,
(L-1));
/* nonce */
for (y = 0; y < 15 - L; y++) {
for (y = 0; y < noncelen; y++) {
PAD[x++] = nonce[y];
}
@@ -170,7 +173,7 @@ int ccm_memory(int cipher,
}
/* encrypt PAD */
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
@@ -195,7 +198,7 @@ int ccm_memory(int cipher,
for (y = 0; y < headerlen; y++) {
if (x == 16) {
/* full block so let's encrypt it */
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
x = 0;
@@ -204,7 +207,7 @@ int ccm_memory(int cipher,
}
/* remainder */
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
@@ -239,7 +242,7 @@ int ccm_memory(int cipher,
ctr[z] = (ctr[z] + 1) & 255;
if (ctr[z]) break;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}
@@ -248,7 +251,7 @@ int ccm_memory(int cipher,
*(LTC_FAST_TYPE_PTR_CAST(&PAD[z])) ^= *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z]));
*(LTC_FAST_TYPE_PTR_CAST(&ct[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])) ^ *(LTC_FAST_TYPE_PTR_CAST(&CTRPAD[z]));
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
@@ -259,7 +262,7 @@ int ccm_memory(int cipher,
ctr[z] = (ctr[z] + 1) & 255;
if (ctr[z]) break;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}
@@ -268,7 +271,7 @@ int ccm_memory(int cipher,
*(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&ct[y+z])) ^ *(LTC_FAST_TYPE_PTR_CAST(&CTRPAD[z]));
*(LTC_FAST_TYPE_PTR_CAST(&PAD[z])) ^= *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z]));
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
@@ -283,7 +286,7 @@ int ccm_memory(int cipher,
ctr[z] = (ctr[z] + 1) & 255;
if (ctr[z]) break;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}
CTRlen = 0;
@@ -299,7 +302,7 @@ int ccm_memory(int cipher,
}
if (x == 16) {
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
x = 0;
@@ -308,7 +311,7 @@ int ccm_memory(int cipher,
}
if (x != 0) {
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
@@ -318,12 +321,12 @@ int ccm_memory(int cipher,
for (y = 15; y > 15 - L; y--) {
ctr[y] = 0x00;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}
if (skey != uskey) {
cipher_descriptor[cipher].done(skey);
ecb_done(skey);
#ifdef LTC_CLEAN_STACK
zeromem(skey, sizeof(*skey));
#endif

View File

@@ -47,7 +47,7 @@ int ccm_process(ccm_state *ccm,
ccm->ctr[z] = (ccm->ctr[z] + 1) & 255;
if (ccm->ctr[z]) break;
}
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->CTRlen = 0;
@@ -63,7 +63,7 @@ int ccm_process(ccm_state *ccm,
}
if (ccm->x == 16) {
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->x = 0;

View File

@@ -108,7 +108,7 @@ int ccm_test(void)
unsigned long taglen, x, y;
unsigned char buf[64], buf2[64], tag[16], tag2[16], tag3[16], zero[64];
int err, idx;
symmetric_key skey;
symmetric_ECB skey;
ccm_state ccm;
zeromem(zero, 64);
@@ -121,11 +121,11 @@ int ccm_test(void)
}
}
for (x = 0; x < (sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < LTC_ARRAY_SIZE(tests); x++) {
for (y = 0; y < 2; y++) {
taglen = tests[x].taglen;
if (y == 0) {
if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
if ((err = ecb_start(idx, tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
return err;
}
@@ -151,7 +151,7 @@ int ccm_test(void)
return err;
}
} else {
if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, (int)tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
return err;
}
if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
@@ -168,10 +168,10 @@ int ccm_test(void)
}
}
if (compare_testvector(buf, tests[x].ptlen, tests[x].ct, tests[x].ptlen, "CCM encrypt data", (int)x)) {
if (compare_testvector(buf, tests[x].ptlen, tests[x].ct, tests[x].ptlen, "CCM encrypt data", x)) {
return CRYPT_FAIL_TESTVECTOR;
}
if (compare_testvector(tag, taglen, tests[x].tag, tests[x].taglen, "CCM encrypt tag", (int)x)) {
if (compare_testvector(tag, taglen, tests[x].tag, tests[x].taglen, "CCM encrypt tag", x)) {
return CRYPT_FAIL_TESTVECTOR;
}
@@ -189,7 +189,7 @@ int ccm_test(void)
return err;
}
} else {
if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, (int)tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
if ((err = ccm_init(&ccm, idx, tests[x].key, 16, tests[x].ptlen, tests[x].taglen, tests[x].headerlen)) != CRYPT_OK) {
return err;
}
if ((err = ccm_add_nonce(&ccm, tests[x].nonce, tests[x].noncelen)) != CRYPT_OK) {
@@ -207,7 +207,7 @@ int ccm_test(void)
}
if (compare_testvector(buf2, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "CCM decrypt data", (int)x)) {
if (compare_testvector(buf2, tests[x].ptlen, tests[x].pt, tests[x].ptlen, "CCM decrypt data", x)) {
return CRYPT_FAIL_TESTVECTOR;
}
if (y == 0) {
@@ -225,17 +225,17 @@ int ccm_test(void)
tag3, &taglen, 1 ) != CRYPT_ERROR) {
return CRYPT_FAIL_TESTVECTOR;
}
if (compare_testvector(buf2, tests[x].ptlen, zero, tests[x].ptlen, "CCM decrypt wrong tag", (int)x)) {
if (compare_testvector(buf2, tests[x].ptlen, zero, tests[x].ptlen, "CCM decrypt wrong tag", x)) {
return CRYPT_FAIL_TESTVECTOR;
}
} else {
if (compare_testvector(tag2, taglen, tests[x].tag, tests[x].taglen, "CCM decrypt tag", (int)x)) {
if (compare_testvector(tag2, taglen, tests[x].tag, tests[x].taglen, "CCM decrypt tag", x)) {
return CRYPT_FAIL_TESTVECTOR;
}
}
if (y == 0) {
cipher_descriptor[idx].done(&skey);
ecb_done(&skey);
}
}
}

View File

@@ -216,7 +216,7 @@ int eax_test(void)
}
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
len = sizeof(outtag);
if ((err = eax_encrypt_authenticate_memory(idx, tests[x].key, tests[x].keylen,
tests[x].nonce, tests[x].noncelen, tests[x].header, tests[x].headerlen,

View File

@@ -20,7 +20,6 @@ int gcm_add_aad(gcm_state *gcm,
const unsigned char *adata, unsigned long adatalen)
{
unsigned long x;
int err;
#ifdef LTC_FAST
unsigned long y;
#endif
@@ -34,10 +33,6 @@ int gcm_add_aad(gcm_state *gcm,
return CRYPT_INVALID_ARG;
}
if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
return err;
}
/* in IV mode? */
if (gcm->mode == LTC_GCM_MODE_IV) {
/* IV length must be > 0 */

View File

@@ -20,7 +20,6 @@ int gcm_add_iv(gcm_state *gcm,
const unsigned char *IV, unsigned long IVlen)
{
unsigned long x, y;
int err;
LTC_ARGCHK(gcm != NULL);
if (IVlen > 0) {
@@ -36,11 +35,6 @@ int gcm_add_iv(gcm_state *gcm,
return CRYPT_INVALID_ARG;
}
if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
return err;
}
/* trip the ivmode flag */
if (IVlen + gcm->buflen > 12) {
gcm->ivmode |= 1;

View File

@@ -30,10 +30,6 @@ int gcm_done(gcm_state *gcm,
return CRYPT_INVALID_ARG;
}
if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
return err;
}
if (gcm->mode == LTC_GCM_MODE_IV) {
/* let's process the IV */
if ((err = gcm_add_aad(gcm, NULL, 0)) != CRYPT_OK) return err;
@@ -63,7 +59,7 @@ int gcm_done(gcm_state *gcm,
gcm_mult_h(gcm, gcm->X);
/* encrypt original counter */
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
for (x = 0; x < 16 && x < *taglen; x++) {
@@ -71,7 +67,7 @@ int gcm_done(gcm_state *gcm,
}
*taglen = x;
cipher_descriptor[gcm->cipher].done(&gcm->K);
ecb_done(&gcm->K);
return CRYPT_OK;
}

View File

@@ -44,20 +44,19 @@ int gcm_init(gcm_state *gcm, int cipher,
}
/* schedule key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &gcm->K)) != CRYPT_OK) {
return err;
}
/* H = E(0) */
zeromem(B, 16);
if ((err = cipher_descriptor[cipher].ecb_encrypt(B, gcm->H, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(B, gcm->H, &gcm->K)) != CRYPT_OK) {
return err;
}
/* setup state */
zeromem(gcm->buf, sizeof(gcm->buf));
zeromem(gcm->X, sizeof(gcm->X));
gcm->cipher = cipher;
gcm->mode = LTC_GCM_MODE_IV;
gcm->ivmode = 0;
gcm->buflen = 0;

View File

@@ -37,10 +37,6 @@ int gcm_process(gcm_state *gcm,
return CRYPT_INVALID_ARG;
}
if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
return err;
}
/* 0xFFFFFFFE0 = ((2^39)-256)/8 */
if (gcm->pttotlen / 8 + (ulong64)gcm->buflen + (ulong64)ptlen >= CONST64(0xFFFFFFFE0)) {
return CRYPT_INVALID_ARG;
@@ -64,7 +60,7 @@ int gcm_process(gcm_state *gcm,
if (++gcm->Y[y] & 255) { break; }
}
/* encrypt the counter */
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
@@ -93,7 +89,7 @@ int gcm_process(gcm_state *gcm,
for (y = 15; y >= 12; y--) {
if (++gcm->Y[y] & 255) { break; }
}
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
}
@@ -111,7 +107,7 @@ int gcm_process(gcm_state *gcm,
for (y = 15; y >= 12; y--) {
if (++gcm->Y[y] & 255) { break; }
}
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
}
@@ -129,7 +125,7 @@ int gcm_process(gcm_state *gcm,
for (y = 15; y >= 12; y--) {
if (++gcm->Y[y] & 255) { break; }
}
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
gcm->buflen = 0;

View File

@@ -338,7 +338,7 @@ int gcm_test(void)
if ((err = gcm_done(&gcm, T[0], &y)) != CRYPT_OK) return err;
if (compare_testvector(T[0], y, tests[0].T, 16, "GCM Encrypt Tag-special", 0)) return CRYPT_FAIL_TESTVECTOR;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
y = sizeof(T[0]);
if ((err = gcm_memory(idx, tests[x].K, tests[x].keylen,
tests[x].IV, tests[x].IVlen,

View File

@@ -25,16 +25,8 @@ int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
/* check if valid cipher */
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
return err;
}
LTC_ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL);
/* check length */
if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
return CRYPT_INVALID_ARG;
}
/* can't use a encrypt-only descriptor */
LTC_ARGCHK(cipher_descriptor[ocb->key.cipher].ecb_decrypt != NULL);
/* Get Z[i] value */
ocb_shift_xor(ocb, Z);
@@ -43,7 +35,7 @@ int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
for (x = 0; x < ocb->block_len; x++) {
tmp[x] = ct[x] ^ Z[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_decrypt_block(tmp, pt, &ocb->key)) != CRYPT_OK) {
return err;
}
for (x = 0; x < ocb->block_len; x++) {

View File

@@ -24,12 +24,6 @@ int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct)
LTC_ARGCHK(ocb != NULL);
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
return err;
}
if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
return CRYPT_INVALID_ARG;
}
/* compute checksum */
for (x = 0; x < ocb->block_len; x++) {
@@ -43,7 +37,7 @@ int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct)
for (x = 0; x < ocb->block_len; x++) {
tmp[x] = pt[x] ^ Z[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, ct, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(tmp, ct, &ocb->key)) != CRYPT_OK) {
return err;
}
for (x = 0; x < ocb->block_len; x++) {

View File

@@ -57,7 +57,7 @@ int ocb_init(ocb_state *ocb, int cipher,
/* determine which polys to use */
ocb->block_len = cipher_descriptor[cipher].block_length;
x = (int)(sizeof(polys)/sizeof(polys[0]));
x = (int)LTC_ARRAY_SIZE(polys);
for (poly = 0; poly < x; poly++) {
if (polys[poly].len == ocb->block_len) {
break;
@@ -71,13 +71,13 @@ int ocb_init(ocb_state *ocb, int cipher,
}
/* schedule the key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &ocb->key)) != CRYPT_OK) {
return err;
}
/* find L = E[0] */
zeromem(ocb->L, ocb->block_len);
if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) {
return err;
}
@@ -85,7 +85,7 @@ int ocb_init(ocb_state *ocb, int cipher,
for (x = 0; x < ocb->block_len; x++) {
ocb->R[x] = ocb->L[x] ^ nonce[x];
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) {
return err;
}
@@ -126,7 +126,6 @@ int ocb_init(ocb_state *ocb, int cipher,
/* set other params */
ocb->block_index = 1;
ocb->cipher = cipher;
return CRYPT_OK;
}

View File

@@ -17,6 +17,11 @@
*/
int ocb_ntz(unsigned long x)
{
#if defined(LTC_HAVE_CTZL_BUILTIN)
if (x == 0)
return sizeof(unsigned long) * CHAR_BIT;
return __builtin_ctzl(x);
#else
int c;
x &= 0xFFFFFFFFUL;
c = 0;
@@ -25,6 +30,7 @@ int ocb_ntz(unsigned long x)
x >>= 1;
}
return c;
#endif
}
#endif

View File

@@ -167,7 +167,7 @@ int ocb_test(void)
}
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
len = sizeof(outtag);
if ((err = ocb_encrypt_authenticate_memory(idx, tests[x].key, 16,
tests[x].nonce, tests[x].pt, tests[x].ptlen, outct, outtag, &len)) != CRYPT_OK) {

View File

@@ -40,11 +40,7 @@ int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(tag != NULL);
LTC_ARGCHK(taglen != NULL);
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
return err;
}
if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length ||
(int)ptlen > ocb->block_len || (int)ptlen < 0) {
if ((int)ptlen > ocb->block_len || (int)ptlen < 0) {
return CRYPT_INVALID_ARG;
}
@@ -76,7 +72,7 @@ int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
}
/* Y[m] = E(X[m])) */
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(X, Y, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(X, Y, &ocb->key)) != CRYPT_OK) {
goto error;
}
@@ -107,10 +103,10 @@ int s_ocb_done(ocb_state *ocb, const unsigned char *pt, unsigned long ptlen,
}
/* encrypt checksum, er... tag!! */
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->checksum, X, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->checksum, X, &ocb->key)) != CRYPT_OK) {
goto error;
}
cipher_descriptor[ocb->cipher].done(&ocb->key);
ecb_done(&ocb->key);
/* now store it */
for (x = 0; x < ocb->block_len && x < (int)*taglen; x++) {

View File

@@ -29,7 +29,7 @@ static int s_ocb3_int_aad_add_block(ocb3_state *ocb, const unsigned char *aad_bl
/* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
ocb3_int_xor_blocks(tmp, aad_block, ocb->aOffset_current, ocb->block_len);
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) {
return err;
}
ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len);

View File

@@ -32,14 +32,7 @@ int ocb3_decrypt(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen,
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(pt != NULL);
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
return err;
}
if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
return CRYPT_INVALID_ARG;
}
if (ctlen % ocb->block_len) { /* ctlen has to bu multiple of block_len */
if (ctlen % ocb->block_len) { /* ctlen has to be multiple of block_len */
return CRYPT_INVALID_ARG;
}
@@ -55,7 +48,7 @@ int ocb3_decrypt(ocb3_state *ocb, const unsigned char *ct, unsigned long ctlen,
ocb3_int_xor_blocks(tmp, ct_b, ocb->Offset_current, ocb->block_len);
/* decrypt */
if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_decrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}

View File

@@ -34,10 +34,6 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct
LTC_ARGCHK(pt != NULL);
}
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
goto LBL_ERR;
}
full_blocks = ctlen/ocb->block_len;
full_blocks_len = full_blocks * ocb->block_len;
last_block_len = ctlen - full_blocks_len;
@@ -54,7 +50,7 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct
ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len);
/* Pad = ENCIPHER(K, Offset_*) */
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
@@ -76,7 +72,7 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct
for(x=0; x<ocb->block_len; x++) {
ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
}
@@ -86,7 +82,7 @@ int ocb3_decrypt_last(ocb3_state *ocb, const unsigned char *ct, unsigned long ct
for(x=0; x<ocb->block_len; x++) {
ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
}

View File

@@ -24,9 +24,6 @@ int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen)
LTC_ARGCHK(ocb != NULL);
LTC_ARGCHK(tag != NULL);
LTC_ARGCHK(taglen != NULL);
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
goto LBL_ERR;
}
/* check taglen */
if ((int)*taglen < ocb->tag_len) {
@@ -52,7 +49,7 @@ int ocb3_done(ocb3_state *ocb, unsigned char *tag, unsigned long *taglen)
}
/* Sum = Sum_m xor ENCIPHER(K, CipherInput) */
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
ocb3_int_xor_blocks(ocb->aSum_current, ocb->aSum_current, tmp, ocb->block_len);

View File

@@ -32,14 +32,7 @@ int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen,
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
return err;
}
if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
return CRYPT_INVALID_ARG;
}
if (ptlen % ocb->block_len) { /* ptlen has to bu multiple of block_len */
if (ptlen % ocb->block_len) { /* ptlen has to be multiple of block_len */
return CRYPT_INVALID_ARG;
}
@@ -55,7 +48,7 @@ int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen,
ocb3_int_xor_blocks(tmp, pt_b, ocb->Offset_current, ocb->block_len);
/* encrypt */
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(tmp, tmp, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}

View File

@@ -34,10 +34,6 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt
LTC_ARGCHK(ct != NULL);
}
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
goto LBL_ERR;
}
full_blocks = ptlen/ocb->block_len;
full_blocks_len = full_blocks * ocb->block_len;
last_block_len = ptlen - full_blocks_len;
@@ -56,7 +52,7 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt
ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len);
/* Pad = ENCIPHER(K, Offset_*) */
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
@@ -78,7 +74,7 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt
for(x=0; x<ocb->block_len; x++) {
ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
} else {
@@ -87,7 +83,7 @@ int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long pt
for(x=0; x<ocb->block_len; x++) {
ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
goto LBL_ERR;
}
}

View File

@@ -34,7 +34,7 @@ static void s_ocb3_int_calc_offset_zero(ocb3_state *ocb, const unsigned char *no
/* Ktop = ENCIPHER(K, Nonce[1..122] || zeros(6)) */
iNonce[ocb->block_len-1] = iNonce[ocb->block_len-1] & 0xC0;
if ((cipher_descriptor[ocb->cipher].ecb_encrypt(iNonce, iKtop, &ocb->key)) != CRYPT_OK) {
if ((ecb_encrypt_block(iNonce, iKtop, &ocb->key)) != CRYPT_OK) {
zeromem(ocb->Offset_current, ocb->block_len);
return;
}
@@ -100,7 +100,6 @@ int ocb3_init(ocb3_state *ocb, int cipher,
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
ocb->cipher = cipher;
/* Valid Nonce?
* As of RFC7253: "string of no more than 120 bits" */
@@ -121,7 +120,7 @@ int ocb3_init(ocb3_state *ocb, int cipher,
/* determine which polys to use */
ocb->block_len = cipher_descriptor[cipher].block_length;
x = (int)(sizeof(polys)/sizeof(polys[0]));
x = (int)LTC_ARRAY_SIZE(polys);
for (poly = 0; poly < x; poly++) {
if (polys[poly].len == ocb->block_len) {
break;
@@ -135,13 +134,13 @@ int ocb3_init(ocb3_state *ocb, int cipher,
}
/* schedule the key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &ocb->key)) != CRYPT_OK) {
return err;
}
/* L_* = ENCIPHER(K, zeros(128)) */
zeromem(ocb->L_star, ocb->block_len);
if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L_star, ocb->L_star, &ocb->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(ocb->L_star, ocb->L_star, &ocb->key)) != CRYPT_OK) {
return err;
}

View File

@@ -16,6 +16,11 @@
*/
int ocb3_int_ntz(unsigned long x)
{
#if defined(LTC_HAVE_CTZL_BUILTIN)
if (x == 0)
return sizeof(unsigned long) * CHAR_BIT;
return __builtin_ctzl(x);
#else
int c;
x &= 0xFFFFFFFFUL;
c = 0;
@@ -24,6 +29,7 @@ int ocb3_int_ntz(unsigned long x)
x >>= 1;
}
return c;
#endif
}
#endif

View File

@@ -209,7 +209,7 @@ int ocb3_test(void)
}
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
len = 16; /* must be the same as the required taglen */
if ((err = ocb3_encrypt_authenticate_memory(idx,
key, sizeof(key),

View File

@@ -612,7 +612,7 @@ int siv_test(void)
cipher = find_cipher("aes");
for (n = 0; n < sizeof(siv_tests)/sizeof(siv_tests[0]); ++n) {
for (n = 0; n < LTC_ARRAY_SIZE(siv_tests); ++n) {
buflen = sizeof(buf);
if ((err = siv_encrypt_memory(cipher,
siv_tests[n].Key, siv_tests[n].Keylen,

View File

@@ -277,7 +277,7 @@ int chc_test(void)
oldhashidx = cipher_idx;
chc_register(idx);
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
if ((err = chc_init(&md)) != CRYPT_OK) {
return err;
}

View File

@@ -380,7 +380,7 @@ int rmd128_test(void)
unsigned char tmp[16];
hash_state md;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
rmd128_init(&md);
rmd128_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
rmd128_done(&md, tmp);

View File

@@ -439,7 +439,7 @@ int rmd160_test(void)
unsigned char tmp[20];
hash_state md;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
rmd160_init(&md);
rmd160_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
rmd160_done(&md, tmp);

View File

@@ -405,7 +405,7 @@ int rmd256_test(void)
unsigned char tmp[32];
hash_state md;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
rmd256_init(&md);
rmd256_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
rmd256_done(&md, tmp);

View File

@@ -470,7 +470,7 @@ int rmd320_test(void)
unsigned char tmp[40];
hash_state md;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
rmd320_init(&md);
rmd320_process(&md, (unsigned char *)tests[i].msg, XSTRLEN(tests[i].msg));
rmd320_done(&md, tmp);

View File

@@ -566,7 +566,7 @@ static const ulong64 table[4*256] = {
CONST64(0xC83223F1720AEF96) /* 1022 */, CONST64(0xC3A0396F7363A51F) /* 1023 */};
/* one round of the hash function */
LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
static LTC_INLINE void s_tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x, int mul)
{
ulong64 tmp;
tmp = (*c ^= x);
@@ -582,14 +582,14 @@ LTC_INLINE static void tiger_round(ulong64 *a, ulong64 *b, ulong64 *c, ulong64 x
/* one complete pass */
static void s_pass(ulong64 *a, ulong64 *b, ulong64 *c, const ulong64 *x, int mul)
{
tiger_round(a,b,c,x[0],mul);
tiger_round(b,c,a,x[1],mul);
tiger_round(c,a,b,x[2],mul);
tiger_round(a,b,c,x[3],mul);
tiger_round(b,c,a,x[4],mul);
tiger_round(c,a,b,x[5],mul);
tiger_round(a,b,c,x[6],mul);
tiger_round(b,c,a,x[7],mul);
s_tiger_round(a,b,c,x[0],mul);
s_tiger_round(b,c,a,x[1],mul);
s_tiger_round(c,a,b,x[2],mul);
s_tiger_round(a,b,c,x[3],mul);
s_tiger_round(b,c,a,x[4],mul);
s_tiger_round(c,a,b,x[5],mul);
s_tiger_round(a,b,c,x[6],mul);
s_tiger_round(b,c,a,x[7],mul);
}
/* The key mixing schedule */

View File

@@ -281,7 +281,7 @@ int whirlpool_test(void)
unsigned char tmp[64];
hash_state md;
for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
for (i = 0; i < (int)LTC_ARRAY_SIZE(tests); i++) {
whirlpool_init(&md);
whirlpool_process(&md, (unsigned char *)tests[i].msg, tests[i].len);
whirlpool_done(&md, tmp);

View File

@@ -295,6 +295,14 @@ typedef unsigned long ltc_mp_digit;
#define LTC_HAVE_ROTATE_BUILTIN
#endif
#if __has_builtin(__builtin_clzl)
#define LTC_HAVE_CLZL_BUILTIN
#endif
#if __has_builtin(__builtin_ctzl)
#define LTC_HAVE_CTZL_BUILTIN
#endif
#if defined(__GNUC__)
#define LTC_ALIGN(n) __attribute__((aligned(n)))
#else

View File

@@ -274,18 +274,14 @@ typedef struct {
#ifdef LTC_CFB_MODE
/** A block cipher CFB structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
/** The pad used to encrypt/decrypt */
pad[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The width of the mode: 1, 8, 64, or 128 */
width,
int width,
/** The padding offset */
padlen;
} symmetric_CFB;
@@ -294,30 +290,23 @@ typedef struct {
#ifdef LTC_OFB_MODE
/** A block cipher OFB structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
int padlen;
} symmetric_OFB;
#endif
#ifdef LTC_CBC_MODE
/** A block cipher CBC structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen;
} symmetric_CBC;
#endif
@@ -325,19 +314,15 @@ typedef struct {
#ifdef LTC_CTR_MODE
/** A block cipher CTR structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The counter */
unsigned char ctr[MAXBLOCKSIZE];
/** The pad used to encrypt/decrypt */
unsigned char pad[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen,
int padlen,
/** The mode (endianess) of the CTR, 0==little, 1==big */
mode,
/** counter width */
@@ -349,18 +334,14 @@ typedef struct {
#ifdef LTC_LRW_MODE
/** A LRW structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[16],
/** the tweak key */
tweak[16],
/** The current pad, it's the product of the first 15 bytes against the tweak key */
pad[16];
/** The scheduled symmetric key */
symmetric_key key;
#ifdef LTC_LRW_TABLES
/** The pre-computed multiplication table */
unsigned char PC[16][256][16];
@@ -374,17 +355,13 @@ typedef struct {
#ifdef LTC_F8_MODE
/** A block cipher F8 structure */
typedef struct {
/** The ECB context of the cipher */
symmetric_ECB ecb;
/** The current IV */
unsigned char IV[MAXBLOCKSIZE],
MIV[MAXBLOCKSIZE];
/** The scheduled key */
symmetric_key key;
/** The index of the cipher chosen */
int cipher,
/** The block size of the given cipher */
blocklen,
/** The padding offset */
padlen;
int padlen;
/** Current block count */
ulong32 blockcnt;
} symmetric_F8;
@@ -451,7 +428,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, symmetric_key *skey);
int (*accel_ecb_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, const symmetric_key *skey);
/** Accelerated ECB decryption
@param pt Plaintext
@@ -460,7 +437,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, symmetric_key *skey);
int (*accel_ecb_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, const symmetric_key *skey);
/** Accelerated CBC encryption
@param pt Plaintext
@@ -470,7 +447,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
int (*accel_cbc_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
/** Accelerated CBC decryption
@param pt Plaintext
@@ -480,7 +457,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, symmetric_key *skey);
int (*accel_cbc_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const symmetric_key *skey);
/** Accelerated CTR encryption
@param pt Plaintext
@@ -491,7 +468,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, symmetric_key *skey);
int (*accel_ctr_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, int mode, const symmetric_key *skey);
/** Accelerated LRW
@param pt Plaintext
@@ -502,7 +479,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
int (*accel_lrw_encrypt)(const unsigned char *pt, unsigned char *ct, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, const symmetric_key *skey);
/** Accelerated LRW
@param ct Ciphertext
@@ -513,7 +490,7 @@ extern struct ltc_cipher_descriptor {
@param skey The scheduled key context
@return CRYPT_OK if successful
*/
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, symmetric_key *skey);
int (*accel_lrw_decrypt)(const unsigned char *ct, unsigned char *pt, unsigned long blocks, unsigned char *IV, const unsigned char *tweak, const symmetric_key *skey);
/** Accelerated CCM packet (one-shot)
@param key The secret key to use
@@ -533,7 +510,7 @@ extern struct ltc_cipher_descriptor {
*/
int (*accel_ccm_memory)(
const unsigned char *key, unsigned long keylen,
symmetric_key *uskey,
const symmetric_key *uskey,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
unsigned char *pt, unsigned long ptlen,
@@ -923,8 +900,8 @@ extern const struct ltc_cipher_descriptor tea_desc;
#ifdef LTC_ECB_MODE
int ecb_start(int cipher, const unsigned char *key,
int keylen, int num_rounds, symmetric_ECB *ecb);
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_ECB *ecb);
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_ECB *ecb);
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, const symmetric_ECB *ecb);
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, const symmetric_ECB *ecb);
int ecb_done(symmetric_ECB *ecb);
#endif
@@ -1016,7 +993,7 @@ int f8_test_mode(void);
#ifdef LTC_XTS_MODE
typedef struct {
symmetric_key key1, key2;
symmetric_ECB key1, key2;
int cipher;
} symmetric_xts;

View File

@@ -710,6 +710,18 @@
#error LTC_NO_MATH defined, but also a math descriptor
#endif
#if !defined(LTC_ECB_MODE)
#if defined(LTC_CFB_MODE) || defined(LTC_OFB_MODE) || defined(LTC_CBC_MODE) || defined(LTC_CTR_MODE) || \
defined(LTC_F8_MODE) || defined(LTC_LRW_MODE) || defined(LTC_XTS_MODE) )
#error LTC_ECB_MODE not defined, but all other modes depend on it
#endif
#if defined(LTC_OMAC) || defined(LTC_PMAC) || defined(LTC_XCBC) || defined(LTC_F9_MODE) || defined(LTC_EAX_MODE) || \
defined(LTC_OCB_MODE) || defined(LTC_OCB3_MODE) || defined(LTC_CCM_MODE) || defined(LTC_GCM_MODE) )
#error LTC_ECB_MODE not defined, but most MAC and AEAD modes depend on it
#endif
#endif
/* THREAD management */
#ifdef LTC_PTHREAD

View File

@@ -29,13 +29,12 @@ int hmac_file(int hash, const char *fname, const unsigned char *key,
#ifdef LTC_OMAC
typedef struct {
int cipher_idx,
buflen,
int buflen,
blklen;
unsigned char block[MAXBLOCKSIZE],
prev[MAXBLOCKSIZE],
Lu[2][MAXBLOCKSIZE];
symmetric_key key;
symmetric_ECB key;
} omac_state;
int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned long keylen);
@@ -66,10 +65,9 @@ typedef struct {
block[MAXBLOCKSIZE], /* currently accumulated block */
checksum[MAXBLOCKSIZE]; /* current checksum */
symmetric_key key; /* scheduled key for cipher */
symmetric_ECB key; /* scheduled key for cipher */
unsigned long block_index; /* index # for current block */
int cipher_idx, /* cipher idx */
block_len, /* length of block */
int block_len, /* length of block */
buflen; /* number of bytes in the buffer */
} pmac_state;
@@ -181,10 +179,9 @@ typedef struct {
unsigned char K[3][MAXBLOCKSIZE],
IV[MAXBLOCKSIZE];
symmetric_key key;
symmetric_ECB key;
int cipher,
buflen,
int buflen,
blocksize;
} xcbc_state;
@@ -215,7 +212,7 @@ typedef struct {
ACC[MAXBLOCKSIZE],
IV[MAXBLOCKSIZE];
symmetric_key key;
symmetric_ECB key;
int cipher,
buflen,
@@ -297,10 +294,9 @@ typedef struct {
R[MAXBLOCKSIZE], /* R value */
checksum[MAXBLOCKSIZE]; /* current checksum */
symmetric_key key; /* scheduled key for cipher */
symmetric_ECB key; /* scheduled key for cipher */
unsigned long block_index; /* index # for current block */
int cipher, /* cipher idx */
block_len; /* length of block */
int block_len; /* length of block */
} ocb_state;
int ocb_init(ocb_state *ocb, int cipher,
@@ -359,12 +355,11 @@ typedef struct {
aOffset_current[MAXBLOCKSIZE], /* AAD related helper variable */
adata_buffer[MAXBLOCKSIZE]; /* AAD buffer */
symmetric_key key; /* scheduled key for cipher */
symmetric_ECB key; /* scheduled key for cipher */
int adata_buffer_bytes; /* bytes in AAD buffer */
unsigned long ablock_index; /* index # for current adata (AAD) block */
unsigned long block_index; /* index # for current data block */
int cipher, /* cipher idx */
tag_len, /* length of tag */
int tag_len, /* length of tag */
block_len; /* length of block */
} ocb3_state;
@@ -407,14 +402,13 @@ int ocb3_test(void);
#define CCM_DECRYPT LTC_DECRYPT
typedef struct {
symmetric_ECB K;
unsigned char PAD[16], /* flags | Nonce N | l(m) */
ctr[16],
CTRPAD[16];
symmetric_key K;
int cipher, /* which cipher */
taglen, /* length of the tag (encoded in M value) */
int taglen, /* length of the tag (encoded in M value) */
x; /* index in PAD */
unsigned long L, /* L value */
@@ -448,7 +442,7 @@ int ccm_done(ccm_state *ccm,
int ccm_memory(int cipher,
const unsigned char *key, unsigned long keylen,
symmetric_key *uskey,
symmetric_ECB *uskey,
const unsigned char *nonce, unsigned long noncelen,
const unsigned char *header, unsigned long headerlen,
unsigned char *pt, unsigned long ptlen,
@@ -480,6 +474,7 @@ extern const unsigned char gcm_shift_table[];
#define LTC_GCM_MODE_TEXT 2
typedef struct {
symmetric_ECB K;
unsigned char H[16], /* multiplier */
X[16], /* accumulator */
Y[16], /* counter */
@@ -489,11 +484,7 @@ typedef struct {
#ifdef LTC_GCM_TABLES
unsigned char PC[16][256][16]; /* 16 tables of 8x128 */
#endif
symmetric_key K;
int cipher, /* which cipher */
ivmode, /* Which mode is the IV in? */
int ivmode, /* Which mode is the IV in? */
mode, /* mode the GCM code is in */
buflen; /* length of data in buf */

View File

@@ -281,8 +281,18 @@ typedef struct {
/** The private key */
void *k;
/** The hash algorithm to use when creating a signature.
* Setting this will enable RFC6979 compatible signature generation.
* The macro ECC_SET_RFC6979_HASH_ALG() is provided as a helper
* to set this.*/
const char *rfc6979_hash_alg;
} ecc_key;
#define ECC_SET_RFC6979_HASH_ALG(key, alg) do { \
(key)->rfc6979_hash_alg = (alg); \
} while(0)
/** Formats of ECC signatures */
typedef enum ecc_signature_type_ {
/* ASN.1 encoded, ANSI X9.62 */
@@ -834,7 +844,6 @@ int der_encode_object_identifier(const unsigned long *words, unsigned long nwor
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
unsigned long *words, unsigned long *outlen);
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen);
unsigned long der_object_identifier_bits(unsigned long x);
/* IA5 STRING */
int der_encode_ia5_string(const unsigned char *in, unsigned long inlen,
@@ -843,9 +852,6 @@ int der_decode_ia5_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_ia5_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
int der_ia5_char_encode(int c);
int der_ia5_value_decode(int v);
/* TELETEX STRING */
int der_decode_teletex_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
@@ -858,9 +864,6 @@ int der_decode_printable_string(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);
int der_length_printable_string(const unsigned char *octets, unsigned long noctets, unsigned long *outlen);
int der_printable_char_encode(int c);
int der_printable_value_decode(int v);
/* UTF-8 */
#if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L || defined(WCHAR_MAX) || defined(__WCHAR_MAX__) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED) || defined (__WCHAR_TYPE__)) && !defined(LTC_NO_WCHAR)
#if defined(__WCHAR_MAX__)
@@ -880,7 +883,6 @@ int der_encode_utf8_string(const wchar_t *in, unsigned long inlen,
int der_decode_utf8_string(const unsigned char *in, unsigned long inlen,
wchar_t *out, unsigned long *outlen);
unsigned long der_utf8_charsize(const wchar_t c);
int der_length_utf8_string(const wchar_t *in, unsigned long noctets, unsigned long *outlen);

View File

@@ -45,6 +45,8 @@ LTC_STATIC_ASSERT(correct_ltc_uintptr_size, sizeof(ltc_uintptr) == sizeof(void*)
#define LTC_NULL ((void *)0)
#endif
#define LTC_ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
/*
* Internal Enums
*/
@@ -122,6 +124,10 @@ typedef struct {
/* tomcrypt_cipher.h */
int ecb_encrypt_block(const unsigned char *pt, unsigned char *ct, const symmetric_ECB *ecb);
int ecb_decrypt_block(const unsigned char *ct, unsigned char *pt, const symmetric_ECB *ecb);
void blowfish_enc(ulong32 *data, unsigned long blocks, const symmetric_key *skey);
int blowfish_expand(const unsigned char *key, int keylen,
const unsigned char *data, int datalen,
@@ -357,6 +363,7 @@ struct get_char {
} data;
struct str unget_buf;
char unget_buf_[LTC_PEM_DECODE_BUFSZ];
int prev_get;
};
#endif
@@ -383,7 +390,7 @@ int pem_decrypt(unsigned char *data, unsigned long *datalen,
int pem_get_char_from_file(struct get_char *g);
#endif /* LTC_NO_FILE */
int pem_get_char_from_buf(struct get_char *g);
int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_char *g);
int pem_read(void *asn1_cert, unsigned long *asn1_len, struct pem_headers *hdr, struct get_char *g);
#endif
/* tomcrypt_pk.h */
@@ -441,6 +448,8 @@ int ecc_verify_hash_internal(void *r, void *s,
const unsigned char *hash, unsigned long hashlen,
int *stat, const ecc_key *key);
int ecc_rfc6979_key(const ecc_key *priv, const unsigned char *in, unsigned long inlen, ecc_key *key);
#ifdef LTC_SSH
int ecc_ssh_ecdsa_encode_name(char *buffer, unsigned long *buflen, const ecc_key *key);
#endif
@@ -579,6 +588,17 @@ int der_length_asn1_length(unsigned long len, unsigned long *outlen);
int der_length_sequence_ex(const ltc_asn1_list *list, unsigned long inlen,
unsigned long *outlen, unsigned long *payloadlen);
int der_length_object_identifier_full(const unsigned long *words, unsigned long nwords,
unsigned long *outlen, unsigned long *datalen);
int der_ia5_char_encode(int c);
int der_ia5_value_decode(int v);
int der_printable_char_encode(int c);
int der_printable_value_decode(int v);
unsigned long der_utf8_charsize(const wchar_t c);
typedef struct {
ltc_asn1_type t;
ltc_asn1_list **pp;

View File

@@ -33,7 +33,7 @@ int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen)
if (f9->buflen != 0) {
/* encrypt */
cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key);
ecb_encrypt_block(f9->IV, f9->IV, &f9->key);
f9->buflen = 0;
for (x = 0; x < f9->blocksize; x++) {
f9->ACC[x] ^= f9->IV[x];
@@ -41,13 +41,13 @@ int f9_done(f9_state *f9, unsigned char *out, unsigned long *outlen)
}
/* schedule modified key */
if ((err = cipher_descriptor[f9->cipher].setup(f9->akey, f9->keylen, 0, &f9->key)) != CRYPT_OK) {
if ((err = ecb_start(f9->cipher, f9->akey, f9->keylen, 0, &f9->key)) != CRYPT_OK) {
return err;
}
/* encrypt the ACC */
cipher_descriptor[f9->cipher].ecb_encrypt(f9->ACC, f9->ACC, &f9->key);
cipher_descriptor[f9->cipher].done(&f9->key);
ecb_encrypt_block(f9->ACC, f9->ACC, &f9->key);
ecb_done(&f9->key);
/* extract tag */
for (x = 0; x < f9->blocksize && (unsigned long)x < *outlen; x++) {

View File

@@ -38,7 +38,7 @@ int f9_init(f9_state *f9, int cipher, const unsigned char *key, unsigned long ke
}
#endif
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &f9->key)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &f9->key)) != CRYPT_OK) {
goto done;
}

View File

@@ -38,7 +38,7 @@ int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen)
for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x])));
}
cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key);
ecb_encrypt_block(f9->IV, f9->IV, &f9->key);
for (x = 0; x < f9->blocksize; x += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST(&(f9->ACC[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(f9->IV[x])));
}
@@ -50,7 +50,7 @@ int f9_process(f9_state *f9, const unsigned char *in, unsigned long inlen)
while (inlen) {
if (f9->buflen == f9->blocksize) {
cipher_descriptor[f9->cipher].ecb_encrypt(f9->IV, f9->IV, &f9->key);
ecb_encrypt_block(f9->IV, f9->IV, &f9->key);
for (x = 0; x < f9->blocksize; x++) {
f9->ACC[x] ^= f9->IV[x];
}

View File

@@ -48,7 +48,7 @@ int f9_test(void)
return CRYPT_NOP;
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
taglen = 4;
if ((err = f9_memory(idx, tests[x].K, 16, tests[x].M, tests[x].msglen, T, &taglen)) != CRYPT_OK) {
return err;

View File

@@ -24,9 +24,6 @@ int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen)
LTC_ARGCHK(omac != NULL);
LTC_ARGCHK(out != NULL);
LTC_ARGCHK(outlen != NULL);
if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) {
return err;
}
if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) ||
(omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) {
@@ -53,10 +50,10 @@ int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen)
}
/* encrypt it */
if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->block, &omac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(omac->block, omac->block, &omac->key)) != CRYPT_OK) {
return err;
}
cipher_descriptor[omac->cipher_idx].done(&omac->key);
ecb_done(&omac->key);
/* output it */
for (x = 0; x < (unsigned)omac->blklen && x < *outlen; x++) {

View File

@@ -51,7 +51,7 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l
default: return CRYPT_INVALID_ARG;
}
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &omac->key)) != CRYPT_OK) {
return err;
}
@@ -59,7 +59,7 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l
/* first calc L which is Ek(0) */
zeromem(omac->Lu[0], cipher_descriptor[cipher].block_length);
if ((err = cipher_descriptor[cipher].ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) {
return err;
}
@@ -81,7 +81,6 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l
}
/* setup state */
omac->cipher_idx = cipher;
omac->buflen = 0;
omac->blklen = len;
zeromem(omac->prev, sizeof(omac->prev));

View File

@@ -24,9 +24,6 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen)
LTC_ARGCHK(omac != NULL);
LTC_ARGCHK(in != NULL);
if ((err = cipher_is_valid(omac->cipher_idx)) != CRYPT_OK) {
return err;
}
if ((omac->buflen > (int)sizeof(omac->block)) || (omac->buflen < 0) ||
(omac->blklen > (int)sizeof(omac->block)) || (omac->buflen > omac->blklen)) {
@@ -34,23 +31,18 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen)
}
#ifdef LTC_FAST
{
unsigned long blklen = cipher_descriptor[omac->cipher_idx].block_length;
if (omac->buflen == 0 && inlen > blklen) {
unsigned long y;
for (x = 0; x < (inlen - blklen); x += blklen) {
for (y = 0; y < blklen; y += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST(&omac->prev[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&in[y]));
if (omac->buflen == 0 && inlen > (unsigned long)omac->blklen) {
for (x = 0; x < (inlen - omac->blklen); x += omac->blklen) {
for (n = 0; n < (unsigned long)omac->blklen; n += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST(&omac->prev[n])) ^= *(LTC_FAST_TYPE_PTR_CAST(&in[n]));
}
in += blklen;
if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) {
in += omac->blklen;
if ((err = ecb_encrypt_block(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) {
return err;
}
}
inlen -= x;
}
}
#endif
while (inlen != 0) {
@@ -59,7 +51,7 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen)
for (x = 0; x < (unsigned long)omac->blklen; x++) {
omac->block[x] ^= omac->prev[x];
}
if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->prev, &omac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(omac->block, omac->prev, &omac->key)) != CRYPT_OK) {
return err;
}
omac->buflen = 0;

View File

@@ -76,7 +76,7 @@ int omac_test(void)
}
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
len = sizeof(out);
if ((err = omac_memory(idx, tests[x].key, tests[x].keylen, tests[x].msg, tests[x].msglen, out, &len)) != CRYPT_OK) {
return err;

View File

@@ -80,7 +80,7 @@ int pelican_test(void)
unsigned char out[16];
pelican_state pel;
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
if ((err = pelican_init(&pel, tests[x].K, tests[x].keylen)) != CRYPT_OK) {
return err;
}

View File

@@ -15,9 +15,6 @@ int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen)
LTC_ARGCHK(pmac != NULL);
LTC_ARGCHK(out != NULL);
if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {
return err;
}
if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) ||
(pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) {
@@ -41,10 +38,10 @@ int pmac_done(pmac_state *pmac, unsigned char *out, unsigned long *outlen)
}
/* encrypt it */
if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(pmac->checksum, pmac->checksum, &pmac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(pmac->checksum, pmac->checksum, &pmac->key)) != CRYPT_OK) {
return err;
}
cipher_descriptor[pmac->cipher_idx].done(&pmac->key);
ecb_done(&pmac->key);
/* store it */
for (x = 0; x < pmac->block_len && x < (int)*outlen; x++) {

View File

@@ -55,12 +55,12 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
/* determine which polys to use */
pmac->block_len = cipher_descriptor[cipher].block_length;
for (poly = 0; poly < (int)(sizeof(polys)/sizeof(polys[0])); poly++) {
for (poly = 0; poly < (int)LTC_ARRAY_SIZE(polys); poly++) {
if (polys[poly].len == pmac->block_len) {
break;
}
}
if (poly >= (int)(sizeof(polys)/sizeof(polys[0]))) {
if (poly >= (int)LTC_ARRAY_SIZE(polys)) {
return CRYPT_INVALID_ARG;
}
if (polys[poly].len != pmac->block_len) {
@@ -75,7 +75,7 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
/* schedule the key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &pmac->key)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, &pmac->key)) != CRYPT_OK) {
return err;
}
@@ -87,7 +87,7 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
/* find L = E[0] */
zeromem(L, pmac->block_len);
if ((err = cipher_descriptor[cipher].ecb_encrypt(L, L, &pmac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(L, L, &pmac->key)) != CRYPT_OK) {
goto error;
}
@@ -124,7 +124,6 @@ int pmac_init(pmac_state *pmac, int cipher, const unsigned char *key, unsigned l
/* zero buffer, counters, etc... */
pmac->block_index = 1;
pmac->cipher_idx = cipher;
pmac->buflen = 0;
zeromem(pmac->block, sizeof(pmac->block));
zeromem(pmac->Li, sizeof(pmac->Li));

View File

@@ -14,6 +14,11 @@
*/
int pmac_ntz(unsigned long x)
{
#if defined(LTC_HAVE_CTZL_BUILTIN)
if (x == 0)
return sizeof(unsigned long) * CHAR_BIT;
return __builtin_ctzl(x);
#else
int c;
x &= 0xFFFFFFFFUL;
c = 0;
@@ -22,6 +27,7 @@ int pmac_ntz(unsigned long x)
x >>= 1;
}
return c;
#endif
}
#endif

View File

@@ -29,9 +29,6 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)
LTC_ARGCHK(pmac != NULL);
LTC_ARGCHK(in != NULL);
if ((err = cipher_is_valid(pmac->cipher_idx)) != CRYPT_OK) {
return err;
}
if ((pmac->buflen > (int)sizeof(pmac->block)) || (pmac->buflen < 0) ||
(pmac->block_len > (int)sizeof(pmac->block)) || (pmac->buflen > pmac->block_len)) {
@@ -46,7 +43,7 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)
for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST(&Z[y])) = *(LTC_FAST_TYPE_PTR_CAST(&in[y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&pmac->Li[y]));
}
if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(Z, Z, &pmac->key)) != CRYPT_OK) {
return err;
}
for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
@@ -65,7 +62,7 @@ int pmac_process(pmac_state *pmac, const unsigned char *in, unsigned long inlen)
for (x = 0; x < (unsigned long)pmac->block_len; x++) {
Z[x] = pmac->Li[x] ^ pmac->block[x];
}
if ((err = cipher_descriptor[pmac->cipher_idx].ecb_encrypt(Z, Z, &pmac->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(Z, Z, &pmac->key)) != CRYPT_OK) {
return err;
}
for (x = 0; x < (unsigned long)pmac->block_len; x++) {

View File

@@ -124,7 +124,7 @@ int pmac_test(void)
}
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
len = sizeof(outtag);
if ((err = pmac_memory(idx, tests[x].key, 16, tests[x].msg, tests[x].msglen, outtag, &len)) != CRYPT_OK) {
return err;

View File

@@ -17,17 +17,11 @@
*/
int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen)
{
int err, x;
int x;
LTC_ARGCHK(xcbc != NULL);
LTC_ARGCHK(out != NULL);
/* check structure */
if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) {
return err;
}
if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) ||
(xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
if ((xcbc->blocksize < 0) || (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
return CRYPT_INVALID_ARG;
}
@@ -46,8 +40,8 @@ int xcbc_done(xcbc_state *xcbc, unsigned char *out, unsigned long *outlen)
}
/* encrypt */
cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
cipher_descriptor[xcbc->cipher].done(&xcbc->key);
ecb_encrypt_block(xcbc->IV, xcbc->IV, &xcbc->key);
ecb_done(&xcbc->key);
/* extract tag */
for (x = 0; x < xcbc->blocksize && (unsigned long)x < *outlen; x++) {

View File

@@ -23,7 +23,7 @@
int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned long keylen)
{
int x, y, err;
symmetric_key *skey;
symmetric_ECB *skey;
unsigned long k1;
LTC_ARGCHK(xcbc != NULL);
@@ -64,7 +64,7 @@ int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned l
return CRYPT_MEM;
}
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, 0, skey)) != CRYPT_OK) {
goto done;
}
@@ -73,20 +73,19 @@ int xcbc_init(xcbc_state *xcbc, int cipher, const unsigned char *key, unsigned l
for (x = 0; x < cipher_descriptor[cipher].block_length; x++) {
xcbc->K[y][x] = y + 1;
}
cipher_descriptor[cipher].ecb_encrypt(xcbc->K[y], xcbc->K[y], skey);
ecb_encrypt_block(xcbc->K[y], xcbc->K[y], skey);
}
}
/* setup K1 */
err = cipher_descriptor[cipher].setup(xcbc->K[0], k1, 0, &xcbc->key);
err = ecb_start(cipher, xcbc->K[0], k1, 0, &xcbc->key);
/* setup struct */
zeromem(xcbc->IV, cipher_descriptor[cipher].block_length);
xcbc->blocksize = cipher_descriptor[cipher].block_length;
xcbc->cipher = cipher;
xcbc->buflen = 0;
done:
cipher_descriptor[cipher].done(skey);
ecb_done(skey);
if (skey != NULL) {
#ifdef LTC_CLEAN_STACK
zeromem(skey, sizeof(*skey));

View File

@@ -17,7 +17,6 @@
*/
int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen)
{
int err;
#ifdef LTC_FAST
int x;
#endif
@@ -25,13 +24,7 @@ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen)
LTC_ARGCHK(xcbc != NULL);
LTC_ARGCHK(in != NULL);
/* check structure */
if ((err = cipher_is_valid(xcbc->cipher)) != CRYPT_OK) {
return err;
}
if ((xcbc->blocksize > cipher_descriptor[xcbc->cipher].block_length) || (xcbc->blocksize < 0) ||
(xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
if ((xcbc->blocksize < 0) || (xcbc->buflen > xcbc->blocksize) || (xcbc->buflen < 0)) {
return CRYPT_INVALID_ARG;
}
@@ -41,7 +34,7 @@ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen)
for (x = 0; x < xcbc->blocksize; x += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST(&(xcbc->IV[x]))) ^= *(LTC_FAST_TYPE_PTR_CAST(&(in[x])));
}
cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
ecb_encrypt_block(xcbc->IV, xcbc->IV, &xcbc->key);
in += xcbc->blocksize;
inlen -= xcbc->blocksize;
}
@@ -50,7 +43,7 @@ int xcbc_process(xcbc_state *xcbc, const unsigned char *in, unsigned long inlen)
while (inlen) {
if (xcbc->buflen == xcbc->blocksize) {
cipher_descriptor[xcbc->cipher].ecb_encrypt(xcbc->IV, xcbc->IV, &xcbc->key);
ecb_encrypt_block(xcbc->IV, xcbc->IV, &xcbc->key);
xcbc->buflen = 0;
}
xcbc->IV[xcbc->buflen++] ^= *in++;

View File

@@ -98,7 +98,7 @@ int xcbc_test(void)
}
}
for (x = 0; x < (int)(sizeof(tests)/sizeof(tests[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tests); x++) {
taglen = 16;
if ((err = xcbc_memory(idx, tests[x].K, 16, tests[x].M, tests[x].msglen, T, &taglen)) != CRYPT_OK) {
return err;

View File

@@ -33,7 +33,7 @@ static int mpi_to_ltc_error(mp_err err)
{
size_t x;
for (x = 0; x < sizeof(mpi_to_ltc_codes)/sizeof(mpi_to_ltc_codes[0]); x++) {
for (x = 0; x < LTC_ARRAY_SIZE(mpi_to_ltc_codes); x++) {
if (err == mpi_to_ltc_codes[x].mpi_code) {
return mpi_to_ltc_codes[x].ltc_code;
}

View File

@@ -39,7 +39,7 @@ static int tfm_to_ltc_error(int err)
{
int x;
for (x = 0; x < (int)(sizeof(tfm_to_ltc_codes)/sizeof(tfm_to_ltc_codes[0])); x++) {
for (x = 0; x < (int)LTC_ARRAY_SIZE(tfm_to_ltc_codes); x++) {
if (err == tfm_to_ltc_codes[x].tfm_code) {
return tfm_to_ltc_codes[x].ltc_code;
}

View File

@@ -52,10 +52,11 @@ int base16_encode(const unsigned char *in, unsigned long inlen,
alphabet = alphabets[1];
}
for (i = 0; i < x; i += 2) {
out[i] = alphabet[(in[i/2] >> 4) & 0x0f];
out[i+1] = alphabet[in[i/2] & 0x0f];
for (i = x; i > 0; i -= 2) {
out[i-2] = alphabet[(in[(i-1)/2] >> 4) & 0x0f];
out[i-1] = alphabet[in[(i-1)/2] & 0x0f];
}
out[x] = '\0';
return CRYPT_OK;

View File

@@ -548,6 +548,15 @@ const char *crypt_build_settings =
" LTC_NO_ROLC "
#endif
#endif
#if defined(LTC_HAVE_ROTATE_BUILTIN)
" LTC_HAVE_ROTATE_BUILTIN "
#endif
#if defined(LTC_HAVE_CLZL_BUILTIN)
" LTC_HAVE_CLZL_BUILTIN "
#endif
#if defined(LTC_HAVE_CTZL_BUILTIN)
" LTC_HAVE_CTZL_BUILTIN "
#endif
#if defined(LTC_NO_TEST)
" LTC_NO_TEST "
#endif

View File

@@ -89,8 +89,13 @@ int register_all_hashes(void)
REGISTER_HASH(&blake2b_512_desc);
#endif
#ifdef LTC_CHC_HASH
{
int aes_index = find_cipher_any("aes", 8, 16);
if (aes_index != -1) {
REGISTER_HASH(&chc_desc);
LTC_ARGCHK(chc_register(find_cipher_any("aes", 8, 16)) == CRYPT_OK);
LTC_ARGCHK(chc_register(aes_index) == CRYPT_OK);
}
}
#endif
return CRYPT_OK;
}

View File

@@ -54,7 +54,7 @@ static const char * const err_2_str[CRYPT_ERR_NUM] =
"The PEM header was not recognized",
};
LTC_STATIC_ASSERT(correct_err_2_str_size, (sizeof(err_2_str)/sizeof(err_2_str[0])) == CRYPT_ERR_NUM)
LTC_STATIC_ASSERT(correct_err_2_str_size, LTC_ARRAY_SIZE(err_2_str) == CRYPT_ERR_NUM)
/**
Convert an LTC error code to ASCII

View File

@@ -51,7 +51,7 @@ static const oid_to_pbes s_pbes2_list[] = {
static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list *hmac_oid, pbes_properties *res)
{
unsigned int i;
for (i = 0; i < sizeof(s_pbes2_list)/sizeof(s_pbes2_list[0]); ++i) {
for (i = 0; i < LTC_ARRAY_SIZE(s_pbes2_list); ++i) {
if (pk_oid_cmp_with_asn1(s_pbes2_list[i].oid, cipher_oid) == CRYPT_OK) {
*res = *s_pbes2_list[i].data;
break;
@@ -59,7 +59,7 @@ static int s_pbes2_from_oid(const ltc_asn1_list *cipher_oid, const ltc_asn1_list
}
if (res->c == NULL) return CRYPT_INVALID_CIPHER;
if (hmac_oid != NULL) {
for (i = 0; i < sizeof(s_hmac_oid_names)/sizeof(s_hmac_oid_names[0]); ++i) {
for (i = 0; i < LTC_ARRAY_SIZE(s_hmac_oid_names); ++i) {
if (pk_oid_cmp_with_asn1(s_hmac_oid_names[i].oid, hmac_oid) == CRYPT_OK) {
res->h = s_hmac_oid_names[i].id;
return CRYPT_OK;

View File

@@ -69,7 +69,7 @@ const struct pem_header_id pem_std_headers[] = {
.pka = LTC_PKA_DSA,
},
};
const unsigned long pem_std_headers_num = sizeof(pem_std_headers)/sizeof(pem_std_headers[0]);
const unsigned long pem_std_headers_num = LTC_ARRAY_SIZE(pem_std_headers);
/* Encrypted PEM files */
const struct str pem_proc_type_encrypted = { SET_CSTR(, "Proc-Type: 4,ENCRYPTED") };
@@ -151,7 +151,7 @@ const struct blockcipher_info pem_dek_infos[] =
{ .name = "SEED-CFB,", .algo = "seed", .keylen = 128 / 8, .mode = cm_cfb, },
{ .name = "SEED-OFB,", .algo = "seed", .keylen = 128 / 8, .mode = cm_ofb, },
};
const unsigned long pem_dek_infos_num = sizeof(pem_dek_infos)/sizeof(pem_dek_infos[0]);
const unsigned long pem_dek_infos_num = LTC_ARRAY_SIZE(pem_dek_infos);
int pem_decrypt(unsigned char *data, unsigned long *datalen,
unsigned char *key, unsigned long keylen,
@@ -201,7 +201,7 @@ int pem_decrypt(unsigned char *data, unsigned long *datalen,
goto error_out;
}
if ((err = padding_depad(data, datalen, padding | s.ctx.cbc.blocklen)) != CRYPT_OK) {
if ((err = padding_depad(data, datalen, padding | s.ctx.cbc.ecb.blocklen)) != CRYPT_OK) {
goto error_out;
}
#else

View File

@@ -16,7 +16,7 @@
extern const struct pem_header_id pem_std_headers[];
extern const unsigned long pem_std_headers_num;
static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_headers *hdr)
static int s_decrypt_pem(unsigned char *asn1_cert, unsigned long *asn1_len, const struct pem_headers *hdr)
{
unsigned char iv[MAXBLOCKSIZE], key[MAXBLOCKSIZE];
unsigned long ivlen, klen;
@@ -38,7 +38,7 @@ static int s_decrypt_pem(unsigned char *pem, unsigned long *l, const struct pem_
return err;
}
err = pem_decrypt(pem, l, key, klen, iv, ivlen, NULL, 0, &hdr->info, LTC_PAD_PKCS7);
err = pem_decrypt(asn1_cert, asn1_len, key, klen, iv, ivlen, NULL, 0, &hdr->info, LTC_PAD_PKCS7);
zeromem(key, sizeof(key));
zeromem(iv, sizeof(iv));
@@ -86,12 +86,12 @@ static const import_fn s_import_x509_fns[LTC_PKA_NUM] = {
#endif
};
static int s_import_x509(unsigned char *pem, unsigned long l, ltc_pka_key *k)
static int s_import_x509(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k)
{
enum ltc_pka_id pka = LTC_PKA_UNDEF;
ltc_asn1_list *d, *spki;
int err;
if ((err = x509_decode_spki(pem, l, &d, &spki)) != CRYPT_OK) {
if ((err = x509_decode_spki(asn1_cert, asn1_len, &d, &spki)) != CRYPT_OK) {
return err;
}
err = s_get_pka(spki, &pka);
@@ -100,23 +100,23 @@ static int s_import_x509(unsigned char *pem, unsigned long l, ltc_pka_key *k)
return err;
}
if (pka < 0
|| pka > sizeof(s_import_x509_fns)/sizeof(s_import_x509_fns[0])
|| pka > LTC_ARRAY_SIZE(s_import_x509_fns)
|| s_import_x509_fns[pka] == NULL) {
return CRYPT_PK_INVALID_TYPE;
}
if ((err = s_import_x509_fns[pka](pem, l, &k->u)) == CRYPT_OK) {
if ((err = s_import_x509_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) {
k->id = pka;
}
return err;
}
static int s_import_pkcs8(unsigned char *pem, unsigned long l, ltc_pka_key *k, const password_ctx *pw_ctx)
static int s_import_pkcs8(unsigned char *asn1_cert, unsigned long asn1_len, ltc_pka_key *k, const password_ctx *pw_ctx)
{
int err;
enum ltc_oid_id pka;
ltc_asn1_list *alg_id, *priv_key;
ltc_asn1_list *p8_asn1 = NULL;
if ((err = pkcs8_decode_flexi(pem, l, pw_ctx, &p8_asn1)) != CRYPT_OK) {
if ((err = pkcs8_decode_flexi(asn1_cert, asn1_len, pw_ctx, &p8_asn1)) != CRYPT_OK) {
goto cleanup;
}
if ((err = pkcs8_get_children(p8_asn1, &pka, &alg_id, &priv_key)) != CRYPT_OK) {
@@ -168,11 +168,11 @@ cleanup:
return err;
}
static int s_extract_pka(unsigned char *pem, unsigned long w, enum ltc_pka_id *pka)
static int s_extract_pka(unsigned char *asn1_cert, unsigned long asn1_len, enum ltc_pka_id *pka)
{
ltc_asn1_list *pub;
int err = CRYPT_ERROR;
if ((err = der_decode_sequence_flexi(pem, &w, &pub)) != CRYPT_OK) {
if ((err = der_decode_sequence_flexi(asn1_cert, &asn1_len, &pub)) != CRYPT_OK) {
return err;
}
err = s_get_pka(pub, pka);
@@ -198,8 +198,8 @@ static const import_fn s_import_openssl_fns[LTC_PKA_NUM] = {
static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx)
{
unsigned char *pem = NULL;
unsigned long w, l, n;
unsigned char *asn1_cert = NULL;
unsigned long w, asn1_len, n;
int err = CRYPT_ERROR;
struct pem_headers hdr = { 0 };
struct password pw = { 0 };
@@ -207,10 +207,10 @@ static int s_decode(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_c
XMEMSET(k, 0, sizeof(*k));
w = LTC_PEM_READ_BUFSIZE * 2;
retry:
pem = XREALLOC(pem, w);
asn1_cert = XREALLOC(asn1_cert, w);
for (n = 0; n < pem_std_headers_num; ++n) {
hdr.id = &pem_std_headers[n];
err = pem_read(pem, &w, &hdr, g);
err = pem_read(asn1_cert, &w, &hdr, g);
if (err == CRYPT_BUFFER_OVERFLOW) {
goto retry;
} else if (err == CRYPT_OK) {
@@ -223,15 +223,15 @@ retry:
/* id not found */
if (hdr.id == NULL)
goto cleanup;
l = w;
asn1_len = w;
if (hdr.id->flags & pf_pkcs8) {
err = s_import_pkcs8(pem, l, k, pw_ctx);
err = s_import_pkcs8(asn1_cert, asn1_len, k, pw_ctx);
goto cleanup;
} else if (hdr.id->flags == pf_x509) {
err = s_import_x509(pem, l, k);
err = s_import_x509(asn1_cert, asn1_len, k);
goto cleanup;
} else if ((hdr.id->flags & pf_public) && hdr.id->pka == LTC_PKA_UNDEF) {
if ((err = s_extract_pka(pem, w, &pka)) != CRYPT_OK) {
if ((err = s_extract_pka(asn1_cert, asn1_len, &pka)) != CRYPT_OK) {
goto cleanup;
}
} else if (hdr.encrypted) {
@@ -246,7 +246,7 @@ retry:
goto cleanup;
}
if ((err = s_decrypt_pem(pem, &l, &hdr)) != CRYPT_OK) {
if ((err = s_decrypt_pem(asn1_cert, &asn1_len, &hdr)) != CRYPT_OK) {
goto cleanup;
}
pka = hdr.id->pka;
@@ -255,18 +255,18 @@ retry:
}
if (pka < 0
|| pka > sizeof(s_import_openssl_fns)/sizeof(s_import_openssl_fns[0])
|| pka > LTC_ARRAY_SIZE(s_import_openssl_fns)
|| s_import_openssl_fns[pka] == NULL) {
err = CRYPT_PK_INVALID_TYPE;
goto cleanup;
}
if ((err = s_import_openssl_fns[pka](pem, l, &k->u)) == CRYPT_OK) {
if ((err = s_import_openssl_fns[pka](asn1_cert, asn1_len, &k->u)) == CRYPT_OK) {
k->id = pka;
}
cleanup:
password_free(hdr.pw, pw_ctx);
XFREE(pem);
XFREE(asn1_cert);
return err;
}

View File

@@ -62,10 +62,10 @@ static void s_tts(char *buf, unsigned long *buflen)
}
}
static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g)
static char* s_get_line_i(char *buf, unsigned long *buflen, struct get_char *g, int search_for_start)
{
unsigned long blen = 0;
int c = -1, c_;
unsigned long blen = 0, wr = 0;
int c_;
if (g->unget_buf.p) {
if (*buflen < g->unget_buf.len) {
return NULL;
@@ -75,30 +75,44 @@ static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g)
RESET_STR(g->unget_buf);
return buf;
}
while(blen < *buflen) {
c_ = c;
c = g->get(g);
if (c == '\n') {
buf[blen] = '\0';
if (g->prev_get == -1) {
return NULL;
}
while(blen < *buflen || search_for_start) {
wr = blen < *buflen ? blen : *buflen - 1;
c_ = g->prev_get;
g->prev_get = g->get(g);
if (g->prev_get == '\n') {
buf[wr] = '\0';
if (c_ == '\r') {
buf[--blen] = '\0';
buf[--wr] = '\0';
}
s_tts(buf, &blen);
*buflen = blen;
s_tts(buf, &wr);
*buflen = wr;
return buf;
}
if (c == -1 || c == '\0') {
buf[blen] = '\0';
s_tts(buf, &blen);
*buflen = blen;
if (g->prev_get == -1 || g->prev_get == '\0') {
buf[wr] = '\0';
s_tts(buf, &wr);
*buflen = wr;
return buf;
}
buf[blen] = c;
buf[wr] = g->prev_get;
blen++;
}
return NULL;
}
LTC_INLINE static char* s_get_first_line(char *buf, unsigned long *buflen, struct get_char *g)
{
return s_get_line_i(buf, buflen, g, 1);
}
LTC_INLINE static char* s_get_line(char *buf, unsigned long *buflen, struct get_char *g)
{
return s_get_line_i(buf, buflen, g, 0);
}
static LTC_INLINE int s_fits_buf(void *dest, unsigned long to_write, void *end)
{
unsigned char *d = dest;
@@ -176,20 +190,29 @@ static int s_pem_decode_headers(struct pem_headers *hdr, struct get_char *g)
return CRYPT_OK;
}
int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_char *g)
int pem_read(void *asn1_cert, unsigned long *asn1_len, struct pem_headers *hdr, struct get_char *g)
{
char buf[LTC_PEM_DECODE_BUFSZ];
char *wpem = pem;
char *end = wpem + *w;
char *wpem = asn1_cert;
char *end = wpem + *asn1_len;
const char pem_start[] = "----";
unsigned long slen, linelen;
int err, hdr_ok = 0;
int would_overflow = 0;
unsigned char empty_lines = 0;
g->prev_get = 0;
do {
linelen = sizeof(buf);
if (s_get_line(buf, &linelen, g) == NULL) {
if (s_get_first_line(buf, &linelen, g) == NULL) {
if (g->prev_get == -1)
return CRYPT_NOP;
else
return CRYPT_INVALID_PACKET;
}
if (linelen < sizeof(pem_start) - 1)
continue;
} while(XMEMCMP(buf, pem_start, sizeof(pem_start) - 1) != 0);
if (hdr->id->start.len != linelen || XMEMCMP(buf, hdr->id->start.p, hdr->id->start.len)) {
s_unget_line(buf, linelen, g);
return CRYPT_UNKNOWN_PEM;
@@ -226,16 +249,16 @@ int pem_read(void *pem, unsigned long *w, struct pem_headers *hdr, struct get_ch
/* NUL termination */
wpem++;
/* prevent a wrap-around */
if (wpem < (char*)pem)
if (wpem < (char*)asn1_cert)
return CRYPT_OVERFLOW;
*w = wpem - (char*)pem;
*asn1_len = wpem - (char*)asn1_cert;
return CRYPT_BUFFER_OVERFLOW;
}
*w = wpem - (char*)pem;
*asn1_len = wpem - (char*)asn1_cert;
*wpem++ = '\0';
if ((err = base64_strict_decode(pem, *w, pem, w)) != CRYPT_OK) {
if ((err = base64_strict_decode(asn1_cert, *asn1_len, asn1_cert, asn1_len)) != CRYPT_OK) {
return err;
}
return CRYPT_OK;

View File

@@ -51,7 +51,7 @@ const struct blockcipher_info ssh_ciphers[] =
{ .name = "twofish256-cbc", .algo = "twofish", .keylen = 256 / 8, .mode = cm_cbc },
{ .name = "twofish256-ctr", .algo = "twofish", .keylen = 256 / 8, .mode = cm_ctr },
};
const unsigned long ssh_ciphers_num = sizeof(ssh_ciphers)/sizeof(ssh_ciphers[0]);
const unsigned long ssh_ciphers_num = LTC_ARRAY_SIZE(ssh_ciphers);
struct kdf_options {
const char *name;
@@ -402,7 +402,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k
remaining -= cur_len;
cur_len = remaining;
for (n = 0; n < sizeof(ssh_pkas)/sizeof(ssh_pkas[0]); ++n) {
for (n = 0; n < LTC_ARRAY_SIZE(ssh_pkas); ++n) {
if (ssh_pkas[n].name.p != NULL) {
if (pkalen != ssh_pkas[n].name.len
|| XMEMCMP(pka, ssh_pkas[n].name.p, ssh_pkas[n].name.len) != 0) continue;
@@ -415,7 +415,7 @@ static int s_decode_key(const unsigned char *in, unsigned long *inlen, ltc_pka_k
}
break;
}
if (n == sizeof(ssh_pkas)/sizeof(ssh_pkas[0])) {
if (n == LTC_ARRAY_SIZE(ssh_pkas)) {
return CRYPT_PK_INVALID_TYPE;
}
@@ -490,7 +490,7 @@ static int s_parse_line(char *line, unsigned long *len, ltc_pka_key *key, char *
rlen = *len;
/* Chop up string into the three authorized_keys_elements */
for (n = 0; n < sizeof(elements)/sizeof(elements[0]) && rlen; ++n) {
for (n = 0; n < LTC_ARRAY_SIZE(elements) && rlen; ++n) {
skip_spaces(&r, &rlen);
elements[n].p = r;
if (n != 2)
@@ -502,7 +502,7 @@ static int s_parse_line(char *line, unsigned long *len, ltc_pka_key *key, char *
r++;
}
for (n = 0; n < sizeof(ssh_pkas)/sizeof(ssh_pkas[0]); ++n) {
for (n = 0; n < LTC_ARRAY_SIZE(ssh_pkas); ++n) {
if (ssh_pkas[n].name.p != NULL) {
if (elements[ake_algo_name].len != ssh_pkas[n].name.len
|| XMEMCMP(elements[ake_algo_name].p, ssh_pkas[n].name.p, ssh_pkas[n].name.len) != 0) continue;
@@ -711,7 +711,7 @@ static const struct pem_header_id pem_openssh[] = {
.flags = pf_public
},
};
static const unsigned long pem_openssh_num = sizeof(pem_openssh)/sizeof(pem_openssh[0]);
static const unsigned long pem_openssh_num = LTC_ARRAY_SIZE(pem_openssh);
static int s_decode_openssh(struct get_char *g, ltc_pka_key *k, const password_ctx *pw_ctx)
{
@@ -819,9 +819,11 @@ int ssh_read_authorized_keys_filehandle(FILE *f, ssh_authorized_key_cb cb, void
LTC_ARGCHK(f != NULL);
LTC_ARGCHK(cb != NULL);
fseek(f, 0, SEEK_END);
if (fseek(f, 0, SEEK_END) == -1)
return CRYPT_ERROR;
tot_data = ftell(f);
rewind(f);
if (fseek(f, 0, SEEK_SET) == -1)
return CRYPT_ERROR;
buf = XMALLOC(tot_data);
if (buf == NULL) {
return CRYPT_MEM;

View File

@@ -32,51 +32,51 @@ int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(cbc != NULL);
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
if ((err = cipher_is_valid(cbc->ecb.cipher)) != CRYPT_OK) {
return err;
}
/* is blocklen valid? */
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV) || cbc->blocklen > (int)sizeof(tmp)) {
if (cbc->ecb.blocklen < 1 || cbc->ecb.blocklen > (int)sizeof(cbc->IV) || cbc->ecb.blocklen > (int)sizeof(tmp)) {
return CRYPT_INVALID_ARG;
}
if (len % cbc->blocklen) {
if (len % cbc->ecb.blocklen) {
return CRYPT_INVALID_ARG;
}
#ifdef LTC_FAST
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
if (cbc->ecb.blocklen % sizeof(LTC_FAST_TYPE)) {
return CRYPT_INVALID_ARG;
}
#endif
if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
if (cipher_descriptor[cbc->ecb.cipher].accel_cbc_decrypt != NULL) {
return cipher_descriptor[cbc->ecb.cipher].accel_cbc_decrypt(ct, pt, len / cbc->ecb.blocklen, cbc->IV, &cbc->ecb.key);
}
while (len) {
/* decrypt */
if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
if ((err = ecb_decrypt_block(ct, tmp, &cbc->ecb)) != CRYPT_OK) {
return err;
}
/* xor IV against plaintext */
#if defined(LTC_FAST)
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) {
tmpy = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^ *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)tmp + x));
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x)) = tmpy;
}
#else
for (x = 0; x < cbc->blocklen; x++) {
for (x = 0; x < cbc->ecb.blocklen; x++) {
tmpy = tmp[x] ^ cbc->IV[x];
cbc->IV[x] = ct[x];
pt[x] = tmpy;
}
#endif
ct += cbc->blocklen;
pt += cbc->blocklen;
len -= cbc->blocklen;
ct += cbc->ecb.blocklen;
pt += cbc->ecb.blocklen;
len -= cbc->ecb.blocklen;
}
return CRYPT_OK;
}

View File

@@ -15,14 +15,9 @@
*/
int cbc_done(symmetric_CBC *cbc)
{
int err;
LTC_ARGCHK(cbc != NULL);
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
return err;
}
cipher_descriptor[cbc->cipher].done(&cbc->key);
return CRYPT_OK;
return ecb_done(&cbc->ecb);
}

View File

@@ -26,58 +26,58 @@ int cbc_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(cbc != NULL);
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
if ((err = cipher_is_valid(cbc->ecb.cipher)) != CRYPT_OK) {
return err;
}
/* is blocklen valid? */
if (cbc->blocklen < 1 || cbc->blocklen > (int)sizeof(cbc->IV)) {
if (cbc->ecb.blocklen < 1 || cbc->ecb.blocklen > (int)sizeof(cbc->IV)) {
return CRYPT_INVALID_ARG;
}
if (len % cbc->blocklen) {
if (len % cbc->ecb.blocklen) {
return CRYPT_INVALID_ARG;
}
#ifdef LTC_FAST
if (cbc->blocklen % sizeof(LTC_FAST_TYPE)) {
if (cbc->ecb.blocklen % sizeof(LTC_FAST_TYPE)) {
return CRYPT_INVALID_ARG;
}
#endif
if (cipher_descriptor[cbc->cipher].accel_cbc_encrypt != NULL) {
return cipher_descriptor[cbc->cipher].accel_cbc_encrypt(pt, ct, len / cbc->blocklen, cbc->IV, &cbc->key);
if (cipher_descriptor[cbc->ecb.cipher].accel_cbc_encrypt != NULL) {
return cipher_descriptor[cbc->ecb.cipher].accel_cbc_encrypt(pt, ct, len / cbc->ecb.blocklen, cbc->IV, &cbc->ecb.key);
}
while (len) {
/* xor IV against plaintext */
#if defined(LTC_FAST)
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) ^= *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)pt + x));
}
#else
for (x = 0; x < cbc->blocklen; x++) {
for (x = 0; x < cbc->ecb.blocklen; x++) {
cbc->IV[x] ^= pt[x];
}
#endif
/* encrypt */
if ((err = cipher_descriptor[cbc->cipher].ecb_encrypt(cbc->IV, ct, &cbc->key)) != CRYPT_OK) {
if ((err = ecb_encrypt_block(cbc->IV, ct, &cbc->ecb)) != CRYPT_OK) {
return err;
}
/* store IV [ciphertext] for a future block */
#if defined(LTC_FAST)
for (x = 0; x < cbc->blocklen; x += sizeof(LTC_FAST_TYPE)) {
for (x = 0; x < cbc->ecb.blocklen; x += sizeof(LTC_FAST_TYPE)) {
*(LTC_FAST_TYPE_PTR_CAST((unsigned char *)cbc->IV + x)) = *(LTC_FAST_TYPE_PTR_CAST((unsigned char *)ct + x));
}
#else
for (x = 0; x < cbc->blocklen; x++) {
for (x = 0; x < cbc->ecb.blocklen; x++) {
cbc->IV[x] = ct[x];
}
#endif
ct += cbc->blocklen;
pt += cbc->blocklen;
len -= cbc->blocklen;
ct += cbc->ecb.blocklen;
pt += cbc->ecb.blocklen;
len -= cbc->ecb.blocklen;
}
return CRYPT_OK;
}

View File

@@ -21,12 +21,12 @@ int cbc_getiv(unsigned char *IV, unsigned long *len, const symmetric_CBC *cbc)
LTC_ARGCHK(IV != NULL);
LTC_ARGCHK(len != NULL);
LTC_ARGCHK(cbc != NULL);
if ((unsigned long)cbc->blocklen > *len) {
*len = cbc->blocklen;
if ((unsigned long)cbc->ecb.blocklen > *len) {
*len = cbc->ecb.blocklen;
return CRYPT_BUFFER_OVERFLOW;
}
XMEMCPY(IV, cbc->IV, cbc->blocklen);
*len = cbc->blocklen;
XMEMCPY(IV, cbc->IV, cbc->ecb.blocklen);
*len = cbc->ecb.blocklen;
return CRYPT_OK;
}

View File

@@ -21,7 +21,7 @@ int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc)
{
LTC_ARGCHK(IV != NULL);
LTC_ARGCHK(cbc != NULL);
if (len != (unsigned long)cbc->blocklen) {
if (len != (unsigned long)cbc->ecb.blocklen) {
return CRYPT_INVALID_ARG;
}
XMEMCPY(cbc->IV, IV, len);

View File

@@ -28,20 +28,13 @@ int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(cbc != NULL);
/* bad param? */
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
/* setup cipher */
if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cbc->key)) != CRYPT_OK) {
if ((err = ecb_start(cipher, key, keylen, num_rounds, &cbc->ecb)) != CRYPT_OK) {
return err;
}
/* copy IV */
cbc->blocklen = cipher_descriptor[cipher].block_length;
cbc->cipher = cipher;
for (x = 0; x < cbc->blocklen; x++) {
for (x = 0; x < cbc->ecb.blocklen; x++) {
cbc->IV[x] = IV[x];
}
return CRYPT_OK;

View File

@@ -57,12 +57,12 @@ int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
return CRYPT_OVERFLOW;
}
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
if ((err = cipher_is_valid(cfb->ecb.cipher)) != CRYPT_OK) {
return err;
}
/* is blocklen/padlen valid? */
if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
if (cfb->ecb.blocklen < 0 || cfb->ecb.blocklen > (int)sizeof(cfb->IV) ||
cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
return CRYPT_INVALID_ARG;
}
@@ -70,8 +70,8 @@ int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
bits_per_round = cfb->width == 1 ? 1 : 8;
while (bitlen > 0) {
if (cfb->padlen == cfb->blocklen) {
if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) {
if (cfb->padlen == cfb->ecb.blocklen) {
if ((err = ecb_encrypt_block(cfb->pad, cfb->IV, &cfb->ecb)) != CRYPT_OK) {
return err;
}
cfb->padlen = 0;
@@ -85,22 +85,22 @@ int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
ct_ <<= 1;
pt_ <<= 1;
}
if (cfb->blocklen == 16)
if (cfb->ecb.blocklen == 16)
s_shift1left_128(cfb->pad, ct_ >> 7);
else
s_shift1left_64(cfb->pad, ct_ >> 7);
pt_ |= ((ct_ ^ cfb->IV[0]) >> 7) & 0x01u;
cfb->padlen = cfb->blocklen;
cfb->padlen = cfb->ecb.blocklen;
if (cur_bit % 8 == 0) {
*pt++ = pt_;
cur_bit = 0;
}
break;
case 8:
XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->blocklen - 1);
cfb->pad[cfb->blocklen - 1] = *ct;
XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->ecb.blocklen - 1);
cfb->pad[cfb->ecb.blocklen - 1] = *ct;
*pt++ = *ct++ ^ cfb->IV[0];
cfb->padlen = cfb->blocklen;
cfb->padlen = cfb->ecb.blocklen;
break;
case 64:
case 128:

View File

@@ -15,14 +15,9 @@
*/
int cfb_done(symmetric_CFB *cfb)
{
int err;
LTC_ARGCHK(cfb != NULL);
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
return err;
}
cipher_descriptor[cfb->cipher].done(&cfb->key);
return CRYPT_OK;
return ecb_done(&cfb->ecb);
}

View File

@@ -57,12 +57,12 @@ int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s
return CRYPT_OVERFLOW;
}
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
if ((err = cipher_is_valid(cfb->ecb.cipher)) != CRYPT_OK) {
return err;
}
/* is blocklen/padlen valid? */
if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
if (cfb->ecb.blocklen < 0 || cfb->ecb.blocklen > (int)sizeof(cfb->IV) ||
cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
return CRYPT_INVALID_ARG;
}
@@ -70,8 +70,8 @@ int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s
bits_per_round = cfb->width == 1 ? 1 : 8;
while (bitlen > 0) {
if (cfb->padlen == cfb->blocklen) {
if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) {
if (cfb->padlen == cfb->ecb.blocklen) {
if ((err = ecb_encrypt_block(cfb->pad, cfb->IV, &cfb->ecb)) != CRYPT_OK) {
return err;
}
cfb->padlen = 0;
@@ -86,22 +86,22 @@ int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, s
ct_ <<= 1;
}
ct_ |= ((pt_ ^ cfb->IV[0]) >> 7) & 0x01u;
if (cfb->blocklen == 16)
if (cfb->ecb.blocklen == 16)
s_shift1left_128(cfb->pad, ct_);
else
s_shift1left_64(cfb->pad, ct_);
cfb->padlen = cfb->blocklen;
cfb->padlen = cfb->ecb.blocklen;
if (cur_bit % 8 == 0) {
*ct++ = ct_;
cur_bit = 0;
}
break;
case 8:
XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->blocklen - 1);
cfb->pad[cfb->blocklen - 1] = (*ct = *pt ^ cfb->IV[0]);
XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->ecb.blocklen - 1);
cfb->pad[cfb->ecb.blocklen - 1] = (*ct = *pt ^ cfb->IV[0]);
++pt;
++ct;
cfb->padlen = cfb->blocklen;
cfb->padlen = cfb->ecb.blocklen;
break;
case 64:
case 128:

View File

@@ -21,12 +21,12 @@ int cfb_getiv(unsigned char *IV, unsigned long *len, const symmetric_CFB *cfb)
LTC_ARGCHK(IV != NULL);
LTC_ARGCHK(len != NULL);
LTC_ARGCHK(cfb != NULL);
if ((unsigned long)cfb->blocklen > *len) {
*len = cfb->blocklen;
if ((unsigned long)cfb->ecb.blocklen > *len) {
*len = cfb->ecb.blocklen;
return CRYPT_BUFFER_OVERFLOW;
}
XMEMCPY(IV, cfb->pad, cfb->blocklen);
*len = cfb->blocklen;
XMEMCPY(IV, cfb->pad, cfb->ecb.blocklen);
*len = cfb->ecb.blocklen;
return CRYPT_OK;
}

Some files were not shown because too many files have changed in this diff Show More