OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of crypto; | 5 part of crypto; |
6 | 6 |
7 class _HMAC implements HMAC { | 7 /** |
| 8 * Hash-based Message Authentication Code support. |
| 9 * |
| 10 * The [add] method is used to add data to the message. The [digest] and |
| 11 * [close] methods are used to extract the message authentication code. |
| 12 */ |
| 13 // TODO(floitsch): make Hash implement Sink, EventSink or similar. |
| 14 class HMAC { |
8 bool _isClosed = false; | 15 bool _isClosed = false; |
9 | 16 |
10 _HMAC(Hash this._hash, List<int> this._key) : _message = []; | 17 /** |
| 18 * Create an [HMAC] object from a [Hash] and a key. |
| 19 */ |
| 20 HMAC(Hash this._hash, List<int> this._key) : _message = []; |
11 | 21 |
| 22 /** |
| 23 * Add a list of bytes to the message. |
| 24 */ |
12 add(List<int> data) { | 25 add(List<int> data) { |
13 if (_isClosed) throw new StateError("HMAC is closed"); | 26 if (_isClosed) throw new StateError("HMAC is closed"); |
14 _message.addAll(data); | 27 _message.addAll(data); |
15 } | 28 } |
16 | 29 |
| 30 /** |
| 31 * Extract the message digest as a list of bytes without closing [this]. |
| 32 */ |
17 List<int> get digest { | 33 List<int> get digest { |
18 var blockSize = _hash.blockSize; | 34 var blockSize = _hash.blockSize; |
19 | 35 |
20 // Hash the key if it is longer than the block size of the hash. | 36 // Hash the key if it is longer than the block size of the hash. |
21 if (_key.length > blockSize) { | 37 if (_key.length > blockSize) { |
22 _hash = _hash.newInstance(); | 38 _hash = _hash.newInstance(); |
23 _hash.add(_key); | 39 _hash.add(_key); |
24 _key = _hash.close(); | 40 _key = _hash.close(); |
25 } | 41 } |
26 | 42 |
(...skipping 24 matching lines...) Expand all Loading... |
51 padding[i] = 0x5c ^ _key[i]; | 67 padding[i] = 0x5c ^ _key[i]; |
52 } | 68 } |
53 | 69 |
54 // Outer hash computation which is the result. | 70 // Outer hash computation which is the result. |
55 _hash = _hash.newInstance(); | 71 _hash = _hash.newInstance(); |
56 _hash.add(padding); | 72 _hash.add(padding); |
57 _hash.add(innerHash); | 73 _hash.add(innerHash); |
58 return _hash.close(); | 74 return _hash.close(); |
59 } | 75 } |
60 | 76 |
| 77 /** |
| 78 * Perform the actual computation and extract the message digest |
| 79 * as a list of bytes. |
| 80 */ |
61 List<int> close() { | 81 List<int> close() { |
62 _isClosed = true; | 82 _isClosed = true; |
63 return digest; | 83 return digest; |
64 } | 84 } |
65 | 85 |
| 86 /** |
| 87 * Verify that the HMAC computed for the data so far matches the |
| 88 * given message digest. |
| 89 * |
| 90 * This method should be used instead of memcmp-style comparisons |
| 91 * to avoid leaking information via timing. |
| 92 * |
| 93 * Throws an exception if the given digest does not have the same |
| 94 * size as the digest computed by this HMAC instance. |
| 95 */ |
66 bool verify(List<int> digest) { | 96 bool verify(List<int> digest) { |
67 var computedDigest = this.digest; | 97 var computedDigest = this.digest; |
68 if (digest.length != computedDigest.length) { | 98 if (digest.length != computedDigest.length) { |
69 throw new ArgumentError( | 99 throw new ArgumentError( |
70 'Invalid digest size: ${digest.length} in HMAC.verify. ' | 100 'Invalid digest size: ${digest.length} in HMAC.verify. ' |
71 'Expected: ${_hash.blockSize}.'); | 101 'Expected: ${_hash.blockSize}.'); |
72 } | 102 } |
73 int result = 0; | 103 int result = 0; |
74 for (var i = 0; i < digest.length; i++) { | 104 for (var i = 0; i < digest.length; i++) { |
75 result |= digest[i] ^ computedDigest[i]; | 105 result |= digest[i] ^ computedDigest[i]; |
76 } | 106 } |
77 return result == 0; | 107 return result == 0; |
78 } | 108 } |
79 | 109 |
80 // HMAC internal state. | 110 // HMAC internal state. |
81 Hash _hash; | 111 Hash _hash; |
82 List<int> _key; | 112 List<int> _key; |
83 List<int> _message; | 113 List<int> _message; |
84 } | 114 } |
OLD | NEW |