Merge branch 'master' of github.com:cyassl/cyassl

This commit is contained in:
toddouska
2014-04-01 12:06:48 -07:00
6 changed files with 154 additions and 72 deletions

View File

@@ -915,7 +915,11 @@ static int DecryptKey(const char* password, int passwordSz, byte* salt,
if (version == PKCS5v2 || version == PKCS12) if (version == PKCS5v2 || version == PKCS12)
desIv = cbcIv; desIv = cbcIv;
Des_SetKey(&dec, key, desIv, DES_DECRYPTION);
ret = Des_SetKey(&dec, key, desIv, DES_DECRYPTION);
if (ret != 0)
return ret;
Des_CbcDecrypt(&dec, input, input, length); Des_CbcDecrypt(&dec, input, input, length);
break; break;
} }

View File

@@ -33,6 +33,7 @@
#endif #endif
#include <cyassl/ctaocrypt/des3.h> #include <cyassl/ctaocrypt/des3.h>
#include <cyassl/ctaocrypt/error-crypt.h>
#ifdef NO_INLINE #ifdef NO_INLINE
#include <cyassl/ctaocrypt/misc.h> #include <cyassl/ctaocrypt/misc.h>
@@ -61,7 +62,7 @@
#include "stm32f2xx.h" #include "stm32f2xx.h"
#include "stm32f2xx_cryp.h" #include "stm32f2xx_cryp.h"
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
{ {
word32 *dkey = des->key; word32 *dkey = des->key;
@@ -69,6 +70,8 @@
ByteReverseWords(dkey, dkey, 8); ByteReverseWords(dkey, dkey, 8);
Des_SetIV(des, iv); Des_SetIV(des, iv);
return 0;
} }
int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
@@ -381,7 +384,7 @@ int Des3_CbcDecrypt(Des3* des3, byte* out, const byte* in, word32 sz)
} }
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
{ {
int i ; int status ; int i ; int status ;
@@ -400,6 +403,7 @@ void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
des->iv[i] = 0x0 ; des->iv[i] = 0x0 ;
} }
return 0;
} }
int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir) int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
@@ -420,6 +424,7 @@ int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
for(i=0; i<DES_IVLEN; i++) for(i=0; i<DES_IVLEN; i++)
des3->iv[i] = 0x0 ; des3->iv[i] = 0x0 ;
} }
return 0; return 0;
} }
@@ -439,7 +444,7 @@ int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
}; };
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
{ {
int i = 0; int i = 0;
byte* dkey = (byte*)des->key; byte* dkey = (byte*)des->key;
@@ -452,6 +457,8 @@ int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]); dkey[i] = ((dkey[i] & 0xFE) | parityLookup[dkey[i] >> 1]);
} }
return 0;
} }
int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
@@ -616,7 +623,7 @@ int Des3_SetKey(Des3* des3, const byte* key, const byte* iv, int dir)
void Des_SetIV(Des* des, const byte* iv); void Des_SetIV(Des* des, const byte* iv);
int Des3_SetIV(Des3* des, const byte* iv); int Des3_SetIV(Des3* des, const byte* iv);
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
{ {
word32 *dkey = des->key ; word32 *dkey = des->key ;
word32 *dreg = des->reg ; word32 *dreg = des->reg ;
@@ -625,6 +632,8 @@ int Des3_SetIV(Des3* des, const byte* iv);
ByteReverseWords(dkey, dkey, 8); ByteReverseWords(dkey, dkey, 8);
XMEMCPY((byte *)dreg, (byte *)iv, 8); XMEMCPY((byte *)dreg, (byte *)iv, 8);
ByteReverseWords(dreg, dreg, 8); ByteReverseWords(dreg, dreg, 8);
return 0;
} }
int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
@@ -977,58 +986,72 @@ static INLINE void FPERM(word32* left, word32* right)
} }
static void DesSetKey(const byte* key, int dir, word32* out) static int DesSetKey(const byte* key, int dir, word32* out)
{ {
byte buffer[56+56+8]; byte* buffer = XMALLOC(56+56+8, NULL, DYNAMIC_TYPE_TMP_BUFFER);
byte *const pc1m = buffer; /* place to modify pc1 into */
byte *const pcr = pc1m + 56; /* place to rotate pc1 into */
byte *const ks = pcr + 56;
register int i,j,l;
int m;
for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */ if (!buffer) {
l = pc1[j] - 1; /* integer bit location */ return MEMORY_E;
m = l & 07; /* find bit */
pc1m[j] = (key[l >> 3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
? 1 : 0; /* and store 1-bit result */
} }
for (i = 0; i < 16; i++) { /* key chunk for each iteration */ else {
XMEMSET(ks, 0, 8); /* Clear key schedule */ byte* const pc1m = buffer; /* place to modify pc1 into */
for (j = 0; j < 56; j++) /* rotate pc1 the right amount */ byte* const pcr = pc1m + 56; /* place to rotate pc1 into */
pcr[j] = pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l: l-28]; byte* const ks = pcr + 56;
/* rotate left and right halves independently */ register int i, j, l;
for (j = 0; j < 48; j++){ /* select bits individually */ int m;
/* check bit that goes to ks[j] */
if (pcr[pc2[j] - 1]){ for (j = 0; j < 56; j++) { /* convert pc1 to bits of key */
/* mask it in if it's there */ l = pc1[j] - 1; /* integer bit location */
l= j % 6; m = l & 07; /* find bit */
ks[j/6] |= bytebit[l] >> 2; pc1m[j] = (key[l >> 3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
? 1 : 0; /* and store 1-bit result */
}
for (i = 0; i < 16; i++) { /* key chunk for each iteration */
XMEMSET(ks, 0, 8); /* Clear key schedule */
for (j = 0; j < 56; j++) /* rotate pc1 the right amount */
pcr[j] =
pc1m[(l = j + totrot[i]) < (j < 28 ? 28 : 56) ? l : l-28];
/* rotate left and right halves independently */
for (j = 0; j < 48; j++) { /* select bits individually */
if (pcr[pc2[j] - 1]) { /* check bit that goes to ks[j] */
l= j % 6; /* mask it in if it's there */
ks[j/6] |= bytebit[l] >> 2;
}
}
/* Now convert to odd/even interleaved form for use in F */
out[2*i] = ((word32) ks[0] << 24)
| ((word32) ks[2] << 16)
| ((word32) ks[4] << 8)
| ((word32) ks[6]);
out[2*i + 1] = ((word32) ks[1] << 24)
| ((word32) ks[3] << 16)
| ((word32) ks[5] << 8)
| ((word32) ks[7]);
}
/* reverse key schedule order */
if (dir == DES_DECRYPTION) {
for (i = 0; i < 16; i += 2) {
word32 swap = out[i];
out[i] = out[DES_KS_SIZE - 2 - i];
out[DES_KS_SIZE - 2 - i] = swap;
swap = out[i + 1];
out[i + 1] = out[DES_KS_SIZE - 1 - i];
out[DES_KS_SIZE - 1 - i] = swap;
} }
} }
/* Now convert to odd/even interleaved form for use in F */
out[2*i] = ((word32)ks[0] << 24) XFREE(buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
| ((word32)ks[2] << 16)
| ((word32)ks[4] << 8)
| ((word32)ks[6]);
out[2*i + 1] = ((word32)ks[1] << 24)
| ((word32)ks[3] << 16)
| ((word32)ks[5] << 8)
| ((word32)ks[7]);
} }
/* reverse key schedule order */ return 0;
if (dir == DES_DECRYPTION)
for (i = 0; i < 16; i += 2) {
word32 swap = out[i];
out[i] = out[DES_KS_SIZE - 2 - i];
out[DES_KS_SIZE - 2 - i] = swap;
swap = out[i + 1];
out[i + 1] = out[DES_KS_SIZE - 1 - i];
out[DES_KS_SIZE - 1 - i] = swap;
}
} }
@@ -1038,24 +1061,34 @@ static INLINE int Reverse(int dir)
} }
void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir) int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir)
{ {
DesSetKey(key, dir, des->key);
Des_SetIV(des, iv); Des_SetIV(des, iv);
return DesSetKey(key, dir, des->key);
} }
int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir) int Des3_SetKey(Des3* des, const byte* key, const byte* iv, int dir)
{ {
int ret;
#ifdef HAVE_CAVIUM #ifdef HAVE_CAVIUM
if (des->magic == CYASSL_3DES_CAVIUM_MAGIC) if (des->magic == CYASSL_3DES_CAVIUM_MAGIC)
return Des3_CaviumSetKey(des, key, iv); return Des3_CaviumSetKey(des, key, iv);
#endif #endif
DesSetKey(key + (dir == DES_ENCRYPTION ? 0 : 16), dir, des->key[0]); ret = DesSetKey(key + (dir == DES_ENCRYPTION ? 0:16), dir, des->key[0]);
DesSetKey(key + 8, Reverse(dir), des->key[1]); if (ret != 0)
DesSetKey(key + (dir == DES_DECRYPTION ? 0 : 16), dir, des->key[2]); return ret;
ret = DesSetKey(key + 8, Reverse(dir), des->key[1]);
if (ret != 0)
return ret;
ret = DesSetKey(key + (dir == DES_DECRYPTION ? 0:16), dir, des->key[2]);
if (ret != 0)
return ret;
return Des3_SetIV(des, iv); return Des3_SetIV(des, iv);
} }

