181b79547SAlex Hornung /*
281b79547SAlex Hornung * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
381b79547SAlex Hornung * All rights reserved.
481b79547SAlex Hornung *
581b79547SAlex Hornung * Redistribution and use in source and binary forms, with or without
681b79547SAlex Hornung * modification, are permitted provided that the following conditions
781b79547SAlex Hornung * are met:
881b79547SAlex Hornung *
981b79547SAlex Hornung * 1. Redistributions of source code must retain the above copyright
1081b79547SAlex Hornung * notice, this list of conditions and the following disclaimer.
1181b79547SAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright
1281b79547SAlex Hornung * notice, this list of conditions and the following disclaimer in
1381b79547SAlex Hornung * the documentation and/or other materials provided with the
1481b79547SAlex Hornung * distribution.
1581b79547SAlex Hornung *
1681b79547SAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1781b79547SAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1881b79547SAlex Hornung * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1981b79547SAlex Hornung * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2081b79547SAlex Hornung * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2181b79547SAlex Hornung * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2281b79547SAlex Hornung * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2381b79547SAlex Hornung * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2481b79547SAlex Hornung * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2581b79547SAlex Hornung * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2681b79547SAlex Hornung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2781b79547SAlex Hornung * SUCH DAMAGE.
2881b79547SAlex Hornung */
2981b79547SAlex Hornung
3081b79547SAlex Hornung #include <errno.h>
3181b79547SAlex Hornung #include <openssl/evp.h>
3281b79547SAlex Hornung
3381b79547SAlex Hornung #include "tcplay.h"
3481b79547SAlex Hornung
3581b79547SAlex Hornung
3681b79547SAlex Hornung int
pbkdf2(struct pbkdf_prf_algo * hash,const char * pass,int passlen,const unsigned char * salt,int saltlen,int keylen,unsigned char * out)3781b79547SAlex Hornung pbkdf2(struct pbkdf_prf_algo *hash, const char *pass, int passlen,
3881b79547SAlex Hornung const unsigned char *salt, int saltlen,
3981b79547SAlex Hornung int keylen, unsigned char *out)
4081b79547SAlex Hornung {
4181b79547SAlex Hornung const EVP_MD *md;
4281b79547SAlex Hornung int r;
4381b79547SAlex Hornung
4481b79547SAlex Hornung OpenSSL_add_all_algorithms();
4581b79547SAlex Hornung
46*7b1e1c8eSDaniel Fojt md = EVP_get_digestbyname(hash->algo);
4781b79547SAlex Hornung if (md == NULL) {
48*7b1e1c8eSDaniel Fojt tc_log(1, "Hash %s not found\n", hash->algo);
4981b79547SAlex Hornung return ENOENT;
5081b79547SAlex Hornung }
5181b79547SAlex Hornung r = PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen,
5281b79547SAlex Hornung hash->iteration_count, md, keylen, out);
5381b79547SAlex Hornung
5481b79547SAlex Hornung if (r == 0) {
5581b79547SAlex Hornung tc_log(1, "Error in PBKDF2\n");
5681b79547SAlex Hornung return EINVAL;
5781b79547SAlex Hornung }
5881b79547SAlex Hornung
5981b79547SAlex Hornung return 0;
6081b79547SAlex Hornung }
6181b79547SAlex Hornung
62