1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 /**
5 @file ocb_encrypt.c
6 OCB implementation, encrypt data, by Tom St Denis
7 */
8 #include "tomcrypt_private.h"
9
10 #ifdef LTC_OCB_MODE
11
12 /**
13 Encrypt a block of data with OCB.
14 @param ocb The OCB state
15 @param pt The plaintext (length of the block size of the block cipher)
16 @param ct [out] The ciphertext (same size as the pt)
17 @return CRYPT_OK if successful
18 */
ocb_encrypt(ocb_state * ocb,const unsigned char * pt,unsigned char * ct)19 int ocb_encrypt(ocb_state *ocb, const unsigned char *pt, unsigned char *ct)
20 {
21 unsigned char Z[MAXBLOCKSIZE], tmp[MAXBLOCKSIZE];
22 int err, x;
23
24 LTC_ARGCHK(ocb != NULL);
25 LTC_ARGCHK(pt != NULL);
26 LTC_ARGCHK(ct != NULL);
27 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
28 return err;
29 }
30 if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
31 return CRYPT_INVALID_ARG;
32 }
33
34 /* compute checksum */
35 for (x = 0; x < ocb->block_len; x++) {
36 ocb->checksum[x] ^= pt[x];
37 }
38
39 /* Get Z[i] value */
40 ocb_shift_xor(ocb, Z);
41
42 /* xor pt in, encrypt, xor Z out */
43 for (x = 0; x < ocb->block_len; x++) {
44 tmp[x] = pt[x] ^ Z[x];
45 }
46 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, ct, &ocb->key)) != CRYPT_OK) {
47 return err;
48 }
49 for (x = 0; x < ocb->block_len; x++) {
50 ct[x] ^= Z[x];
51 }
52
53 #ifdef LTC_CLEAN_STACK
54 zeromem(Z, sizeof(Z));
55 zeromem(tmp, sizeof(tmp));
56 #endif
57 return CRYPT_OK;
58 }
59
60 #endif
61