blob: 07ecb1f7d7a9aa5aaef08bbf478a61ac96cdad92 [file] [log] [blame]
bigbiff7ba75002020-04-11 20:47:09 -04001/*
2 * Copyright (C) 2016 Team Win Recovery Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/*
18 * This computes the "secret" used by Android as one of the parameters
19 * to decrypt File Based Encryption. The secret is prefixed with
20 * "Android FBE credential hash" padded with 0s to 128 bytes then the
21 * user's password is appended to the end of the 128 bytes. This string
22 * is then hashed with sha512 and the sha512 value is then converted to
23 * hex with upper-case characters.
24 */
25
26#include <stdio.h>
27#include <string>
28#include <stdlib.h>
29#include <openssl/sha.h>
30#include <openssl/hmac.h>
31
32#include "HashPassword.h"
33
34#define PASS_PADDING_SIZE 128
35#define SHA512_HEX_SIZE SHA512_DIGEST_LENGTH * 2
36#define SHA256_HEX_SIZE SHA256_DIGEST_LENGTH * 2
37
38void* PersonalizedHashBinary(const char* prefix, const char* key, const size_t key_size) {
39 size_t size = PASS_PADDING_SIZE + key_size;
40 unsigned char* buffer = (unsigned char*)calloc(1, size);
41 if (!buffer) return NULL; // failed to malloc
42 memcpy((void*)buffer, (void*)prefix, strlen(prefix));
43 unsigned char* ptr = buffer + PASS_PADDING_SIZE;
44 memcpy((void*)ptr, key, key_size);
45 unsigned char hash[SHA512_DIGEST_LENGTH];
46 SHA512_CTX sha512;
47 SHA512_Init(&sha512);
48 SHA512_Update(&sha512, buffer, size);
49 SHA512_Final(hash, &sha512);
50 free(buffer);
51 void* ret = malloc(SHA512_DIGEST_LENGTH);
52 if (!ret) return NULL; // failed to malloc
53 memcpy(ret, (void*)&hash[0], SHA512_DIGEST_LENGTH);
54 return ret;
55}
56
57std::string PersonalizedHash(const char* prefix, const char* key, const size_t key_size) {
58 size_t size = PASS_PADDING_SIZE + key_size;
59 unsigned char* buffer = (unsigned char*)calloc(1, size);
60 if (!buffer) return ""; // failed to malloc
61 memcpy((void*)buffer, (void*)prefix, strlen(prefix));
62 unsigned char* ptr = buffer + PASS_PADDING_SIZE;
63 memcpy((void*)ptr, key, key_size);
64 unsigned char hash[SHA512_DIGEST_LENGTH];
65 SHA512_CTX sha512;
66 SHA512_Init(&sha512);
67 SHA512_Update(&sha512, buffer, size);
68 SHA512_Final(hash, &sha512);
69 int index = 0;
70 char hex_hash[SHA512_HEX_SIZE + 1];
71 for(index = 0; index < SHA512_DIGEST_LENGTH; index++)
72 sprintf(hex_hash + (index * 2), "%02X", hash[index]);
73 hex_hash[128] = 0;
74 std::string ret = hex_hash;
75 free(buffer);
76 return ret;
77}
78
79std::string PersonalizedHash(const char* prefix, const std::string& Password) {
80 return PersonalizedHash(prefix, Password.c_str(), Password.size());
81}
82
83std::string PersonalizedHashSP800(const char* label, const char* context, const char* key, const size_t key_size) {
84 HMAC_CTX ctx;
85 HMAC_CTX_init(&ctx);
86 HMAC_Init_ex(&ctx, key, key_size, EVP_sha256(), NULL);
87 unsigned int counter = 1;
88 endianswap(&counter);
89 HMAC_Update(&ctx, (const unsigned char*)&counter, 4);
90 HMAC_Update(&ctx, (const unsigned char*)label, strlen(label));
91 const unsigned char divider = 0;
92 HMAC_Update(&ctx, &divider, 1);
93 HMAC_Update(&ctx, (const unsigned char*)context, strlen(context));
94 unsigned int contextDisambiguation = strlen(context) * 8;
95 endianswap(&contextDisambiguation);
96 HMAC_Update(&ctx, (const unsigned char*)&contextDisambiguation, 4);
97 unsigned int finalValue = 256;
98 endianswap(&finalValue);
99 HMAC_Update(&ctx, (const unsigned char*)&finalValue, 4);
100
101 unsigned char output[SHA256_DIGEST_LENGTH];
102 unsigned int out_size = 0;
103 HMAC_Final(&ctx, output, &out_size);
104
105 int index = 0;
106 char hex_hash[SHA256_HEX_SIZE + 1];
107 for(index = 0; index < SHA256_DIGEST_LENGTH; index++)
108 sprintf(hex_hash + (index * 2), "%02x", output[index]);
109 hex_hash[SHA256_HEX_SIZE] = 0;
110 std::string ret = hex_hash;
111 return ret;
112}
113
114std::string HashPassword(const std::string& Password) {
115 const char* prefix = FBE_PERSONALIZATION;
116 return PersonalizedHash(prefix, Password);
117}