1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 /*
3  * Copyright (C) 2017 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.org.conscrypt;
19 
20 import java.io.IOException;
21 import java.security.AlgorithmParametersSpi;
22 import java.security.spec.AlgorithmParameterSpec;
23 import java.security.spec.InvalidParameterSpecException;
24 import javax.crypto.spec.IvParameterSpec;
25 
26 /**
27  * An implementation of {@link java.security.AlgorithmParameters} that contains only an IV.  The
28  * supported encoding formats are ASN.1 (primary) and RAW.
29  * @hide This class is not part of the Android public SDK API
30  */
31 @libcore.api.IntraCoreApi
32 @Internal
33 public class IvParameters extends AlgorithmParametersSpi {
34     private byte[] iv;
35 
36     @libcore.api.IntraCoreApi
IvParameters()37     public IvParameters() {}
38 
39     @Override
engineInit(AlgorithmParameterSpec algorithmParameterSpec)40     protected void engineInit(AlgorithmParameterSpec algorithmParameterSpec)
41             throws InvalidParameterSpecException {
42         if (!(algorithmParameterSpec instanceof IvParameterSpec)) {
43             throw new InvalidParameterSpecException("Only IvParameterSpec is supported");
44         }
45         iv = ((IvParameterSpec) algorithmParameterSpec).getIV().clone();
46     }
47 
48     @Override
engineInit(byte[] bytes)49     protected void engineInit(byte[] bytes) throws IOException {
50         long readRef = 0;
51         try {
52             readRef = NativeCrypto.asn1_read_init(bytes);
53             byte[] newIv = NativeCrypto.asn1_read_octetstring(readRef);
54             if (!NativeCrypto.asn1_read_is_empty(readRef)) {
55                 throw new IOException("Error reading ASN.1 encoding");
56             }
57             this.iv = newIv;
58         } finally {
59             NativeCrypto.asn1_read_free(readRef);
60         }
61     }
62 
63     @Override
engineInit(byte[] bytes, String format)64     protected void engineInit(byte[] bytes, String format) throws IOException {
65         if (format == null || format.equals("ASN.1")) {
66             engineInit(bytes);
67         } else if (format.equals("RAW")) {
68             iv = bytes.clone();
69         } else {
70             throw new IOException("Unsupported format: " + format);
71         }
72     }
73 
74     @Override
75     @SuppressWarnings("unchecked")
engineGetParameterSpec(Class<T> aClass)76     protected <T extends AlgorithmParameterSpec> T engineGetParameterSpec(Class<T> aClass)
77             throws InvalidParameterSpecException {
78         if (aClass != IvParameterSpec.class) {
79             throw new InvalidParameterSpecException(
80                     "Incompatible AlgorithmParametersSpec class: " + aClass);
81         }
82         return (T) new IvParameterSpec(iv);
83     }
84 
85     @Override
engineGetEncoded()86     protected byte[] engineGetEncoded() throws IOException {
87         long cbbRef = 0;
88         try {
89             cbbRef = NativeCrypto.asn1_write_init();
90             NativeCrypto.asn1_write_octetstring(cbbRef, this.iv);
91             return NativeCrypto.asn1_write_finish(cbbRef);
92         } catch (IOException e) {
93             NativeCrypto.asn1_write_cleanup(cbbRef);
94             throw e;
95         } finally {
96             NativeCrypto.asn1_write_free(cbbRef);
97         }
98     }
99 
100     @Override
engineGetEncoded(String format)101     protected byte[] engineGetEncoded(String format) throws IOException {
102         if (format == null || format.equals("ASN.1")) {
103             return engineGetEncoded();
104         } else if (format.equals("RAW")) {
105             return iv.clone();
106         } else {
107             throw new IOException("Unsupported format: " + format);
108         }
109     }
110 
111     @Override
engineToString()112     protected String engineToString() {
113         return "Conscrypt IV AlgorithmParameters";
114     }
115 
116     /**
117      * @hide This class is not part of the Android public SDK API
118      */
119     @libcore.api.IntraCoreApi
120     public static class AES extends IvParameters {
121         @libcore.api.IntraCoreApi
AES()122         public AES() {}
123     }
124     /**
125      * @hide This class is not part of the Android public SDK API
126      */
127     @libcore.api.IntraCoreApi
128     public static class DESEDE extends IvParameters {
129         @libcore.api.IntraCoreApi
DESEDE()130         public DESEDE() {}
131     }
132     /**
133      * @hide This class is not part of the Android public SDK API
134      */
135     @libcore.api.IntraCoreApi
136     public static class ChaCha20 extends IvParameters {
137         @libcore.api.IntraCoreApi
ChaCha20()138         public ChaCha20() {}
139     }
140 }
141