gnunet-android

GNUnet for Android
Log | Files | Refs | README

hmac.h (6898B)


      1 /* hmac.h
      2  *
      3  * Copyright (C) 2006-2025 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 3 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     \file wolfssl/wolfcrypt/hmac.h
     24 */
     25 
     26 #ifndef WOLF_CRYPT_HMAC_H
     27 #define WOLF_CRYPT_HMAC_H
     28 
     29 #include <wolfssl/wolfcrypt/hash.h>
     30 
     31 #ifndef NO_HMAC
     32 
     33 #if FIPS_VERSION3_GE(2,0,0)
     34     #include <wolfssl/wolfcrypt/fips.h>
     35 #endif
     36 
     37 #ifdef __cplusplus
     38     extern "C" {
     39 #endif
     40 
     41 #if FIPS_VERSION3_GE(6,0,0)
     42     extern const unsigned int wolfCrypt_FIPS_hmac_ro_sanity[2];
     43     WOLFSSL_LOCAL int wolfCrypt_FIPS_HMAC_sanity(void);
     44 #endif
     45 
     46 #if FIPS_VERSION3_GE(6,0,0)
     47     #define FIPS_ALLOW_SHORT 1
     48 #endif
     49 
     50 /* avoid redefinition of structs */
     51 #if !defined(HAVE_FIPS) || FIPS_VERSION3_GE(2,0,0)
     52 
     53 #ifdef WOLFSSL_ASYNC_CRYPT
     54     #include <wolfssl/wolfcrypt/async.h>
     55 #endif
     56 
     57 #if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_HMAC)
     58     #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
     59 #endif
     60 
     61 #ifndef NO_OLD_WC_NAMES
     62     #define HMAC_BLOCK_SIZE WC_HMAC_BLOCK_SIZE
     63 #endif
     64 
     65 #define WC_HMAC_INNER_HASH_KEYED_SW     1
     66 #define WC_HMAC_INNER_HASH_KEYED_DEV    2
     67 
     68 enum {
     69     HMAC_FIPS_MIN_KEY = 14,   /* 112 bit key length minimum */
     70 
     71     IPAD    = 0x36,
     72     OPAD    = 0x5C,
     73 
     74 /* If any hash is not enabled, add the ID here. */
     75 #ifdef NO_MD5
     76     WC_MD5     = WC_HASH_TYPE_MD5,
     77 #endif
     78 #ifdef NO_SHA
     79     WC_SHA     = WC_HASH_TYPE_SHA,
     80 #endif
     81 #ifdef NO_SHA256
     82     WC_SHA256  = WC_HASH_TYPE_SHA256,
     83 #endif
     84 #ifndef WOLFSSL_SHA512
     85     WC_SHA512  = WC_HASH_TYPE_SHA512,
     86     #ifndef WOLFSSL_NOSHA512_224
     87     WC_SHA512_224  = WC_HASH_TYPE_SHA512_224,
     88     #endif
     89     #ifndef WOLFSSL_NOSHA512_256
     90     WC_SHA512_256  = WC_HASH_TYPE_SHA512_256,
     91     #endif
     92 #endif
     93 #ifndef WOLFSSL_SHA384
     94     WC_SHA384  = WC_HASH_TYPE_SHA384,
     95 #endif
     96 #ifndef WOLFSSL_SHA224
     97     WC_SHA224  = WC_HASH_TYPE_SHA224,
     98 #endif
     99 #ifndef WOLFSSL_SHA3
    100     WC_SHA3_224 = WC_HASH_TYPE_SHA3_224,
    101     WC_SHA3_256 = WC_HASH_TYPE_SHA3_256,
    102     WC_SHA3_384 = WC_HASH_TYPE_SHA3_384,
    103     WC_SHA3_512 = WC_HASH_TYPE_SHA3_512,
    104 #endif
    105 #ifdef WOLF_PRIVATE_KEY_ID
    106     HMAC_MAX_ID_LEN    = 32,
    107     HMAC_MAX_LABEL_LEN = 32,
    108 #endif
    109 
    110     WOLF_ENUM_DUMMY_LAST_ELEMENT(HMAC)
    111 };
    112 
    113 /* Select the largest available hash for the buffer size. */
    114 #define WC_HMAC_BLOCK_SIZE WC_MAX_BLOCK_SIZE
    115 
    116 #if !defined(WOLFSSL_SHA3) && !defined(WOLFSSL_SHA512) && \
    117     !defined(WOLFSSL_SHA384) && defined(NO_SHA256) && \
    118     defined(WOLFSSL_SHA224) && defined(NO_SHA) && defined(NO_MD5)
    119     #error "You have to have some kind of hash if you want to use HMAC."
    120 #endif
    121 
    122 typedef wc_Hashes wc_HmacHash;
    123 
    124 /* Hmac digest */
    125 struct Hmac {
    126     wc_HmacHash hash;
    127 #ifdef WOLFSSL_HMAC_COPY_HASH
    128     wc_HmacHash i_hash;
    129     wc_HmacHash o_hash;
    130 #endif
    131     word32  ipad[WC_HMAC_BLOCK_SIZE  / sizeof(word32)];  /* same block size all*/
    132     word32  opad[WC_HMAC_BLOCK_SIZE  / sizeof(word32)];
    133     word32  innerHash[WC_MAX_DIGEST_SIZE / sizeof(word32)];
    134     void*   heap;                 /* heap hint */
    135     byte    macType;              /* md5 sha or sha256 */
    136     byte    innerHashKeyed;       /* keyed flag */
    137 #ifdef WOLFSSL_KCAPI_HMAC
    138     struct kcapi_handle* handle;
    139 #endif
    140 #ifdef WOLFSSL_ASYNC_CRYPT
    141     WC_ASYNC_DEV asyncDev;
    142 #endif /* WOLFSSL_ASYNC_CRYPT */
    143 #if defined(WOLFSSL_DEVCRYPTO) && defined(WOLFSSL_DEVCRYPTO_HMAC)
    144     WC_CRYPTODEV ctx;
    145 #endif
    146 #ifdef WOLF_CRYPTO_CB
    147     int     devId;
    148     void*   devCtx;
    149     const byte* keyRaw;
    150 #endif
    151 #ifdef WOLF_PRIVATE_KEY_ID
    152     byte    id[HMAC_MAX_ID_LEN];
    153     int     idLen;
    154     char    label[HMAC_MAX_LABEL_LEN];
    155     int     labelLen;
    156 #endif
    157 #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLF_CRYPTO_CB)
    158     word16  keyLen;          /* hmac key length (key in ipad) */
    159 #endif
    160 };
    161 
    162 #ifndef WC_HMAC_TYPE_DEFINED
    163     typedef struct Hmac Hmac;
    164     #define WC_HMAC_TYPE_DEFINED
    165 #endif
    166 
    167 
    168 #endif /* HAVE_FIPS */
    169 
    170 /* does init */
    171 WOLFSSL_API int wc_HmacSetKey(Hmac* hmac, int type, const byte* key,
    172                               word32 keySz);
    173 WOLFSSL_API int wc_HmacSetKey_ex(Hmac* hmac, int type, const byte* key,
    174                                  word32 length, int allowFlag);
    175 WOLFSSL_API int wc_HmacUpdate(Hmac* hmac, const byte* in, word32 sz);
    176 WOLFSSL_API int wc_HmacFinal(Hmac* hmac, byte* out);
    177 #ifdef WOLFSSL_KCAPI_HMAC
    178 WOLFSSL_API int wc_HmacSetKey_Software(Hmac* hmac, int type, const byte* key,
    179                                        word32 keySz);
    180 WOLFSSL_API int wc_HmacUpdate_Software(Hmac* hmac, const byte* in, word32 sz);
    181 WOLFSSL_API int wc_HmacFinal_Software(Hmac* hmac, byte* out);
    182 #endif
    183 WOLFSSL_API int wc_HmacSizeByType(int type);
    184 
    185 WOLFSSL_API int wc_HmacInit(Hmac* hmac, void* heap, int devId);
    186 #ifdef WOLF_PRIVATE_KEY_ID
    187 WOLFSSL_API int wc_HmacInit_Id(Hmac* hmac, byte* id, int len, void* heap,
    188                                int devId);
    189 WOLFSSL_API int wc_HmacInit_Label(Hmac* hmac, const char* label, void* heap,
    190                                   int devId);
    191 #endif
    192 WOLFSSL_API void wc_HmacFree(Hmac* hmac);
    193 
    194 WOLFSSL_API int wolfSSL_GetHmacMaxSize(void);
    195 
    196 WOLFSSL_LOCAL int _InitHmac(Hmac* hmac, int type, void* heap);
    197 WOLFSSL_LOCAL int _HmacInitIOHashes(Hmac* hmac);
    198 
    199 #ifdef HAVE_HKDF
    200 
    201 WOLFSSL_API int wc_HKDF_Extract_ex(int type, const byte* salt, word32 saltSz,
    202                                 const byte* inKey, word32 inKeySz, byte* out,
    203                                 void* heap, int devId);
    204 
    205 WOLFSSL_API int wc_HKDF_Extract(int type, const byte* salt, word32 saltSz,
    206                                 const byte* inKey, word32 inKeySz, byte* out);
    207 
    208 WOLFSSL_API int wc_HKDF_Expand_ex(int type, const byte* inKey, word32 inKeySz,
    209                                const byte* info, word32 infoSz,
    210                                byte* out, word32 outSz, void* heap, int devId);
    211 WOLFSSL_API int wc_HKDF_Expand(int type, const byte* inKey, word32 inKeySz,
    212                                const byte* info, word32 infoSz,
    213                                byte* out, word32 outSz);
    214 
    215 WOLFSSL_API int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
    216                     const byte* salt, word32 saltSz,
    217                     const byte* info, word32 infoSz,
    218                     byte* out, word32 outSz);
    219 
    220 #endif /* HAVE_HKDF */
    221 
    222 #ifdef __cplusplus
    223     } /* extern "C" */
    224 #endif
    225 
    226 #endif /* NO_HMAC */
    227 #endif /* WOLF_CRYPT_HMAC_H */