iPXE
hmac.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  *
23  * Alternatively, you may distribute this code in source or binary
24  * form, with or without modification, provided that the following
25  * conditions are met:
26  *
27  * 1. Redistributions of source code must retain the above copyright
28  * notice, this list of conditions and the above disclaimer.
29  *
30  * 2. Redistributions in binary form must reproduce the above
31  * copyright notice, this list of conditions and the above
32  * disclaimer in the documentation and/or other materials provided
33  * with the distribution.
34  */
35 
36 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 
38 /**
39  * @file
40  *
41  * Keyed-Hashing for Message Authentication
42  */
43 
44 #include <string.h>
45 #include <assert.h>
46 #include <ipxe/crypto.h>
47 #include <ipxe/hmac.h>
48 
49 /**
50  * Initialise HMAC
51  *
52  * @v digest Digest algorithm to use
53  * @v ctx HMAC context
54  * @v key Key
55  * @v key_len Length of key
56  */
57 void hmac_init ( struct digest_algorithm *digest, void *ctx, const void *key,
58  size_t key_len ) {
59  hmac_context_t ( digest ) *hctx = ctx;
60  unsigned int i;
61 
62  /* Construct input pad */
63  memset ( hctx->pad, 0, sizeof ( hctx->pad ) );
64  if ( key_len <= sizeof ( hctx->pad ) ) {
65  memcpy ( hctx->pad, key, key_len );
66  } else {
67  digest_init ( digest, hctx->ctx );
68  digest_update ( digest, hctx->ctx, key, key_len );
69  digest_final ( digest, hctx->ctx, hctx->pad );
70  }
71  for ( i = 0 ; i < sizeof ( hctx->pad ) ; i++ ) {
72  hctx->pad[i] ^= 0x36;
73  }
74 
75  /* Start inner hash */
76  digest_init ( digest, hctx->ctx );
77  digest_update ( digest, hctx->ctx, hctx->pad, sizeof ( hctx->pad ) );
78 }
79 
80 /**
81  * Finalise HMAC
82  *
83  * @v digest Digest algorithm to use
84  * @v ctx HMAC context
85  * @v hmac HMAC digest to fill in
86  */
87 void hmac_final ( struct digest_algorithm *digest, void *ctx, void *hmac ) {
88  hmac_context_t ( digest ) *hctx = ctx;
89  unsigned int i;
90 
91  /* Construct output pad from input pad */
92  for ( i = 0 ; i < sizeof ( hctx->pad ) ; i++ ) {
93  hctx->pad[i] ^= 0x6a;
94  }
95 
96  /* Finish inner hash */
97  digest_final ( digest, hctx->ctx, hmac );
98 
99  /* Perform outer hash */
100  digest_init ( digest, hctx->ctx );
101  digest_update ( digest, hctx->ctx, hctx->pad, sizeof ( hctx->pad ) );
102  digest_update ( digest, hctx->ctx, hmac, digest->digestsize );
103  digest_final ( digest, hctx->ctx, hmac );
104 
105  /* Erase output pad (from which the key may be derivable) */
106  memset ( hctx->pad, 0, sizeof ( hctx->pad ) );
107 }
void hmac_init(struct digest_algorithm *digest, void *ctx, const void *key, size_t key_len)
Initialise HMAC.
Definition: hmac.c:57
#define hmac_context_t(digest)
HMAC context type.
Definition: hmac.h:14
Cryptographic API.
static void const void size_t key_len
Definition: crypto.h:285
void * memcpy(void *dest, const void *src, size_t len) __nonnull
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Assertions.
Keyed-Hashing for Message Authentication.
static void struct digest_algorithm * digest
HMAC-MD5 digest.
Definition: crypto.h:308
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
size_t digestsize
Digest size.
Definition: crypto.h:25
A message digest algorithm.
Definition: crypto.h:17
void hmac_final(struct digest_algorithm *digest, void *ctx, void *hmac)
Finalise HMAC.
Definition: hmac.c:87
String functions.
union @382 key
Sense key.
Definition: crypto.h:284
void * memset(void *dest, int character, size_t len) __nonnull