hmac.h (5472B)
1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OPENSSL_HEADER_HMAC_H 16 #define OPENSSL_HEADER_HMAC_H 17 18 #include <openssl/base.h> // IWYU pragma: export 19 20 #include <openssl/digest.h> 21 22 #if defined(__cplusplus) 23 extern "C" { 24 #endif 25 26 27 // HMAC contains functions for constructing PRFs from Merkle–Damgård hash 28 // functions using HMAC. 29 30 31 // One-shot operation. 32 33 // HMAC calculates the HMAC of |data_len| bytes of |data|, using the given key 34 // and hash function, and writes the result to |out|. On entry, |out| must 35 // contain at least |EVP_MD_size| bytes of space. The actual length of the 36 // result is written to |*out_len|. An output size of |EVP_MAX_MD_SIZE| will 37 // always be large enough. It returns |out| or NULL on error. 38 OPENSSL_EXPORT uint8_t *HMAC(const EVP_MD *evp_md, const void *key, 39 size_t key_len, const uint8_t *data, 40 size_t data_len, uint8_t *out, 41 unsigned int *out_len); 42 43 44 // Incremental operation. 45 46 // HMAC_CTX_init initialises |ctx| for use in an HMAC operation. It's assumed 47 // that HMAC_CTX objects will be allocated on the stack thus no allocation 48 // function is provided. 49 OPENSSL_EXPORT void HMAC_CTX_init(HMAC_CTX *ctx); 50 51 // HMAC_CTX_new allocates and initialises a new |HMAC_CTX| and returns it, or 52 // NULL on allocation failure. The caller must use |HMAC_CTX_free| to release 53 // the resulting object. 54 OPENSSL_EXPORT HMAC_CTX *HMAC_CTX_new(void); 55 56 // HMAC_CTX_cleanup frees data owned by |ctx|. It does not free |ctx| itself. 57 OPENSSL_EXPORT void HMAC_CTX_cleanup(HMAC_CTX *ctx); 58 59 // HMAC_CTX_cleanse zeros the digest state from |ctx| and then performs the 60 // actions of |HMAC_CTX_cleanup|. 61 OPENSSL_EXPORT void HMAC_CTX_cleanse(HMAC_CTX *ctx); 62 63 // HMAC_CTX_free calls |HMAC_CTX_cleanup| and then frees |ctx| itself. 64 OPENSSL_EXPORT void HMAC_CTX_free(HMAC_CTX *ctx); 65 66 // HMAC_Init_ex sets up an initialised |HMAC_CTX| to use |md| as the hash 67 // function and |key| as the key. For a non-initial call, |md| may be NULL, in 68 // which case the previous hash function will be used. If the hash function has 69 // not changed and |key| is NULL, |ctx| reuses the previous key. It returns one 70 // on success or zero on allocation failure. 71 // 72 // WARNING: NULL and empty keys are ambiguous on non-initial calls. Passing NULL 73 // |key| but repeating the previous |md| reuses the previous key rather than the 74 // empty key. 75 OPENSSL_EXPORT int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len, 76 const EVP_MD *md, ENGINE *impl); 77 78 // HMAC_Update hashes |data_len| bytes from |data| into the current HMAC 79 // operation in |ctx|. It returns one. 80 OPENSSL_EXPORT int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data, 81 size_t data_len); 82 83 // HMAC_Final completes the HMAC operation in |ctx| and writes the result to 84 // |out| and the sets |*out_len| to the length of the result. On entry, |out| 85 // must contain at least |HMAC_size| bytes of space. An output size of 86 // |EVP_MAX_MD_SIZE| will always be large enough. It returns one on success or 87 // zero on allocation failure. 88 OPENSSL_EXPORT int HMAC_Final(HMAC_CTX *ctx, uint8_t *out, 89 unsigned int *out_len); 90 91 92 // Utility functions. 93 94 // HMAC_size returns the size, in bytes, of the HMAC that will be produced by 95 // |ctx|. On entry, |ctx| must have been setup with |HMAC_Init_ex|. 96 OPENSSL_EXPORT size_t HMAC_size(const HMAC_CTX *ctx); 97 98 // HMAC_CTX_get_md returns |ctx|'s hash function. 99 OPENSSL_EXPORT const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx); 100 101 // HMAC_CTX_copy_ex sets |dest| equal to |src|. On entry, |dest| must have been 102 // initialised by calling |HMAC_CTX_init|. It returns one on success and zero 103 // on error. 104 OPENSSL_EXPORT int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src); 105 106 // HMAC_CTX_reset calls |HMAC_CTX_cleanup| followed by |HMAC_CTX_init|. 107 OPENSSL_EXPORT void HMAC_CTX_reset(HMAC_CTX *ctx); 108 109 110 // Deprecated functions. 111 112 OPENSSL_EXPORT int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len, 113 const EVP_MD *md); 114 115 // HMAC_CTX_copy calls |HMAC_CTX_init| on |dest| and then sets it equal to 116 // |src|. On entry, |dest| must /not/ be initialised for an operation with 117 // |HMAC_Init_ex|. It returns one on success and zero on error. 118 OPENSSL_EXPORT int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src); 119 120 121 // Private functions 122 123 struct hmac_ctx_st { 124 const EVP_MD *md; 125 EVP_MD_CTX md_ctx; 126 EVP_MD_CTX i_ctx; 127 EVP_MD_CTX o_ctx; 128 } /* HMAC_CTX */; 129 130 131 #if defined(__cplusplus) 132 } // extern C 133 134 #if !defined(BORINGSSL_NO_CXX) 135 extern "C++" { 136 137 BSSL_NAMESPACE_BEGIN 138 139 BORINGSSL_MAKE_DELETER(HMAC_CTX, HMAC_CTX_free) 140 141 using ScopedHMAC_CTX = 142 internal::StackAllocated<HMAC_CTX, void, HMAC_CTX_init, HMAC_CTX_cleanup>; 143 144 BSSL_NAMESPACE_END 145 146 } // extern C++ 147 #endif 148 149 #endif 150 151 #endif // OPENSSL_HEADER_HMAC_H