]> pd.if.org Git - zpackage/blob - libtomcrypt/src/mac/hmac/hmac_memory.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / mac / hmac / hmac_memory.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file hmac_memory.c
13   HMAC support, process a block of memory, Tom St Denis/Dobes Vandermeer
14 */
15
16 #ifdef LTC_HMAC
17
18 /**
19    HMAC a block of memory to produce the authentication tag
20    @param hash      The index of the hash to use
21    @param key       The secret key
22    @param keylen    The length of the secret key (octets)
23    @param in        The data to HMAC
24    @param inlen     The length of the data to HMAC (octets)
25    @param out       [out] Destination of the authentication tag
26    @param outlen    [in/out] Max size and resulting size of authentication tag
27    @return CRYPT_OK if successful
28 */
29 int hmac_memory(int hash,
30                 const unsigned char *key,  unsigned long keylen,
31                 const unsigned char *in,   unsigned long inlen,
32                       unsigned char *out,  unsigned long *outlen)
33 {
34     hmac_state *hmac;
35     int         err;
36
37     LTC_ARGCHK(key    != NULL);
38     LTC_ARGCHK(in     != NULL);
39     LTC_ARGCHK(out    != NULL);
40     LTC_ARGCHK(outlen != NULL);
41
42     /* make sure hash descriptor is valid */
43     if ((err = hash_is_valid(hash)) != CRYPT_OK) {
44        return err;
45     }
46
47     /* is there a descriptor? */
48     if (hash_descriptor[hash].hmac_block != NULL) {
49         return hash_descriptor[hash].hmac_block(key, keylen, in, inlen, out, outlen);
50     }
51
52     /* nope, so call the hmac functions */
53     /* allocate ram for hmac state */
54     hmac = XMALLOC(sizeof(hmac_state));
55     if (hmac == NULL) {
56        return CRYPT_MEM;
57     }
58
59     if ((err = hmac_init(hmac, hash, key, keylen)) != CRYPT_OK) {
60        goto LBL_ERR;
61     }
62
63     if ((err = hmac_process(hmac, in, inlen)) != CRYPT_OK) {
64        goto LBL_ERR;
65     }
66
67     if ((err = hmac_done(hmac, out, outlen)) != CRYPT_OK) {
68        goto LBL_ERR;
69     }
70
71    err = CRYPT_OK;
72 LBL_ERR:
73 #ifdef LTC_CLEAN_STACK
74    zeromem(hmac, sizeof(hmac_state));
75 #endif
76
77    XFREE(hmac);
78    return err;
79 }
80
81 #endif
82
83
84 /* ref:         $Format:%D$ */
85 /* git commit:  $Format:%H$ */
86 /* commit time: $Format:%ai$ */