Check-in [968f8bf25b]
Overview
Comment:Added description and provider to cipher, digest, mac, and pkey info commands. This only work for OpenSSL 3.0+.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | crypto
Files: files | file ages | folders
SHA3-256: 968f8bf25be359fbe919b3393e079b381e36fdd0269a66d5c93b5b2065202012
User & Date: bohagan on 2024-03-31 20:57:43
Other Links: branch diff | manifest | tags
Context
2024-04-07
20:52
Added RC4 stream cipher test vectors check-in: 6e07821c1e user: bohagan tags: crypto
2024-03-31
20:57
Added description and provider to cipher, digest, mac, and pkey info commands. This only work for OpenSSL 3.0+. check-in: 968f8bf25b user: bohagan tags: crypto
03:12
Added DES test vectors check-in: 66cee0f83a user: bohagan tags: crypto
Changes
68
69
70
71
72
73
74

75
76
77
78

79



80
81
82
83
84
85
86

87
88
89


90


91
92

93



94
95
96
97

98



99
100
101
102
103
104
105
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

91
92
93

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121







+




+

+
+
+






-
+


-
+
+

+
+


+

+
+
+




+

+
+
+







 *
 *-------------------------------------------------------------------
 */
int CipherInfo(Tcl_Interp *interp, Tcl_Obj *nameObj) {
    const EVP_CIPHER *cipher;
    Tcl_Obj *resultObj, *listObj;
    unsigned long flags, mode;
    int res = TCL_OK;
    char *modeName = NULL;
    char *name = Tcl_GetString(nameObj);

    /* Get cipher */
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    cipher = EVP_get_cipherbyname(name);
#else
    cipher = EVP_CIPHER_fetch(NULL, name, NULL);
#endif

    if (cipher == NULL) {
	Tcl_AppendResult(interp, "Invalid cipher \"", name, "\"", (char *) NULL);
	return TCL_ERROR;
    }

    /* Get properties */
    /* Create result object */
    resultObj = Tcl_NewListObj(0, NULL);
    if (resultObj == NULL) {
	return TCL_ERROR;
	res = TCL_ERROR;
	goto done;
    }

    /* Get properties */
    LAPPEND_STR(interp, resultObj, "nid", OBJ_nid2ln(EVP_CIPHER_nid(cipher)), -1);
    LAPPEND_STR(interp, resultObj, "name", EVP_CIPHER_name(cipher), -1);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "description", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "description", EVP_CIPHER_get0_description(cipher), -1);
#endif
    LAPPEND_INT(interp, resultObj, "block_size", EVP_CIPHER_block_size(cipher));
    LAPPEND_INT(interp, resultObj, "key_length", EVP_CIPHER_key_length(cipher));
    LAPPEND_INT(interp, resultObj, "iv_length", EVP_CIPHER_iv_length(cipher));
    LAPPEND_STR(interp, resultObj, "type", OBJ_nid2ln(EVP_CIPHER_type(cipher)), -1);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "provider", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "provider", OSSL_PROVIDER_get0_name(EVP_CIPHER_get0_provider(cipher)), -1);
#endif
    flags = EVP_CIPHER_flags(cipher);
    mode  = EVP_CIPHER_mode(cipher);

    /* EVP_CIPHER_get_mode */
    switch(mode) {
	case EVP_CIPH_STREAM_CIPHER:
	    modeName = "STREAM";
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
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
203
204
205
206

207
208
209
210
211
212
213
214







+







+
+
+














+
+
+
+
+
-
+








    /* CTX only properties */
    {
	EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
	int tag_len = 0;

	EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
	if (mode == EVP_CIPH_GCM_MODE || mode == EVP_CIPH_OCB_MODE) {
	    tag_len = EVP_GCM_TLS_TAG_LEN; /* EVP_MAX_AEAD_TAG_LENGTH */
	} else if (mode == EVP_CIPH_CCM_MODE) {
	    tag_len = EVP_CCM_TLS_TAG_LEN;
	} else if (cipher == EVP_get_cipherbyname("chacha20-poly1305")) {
	    tag_len = EVP_CHACHAPOLY_TLS_TAG_LEN; /* POLY1305_BLOCK_SIZE */
	}
#else
	tag_len = EVP_CIPHER_CTX_get_tag_length(ctx);
#endif
	EVP_CIPHER_CTX_free(ctx);
	LAPPEND_INT(interp, resultObj, "tag_length", tag_len);
    }
    
    /* AEAD properties */
    {
	int aad_len = 0;
	if (flags & EVP_CIPH_FLAG_AEAD_CIPHER) {
	    aad_len = EVP_AEAD_TLS1_AAD_LEN;
	}
	LAPPEND_INT(interp, resultObj, "aad_length", aad_len);
    }

    Tcl_SetObjResult(interp, resultObj);

done:
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_CIPHER_free(cipher);
#endif
    return TCL_OK;
    return res;
}

