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 FILE_SECBOOT ( PERMITTED );
38 
39 /**
40  * @file
41  *
42  * Keyed-Hashing for Message Authentication
43  */
44 
45 #include <string.h>
46 #include <assert.h>
47 #include <ipxe/crypto.h>
48 #include <ipxe/hmac.h>
49 
50 /**
51  * Initialise HMAC
52  *
53  * @v digest Digest algorithm to use
54  * @v ctx HMAC context
55  * @v key Key
56  * @v key_len Length of key
57  */
58 void hmac_init ( struct digest_algorithm *digest, void *ctx, const void *key,
59  size_t key_len ) {
60  hmac_context_t ( digest ) *hctx = ctx;
61  unsigned int i;
62 
63  /* Construct input pad */
64  memset ( hctx->pad, 0, sizeof ( hctx->pad ) );
65  if ( key_len <= sizeof ( hctx->pad ) ) {
66  memcpy ( hctx->pad, key, key_len );
67  } else {
68  digest_init ( digest, hctx->ctx );
69  digest_update ( digest, hctx->ctx, key, key_len );
70  digest_final ( digest, hctx->ctx, hctx->pad );
71  }
72  for ( i = 0 ; i < sizeof ( hctx->pad ) ; i++ ) {
73  hctx->pad[i] ^= 0x36;
74  }
75 
76  /* Start inner hash */
77  digest_init ( digest, hctx->ctx );
78  digest_update ( digest, hctx->ctx, hctx->pad, sizeof ( hctx->pad ) );
79 }
80 
81 /**
82  * Finalise HMAC
83  *
84  * @v digest Digest algorithm to use
85  * @v ctx HMAC context
86  * @v hmac HMAC digest to fill in
87  */
88 void hmac_final ( struct digest_algorithm *digest, void *ctx, void *hmac ) {
89  hmac_context_t ( digest ) *hctx = ctx;
90  unsigned int i;
91 
92  /* Construct output pad from input pad */
93  for ( i = 0 ; i < sizeof ( hctx->pad ) ; i++ ) {
94  hctx->pad[i] ^= 0x6a;
95  }
96 
97  /* Finish inner hash */
98  digest_final ( digest, hctx->ctx, hmac );
99 
100  /* Perform outer hash */
101  digest_init ( digest, hctx->ctx );
102  digest_update ( digest, hctx->ctx, hctx->pad, sizeof ( hctx->pad ) );
103  digest_update ( digest, hctx->ctx, hmac, digest->digestsize );
104  digest_final ( digest, hctx->ctx, hmac );
105 
106  /* Erase output pad (from which the key may be derivable) */
107  memset ( hctx->pad, 0, sizeof ( hctx->pad ) );
108 }
void hmac_init(struct digest_algorithm *digest, void *ctx, const void *key, size_t key_len)
Initialise HMAC.
Definition: hmac.c:58
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:224
#define hmac_context_t(digest)
HMAC context type.
Definition: hmac.h:15
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:230
Cryptographic API.
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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 digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:219
FILE_SECBOOT(PERMITTED)
size_t digestsize
Digest size.
Definition: crypto.h:27
A message digest algorithm.
Definition: crypto.h:19
void hmac_final(struct digest_algorithm *digest, void *ctx, void *hmac)
Finalise HMAC.
Definition: hmac.c:88
String functions.
union @391 key
Sense key.
Definition: scsi.h:18
void * memset(void *dest, int character, size_t len) __nonnull