summaryrefslogtreecommitdiff
path: root/mobicore/common/MobiCore/inc/mcSo.h
blob: 848d6d46198af8faf7bf2320c7e30fb972f0d257 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
 * @defgroup MC_DATA_TYPES MobiCore generic data types
 *
 * @addtogroup MC_SO mcSo - Secure objects definitions.
 *
 * Copyright (c) 2013 TRUSTONIC LIMITED
 * All rights reserved
 *
 * The present software is the confidential and proprietary information of
 * TRUSTONIC LIMITED. You shall not disclose the present software and shall
 * use it only in accordance with the terms of the license agreement you
 * entered into with TRUSTONIC LIMITED. This software may be subject to
 * export or import laws in certain countries.
 *
 * @ingroup  MC_DATA_TYPES
 * @{
 *
 */

#ifndef MC_SO_H_
#define MC_SO_H_

#include "mcUuid.h"
#include "mcSpid.h"
#include "mcRootid.h"

#define SO_USE_VERSION_22 1

#define SO_VERSION_MAJOR   2
#define SO_VERSION_MINOR   2

#define MC_ENUM_32BIT_SPACER           ((int32_t)-1)

/** Secure object type. */
typedef enum {
    /** Regular secure object. */
    MC_SO_TYPE_REGULAR = 0x00000001,
    /** Dummy to ensure that enum is 32 bit wide. */
    MC_SO_TYPE_DUMMY = MC_ENUM_32BIT_SPACER,
} mcSoType_t;


/** Secure object context.
 * A context defines which key to use to encrypt/decrypt a secure object.
 */
typedef enum {
    /** Trustlet context. */
    MC_SO_CONTEXT_TLT = 0x00000001,
     /** Service provider context. */
    MC_SO_CONTEXT_SP = 0x00000002,
     /** Device context. */
    MC_SO_CONTEXT_DEVICE = 0x00000003,
    /** Dummy to ensure that enum is 32 bit wide. */
    MC_SO_CONTEXT_DUMMY = MC_ENUM_32BIT_SPACER,
} mcSoContext_t;

/** Secure object lifetime.
 * A lifetime defines how long a secure object is valid.
 */
typedef enum {
    /** SO does not expire. */
    MC_SO_LIFETIME_PERMANENT = 0x00000000,
    /** SO expires on reboot (coldboot). */
    MC_SO_LIFETIME_POWERCYCLE = 0x00000001,
    /** SO expires when Trustlet is closed. */
    MC_SO_LIFETIME_SESSION = 0x00000002,
    /** Dummy to ensure that enum is 32 bit wide. */
    MC_SO_LIFETIME_DUMMY = MC_ENUM_32BIT_SPACER,
} mcSoLifeTime_t;

/** Service provider Trustlet id.
 * The combination of service provider id and Trustlet UUID forms a unique
 * Trustlet identifier.
 */
typedef struct {
    /** Service provider id. */
    mcSpid_t spid;
    /** Trustlet UUID. */
    mcUuid_t uuid;
} tlApiSpTrustletId_t;

/** Service provider Trustlet id with specific RootId.
 */
typedef struct {
    /** Service provider id. */
    mcSpid_t spid;
    /** Trustlet UUID. */
    mcUuid_t uuid;
    /** Trustlet RootId. */
    mcRootid_t rootid;
} tlApiSpTrustletIdEx_t;

/** Secure object header v2.2.
 * A secure object header introduces a secure object.
 * Layout of a secure object:
 * <pre>
 * <code>
 *
 *     +--------+------------------+------------------+--------+--------+
 *     | Header |   plain-data     |  encrypted-data  |  hash  | random |
 *     +--------+------------------+------------------+--------+--------+
 *
 *     /--------/---- plainLen ----/-- encryptedLen --/-- 32 --/-- 16 --/
 *
 *     /----------------- toBeHashedLen --------------/
 *
 *                                 /-- toBeEncryptedLen --/
 *
 *     /--------------------------- totalSoSize ------------------------/
 *
 * </code>
 * </pre>
 */

/** Secure object header v2.1.
 * A secure object header introduces a secure object.
 * Layout of a secure object:
 * <pre>
 * <code>
 *
 *     +--------+------------------+------------------+--------+--------+---------+
 *     | Header |   plain-data     |  encrypted-data  |  hash  | random | padding |
 *     +--------+------------------+------------------+--------+--------+---------+
 *
 *     /--------/---- plainLen ----/-- encryptedLen --/-- 24 --/--- 9 --/- 0..15 -/
 *
 *     /----------------- toBeHashedLen --------------/
 *
 *                                 /-- toBeEncryptedLen --/
 *
 *     /--------------------------- totalSoSize ----------------------------------/
 *
 * </code>
 * </pre>
 */

/** Secure object header v2.0.
 * A secure object header introduces a secure object.
 * Layout of a secure object:
 * <pre>
 * <code>
 *
 *     +--------+------------------+------------------+--------+---------+
 *     | Header |   plain-data     |  encrypted-data  |  hash  | padding |
 *     +--------+------------------+------------------+--------+---------+
 *
 *     /--------/---- plainLen ----/-- encryptedLen --/-- 32 --/- 1..16 -/
 *
 *     /----------------- toBeHashedLen --------------/
 *
 *                                 /---------- toBeEncryptedLen ---------/
 *
 *     /--------------------------- totalSoSize -------------------------/
 *
 * </code>
 * </pre>
 */
typedef struct {
    /** Type of secure object. */
    uint32_t type;
    /** Secure object version. */
    uint32_t version;
    /** Secure object context. */
    mcSoContext_t context;
    /** Secure object lifetime. */
    mcSoLifeTime_t lifetime;
    /** Producer Trustlet id. */
    tlApiSpTrustletId_t producer;
    /** Length of unencrypted user data (after the header). */
    uint32_t plainLen;
    /** Length of encrypted user data (after unencrypted data, excl. checksum
     * and excl. padding bytes). */
    uint32_t encryptedLen;
} mcSoHeader_t;

/** Maximum size of the payload (plain length + encrypted length) of a secure object. */
#define MC_SO_PAYLOAD_MAX_SIZE      1000000

/** Block size of encryption algorithm used for secure objects. */
#define MC_SO_ENCRYPT_BLOCK_SIZE    16

/** Maximum number of ISO padding bytes. */
#define MC_SO_MAX_PADDING_SIZE (MC_SO_ENCRYPT_BLOCK_SIZE)

/** Size of hash used for secure objects v2. */
#define MC_SO_HASH_SIZE             32

/** Size of hash used for secure object v2.1. */
#define MC_SO21_HASH_SIZE            24
/** Size of random used for secure objects v2.1. */
#define MC_SO21_RND_SIZE             9

/** Size of hash used for secure object v2.2. */
#define MC_SO22_HASH_SIZE            32
/** Size of random used for secure objects v2.2. */
#define MC_SO22_RND_SIZE             16

/** Hash size for current generated wrapping */
#define MC_SO2X_HASH_SIZE MC_SO22_HASH_SIZE
/** Random size for current generated wrapping */
#define MC_SO2X_RND_SIZE MC_SO22_RND_SIZE

#define MC_SO_ENCRYPT_PADDED_SIZE_F21(netsize) ( (netsize) + \
    MC_SO_MAX_PADDING_SIZE - (netsize) % MC_SO_MAX_PADDING_SIZE )

