• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 package com.google.crypto.tink.streamingaead;
18 
19 import com.google.crypto.tink.StreamingAead;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.nio.channels.ReadableByteChannel;
24 import java.nio.channels.SeekableByteChannel;
25 import java.nio.channels.WritableByteChannel;
26 import java.security.GeneralSecurityException;
27 import java.util.List;
28 
29 /**
30  * A helper for creating {@link StreamingAead}-primitives from keysets.
31  */
32 final class StreamingAeadHelper implements StreamingAead {
33   private final List<StreamingAead> allPrimitives;
34   private final StreamingAead primary;
35 
36   /**
37    * Creates a helper that uses the provided primitives for encryption
38    * and decryption of data provided via channels.
39    * For encryption it uses the primitive corresponding to the primary key.
40    * For decryption it uses an enabled primitive that matches the given ciphertext.
41    */
StreamingAeadHelper(List<StreamingAead> allPrimitives, StreamingAead primary)42   public StreamingAeadHelper(List<StreamingAead> allPrimitives, StreamingAead primary)
43       throws GeneralSecurityException {
44     this.allPrimitives = allPrimitives;
45     this.primary = primary;
46   }
47 
48   @Override
newEncryptingChannel( WritableByteChannel ciphertextDestination, byte[] associatedData)49   public WritableByteChannel newEncryptingChannel(
50       WritableByteChannel ciphertextDestination, byte[] associatedData)
51       throws GeneralSecurityException, IOException {
52     return primary.newEncryptingChannel(ciphertextDestination, associatedData);
53   }
54 
55   @Override
newDecryptingChannel( ReadableByteChannel ciphertextChannel, byte[] associatedData)56   public ReadableByteChannel newDecryptingChannel(
57       ReadableByteChannel ciphertextChannel, byte[] associatedData)
58       throws GeneralSecurityException, IOException {
59     return new ReadableByteChannelDecrypter(allPrimitives, ciphertextChannel, associatedData);
60   }
61 
62   @Override
newSeekableDecryptingChannel( SeekableByteChannel ciphertextChannel, byte[] associatedData)63   public SeekableByteChannel newSeekableDecryptingChannel(
64       SeekableByteChannel ciphertextChannel, byte[] associatedData)
65       throws GeneralSecurityException, IOException {
66     return new SeekableByteChannelDecrypter(allPrimitives, ciphertextChannel, associatedData);
67   }
68 
69   @Override
newDecryptingStream( InputStream ciphertextStream, byte[] associatedData)70   public InputStream newDecryptingStream(
71       InputStream ciphertextStream,
72       byte[] associatedData)
73       throws GeneralSecurityException, IOException {
74     return new InputStreamDecrypter(allPrimitives, ciphertextStream, associatedData);
75   }
76 
77   @Override
newEncryptingStream( OutputStream ciphertext, byte[] associatedData)78   public OutputStream newEncryptingStream(
79       OutputStream ciphertext, byte[] associatedData)
80       throws GeneralSecurityException, IOException {
81     return primary.newEncryptingStream(ciphertext, associatedData);
82   }
83 }
84