Skip to content

Commit e912aff

Browse files
night1riderZackLabPC
authored andcommitted
DES ECB using mmcau HW Library, and DES ECB basic test
1 parent 4d837e7 commit e912aff

3 files changed

Lines changed: 284 additions & 0 deletions

File tree

wolfcrypt/src/des3.c

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,169 @@
10311031
}
10321032

10331033

1034+
#ifdef WOLFSSL_DES_ECB
1035+
/* One block, compatibility only */
1036+
int wc_Des_EcbEncrypt(Des* des, byte* out, const byte* in, word32 sz)
1037+
{
1038+
int offset = 0;
1039+
int len = sz;
1040+
int ret = 0;
1041+
byte temp_block[DES_BLOCK_SIZE];
1042+
1043+
1044+
#ifdef FREESCALE_MMCAU_CLASSIC
1045+
if ((wc_ptr_t)out % WOLFSSL_MMCAU_ALIGNMENT) {
1046+
WOLFSSL_MSG("Bad cau_des_encrypt alignment");
1047+
return BAD_ALIGN_E;
1048+
}
1049+
#endif
1050+
1051+
while (len > 0)
1052+
{
1053+
XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
1054+
1055+
ret = wolfSSL_CryptHwMutexLock();
1056+
if (ret != 0) {
1057+
return ret;
1058+
}
1059+
#ifdef FREESCALE_MMCAU_CLASSIC
1060+
cau_des_encrypt(temp_block, (byte*)des->key, out + offset);
1061+
#else
1062+
MMCAU_DES_EncryptEcb(temp_block, (byte*)des->key, out + offset);
1063+
#endif
1064+
wolfSSL_CryptHwMutexUnLock();
1065+
1066+
len -= DES_BLOCK_SIZE;
1067+
offset += DES_BLOCK_SIZE;
1068+
1069+
}
1070+
return ret;
1071+
1072+
}
1073+
1074+
int wc_Des_EcbDecrypt(Des* des, byte* out, const byte* in, word32 sz)
1075+
{
1076+
int offset = 0;
1077+
int len = sz;
1078+
int ret = 0;
1079+
byte temp_block[DES_BLOCK_SIZE];
1080+
1081+
#ifdef FREESCALE_MMCAU_CLASSIC
1082+
if ((wc_ptr_t)out % WOLFSSL_MMCAU_ALIGNMENT) {
1083+
WOLFSSL_MSG("Bad cau_des_decrypt alignment");
1084+
return BAD_ALIGN_E;
1085+
}
1086+
#endif
1087+
1088+
while (len > 0)
1089+
{
1090+
XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
1091+
1092+
ret = wolfSSL_CryptHwMutexLock();
1093+
if (ret != 0) {
1094+
return ret;
1095+
}
1096+
1097+
#ifdef FREESCALE_MMCAU_CLASSIC
1098+
cau_des_decrypt(in + offset, (byte*)des->key, out + offset);
1099+
#else
1100+
MMCAU_DES_DecryptEcb(in + offset, (byte*)des->key, out + offset);
1101+
#endif
1102+
wolfSSL_CryptHwMutexUnLock();
1103+
1104+
len -= DES_BLOCK_SIZE;
1105+
offset += DES_BLOCK_SIZE;
1106+
}
1107+
1108+
return ret;
1109+
}
1110+
1111+
int wc_Des3_EcbEncrypt(Des3* des, byte* out, const byte* in, word32 sz)
1112+
{
1113+
int offset = 0;
1114+
int len = sz;
1115+
int ret = 0;
1116+
1117+
byte temp_block[DES_BLOCK_SIZE];
1118+
1119+
1120+
#ifdef FREESCALE_MMCAU_CLASSIC
1121+
if ((wc_ptr_t)out % WOLFSSL_MMCAU_ALIGNMENT) {
1122+
WOLFSSL_MSG("Bad 3ede cau_des_encrypt alignment");
1123+
return BAD_ALIGN_E;
1124+
}
1125+
#endif
1126+
1127+
while (len > 0)
1128+
{
1129+
XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
1130+
1131+
ret = wolfSSL_CryptHwMutexLock();
1132+
if (ret != 0) {
1133+
return ret;
1134+
}
1135+
#ifdef FREESCALE_MMCAU_CLASSIC
1136+
cau_des_encrypt(temp_block, (byte*)des->key[0], out + offset);
1137+
cau_des_decrypt(out + offset, (byte*)des->key[1], out + offset);
1138+
cau_des_encrypt(out + offset, (byte*)des->key[2], out + offset);
1139+
#else
1140+
MMCAU_DES_EncryptEcb(temp_block , (byte*)des->key[0], out + offset);
1141+
MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[1], out + offset);
1142+
MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[2], out + offset);
1143+
#endif
1144+
wolfSSL_CryptHwMutexUnLock();
1145+
1146+
len -= DES_BLOCK_SIZE;
1147+
offset += DES_BLOCK_SIZE;
1148+
1149+
}
1150+
1151+
return ret;
1152+
}
1153+
1154+
int wc_Des3_EcbDecrypt(Des3* des, byte* out, const byte* in, word32 sz)
1155+
{
1156+
int offset = 0;
1157+
int len = sz;
1158+
int ret = 0;
1159+
1160+
byte temp_block[DES_BLOCK_SIZE];
1161+
1162+
#ifdef FREESCALE_MMCAU_CLASSIC
1163+
if ((wc_ptr_t)out % WOLFSSL_MMCAU_ALIGNMENT) {
1164+
WOLFSSL_MSG("Bad 3ede cau_des_decrypt alignment");
1165+
return BAD_ALIGN_E;
1166+
}
1167+
#endif
1168+
1169+
while (len > 0)
1170+
{
1171+
XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE);
1172+
1173+
ret = wolfSSL_CryptHwMutexLock();
1174+
if (ret != 0) {
1175+
return ret;
1176+
}
1177+
#ifdef FREESCALE_MMCAU_CLASSIC
1178+
cau_des_decrypt(in + offset, (byte*)des->key[2], out + offset);
1179+
cau_des_encrypt(out + offset, (byte*)des->key[1], out + offset);
1180+
cau_des_decrypt(out + offset, (byte*)des->key[0], out + offset);
1181+
#else
1182+
MMCAU_DES_DecryptEcb(in + offset , (byte*)des->key[2], out + offset);
1183+
MMCAU_DES_EncryptEcb(out + offset, (byte*)des->key[1], out + offset);
1184+
MMCAU_DES_DecryptEcb(out + offset, (byte*)des->key[0], out + offset);
1185+
#endif
1186+
wolfSSL_CryptHwMutexUnLock();
1187+
1188+
len -= DES_BLOCK_SIZE;
1189+
offset += DES_BLOCK_SIZE;
1190+
}
1191+
1192+
return ret;
1193+
}
1194+
#endif /* WOLFSSL_DES_ECB */
1195+
1196+
10341197
#elif defined(WOLFSSL_PIC32MZ_CRYPT)
10351198

