972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
|
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
|
-
+
-
+
|
* Side effects:
* Sets result to message digest or error message
*
*-------------------------------------------------------------------
*/
int
DigestDataHandler(Tcl_Interp *interp, Tcl_Obj *dataObj, const EVP_MD *md,
const EVP_CIPHER *cipher, int format, Tcl_Obj *keyObj) {
const EVP_CIPHER *cipher, int format, Tcl_Obj *keyObj, EVP_MAC *mac) {
char *data;
int data_len;
DigestState *statePtr;
/* Get data */
data = Tcl_GetByteArrayFromObj(dataObj, &data_len);
if (data == NULL) {
Tcl_SetResult(interp, "No data", NULL);
return TCL_ERROR;
}
/* Create state data structure */
if ((statePtr = DigestStateNew(interp, format)) == NULL) {
Tcl_AppendResult(interp, "Memory allocation error", (char *) NULL);
return TCL_ERROR;
}
/* Calc Digest, abort for error */
if (DigestInitialize(interp, statePtr, md, cipher, keyObj) != TCL_OK ||
if (DigestInitialize(interp, statePtr, md, cipher, keyObj, mac) != TCL_OK ||
DigestUpdate(statePtr, data, (size_t) data_len, 1) == 0 ||
DigestFinalize(interp, statePtr, NULL) != TCL_OK) {
DigestStateFree(statePtr);
return TCL_ERROR;
}
/* Clean-up */
|
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
|
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-
+
+
-
-
+
-
-
+
-
-
-
-
-
+
-
+
-
+
-
+
|
}
/*******************************************************************/
/*
*-------------------------------------------------------------------
*
* GetDigest -- Get message digest
*
* Returns:
* EVP_MD * or NULL
*
*-------------------------------------------------------------------
*/
EVP_MD *GetDigest(Tcl_Interp *interp, Tcl_Obj *objPtr, int *format) {
const EVP_MD *md = NULL;
char *digestName = Tcl_GetStringFromObj(objPtr, NULL);
if (digestName != NULL) {
md = EVP_get_digestbyname(digestName);
if (md == NULL) {
Tcl_AppendResult(interp, "Invalid digest \"", digestName, "\"", NULL);
return NULL;
} else if (md == EVP_shake128() || md == EVP_shake256()) {
*format |= IS_XOF;
}
} else {
Tcl_AppendResult(interp, "No digest specified", NULL);
return NULL;
}
return md;
}
/*
*-------------------------------------------------------------------
*
* GetCipher -- Get cipher
*
* Returns:
* EVP_CIPHER * or NULL
*
*-------------------------------------------------------------------
*/
EVP_CIPHER *GetCipher(Tcl_Interp *interp, Tcl_Obj *objPtr, int *type) {
const EVP_CIPHER *cipher = NULL;
char *cipherName = Tcl_GetStringFromObj(objPtr, NULL);
if (cipherName != NULL) {
cipher = EVP_get_cipherbyname(cipherName);
*type = TYPE_CMAC;
if (cipher == NULL) {
Tcl_AppendResult(interp, "Invalid cipher \"", cipherName, "\"", NULL);
return NULL;
}
} else {
Tcl_AppendResult(interp, "No cipher specified", NULL);
return NULL;
}
return cipher;
}
/*
*-------------------------------------------------------------------
*
* GetKey -- Get key
*
* Returns:
* unsigned char * or NULL
*
*-------------------------------------------------------------------
*/
unsigned char *GetKey(Tcl_Interp *interp, Tcl_Obj *objPtr, int *type) {
unsigned char *key = Tcl_GetByteArrayFromObj(objPtr, NULL);
if (key == NULL) {
Tcl_AppendResult(interp, "No key specified", NULL);
return NULL;
}
if (*type == TYPE_MD) {
*type = TYPE_HMAC;
}
return key;
}
/*
*-------------------------------------------------------------------
*
* GetMAC -- Get MAC
*
* Returns:
* EVP_MAC * or NULL
*
*-------------------------------------------------------------------
*/
EVP_MAC *GetMAC(Tcl_Interp *interp, Tcl_Obj *objPtr, int *type) {
EVP_MAC *mac = NULL;
char *macName = Tcl_GetStringFromObj(objPtr, NULL);
if (macName != NULL) {
if (strcmp(macName, "cmac") == 0) {
*type = TYPE_CMAC;
} else if (strcmp(macName, "hmac") == 0) {
*type = TYPE_HMAC;
} else {
Tcl_AppendResult(interp, "Invalid MAC \"", macName, "\"", NULL);
return NULL;
}
mac = (void *) macName;
} else {
Tcl_AppendResult(interp, "No MAC specified", NULL);
return NULL;
}
return mac;
}
/*******************************************************************/
/*
*-------------------------------------------------------------------
*
* DigestMain --
*
* Return message digest or Message Authentication Code (MAC) of
* data using user specified hash function.
*
* Returns:
* TCL_OK or TCL_ERROR
*
* Side effects:
* Sets result to message digest or error message
*
*-------------------------------------------------------------------
*/
static int DigestMain(int type, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
int idx, start = 1, format = HEX_FORMAT, res = TCL_OK;
Tcl_Obj *cipherObj = NULL, *cmdObj = NULL, *dataObj = NULL, *digestObj = NULL;
Tcl_Obj *cmdObj = NULL, *dataObj = NULL, *fileObj = NULL, *keyObj = NULL;
const char *digestName = NULL, *cipherName = NULL, *macName = NULL, *channel = NULL, *opt;
Tcl_Obj *fileObj = NULL, *keyObj = NULL, *macObj = NULL;
const char *channel = NULL, *opt;
const EVP_MD *md = NULL;
const EVP_CIPHER *cipher = NULL;
EVP_MAC *mac = NULL;
/* Clear interp result */
Tcl_ResetResult(interp);
/* Validate arg count */
if (objc < 3 || objc > 12) {
Tcl_WrongNumArgs(interp, 1, objv, "?-bin|-hex? ?-cipher name? ?-digest name? ?-key key? ?-mac name? [-channel chan | -command cmdName | -file filename | ?-data? data]");
return TCL_ERROR;
}
/* Special case of first arg is digest, cipher, or mac */
opt = Tcl_GetStringFromObj(objv[start], NULL);
if (opt[0] != '-') {
if (type == TYPE_MD || type == TYPE_HMAC) {
digestName = opt;
digestObj = objv[start];
start++;
} else if (type == TYPE_CMAC) {
cipherName = opt;
cipherObj = objv[start];
start++;
} else if (type == TYPE_MAC) {
macName = opt;
macObj = objv[start];
start++;
}
}
/* Get options */
for (idx = start; idx < objc; idx++) {
opt = Tcl_GetStringFromObj(objv[idx], NULL);
if (opt[0] != '-') {
break;
}
OPTFLAG("-bin", format, BIN_FORMAT);
OPTFLAG("-binary", format, BIN_FORMAT);
OPTFLAG("-hex", format, HEX_FORMAT);
OPTFLAG("-hexadecimal", format, HEX_FORMAT);
OPTSTR("-chan", channel);
OPTSTR("-channel", channel);
OPTSTR("-cipher", cipherName);
OPTOBJ("-cipher", cipherObj);
OPTOBJ("-command", cmdObj);
OPTOBJ("-data", dataObj);
OPTSTR("-digest", digestName);
OPTOBJ("-digest", digestObj);
OPTOBJ("-file", fileObj);
OPTOBJ("-filename", fileObj);
OPTOBJ("-key", keyObj);
OPTSTR("-mac", macName);
OPTOBJ("-mac", macObj);
OPTBAD("option", "-bin, -channel, -cipher, -command, -data, -digest, -file, -filename, -hex, -key, or -mac");
return TCL_ERROR;
}
/* If only 1 arg left, it's the data */
if (idx < objc && dataObj == NULL) {
dataObj = objv[idx];
}
/* Get cipher */
if (cipherName != NULL) {
if (cipherObj != NULL) {
cipher = EVP_get_cipherbyname(cipherName);
type = TYPE_CMAC;
if (cipher == NULL) {
if ((cipher = GetCipher(interp, cipherObj, &type)) == NULL) {
Tcl_AppendResult(interp, "Invalid cipher \"", cipherName, "\"", NULL);
return TCL_ERROR;
}
} else if (type == TYPE_CMAC) {
Tcl_AppendResult(interp, "No cipher specified", NULL);
return TCL_ERROR;
}
/* Get message digest */
if (digestName != NULL) {
if (digestObj != NULL) {
md = EVP_get_digestbyname(digestName);
if (md == NULL) {
if ((md = GetDigest(interp, digestObj, &format)) == NULL) {
Tcl_AppendResult(interp, "Invalid digest \"", digestName, "\"", NULL);
return TCL_ERROR;
} else if (md == EVP_shake128() || md == EVP_shake256()) {
format |= IS_XOF;
}
} else if (type == TYPE_MD || type == TYPE_HMAC) {
Tcl_AppendResult(interp, "No digest specified", NULL);
return TCL_ERROR;
}
/* Get key */
if (keyObj != NULL) {
if (type == TYPE_MD) {
type = TYPE_HMAC;
if (GetKey(interp, keyObj, &type) == NULL) {
return TCL_ERROR;
}
} else if (type != TYPE_MD) {
Tcl_AppendResult(interp, "No key specified", NULL);
return TCL_ERROR;
}
/* Get MAC */
if (macName != NULL) {
if (macObj != NULL) {
if (strcmp(macName, "cmac") == 0) {
type = TYPE_CMAC;
if ((mac = GetMAC(interp, macObj, &type)) == NULL) {
} else if (strcmp(macName, "hmac") == 0) {
type = TYPE_HMAC;
} else {
Tcl_AppendResult(interp, "Invalid MAC \"", macName, "\"", NULL);
return TCL_ERROR;
}
} else if (type == TYPE_MAC) {
Tcl_AppendResult(interp, "No MAC specified", NULL);
return TCL_ERROR;
}
/* Calc digest on file, stacked channel, using instance command, or data blob */
if (fileObj != NULL) {
res = DigestFileHandler(interp, fileObj, md, cipher, format | type, keyObj);
res = DigestFileHandler(interp, fileObj, md, cipher, format | type, keyObj, mac);
} else if (channel != NULL) {
res = DigestChannelHandler(interp, channel, md, cipher, format | type, keyObj);
res = DigestChannelHandler(interp, channel, md, cipher, format | type, keyObj, mac);
} else if (cmdObj != NULL) {
res = DigestCommandHandler(interp, cmdObj, md, cipher, format | type, keyObj);
res = DigestCommandHandler(interp, cmdObj, md, cipher, format | type, keyObj, mac);
} else if (dataObj != NULL) {
res = DigestDataHandler(interp, dataObj, md, cipher, format | type, keyObj);
res = DigestDataHandler(interp, dataObj, md, cipher, format | type, keyObj, mac);
} else {
Tcl_AppendResult(interp, "No operation specified: Use -channel, -command, -data, or -file option", NULL);
res = TCL_ERROR;
}
return res;
}
|
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
|
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
|
-
+
-
+
-
+
-
+
-
+
|
Tcl_WrongNumArgs(interp, 1, objv, "data"); \
return TCL_ERROR; \
} \
}
int MD4ObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
validate_argc(objc, objv);
return DigestDataHandler(interp, objv[1], EVP_md4(), NULL, HEX_FORMAT | TYPE_MD, NULL);
return DigestDataHandler(interp, objv[1], EVP_md4(), NULL, HEX_FORMAT | TYPE_MD, NULL, NULL);
}
int MD5ObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
validate_argc(objc, objv);
return DigestDataHandler(interp, objv[1], EVP_md5(), NULL, HEX_FORMAT | TYPE_MD, NULL);
return DigestDataHandler(interp, objv[1], EVP_md5(), NULL, HEX_FORMAT | TYPE_MD, NULL, NULL);
}
int SHA1ObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
validate_argc(objc, objv);
return DigestDataHandler(interp, objv[1], EVP_sha1(), NULL, HEX_FORMAT | TYPE_MD, NULL);
return DigestDataHandler(interp, objv[1], EVP_sha1(), NULL, HEX_FORMAT | TYPE_MD, NULL, NULL);
}
int SHA256ObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
validate_argc(objc, objv);
return DigestDataHandler(interp, objv[1], EVP_sha256(), NULL, HEX_FORMAT | TYPE_MD, NULL);
return DigestDataHandler(interp, objv[1], EVP_sha256(), NULL, HEX_FORMAT | TYPE_MD, NULL, NULL);
}
int SHA512ObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
validate_argc(objc, objv);
return DigestDataHandler(interp, objv[1], EVP_sha512(), NULL, HEX_FORMAT | TYPE_MD, NULL);
return DigestDataHandler(interp, objv[1], EVP_sha512(), NULL, HEX_FORMAT | TYPE_MD, NULL, NULL);
}
/*
*-------------------------------------------------------------------
*
* Tls_DigestCommands --
*
|