Skip to content

Commit 422a769

Browse files
Tomasz BursztykaAnas Nashif
authored andcommitted
samples/crypto: Check error code everytime and improve logging
If the encryption/decryption failed there is no need to procceed further. Change-Id: If450e40ed6fd601b698b74c56ae21fc7f903d087 Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent 5d8eadd commit 422a769

File tree

1 file changed

+28
-14
lines changed
  • samples/drivers/crypto/src

1 file changed

+28
-14
lines changed

samples/drivers/crypto/src/main.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,18 @@ void cbc_mode(void)
110110
encrpt.out_buf_max = sizeof(encrypted);
111111
encrpt.out_buf = encrypted;
112112

113-
cipher_cbc_op(&ini, &encrpt, iv);
113+
if (cipher_cbc_op(&ini, &encrpt, iv)) {
114+
SYS_LOG_ERR("CBC mode ENCRYPT - Failed");
115+
goto out;
116+
}
114117

115118
if (memcmp(encrpt.out_buf, ciphertext, sizeof(ciphertext))) {
116-
SYS_LOG_ERR("cbc mode ENCRYPT - Mismatch between expected and "
119+
SYS_LOG_ERR("CBC mode ENCRYPT - Mismatch between expected and "
117120
"returned cipher text");
118121
goto out;
119122
}
120123

121-
SYS_LOG_INF("cbc mode ENCRYPT - Match");
124+
SYS_LOG_INF("CBC mode ENCRYPT - Match");
122125
cipher_free_session(dev, &ini);
123126

124127
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
@@ -133,15 +136,18 @@ void cbc_mode(void)
133136
decrypt.out_buf_max = sizeof(decrypted);
134137

135138
/* TinyCrypt keeps IV at the start of encrypted buffer */
136-
cipher_cbc_op(&ini, &decrypt, encrypted);
139+
if (cipher_cbc_op(&ini, &decrypt, encrypted)) {
140+
SYS_LOG_ERR("CBC mode DECRYPT - Failed");
141+
goto out;
142+
}
137143

138144
if (memcmp(decrypt.out_buf, plaintext, sizeof(plaintext))) {
139-
SYS_LOG_ERR("cbc mode DECRYPT - Mismatch between plaintext and "
145+
SYS_LOG_ERR("CBC mode DECRYPT - Mismatch between plaintext and "
140146
"decrypted cipher text");
141147
goto out;
142148
}
143149

144-
SYS_LOG_INF("cbc mode DECRYPT - Match");
150+
SYS_LOG_INF("CBC mode DECRYPT - Match");
145151
out:
146152
cipher_free_session(dev, &ini);
147153
}
@@ -201,15 +207,18 @@ void ctr_mode(void)
201207
encrpt.out_buf_max = sizeof(encrypted);
202208
encrpt.out_buf = encrypted;
203209

204-
cipher_ctr_op(&ini, &encrpt, iv);
210+
if (cipher_ctr_op(&ini, &encrpt, iv)) {
211+
SYS_LOG_ERR("CTR mode ENCRYPT - Failed");
212+
goto out;
213+
}
205214

206215
if (memcmp(encrpt.out_buf, ctr_ciphertext, sizeof(ctr_ciphertext))) {
207-
SYS_LOG_ERR("ctr mode ENCRYPT - Mismatch between expected "
216+
SYS_LOG_ERR("CTR mode ENCRYPT - Mismatch between expected "
208217
"and returned cipher text");
209218
goto out;
210219
}
211220

212-
SYS_LOG_INF("ctr mode ENCRYPT - Match");
221+
SYS_LOG_INF("CTR mode ENCRYPT - Match");
213222
cipher_free_session(dev, &ini);
214223

215224
if (cipher_begin_session(dev, &ini, CRYPTO_CIPHER_ALGO_AES,
@@ -218,21 +227,23 @@ void ctr_mode(void)
218227
return;
219228
}
220229

221-
222230
decrypt.in_buf = encrypted;
223231
decrypt.in_len = sizeof(encrypted);
224232
decrypt.out_buf = decrypted;
225233
decrypt.out_buf_max = sizeof(decrypted);
226234

227-
cipher_ctr_op(&ini, &decrypt, iv);
235+
if (cipher_ctr_op(&ini, &decrypt, iv)) {
236+
SYS_LOG_ERR("CTR mode DECRYPT - Failed");
237+
goto out;
238+
}
228239

229240
if (memcmp(decrypt.out_buf, plaintext, sizeof(plaintext))) {
230-
SYS_LOG_ERR("ctr mode DECRYPT - Mismatch between plaintext "
241+
SYS_LOG_ERR("CTR mode DECRYPT - Mismatch between plaintext "
231242
"and decypted cipher text");
232243
goto out;
233244
}
234245

235-
SYS_LOG_INF("ctr mode DECRYPT - Match");
246+
SYS_LOG_INF("CTR mode DECRYPT - Match");
236247
out:
237248
cipher_free_session(dev, &ini);
238249
}
@@ -303,7 +314,10 @@ void ccm_mode(void)
303314
ccm_op.ad_len = sizeof(ccm_hdr);
304315
ccm_op.pkt = &encrpt;
305316

306-
cipher_ccm_op(&ini, &ccm_op, ccm_nonce);
317+
if (cipher_ccm_op(&ini, &ccm_op, ccm_nonce)) {
318+
SYS_LOG_ERR("CCM mode ENCRYPT - Failed");
319+
goto out;
320+
}
307321

308322
if (memcmp(encrpt.out_buf, ccm_expected, sizeof(ccm_expected))) {
309323
SYS_LOG_ERR("CCM mode ENCRYPT - Mismatch between expected "

0 commit comments

Comments
 (0)