aboutsummaryrefslogtreecommitdiff
path: root/ready_se/google/keymint/KM300/Applet/src/com/android/javacard/keymaster/KMIntegerArrayTag.java
blob: bf45981d2243eef248268d6818e76a341e0d284f (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
/*
 * Copyright(C) 2020 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;

/**
 * KMIntegerArrayTag represents UINT_REP and ULONG_REP tags specified in keymaster hal specs.
 * struct{byte TAG_TYPE; short length; struct{short UINT_TAG/ULONG_TAG; short tagKey; short arrPtr},
 * where arrPtr is the pointer to KMArray of KMInteger instances.
 */
public class KMIntegerArrayTag extends KMTag {

  private static final short[] tags = {USER_SECURE_ID};
  private static KMIntegerArrayTag prototype;

  private KMIntegerArrayTag() {}

  private static KMIntegerArrayTag proto(short ptr) {
    if (prototype == null) {
      prototype = new KMIntegerArrayTag();
    }
    KMType.instanceTable[KM_INTEGER_ARRAY_TAG_OFFSET] = ptr;
    return prototype;
  }

  public static short exp(short tagType) {
    if (!validateTagType(tagType)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    short arrPtr = KMArray.exp(KMInteger.exp());
    short ptr = instance(TAG_TYPE, (short) 6);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), tagType);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), INVALID_TAG);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 4), arrPtr);
    return ptr;
  }

  public static short instance(short tagType, short key) {
    if (!validateTagType(tagType)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    if (!validateKey(key)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    short arrPtr = KMArray.exp();
    return instance(tagType, key, arrPtr);
  }

  public static short instance(short tagType, short key, short arrObj) {
    if (!validateTagType(tagType)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    if (!validateKey(key)) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    if (heap[arrObj] != ARRAY_TYPE) {
      ISOException.throwIt(ISO7816.SW_DATA_INVALID);
    }
    short ptr = instance(TAG_TYPE, (short) 6);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE), tagType);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 2), key);
    Util.setShort(heap, (short) (ptr + TLV_HEADER_SIZE + 4), arrObj);
    return ptr;
  }

  public static KMIntegerArrayTag cast(short ptr) {
    if (heap[ptr] != TAG_TYPE) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    short tagType = Util.getShort(heap, (short) (ptr + TLV_HEADER_SIZE));
    if (!validateTagType(tagType)) {
      ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
    }
    return proto(ptr);
  }

  private static boolean validateKey(short key) {
    short index = (short) tags.length;
    while (--index >= 0) {
      if (tags[index] == key) {
        return true;
      }
    }
    return false;
  }

  private static boolean validateTagType(short tagType) {
    return (tagType == ULONG_ARRAY_TAG) || (tagType == UINT_ARRAY_TAG);
  }

  public static boolean contains(short tagId, short tagValue, short params) {
    short tag = KMKeyParameters.findTag(KMType.UINT_ARRAY_TAG, tagId, params);
    if (tag != KMType.INVALID_VALUE) {
      short index = 0;
      tag = KMIntegerArrayTag.cast(tag).getValues();
      while (index < KMArray.cast(tag).length()) {
        if (KMInteger.compare(tagValue, KMArray.cast(tag).get(index)) == 0) {
          return true;
        }
        index++;
      }
    }
    return false;
  }

  public short getTagType() {
    return Util.getShort(
        heap, (short) (KMType.instanceTable[KM_INTEGER_ARRAY_TAG_OFFSET] + TLV_HEADER_SIZE));
  }

  public short getKey() {
    return Util.getShort(
        heap, (short) (KMType.instanceTable[KM_INTEGER_ARRAY_TAG_OFFSET] + TLV_HEADER_SIZE + 2));
  }

  public short getValues() {
    return Util.getShort(
        heap, (short) (KMType.instanceTable[KM_INTEGER_ARRAY_TAG_OFFSET] + TLV_HEADER_SIZE + 4));
  }

  public short length() {
    short ptr = getValues();
    return KMArray.cast(ptr).length();
  }

  public void add(short index, short val) {
    KMArray arr = KMArray.cast(getValues());
    arr.add(index, val);
  }

  public short get(short index) {
    KMArray arr = KMArray.cast(getValues());
    return arr.get(index);
  }

  public boolean contains(short tagValue) {
    short index = 0;
    while (index < length()) {
      if (KMInteger.compare(tagValue, get(index)) == 0) {
        return true;
      }
      index++;
    }
    return false;
  }
}