Skip to content

Commit ac447d1

Browse files
Merge pull request #7031 from douzzer/20231201-openssl-compat-fixes
20231201-openssl-compat-fixes
2 parents f708d42 + 803b17a commit ac447d1

19 files changed

Lines changed: 1065 additions & 255 deletions

File tree

doc/dox_comments/header_files/aes.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,82 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out,
658658
const byte* authTag, word32 authTagSz,
659659
const byte* authIn, word32 authInSz);
660660

661+
/*!
662+
\ingroup AES
663+
664+
\brief This is to initialize an AES-XTS context. It is up to user to call
665+
wc_AesXtsFree on aes key when done.
666+
667+
\return 0 Success
668+
669+
\param aes AES keys for encrypt/decrypt process
670+
\param heap heap hint to use for memory. Can be NULL
671+
\param devId id to use with async crypto. Can be 0
672+
673+
_Example_
674+
\code
675+
XtsAes aes;
676+
677+
if(wc_AesXtsInit(&aes, NULL, 0) != 0)
678+
{
679+
// Handle error
680+
}
681+
if(wc_AesXtsSetKeyNoInit(&aes, key, sizeof(key), AES_ENCRYPTION) != 0)
682+
{
683+
// Handle error
684+
}
685+
wc_AesXtsFree(&aes);
686+
\endcode
687+
688+
\sa wc_AesXtsSetKey
689+
\sa wc_AesXtsSetKeyNoInit
690+
\sa wc_AesXtsEncrypt
691+
\sa wc_AesXtsDecrypt
692+
\sa wc_AesXtsFree
693+
*/
694+
int wc_AesXtsInit(XtsAes* aes, void* heap, int devId);
695+
696+
697+
/*!
698+
\ingroup AES
699+
700+
\brief This is to help with setting keys to correct encrypt or decrypt type,
701+
after first calling wc_AesXtsInit(). It is up to user to call wc_AesXtsFree
702+
on aes key when done.
703+
704+
\return 0 Success
705+
706+
\param aes AES keys for encrypt/decrypt process
707+
\param key buffer holding aes key | tweak key
708+
\param len length of key buffer in bytes. Should be twice that of
709+
key size.
710+
i.e. 32 for a 16 byte key.
711+
\param dir direction, either AES_ENCRYPTION or AES_DECRYPTION
712+
713+
_Example_
714+
\code
715+
XtsAes aes;
716+
717+
if(wc_AesXtsInit(&aes, NULL, 0) != 0)
718+
{
719+
// Handle error
720+
}
721+
if(wc_AesXtsSetKeyNoInit(&aes, key, sizeof(key), AES_ENCRYPTION, NULL, 0)
722+
!= 0)
723+
{
724+
// Handle error
725+
}
726+
wc_AesXtsFree(&aes);
727+
\endcode
728+
729+
\sa wc_AesXtsEncrypt
730+
\sa wc_AesXtsDecrypt
731+
\sa wc_AesXtsFree
732+
*/
733+
int wc_AesXtsSetKeyNoInit(XtsAes* aes, const byte* key,
734+
word32 len, int dir);
735+
736+
661737
/*!
662738
\ingroup AES
663739
@@ -686,6 +762,8 @@ int wc_AesCcmDecrypt(Aes* aes, byte* out,
686762
wc_AesXtsFree(&aes);
687763
\endcode
688764
765+
\sa wc_AesXtsInit
766+
\sa wc_AesXtsSetKeyNoInit
689767
\sa wc_AesXtsEncrypt
690768
\sa wc_AesXtsDecrypt
691769
\sa wc_AesXtsFree
@@ -726,6 +804,8 @@ int wc_AesXtsSetKey(XtsAes* aes, const byte* key,
726804
727805
\sa wc_AesXtsEncrypt
728806
\sa wc_AesXtsDecrypt
807+
\sa wc_AesXtsInit
808+
\sa wc_AesXtsSetKeyNoInit
729809
\sa wc_AesXtsSetKey
730810
\sa wc_AesXtsFree
731811
*/
@@ -765,6 +845,8 @@ int wc_AesXtsEncryptSector(XtsAes* aes, byte* out,
765845
766846
\sa wc_AesXtsEncrypt
767847
\sa wc_AesXtsDecrypt
848+
\sa wc_AesXtsInit
849+
\sa wc_AesXtsSetKeyNoInit
768850
\sa wc_AesXtsSetKey
769851
\sa wc_AesXtsFree
770852
*/
@@ -805,6 +887,8 @@ int wc_AesXtsDecryptSector(XtsAes* aes, byte* out,
805887
\endcode
806888
807889
\sa wc_AesXtsDecrypt
890+
\sa wc_AesXtsInit
891+
\sa wc_AesXtsSetKeyNoInit
808892
\sa wc_AesXtsSetKey
809893
\sa wc_AesXtsFree
810894
*/
@@ -844,6 +928,8 @@ int wc_AesXtsEncrypt(XtsAes* aes, byte* out,
844928
\endcode
845929
846930
\sa wc_AesXtsEncrypt
931+
\sa wc_AesXtsInit
932+
\sa wc_AesXtsSetKeyNoInit
847933
\sa wc_AesXtsSetKey
848934
\sa wc_AesXtsFree
849935
*/
@@ -872,6 +958,8 @@ int wc_AesXtsDecrypt(XtsAes* aes, byte* out,
872958
873959
\sa wc_AesXtsEncrypt
874960
\sa wc_AesXtsDecrypt
961+
\sa wc_AesXtsInit
962+
\sa wc_AesXtsSetKeyNoInit
875963
\sa wc_AesXtsSetKey
876964
*/
877965
int wc_AesXtsFree(XtsAes* aes);

doc/dox_comments/header_files/cmac.h

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
\sa wc_InitCmac_ex
2424
\sa wc_CmacUpdate
2525
\sa wc_CmacFinal
26+
\sa wc_CmacFinalNoFree
27+
\sa wc_CmacFree
2628
*/
2729
int wc_InitCmac(Cmac* cmac,
2830
const byte* key, word32 keySz,
@@ -55,6 +57,8 @@ int wc_InitCmac(Cmac* cmac,
5557
\sa wc_InitCmac_ex
5658
\sa wc_CmacUpdate
5759
\sa wc_CmacFinal
60+
\sa wc_CmacFinalNoFree
61+
\sa wc_CmacFree
5862
*/
5963
int wc_InitCmac_ex(Cmac* cmac,
6064
const byte* key, word32 keySz,
@@ -75,29 +79,74 @@ int wc_InitCmac_ex(Cmac* cmac,
7579
7680
\sa wc_InitCmac
7781
\sa wc_CmacFinal
82+
\sa wc_CmacFinalNoFree
83+
\sa wc_CmacFree
7884
*/
7985
int wc_CmacUpdate(Cmac* cmac,
8086
const byte* in, word32 inSz);
8187

88+
8289
/*!
8390
\ingroup CMAC
84-
\brief Generate the final result using Cipher-based Message Authentication Code
91+
\brief Generate the final result using Cipher-based Message Authentication Code, deferring context cleanup.
8592
\return 0 on success
8693
\param cmac pointer to the Cmac structure
8794
\param out pointer to return the result
8895
\param outSz pointer size of output (in/out)
8996
9097
_Example_
9198
\code
92-
ret = wc_CmacFinal(cmac, out, &outSz);
99+
ret = wc_CmacFinalNoFree(cmac, out, &outSz);
100+
(void)wc_CmacFree(cmac);
93101
\endcode
94102
95103
\sa wc_InitCmac
96104
\sa wc_CmacFinal
105+
\sa wc_CmacFinalNoFree
106+
\sa wc_CmacFree
97107
*/
98-
int wc_CmacFinal(Cmac* cmac,
108+
int wc_CmacFinalNoFree(Cmac* cmac,
99109
byte* out, word32* outSz);
100110

111+
/*!
112+
\ingroup CMAC
113+
\brief Generate the final result using Cipher-based Message Authentication Code, and clean up the context with wc_CmacFree().
114+
\return 0 on success
115+
\param cmac pointer to the Cmac structure
116+
\param out pointer to return the result
117+
\param outSz pointer size of output (in/out)
118+
119+
_Example_
120+
\code
121+
ret = wc_CmacFinal(cmac, out, &outSz);
122+
\endcode
123+
124+
\sa wc_InitCmac
125+
\sa wc_CmacFinalNoFree
126+
\sa wc_CmacFinalNoFree
127+
\sa wc_CmacFree
128+
*/
129+
int wc_CmacFinalNoFree(Cmac* cmac);
130+
131+
/*!
132+
\ingroup CMAC
133+
\brief Clean up allocations in a CMAC context.
134+
\return 0 on success
135+
\param cmac pointer to the Cmac structure
136+
137+
_Example_
138+
\code
139+
ret = wc_CmacFinalNoFree(cmac, out, &outSz);
140+
(void)wc_CmacFree(cmac);
141+
\endcode
142+
143+
\sa wc_InitCmac
144+
\sa wc_CmacFinalNoFree
145+
\sa wc_CmacFinal
146+
\sa wc_CmacFree
147+
*/
148+
int wc_CmacFree(Cmac* cmac);
149+
101150
/*!
102151
\ingroup CMAC
103152
\brief Single shot function for generating a CMAC

src/quic.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,7 @@ size_t wolfSSL_quic_get_aead_tag_len(const WOLFSSL_EVP_CIPHER* aead_cipher)
10551055
ret = 0;
10561056
}
10571057

1058+
(void)wolfSSL_EVP_CIPHER_CTX_cleanup(ctx);
10581059
#ifdef WOLFSSL_SMALL_STACK
10591060
XFREE(ctx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
10601061
#endif

src/ssl.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29829,6 +29829,8 @@ static int wolfSSL_TicketKeyCb(WOLFSSL* ssl,
2982929829
end:
2983029830

2983129831
(void)wc_HmacFree(&hmacCtx.hmac);
29832+
(void)wolfSSL_EVP_CIPHER_CTX_cleanup(evpCtx);
29833+
2983229834
#ifdef WOLFSSL_SMALL_STACK
2983329835
XFREE(evpCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
2983429836
#endif

src/ssl_crypto.c

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,14 +2079,10 @@ WOLFSSL_CMAC_CTX* wolfSSL_CMAC_CTX_new(void)
20792079
ctx = (WOLFSSL_CMAC_CTX*)XMALLOC(sizeof(WOLFSSL_CMAC_CTX), NULL,
20802080
DYNAMIC_TYPE_OPENSSL);
20812081
if (ctx != NULL) {
2082-
/* Allocate memory for wolfSSL CMAC object. */
2083-
ctx->internal = (Cmac*)XMALLOC(sizeof(Cmac), NULL, DYNAMIC_TYPE_CMAC);
2084-
if (ctx->internal == NULL) {
2085-
XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
2086-
ctx = NULL;
2087-
}
2088-
}
2089-
if (ctx != NULL) {
2082+
/* Memory for wolfSSL CMAC object is allocated in
2083+
* wolfSSL_CMAC_Init().
2084+
*/
2085+
ctx->internal = NULL;
20902086
/* Allocate memory for EVP cipher context object. */
20912087
ctx->cctx = wolfSSL_EVP_CIPHER_CTX_new();
20922088
if (ctx->cctx == NULL) {
@@ -2110,9 +2106,13 @@ void wolfSSL_CMAC_CTX_free(WOLFSSL_CMAC_CTX *ctx)
21102106
if (ctx != NULL) {
21112107
/* Deallocate dynamically allocated fields. */
21122108
if (ctx->internal != NULL) {
2109+
#if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
2110+
wc_CmacFree((Cmac*)ctx->internal);
2111+
#endif
21132112
XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
21142113
}
21152114
if (ctx->cctx != NULL) {
2115+
wolfSSL_EVP_CIPHER_CTX_cleanup(ctx->cctx);
21162116
wolfSSL_EVP_CIPHER_CTX_free(ctx->cctx);
21172117
}
21182118
/* Deallocate CMAC context object. */
@@ -2167,22 +2167,37 @@ int wolfSSL_CMAC_Init(WOLFSSL_CMAC_CTX* ctx, const void *key, size_t keySz,
21672167
/* Only AES-CBC ciphers are supported. */
21682168
if ((ret == 1) && (cipher != EVP_AES_128_CBC) &&
21692169
(cipher != EVP_AES_192_CBC) && (cipher != EVP_AES_256_CBC)) {
2170+
WOLFSSL_MSG("wolfSSL_CMAC_Init: requested cipher is unsupported");
21702171
ret = 0;
21712172
}
21722173
/* Key length must match cipher. */
21732174
if ((ret == 1) && ((int)keySz != wolfSSL_EVP_Cipher_key_length(cipher))) {
2175+
WOLFSSL_MSG("wolfSSL_CMAC_Init: "
2176+
"supplied key size doesn't match requested cipher");
21742177
ret = 0;
21752178
}
21762179

2180+
if ((ret == 1) && (ctx->internal == NULL)) {
2181+
/* Allocate memory for wolfSSL CMAC object. */
2182+
ctx->internal = (Cmac*)XMALLOC(sizeof(Cmac), NULL, DYNAMIC_TYPE_CMAC);
2183+
if (ctx->internal == NULL)
2184+
ret = 0;
2185+
}
2186+
21772187
/* Initialize the wolfCrypt CMAC object. */
21782188
if ((ret == 1) && (wc_InitCmac((Cmac*)ctx->internal, (const byte*)key,
21792189
(word32)keySz, WC_CMAC_AES, NULL) != 0)) {
2190+
WOLFSSL_MSG("wolfSSL_CMAC_Init: wc_InitCmac() failed");
2191+
XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
2192+
ctx->internal = NULL;
21802193
ret = 0;
21812194
}
21822195
if (ret == 1) {
21832196
/* Initialize the EVP cipher context object for encryption. */
21842197
ret = wolfSSL_EVP_CipherInit(ctx->cctx, cipher, (const byte*)key, NULL,
21852198
1);
2199+
if (ret != WOLFSSL_SUCCESS)
2200+
WOLFSSL_MSG("wolfSSL_CMAC_Init: wolfSSL_EVP_CipherInit() failed");
21862201
}
21872202

21882203
WOLFSSL_LEAVE("wolfSSL_CMAC_Init", ret);
@@ -2237,7 +2252,7 @@ int wolfSSL_CMAC_Final(WOLFSSL_CMAC_CTX* ctx, unsigned char* out, size_t* len)
22372252

22382253
WOLFSSL_ENTER("wolfSSL_CMAC_Final");
22392254

2240-
/* Valiudate parameters. */
2255+
/* Validate parameters. */
22412256
if (ctx == NULL) {
22422257
ret = 0;
22432258
}
@@ -2268,6 +2283,9 @@ int wolfSSL_CMAC_Final(WOLFSSL_CMAC_CTX* ctx, unsigned char* out, size_t* len)
22682283
else if (len != NULL) {
22692284
*len = (size_t)len32;
22702285
}
2286+
2287+
XFREE(ctx->internal, NULL, DYNAMIC_TYPE_CMAC);
2288+
ctx->internal = NULL;
22712289
}
22722290

22732291
WOLFSSL_LEAVE("wolfSSL_CMAC_Final", ret);
@@ -2899,7 +2917,7 @@ void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* in, WOLFSSL_DES_cblock* out,
28992917

29002918
#ifdef OPENSSL_EXTRA
29012919

2902-
#ifndef NO_AES
2920+
#if !defined(NO_AES) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
29032921

29042922
/* Sets the key into the AES key object for encryption or decryption.
29052923
*
@@ -3408,7 +3426,7 @@ size_t wolfSSL_CRYPTO_cts128_decrypt(const unsigned char *in,
34083426
return len;
34093427
}
34103428
#endif /* HAVE_CTS */
3411-
#endif /* NO_AES */
3429+
#endif /* !NO_AES && !WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API */
34123430
#endif /* OPENSSL_EXTRA */
34133431

34143432
/*******************************************************************************

0 commit comments

Comments
 (0)