| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
| 8 #include "base/macros.h" |
| 5 #include "base/values.h" | 9 #include "base/values.h" |
| 6 #include "components/webcrypto/algorithm_dispatch.h" | 10 #include "components/webcrypto/algorithm_dispatch.h" |
| 7 #include "components/webcrypto/algorithms/test_helpers.h" | 11 #include "components/webcrypto/algorithms/test_helpers.h" |
| 8 #include "components/webcrypto/crypto_data.h" | 12 #include "components/webcrypto/crypto_data.h" |
| 9 #include "components/webcrypto/status.h" | 13 #include "components/webcrypto/status.h" |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 14 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 11 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 15 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| 12 | 16 |
| 13 namespace webcrypto { | 17 namespace webcrypto { |
| 14 | 18 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 key, CryptoData(test_cipher_text), &output)); | 70 key, CryptoData(test_cipher_text), &output)); |
| 67 EXPECT_BYTES_EQ(test_plain_text, output); | 71 EXPECT_BYTES_EQ(test_plain_text, output); |
| 68 } | 72 } |
| 69 } | 73 } |
| 70 | 74 |
| 71 // The counter block must be exactly 16 bytes. | 75 // The counter block must be exactly 16 bytes. |
| 72 TEST_F(WebCryptoAesCtrTest, InvalidCounterBlockLength) { | 76 TEST_F(WebCryptoAesCtrTest, InvalidCounterBlockLength) { |
| 73 const unsigned int kBadCounterBlockLengthBytes[] = {0, 15, 17}; | 77 const unsigned int kBadCounterBlockLengthBytes[] = {0, 15, 17}; |
| 74 | 78 |
| 75 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 79 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 76 std::vector<uint8>(16), // 128-bit key of all zeros. | 80 std::vector<uint8_t>(16), // 128-bit key of all zeros. |
| 77 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), | 81 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), |
| 78 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 82 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 79 | 83 |
| 80 std::vector<uint8_t> input(32); | 84 std::vector<uint8_t> input(32); |
| 81 std::vector<uint8_t> output; | 85 std::vector<uint8_t> output; |
| 82 | 86 |
| 83 for (size_t i = 0; i < arraysize(kBadCounterBlockLengthBytes); ++i) { | 87 for (size_t i = 0; i < arraysize(kBadCounterBlockLengthBytes); ++i) { |
| 84 std::vector<uint8_t> bad_counter(kBadCounterBlockLengthBytes[i]); | 88 std::vector<uint8_t> bad_counter(kBadCounterBlockLengthBytes[i]); |
| 85 | 89 |
| 86 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(), | 90 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(), |
| 87 Encrypt(CreateAesCtrAlgorithm(bad_counter, 128), key, | 91 Encrypt(CreateAesCtrAlgorithm(bad_counter, 128), key, |
| 88 CryptoData(input), &output)); | 92 CryptoData(input), &output)); |
| 89 | 93 |
| 90 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(), | 94 EXPECT_EQ(Status::ErrorIncorrectSizeAesCtrCounter(), |
| 91 Decrypt(CreateAesCtrAlgorithm(bad_counter, 128), key, | 95 Decrypt(CreateAesCtrAlgorithm(bad_counter, 128), key, |
| 92 CryptoData(input), &output)); | 96 CryptoData(input), &output)); |
| 93 } | 97 } |
| 94 } | 98 } |
| 95 | 99 |
| 96 // The counter length cannot be less than 1 or greater than 128. | 100 // The counter length cannot be less than 1 or greater than 128. |
| 97 TEST_F(WebCryptoAesCtrTest, InvalidCounterLength) { | 101 TEST_F(WebCryptoAesCtrTest, InvalidCounterLength) { |
| 98 const uint8_t kBadCounterLengthBits[] = {0, 129}; | 102 const uint8_t kBadCounterLengthBits[] = {0, 129}; |
| 99 | 103 |
| 100 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 104 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 101 std::vector<uint8>(16), // 128-bit key of all zeros. | 105 std::vector<uint8_t>(16), // 128-bit key of all zeros. |
| 102 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), | 106 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), |
| 103 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 107 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 104 | 108 |
| 105 std::vector<uint8_t> counter(16); | 109 std::vector<uint8_t> counter(16); |
| 106 std::vector<uint8_t> input(32); | 110 std::vector<uint8_t> input(32); |
| 107 std::vector<uint8_t> output; | 111 std::vector<uint8_t> output; |
| 108 | 112 |
| 109 for (size_t i = 0; i < arraysize(kBadCounterLengthBits); ++i) { | 113 for (size_t i = 0; i < arraysize(kBadCounterLengthBits); ++i) { |
| 110 uint8_t bad_counter_length_bits = kBadCounterLengthBits[i]; | 114 uint8_t bad_counter_length_bits = kBadCounterLengthBits[i]; |
| 111 | 115 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 124 // Wrap-around is allowed, however if the counter repeats itself an error should | 128 // Wrap-around is allowed, however if the counter repeats itself an error should |
| 125 // be thrown. | 129 // be thrown. |
| 126 // | 130 // |
| 127 // Using a 4-bit counter it is possible to encrypt 16 blocks. However the 17th | 131 // Using a 4-bit counter it is possible to encrypt 16 blocks. However the 17th |
| 128 // block would end up wrapping back to the starting value. | 132 // block would end up wrapping back to the starting value. |
| 129 TEST_F(WebCryptoAesCtrTest, OverflowAndRepeatCounter) { | 133 TEST_F(WebCryptoAesCtrTest, OverflowAndRepeatCounter) { |
| 130 const uint8_t kCounterLengthBits = 4; | 134 const uint8_t kCounterLengthBits = 4; |
| 131 const uint8_t kStartCounter[] = {0, 1, 15}; | 135 const uint8_t kStartCounter[] = {0, 1, 15}; |
| 132 | 136 |
| 133 blink::WebCryptoKey key = ImportSecretKeyFromRaw( | 137 blink::WebCryptoKey key = ImportSecretKeyFromRaw( |
| 134 std::vector<uint8>(16), // 128-bit key of all zeros. | 138 std::vector<uint8_t>(16), // 128-bit key of all zeros. |
| 135 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), | 139 CreateAlgorithm(blink::WebCryptoAlgorithmIdAesCtr), |
| 136 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); | 140 blink::WebCryptoKeyUsageEncrypt | blink::WebCryptoKeyUsageDecrypt); |
| 137 | 141 |
| 138 std::vector<uint8_t> buffer(272); | 142 std::vector<uint8_t> buffer(272); |
| 139 | 143 |
| 140 // 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes | 144 // 16 and 17 AES blocks worth of data respectively (AES blocks are 16 bytes |
| 141 // long). | 145 // long). |
| 142 CryptoData input_16(buffer.data(), 256); | 146 CryptoData input_16(buffer.data(), 256); |
| 143 CryptoData input_17(buffer.data(), 272); | 147 CryptoData input_17(buffer.data(), 272); |
| 144 | 148 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 160 input_17, &output)); | 164 input_17, &output)); |
| 161 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(), | 165 EXPECT_EQ(Status::ErrorAesCtrInputTooLongCounterRepeated(), |
| 162 Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, | 166 Decrypt(CreateAesCtrAlgorithm(counter, kCounterLengthBits), key, |
| 163 input_17, &output)); | 167 input_17, &output)); |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 | 170 |
| 167 } // namespace | 171 } // namespace |
| 168 | 172 |
| 169 } // namespace webcrypto | 173 } // namespace webcrypto |
| OLD | NEW |