~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/base/messagedigest.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2011, Google Inc.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are met:
 
7
 *
 
8
 *  1. Redistributions of source code must retain the above copyright notice,
 
9
 *     this list of conditions and the following disclaimer.
 
10
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 
11
 *     this list of conditions and the following disclaimer in the documentation
 
12
 *     and/or other materials provided with the distribution.
 
13
 *  3. The name of the author may not be used to endorse or promote products
 
14
 *     derived from this software without specific prior written permission
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
17
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
18
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 
19
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
20
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
21
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 
22
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 
23
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 
24
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
25
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
26
 */
 
27
 
 
28
#include "talk/base/messagedigest.h"
 
29
 
 
30
#include <string.h>
 
31
 
 
32
#if SSL_USE_OPENSSL
 
33
#include "talk/base/openssldigest.h"
 
34
#else
 
35
#include "talk/base/md5digest.h"
 
36
#include "talk/base/sha1digest.h"
 
37
#endif
 
38
#include "talk/base/scoped_ptr.h"
 
39
#include "talk/base/stringencode.h"
 
40
 
 
41
namespace talk_base {
 
42
 
 
43
// From RFC 4572.
 
44
const char DIGEST_MD5[]     = "md5";
 
45
const char DIGEST_SHA_1[]   = "sha-1";
 
46
const char DIGEST_SHA_224[] = "sha-224";
 
47
const char DIGEST_SHA_256[] = "sha-256";
 
48
const char DIGEST_SHA_384[] = "sha-384";
 
49
const char DIGEST_SHA_512[] = "sha-512";
 
50
 
 
51
static const size_t kBlockSize = 64;  // valid for SHA-256 and down
 
52
 
 
53
MessageDigest* MessageDigestFactory::Create(const std::string& alg) {
 
54
#if SSL_USE_OPENSSL
 
55
  MessageDigest* digest = new OpenSSLDigest(alg);
 
56
  if (digest->Size() == 0) {  // invalid algorithm
 
57
    delete digest;
 
58
    digest = NULL;
 
59
  }
 
60
  return digest;
 
61
#else
 
62
  MessageDigest* digest = NULL;
 
63
  if (alg == DIGEST_MD5) {
 
64
    digest = new Md5Digest();
 
65
  } else if (alg == DIGEST_SHA_1) {
 
66
    digest = new Sha1Digest();
 
67
  }
 
68
  return digest;
 
69
#endif
 
70
}
 
71
 
 
72
size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len,
 
73
                     void* output, size_t out_len) {
 
74
  digest->Update(input, in_len);
 
75
  return digest->Finish(output, out_len);
 
76
}
 
77
 
 
78
size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len,
 
79
                     void* output, size_t out_len) {
 
80
  scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
 
81
  return (digest.get()) ?
 
82
      ComputeDigest(digest.get(), input, in_len, output, out_len) :
 
83
      0;
 
84
}
 
85
 
 
86
std::string ComputeDigest(MessageDigest* digest, const std::string& input) {
 
87
  scoped_array<char> output(new char[digest->Size()]);
 
88
  ComputeDigest(digest, input.data(), input.size(),
 
89
                output.get(), digest->Size());
 
90
  return hex_encode(output.get(), digest->Size());
 
91
}
 
92
 
 
93
bool ComputeDigest(const std::string& alg, const std::string& input,
 
94
                   std::string* output) {
 
95
  scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
 
96
  if (!digest.get()) {
 
97
    return false;
 
98
  }
 
99
  *output = ComputeDigest(digest.get(), input);
 
100
  return true;
 
101
}
 
102
 
 
103
std::string ComputeDigest(const std::string& alg, const std::string& input) {
 
104
  std::string output;
 
105
  ComputeDigest(alg, input, &output);
 
106
  return output;
 
107
}
 
108
 
 
109
// Compute a RFC 2104 HMAC: H(K XOR opad, H(K XOR ipad, text))
 
110
size_t ComputeHmac(MessageDigest* digest,
 
111
                   const void* key, size_t key_len,
 
112
                   const void* input, size_t in_len,
 
113
                   void* output, size_t out_len) {
 
114
  // We only handle algorithms with a 64-byte blocksize.
 
115
  // TODO: Add BlockSize() method to MessageDigest.
 
116
  size_t block_len = kBlockSize;
 
117
  if (digest->Size() > 32) {
 
118
    return 0;
 
119
  }
 
120
  // Copy the key to a block-sized buffer to simplify padding.
 
121
  // If the key is longer than a block, hash it and use the result instead.
 
122
  scoped_array<uint8> new_key(new uint8[block_len]);
 
123
  if (key_len > block_len) {
 
124
    ComputeDigest(digest, key, key_len, new_key.get(), block_len);
 
125
    memset(new_key.get() + digest->Size(), 0, block_len - digest->Size());
 
126
  } else {
 
127
    memcpy(new_key.get(), key, key_len);
 
128
    memset(new_key.get() + key_len, 0, block_len - key_len);
 
129
  }
 
130
  // Set up the padding from the key, salting appropriately for each padding.
 
131
  scoped_array<uint8> o_pad(new uint8[block_len]), i_pad(new uint8[block_len]);
 
132
  for (size_t i = 0; i < block_len; ++i) {
 
133
    o_pad[i] = 0x5c ^ new_key[i];
 
134
    i_pad[i] = 0x36 ^ new_key[i];
 
135
  }
 
136
  // Inner hash; hash the inner padding, and then the input buffer.
 
137
  scoped_array<uint8> inner(new uint8[digest->Size()]);
 
138
  digest->Update(i_pad.get(), block_len);
 
139
  digest->Update(input, in_len);
 
140
  digest->Finish(inner.get(), digest->Size());
 
141
  // Outer hash; hash the outer padding, and then the result of the inner hash.
 
142
  digest->Update(o_pad.get(), block_len);
 
143
  digest->Update(inner.get(), digest->Size());
 
144
  return digest->Finish(output, out_len);
 
145
}
 
146
 
 
147
size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len,
 
148
                   const void* input, size_t in_len,
 
149
                   void* output, size_t out_len) {
 
150
  scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
 
151
  if (!digest.get()) {
 
152
    return 0;
 
153
  }
 
154
  return ComputeHmac(digest.get(), key, key_len,
 
155
                     input, in_len, output, out_len);
 
156
}
 
157
 
 
158
std::string ComputeHmac(MessageDigest* digest, const std::string& key,
 
159
                        const std::string& input) {
 
160
  scoped_array<char> output(new char[digest->Size()]);
 
161
  ComputeHmac(digest, key.data(), key.size(),
 
162
              input.data(), input.size(), output.get(), digest->Size());
 
163
  return hex_encode(output.get(), digest->Size());
 
164
}
 
165
 
 
166
bool ComputeHmac(const std::string& alg, const std::string& key,
 
167
                 const std::string& input, std::string* output) {
 
168
  scoped_ptr<MessageDigest> digest(MessageDigestFactory::Create(alg));
 
169
  if (!digest.get()) {
 
170
    return false;
 
171
  }
 
172
  *output = ComputeHmac(digest.get(), key, input);
 
173
  return true;
 
174
}
 
175
 
 
176
std::string ComputeHmac(const std::string& alg, const std::string& key,
 
177
                        const std::string& input) {
 
178
  std::string output;
 
179
  ComputeHmac(alg, key, input, &output);
 
180
  return output;
 
181
}
 
182
 
 
183
}  // namespace talk_base