1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file omac_memory.c
7   OMAC1 support, process a block of memory, Tom St Denis
8 */
9 
10 #ifdef LTC_OMAC
11 
12 /**
13    OMAC a block of memory
14    @param cipher    The index of the desired cipher
15    @param key       The secret key
16    @param keylen    The length of the secret key (octets)
17    @param in        The data to send through OMAC
18    @param inlen     The length of the data to send through OMAC (octets)
19    @param out       [out] The destination of the authentication tag
20    @param outlen    [in/out]  The max size and resulting size of the authentication tag (octets)
21    @return CRYPT_OK if successful
22 */
omac_memory(int cipher,const unsigned char * key,unsigned long keylen,const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen)23 int omac_memory(int cipher,
24                 const unsigned char *key, unsigned long keylen,
25                 const unsigned char *in,  unsigned long inlen,
26                       unsigned char *out, unsigned long *outlen)
27 {
28    int err;
29    omac_state *omac;
30 
31    LTC_ARGCHK(key    != NULL);
32    LTC_ARGCHK(in     != NULL);
33    LTC_ARGCHK(out    != NULL);
34    LTC_ARGCHK(outlen != NULL);
35 
36    /* is the cipher valid? */
37    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
38       return err;
39    }
40 
41    /* Use accelerator if found */
42    if (cipher_descriptor[cipher]->omac_memory != NULL) {
43       return cipher_descriptor[cipher]->omac_memory(key, keylen, in, inlen, out, outlen);
44    }
45 
46    /* allocate ram for omac state */
47    omac = XMALLOC(sizeof(omac_state));
48    if (omac == NULL) {
49       return CRYPT_MEM;
50    }
51 
52    /* omac process the message */
53    if ((err = omac_init(omac, cipher, key, keylen)) != CRYPT_OK) {
54       goto LBL_ERR;
55    }
56    if ((err = omac_process(omac, in, inlen)) != CRYPT_OK) {
57       goto LBL_ERR;
58    }
59    if ((err = omac_done(omac, out, outlen)) != CRYPT_OK) {
60       goto LBL_ERR;
61    }
62 
63    err = CRYPT_OK;
64 LBL_ERR:
65 #ifdef LTC_CLEAN_STACK
66    zeromem(omac, sizeof(omac_state));
67 #endif
68 
69    XFREE(omac);
70    return err;
71 }
72 
73 #endif
74