pic32mz-crypt.h
1 /* pic32mz-crypt.h
2  *
3  * Copyright (C) 2006-2020 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 
23 #ifndef PIC32MZ_CRYPT_H
24 #define PIC32MZ_CRYPT_H
25 
26 #ifdef __cplusplus
27  extern "C" {
28 #endif
29 
30 #include <wolfssl/wolfcrypt/settings.h>
31 
32 #ifdef WOLFSSL_MICROCHIP_PIC32MZ
33 
34 #ifndef MICROCHIP_PIC32
35  #define MICROCHIP_PIC32
36 #endif
37 
38 /* If algos aren't enabled then turn off */
39 #ifdef WOLFSSL_PIC32MZ_HASH
40  #if defined(NO_MD5) && defined(NO_SHA) && defined(NO_SHA256)
41  #undef WOLFSSL_PIC32MZ_HASH
42  #endif
43 #endif
44 
45 #ifdef WOLFSSL_PIC32MZ_CRYPT
46  #if defined(NO_AES) && defined(NO_DES3)
47  #undef WOLFSSL_PIC32MZ_CRYPT
48  #endif
49 #endif
50 
51 /* Enables support for large hashing */
52 /* requires exclusive access to crypto hardware done at application layer */
53 #define WOLFSSL_PIC32MZ_LARGE_HASH
54 
55 #include <xc.h>
56 #include <sys/endian.h>
57 #include <sys/kmem.h>
58 
59 
60 /* PIC32 Crypto Structures */
61 typedef struct saCtrl {
62  unsigned int CRYPTOALGO : 4;
63  unsigned int MULTITASK : 3;
64  unsigned int KEYSIZE : 2;
65  unsigned int ENCTYPE : 1;
66  unsigned int ALGO : 7;
67  unsigned int : 3;
68  unsigned int FLAGS : 1;
69  unsigned int FB : 1;
70  unsigned int LOADIV : 1;
71  unsigned int LNC : 1;
72  unsigned int IRFLAG : 1;
73  unsigned int ICVONLY : 1;
74  unsigned int OR_EN : 1;
75  unsigned int NO_RX : 1;
76  unsigned int : 1;
77  unsigned int VERIFY : 1;
78  unsigned int : 2;
79 } saCtrl;
80 
81 typedef struct securityAssociation {
82  saCtrl SA_CTRL;
83  unsigned int SA_AUTHKEY[8];
84  unsigned int SA_ENCKEY[8];
85  unsigned int SA_AUTHIV[8];
86  unsigned int SA_ENCIV[4];
88 
89 typedef struct bdCtrl {
90  unsigned int BUFLEN : 16;
91  unsigned int CBD_INT_EN : 1;
92  unsigned int PKT_INT_EN : 1;
93  unsigned int LIFM : 1;
94  unsigned int LAST_BD : 1;
95  unsigned int CRDMA_EN : 1;
96  unsigned int UPD_RES : 1;
97  unsigned int SA_FETCH_EN : 1;
98  unsigned int SEC_CODE : 8;
99  volatile unsigned int DESC_EN : 1;
100 } bdCtrl;
101 
102 typedef struct bufferDescriptor {
103  bdCtrl BD_CTRL;
104  unsigned int SA_ADDR;
105  unsigned int SRCADDR;
106  unsigned int DSTADDR;
107  unsigned int NXTPTR;
108  unsigned int UPDPTR;
109  unsigned int MSGLEN;
110  unsigned int ENCOFF;
112 
113 
114 /* Cache Updates Struct */
115 typedef struct hashUpdCache {
116  unsigned char* buf;
117  unsigned int bufLen;
118  unsigned int updLen;
119  int isCopy;
120 #ifdef WOLFSSL_PIC32MZ_LARGE_HASH
121  unsigned int finalLen;
122 #endif
123 } hashUpdCache;
124 
125 
126 /* Direction */
127 #define PIC32_ENCRYPTION 0b1
128 #define PIC32_DECRYPTION 0b0
129 
130 /* Algorithm */
131 #define PIC32_ALGO_HMAC1 0b01000000
132 #define PIC32_ALGO_SHA256 0b00100000
133 #define PIC32_ALGO_SHA1 0b00010000
134 #define PIC32_ALGO_MD5 0b00001000
135 
136 #define PIC32_ALGO_AES 0b00000100
137 #define PIC32_ALGO_TDES 0b00000010
138 #define PIC32_ALGO_DES 0b00000001
139 
140 /* Crypto Algo */
141 /* AES */
142 #define PIC32_CRYPTOALGO_AES_GCM 0b1110
143 #define PIC32_CRYPTOALGO_RCTR 0b1101
144 #define PIC32_CRYPTOALGO_RCBC_MAC 0b1100
145 #define PIC32_CRYPTOALGO_ROFB 0b1011
146 #define PIC32_CRYPTOALGO_RCFB 0b1010
147 #define PIC32_CRYPTOALGO_RCBC 0b1001
148 #define PIC32_CRYPTOALGO_RECB 0b1000
149 /* Triple-DES */
150 #define PIC32_CRYPTOALGO_TOFB 0b0111
151 #define PIC32_CRYPTOALGO_TCFB 0b0110
152 #define PIC32_CRYPTOALGO_TCBC 0b0101
153 #define PIC32_CRYPTOALGO_TECB 0b0100
154 /* DES */
155 #define PIC32_CRYPTOALGO_OFB 0b0011
156 #define PIC32_CRYPTOALGO_CFB 0b0010
157 #define PIC32_CRYPTOALGO_CBC 0b0001
158 #define PIC32_CRYPTOALGO_ECB 0b0000
159 
160 /* Key Size */
161 #define PIC32_KEYSIZE_256 0b10
162 #define PIC32_KEYSIZE_192 0b01
163 #define PIC32_KEYSIZE_128 0b00
164 
165 /* PIC32 Minimum Buffer/Block Sizes */
166 #define PIC32_BLOCKSIZE_HASH 64
167 #define PIC32_BLOCKSIZE_HMAC PIC32_BLOCKSIZE_HASH
168 #define PIC32_BLOCKSIZE_MD5 PIC32_BLOCKSIZE_HASH
169 #define PIC32_BLOCKSIZE_SHA1 PIC32_BLOCKSIZE_HASH
170 #define PIC32_BLOCKSIZE_SHA256 PIC32_BLOCKSIZE_HASH
171 #define PIC32_BLOCKSIZE_AES 16
172 #define PIC32_BLOCKSIZE_TDES 24
173 #define PIC32_BLOCKSIZE_DES 8
174 
175 #define PIC32_DIGEST_SIZE 32
176 
177 
178 /* Helper Macros */
179 #define PIC32MZ_IF_RAM(addr) (KVA_TO_PA(addr) < 0x1D000000)
180 
181 /* If EF part then Crypto engine supports swapping output bytes */
182 #define PIC32_NO_OUT_SWAP ((__PIC32_FEATURE_SET0 == 'E') && \
183  (__PIC32_FEATURE_SET1 == 'C'))
184 
185 
186 #ifndef NO_AES
187 int wc_Pic32AesCrypt(word32 *key, int keyLen, word32 *iv, int ivLen,
188  byte* out, const byte* in, word32 sz,
189  int dir, int algo, int cryptoalgo);
190 #endif
191 #ifndef NO_DES3
192 int wc_Pic32DesCrypt(word32 *key, int keyLen, word32 *iv, int ivLen,
193  byte* out, const byte* in, word32 sz,
194  int dir, int algo, int cryptoalgo);
195 #endif
196 
197 #ifdef WOLFSSL_PIC32MZ_HASH
198 #define WOLFSSL_NO_HASH_RAW
199 
200 int wc_Pic32Hash(const byte* in, int inLen, word32* out, int outLen, int algo);
201 int wc_Pic32HashCopy(hashUpdCache* src, hashUpdCache* dst);
202 
203 #ifndef NO_MD5
204 struct wc_Md5;
205 void wc_Md5Pic32Free(struct wc_Md5* md5);
206 #endif
207 #ifndef NO_SHA
208 struct wc_Sha;
209 void wc_ShaPic32Free(struct wc_Sha* sha);
210 #endif
211 
212 #ifndef NO_SHA256
213 struct wc_Sha256;
214 void wc_Sha256Pic32Free(struct wc_Sha256* sha256);
215 #endif
216 #endif /* WOLFSSL_PIC32MZ_HASH */
217 
218 #endif /* WOLFSSL_MICROCHIP_PIC32MZ */
219 
220 #ifdef __cplusplus
221  } /* extern "C" */
222 #endif
223 
224 #endif /* PIC32MZ_CRYPT_H */
Definition: sha256.h:132
Definition: md5.h:79
Definition: pic32mz-crypt.h:115
Definition: pic32mz-crypt.h:102
Definition: pic32mz-crypt.h:81
Definition: wolfcaam_sha.h:68
Definition: pic32mz-crypt.h:89