/*
 *-------------------------------------------------------------------
 *
 * CipherList --
 *
451
452
453
454
455
456
457

458
459
460

461



462
463
464
465
466
467
468
469
470
471


472
473

474



475
476
477
478
479





480
481
482
483
484
485
486
487
488
489
490
491
492





493

494
495
496
497
498
499
500
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

501
502
503
504
505
506
507
508
509
510
511

512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536

537
538
539
540
541
542
543
544







+



+

+
+
+









-
+
+


+

+
+
+


-


+
+
+
+
+













+
+
+
+
+
-
+







 *
 *-------------------------------------------------------------------
 */
int DigestInfo(Tcl_Interp *interp, Tcl_Obj *nameObj) {
    const EVP_MD *md;
    Tcl_Obj *resultObj, *listObj;
    unsigned long flags;
    int res = TCL_OK;
    char *name = Tcl_GetString(nameObj);

    /* Get message digest */
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    md = EVP_get_digestbyname(name);
#else
    md = EVP_MD_fetch(NULL, name, NULL);
#endif

    if (md == NULL) {
	Tcl_AppendResult(interp, "Invalid digest \"", name, "\"", (char *) NULL);
	return TCL_ERROR;
    }

    /* Get properties */
    resultObj = Tcl_NewListObj(0, NULL);
    if (resultObj == NULL) {
	return TCL_ERROR;
	res = TCL_ERROR;
	goto done;
    }
    LAPPEND_STR(interp, resultObj, "name", EVP_MD_name(md), -1);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "description", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "description", EVP_MD_get0_description(md), -1);
#endif
    LAPPEND_INT(interp, resultObj, "size", EVP_MD_size(md));
    LAPPEND_INT(interp, resultObj, "block_size", EVP_MD_block_size(md));
    LAPPEND_STR(interp, resultObj, "provider", "", -1);
    LAPPEND_STR(interp, resultObj, "type", OBJ_nid2ln(EVP_MD_type(md)), -1);
    LAPPEND_STR(interp, resultObj, "pkey_type", OBJ_nid2ln(EVP_MD_pkey_type(md)), -1);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "provider", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "provider", OSSL_PROVIDER_get0_name(EVP_MD_get0_provider(md)), -1);
#endif
    flags = EVP_MD_flags(md);

    /* Flags */
    listObj = Tcl_NewListObj(0, NULL);
    LAPPEND_BOOL(interp, listObj, "One-shot", flags & EVP_MD_FLAG_ONESHOT);
    LAPPEND_BOOL(interp, listObj, "XOF", flags & EVP_MD_FLAG_XOF);
    LAPPEND_BOOL(interp, listObj, "DigestAlgorithmId_NULL", flags & EVP_MD_FLAG_DIGALGID_NULL);
    LAPPEND_BOOL(interp, listObj, "DigestAlgorithmId_Abscent", flags & EVP_MD_FLAG_DIGALGID_ABSENT);
    LAPPEND_BOOL(interp, listObj, "DigestAlgorithmId_Custom", flags & EVP_MD_FLAG_DIGALGID_CUSTOM);
    LAPPEND_BOOL(interp, listObj, "FIPS", flags & EVP_MD_FLAG_FIPS);
    LAPPEND_OBJ(interp, resultObj, "flags", listObj);

    Tcl_SetObjResult(interp, resultObj);

done:
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_MD_free(md);
#endif
    return TCL_OK;
    return res;
}

/*
 *-------------------------------------------------------------------
 *
 * DigestList --
 *
646
647
648
649
650
651
652

653
654
655
656








657
658
659
660
661


662

663
664
665





666
667





668
669
670
671
672
673
674
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713

714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739







+




+
+
+
+
+
+
+
+




-
+
+

+



+
+
+
+
+


+
+
+
+
+







 *-------------------------------------------------------------------
 */
