aboutsummaryrefslogtreecommitdiff
path: root/ready_se/google/keymint/KM300/Applet/src/com/android/javacard/keymaster/KMMap.java
blob: 24182041c4fe2dd61e9c8a40d43486dfd824af34 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright(C) 2021 The Android Open Source Project
 *
 * Licensed 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 com.android.javacard.keymaster;

import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

/**
 * KMMap represents an array of a KMType key and a KMType value. Map is the sequence of pairs. Each
 * pair is one or more sub-types of KMType. The KMMap instance maps to the CBOR type map. KMMap is a
 * KMType and it further extends the value field in TLV_HEADER as MAP_HEADER struct{ short
 * subType;short length;} followed by a sequence of pairs. Each pair contains a key and a value as
 * short pointers to KMType instances.
 */
public class KMMap extends KMType {

  public static final short ANY_MAP_LENGTH = 0x1000;
  private static final byte MAP_HEADER_SIZE = 4;
  private static KMMap prototype;

  private KMMap() {}

  private static KMMap proto(short ptr) {
    if (prototype == null) {
      prototype = new KMMap();
    }
    instanceTable[KM_MAP_OFFSET] = ptr;
    return prototype;
  }

  public static short exp() {
    short ptr = instance(MAP_TYPE, (short) MAP_HEADER_SIZE);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), (short) 0);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), ANY_MAP_LENGTH);
    return ptr;
  }

  public static short instance(short length) {
    short ptr = KMType.instance(MAP_TYPE, (short) (MAP_HEADER_SIZE + (length * 4)));
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), (short) 0);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), length);
    return ptr;
  }

  public static short instance(short length, byte type) {
    short ptr = instance(length);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), type);
    return ptr;
  }

  public static KMMap cast(short ptr) {
    if (heap[ptr] != MAP_TYPE) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    return proto(ptr);
  }

  public void add(short index, short keyPtr, short valPtr) {
    short len = length();
    if (index >= len) {
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }
    short keyIndex =
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index * 4));
    Util.setShort(heap, keyIndex, keyPtr);
    Util.setShort(heap, (short) (keyIndex + 2), valPtr);
  }

  public short getKey(short index) {
    short len = length();
    if (index >= len) {
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }
    return Util.getShort(
        heap,
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index * 4)));
  }

  public short getKeyValue(short index) {
    short len = length();
    if (index >= len) {
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }
    return Util.getShort(
        heap,
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index * 4 + 2)));
  }

  public void swap(short index1, short index2) {
    short len = length();
    if (index1 >= len || index2 >= len) {
      ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    }
    // Swap keys
    short indexPtr1 =
        Util.getShort(
            heap,
            (short)
                (instanceTable[KM_MAP_OFFSET]
                    + TLV_HEADER_SIZE
                    + MAP_HEADER_SIZE
                    + (short) (index1 * 4)));
    short indexPtr2 =
        Util.getShort(
            heap,
            (short)
                (instanceTable[KM_MAP_OFFSET]
                    + TLV_HEADER_SIZE
                    + MAP_HEADER_SIZE
                    + (short) (index2 * 4)));
    Util.setShort(
        heap,
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index1 * 4)),
        indexPtr2);
    Util.setShort(
        heap,
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index2 * 4)),
        indexPtr1);

    // Swap Values
    indexPtr1 =
        Util.getShort(
            heap,
            (short)
                (instanceTable[KM_MAP_OFFSET]
                    + TLV_HEADER_SIZE
                    + MAP_HEADER_SIZE
                    + (short) (index1 * 4 + 2)));
    indexPtr2 =
        Util.getShort(
            heap,
            (short)
                (instanceTable[KM_MAP_OFFSET]
                    + TLV_HEADER_SIZE
                    + MAP_HEADER_SIZE
                    + (short) (index2 * 4 + 2)));
    Util.setShort(
        heap,
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index1 * 4 + 2)),
        indexPtr2);
    Util.setShort(
        heap,
        (short)
            (instanceTable[KM_MAP_OFFSET]
                + TLV_HEADER_SIZE
                + MAP_HEADER_SIZE
                + (short) (index2 * 4 + 2)),
        indexPtr1);
  }

  public void canonicalize() {
    KMCoseMap.canonicalize(instanceTable[KM_MAP_OFFSET], length());
  }

  public short containedType() {
    return Util.getShort(heap, (short) (instanceTable[KM_MAP_OFFSET] + TLV_HEADER_SIZE));
  }

  public short getStartOff() {
    return (short) (instanceTable[KM_MAP_OFFSET] + TLV_HEADER_SIZE + MAP_HEADER_SIZE);
  }

  public short length() {
    return Util.getShort(heap, (short) (instanceTable[KM_MAP_OFFSET] + TLV_HEADER_SIZE + 2));
  }

  public byte[] getBuffer() {
    return heap;
  }
}