1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 *
4 * LibTomCrypt is a library that provides various cryptographic
5 * algorithms in a highly modular and flexible manner.
6 *
7 * The library is free for all purposes without any express
8 * guarantee it works.
9 */
10
11 /**
12 @file ocb_decrypt.c
13 OCB implementation, decrypt data, by Tom St Denis
14 */
15 #include "tomcrypt_private.h"
16
17 #ifdef LTC_OCB_MODE
18
19 /**
20 Decrypt a block with OCB.
21 @param ocb The OCB state
22 @param ct The ciphertext (length of the block size of the block cipher)
23 @param pt [out] The plaintext (length of ct)
24 @return CRYPT_OK if successful
25 */
ocb_decrypt(ocb_state * ocb,const unsigned char * ct,unsigned char * pt)26 int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
27 {
28 unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE];
29 int err, x;
30
31 LTC_ARGCHK(ocb != NULL);
32 LTC_ARGCHK(pt != NULL);
33 LTC_ARGCHK(ct != NULL);
34
35 /* check if valid cipher */
36 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
37 return err;
38 }
39 LTC_ARGCHK(cipher_descriptor[ocb->cipher]->ecb_decrypt != NULL);
40
41 /* check length */
42 if (ocb->block_len != cipher_descriptor[ocb->cipher]->block_length) {
43 return CRYPT_INVALID_ARG;
44 }
45
46 /* Get Z[i] value */
47 ocb_shift_xor(ocb, Z);
48
49 /* xor ct in, encrypt, xor Z out */
50 for (x = 0; x < ocb->block_len; x++) {
51 tmp[x] = ct[x] ^ Z[x];
52 }
53 if ((err = cipher_descriptor[ocb->cipher]->ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) {
54 return err;
55 }
56 for (x = 0; x < ocb->block_len; x++) {
57 pt[x] ^= Z[x];
58 }
59
60 /* compute checksum */
61 for (x = 0; x < ocb->block_len; x++) {
62 ocb->checksum[x] ^= pt[x];
63 }
64
65
66 #ifdef LTC_CLEAN_STACK
67 zeromem(Z, sizeof(Z));
68 zeromem(tmp, sizeof(tmp));
69 #endif
70 return CRYPT_OK;
71 }
72
73 #endif
74
75
76 /* ref: $Format:%D$ */
77 /* git commit: $Format:%H$ */
78 /* commit time: $Format:%ai$ */
79