1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #ifdef _KERNEL 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #else 34 #include <sys/resource.h> 35 #include <stdint.h> 36 #include <strings.h> 37 #endif 38 39 #include <geom/eli/g_eli.h> 40 #include <geom/eli/pkcs5v2.h> 41 42 static __inline void 43 xor(uint8_t *dst, const uint8_t *src, size_t size) 44 { 45 46 for (; size > 0; size--) 47 *dst++ ^= *src++; 48 } 49 50 void 51 pkcs5v2_genkey(uint8_t *key, unsigned keylen, const uint8_t *salt, 52 size_t saltsize, const char *passphrase, u_int iterations) 53 { 54 55 pkcs5v2_genkey_raw(key, keylen, salt, saltsize, passphrase, 56 strlen(passphrase), iterations); 57 } 58 59 void 60 pkcs5v2_genkey_raw(uint8_t *key, unsigned keylen, const uint8_t *salt, 61 size_t saltsize, const uint8_t *passphrase, size_t passlen, 62 u_int iterations) 63 { 64 uint8_t md[SHA512_MDLEN], saltcount[saltsize + sizeof(uint32_t)]; 65 uint8_t *counter, *keyp; 66 u_int i, bsize; 67 uint32_t count; 68 struct hmac_ctx startpoint, ctx; 69 70 bzero(key, keylen); 71 bcopy(salt, saltcount, saltsize); 72 counter = saltcount + saltsize; 73 74 keyp = key; 75 for (count = 1; keylen > 0; count++, keylen -= bsize, keyp += bsize) { 76 bsize = MIN(keylen, sizeof(md)); 77 78 be32enc(counter, count); 79 80 g_eli_crypto_hmac_init(&startpoint, passphrase, passlen); 81 ctx = startpoint; 82 g_eli_crypto_hmac_update(&ctx, saltcount, sizeof(saltcount)); 83 g_eli_crypto_hmac_final(&ctx, md, sizeof(md)); 84 xor(keyp, md, bsize); 85 86 for(i = 1; i < iterations; i++) { 87 ctx = startpoint; 88 g_eli_crypto_hmac_update(&ctx, md, sizeof(md)); 89 g_eli_crypto_hmac_final(&ctx, md, sizeof(md)); 90 xor(keyp, md, bsize); 91 } 92 } 93 explicit_bzero(&startpoint, sizeof(startpoint)); 94 explicit_bzero(&ctx, sizeof(ctx)); 95 } 96 97 #ifndef _KERNEL 98 #ifndef _STANDALONE 99 /* 100 * Return the number of microseconds needed for 'interations' iterations. 101 */ 102 static int 103 pkcs5v2_probe(int iterations) 104 { 105 uint8_t key[G_ELI_USERKEYLEN], salt[G_ELI_SALTLEN]; 106 const char passphrase[] = "passphrase"; 107 struct rusage start, end; 108 int usecs; 109 110 getrusage(RUSAGE_SELF, &start); 111 pkcs5v2_genkey(key, sizeof(key), salt, sizeof(salt), passphrase, 112 iterations); 113 getrusage(RUSAGE_SELF, &end); 114 115 usecs = end.ru_utime.tv_sec - start.ru_utime.tv_sec; 116 usecs *= 1000000; 117 usecs += end.ru_utime.tv_usec - start.ru_utime.tv_usec; 118 return (usecs); 119 } 120 121 /* 122 * Return the number of iterations which takes 'usecs' microseconds. 123 */ 124 int 125 pkcs5v2_calculate(int usecs) 126 { 127 int iterations, v; 128 129 for (iterations = 1; ; iterations <<= 1) { 130 v = pkcs5v2_probe(iterations); 131 if (v > 2000000) 132 break; 133 } 134 return (((intmax_t)iterations * (intmax_t)usecs) / v); 135 } 136 #endif /* !_STANDALONE */ 137 #endif /* !_KERNEL */ 138