comparison libtomcrypt/src/encauth/ocb3/ocb3_encrypt.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9
10 /**
11 @file ocb3_encrypt.c
12 OCB implementation, encrypt data, by Tom St Denis
13 */
14 #include "tomcrypt.h"
15
16 #ifdef LTC_OCB3_MODE
17
18 /**
19 Encrypt blocks of data with OCB
20 @param ocb The OCB state
21 @param pt The plaintext (length multiple of the block size of the block cipher)
22 @param ptlen The length of the input (octets)
23 @param ct [out] The ciphertext (same size as the pt)
24 @return CRYPT_OK if successful
25 */
26 int ocb3_encrypt(ocb3_state *ocb, const unsigned char *pt, unsigned long ptlen, unsigned char *ct)
27 {
28 unsigned char tmp[MAXBLOCKSIZE];
29 int err, i, full_blocks;
30 unsigned char *pt_b, *ct_b;
31
32 LTC_ARGCHK(ocb != NULL);
33 if (ptlen == 0) return CRYPT_OK; /* no data, nothing to do */
34 LTC_ARGCHK(pt != NULL);
35 LTC_ARGCHK(ct != NULL);
36
37 if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
38 return err;
39 }
40 if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
41 return CRYPT_INVALID_ARG;
42 }
43
44 if (ptlen % ocb->block_len) { /* ptlen has to bu multiple of block_len */
45 return CRYPT_INVALID_ARG;
46 }
47
48 full_blocks = ptlen/ocb->block_len;
49 for(i=0; i<full_blocks; i++) {
50 pt_b = (unsigned char *)pt+i*ocb->block_len;
51 ct_b = (unsigned char *)ct+i*ocb->block_len;
52
53 /* ocb->Offset_current[] = ocb->Offset_current[] ^ Offset_{ntz(block_index)} */
54 ocb3_int_xor_blocks(ocb->Offset_current, ocb->Offset_current, ocb->L_[ocb3_int_ntz(ocb->block_index)], ocb->block_len);
55
56 /* tmp[] = pt[] XOR ocb->Offset_current[] */
57 ocb3_int_xor_blocks(tmp, pt_b, ocb->Offset_current, ocb->block_len);
58
59 /* encrypt */
60 if ((err = cipher_descriptor[ocb->cipher].ecb_encrypt(tmp, tmp, &ocb->key)) != CRYPT_OK) {
61 goto LBL_ERR;
62 }
63
64 /* ct[] = tmp[] XOR ocb->Offset_current[] */
65 ocb3_int_xor_blocks(ct_b, tmp, ocb->Offset_current, ocb->block_len);
66
67 /* ocb->checksum[] = ocb->checksum[] XOR pt[] */
68 ocb3_int_xor_blocks(ocb->checksum, ocb->checksum, pt_b, ocb->block_len);
69
70 ocb->block_index++;
71 }
72
73 err = CRYPT_OK;
74
75 LBL_ERR:
76 #ifdef LTC_CLEAN_STACK
77 zeromem(tmp, sizeof(tmp));
78 #endif
79 return err;
80 }
81
82 #endif
83
84 /* ref: $Format:%D$ */
85 /* git commit: $Format:%H$ */
86 /* commit time: $Format:%ai$ */