2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <openssl/evp.h>
19 #include <sys/types.h>
28 * Simple program to generate a key based on PBKDF2 with preset inputs.
30 * Will print out the salt and key in hex.
37 int main(int argc, char* argv[])
40 fprintf(stderr, "Usage: %s <password>\n", argv[0]);
44 int fd = open("/dev/urandom", O_RDONLY);
46 fprintf(stderr, "Could not open /dev/urandom: %s\n", strerror(errno));
51 unsigned char salt[SALT_LEN];
53 if (read(fd, &salt, SALT_LEN) != SALT_LEN) {
54 fprintf(stderr, "Could not read salt from /dev/urandom: %s\n", strerror(errno));
60 unsigned char rawKey[KEY_BITS];
62 if (PKCS5_PBKDF2_HMAC_SHA1(argv[1], strlen(argv[1]), salt, SALT_LEN,
63 ROUNDS, KEY_BITS, rawKey) != 1) {
64 fprintf(stderr, "Could not generate PBKDF2 output: %s\n", strerror(errno));
69 for (int i = 0; i < SALT_LEN; i++) {
70 printf("%02x", salt[i]);
75 for (int i = 0; i < (KEY_BITS / 8); i++) {
76 printf("%02x", rawKey[i]);