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