Skip to content

Commit 931ac4e

Browse files
committed
add documentation for wc_AesXtsInit(), wc_AesXtsSetKeyNoInit(), wc_CmacFinalNoFree(), and wc_CmacFree();
rename wc_AesXtsSetKey_NoInit() to wc_AesXtsSetKeyNoInit() for morphological consistency; refactor wc_AesXtsSetKey() to call wc_AesXtsSetKeyNoInit() and clean up on failure; readability tweak in wolfSSL_EVP_CipherFinal().
1 parent b14aba4 commit 931ac4e

6 files changed

Lines changed: 175 additions & 57 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

wolfcrypt/src/aes.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12283,7 +12283,7 @@ int wc_AesXtsInit(XtsAes* aes, void* heap, int devId)
1228312283
*
1228412284
* return 0 on success
1228512285
*/
12286-
int wc_AesXtsSetKey_NoInit(XtsAes* aes, const byte* key, word32 len, int dir)
12286+
int wc_AesXtsSetKeyNoInit(XtsAes* aes, const byte* key, word32 len, int dir)
1228712287
{
1228812288
word32 keySz;
1228912289
int ret = 0;
@@ -12317,7 +12317,7 @@ int wc_AesXtsSetKey_NoInit(XtsAes* aes, const byte* key, word32 len, int dir)
1231712317
return ret;
1231812318
}
1231912319

12320-
/* Combined call to wc_AesXtsInit() and wc_AesXtsSetKey_NoInit().
12320+
/* Combined call to wc_AesXtsInit() and wc_AesXtsSetKeyNoInit().
1232112321
*
1232212322
* Note: is up to user to call wc_AesXtsFree when done.
1232312323
*
@@ -12326,7 +12326,6 @@ int wc_AesXtsSetKey_NoInit(XtsAes* aes, const byte* key, word32 len, int dir)
1232612326
int wc_AesXtsSetKey(XtsAes* aes, const byte* key, word32 len, int dir,
1232712327
void* heap, int devId)
1232812328
{
12329-
word32 keySz;
1233012329
int ret = 0;
1233112330

1233212331
if (aes == NULL || key == NULL) {
@@ -12337,27 +12336,10 @@ int wc_AesXtsSetKey(XtsAes* aes, const byte* key, word32 len, int dir,
1233712336
if (ret != 0)
1233812337
return ret;
1233912338

12340-
keySz = len/2;
12341-
if (keySz != 16 && keySz != 32) {
12342-
WOLFSSL_MSG("Unsupported key size");
12343-
return WC_KEY_SIZE_E;
12344-
}
12339+
ret = wc_AesXtsSetKeyNoInit(aes, key, len, dir);
1234512340

12346-
if ((ret = wc_AesSetKey(&aes->aes, key, keySz, NULL, dir)) == 0) {
12347-
ret = wc_AesSetKey(&aes->tweak, key + keySz, keySz, NULL,
12348-
AES_ENCRYPTION);
12349-
if (ret != 0) {
12350-
wc_AesFree(&aes->aes);
12351-
}
12352-
#ifdef WOLFSSL_AESNI
12353-
if (aes->aes.use_aesni != aes->tweak.use_aesni) {
12354-
if (aes->aes.use_aesni)
12355-
aes->aes.use_aesni = 0;
12356-
else
12357-
aes->tweak.use_aesni = 0;
12358-
}
12359-
#endif
12360-
}
12341+
if (ret != 0)
12342+
wc_AesXtsFree(aes);
1236112343

1236212344
return ret;
1236312345
}

wolfcrypt/src/evp.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,11 @@ int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *out,
12261226
}
12271227
}
12281228

1229+
if (ret == 0)
1230+
ret = WOLFSSL_SUCCESS;
1231+
else
1232+
ret = WOLFSSL_FAILURE;
1233+
12291234
/* Reinitialize for subsequent wolfSSL_EVP_Cipher calls. */
12301235
if (wc_AesGcmInit(&ctx->cipher.aes, NULL, 0,
12311236
(byte*)ctx->cipher.aes.reg,
@@ -1234,12 +1239,6 @@ int wolfSSL_EVP_CipherFinal(WOLFSSL_EVP_CIPHER_CTX *ctx, unsigned char *out,
12341239
WOLFSSL_MSG("wc_AesGcmInit failed");
12351240
ret = WOLFSSL_FAILURE;
12361241
}
1237-
else {
1238-
if (ret == 0)
1239-
ret = WOLFSSL_SUCCESS;
1240-
else
1241-
ret = WOLFSSL_FAILURE;
1242-
}
12431242
#endif /* WOLFSSL_AESGCM_STREAM */
12441243
if (ret == WOLFSSL_SUCCESS) {
12451244
if (ctx->authIncIv) {
@@ -7498,7 +7497,7 @@ void wolfSSL_EVP_init(void)
74987497
}
74997498

75007499
if (key) {
7501-
ret = wc_AesXtsSetKey_NoInit(&ctx->cipher.xts, key,
7500+
ret = wc_AesXtsSetKeyNoInit(&ctx->cipher.xts, key,
75027501
(word32)ctx->keyLen,
75037502
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
75047503
if (ret != 0) {
@@ -7539,7 +7538,7 @@ void wolfSSL_EVP_init(void)
75397538
}
75407539

75417540
if (key) {
7542-
ret = wc_AesXtsSetKey_NoInit(&ctx->cipher.xts, key,
7541+
ret = wc_AesXtsSetKeyNoInit(&ctx->cipher.xts, key,
75437542
(word32)ctx->keyLen,
75447543
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
75457544
if (ret != 0) {

0 commit comments

Comments
 (0)