int MacInfo(Tcl_Interp *interp, Tcl_Obj *nameObj) {
    Tcl_Obj *resultObj;
    int res = TCL_OK;
    char *name = Tcl_GetString(nameObj);

#if OPENSSL_VERSION_NUMBER < 0x30000000L
    if (strcmp(name, "cmac") != 0 && strcmp(name, "hmac") != 0) {
	Tcl_AppendResult(interp, "Invalid MAC \"", name, "\"", (char *) NULL);
	return TCL_ERROR;
    }
#else
    EVP_MAC *mac = EVP_MAC_fetch(NULL, name, NULL);

    if (mac == NULL) {
	Tcl_AppendResult(interp, "Invalid MAC \"", name, "\"", (char *) NULL);
	return TCL_ERROR;
    }
#endif

    /* Get properties */
    resultObj = Tcl_NewListObj(0, NULL);
    if (resultObj == NULL) {
	return TCL_ERROR;
	res = TCL_ERROR;
	goto done;
    }
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "name", name, -1);
    LAPPEND_STR(interp, resultObj, "description", "", -1);
    LAPPEND_STR(interp, resultObj, "provider", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "name", EVP_MAC_get0_name(mac), -1);
    LAPPEND_STR(interp, resultObj, "description", EVP_MAC_get0_description(mac), -1);
    LAPPEND_STR(interp, resultObj, "provider", OSSL_PROVIDER_get0_name(EVP_MAC_get0_provider(mac)), -1);
#endif

    Tcl_SetObjResult(interp, resultObj);

done:
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_MAC_free(mac);
#endif
    return res;
}

/*
 *-------------------------------------------------------------------
 *
 * MacList --
760
761
762
763
764
765
766
767


768
769

770



771
772
773
774


775


776



777
778
779
780
781
782
783
784
785





786
787
788
789
790
791
792
825
826
827
828
829
830
831

832
833
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
869
870
871
872
873







-
+
+


+

+
+
+




+
+

+
+
-
+
+
+









+
+
+
+
+







	Tcl_AppendResult(interp, "Invalid public key method \"", name, "\"", (char *) NULL);
	return TCL_ERROR;
    }

    /* Get properties */
    resultObj = Tcl_NewListObj(0, NULL);
    if (resultObj == NULL) {
	return TCL_ERROR;
	res = TCL_ERROR;
	goto done;
    }
    LAPPEND_STR(interp, resultObj, "name", OBJ_nid2ln(EVP_PKEY_id(pkey)), -1);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "description", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "description", EVP_PKEY_get0_description(pkey), -1);
#endif
    LAPPEND_INT(interp, resultObj, "size", EVP_PKEY_size(pkey));
    LAPPEND_INT(interp, resultObj, "bits", EVP_PKEY_bits(pkey));
    LAPPEND_INT(interp, resultObj, "security_bits", EVP_PKEY_security_bits(pkey));
    LAPPEND_STR(interp, resultObj, "baseId", OBJ_nid2ln(EVP_PKEY_base_id(pkey)), -1);
    LAPPEND_STR(interp, resultObj, "type", OBJ_nid2ln(EVP_PKEY_type(EVP_PKEY_id(pkey))), -1);
#if OPENSSL_VERSION_NUMBER < 0x30000000L
    LAPPEND_STR(interp, resultObj, "provider", "", -1);
#else
    LAPPEND_STR(interp, resultObj, "provider", OSSL_PROVIDER_get0_name(EVP_PKEY_get0_provider(pkey)), -1);
    LAPPEND_STR(interp, resultObj, "type", OBJ_nid2ln(EVP_PKEY_type(EVP_PKEY_id(pkey))), -1);
    LAPPEND_STR(interp, resultObj, "type_name", EVP_PKEY_get0_type_name(pkey), -1);
    LAPPEND_BOOL(interp, resultObj, "can_sign", EVP_PKEY_can_sign(pkey));
#endif

    {
	int pnid;
	if (EVP_PKEY_get_default_digest_nid(pkey, &pnid) > 0) {
	    LAPPEND_STR(interp, resultObj, "default_digest", OBJ_nid2ln(pnid), -2);
	}
    }

    Tcl_SetObjResult(interp, resultObj);

done:
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
    EVP_PKEY_free(pkey);
#endif
    return res;
}

/*
 *-------------------------------------------------------------------
 *
 * PkeyList --