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
|
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
|
+
+
+
+
+
+
+
+
+
-
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
/* Clean-up */
DigestStateFree(statePtr);
return res;
}
/*******************************************************************/
static const char *command_opts [] = { "-bin", "-binary", "-hex", "-hexadecimal",
"-chan", "-channel", "-cipher", "-command", "-data", "-digest", "-file", "-filename",
"-hash", "-key", "-mac", NULL};
enum _command_opts {
_opt_bin, _opt_binary, _opt_hex, _opt_hexadecimal, _opt_chan, _opt_channel, _opt_cipher,
_opt_command, _opt_data, _opt_digest, _opt_file, _opt_filename, _opt_hash, _opt_key, _opt_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;
int start = 1, format = HEX_FORMAT, res = TCL_OK, fn;
Tcl_Obj *cipherObj = NULL, *cmdObj = NULL, *dataObj = NULL, *digestObj = NULL;
Tcl_Obj *fileObj = NULL, *keyObj = NULL, *macObj = NULL;
const char *channel = NULL, *opt;
dprintf("Called");
/* 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] != '-') {
switch(type) {
case TYPE_MD:
if (type == TYPE_MD || type == TYPE_HMAC) {
digestObj = objv[start];
start++;
} else if (type == TYPE_CMAC) {
cipherObj = objv[start];
start++;
} else if (type == TYPE_MAC) {
macObj = objv[start];
start++;
case TYPE_HMAC:
digestObj = objv[start++];
break;
case TYPE_CMAC:
cipherObj = objv[start++];
break;
case TYPE_MAC:
macObj = objv[start++];
break;
}
}
/* Get options */
for (idx = start; idx < objc; idx++) {
opt = Tcl_GetStringFromObj(objv[idx], NULL);
for (int idx = start; idx < objc; idx++) {
/* Special case for when last arg is data */
if (idx == objc - 1) {
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);
OPTOBJ("-cipher", cipherObj);
OPTOBJ("-command", cmdObj);
OPTOBJ("-data", dataObj);
OPTOBJ("-digest", digestObj);
OPTOBJ("-file", fileObj);
OPTOBJ("-filename", fileObj);
OPTOBJ("-hash", digestObj);
OPTOBJ("-key", keyObj);
OPTOBJ("-mac", macObj);
if (opt[0] != '-' && dataObj == NULL) {
dataObj = objv[idx];
break;
}
}
/* Get option */
if (Tcl_GetIndexFromObj(interp, objv[idx], command_opts, "option", 0, &fn) != TCL_OK) {
return TCL_ERROR;
}
/* Validate arg has value */
if (fn > _opt_hexadecimal) {
if (++idx >= objc) {
Tcl_AppendResult(interp, "No value for option \"", command_opts[fn], "\"", (char *) NULL);
return TCL_ERROR;
}
}
switch(fn) {
case _opt_bin:
case _opt_binary:
format = BIN_FORMAT;
break;
case _opt_hex:
case _opt_hexadecimal:
format = HEX_FORMAT;
break;
case _opt_chan:
case _opt_channel:
GET_OPT_STRING(objv[idx], channel, NULL);
break;
case _opt_cipher:
cipherObj = objv[idx];
break;
case _opt_command:
cmdObj = objv[idx];
break;
case _opt_data:
dataObj = objv[idx];
break;
case _opt_digest:
case _opt_hash:
digestObj = objv[idx];
break;
case _opt_file:
case _opt_filename:
fileObj = objv[idx];
break;
case _opt_key:
keyObj = objv[idx];
break;
case _opt_mac:
macObj = objv[idx];
OPTBAD("option", "-bin, -channel, -cipher, -command, -data, -digest, -file, -filename, -hex, -key, or -mac");
return TCL_ERROR;
}
break;
}
/* If only 1 arg left, it's the data */
if (idx < objc && dataObj == NULL) {
dataObj = objv[idx];
}
/* Check types */
if (type == TYPE_MD && cipherObj != NULL) {
type = TYPE_CMAC;
} else if (type == TYPE_MD && keyObj != NULL) {
type = TYPE_HMAC;
if (type == TYPE_MD) {
if (macObj != NULL) {
type = TYPE_MAC;
} else if (cipherObj != NULL) {
type = TYPE_CMAC;
} else if (keyObj != NULL) {
type = TYPE_HMAC;
}
}
if (type == TYPE_MAC) {
if (macObj != NULL) {
char *macName = Tcl_GetStringFromObj(macObj, 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 TCL_ERROR;
}
} else {
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, digestObj, cipherObj, format | type, keyObj, macObj);
} else if (channel != NULL) {
res = DigestChannelHandler(interp, channel, digestObj, cipherObj, format | type, keyObj, macObj);
|
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
|
1382
1383
1384
1385
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
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
|
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
-
+
-
-
+
-
-
+
-
-
+
|
* TCL_OK or TCL_ERROR
*
* Side effects:
* Sets result to message digest or error message
*
*-------------------------------------------------------------------
*/
int TemplateCmd(Tcl_Interp *interp, int objc, Tcl_Obj *const objv[], char *digestName, int format) {
Tcl_Obj *dataObj, *digestObj;
int res;
#define VALIDATE_ARGC(objc, objv) { \
if (objc != 2) { \
Tcl_WrongNumArgs(interp, 1, objv, "data"); \
return TCL_ERROR; \
} \
if (objc == 2) {
dataObj = objv[1];
} else {
Tcl_WrongNumArgs(interp, 1, objv, "data");
return TCL_ERROR;
}
digestObj = Tcl_NewStringObj(digestName, -1);
Tcl_IncrRefCount(digestObj);
res = DigestDataHandler(interp, dataObj, digestObj, NULL, format, NULL, NULL);
Tcl_DecrRefCount(digestObj);
return res;
}
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, NULL);
return TemplateCmd(interp, objc, objv, "md4", HEX_FORMAT | TYPE_MD);
}
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, NULL);
return TemplateCmd(interp, objc, objv, "md5", HEX_FORMAT | TYPE_MD);
}
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, NULL);
return TemplateCmd(interp, objc, objv, "sha1", HEX_FORMAT | TYPE_MD);
}
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, NULL);
return TemplateCmd(interp, objc, objv, "sha256", HEX_FORMAT | TYPE_MD);
}
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, NULL);
return TemplateCmd(interp, objc, objv, "sha512", HEX_FORMAT | TYPE_MD);
}
/*
*-------------------------------------------------------------------
*
* Tls_DigestCommands --
*
|