Upgrade libtomcrypt

This commit is contained in:
2025-10-24 19:33:21 +03:00
parent d770dd8df3
commit acc69bb8ad
157 changed files with 922 additions and 761 deletions

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,