1 /* se050_port.h
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 #ifndef _SE050_PORT_H_
23 #define _SE050_PORT_H_
24 
25 #include <wolfssl/wolfcrypt/settings.h>
26 #include <wolfssl/wolfcrypt/visibility.h>
27 #include <wolfssl/wolfcrypt/asn_public.h>
28 
29 #ifdef __GNUC__
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wundef"
SectionChunk(ObjFile * F,const coff_section * H)32 #pragma GCC diagnostic ignored "-Wredundant-decls"
33 #endif
34 
35 #include "fsl_sss_se05x_types.h"
36 #include "fsl_sss_se05x_apis.h"
37 
38 #if (SSS_HAVE_SSS > 1)
39 #include "fsl_sss_api.h"
40 #endif
41 
42 #ifdef WOLFSSL_SE050
43     /* NXP SE050 - Disable SHA512 224/256 support */
44     #ifndef WOLFSSL_NOSHA512_224
45     #define WOLFSSL_NOSHA512_224
46     #endif
47     #ifndef WOLFSSL_NOSHA512_256
48     #define WOLFSSL_NOSHA512_256
49     #endif
50 #endif
51 
52 #ifdef __GNUC__
53 #pragma GCC diagnostic pop
54 #endif
55 
56 
57 /* Default key ID's */
58 #ifndef SE050_KEYSTOREID_AES
59 #define SE050_KEYSTOREID_AES     55
60 #endif
61 #ifndef SE050_KEYSTOREID_ED25519
62 #define SE050_KEYSTOREID_ED25519 58
63 #endif
64 #ifndef SE050_KEYSTOREID_ECC
65 #define SE050_KEYSTOREID_ECC     60
66 #endif
67 #ifndef SE050_KEYSTOREID_CURVE25519
68 #define SE050_KEYSTOREID_CURVE25519 59
69 #endif
70 
71 enum {
72     SSS_BLOCK_SIZE = 512,
73 
74     SSS_MAX_ECC_BITS = 521
75 };
applySecIdx(uint8_t * Off,OutputSection * OS)76 
77 enum SE050KeyType {
78     SE050_ANY_KEY,
79     SE050_AES_KEY,
80     SE050_ECC_KEY,
81     SE050_ED25519_KEY,
82     SE050_CURVE25519_KEY
83 };
84 
85 
applyRelX64(uint8_t * Off,uint16_t Type,OutputSection * OS,uint64_t S,uint64_t P) const86 typedef struct {
87     void*  heap;
88     byte*  msg;
89     word32 used;
90     word32 len;
91 } SE050_HASH_Context;
92 
93 /* Public Functions */
94 WOLFSSL_API int wc_se050_set_config(sss_session_t *pSession,
95     sss_key_store_t *pHostKeyStore, sss_key_store_t *pKeyStore);
96 #ifdef WOLFSSL_SE050_INIT
97 WOLFSSL_API int wc_se050_init(const char* portName);
98 #endif
99 
100 /* Private Functions */
101 WOLFSSL_LOCAL int se050_allocate_key(int keyType);
102 WOLFSSL_LOCAL int se050_get_random_number(uint32_t count, uint8_t* rand_out);
103 
104 WOLFSSL_LOCAL int se050_hash_init(SE050_HASH_Context* se050Ctx, void* heap);
105 WOLFSSL_LOCAL int se050_hash_update(SE050_HASH_Context* se050Ctx,
applyRelX86(uint8_t * Off,uint16_t Type,OutputSection * OS,uint64_t S,uint64_t P) const106     const byte* data, word32 len);
107 WOLFSSL_LOCAL int se050_hash_final(SE050_HASH_Context* se050Ctx, byte* hash,
108     size_t digestLen, word32 algo);
109 WOLFSSL_LOCAL void se050_hash_free(SE050_HASH_Context* se050Ctx);
110 
111 struct Aes;
112 WOLFSSL_LOCAL int se050_aes_set_key(struct Aes* aes, const byte* key,
113     word32 len, const byte* iv, int dir);
114 WOLFSSL_LOCAL int se050_aes_crypt(struct Aes* aes, const byte* in, byte* out,
115     word32 sz, int dir, sss_algorithm_t algorithm);
116 WOLFSSL_LOCAL void se050_aes_free(struct Aes* aes);
117 
118 
119 struct ecc_key;
120 struct WC_RNG;
applyMOV(uint8_t * Off,uint16_t V)121 #ifdef WOLFSSL_SP_MATH
122     struct sp_int;
123     #define MATH_INT_T struct sp_int
124 #elif defined(USE_FAST_MATH)
125     struct fp_int;
readMOV(uint8_t * Off)126     #define MATH_INT_T struct fp_int
127 #else
128     struct mp_int;
129 	#define MATH_INT_T struct mp_int
130 #endif
131 
132 WOLFSSL_LOCAL int se050_ecc_sign_hash_ex(const byte* in, word32 inLen,
applyMOV32T(uint8_t * Off,uint32_t V)133     byte* out, word32 *outLen, struct ecc_key* key);
134 
135 WOLFSSL_LOCAL int se050_ecc_verify_hash_ex(const byte* hash, word32 hashlen,
136     byte* sigRS, word32 sigRSLen, struct ecc_key* key, int* res);
137 
138 WOLFSSL_LOCAL int se050_ecc_create_key(struct ecc_key* key, int curve_id, int keySize);
139 WOLFSSL_LOCAL int se050_ecc_shared_secret(struct ecc_key* private_key,
140     struct ecc_key* public_key, byte* out, word32* outlen);
141 WOLFSSL_LOCAL void se050_ecc_free_key(struct ecc_key* key);
applyBranch20T(uint8_t * Off,int32_t V)142 
143 struct ed25519_key;
144 WOLFSSL_LOCAL int se050_ed25519_create_key(struct ed25519_key* key);
145 WOLFSSL_LOCAL void se050_ed25519_free_key(struct ed25519_key* key);
146 WOLFSSL_LOCAL int se050_ed25519_sign_msg(const byte* in, word32 inLen,
147     byte* out, word32 *outLen, struct ed25519_key* key);
148 
149 WOLFSSL_LOCAL int se050_ed25519_verify_msg(const byte* signature,
150     word32 signatureLen, const byte* msg, word32 msgLen,
151     struct ed25519_key* key, int* res);
applyBranch24T(uint8_t * Off,int32_t V)152 
153 struct curve25519_key;
154 struct ECPoint;
155 WOLFSSL_LOCAL int se050_curve25519_create_key(struct curve25519_key* key, int keySize);
156 WOLFSSL_LOCAL int se050_curve25519_shared_secret(struct curve25519_key* private_key,
157     struct curve25519_key* public_key, struct ECPoint* out);
158 WOLFSSL_LOCAL void se050_curve25519_free_key(struct curve25519_key* key);
159 #endif /* _SE050_PORT_H_ */
160