Skip to content

Commit 4545a9b

Browse files
authored
Merge pull request #7960 from night1rider/mmcau-DesEcb
DES ECB using mmcau HW Library, and DES ECB basic test
2 parents 0d5659f + e912aff commit 4545a9b

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
@@ -8622,6 +8622,31 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
86228622
0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
86238623
};
86248624

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

@@ -8651,6 +8676,32 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des_test(void)
86518676
if (ret != 0)
86528677
return WC_TEST_RET_ENC_EC(ret);
86538678

8679+
/* Test basic ECB Process for DES*/
8680+
#ifdef WOLFSSL_DES_ECB
8681+
ret = wc_Des_SetKey(&enc, key_ecb, iv, DES_ENCRYPTION);
8682+
if (ret != 0)
8683+
return WC_TEST_RET_ENC_EC(ret);
8684+
8685+
ret = wc_Des_EcbEncrypt(&enc, cipher, vector_ecb, sizeof(vector));
8686+
if (ret != 0)
8687+
return WC_TEST_RET_ENC_EC(ret);
8688+
8689+
ret = wc_Des_SetKey(&dec, key_ecb, iv, DES_DECRYPTION);
8690+
if (ret != 0)
8691+
return WC_TEST_RET_ENC_EC(ret);
8692+
8693+
ret = wc_Des_EcbDecrypt(&dec, plain, cipher, sizeof(cipher));
8694+
if (ret != 0)
8695+
return WC_TEST_RET_ENC_EC(ret);
8696+
8697+
if (XMEMCMP(plain, vector_ecb, sizeof(plain)))
8698+
return WC_TEST_RET_ENC_NC;
8699+
8700+
if (XMEMCMP(cipher, verify_ecb, sizeof(cipher)))
8701+
return WC_TEST_RET_ENC_NC;
8702+
8703+
#endif /* WOLFSSL_DES_ECB */
8704+
86548705
#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_SHA)
86558706
{
86568707
EncryptedInfo info;
@@ -8722,6 +8773,33 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t des3_test(void)
87228773
0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
87238774
};
87248775

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

8841+
/* Test basic ECB Process for DES3*/
8842+
#ifdef WOLFSSL_DES_ECB
8843+
ret = wc_Des3Init(&enc, HEAP_HINT, devId);
8844+
if (ret != 0)
8845+
return WC_TEST_RET_ENC_EC(ret);
8846+
ret = wc_Des3Init(&dec, HEAP_HINT, devId);
8847+
if (ret != 0)
8848+
return WC_TEST_RET_ENC_EC(ret);
8849+
8850+
ret = wc_Des3_SetKey(&enc, key3_ecb, NULL, DES_ENCRYPTION);
8851+
if (ret != 0)
8852+
return WC_TEST_RET_ENC_EC(ret);
8853+
ret = wc_Des3_SetKey(&dec, key3_ecb, NULL, DES_DECRYPTION);
8854+
if (ret != 0)
8855+
return WC_TEST_RET_ENC_EC(ret);
8856+
ret = wc_Des3_EcbEncrypt(&enc, cipher, vector_ecb, sizeof(vector_ecb));
8857+
#if defined(WOLFSSL_ASYNC_CRYPT)
8858+
ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
8859+
#endif
8860+
if (ret != 0)
8861+
return WC_TEST_RET_ENC_EC(ret);
8862+
ret = wc_Des3_EcbDecrypt(&dec, plain, cipher, sizeof(cipher));
8863+
#if defined(WOLFSSL_ASYNC_CRYPT)
8864+
ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
8865+
#endif
8866+
if (ret != 0)
8867+
return WC_TEST_RET_ENC_EC(ret);
8868+
8869+
if (XMEMCMP(plain, vector_ecb, sizeof(plain)))
8870+
return WC_TEST_RET_ENC_NC;
8871+
8872+
if (XMEMCMP(cipher, verify3_ecb, sizeof(cipher)))
8873+
return WC_TEST_RET_ENC_NC;
8874+
8875+
#endif /* WOLFSSL_DES_ECB */
8876+
87638877
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
87648878
/* test the same vectors with using compatibility layer */
87658879
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)