10361199
/* PIC32MZ DES hardware requires size multiple of block size */

wolfcrypt/test/test.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8617,6 +8617,31 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
86178617
0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
86188618
};
86198619

8620+
#ifdef WOLFSSL_DES_ECB
8621+
8622+
/* "Stay strong and move on!"" */
8623+
WOLFSSL_SMALL_STACK_STATIC const byte vector_ecb[] =
8624+
{
8625+
0x53,0x74,0x61,0x79,0x20,0x73,0x74,0x72,
8626+
0x6F,0x6E,0x67,0x20,0x61,0x6E,0x64,0x20,
8627+
0x6D,0x6F,0x76,0x65,0x20,0x6F,0x6E,0x21
8628+
};
8629+
8630+
WOLFSSL_SMALL_STACK_STATIC const byte verify_ecb[] =
8631+
{
8632+
0x70,0x4F,0x20,0xF6,0x72,0xB4,0xD0,0x2A,
8633+
0xB5,0xA9,0x94,0x9F,0x11,0xCF,0x87,0xED,
8634+
0x13,0x33,0x82,0xCB,0x8B,0xF1,0x82,0x56
8635+
};
8636+
8637+
/* "Lemmings" */
8638+
WOLFSSL_SMALL_STACK_STATIC const byte key_ecb[] =
8639+
{
8640+
0x4C,0x65,0x6D,0x6D,0x69,0x6E,0x67,0x73
8641+
};
8642+
8643+
#endif /* WOLFSSL_DES_ECB */
8644+
86208645
wc_test_ret_t ret;
86218646
WOLFSSL_ENTER("des_test");
86228647

@@ -8646,6 +8671,32 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
86468671
if (ret != 0)
86478672
return WC_TEST_RET_ENC_EC(ret);
86488673

8674+
/* Test basic ECB Process for DES*/
8675+
#ifdef WOLFSSL_DES_ECB
8676+
ret = wc_Des_SetKey(&enc, key_ecb, iv, DES_ENCRYPTION);
8677+
if (ret != 0)
8678+
return WC_TEST_RET_ENC_EC(ret);
8679+
8680+
ret = wc_Des_EcbEncrypt(&enc, cipher, vector_ecb, sizeof(vector));
8681+
if (ret != 0)
8682+
return WC_TEST_RET_ENC_EC(ret);
8683+
8684+
ret = wc_Des_SetKey(&dec, key_ecb, iv, DES_DECRYPTION);
8685+
if (ret != 0)
8686+
return WC_TEST_RET_ENC_EC(ret);
8687+
8688+
ret = wc_Des_EcbDecrypt(&dec, plain, cipher, sizeof(cipher));
8689+
if (ret != 0)
8690+
return WC_TEST_RET_ENC_EC(ret);
8691+
8692+
if (XMEMCMP(plain, vector_ecb, sizeof(plain)))
8693+
return WC_TEST_RET_ENC_NC;
8694+
8695+
if (XMEMCMP(cipher, verify_ecb, sizeof(cipher)))
8696+
return WC_TEST_RET_ENC_NC;
8697+
8698+
#endif /* WOLFSSL_DES_ECB */
8699+
86498700
#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
86508701
{
86518702
EncryptedInfo info;
@@ -8717,6 +8768,33 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
87178768
0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
87188769
};
87198770

