BOLOS TEE
bolos_crypto_common.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * BOLOS TEE
4 * (c) 2016, 2017 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18 
29 #ifndef __BOLOS_CRYPTO_COMMON_H__
30 
31 #define __BOLOS_CRYPTO_COMMON_H__
32 
33 
34 /* ####################################################################### */
35 /* COMMON */
36 /* ####################################################################### */
37 /*
38  * Crypto mode encoding:
39  * =====================
40  *
41  * size:
42  * -----
43  * int, a least 16 bits
44  *
45  * encoding:
46  * ---------
47  * | bit pos | H constant | meanings
48  * ---------------------------------------------------
49  * | 0 | BLS_LAST | last block
50  * | | |
51  *
52  * | 2:1 | BLS_ENCRYPT |
53  * | | BLS_DECRYPT |
54  * | | BLS_SIGN |
55  * | | BLS_VERIFY |
56  *
57  * | 5:3 | BLS_PAD_NONE |
58  * | | BLS_PAD_ISO9797M1 |
59  * | | BLS_PAD_ISO9797M2 |
60  * | | BLS_PAD_PKCS1_1o5 |
61  * | | BLS_PAD_PSS |
62  *
63  * | 7:6 | BLS_CHAIN_ECB |
64  * | | BLS_CHAIN_CBC |
65  *
66  * | 9:8 | BLS_RND_TRNG |
67  * | | BLS_RND_RFC6979 |
68  *
69  * | 11:10 | BLS_ECDH_POINT | share full point
70  * | | BLS_ECDH_X | share only x coordinate
71  * | | BLS_ECDH_HASHED | return a sha256 of the x coordinate
72  *
73  * | 12 | BLS_DISCARD | do not reinitialize context on BLS_LAST when supported
74 
75  * | 14:13 | BLS_AES_PAD_CTR |
76  * | | BLS_AES_PAD_CFB |
77  * | | BLS_AES_PAD_OFB |
78  *
79  * | 16:15 | RFU |
80  */
81 
85 #define BLS_LAST (1<<0)
86 
90 #define BLS_SIG_MODE (1<<1)
91 
95 #define BLS_MASK_SIGCRYPT (3<<1)
96 #define BLS_ENCRYPT (2<<1)
97 #define BLS_DECRYPT (0<<1)
98 #define BLS_SIGN (BLS_SIG_MODE|BLS_ENCRYPT)
99 #define BLS_VERIFY (BLS_SIG_MODE|BLS_DECRYPT)
100 
101 
105 #define BLS_MASK_PAD (7<<3)
106 #define BLS_PAD_NONE (0<<3)
107 #define BLS_PAD_ISO9797M1 (1<<3)
108 #define BLS_PAD_ISO9797M2 (2<<3)
109 #define BLS_PAD_PKCS1_1o5 (3<<3)
110 #define BLS_PAD_PSS BLS_PAD_ISO9797M1
111 #define BLS_PAD_PKCS5 BLS_PAD_PKCS1_1o5
112 
116 #define BLS_MASK_CHAIN (3<<6)
117 #define BLS_CHAIN_ECB (0<<6)
118 #define BLS_CHAIN_CBC (1<<6)
119 
123 #define BLS_MASK_RND (3<<8)
124 #define BLS_RND_PRNG (1<<8)
125 #define BLS_RND_TRNG (2<<8)
126 #define BLS_RND_RFC6979 (3<<8)
127 
131 #define BLS_MASK_ECDH (3<<10)
132 #define BLS_ECDH_POINT (1<<10)
133 #define BLS_ECDH_X (2<<10)
134 #define BLS_ECDH_HASHED (3<<10)
135 
139 #define BLS_DISCARD (1<<12)
140 
144 #define BLS_MASK_AES_CHAIN (3<<13)
145 #define BLS_AES_CHAIN_CTR (1<<13)
146 #define BLS_AES_CHAIN_CFB (2<<13)
147 #define BLS_AES_CHAIN_OFB (3<<13)
148 
149 /* ####################################################################### */
150 /* HASH/HMAC */
151 /* ####################################################################### */
152 
153 /* ======================================================================= */
154 /* HASH */
155 /* ======================================================================= */
156 
157 enum bls_md_e {
158  BLS_NONE,
159  BLS_RIPEMD160,
160  BLS_SHA224,
161  BLS_SHA256,
162  BLS_SHA384,
163  BLS_SHA512,
164  BLS_SHA1,
165  BLS_SHA3,
166  BLS_KECCAK
167 };
168 typedef enum bls_md_e bls_md_t;
169 
170 #define BLS_RIPEMD160_SIZE 20
171 #define BLS_SHA256_SIZE 32
172 #define BLS_SHA512_SIZE 64
173 
174 #define BLS_HASH_MAX_BLOCK_COUNT 65535
175 
176 
177 
178 /* ####################################################################### */
179 /* CIPHER/SIGNATURE */
180 /* ####################################################################### */
181 
182 
183 /* ======================================================================= */
184 /* DES */
185 /* ======================================================================= */
186 
187 
188 #define BLS_DES_BLOCK_SIZE 8
189 
190 
191 /* ======================================================================= */
192 /* AES */
193 /* ======================================================================= */
194 
195 
196 #define BLS_AES_BLOCK_SIZE 16
197 
198 
199 /* ======================================================================= */
200 /* RSA */
201 /* ======================================================================= */
202 
203 
205  uint8_t WIDE *Q;
206  uint32_t QSize;
207  uint8_t WIDE *P;
208  uint32_t PSize;
209  uint8_t WIDE *DQ;
210  uint32_t DQSize;
211  uint8_t WIDE *DP;
212  uint32_t DPSize;
213  uint8_t WIDE *QInv;
214  uint32_t QInvSize;
215 };
216 typedef struct bls_rsa_crt_s bls_rsa_crt_t;
217 
219  uint8_t *publicExponent;
220  uint32_t publicExponentSize;
221  uint8_t *modulus;
222  uint32_t modulusSize;
223  uint8_t *privateExponent;
224  uint32_t privateExponentSize;
225  bls_rsa_crt_t *privateCrt;
226 };
227 typedef struct bls_rsa_keypair_data_s bls_rsa_keypair_data_t;
228 
229 
230 /* ======================================================================= */
231 /* ECDSA */
232 /* ======================================================================= */
233 
234 enum bls_curve_e {
235  BLS_CURVE_NONE,
236  BLS_CURVE_256K1,
237  BLS_CURVE_256R1,
238  BLS_CURVE_192K1,
239  BLS_CURVE_192R1,
240 };
241 typedef enum bls_curve_e bls_curve_t;
242 
243 
244 
245 /* ======================================================================= */
246 /* CRC */
247 /* ======================================================================= */
248 
249 #define BLS_CRC16_INIT 0xFFFF
250 
251 
252 #endif //__BOLOS_CRYPTO_COMMON_H__
253 
Definition: bolos_crypto_common.h:218
Definition: bolos_crypto_common.h:204