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 #include "tomcrypt.h"
10
11 /**
12 @file pmac_done.c
13 PMAC implementation, terminate a session, by Tom St Denis
14 */
15
16 #ifdef LTC_PMAC
17
pmac_done(pmac_state * state,unsigned char * out,unsigned long * outlen)18 int pmac_done(pmac_state *state, unsigned char *out, unsigned long *outlen)
19 {
20 int err, x;
21
22 LTC_ARGCHK(state != NULL);
23 LTC_ARGCHK(out != NULL);
24 if ((err = cipher_is_valid(state->cipher_idx)) != CRYPT_OK) {
25 return err;
26 }
27
28 if ((state->buflen > (int)sizeof(state->block)) || (state->buflen < 0) ||
29 (state->block_len > (int)sizeof(state->block)) || (state->buflen > state->block_len)) {
30 return CRYPT_INVALID_ARG;
31 }
32
33
34 /* handle padding. If multiple xor in L/x */
35
36 if (state->buflen == state->block_len) {
37 /* xor Lr against the checksum */
38 for (x = 0; x < state->block_len; x++) {
39 state->checksum[x] ^= state->block[x] ^ state->Lr[x];
40 }
41 } else {
42 /* otherwise xor message bytes then the 0x80 byte */
43 for (x = 0; x < state->buflen; x++) {
44 state->checksum[x] ^= state->block[x];
45 }
46 state->checksum[x] ^= 0x80;
47 }
48
49 /* encrypt it */
50 if ((err = cipher_descriptor[state->cipher_idx].ecb_encrypt(state->checksum, state->checksum, &state->key)) != CRYPT_OK) {
51 return err;
52 }
53 cipher_descriptor[state->cipher_idx].done(&state->key);
54
55 /* store it */
56 for (x = 0; x < state->block_len && x < (int)*outlen; x++) {
57 out[x] = state->checksum[x];
58 }
59 *outlen = x;
60
61 #ifdef LTC_CLEAN_STACK
62 zeromem(state, sizeof(*state));
63 #endif
64 return CRYPT_OK;
65 }
66
67 #endif
68
69
70 /* ref: HEAD -> master, tag: v1.18.2 */
71 /* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
72 /* commit time: 2018-07-01 22:49:01 +0200 */
73