-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathCryptoUtils.java
More file actions
166 lines (152 loc) · 5.92 KB
/
CryptoUtils.java
File metadata and controls
166 lines (152 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.orc.impl;
import org.apache.hadoop.conf.Configuration;
import org.apache.orc.InMemoryKeystore;
import org.apache.orc.OrcConf;
import org.apache.orc.OrcProto;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.ServiceLoader;
import java.util.function.Consumer;
/**
* This class has routines to work with encryption within ORC files.
* @since 1.6.0
*/
public class CryptoUtils {
private static final int COLUMN_ID_LENGTH = 3;
private static final int KIND_LENGTH = 2;
private static final int STRIPE_ID_LENGTH = 3;
private static final int MIN_COUNT_BYTES = 8;
static final int MAX_COLUMN = 0xffffff;
static final int MAX_KIND = 0xffff;
static final int MAX_STRIPE = 0xffffff;
/**
* Update the unique IV for each stream within a single key.
* The top bytes are set with the column, stream kind, and stripe id and the
* lower 8 bytes are always 0.
* @param name the stream name
* @param stripeId the stripe id
*/
public static Consumer<byte[]> modifyIvForStream(StreamName name,
long stripeId) {
return modifyIvForStream(name.getColumn(), name.getKind(), stripeId);
}
/**
* Update the unique IV for each stream within a single key.
* The top bytes are set with the column, stream kind, and stripe id and the
* lower 8 bytes are always 0.
* @param columnId the column id
* @param kind the stream kind
* @param stripeId the stripe id
*/
public static Consumer<byte[]> modifyIvForStream(int columnId,
OrcProto.Stream.Kind kind,
long stripeId) {
if (columnId < 0 || columnId > MAX_COLUMN) {
throw new IllegalArgumentException("ORC encryption is limited to " +
MAX_COLUMN + " columns. Value = " + columnId);
}
int k = kind.getNumber();
if (k < 0 || k > MAX_KIND) {
throw new IllegalArgumentException("ORC encryption is limited to " +
MAX_KIND + " stream kinds. Value = " + k);
}
return (byte[] iv) -> {
// the rest of the iv is used for counting within the stream
if (iv.length - (COLUMN_ID_LENGTH + KIND_LENGTH + STRIPE_ID_LENGTH) < MIN_COUNT_BYTES) {
throw new IllegalArgumentException("Not enough space in the iv for the count");
}
iv[0] = (byte) (columnId >> 16);
iv[1] = (byte) (columnId >> 8);
iv[2] = (byte) columnId;
iv[COLUMN_ID_LENGTH] = (byte) (k >> 8);
iv[COLUMN_ID_LENGTH + 1] = (byte) (k);
modifyIvForStripe(stripeId).accept(iv);
};
}
/**
* Modify the IV for the given stripe id and make sure the low bytes are
* set to 0.
* @param stripeId the stripe id
*/
public static Consumer<byte[]> modifyIvForStripe(long stripeId) {
if (stripeId < 1 || stripeId > MAX_STRIPE) {
throw new IllegalArgumentException("ORC encryption is limited to " +
MAX_STRIPE + " stripes. Value = " +
stripeId);
}
return (byte[] iv) -> {
iv[COLUMN_ID_LENGTH + KIND_LENGTH] = (byte) (stripeId >> 16);
iv[COLUMN_ID_LENGTH + KIND_LENGTH + 1] = (byte) (stripeId >> 8);
iv[COLUMN_ID_LENGTH + KIND_LENGTH + 2] = (byte) stripeId;
clearCounter(iv);
};
}
/**
* Clear the counter part of the IV.
* @param iv the IV to modify
*/
public static void clearCounter(byte[] iv) {
for(int i= COLUMN_ID_LENGTH + KIND_LENGTH + STRIPE_ID_LENGTH; i < iv.length; ++i) {
iv[i] = 0;
}
}
/** A cache for the key providers */
private static final Map<String, KeyProvider> keyProviderCache = new HashMap<>();
/**
* Create a KeyProvider.
* It will cache the result, so that only one provider of each kind will be
* created.
*
* @param random the random generator to use
* @return the new KeyProvider
*/
public static KeyProvider getKeyProvider(Configuration conf,
Random random) throws IOException {
String kind = OrcConf.KEY_PROVIDER.getString(conf);
String cacheKey = kind + "." + random.getClass().getName();
KeyProvider result = keyProviderCache.get(cacheKey);
if (result == null) {
ServiceLoader<KeyProvider.Factory> loader = ServiceLoader.load(KeyProvider.Factory.class);
for (KeyProvider.Factory factory : loader) {
result = factory.create(kind, conf, random);
if (result != null) {
keyProviderCache.put(cacheKey, result);
break;
}
}
}
return result;
}
public static class HadoopKeyProviderFactory implements KeyProvider.Factory {
@Override
public KeyProvider create(String kind,
Configuration conf,
Random random) throws IOException {
if ("hadoop".equals(kind)) {
return HadoopShimsFactory.get().getHadoopKeyProvider(conf, random);
} else if ("memory".equals(kind)) {
return new InMemoryKeystore(random);
}
return null;
}
}
}