| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 const void* input, size_t in_len, | 110 const void* input, size_t in_len, |
| 111 void* output, size_t out_len) { | 111 void* output, size_t out_len) { |
| 112 // We only handle algorithms with a 64-byte blocksize. | 112 // We only handle algorithms with a 64-byte blocksize. |
| 113 // TODO: Add BlockSize() method to MessageDigest. | 113 // TODO: Add BlockSize() method to MessageDigest. |
| 114 size_t block_len = kBlockSize; | 114 size_t block_len = kBlockSize; |
| 115 if (digest->Size() > 32) { | 115 if (digest->Size() > 32) { |
| 116 return 0; | 116 return 0; |
| 117 } | 117 } |
| 118 // Copy the key to a block-sized buffer to simplify padding. | 118 // Copy the key to a block-sized buffer to simplify padding. |
| 119 // If the key is longer than a block, hash it and use the result instead. | 119 // If the key is longer than a block, hash it and use the result instead. |
| 120 scoped_ptr<uint8[]> new_key(new uint8[block_len]); | 120 scoped_ptr<uint8_t[]> new_key(new uint8_t[block_len]); |
| 121 if (key_len > block_len) { | 121 if (key_len > block_len) { |
| 122 ComputeDigest(digest, key, key_len, new_key.get(), block_len); | 122 ComputeDigest(digest, key, key_len, new_key.get(), block_len); |
| 123 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size()); | 123 memset(new_key.get() + digest->Size(), 0, block_len - digest->Size()); |
| 124 } else { | 124 } else { |
| 125 memcpy(new_key.get(), key, key_len); | 125 memcpy(new_key.get(), key, key_len); |
| 126 memset(new_key.get() + key_len, 0, block_len - key_len); | 126 memset(new_key.get() + key_len, 0, block_len - key_len); |
| 127 } | 127 } |
| 128 // Set up the padding from the key, salting appropriately for each padding. | 128 // Set up the padding from the key, salting appropriately for each padding. |
| 129 scoped_ptr<uint8[]> o_pad(new uint8[block_len]), i_pad(new uint8[block_len]); | 129 scoped_ptr<uint8_t[]> o_pad(new uint8_t[block_len]); |
| 130 scoped_ptr<uint8_t[]> i_pad(new uint8_t[block_len]); |
| 130 for (size_t i = 0; i < block_len; ++i) { | 131 for (size_t i = 0; i < block_len; ++i) { |
| 131 o_pad[i] = 0x5c ^ new_key[i]; | 132 o_pad[i] = 0x5c ^ new_key[i]; |
| 132 i_pad[i] = 0x36 ^ new_key[i]; | 133 i_pad[i] = 0x36 ^ new_key[i]; |
| 133 } | 134 } |
| 134 // Inner hash; hash the inner padding, and then the input buffer. | 135 // Inner hash; hash the inner padding, and then the input buffer. |
| 135 scoped_ptr<uint8[]> inner(new uint8[digest->Size()]); | 136 scoped_ptr<uint8_t[]> inner(new uint8_t[digest->Size()]); |
| 136 digest->Update(i_pad.get(), block_len); | 137 digest->Update(i_pad.get(), block_len); |
| 137 digest->Update(input, in_len); | 138 digest->Update(input, in_len); |
| 138 digest->Finish(inner.get(), digest->Size()); | 139 digest->Finish(inner.get(), digest->Size()); |
| 139 // Outer hash; hash the outer padding, and then the result of the inner hash. | 140 // Outer hash; hash the outer padding, and then the result of the inner hash. |
| 140 digest->Update(o_pad.get(), block_len); | 141 digest->Update(o_pad.get(), block_len); |
| 141 digest->Update(inner.get(), digest->Size()); | 142 digest->Update(inner.get(), digest->Size()); |
| 142 return digest->Finish(output, out_len); | 143 return digest->Finish(output, out_len); |
| 143 } | 144 } |
| 144 | 145 |
| 145 size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len, | 146 size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 172 } | 173 } |
| 173 | 174 |
| 174 std::string ComputeHmac(const std::string& alg, const std::string& key, | 175 std::string ComputeHmac(const std::string& alg, const std::string& key, |
| 175 const std::string& input) { | 176 const std::string& input) { |
| 176 std::string output; | 177 std::string output; |
| 177 ComputeHmac(alg, key, input, &output); | 178 ComputeHmac(alg, key, input, &output); |
| 178 return output; | 179 return output; |
| 179 } | 180 } |
| 180 | 181 |
| 181 } // namespace rtc | 182 } // namespace rtc |
| OLD | NEW |