Check-in [24e26c8844]
Overview
Comment:Code Cleanup to use switch statements
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | crypto
Files: files | file ages | folders
SHA3-256: 24e26c8844c8ad79ccffa1fd30d5c2c596cebca204845eb8505aa40ddb40f94e
User & Date: bohagan on 2023-11-27 02:39:19
Other Links: branch diff | manifest | tags
Context
2023-12-02
04:25
Added function to encrypt and decrypt data check-in: b395f93924 user: bohagan tags: crypto
2023-11-27
02:39
Code Cleanup to use switch statements check-in: 24e26c8844 user: bohagan tags: crypto
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
Changes
48
49
50
51
52
53
54
55
56


57
58
59
60
61
62
63
48
49
50
51
52
53
54


55
56
57
58
59
60
61
62
63







-
-
+
+







	int flags;		/* Chan config flags */
	int watchMask;		/* Current WatchProc mask */
	int mode;		/* Current mode of parent channel */
	int format;		/* Digest format and operation */

	Tcl_Interp *interp;	/* Current interpreter */
	EVP_MD_CTX *ctx;	/* MD Context */
	HMAC_CTX *hctx;		/* HMAC Context */
	CMAC_CTX *cctx;		/* CMAC Context */
	HMAC_CTX *hctx;		/* HMAC context */
	CMAC_CTX *cctx;		/* CMAC context */
	Tcl_Command token;	/* Command token */
} DigestState;