View File

@@ -1041,14 +1041,27 @@ int PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, byte* output, word32 outputSz)
/* encrypt content */ /* encrypt content */
if (pkcs7->encryptOID == DESb) { if (pkcs7->encryptOID == DESb) {
Des des; Des des;
Des_SetKey(&des, contentKeyPlain, tmpIv, DES_ENCRYPTION);
Des_CbcEncrypt(&des, encryptedContent, plain, desOutSz);
} else if (pkcs7->encryptOID == DES3b) { ret = Des_SetKey(&des, contentKeyPlain, tmpIv, DES_ENCRYPTION);
if (ret == 0)
ret = Des_CbcEncrypt(&des, encryptedContent, plain, desOutSz);
if (ret != 0) {
XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (dynamicFlag)
XFREE(plain, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
}
else if (pkcs7->encryptOID == DES3b) {
Des3 des3; Des3 des3;
ret = Des3_SetKey(&des3, contentKeyPlain, tmpIv, DES_ENCRYPTION); ret = Des3_SetKey(&des3, contentKeyPlain, tmpIv, DES_ENCRYPTION);
if (ret == 0) if (ret == 0)
ret = Des3_CbcEncrypt(&des3, encryptedContent, plain, desOutSz); ret = Des3_CbcEncrypt(&des3, encryptedContent, plain, desOutSz);
if (ret != 0) { if (ret != 0) {
XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (dynamicFlag) if (dynamicFlag)
@@ -1321,15 +1334,24 @@ CYASSL_API int PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg,
/* decrypt encryptedContent */ /* decrypt encryptedContent */
if (encOID == DESb) { if (encOID == DESb) {
Des des; Des des;
Des_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION); ret = Des_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION);
Des_CbcDecrypt(&des, encryptedContent, encryptedContent,
encryptedContentSz); if (ret == 0)
} else if (encOID == DES3b) { ret = Des_CbcDecrypt(&des, encryptedContent, encryptedContent,
encryptedContentSz);
if (ret != 0) {
XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
}
else if (encOID == DES3b) {
Des3 des; Des3 des;
ret = Des3_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION); ret = Des3_SetKey(&des, decryptedKey, tmpIv, DES_DECRYPTION);
if (ret == 0) if (ret == 0)
ret = Des3_CbcDecrypt(&des, encryptedContent, encryptedContent, ret = Des3_CbcDecrypt(&des, encryptedContent, encryptedContent,
encryptedContentSz); encryptedContentSz);
if (ret != 0) { if (ret != 0) {
XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER); XFREE(encryptedContent, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret; return ret;

View File

@@ -1788,17 +1788,23 @@ int des_test(void)
0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
}; };
int ret;
ret = Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
if (ret != 0)
return -31;
Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector)); Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
Des_SetKey(&dec, key, iv, DES_DECRYPTION); ret = Des_SetKey(&dec, key, iv, DES_DECRYPTION);
if (ret != 0)
return -32;
Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher)); Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
if (memcmp(plain, vector, sizeof(plain))) if (memcmp(plain, vector, sizeof(plain)))
return -31; return -33;
if (memcmp(cipher, verify, sizeof(cipher))) if (memcmp(cipher, verify, sizeof(cipher)))
return -32; return -34;
return 0; return 0;
} }

