BOLOS TEE
bolos_crypto_common.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * BOLOS TEE
4 * (c) 2016 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 };
166 typedef enum bls_md_e bls_md_t;
167 
168 #define BLS_RIPEMD160_SIZE 20
169 #define BLS_SHA256_SIZE 32
170 #define BLS_SHA512_SIZE 64
171 
172 #define BLS_HASH_MAX_BLOCK_COUNT 65535
173 
174 
175 
176 /* ####################################################################### */
177 /* CIPHER/SIGNATURE */
178 /* ####################################################################### */
179 
180 
181 /* ======================================================================= */
182 /* DES */
183 /* ======================================================================= */
184 
185 
186 #define BLS_DES_BLOCK_SIZE 8
187 
188 
189 /* ======================================================================= */
190 /* AES */
191 /* ======================================================================= */
192 
193 
194 #define BLS_AES_BLOCK_SIZE 16
195 
196 
197 /* ======================================================================= */
198 /* RSA */
199 /* ======================================================================= */
200 
201 
203  uint8_t WIDE *Q;
204  uint32_t QSize;
205  uint8_t WIDE *P;
206  uint32_t PSize;
207  uint8_t WIDE *DQ;
208  uint32_t DQSize;
209  uint8_t WIDE *DP;
210  uint32_t DPSize;
211  uint8_t WIDE *QInv;
212  uint32_t QInvSize;
213 };
214 typedef struct bls_rsa_crt_s bls_rsa_crt_t;
215 
217  uint8_t *publicExponent;
218  uint32_t publicExponentSize;
219  uint8_t *modulus;
220  uint32_t modulusSize;
221  uint8_t *privateExponent;
222  uint32_t privateExponentSize;
223  bls_rsa_crt_t *privateCrt;
224 };
226 
227 
228 /* ======================================================================= */
229 /* ECDSA */
230 /* ======================================================================= */
231 
232 enum bls_curve_e {
233  BLS_CURVE_NONE,
234  BLS_CURVE_256K1,
235  BLS_CURVE_256R1,
236  BLS_CURVE_192K1,
237  BLS_CURVE_192R1,
238 };
239 typedef enum bls_curve_e bls_curve_t;
240 
241 
242 
243 /* ======================================================================= */
244 /* CRC */
245 /* ======================================================================= */
246 
247 #define BLS_CRC16_INIT 0xFFFF
248 
249 
250 #endif //__BOLOS_CRYPTO_COMMON_H__
251 
Definition: bolos_crypto_common.h:216
Definition: bolos_crypto_common.h:202