/*
 *-------------------------------------------------------------------
 *
 * DigestStateNew --
150
151
152
153
154
155
156
157
158



159
160

161

162
163

164

165
166

167

168
169
170
171
172
173
174
175
176
177
178
179



180

181

182

183

184

185

186
187
188
189
190
191
192
150
151
152
153
154
155
156


157
158
159
160
161
162

163
164
165
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182


183
184
185
186
187

188
189
190

191
192
193
194
195
196
197
198
199
200
201
202







-
-
+
+
+


+
-
+


+
-
+


+

+










-
-
+
+
+

+
-
+

+
-
+

+

+







 *-------------------------------------------------------------------
 */
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) {
    /* Create contexts */
    switch(statePtr->format & 0xFF0) {
    case TYPE_MD:
	statePtr->ctx = EVP_MD_CTX_new();
	res = (statePtr->ctx != NULL);
	break;
    } else if (statePtr->format & TYPE_HMAC) {
    case TYPE_HMAC:
	statePtr->hctx = HMAC_CTX_new();
	res = (statePtr->hctx != NULL);
	break;
    } else if (statePtr->format & TYPE_CMAC) {
    case TYPE_CMAC:
	statePtr->cctx = CMAC_CTX_new();
	res = (statePtr->cctx != NULL);
	break;
    }

    if (!res) {
	Tcl_AppendResult(interp, "Create context failed: ", REASON(), NULL);
	return TCL_ERROR;
    }

    /* Get key */
    if (keyObj != NULL) {
	key = Tcl_GetByteArrayFromObj(keyObj, &key_len);
    }

    /* Initialize hash function */
    if (statePtr->format & TYPE_MD) {
    /* Initialize cryptography function */
    switch(statePtr->format & 0xFF0) {
    case TYPE_MD:
	res = EVP_DigestInit_ex(statePtr->ctx, md, NULL);
	break;
    } else if (statePtr->format & TYPE_HMAC) {
    case TYPE_HMAC:
	res = HMAC_Init_ex(statePtr->hctx, (const void *) key, key_len, md, NULL);
	break;
    } else if (statePtr->format & TYPE_CMAC) {
    case TYPE_CMAC:
	res = CMAC_Init(statePtr->cctx, (const void *) key, key_len, cipher, NULL);
	break;
    }

    if (!res) {
	Tcl_AppendResult(interp, "Initialize failed: ", REASON(), NULL);
	return TCL_ERROR;
    }
    return TCL_OK;
}

204
205
206
207
208
209
210
211
212
213
214
215
216










217

218
219
220
221
222
223
224
214
215
216
217
218
219
220






221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239







-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+

+







 *	Adds buf data to hash function or sets result to error message
 *
 *-------------------------------------------------------------------
 */
int DigestUpdate(DigestState *statePtr, char *buf, size_t read, int do_result) {
    int res = 0;

    if (statePtr->format & TYPE_MD) {
	res = EVP_DigestUpdate(statePtr->ctx, buf, read);
    } else if (statePtr->format & TYPE_HMAC) {
	res = HMAC_Update(statePtr->hctx, buf, read);
    } else if (statePtr->format & TYPE_CMAC) {
	res = CMAC_Update(statePtr->cctx, buf, read);
    switch(statePtr->format & 0xFF0) {
    case TYPE_MD:
        res = EVP_DigestUpdate(statePtr->ctx, buf, read);
	break;
    case TYPE_HMAC:
        res = HMAC_Update(statePtr->hctx, buf, read);
	break;
    case TYPE_CMAC:
        res = CMAC_Update(statePtr->cctx, buf, read);
	break;
    }

    if (!res && do_result) {
	Tcl_AppendResult(statePtr->interp, "Update failed: ", REASON(), NULL);
	return TCL_ERROR;
    }
    return res;
}

236
237
238
239
240
241
242
243
244


245
246
247



248
249


250
251


252
253
254
255
256
257
258
259
260










261
262
263
264
265
266
267
251
252
253
254
255
256
257


258
259
260


261
262
263
264

265
266
267

268
269
270








271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287







-
-
+
+

-
-
+
+
+

-
+
+

-
+
+

-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+







 * Side effects:
 *	Sets result to message digest or an error message.
 *
 *-------------------------------------------------------------------
 */
int DigestFinalize(Tcl_Interp *interp, DigestState *statePtr, Tcl_Obj **resultObj) {
    unsigned char md_buf[EVP_MAX_MD_SIZE];
    unsigned int md_len;
    int res = 0;
    unsigned int ulen;
    int res = 0, md_len = 0;

    /* Finalize hash function and calculate message digest */
    if (statePtr->format & TYPE_MD) {
    /* Finalize cryptography function and get result */
    switch(statePtr->format & 0xFF0) {
    case TYPE_MD:
	if (!(statePtr->format & IS_XOF)) {
	    res = EVP_DigestFinal_ex(statePtr->ctx, md_buf, &md_len);
	    res = EVP_DigestFinal_ex(statePtr->ctx, md_buf, &ulen);
	    md_len = (int) ulen;
	} else {
	    res = EVP_DigestFinalXOF(statePtr->ctx, md_buf, EVP_MAX_MD_SIZE);
	    res = EVP_DigestFinalXOF(statePtr->ctx, md_buf, (size_t) EVP_MAX_MD_SIZE);
	    md_len = EVP_MAX_MD_SIZE;
	}

    } else if (statePtr->format & TYPE_HMAC) {
	res = HMAC_Final(statePtr->hctx, md_buf, &md_len);

    } else if (statePtr->format & TYPE_CMAC) {
	size_t len;
	res = CMAC_Final(statePtr->cctx, md_buf, &len);
	md_len = (unsigned int) len;
	break;
    case TYPE_HMAC:
	res = HMAC_Final(statePtr->hctx, md_buf, &ulen);
	md_len = (int) ulen;
	break;
    case TYPE_CMAC:
	size_t size;
	res = CMAC_Final(statePtr->cctx, md_buf, &size);
	md_len = (int) size;
	break;
    }

    if (!res) {
	if (resultObj == NULL) {
	    Tcl_AppendResult(interp, "Finalize failed: ", REASON(), NULL);
	}
	return TCL_ERROR;
276
277
278
279
280
281
282
283

284
285
286
287
288
289
290
296
297
298
299
300
301
302

303
304
305
306
307
308
309
310







-
+







	    Tcl_IncrRefCount(*resultObj);
	}

    } else {
	Tcl_Obj *newObj = Tcl_NewObj();
	unsigned char *ptr = Tcl_SetByteArrayLength(newObj, md_len*2);

	for (unsigned int i = 0; i < md_len; i++) {
	for (int i = 0; i < md_len; i++) {
	    *ptr++ = hex[(md_buf[i] >> 4) & 0x0F];
	    *ptr++ = hex[md_buf[i] & 0x0F];
	}

	if (resultObj == NULL) {
	    Tcl_SetObjResult(interp, newObj);
	} else {