8771+
#ifdef WOLFSSL_DES_ECB
8772+
8773+
/* Stay strong and move on! */
8774+
WOLFSSL_SMALL_STACK_STATIC const byte vector_ecb[] =
8775+
{
8776+
0x53,0x74,0x61,0x79,0x20,0x73,0x74,0x72,
8777+
0x6F,0x6E,0x67,0x20,0x61,0x6E,0x64,0x20,
8778+
0x6D,0x6F,0x76,0x65,0x20,0x6F,0x6E,0x21
8779+
};
8780+
8781+
WOLFSSL_SMALL_STACK_STATIC const byte verify3_ecb[] =
8782+
{
8783+
0x45,0x7E,0xFA,0xA1,0x05,0xDD,0x48,0x86,
8784+
0x4D,0xB2,0xAB,0xE4,0xF9,0x63,0xD6,0x54,
8785+
0x7C,0x5A,0xB3,0x67,0x32,0x25,0x67,0x3D
8786+
};
8787+
8788+
/* "Life is what you make it" */
8789+
WOLFSSL_SMALL_STACK_STATIC const byte key3_ecb[] =
8790+
{
8791+
0x4C,0x69,0x66,0x65,0x20,0x69,0x73,0x20,
8792+
0x77,0x68,0x61,0x74,0x20,0x79,0x6F,0x75,
8793+
0x20,0x6D,0x61,0x6B,0x65,0x20,0x69,0x74
8794+
};
8795+
8796+
#endif /* WOLFSSL_DES_ECB */
8797+
87208798
wc_test_ret_t ret;
87218799
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
87228800
size_t i;
@@ -8755,6 +8833,42 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
87558833
if (XMEMCMP(cipher, verify3, sizeof(cipher)))
87568834
return WC_TEST_RET_ENC_NC;
87578835

8836+
/* Test basic ECB Process for DES3*/
8837+
#ifdef WOLFSSL_DES_ECB
8838+
ret = wc_Des3Init(&enc, HEAP_HINT, devId);
8839+
if (ret != 0)
8840+
return WC_TEST_RET_ENC_EC(ret);
8841+
ret = wc_Des3Init(&dec, HEAP_HINT, devId);
8842+
if (ret != 0)
8843+
return WC_TEST_RET_ENC_EC(ret);
8844+
8845+
ret = wc_Des3_SetKey(&enc, key3_ecb, NULL, DES_ENCRYPTION);
8846+
if (ret != 0)
8847+
return WC_TEST_RET_ENC_EC(ret);
8848+
ret = wc_Des3_SetKey(&dec, key3_ecb, NULL, DES_DECRYPTION);
8849+
if (ret != 0)
8850+
return WC_TEST_RET_ENC_EC(ret);
8851+
ret = wc_Des3_EcbEncrypt(&enc, cipher, vector_ecb, sizeof(vector_ecb));
8852+
#if defined(WOLFSSL_ASYNC_CRYPT)
8853+
ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
8854+
#endif
8855+
if (ret != 0)
8856+
return WC_TEST_RET_ENC_EC(ret);
8857+
ret = wc_Des3_EcbDecrypt(&dec, plain, cipher, sizeof(cipher));
8858+
#if defined(WOLFSSL_ASYNC_CRYPT)
8859+
ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
8860+
#endif
8861+
if (ret != 0)
8862+
return WC_TEST_RET_ENC_EC(ret);
8863+
8864+
if (XMEMCMP(plain, vector_ecb, sizeof(plain)))
8865+
return WC_TEST_RET_ENC_NC;
8866+
8867+
if (XMEMCMP(cipher, verify3_ecb, sizeof(cipher)))
8868+
return WC_TEST_RET_ENC_NC;
8869+
8870+
#endif /* WOLFSSL_DES_ECB */
8871+
87588872
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
87598873
/* test the same vectors with using compatibility layer */
87608874
for (i = 0; i < sizeof(vector); i += DES_BLOCK_SIZE){

wolfssl/wolfcrypt/des3.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,16 @@ WOLFSSL_API int wc_Des_EcbEncrypt(Des* des, byte* out,
134134
WOLFSSL_API int wc_Des3_EcbEncrypt(Des3* des, byte* out,
135135
const byte* in, word32 sz);
136136

137+
#ifdef FREESCALE_MMCAU /* Has separate encrypt/decrypt functions */
138+
WOLFSSL_API int wc_Des_EcbDecrypt(Des* des, byte* out,
139+
const byte* in, word32 sz);
140+
WOLFSSL_API int wc_Des3_EcbDecrypt(Des3* des, byte* out,
141+
const byte* in, word32 sz);
142+
#else
137143
/* ECB decrypt same process as encrypt but with decrypt key */
138144
#define wc_Des_EcbDecrypt wc_Des_EcbEncrypt
139145
#define wc_Des3_EcbDecrypt wc_Des3_EcbEncrypt
146+
#endif
140147

141148
WOLFSSL_API int wc_Des3_SetKey(Des3* des, const byte* key,
142149
const byte* iv,int dir);

0 commit comments

Comments
 (0)