#if SO_USE_VERSION_22
    // No encryption padding at all.
#else
    /** Calculates gross size of cryptogram within secure object including ISO padding bytes. */
    #define MC_SO_ENCRYPT_PADDED_SIZE(netsize) MC_SO_ENCRYPT_PADDED_SIZE_F21(netsize)
#endif


/** Calculates the total size of a secure object.
 * @param plainLen Length of plain text part within secure object.
 * @param encryptedLen Length of encrypted part within secure object (excl.
 * hash, padding).
 * @return Total (gross) size of the secure object or 0 if given parameters are
 * illegal or would lead to a secure object of invalid size.
 */
#define MC_SO_SIZE_F22(plainLen, encryptedLen) ( \
    ((plainLen) + (encryptedLen) < (encryptedLen) || (plainLen) + (encryptedLen) > MC_SO_PAYLOAD_MAX_SIZE) ? 0 : \
            sizeof(mcSoHeader_t) + (plainLen) + (encryptedLen) +MC_SO22_HASH_SIZE +MC_SO22_RND_SIZE \
    )
#define MC_SO_SIZE_F21(plainLen, encryptedLen) ( \
    ((plainLen) + (encryptedLen) < (encryptedLen) || (plainLen) + (encryptedLen) > MC_SO_PAYLOAD_MAX_SIZE) ? 0 : \
            sizeof(mcSoHeader_t) +(plainLen) +MC_SO_ENCRYPT_PADDED_SIZE_F21((encryptedLen) +MC_SO_HASH_SIZE) \
)

#define MC_SO_SIZE(plainLen, encryptedLen) MC_SO_SIZE_F22(plainLen, encryptedLen)

#endif // MC_SO_H_

/** @} */