Skip to content

Commit 9ac6097

Browse files
Merge pull request #193 from lealem47/fix_old_algo_name
Fix for enc command old algo name format
2 parents 7b5c167 + e905c35 commit 9ac6097

File tree

2 files changed

+83
-29
lines changed

2 files changed

+83
-29
lines changed

src/tools/clu_funcs.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -722,19 +722,31 @@ static int wolfCLU_parseAlgo(char* name, int* alg, char** mode, int* size)
722722
nameCheck = 1;
723723
}
724724

725-
/* gets mode after second "-" and before the third */
725+
/* gets mode and size after the algorithm name, supports both
726+
* "alg-size-mode" (aes-256-cbc) and "alg-mode-size" (aes-cbc-256) */
726727
if (nameCheck != 0) {
727-
/* gets size after third "-" */
728728
sz = strtok_r(NULL, "-", &end);
729729
if (sz == NULL) {
730730
return WOLFCLU_FATAL_ERROR;
731731
}
732+
tmpMode = strtok_r(NULL, "-", &end);
733+
if (tmpMode == NULL) {
734+
return WOLFCLU_FATAL_ERROR;
735+
}
736+
737+
/* if second token isn't numeric, it's the mode (alg-mode-size) */
738+
if (sz[0] < '0' || sz[0] > '9') {
739+
char* tmp = sz;
740+
sz = tmpMode;
741+
tmpMode = tmp;
742+
}
732743
*size = XATOI(sz);
733744
}
734-
735-
tmpMode = strtok_r(NULL, "-", &end);
736-
if (tmpMode == NULL) {
737-
return WOLFCLU_FATAL_ERROR;
745+
else {
746+
tmpMode = strtok_r(NULL, "-", &end);
747+
if (tmpMode == NULL) {
748+
return WOLFCLU_FATAL_ERROR;
749+
}
738750
}
739751

740752
for (i = 0; i < (int) (sizeof(acceptMode)/sizeof(acceptMode[0])); i++) {
@@ -866,16 +878,16 @@ static int wolfCLU_parseAlgo(char* name, int* alg, char** mode, int* size)
866878
return ret;
867879
}
868880

869-
static const char WOLFCLU_AES128CTR_NAME[] = "aes-128-ctr";
870-
static const char WOLFCLU_AES192CTR_NAME[] = "aes-192-ctr";
871-
static const char WOLFCLU_AES256CTR_NAME[] = "aes-256-ctr";
872-
static const char WOLFCLU_AES128CBC_NAME[] = "aes-128-cbc";
873-
static const char WOLFCLU_AES192CBC_NAME[] = "aes-192-cbc";
874-
static const char WOLFCLU_AES256CBC_NAME[] = "aes-256-cbc";
875-
static const char WOLFCLU_CAMELLIA128CBC_NAME[] = "camellia-128-cbc";
876-
static const char WOLFCLU_CAMELLIA192CBC_NAME[] = "camellia-192-cbc";
877-
static const char WOLFCLU_CAMELLIA256CBC_NAME[] = "camellia-256-cbc";
878-
static const char WOLFCLU_DESCBC_NAME[] = "des-cbc";
881+
static const char WOLFCLU_AES128CTR_NAME[] = "-aes-128-ctr";
882+
static const char WOLFCLU_AES192CTR_NAME[] = "-aes-192-ctr";
883+
static const char WOLFCLU_AES256CTR_NAME[] = "-aes-256-ctr";
884+
static const char WOLFCLU_AES128CBC_NAME[] = "-aes-128-cbc";
885+
static const char WOLFCLU_AES192CBC_NAME[] = "-aes-192-cbc";
886+
static const char WOLFCLU_AES256CBC_NAME[] = "-aes-256-cbc";
887+
static const char WOLFCLU_CAMELLIA128CBC_NAME[] = "-camellia-128-cbc";
888+
static const char WOLFCLU_CAMELLIA192CBC_NAME[] = "-camellia-192-cbc";
889+
static const char WOLFCLU_CAMELLIA256CBC_NAME[] = "-camellia-256-cbc";
890+
static const char WOLFCLU_DESCBC_NAME[] = "-des-cbc";
879891

880892
static const char* algoName[] = {
881893
WOLFCLU_AES128CTR_NAME,
@@ -894,23 +906,21 @@ static const char* algoName[] = {
894906
* names */
895907
#define MAX_AES_IDX 6
896908
static const char* oldAlgoName[] = {
897-
"aes-ctr-128",
898-
"aes-ctr-192",
899-
"aes-ctr-256",
900-
"aes-cbc-128",
901-
"aes-cbc-192",
902-
"aes-cbc-256",
909+
"-aes-ctr-128",
910+
"-aes-ctr-192",
911+
"-aes-ctr-256",
912+
"-aes-cbc-128",
913+
"-aes-cbc-192",
914+
"-aes-cbc-256",
903915
};
904916

905917

906918
/* convert an old algo name into one optargs can handle */
907-
static void wolfCLU_oldAlgo(int argc, char** argv, int maxIdx)
919+
static void wolfCLU_oldAlgo(int argc, char** argv)
908920
{
909-
int end;
910921
int i, j;
911922

912-
end = (argc < maxIdx)? argc : maxIdx;
913-
for (i = 0; i < end; i++) {
923+
for (i = 0; i < argc; i++) {
914924
for (j = 0; j < MAX_AES_IDX; j++) {
915925
if (XSTRCMP(argv[i], oldAlgoName[j]) == 0) {
916926
argv[i] = (char*)algoName[j];
@@ -957,7 +967,7 @@ int wolfCLU_getAlgo(int argc, char** argv, int* alg, char** mode, int* size)
957967
int option;
958968
char name[80];
959969

960-
wolfCLU_oldAlgo(argc, argv, 3);
970+
wolfCLU_oldAlgo(argc, argv);
961971
XMEMSET(name, 0, sizeof(name));
962972
XSTRLCPY(name, argv[2], XSTRLEN(argv[2])+1);
963973
ret = wolfCLU_parseAlgo(name, alg, mode, size);
@@ -1165,8 +1175,8 @@ int wolfCLU_checkForArg(const char* searchTerm, int length, int argc,
11651175
return 1;
11661176

11671177
}
1168-
else if (XMEMCMP(argv[i], searchTerm, length) == 0 &&
1169-
(int)XSTRLEN(argv[i]) == length) {
1178+
else if ((int)XSTRLEN(argv[i]) == length &&
1179+
XMEMCMP(argv[i], searchTerm, length) == 0) {
11701180
ret = i;
11711181
if (argFound == 1) {
11721182
wolfCLU_LogError("ERROR: argument found twice: \"%s\"", searchTerm);

tests/encrypt/enc-test.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,49 @@ if [ $? == 0 ]; then
141141
rm -f test-enc.der
142142
fi
143143

144+
# test legacy algo names
145+
run "enc -base64 -aes-cbc-256 -in certs/crl.der -out test-enc.der" "test password"
146+
run "enc -base64 -d -aes-cbc-256 -in test-enc.der -out test-dec.der" "test password"
147+
diff "./certs/crl.der" "./test-dec.der" &> /dev/null
148+
if [ $? != 0 ]; then
149+
echo "issue with legacy name aes-cbc-256 round trip"
150+
exit 99
151+
fi
152+
rm -f test-dec.der
153+
rm -f test-enc.der
154+
155+
# encrypt with legacy name, decrypt with canonical name
156+
run "enc -aes-cbc-256 -in certs/crl.der -out test-enc.der" "test password"
157+
run "enc -d -aes-256-cbc -in test-enc.der -out test-dec.der" "test password"
158+
diff "./certs/crl.der" "./test-dec.der" &> /dev/null
159+
if [ $? != 0 ]; then
160+
echo "issue with legacy enc / canonical dec"
161+
exit 99
162+
fi
163+
rm -f test-dec.der
164+
rm -f test-enc.der
165+
166+
# encrypt with canonical name, decrypt with legacy name
167+
run "enc -aes-256-cbc -in certs/crl.der -out test-enc.der" "test password"
168+
run "enc -d -aes-cbc-256 -in test-enc.der -out test-dec.der" "test password"
169+
diff "./certs/crl.der" "./test-dec.der" &> /dev/null
170+
if [ $? != 0 ]; then
171+
echo "issue with canonical enc / legacy dec"
172+
exit 99
173+
fi
174+
rm -f test-dec.der
175+
rm -f test-enc.der
176+
177+
# test legacy name with aes-cbc-128
178+
run "enc -aes-cbc-128 -in certs/crl.der -out test-enc.der" "test password"
179+
run "enc -d -aes-cbc-128 -in test-enc.der -out test-dec.der" "test password"
180+
diff "./certs/crl.der" "./test-dec.der" &> /dev/null
181+
if [ $? != 0 ]; then
182+
echo "issue with legacy name aes-cbc-128 round trip"
183+
exit 99
184+
fi
185+
rm -f test-dec.der
186+
rm -f test-enc.der
187+
144188
echo "Done"
145189
exit 0

0 commit comments

Comments
 (0)