blob: 9519af9f395cd664659c3ff8048ec7e28df10736 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* cmac.h
2 *
3 * Copyright (C) 2006-2021 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20 */
21
22
23#ifndef WOLF_CRYPT_CMAC_H
24#define WOLF_CRYPT_CMAC_H
25
26#include <wolfssl/wolfcrypt/types.h>
27#include <wolfssl/wolfcrypt/aes.h>
28
29#if !defined(NO_AES) && defined(WOLFSSL_CMAC)
30
31#if defined(HAVE_FIPS) && \
32 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
33 #include <wolfssl/wolfcrypt/fips.h>
34#endif /* HAVE_FIPS_VERSION >= 2 */
35
36#ifdef __cplusplus
37 extern "C" {
38#endif
39
40/* avoid redefinition of structs */
41#if !defined(HAVE_FIPS) || \
42 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
43
44#ifndef WC_CMAC_TYPE_DEFINED
45 typedef struct Cmac Cmac;
46 #define WC_CMAC_TYPE_DEFINED
47#endif
48struct Cmac {
49 Aes aes;
50 byte buffer[AES_BLOCK_SIZE]; /* partially stored block */
51 byte digest[AES_BLOCK_SIZE]; /* running digest */
52 byte k1[AES_BLOCK_SIZE];
53 byte k2[AES_BLOCK_SIZE];
54 word32 bufferSz;
55 word32 totalSz;
56#ifdef WOLF_CRYPTO_CB
57 int devId;
58 void* devCtx;
59 #ifdef WOLFSSL_QNX_CAAM
60 byte ctx[32]; /* hold state for save and return */
61 word32 blackKey;
62 word32 keylen;
63 byte initialized;
64 #endif
65#endif
66};
67
68
69
70typedef enum CmacType {
71 WC_CMAC_AES = 1
72} CmacType;
73
74#define WC_CMAC_TAG_MAX_SZ AES_BLOCK_SIZE
75#define WC_CMAC_TAG_MIN_SZ (AES_BLOCK_SIZE/4)
76
77#endif /* HAVE_FIPS */
78
79WOLFSSL_API
80int wc_InitCmac(Cmac* cmac,
81 const byte* key, word32 keySz,
82 int type, void* unused);
83
84WOLFSSL_API
85int wc_InitCmac_ex(Cmac* cmac,
86 const byte* key, word32 keySz,
87 int type, void* unused, void* heap, int devId);
88
89WOLFSSL_API
90int wc_CmacUpdate(Cmac* cmac,
91 const byte* in, word32 inSz);
92WOLFSSL_API
93int wc_CmacFinal(Cmac* cmac,
94 byte* out, word32* outSz);
95
96WOLFSSL_API
97int wc_AesCmacGenerate(byte* out, word32* outSz,
98 const byte* in, word32 inSz,
99 const byte* key, word32 keySz);
100
101WOLFSSL_API
102int wc_AesCmacVerify(const byte* check, word32 checkSz,
103 const byte* in, word32 inSz,
104 const byte* key, word32 keySz);
105
106#ifdef __cplusplus
107 } /* extern "C" */
108#endif
109
110
111#endif /* NO_AES && WOLFSSL_CMAC */
112#endif /* WOLF_CRYPT_CMAC_H */
113