1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 /**
5 @file ocb3_encrypt_last.c
6 OCB implementation, internal helper, by Karel Miko
7 */
8 #include "tomcrypt_private.h"
9
10 #ifdef LTC_OCB3_MODE
11
12 /**
13 Finish an OCB (encryption) stream
14 @param ocb The OCB state
15 @param pt The remaining plaintext
16 @param ptlen The length of the plaintext (octets)
17 @param ct [out] The output buffer
18 @return CRYPT_OK if successful
19 */
ocb3_encrypt_last(ocb3_state * ocb,const unsigned char * pt,unsigned long ptlen,unsigned char * ct)20 int ocb3_encrypt_last(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct)
21 {
22 unsigned char iOffset_star[MAXBLOCKSIZE];
23 unsigned char iPad[MAXBLOCKSIZE];
24 int err, x, full_blocks, full_blocks_len, last_block_len;
25
26 LTC_ARGCHK(ocb != NULL);
27 if (pt == NULL) LTC_ARGCHK(ptlen == 0);
28 if (ptlen != 0) {
29 LTC_ARGCHK(pt != NULL);
30 LTC_ARGCHK(ct != NULL);
31 }
32
33 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
34 goto LBL_ERR;
35 }
36
37 full_blocks = ptlen/ocb->block_len;
38 full_blocks_len = full_blocks * ocb->block_len;
39 last_block_len = ptlen - full_blocks_len;
40
41 /* process full blocks first */
42 if (full_blocks>0) {
43 if ((err = ocb3_encrypt(ocb, pt, full_blocks_len, ct)) != CRYPT_OK) {
44 goto LBL_ERR;
45 }
46 }
47
48 /* at this point: m = ocb->block_index (last block index), Offset_m = ocb->Offset_current */
49
50 if (last_block_len>0) {
51 /* Offset_* = Offset_m xor L_* */
52 ocb3_int_xor_blocks(iOffset_star, ocb->Offset_current, ocb->L_star, ocb->block_len);
53
54 /* Pad = ENCIPHER(K, Offset_*) */
55 if ((err = cipher_descriptor[ocb->cipher]->ecb_encrypt(iOffset_star, iPad, &ocb->key)) != CRYPT_OK) {
56 goto LBL_ERR;
57 }
58
59 /* C_* = P_* xor Pad[1..bitlen(P_*)] */
60 ocb3_int_xor_blocks(ct+full_blocks_len, pt+full_blocks_len, iPad, last_block_len);
61
62 /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
63 ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt+full_blocks_len, last_block_len);
64 for(x=last_block_len; x<ocb->block_len; x++) {
65 if (x == last_block_len) {
66 ocb->checksum[x] ^= 0x80;
67 } else {
68 ocb->checksum[x] ^= 0x00;
69 }
70 }
71
72 /* Tag = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) xor HASH(K,A) */
73 /* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_* xor Offset_* xor L_$) */
74 for(x=0; x<ocb->block_len; x++) {
75 ocb->tag_part[x] = (ocb->checksum[x] ^ iOffset_star[x]) ^ ocb->L_dollar[x];
76 }
77 if ((err = cipher_descriptor[ocb->cipher]->ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
78 goto LBL_ERR;
79 }
80 } else {
81 /* Tag = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) xor HASH(K,A) */
82 /* at this point we calculate only: Tag_part = ENCIPHER(K, Checksum_m xor Offset_m xor L_$) */
83 for(x=0; x<ocb->block_len; x++) {
84 ocb->tag_part[x] = (ocb->checksum[x] ^ ocb->Offset_current[x]) ^ ocb->L_dollar[x];
85 }
86 if ((err = cipher_descriptor[ocb->cipher]->ecb_encrypt(ocb->tag_part, ocb->tag_part, &ocb->key)) != CRYPT_OK) {
87 goto LBL_ERR;
88 }
89 }
90
91 err = CRYPT_OK;
92
93 LBL_ERR:
94 #ifdef LTC_CLEAN_STACK
95 zeromem(iOffset_star, MAXBLOCKSIZE);
96 zeromem(iPad, MAXBLOCKSIZE);
97 #endif
98
99 return err;
100 }
101
102 #endif
103