Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / wtf / SHA1.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 // A straightforward SHA-1 implementation based on RFC 3174.
32 // http://www.ietf.org/rfc/rfc3174.txt
33 // The names of functions and variables (such as "a", "b", and "f") follow notations in RFC 3174.
34
35 #include "config.h"
36 #include "wtf/SHA1.h"
37
38 #include "wtf/Assertions.h"
39
40 namespace WTF {
41
42 static inline uint32_t f(int t, uint32_t b, uint32_t c, uint32_t d)
43 {
44     ASSERT(t >= 0 && t < 80);
45     if (t < 20)
46         return (b & c) | ((~b) & d);
47     if (t < 40)
48         return b ^ c ^ d;
49     if (t < 60)
50         return (b & c) | (b & d) | (c & d);
51     return b ^ c ^ d;
52 }
53
54 static inline uint32_t k(int t)
55 {
56     ASSERT(t >= 0 && t < 80);
57     if (t < 20)
58         return 0x5a827999;
59     if (t < 40)
60         return 0x6ed9eba1;
61     if (t < 60)
62         return 0x8f1bbcdc;
63     return 0xca62c1d6;
64 }
65
66 static inline uint32_t rotateLeft(int n, uint32_t x)
67 {
68     ASSERT(n >= 0 && n < 32);
69     return (x << n) | (x >> (32 - n));
70 }
71
72 SHA1::SHA1()
73 {
74     reset();
75 }
76
77 void SHA1::addBytes(const uint8_t* input, size_t length)
78 {
79     while (length--) {
80         ASSERT(m_cursor < 64);
81         m_buffer[m_cursor++] = *input++;
82         ++m_totalBytes;
83         if (m_cursor == 64)
84             processBlock();
85     }
86 }
87
88 void SHA1::computeHash(Vector<uint8_t, 20>& digest)
89 {
90     finalize();
91
92     digest.clear();
93     digest.resize(20);
94     for (size_t i = 0; i < 5; ++i) {
95         // Treat hashValue as a big-endian value.
96         uint32_t hashValue = m_hash[i];
97         for (int j = 0; j < 4; ++j) {
98             digest[4 * i + (3 - j)] = hashValue & 0xFF;
99             hashValue >>= 8;
100         }
101     }
102
103     reset();
104 }
105
106 void SHA1::finalize()
107 {
108     ASSERT(m_cursor < 64);
109     m_buffer[m_cursor++] = 0x80;
110     if (m_cursor > 56) {
111         // Pad out to next block.
112         while (m_cursor < 64)
113             m_buffer[m_cursor++] = 0x00;
114         processBlock();
115     }
116
117     for (size_t i = m_cursor; i < 56; ++i)
118         m_buffer[i] = 0x00;
119
120     // Write the length as a big-endian 64-bit value.
121     uint64_t bits = m_totalBytes * 8;
122     for (int i = 0; i < 8; ++i) {
123         m_buffer[56 + (7 - i)] = bits & 0xFF;
124         bits >>= 8;
125     }
126     m_cursor = 64;
127     processBlock();
128 }
129
130 void SHA1::processBlock()
131 {
132     ASSERT(m_cursor == 64);
133
134     uint32_t w[80] = { 0 };
135     for (int t = 0; t < 16; ++t)
136         w[t] = (m_buffer[t * 4] << 24) | (m_buffer[t * 4 + 1] << 16) | (m_buffer[t * 4 + 2] << 8) | m_buffer[t * 4 + 3];
137     for (int t = 16; t < 80; ++t)
138         w[t] = rotateLeft(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
139
140     uint32_t a = m_hash[0];
141     uint32_t b = m_hash[1];
142     uint32_t c = m_hash[2];
143     uint32_t d = m_hash[3];
144     uint32_t e = m_hash[4];
145
146     for (int t = 0; t < 80; ++t) {
147         uint32_t temp = rotateLeft(5, a) + f(t, b, c, d) + e + w[t] + k(t);
148         e = d;
149         d = c;
150         c = rotateLeft(30, b);
151         b = a;
152         a = temp;
153     }
154
155     m_hash[0] += a;
156     m_hash[1] += b;
157     m_hash[2] += c;
158     m_hash[3] += d;
159     m_hash[4] += e;
160
161     m_cursor = 0;
162 }
163
164 void SHA1::reset()
165 {
166     m_cursor = 0;
167     m_totalBytes = 0;
168     m_hash[0] = 0x67452301;
169     m_hash[1] = 0xefcdab89;
170     m_hash[2] = 0x98badcfe;
171     m_hash[3] = 0x10325476;
172     m_hash[4] = 0xc3d2e1f0;
173
174     // Clear the buffer after use in case it's sensitive.
175     memset(m_buffer, 0, sizeof(m_buffer));
176 }
177
178 } // namespace WTF