The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
hmac_sha1.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** SHA1 HMAC not dependent on OpenSSL
18 *
19 * @note New code that needs fast or incremental HMACs should use the OpenSSL EVP_* HMAC
20 * interface instead, as that can take advantage of acceleration instructions provided
21 * by various CPUs (and provides an incremental hashing interface).
22 *
23 * Adapted from hmacmd5.c (HMAC-MD5). Test cases from RFC2202.
24 *
25 * @file src/lib/util/hmac_sha1.c
26 *
27 * @author Michael Richardson (mcr@sandelman.ottawa.on.ca)
28 *
29 * @copyright 2003 Michael Richardson (mcr@sandelman.ottawa.on.ca)
30 * @copyright 2000,2003,2006 The FreeRADIUS server project
31 */
32RCSID("$Id: 133cdd0973f7390ac021e3c1a91639c7dc150b42 $")
33
34#include <freeradius-devel/util/sha1.h>
35#include <freeradius-devel/util/atexit.h>
36#include <freeradius-devel/util/strerror.h>
37
38#ifdef HMAC_SHA1_DATA_PROBLEMS
39unsigned int sha1_data_problems = 0;
40#endif
41
42#ifdef HAVE_OPENSSL_EVP_H
43# include <openssl/hmac.h>
44
45static _Thread_local EVP_MD_CTX *sha1_hmac_ctx;
46
47static int _hmac_sha1_ctx_free_on_exit(void *arg)
48{
49 EVP_MD_CTX_free(arg);
50 return 0;
51}
52
53/** Calculate HMAC using OpenSSL's SHA1 implementation
54 *
55 * @param digest Caller digest to be filled in.
56 * @param in Pointer to data stream.
57 * @param inlen length of data stream.
58 * @param key Pointer to authentication key.
59 * @param key_len Length of authentication key.
60 * @return
61 * - 0 on success.
62 * - -1 on error.
63 */
64int fr_hmac_sha1(uint8_t digest[SHA1_DIGEST_LENGTH], uint8_t const *in, size_t inlen,
65 uint8_t const *key, size_t key_len)
66{
67 EVP_MD_CTX *ctx;
68 EVP_PKEY *pkey;
69
70 if (unlikely(!sha1_hmac_ctx)) {
71 ctx = EVP_MD_CTX_new();
72 if (unlikely(!ctx)) {
73 fr_strerror_const("Failed allocating EVP_MD_CTX for HMAC-SHA1");
74 return -1;
75 }
76 EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
77 fr_atexit_thread_local(sha1_hmac_ctx, _hmac_sha1_ctx_free_on_exit, ctx);
78 } else {
79 ctx = sha1_hmac_ctx;
80 }
81
82 pkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, key, key_len);
83 if (unlikely(pkey == NULL)) {
84 fr_strerror_const("Failed allocating pkey for HMAC-SHA1");
85 return -1;
86 }
87
88 if (unlikely(EVP_DigestSignInit(ctx, NULL, EVP_sha1(), NULL, pkey) != 1)) {
89 fr_strerror_const("Failed initialising EVP_MD_CTX for HMAC-SHA1");
90 error:
91 EVP_PKEY_free(pkey);
92 return -1;
93 }
94 if (unlikely(EVP_DigestSignUpdate(ctx, in, inlen) != 1)) {
95 fr_strerror_const("Failed ingesting data for HMAC-SHA1");
96 goto error;
97 }
98 /*
99 * OpenSSL <= 1.1.1 requires a non-null pointer for len
100 */
101 if (unlikely(EVP_DigestSignFinal(ctx, digest, &(size_t){ SHA1_DIGEST_LENGTH }) != 1)) {
102 fr_strerror_const("Failed finalising HMAC-SHA1");
103 goto error;
104 }
105
106 EVP_PKEY_free(pkey);
107 EVP_MD_CTX_reset(ctx);
108
109 return 0;
110}
111#else
112/** Calculate HMAC using internal SHA1 implementation
113 *
114 * @param digest Caller digest to be filled in.
115 * @param in Pointer to data stream.
116 * @param inlen length of data stream.
117 * @param key Pointer to authentication key.
118 * @param key_len Length of authentication key.
119 * @return
120 * - 0 on success.
121 * - -1 on error.
122 */
123int fr_hmac_sha1(uint8_t digest[static SHA1_DIGEST_LENGTH], uint8_t const *in, size_t inlen,
124 uint8_t const *key, size_t key_len)
125{
126 fr_sha1_ctx ctx;
127 uint8_t k_ipad[65]; /* inner padding - key XORd with ipad */
128 uint8_t k_opad[65]; /* outer padding - key XORd with opad */
129 uint8_t tk[20];
130 int i;
131 /* if key is longer than 64 bytes reset it to key=SHA1(key) */
132 if (key_len > 64) {
133
134 fr_sha1_ctx tctx;
135
136 fr_sha1_init(&tctx);
137 fr_sha1_update(&tctx, key, key_len);
138 fr_sha1_final(tk, &tctx);
139
140 key = tk;
141 key_len = 20;
142 }
143
144#ifdef HMAC_SHA1_DATA_PROBLEMS
145 if(sha1_data_problems)
146 {
147 int j,k;
148
149 printf("\nhmac-sha1 key(%d): ", key_len);
150 j=0; k=0;
151 for (i = 0; i < key_len; i++) {
152 if(j==4) {
153 printf("_");
154 j=0;
155 }
156 j++;
157
158 printf("%02x", key[i]);
159 }
160 printf("\nDATA: (%d) ",inlen);
161
162 j=0; k=0;
163 for (i = 0; i < inlen; i++) {
164 if(k==20) {
165 printf("\n ");
166 k=0;
167 j=0;
168 }
169 if(j==4) {
170 printf("_");
171 j=0;
172 }
173 k++;
174 j++;
175
176 printf("%02x", in[i]);
177 }
178 printf("\n");
179 }
180#endif
181
182
183 /*
184 * the HMAC_SHA1 transform looks like:
185 *
186 * SHA1(K XOR opad, SHA1(K XOR ipad, in))
187 *
188 * where K is an n byte key
189 * ipad is the byte 0x36 repeated 64 times
190
191 * opad is the byte 0x5c repeated 64 times
192 * and in is the data being protected
193 */
194
195 /* start out by storing key in pads */
196 memset(k_ipad, 0, sizeof(k_ipad));
197 memset(k_opad, 0, sizeof(k_opad));
198 memcpy(k_ipad, key, key_len);
199 memcpy(k_opad, key, key_len);
200
201 /* XOR key with ipad and opad values */
202 for (i = 0; i < 64; i++) {
203 k_ipad[i] ^= 0x36;
204 k_opad[i] ^= 0x5c;
205 }
206 /*
207 * perform inner SHA1
208 */
209 fr_sha1_init(&ctx); /* init ctx for 1st pass */
210 fr_sha1_update(&ctx, k_ipad, 64); /* start with inner pad */
211 fr_sha1_update(&ctx, in, inlen); /* then in of datagram */
212 fr_sha1_final(digest, &ctx); /* finish up 1st pass */
213 /*
214 * perform outer SHA1
215 */
216 fr_sha1_init(&ctx); /* init ctx for 2nd pass */
217 fr_sha1_update(&ctx, k_opad, 64); /* start with outer pad */
218 fr_sha1_update(&ctx, digest, 20); /* then results of 1st hash */
219 fr_sha1_final(digest, &ctx); /* finish up 2nd pass */
220
221#ifdef HMAC_SHA1_DATA_PROBLEMS
222 if (sha1_data_problems) {
223 int j;
224
225 printf("\nhmac-sha1 mac(20): ");
226 j=0;
227 for (i = 0; i < 20; i++) {
228 if(j==4) {
229 printf("_");
230 j=0;
231 }
232 j++;
233
234 printf("%02x", digest[i]);
235 }
236 printf("\n");
237 }
238#endif
239 return 0;
240}
241#endif /* HAVE_OPENSSL_EVP_H */
#define fr_atexit_thread_local(_name, _free, _uctx)
Definition atexit.h:220
#define RCSID(id)
Definition build.h:488
#define unlikely(_x)
Definition build.h:384
static fr_slen_t in
Definition dict.h:882
int fr_hmac_sha1(uint8_t digest[static SHA1_DIGEST_LENGTH], uint8_t const *in, size_t inlen, uint8_t const *key, size_t key_len)
Calculate HMAC using internal SHA1 implementation.
Definition hmac_sha1.c:123
unsigned char uint8_t
void fr_sha1_init(fr_sha1_ctx *context)
Definition sha1.c:93
void fr_sha1_final(uint8_t digest[static SHA1_DIGEST_LENGTH], fr_sha1_ctx *context)
Definition sha1.c:141
void fr_sha1_update(fr_sha1_ctx *context, uint8_t const *in, size_t len)
Definition sha1.c:105
#define SHA1_DIGEST_LENGTH
Definition sha1.h:29
#define fr_strerror_const(_msg)
Definition strerror.h:223
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030