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 <openssl/evp.h> | 5 #include <openssl/evp.h> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "components/webcrypto/algorithms/aes.h" | 10 #include "components/webcrypto/algorithms/aes.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 | 34 |
35 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode, | 35 Status AesGcmEncryptDecrypt(EncryptOrDecrypt mode, |
36 const blink::WebCryptoAlgorithm& algorithm, | 36 const blink::WebCryptoAlgorithm& algorithm, |
37 const blink::WebCryptoKey& key, | 37 const blink::WebCryptoKey& key, |
38 const CryptoData& data, | 38 const CryptoData& data, |
39 std::vector<uint8_t>* buffer) { | 39 std::vector<uint8_t>* buffer) { |
40 const std::vector<uint8_t>& raw_key = GetSymmetricKeyData(key); | 40 const std::vector<uint8_t>& raw_key = GetSymmetricKeyData(key); |
41 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams(); | 41 const blink::WebCryptoAesGcmParams* params = algorithm.aesGcmParams(); |
42 | 42 |
43 unsigned int tag_length_bits; | 43 // The WebCrypto spec defines the default value for the tag length, as well as |
44 Status status = GetAesGcmTagLengthInBits(params, &tag_length_bits); | 44 // the allowed values for tag length. |
45 if (status.IsError()) | 45 unsigned int tag_length_bits = 128; |
46 return status; | 46 if (params->hasTagLengthBits()) { |
| 47 tag_length_bits = params->optionalTagLengthBits(); |
| 48 if (tag_length_bits != 32 && tag_length_bits != 64 && |
| 49 tag_length_bits != 96 && tag_length_bits != 104 && |
| 50 tag_length_bits != 112 && tag_length_bits != 120 && |
| 51 tag_length_bits != 128) { |
| 52 return Status::ErrorInvalidAesGcmTagLength(); |
| 53 } |
| 54 } |
47 | 55 |
48 return AeadEncryptDecrypt( | 56 return AeadEncryptDecrypt( |
49 mode, raw_key, data, tag_length_bits / 8, CryptoData(params->iv()), | 57 mode, raw_key, data, tag_length_bits / 8, CryptoData(params->iv()), |
50 CryptoData(params->optionalAdditionalData()), | 58 CryptoData(params->optionalAdditionalData()), |
51 GetAesGcmAlgorithmFromKeySize(raw_key.size()), buffer); | 59 GetAesGcmAlgorithmFromKeySize(raw_key.size()), buffer); |
52 } | 60 } |
53 | 61 |
54 class AesGcmImplementation : public AesAlgorithm { | 62 class AesGcmImplementation : public AesAlgorithm { |
55 public: | 63 public: |
56 AesGcmImplementation() : AesAlgorithm("GCM") {} | 64 AesGcmImplementation() : AesAlgorithm("GCM") {} |
(...skipping 13 matching lines...) Expand all Loading... |
70 } | 78 } |
71 }; | 79 }; |
72 | 80 |
73 } // namespace | 81 } // namespace |
74 | 82 |
75 scoped_ptr<AlgorithmImplementation> CreateAesGcmImplementation() { | 83 scoped_ptr<AlgorithmImplementation> CreateAesGcmImplementation() { |
76 return make_scoped_ptr(new AesGcmImplementation); | 84 return make_scoped_ptr(new AesGcmImplementation); |
77 } | 85 } |
78 | 86 |
79 } // namespace webcrypto | 87 } // namespace webcrypto |
OLD | NEW |