2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 2001-2002
5
* Sleepycat Software. All rights reserved.
7
* Some parts of this code originally written by Adam Stubblefield,
11
#include "db_config.h"
14
static const char revid[] = "$Id$";
17
#ifndef NO_SYSTEM_INCLUDES
22
#include "dbinc/crypto.h"
23
#include "dbinc/db_page.h" /* for hash.h only */
24
#include "dbinc/hash.h"
25
#include "dbinc/hmac.h"
27
#define HMAC_OUTPUT_SIZE 20
28
#define HMAC_BLOCK_SIZE 64
30
static void __db_hmac __P((u_int8_t *, u_int8_t *, size_t, u_int8_t *));
34
* All of these functions use a ctx structure on the stack. The __db_SHA1Init
35
* call does not initialize the 64-byte buffer portion of it. The
36
* underlying SHA1 functions will properly pad the buffer if the data length
37
* is less than 64-bytes, so there isn't a chance of reading uninitialized
38
* memory. Although it would be cleaner to do a memset(ctx.buffer, 0, 64)
39
* we do not want to incur that penalty if we don't have to for performance.
47
__db_hmac(k, data, data_len, mac)
48
u_int8_t *k, *data, *mac;
52
u_int8_t key[HMAC_BLOCK_SIZE];
53
u_int8_t ipad[HMAC_BLOCK_SIZE];
54
u_int8_t opad[HMAC_BLOCK_SIZE];
55
u_int8_t tmp[HMAC_OUTPUT_SIZE];
58
memset(key, 0x00, HMAC_BLOCK_SIZE);
59
memset(ipad, 0x36, HMAC_BLOCK_SIZE);
60
memset(opad, 0x5C, HMAC_BLOCK_SIZE);
62
memcpy(key, k, HMAC_OUTPUT_SIZE);
64
for (i = 0; i < HMAC_BLOCK_SIZE; i++) {
70
__db_SHA1Update(&ctx, ipad, HMAC_BLOCK_SIZE);
71
__db_SHA1Update(&ctx, data, data_len);
72
__db_SHA1Final(tmp, &ctx);
74
__db_SHA1Update(&ctx, opad, HMAC_BLOCK_SIZE);
75
__db_SHA1Update(&ctx, tmp, HMAC_OUTPUT_SIZE);
76
__db_SHA1Final(mac, &ctx);
82
* Create a MAC/SHA1 checksum.
84
* PUBLIC: void __db_chksum __P((u_int8_t *, size_t, u_int8_t *, u_int8_t *));
87
__db_chksum(data, data_len, mac_key, store)
95
u_int8_t tmp[DB_MAC_KEY];
98
* Since the checksum might be on a page of data we are checksumming
99
* we might be overwriting after checksumming, we zero-out the
100
* checksum value so that we can have a known value there when
101
* we verify the checksum.
104
sumlen = sizeof(u_int32_t);
107
memset(store, 0, sumlen);
108
if (mac_key == NULL) {
109
/* Just a hash, no MAC */
110
hash4 = __ham_func4(NULL, data, (u_int32_t)data_len);
111
memcpy(store, &hash4, sumlen);
113
memset(tmp, 0, DB_MAC_KEY);
114
__db_hmac(mac_key, data, data_len, tmp);
115
memcpy(store, tmp, sumlen);
121
* Create a MAC/SHA1 key.
123
* PUBLIC: void __db_derive_mac __P((u_int8_t *, size_t, u_int8_t *));
126
__db_derive_mac(passwd, plen, mac_key)
133
/* Compute the MAC key. mac_key must be 20 bytes. */
135
__db_SHA1Update(&ctx, passwd, plen);
136
__db_SHA1Update(&ctx, (u_int8_t *)DB_MAC_MAGIC, strlen(DB_MAC_MAGIC));
137
__db_SHA1Update(&ctx, passwd, plen);
138
__db_SHA1Final(mac_key, &ctx);
144
* __db_check_chksum --
147
* Return 0 on success, >0 (errno) on error, -1 on checksum mismatch.
149
* PUBLIC: int __db_check_chksum __P((DB_ENV *,
150
* PUBLIC: DB_CIPHER *, u_int8_t *, void *, size_t, int));
153
__db_check_chksum(dbenv, db_cipher, chksum, data, data_len, is_hmac)
155
DB_CIPHER *db_cipher;
164
u_int8_t *mac_key, old[DB_MAC_KEY], new[DB_MAC_KEY];
167
* If we are just doing checksumming and not encryption, then checksum
168
* is 4 bytes. Otherwise, it is DB_MAC_KEY size. Check for illegal
169
* combinations of crypto/non-crypto checksums.
172
if (db_cipher != NULL) {
174
"Unencrypted checksum with a supplied encryption key");
177
sum_len = sizeof(u_int32_t);
180
if (db_cipher == NULL) {
182
"Encrypted checksum: no encryption key specified");
185
sum_len = DB_MAC_KEY;
186
mac_key = db_cipher->mac_key;
191
* Since the checksum might be on the page, we need to have known data
192
* there so that we can generate the same original checksum. We zero
193
* it out, just like we do in __db_chksum above.
195
memcpy(old, chksum, sum_len);
196
memset(chksum, 0, sum_len);
197
if (mac_key == NULL) {
198
/* Just a hash, no MAC */
199
hash4 = __ham_func4(NULL, data, (u_int32_t)data_len);
200
ret = memcmp((u_int32_t *)old, &hash4, sum_len) ? -1 : 0;
202
__db_hmac(mac_key, data, data_len, new);
203
ret = memcmp(old, new, sum_len) ? -1 : 0;