xref: /aosp_15_r20/external/rappor/client/cpp/openssl_hash_impl.cc (revision 2abb31345f6c95944768b5222a9a5ed3fc68cc00)
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "openssl_hash_impl.h"
16 
17 #include <stdlib.h>
18 #include <string>
19 
20 #include <openssl/evp.h>  // EVP_sha256
21 #include <openssl/hmac.h>  // HMAC
22 #include <openssl/md5.h>  // MD5
23 #include <openssl/sha.h>  // SHA256_DIGEST_LENGTH
24 
25 namespace rappor {
26 
27 // of type HmacFunc in rappor_deps.h
HmacSha256(const std::string & key,const std::string & value,std::vector<uint8_t> * output)28 bool HmacSha256(const std::string& key, const std::string& value,
29           std::vector<uint8_t>* output) {
30   output->resize(SHA256_DIGEST_LENGTH, 0);
31 
32   // Returns a pointer on success, or NULL on failure.
33   unsigned char* result = HMAC(
34       EVP_sha256(), key.c_str(), key.size(),
35       // std::string has 'char', OpenSSL wants unsigned char.
36       reinterpret_cast<const unsigned char*>(value.c_str()),
37       value.size(),
38       output->data(),
39       NULL);
40 
41   return (result != NULL);
42 }
43 
44 // Of type HmacFunc in rappor_deps.h
45 //
46 // The length of the passed-in output vector determines how many
47 // bytes are returned.
48 //
49 // No reseed operation, but recommended reseed_interval <= 2^48 updates.
50 // Since we're seeding for each value and typically don't need
51 // so many bytes, we should be OK.
HmacDrbg(const std::string & key,const std::string & value,std::vector<uint8_t> * output)52 bool HmacDrbg(const std::string& key, const std::string& value,
53               std::vector<uint8_t>* output) {
54   const unsigned char k_array[] = {
55     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
59   };
60   std::string v;
61   std::vector<uint8_t> temp_output;
62   int num_bytes = output->size();
63   if (num_bytes == 0) {
64     // By default return 32 bytes for Uint32 applications.
65     num_bytes = 32;
66   }
67 
68   v.append(32u, 0x01);
69   temp_output.resize(32, 0);
70 
71   std::string temp_str(v);
72   temp_str.append(std::string("\0", 1));
73   // provided_data is key|value.
74   temp_str.append(key);
75   temp_str.append(value);
76 
77   output->resize(0);
78 
79   // Instantiate.
80   if (!HmacSha256(std::string(k_array, k_array + 32), temp_str, &temp_output)) {
81     return false;
82   }
83   std::string k(temp_output.begin(), temp_output.end());
84   if (!HmacSha256(k, v, &temp_output)) {
85     return false;
86   }
87   v = std::string(temp_output.begin(), temp_output.end());
88   if (!HmacSha256(k, v + std::string("\1", 1) + key + value, &temp_output)) {
89     return false;
90   }
91   k = std::string(temp_output.begin(), temp_output.end());
92   if (!HmacSha256(k, v, &temp_output)) {
93     return false;
94   }
95   v = std::string(temp_output.begin(), temp_output.end());
96 
97   while (output->size() < num_bytes) {
98     // Generate.
99     if (!HmacSha256(k, v, &temp_output)) {
100       return false;
101     }
102     v = std::string(temp_output.begin(), temp_output.end());
103     output->insert(output->end(), temp_output.begin(), temp_output.end());
104   }
105   output->resize(num_bytes);
106   return true;
107 }
108 
109 // of type HashFunc in rappor_deps.h
Md5(const std::string & value,std::vector<uint8_t> * output)110 bool Md5(const std::string& value, std::vector<uint8_t>* output) {
111   output->resize(MD5_DIGEST_LENGTH, 0);
112 
113   // std::string has 'char', OpenSSL wants unsigned char.
114   MD5(reinterpret_cast<const unsigned char*>(value.c_str()),
115       value.size(), output->data());
116   return true;  // OpenSSL MD5 doesn't return an error code
117 }
118 
119 }  // namespace rappor
120