View File

@@ -90,7 +90,7 @@ typedef struct Des3 {
} Des3; } Des3;
CYASSL_API void Des_SetKey(Des* des, const byte* key, const byte* iv, int dir); CYASSL_API int Des_SetKey(Des* des, const byte* key, const byte* iv, int dir);
CYASSL_API void Des_SetIV(Des* des, const byte* iv); CYASSL_API void Des_SetIV(Des* des, const byte* iv);
CYASSL_API void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz); CYASSL_API void Des_CbcEncrypt(Des* des, byte* out, const byte* in, word32 sz);
CYASSL_API void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz); CYASSL_API void Des_CbcDecrypt(Des* des, byte* out, const byte* in, word32 sz);

View File

@@ -2022,14 +2022,20 @@ int CyaSSL_Init(void)
if (XSTRNCMP(info.name, "DES-CBC", 7) == 0) { if (XSTRNCMP(info.name, "DES-CBC", 7) == 0) {
Des enc; Des enc;
Des_SetKey(&enc, key, info.iv, DES_DECRYPTION);
ret = Des_SetKey(&enc, key, info.iv, DES_DECRYPTION);
if (ret != 0)
return ret;
Des_CbcDecrypt(&enc, der.buffer, der.buffer, der.length); Des_CbcDecrypt(&enc, der.buffer, der.buffer, der.length);
} }
else if (XSTRNCMP(info.name, "DES-EDE3-CBC", 13) == 0) { else if (XSTRNCMP(info.name, "DES-EDE3-CBC", 13) == 0) {
Des3 enc; Des3 enc;
ret = Des3_SetKey(&enc, key, info.iv, DES_DECRYPTION); ret = Des3_SetKey(&enc, key, info.iv, DES_DECRYPTION);
if (ret != 0) if (ret != 0)
return ret; return ret;
ret = Des3_CbcDecrypt(&enc, der.buffer, der.buffer, der.length); ret = Des3_CbcDecrypt(&enc, der.buffer, der.buffer, der.length);
if (ret != 0) if (ret != 0)
return ret; return ret;
@@ -6929,9 +6935,13 @@ int CyaSSL_set_compression(CYASSL* ssl)
ctx->keyLen = 8; ctx->keyLen = 8;
if (enc == 0 || enc == 1) if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0; ctx->enc = enc ? 1 : 0;
if (key) if (key) {
Des_SetKey(&ctx->cipher.des, key, iv, ret = Des_SetKey(&ctx->cipher.des, key, iv,
ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION); ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
if (ret != 0)
return ret;
}
if (iv && key == NULL) if (iv && key == NULL)
Des_SetIV(&ctx->cipher.des, iv); Des_SetIV(&ctx->cipher.des, iv);
} }
@@ -6948,6 +6958,7 @@ int CyaSSL_set_compression(CYASSL* ssl)
if (ret != 0) if (ret != 0)
return ret; return ret;
} }
if (iv && key == NULL) { if (iv && key == NULL) {
ret = Des3_SetIV(&ctx->cipher.des3, iv); ret = Des3_SetIV(&ctx->cipher.des3, iv);
if (ret != 0) if (ret != 0)
@@ -7369,7 +7380,10 @@ int CyaSSL_set_compression(CYASSL* ssl)
int enc) int enc)
{ {
Des myDes; Des myDes;
CYASSL_ENTER("DES_cbc_encrypt"); CYASSL_ENTER("DES_cbc_encrypt");
/* OpenSSL compat, no ret */
Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc); Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
if (enc) if (enc)
@@ -7386,7 +7400,10 @@ int CyaSSL_set_compression(CYASSL* ssl)
int enc) int enc)
{ {
Des myDes; Des myDes;
CYASSL_ENTER("DES_ncbc_encrypt"); CYASSL_ENTER("DES_ncbc_encrypt");
/* OpenSSL compat, no ret */
Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc); Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
if (enc) if (enc)