added libtomcrypt-1.00
This commit is contained in:
committed by
Steffen Jaeckel
parent
1c1822d510
commit
bfc2f5b078
@@ -0,0 +1,69 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_decrypt.c
|
||||
CBC implementation, decrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CBC
|
||||
|
||||
/**
|
||||
CBC decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param cbc CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
unsigned char tmp[MAXBLOCKSIZE], tmp2[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
|
||||
/* decrypt the block from ct into tmp */
|
||||
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
LTC_ARGCHK(cipher_descriptor[cbc->cipher].ecb_decrypt != NULL);
|
||||
|
||||
/* is blocklen valid? */
|
||||
if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* decrypt and xor IV against the plaintext of the previous step */
|
||||
cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key);
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
/* copy CT in case ct == pt */
|
||||
tmp2[x] = ct[x];
|
||||
|
||||
/* actually decrypt the byte */
|
||||
pt[x] = tmp[x] ^ cbc->IV[x];
|
||||
}
|
||||
|
||||
/* replace IV with this current ciphertext */
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] = tmp2[x];
|
||||
}
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
zeromem(tmp2, sizeof(tmp2));
|
||||
#endif
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_encrypt.c
|
||||
CBC implementation, encrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef CBC
|
||||
|
||||
/**
|
||||
CBC encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param cbc CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen valid? */
|
||||
if (cbc->blocklen < 0 || cbc->blocklen > (int)sizeof(cbc->IV)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* xor IV against plaintext */
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
tmp[x] = pt[x] ^ cbc->IV[x];
|
||||
}
|
||||
|
||||
/* encrypt */
|
||||
cipher_descriptor[cbc->cipher].ecb_encrypt(tmp, ct, &cbc->key);
|
||||
|
||||
/* store IV [ciphertext] for a future block */
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] = ct[x];
|
||||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_getiv.c
|
||||
CBC implementation, get IV, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CBC
|
||||
|
||||
/**
|
||||
Get the current initial vector
|
||||
@param IV [out] The destination of the initial vector
|
||||
@param len [in/out] The max size and resulting size of the initial vector
|
||||
@param cbc The CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc)
|
||||
{
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(len != NULL);
|
||||
LTC_ARGCHK(cbc != NULL);
|
||||
if ((unsigned long)cbc->blocklen > *len) {
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
XMEMCPY(IV, cbc->IV, cbc->blocklen);
|
||||
*len = cbc->blocklen;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,40 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_setiv.c
|
||||
CBC implementation, set IV, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef CBC
|
||||
|
||||
/**
|
||||
Set an initial vector
|
||||
@param IV The initial vector
|
||||
@param len The length of the vector (in octets)
|
||||
@param cbc The CBC state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
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) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
XMEMCPY(cbc->IV, IV, len);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cbc_start.c
|
||||
CBC implementation, start chain, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CBC
|
||||
|
||||
/**
|
||||
Initialize a CBC context
|
||||
@param cipher The index of the cipher desired
|
||||
@param IV The initial vector
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param num_rounds Number of rounds in the cipher desired (0 for default)
|
||||
@param cbc The CBC state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key,
|
||||
int keylen, int num_rounds, symmetric_CBC *cbc)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
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) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* copy IV */
|
||||
cbc->blocklen = cipher_descriptor[cipher].block_length;
|
||||
cbc->cipher = cipher;
|
||||
for (x = 0; x < cbc->blocklen; x++) {
|
||||
cbc->IV[x] = IV[x];
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cfb_decrypt.c
|
||||
CFB implementation, decrypt data, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CFB
|
||||
|
||||
/**
|
||||
CFB decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param len Length of ciphertext (octets)
|
||||
@param cfb CFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cfb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen/padlen valid? */
|
||||
if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
|
||||
cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
while (len-- > 0) {
|
||||
if (cfb->padlen == cfb->blocklen) {
|
||||
cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key);
|
||||
cfb->padlen = 0;
|
||||
}
|
||||
cfb->pad[cfb->padlen] = *ct;
|
||||
*pt = *ct ^ cfb->IV[cfb->padlen];
|
||||
++pt;
|
||||
++ct;
|
||||
++cfb->padlen;
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cfb_encrypt.c
|
||||
CFB implementation, encrypt data, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CFB
|
||||
|
||||
/**
|
||||
CFB encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param len Length of plaintext (octets)
|
||||
@param cfb CFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(cfb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen/padlen valid? */
|
||||
if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
|
||||
cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
while (len-- > 0) {
|
||||
if (cfb->padlen == cfb->blocklen) {
|
||||
cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key);
|
||||
cfb->padlen = 0;
|
||||
}
|
||||
cfb->pad[cfb->padlen] = (*ct = *pt ^ cfb->IV[cfb->padlen]);
|
||||
++pt;
|
||||
++ct;
|
||||
++cfb->padlen;
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cfb_getiv.c
|
||||
CFB implementation, get IV, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CFB
|
||||
|
||||
/**
|
||||
Get the current initial vector
|
||||
@param IV [out] The destination of the initial vector
|
||||
@param len [in/out] The max size and resulting size of the initial vector
|
||||
@param cfb The CFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb)
|
||||
{
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(len != NULL);
|
||||
LTC_ARGCHK(cfb != NULL);
|
||||
if ((unsigned long)cfb->blocklen > *len) {
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
XMEMCPY(IV, cfb->IV, cfb->blocklen);
|
||||
*len = cfb->blocklen;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cfb_setiv.c
|
||||
CFB implementation, set IV, Tom St Denis
|
||||
*/
|
||||
#ifdef CFB
|
||||
|
||||
/**
|
||||
Set an initial vector
|
||||
@param IV The initial vector
|
||||
@param len The length of the vector (in octets)
|
||||
@param cfb The CFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(cfb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (len != (unsigned long)cfb->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* force next block */
|
||||
cfb->padlen = 0;
|
||||
cipher_descriptor[cfb->cipher].ecb_encrypt(IV, cfb->IV, &cfb->key);
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file cfb_start.c
|
||||
CFB implementation, start chain, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef CFB
|
||||
|
||||
/**
|
||||
Initialize a CFB context
|
||||
@param cipher The index of the cipher desired
|
||||
@param IV The initial vector
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param num_rounds Number of rounds in the cipher desired (0 for default)
|
||||
@param cfb The CFB state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
|
||||
int keylen, int num_rounds, symmetric_CFB *cfb)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(cfb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
/* copy data */
|
||||
cfb->cipher = cipher;
|
||||
cfb->blocklen = cipher_descriptor[cipher].block_length;
|
||||
for (x = 0; x < cfb->blocklen; x++)
|
||||
cfb->IV[x] = IV[x];
|
||||
|
||||
/* init the cipher */
|
||||
if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* encrypt the IV */
|
||||
cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->IV, cfb->IV, &cfb->key);
|
||||
cfb->padlen = 0;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ctr_decrypt.c
|
||||
CTR implementation, decrypt data, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CTR
|
||||
|
||||
/**
|
||||
CTR decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param len Length of ciphertext (octets)
|
||||
@param ctr CTR state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr)
|
||||
{
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(ctr != NULL);
|
||||
|
||||
return ctr_encrypt(ct, pt, len, ctr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ctr_encrypt.c
|
||||
CTR implementation, encrypt data, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef CTR
|
||||
|
||||
/**
|
||||
CTR encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param len Length of plaintext (octets)
|
||||
@param ctr CTR state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(ctr != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen/padlen valid? */
|
||||
if (ctr->blocklen < 0 || ctr->blocklen > (int)sizeof(ctr->ctr) ||
|
||||
ctr->padlen < 0 || ctr->padlen > (int)sizeof(ctr->pad)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
while (len-- > 0) {
|
||||
/* is the pad empty? */
|
||||
if (ctr->padlen == ctr->blocklen) {
|
||||
/* increment counter */
|
||||
if (ctr->mode == 0) {
|
||||
/* little-endian */
|
||||
for (x = 0; x < ctr->blocklen; x++) {
|
||||
ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
|
||||
if (ctr->ctr[x] != (unsigned char)0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* big-endian */
|
||||
for (x = ctr->blocklen-1; x >= 0; x--) {
|
||||
ctr->ctr[x] = (ctr->ctr[x] + (unsigned char)1) & (unsigned char)255;
|
||||
if (ctr->ctr[x] != (unsigned char)0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* encrypt it */
|
||||
cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
|
||||
ctr->padlen = 0;
|
||||
}
|
||||
*ct++ = *pt++ ^ ctr->pad[ctr->padlen++];
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ctr_getiv.c
|
||||
CTR implementation, get IV, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CTR
|
||||
|
||||
/**
|
||||
Get the current initial vector
|
||||
@param IV [out] The destination of the initial vector
|
||||
@param len [in/out] The max size and resulting size of the initial vector
|
||||
@param ctr The CTR state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr)
|
||||
{
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(len != NULL);
|
||||
LTC_ARGCHK(ctr != NULL);
|
||||
if ((unsigned long)ctr->blocklen > *len) {
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
XMEMCPY(IV, ctr->ctr, ctr->blocklen);
|
||||
*len = ctr->blocklen;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ctr_setiv.c
|
||||
CTR implementation, set IV, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef CTR
|
||||
|
||||
/**
|
||||
Set an initial vector
|
||||
@param IV The initial vector
|
||||
@param len The length of the vector (in octets)
|
||||
@param ctr The CTR state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(ctr != NULL);
|
||||
|
||||
/* bad param? */
|
||||
if ((err = cipher_is_valid(ctr->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (len != (unsigned long)ctr->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* set IV */
|
||||
XMEMCPY(ctr->ctr, IV, len);
|
||||
|
||||
/* force next block */
|
||||
ctr->padlen = 0;
|
||||
cipher_descriptor[ctr->cipher].ecb_encrypt(IV, ctr->pad, &ctr->key);
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ctr_start.c
|
||||
CTR implementation, start chain, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef CTR
|
||||
|
||||
/**
|
||||
Initialize a CTR context
|
||||
@param cipher The index of the cipher desired
|
||||
@param count The initial vector
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param num_rounds Number of rounds in the cipher desired (0 for default)
|
||||
@param ctr The CTR state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ctr_start(int cipher, const unsigned char *count, const unsigned char *key, int keylen,
|
||||
int num_rounds, symmetric_CTR *ctr)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(count != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ctr != 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, &ctr->key)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* copy ctr */
|
||||
ctr->blocklen = cipher_descriptor[cipher].block_length;
|
||||
ctr->cipher = cipher;
|
||||
ctr->padlen = 0;
|
||||
ctr->mode = 0;
|
||||
for (x = 0; x < ctr->blocklen; x++) {
|
||||
ctr->ctr[x] = count[x];
|
||||
}
|
||||
cipher_descriptor[ctr->cipher].ecb_encrypt(ctr->ctr, ctr->pad, &ctr->key);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecb_decrypt.c
|
||||
ECB implementation, decrypt block, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef ECB
|
||||
|
||||
/**
|
||||
ECB decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param ecb ECB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_ECB *ecb)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(ecb != NULL);
|
||||
|
||||
/* valid cipher? */
|
||||
if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
LTC_ARGCHK(cipher_descriptor[ecb->cipher].ecb_decrypt != NULL);
|
||||
|
||||
cipher_descriptor[ecb->cipher].ecb_decrypt(ct, pt, &ecb->key);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecb_encrypt.c
|
||||
ECB implementation, encrypt a block, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef ECB
|
||||
|
||||
/**
|
||||
ECB encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param ecb ECB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_ECB *ecb)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(ecb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(ecb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
cipher_descriptor[ecb->cipher].ecb_encrypt(pt, ct, &ecb->key);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,44 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ecb_start.c
|
||||
ECB implementation, start chain, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef ECB
|
||||
|
||||
/**
|
||||
Initialize a ECB context
|
||||
@param cipher The index of the cipher desired
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param num_rounds Number of rounds in the cipher desired (0 for default)
|
||||
@param ecb The ECB state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ecb_start(int cipher, const unsigned char *key, int keylen, int num_rounds, symmetric_ECB *ecb)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ecb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
ecb->cipher = cipher;
|
||||
ecb->blocklen = cipher_descriptor[cipher].block_length;
|
||||
return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ecb->key);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,39 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ofb_decrypt.c
|
||||
OFB implementation, decrypt data, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef OFB
|
||||
|
||||
/**
|
||||
OFB decrypt
|
||||
@param ct Ciphertext
|
||||
@param pt [out] Plaintext
|
||||
@param len Length of ciphertext (octets)
|
||||
@param ofb OFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb)
|
||||
{
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(ofb != NULL);
|
||||
return ofb_encrypt(ct, pt, len, ofb);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ofb_encrypt.c
|
||||
OFB implementation, encrypt data, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef OFB
|
||||
|
||||
/**
|
||||
OFB encrypt
|
||||
@param pt Plaintext
|
||||
@param ct [out] Ciphertext
|
||||
@param len Length of plaintext (octets)
|
||||
@param ofb OFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(pt != NULL);
|
||||
LTC_ARGCHK(ct != NULL);
|
||||
LTC_ARGCHK(ofb != NULL);
|
||||
if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* is blocklen/padlen valid? */
|
||||
if (ofb->blocklen < 0 || ofb->blocklen > (int)sizeof(ofb->IV) ||
|
||||
ofb->padlen < 0 || ofb->padlen > (int)sizeof(ofb->IV)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
while (len-- > 0) {
|
||||
if (ofb->padlen == ofb->blocklen) {
|
||||
cipher_descriptor[ofb->cipher].ecb_encrypt(ofb->IV, ofb->IV, &ofb->key);
|
||||
ofb->padlen = 0;
|
||||
}
|
||||
*ct++ = *pt++ ^ ofb->IV[ofb->padlen++];
|
||||
}
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ofb_getiv.c
|
||||
OFB implementation, get IV, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef OFB
|
||||
|
||||
/**
|
||||
Get the current initial vector
|
||||
@param IV [out] The destination of the initial vector
|
||||
@param len [in/out] The max size and resulting size of the initial vector
|
||||
@param ofb The OFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb)
|
||||
{
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(len != NULL);
|
||||
LTC_ARGCHK(ofb != NULL);
|
||||
if ((unsigned long)ofb->blocklen > *len) {
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
XMEMCPY(IV, ofb->IV, ofb->blocklen);
|
||||
*len = ofb->blocklen;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ofb_setiv.c
|
||||
OFB implementation, set IV, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef OFB
|
||||
|
||||
/**
|
||||
Set an initial vector
|
||||
@param IV The initial vector
|
||||
@param len The length of the vector (in octets)
|
||||
@param ofb The OFB state
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(ofb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(ofb->cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (len != (unsigned long)ofb->blocklen) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* force next block */
|
||||
ofb->padlen = 0;
|
||||
cipher_descriptor[ofb->cipher].ecb_encrypt(IV, ofb->IV, &ofb->key);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*
|
||||
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file ofb_start.c
|
||||
OFB implementation, start chain, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef OFB
|
||||
|
||||
/**
|
||||
Initialize a OFB context
|
||||
@param cipher The index of the cipher desired
|
||||
@param IV The initial vector
|
||||
@param key The secret key
|
||||
@param keylen The length of the secret key (octets)
|
||||
@param num_rounds Number of rounds in the cipher desired (0 for default)
|
||||
@param ofb The OFB state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key,
|
||||
int keylen, int num_rounds, symmetric_OFB *ofb)
|
||||
{
|
||||
int x, err;
|
||||
|
||||
LTC_ARGCHK(IV != NULL);
|
||||
LTC_ARGCHK(key != NULL);
|
||||
LTC_ARGCHK(ofb != NULL);
|
||||
|
||||
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* copy details */
|
||||
ofb->cipher = cipher;
|
||||
ofb->blocklen = cipher_descriptor[cipher].block_length;
|
||||
for (x = 0; x < ofb->blocklen; x++) {
|
||||
ofb->IV[x] = IV[x];
|
||||
}
|
||||
|
||||
/* init the cipher */
|
||||
ofb->padlen = ofb->blocklen;
|
||||
return cipher_descriptor[cipher].setup(key, keylen, num_rounds, &ofb->key);
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user