Overview
Comment: | Split get digest, cipher, key, and mac ids into separate functions. Simplified DigestMain to pass Tcl objects to get functions. Pass mac id to functions. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | crypto |
Files: | files | file ages | folders |
SHA3-256: |
0aae4d5b6f248afb36cf5bbc2941b625 |
User & Date: | bohagan on 2023-11-25 05:23:07 |
Other Links: | branch diff | manifest | tags |
Context
2023-11-26
| ||
02:15 | Split doc file into TLS and cryptography files. Moved digest functions to cryptography.html doc file. Added schema file from TCL man pages. Updated TLS doc file. check-in: b5b7a4964e user: bohagan tags: crypto | |
2023-11-25
| ||
05:23 | Split get digest, cipher, key, and mac ids into separate functions. Simplified DigestMain to pass Tcl objects to get functions. Pass mac id to functions. check-in: 0aae4d5b6f user: bohagan tags: crypto | |
2023-11-23
| ||
04:17 | Added more test cases to check for errors, test mac command, etc. check-in: 4ba41f1db2 user: bohagan tags: crypto | |
Changes
Modified generic/tlsDigest.c
from [4e866a5357]
to [83013db897].
︙ | ︙ | |||
30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #define HEX_FORMAT 0x02 #define IS_XOF 0x08 #define TYPE_MD 0x10 #define TYPE_HMAC 0x20 #define TYPE_CMAC 0x40 #define TYPE_MAC 0x80 /* * This structure defines the per-instance state of a digest operation. */ typedef struct DigestState { Tcl_Channel self; /* This socket channel */ Tcl_TimerToken timer; /* Timer for read events */ | > > > > | 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #define HEX_FORMAT 0x02 #define IS_XOF 0x08 #define TYPE_MD 0x10 #define TYPE_HMAC 0x20 #define TYPE_CMAC 0x40 #define TYPE_MAC 0x80 #if OPENSSL_VERSION_NUMBER <= 0x30000000L #define EVP_MAC void #endif /* * This structure defines the per-instance state of a digest operation. */ typedef struct DigestState { Tcl_Channel self; /* This socket channel */ Tcl_TimerToken timer; /* Timer for read events */ |
︙ | ︙ | |||
142 143 144 145 146 147 148 | * * Side effects: * No result or error message * *------------------------------------------------------------------- */ int DigestInitialize(Tcl_Interp *interp, DigestState *statePtr, const EVP_MD *md, | | | 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | * * Side effects: * No result or error message * *------------------------------------------------------------------- */ int DigestInitialize(Tcl_Interp *interp, DigestState *statePtr, const EVP_MD *md, const EVP_CIPHER *cipher, Tcl_Obj *keyObj, EVP_MAC *mac) { int key_len = 0, res = 0; const unsigned char *key = NULL; /* Create message digest context */ if (statePtr->format & TYPE_MD) { statePtr->ctx = EVP_MD_CTX_new(); res = (statePtr->ctx != NULL); |
︙ | ︙ | |||
729 730 731 732 733 734 735 | * Side effects: * Adds transform to channel and sets result to channel id or error message. * *---------------------------------------------------------------------- */ static int DigestChannelHandler(Tcl_Interp *interp, const char *channel, const EVP_MD *md, | | | 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 | * Side effects: * Adds transform to channel and sets result to channel id or error message. * *---------------------------------------------------------------------- */ static int DigestChannelHandler(Tcl_Interp *interp, const char *channel, const EVP_MD *md, const EVP_CIPHER *cipher, int format, Tcl_Obj *keyObj, EVP_MAC *mac) { int mode; /* OR-ed combination of TCL_READABLE and TCL_WRITABLE */ Tcl_Channel chan; DigestState *statePtr; /* Validate args */ if (channel == (const char *) NULL) { return TCL_ERROR; |
︙ | ︙ | |||
757 758 759 760 761 762 763 | Tcl_AppendResult(interp, "Memory allocation error", (char *) NULL); return TCL_ERROR; } statePtr->self = chan; statePtr->mode = mode; /* Initialize hash function */ | | | 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | Tcl_AppendResult(interp, "Memory allocation error", (char *) NULL); return TCL_ERROR; } statePtr->self = chan; statePtr->mode = mode; /* Initialize hash function */ if (DigestInitialize(interp, statePtr, md, cipher, keyObj, mac) != TCL_OK) { return TCL_ERROR; } /* Configure channel */ Tcl_SetChannelOption(interp, chan, "-translation", "binary"); if (Tcl_GetChannelBufferSize(chan) < EVP_MAX_MD_SIZE * 2) { Tcl_SetChannelBufferSize(chan, EVP_MAX_MD_SIZE * 2); |
︙ | ︙ | |||
802 803 804 805 806 807 808 | static int DigestUnstackObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { Tcl_Channel chan; int mode; /* OR-ed combination of TCL_READABLE and TCL_WRITABLE */ /* Validate arg count */ if (objc != 2) { | | | 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 | static int DigestUnstackObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { Tcl_Channel chan; int mode; /* OR-ed combination of TCL_READABLE and TCL_WRITABLE */ /* Validate arg count */ if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "channelId"); return TCL_ERROR; } /* Get channel */ chan = Tcl_GetChannel(interp, Tcl_GetStringFromObj(objv[1], NULL), &mode); if (chan == (Tcl_Channel) NULL) { return TCL_ERROR; |
︙ | ︙ | |||
830 831 832 833 834 835 836 | /* Pop transform from channel */ return Tcl_UnstackChannel(interp, chan); clientData = clientData; } /*******************************************************************/ | < < > | 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | /* Pop transform from channel */ return Tcl_UnstackChannel(interp, chan); clientData = clientData; } /*******************************************************************/ /* *------------------------------------------------------------------- * * DigestInstanceObjCmd -- * * Handler for digest command instances. Used to add data to hash * function or retrieve message digest. * * Returns: * TCL_OK or TCL_ERROR * * Side effects: * Adds data to hash or returns message digest * *------------------------------------------------------------------- */ int DigestInstanceObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { DigestState *statePtr = (DigestState *) clientData; int fn, len = 0; char *buf = NULL; static const char *instance_fns [] = { "finalize", "update", NULL }; /* Validate arg count */ if (objc < 2 || objc > 3) { Tcl_WrongNumArgs(interp, 1, objv, "function ?data?"); return TCL_ERROR; } |
︙ | ︙ | |||
928 929 930 931 932 933 934 | * * Side effects: * Creates command or error message * *------------------------------------------------------------------- */ int DigestCommandHandler(Tcl_Interp *interp, Tcl_Obj *cmdObj, const EVP_MD *md, | | | | 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 | * * Side effects: * Creates command or error message * *------------------------------------------------------------------- */ int DigestCommandHandler(Tcl_Interp *interp, Tcl_Obj *cmdObj, const EVP_MD *md, const EVP_CIPHER *cipher, int format, Tcl_Obj *keyObj, EVP_MAC *mac) { DigestState *statePtr; char *cmdName = Tcl_GetStringFromObj(cmdObj, NULL); /* Create state data structure */ if ((statePtr = DigestStateNew(interp, format)) == NULL) { Tcl_AppendResult(interp, "Memory allocation error", (char *) NULL); return TCL_ERROR; } /* Initialize hash function */ if (DigestInitialize(interp, statePtr, md, cipher, keyObj, mac) != TCL_OK) { return TCL_ERROR; } /* Create instance command */ statePtr->token = Tcl_CreateObjCommand(interp, cmdName, DigestInstanceObjCmd, (ClientData) statePtr, DigestCommandDeleteHandler); |
︙ | ︙ | |||
972 973 974 975 976 977 978 | * Side effects: * Sets result to message digest or error message * *------------------------------------------------------------------- */ int DigestDataHandler(Tcl_Interp *interp, Tcl_Obj *dataObj, const EVP_MD *md, | | | | 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, 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, 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 */ |
︙ | ︙ | |||
1021 1022 1023 1024 1025 1026 1027 | * * Side effects: * Result is message digest or error message * *------------------------------------------------------------------- */ int DigestFileHandler(Tcl_Interp *interp, Tcl_Obj *filename, const EVP_MD *md, | | | 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | * * Side effects: * Result is message digest or error message * *------------------------------------------------------------------- */ int DigestFileHandler(Tcl_Interp *interp, Tcl_Obj *filename, const EVP_MD *md, const EVP_CIPHER *cipher, int format, Tcl_Obj *keyObj, EVP_MAC *mac) { DigestState *statePtr; Tcl_Channel chan = NULL; unsigned char buf[BUFFER_SIZE]; int res = TCL_OK, len; /* Create state data structure */ if ((statePtr = DigestStateNew(interp, format)) == NULL) { |
︙ | ︙ | |||
1047 1048 1049 1050 1051 1052 1053 | /* Configure channel */ if ((res = Tcl_SetChannelOption(interp, chan, "-translation", "binary")) == TCL_ERROR) { goto done; } Tcl_SetChannelBufferSize(chan, BUFFER_SIZE); /* Initialize hash function */ | | | 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | /* Configure channel */ if ((res = Tcl_SetChannelOption(interp, chan, "-translation", "binary")) == TCL_ERROR) { goto done; } Tcl_SetChannelBufferSize(chan, BUFFER_SIZE); /* Initialize hash function */ if ((res = DigestInitialize(interp, statePtr, md, cipher, keyObj, mac)) != TCL_OK) { goto done; } /* Read file data and update hash function */ while (!Tcl_Eof(chan)) { len = Tcl_ReadRaw(chan, (char *) buf, BUFFER_SIZE); if (len > 0) { |
︙ | ︙ | |||
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 | } /*******************************************************************/ /* *------------------------------------------------------------------- * * 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; | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | > | | | | | | | < < | < < | < | < < < | | < | < | < < < < | | | | | 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 *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) { digestObj = objv[start]; start++; } else if (type == TYPE_CMAC) { cipherObj = objv[start]; start++; } else if (type == TYPE_MAC) { 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); OPTOBJ("-cipher", cipherObj); OPTOBJ("-command", cmdObj); OPTOBJ("-data", dataObj); OPTOBJ("-digest", digestObj); OPTOBJ("-file", fileObj); OPTOBJ("-filename", fileObj); OPTOBJ("-key", keyObj); 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 (cipherObj != NULL) { if ((cipher = GetCipher(interp, cipherObj, &type)) == NULL) { return TCL_ERROR; } } else if (type == TYPE_CMAC) { Tcl_AppendResult(interp, "No cipher specified", NULL); return TCL_ERROR; } /* Get message digest */ if (digestObj != NULL) { if ((md = GetDigest(interp, digestObj, &format)) == NULL) { return TCL_ERROR; } } else if (type == TYPE_MD || type == TYPE_HMAC) { Tcl_AppendResult(interp, "No digest specified", NULL); return TCL_ERROR; } /* Get key */ if (keyObj != NULL) { 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 (macObj != NULL) { if ((mac = GetMAC(interp, macObj, &type)) == 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, mac); } else if (channel != NULL) { res = DigestChannelHandler(interp, channel, md, cipher, format | type, keyObj, mac); } else if (cmdObj != NULL) { res = DigestCommandHandler(interp, cmdObj, md, cipher, format | type, keyObj, mac); } else if (dataObj != NULL) { 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 | 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); | | | | | | | 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, 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, 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, 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, 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, NULL); } /* *------------------------------------------------------------------- * * Tls_DigestCommands -- * |
︙ | ︙ |