2 * Copyright (c) 2003-2012 Broadcom Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _NLM_HAL_CRYPTO_H_
32 #define _NLM_HAL_CRYPTO_H_
34 #define SAE_CFG_REG 0x00
35 #define SAE_ENG_SEL_0 0x01
36 #define SAE_ENG_SEL_1 0x02
37 #define SAE_ENG_SEL_2 0x03
38 #define SAE_ENG_SEL_3 0x04
39 #define SAE_ENG_SEL_4 0x05
40 #define SAE_ENG_SEL_5 0x06
41 #define SAE_ENG_SEL_6 0x07
42 #define SAE_ENG_SEL_7 0x08
44 #define RSA_CFG_REG 0x00
45 #define RSA_ENG_SEL_0 0x01
46 #define RSA_ENG_SEL_1 0x02
47 #define RSA_ENG_SEL_2 0x03
49 #define nlm_read_sec_reg(b, r) nlm_read_reg(b, r)
50 #define nlm_write_sec_reg(b, r, v) nlm_write_reg(b, r, v)
51 #define nlm_get_sec_pcibase(node) nlm_pcicfg_base(XLP_IO_SEC_OFFSET(node))
52 #define nlm_get_sec_regbase(node) \
53 (nlm_get_sec_pcibase(node) + XLP_IO_PCI_HDRSZ)
55 #define nlm_read_rsa_reg(b, r) nlm_read_reg(b, r)
56 #define nlm_write_rsa_reg(b, r, v) nlm_write_reg(b, r, v)
57 #define nlm_get_rsa_pcibase(node) nlm_pcicfg_base(XLP_IO_RSA_OFFSET(node))
58 #define nlm_get_rsa_regbase(node) \
59 (nlm_get_rsa_pcibase(node) + XLP_IO_PCI_HDRSZ)
61 #define nlm_pcibase_sec(node) nlm_pcicfg_base(XLP_IO_SEC_OFFSET(node))
62 #define nlm_qidstart_sec(node) nlm_qidstart_kseg(nlm_pcibase_sec(node))
63 #define nlm_qnum_sec(node) nlm_qnum_kseg(nlm_pcibase_sec(node))
66 * Since buffer allocation for crypto at kernel is done as malloc, each
67 * segment size is given as page size which is 4K by default
69 #define NLM_CRYPTO_MAX_SEG_LEN PAGE_SIZE
71 #define MAX_KEY_LEN_IN_DW 20
73 #define left_shift64(x, bitshift, numofbits) \
74 ((uint64_t)(x) << (bitshift))
76 #define left_shift64_mask(x, bitshift, numofbits) \
77 (((uint64_t)(x) & ((1ULL << (numofbits)) - 1)) << (bitshift))
80 * @brief cipher algorithms
83 enum nlm_cipher_algo {
84 NLM_CIPHER_BYPASS = 0,
87 NLM_CIPHER_AES128 = 3,
88 NLM_CIPHER_AES192 = 4,
89 NLM_CIPHER_AES256 = 5,
91 NLM_CIPHER_KASUMI_F8 = 7,
92 NLM_CIPHER_SNOW3G_F8 = 8,
93 NLM_CIPHER_CAMELLIA128 = 9,
94 NLM_CIPHER_CAMELLIA192 = 0xA,
95 NLM_CIPHER_CAMELLIA256 = 0xB,
100 * @brief cipher modes
103 enum nlm_cipher_mode {
104 NLM_CIPHER_MODE_ECB = 0,
105 NLM_CIPHER_MODE_CBC = 1,
106 NLM_CIPHER_MODE_CFB = 2,
107 NLM_CIPHER_MODE_OFB = 3,
108 NLM_CIPHER_MODE_CTR = 4,
109 NLM_CIPHER_MODE_AES_F8 = 5,
110 NLM_CIPHER_MODE_GCM = 6,
111 NLM_CIPHER_MODE_CCM = 7,
112 NLM_CIPHER_MODE_UNDEFINED1 = 8,
113 NLM_CIPHER_MODE_UNDEFINED2 = 9,
114 NLM_CIPHER_MODE_LRW = 0xA,
115 NLM_CIPHER_MODE_XTS = 0xB,
116 NLM_CIPHER_MODE_MAX = 0xC,
120 * @brief hash algorithms
127 NLM_HASH_UNDEFINED = 3,
131 NLM_HASH_KASUMI_F9 = 7,
132 NLM_HASH_SNOW3G_F9 = 8,
133 NLM_HASH_CAMELLIA128 = 9,
134 NLM_HASH_CAMELLIA192 = 0xA,
135 NLM_HASH_CAMELLIA256 = 0xB,
136 NLM_HASH_GHASH = 0xC,
145 NLM_HASH_MODE_SHA1 = 0, /* Only SHA */
146 NLM_HASH_MODE_SHA224 = 1, /* Only SHA */
147 NLM_HASH_MODE_SHA256 = 2, /* Only SHA */
148 NLM_HASH_MODE_SHA384 = 3, /* Only SHA */
149 NLM_HASH_MODE_SHA512 = 4, /* Only SHA */
150 NLM_HASH_MODE_CMAC = 5, /* AES and Camellia */
151 NLM_HASH_MODE_XCBC = 6, /* AES and Camellia */
152 NLM_HASH_MODE_CBC_MAC = 7, /* AES and Camellia */
153 NLM_HASH_MODE_CCM = 8, /* AES */
154 NLM_HASH_MODE_GCM = 9, /* AES */
155 NLM_HASH_MODE_MAX = 0xA,
159 * @brief crypto control descriptor, should be cache aligned
162 struct nlm_crypto_pkt_ctrl {
164 /* combination of cipher and hash keys */
165 uint64_t key[MAX_KEY_LEN_IN_DW];
166 uint32_t cipherkeylen;
172 * @brief crypto packet descriptor, should be cache aligned
175 struct nlm_crypto_pkt_param {
180 uint64_t segment[1][2];
183 static __inline__ uint64_t
184 nlm_crypto_form_rsa_ecc_fmn_entry0(unsigned int l3alloc, unsigned int type,
185 unsigned int func, uint64_t srcaddr)
187 return (left_shift64(l3alloc, 61, 1) |
188 left_shift64(type, 46, 7) |
189 left_shift64(func, 40, 6) |
190 left_shift64(srcaddr, 0, 40));
193 static __inline__ uint64_t
194 nlm_crypto_form_rsa_ecc_fmn_entry1(unsigned int dstclobber,
195 unsigned int l3alloc, unsigned int fbvc, uint64_t dstaddr)
197 return (left_shift64(dstclobber, 62, 1) |
198 left_shift64(l3alloc, 61, 1) |
199 left_shift64(fbvc, 40, 12) |
200 left_shift64(dstaddr, 0, 40));
204 * @brief Generate cypto control descriptor
206 * hmac : 1 for hash with hmac
207 * hashalg, see hash_alg enums
208 * hashmode, see hash_mode enums
209 * cipherhalg, see cipher_alg enums
210 * ciphermode, see cipher_mode enums
211 * arc4_cipherkeylen : length of arc4 cipher key, 0 is interpreted as 32
213 * cfbmask : cipher text for feedback,
214 * 0(1 bit), 1(2 bits), 2(4 bits), 3(8 bits), 4(16bits), 5(32 bits),
215 * 6(64 bits), 7(128 bits)
217 static __inline__ uint64_t
218 nlm_crypto_form_pkt_ctrl_desc(unsigned int hmac, unsigned int hashalg,
219 unsigned int hashmode, unsigned int cipheralg, unsigned int ciphermode,
220 unsigned int arc4_cipherkeylen, unsigned int arc4_keyinit,
221 unsigned int cfbmask)
223 return (left_shift64(hmac, 61, 1) |
224 left_shift64(hashalg, 52, 8) |
225 left_shift64(hashmode, 43, 8) |
226 left_shift64(cipheralg, 34, 8) |
227 left_shift64(ciphermode, 25, 8) |
228 left_shift64(arc4_cipherkeylen, 18, 5) |
229 left_shift64(arc4_keyinit, 17, 1) |
230 left_shift64(cfbmask, 0, 3));
233 * @brief Generate cypto packet descriptor 0
235 * tls : 1 (tls enabled) 0(tls disabled)
236 * hash_source : 1 (encrypted data is sent to the auth engine)
237 * 0 (plain data is sent to the auth engine)
238 * hashout_l3alloc : 1 (auth output is transited through l3 cache)
239 * encrypt : 1 (for encrypt) 0 (for decrypt)
240 * ivlen : iv length in bytes
241 * hashdst_addr : hash out physical address, byte aligned
243 static __inline__ uint64_t
244 nlm_crypto_form_pkt_desc0(unsigned int tls, unsigned int hash_source,
245 unsigned int hashout_l3alloc, unsigned int encrypt, unsigned int ivlen,
246 uint64_t hashdst_addr)
248 return (left_shift64(tls, 63, 1) |
249 left_shift64(hash_source, 62, 1) |
250 left_shift64(hashout_l3alloc, 60, 1) |
251 left_shift64(encrypt, 59, 1) |
252 left_shift64_mask((ivlen - 1), 41, 16) |
253 left_shift64(hashdst_addr, 0, 40));
257 * @brief Generate cypto packet descriptor 1
259 * cipherlen : cipher length in bytes
260 * hashlen : hash length in bytes
262 static __inline__ uint64_t
263 nlm_crypto_form_pkt_desc1(unsigned int cipherlen, unsigned int hashlen)
265 return (left_shift64_mask((cipherlen - 1), 32, 32) |
266 left_shift64_mask((hashlen - 1), 0, 32));
270 * @brief Generate cypto packet descriptor 2
272 * ivoff : iv offset, offset from start of src data addr
273 * ciperbit_cnt : number of valid bits in the last input byte to the cipher,
274 * 0 (8 bits), 1 (1 bit)..7 (7 bits)
275 * cipheroff : cipher offset, offset from start of src data addr
276 * hashbit_cnt : number of valid bits in the last input byte to the auth
277 * 0 (8 bits), 1 (1 bit)..7 (7 bits)
278 * hashclobber : 1 (hash output will be written as multiples of cachelines, no
280 * hashoff : hash offset, offset from start of src data addr
283 static __inline__ uint64_t
284 nlm_crypto_form_pkt_desc2(unsigned int ivoff, unsigned int cipherbit_cnt,
285 unsigned int cipheroff, unsigned int hashbit_cnt, unsigned int hashclobber,
286 unsigned int hashoff)
288 return (left_shift64(ivoff , 45, 16) |
289 left_shift64(cipherbit_cnt, 42, 3) |
290 left_shift64(cipheroff, 22, 16) |
291 left_shift64(hashbit_cnt, 19, 3) |
292 left_shift64(hashclobber, 18, 1) |
293 left_shift64(hashoff, 0, 16));
297 * @brief Generate cypto packet descriptor 3
299 * designer_vc : designer freeback fmn destination id
300 * taglen : length in bits of the tag generated by the auth engine
301 * md5 (128 bits), sha1 (160), sha224 (224), sha384 (384),
302 * sha512 (512), Kasumi (32), snow3g (32), gcm (128)
303 * hmacpad : 1 if hmac padding is already done
305 static __inline__ uint64_t
306 nlm_crypto_form_pkt_desc3(unsigned int designer_vc, unsigned int taglen,
307 unsigned int arc4_state_save_l3, unsigned int arc4_save_state,
308 unsigned int hmacpad)
310 return (left_shift64(designer_vc, 48, 16) |
311 left_shift64(taglen, 11, 16) |
312 left_shift64(arc4_state_save_l3, 8, 1) |
313 left_shift64(arc4_save_state, 6, 1) |
314 left_shift64(hmacpad, 5, 1));
318 * @brief Generate cypto packet descriptor 4
320 * srcfraglen : length of the source fragment(header + data + tail) in bytes
321 * srcfragaddr : physical address of the srouce fragment
323 static __inline__ uint64_t
324 nlm_crypto_form_pkt_desc4(uint64_t srcfraglen,
325 unsigned int srcfragaddr )
327 return (left_shift64_mask((srcfraglen - 1), 48, 16) |
328 left_shift64(srcfragaddr, 0, 40));
332 * @brief Generate cypto packet descriptor 5
334 * dstfraglen : length of the dst fragment(header + data + tail) in bytes
335 * chipherout_l3alloc : 1(cipher output is transited through l3 cache)
336 * cipherclobber : 1 (cipher output will be written as multiples of cachelines,
337 * no read modify write)
338 * chiperdst_addr : physical address of the cipher destination address
340 static __inline__ uint64_t
341 nlm_crypto_form_pkt_desc5(unsigned int dstfraglen,
342 unsigned int cipherout_l3alloc, unsigned int cipherclobber,
343 uint64_t cipherdst_addr)
346 return (left_shift64_mask((dstfraglen - 1), 48, 16) |
347 left_shift64(cipherout_l3alloc, 46, 1) |
348 left_shift64(cipherclobber, 41, 1) |
349 left_shift64(cipherdst_addr, 0, 40));
353 * @brief Generate crypto packet fmn message entry 0
355 * freeback_vc: freeback response destination address
356 * designer_fblen : Designer freeback length, 1 - 4
357 * designerdesc_valid : designer desc valid or not
358 * cipher_keylen : cipher key length in bytes
359 * ctrldesc_addr : physicall address of the control descriptor
361 static __inline__ uint64_t
362 nlm_crypto_form_pkt_fmn_entry0(unsigned int freeback_vc,
363 unsigned int designer_fblen, unsigned int designerdesc_valid,
364 unsigned int cipher_keylen, uint64_t cntldesc_addr)
366 return (left_shift64(freeback_vc, 48, 16) |
367 left_shift64_mask(designer_fblen - 1, 46, 2) |
368 left_shift64(designerdesc_valid, 45, 1) |
369 left_shift64_mask(((cipher_keylen + 7) >> 3), 40, 5) |
370 left_shift64(cntldesc_addr >> 6, 0, 34));
374 * @brief Generate crypto packet fmn message entry 1
376 * arc4load_state : 1 if load state required 0 otherwise
377 * hash_keylen : hash key length in bytes
378 * pktdesc_size : packet descriptor size in bytes
379 * pktdesc_addr : physicall address of the packet descriptor
381 static __inline__ uint64_t
382 nlm_crypto_form_pkt_fmn_entry1(unsigned int arc4load_state,
383 unsigned int hash_keylen, unsigned int pktdesc_size,
384 uint64_t pktdesc_addr)
386 return (left_shift64(arc4load_state, 63, 1) |
387 left_shift64_mask(((hash_keylen + 7) >> 3), 56, 5) |
388 left_shift64_mask(((pktdesc_size >> 4) - 1), 43, 12) |
389 left_shift64(pktdesc_addr >> 6, 0, 34));
392 static __inline__ int
393 nlm_crypto_get_hklen_taglen(enum nlm_hash_algo hashalg,
394 enum nlm_hash_mode hashmode, unsigned int *taglen, unsigned int *hklen)
396 if (hashalg == NLM_HASH_MD5) {
399 } else if (hashalg == NLM_HASH_SHA) {
401 case NLM_HASH_MODE_SHA1:
405 case NLM_HASH_MODE_SHA224:
409 case NLM_HASH_MODE_SHA256:
413 case NLM_HASH_MODE_SHA384:
417 case NLM_HASH_MODE_SHA512:
422 printf("Error : invalid shaid (%s)\n", __func__);
425 } else if (hashalg == NLM_HASH_KASUMI_F9) {
428 } else if (hashalg == NLM_HASH_SNOW3G_F9) {
431 } else if (hashmode == NLM_HASH_MODE_XCBC) {
434 } else if (hashmode == NLM_HASH_MODE_GCM) {
437 } else if (hashalg == NLM_HASH_BYPASS) {
441 printf("Error:Hash alg/mode not found\n");
445 /* TODO : Add remaining cases */
450 * @brief Generate fill cryto control info structure
452 * hmac : 1 for hash with hmac
453 * hashalg: see above, hash_alg enums
454 * hashmode: see above, hash_mode enums
455 * cipherhalg: see above, cipher_alg enums
456 * ciphermode: see above, cipher_mode enums
459 static __inline__ int
460 nlm_crypto_fill_pkt_ctrl(struct nlm_crypto_pkt_ctrl *ctrl, unsigned int hmac,
461 enum nlm_hash_algo hashalg, enum nlm_hash_mode hashmode,
462 enum nlm_cipher_algo cipheralg, enum nlm_cipher_mode ciphermode,
463 unsigned char *cipherkey, unsigned int cipherkeylen,
464 unsigned char *hashkey, unsigned int hashkeylen)
466 unsigned int taglen = 0, hklen = 0;
468 ctrl->desc0 = nlm_crypto_form_pkt_ctrl_desc(hmac, hashalg, hashmode,
469 cipheralg, ciphermode, 0, 0, 0);
470 memset(ctrl->key, 0, sizeof(ctrl->key));
472 memcpy(ctrl->key, cipherkey, cipherkeylen);
474 memcpy((unsigned char *)&ctrl->key[(cipherkeylen + 7) / 8],
475 hashkey, hashkeylen);
476 if (nlm_crypto_get_hklen_taglen(hashalg, hashmode, &taglen, &hklen)
480 ctrl->cipherkeylen = cipherkeylen;
481 ctrl->hashkeylen = hklen;
482 ctrl->taglen = taglen;
484 /* TODO : add the invalid checks and return error */
489 * @brief Top level function for generation pkt desc 0 to 3 for cipher auth
491 * ctrl : pointer to control structure
492 * param : pointer to the param structure
493 * encrypt : 1(for encrypt) 0(for decrypt)
494 * hash_source : 1(encrypted data is sent to the auth engine) 0(plain data is
495 * sent to the auth engine)
496 * ivoff : iv offset from start of data
497 * ivlen : iv length in bytes
498 * hashoff : hash offset from start of data
499 * hashlen : hash length in bytes
500 * hmacpad : hmac padding required or not, 1 if already padded
501 * cipheroff : cipher offset from start of data
502 * cipherlen : cipher length in bytes
503 * hashdst_addr : hash destination physical address
505 static __inline__ void
506 nlm_crypto_fill_cipher_auth_pkt_param(struct nlm_crypto_pkt_ctrl *ctrl,
507 struct nlm_crypto_pkt_param *param, unsigned int encrypt,
508 unsigned int hash_source, unsigned int ivoff, unsigned int ivlen,
509 unsigned int hashoff, unsigned int hashlen, unsigned int hmacpad,
510 unsigned int cipheroff, unsigned int cipherlen, unsigned char *hashdst_addr)
512 param->desc0 = nlm_crypto_form_pkt_desc0(0, hash_source, 1, encrypt,
513 ivlen, vtophys(hashdst_addr));
514 param->desc1 = nlm_crypto_form_pkt_desc1(cipherlen, hashlen);
515 param->desc2 = nlm_crypto_form_pkt_desc2(ivoff, 0, cipheroff, 0, 0,
517 param->desc3 = nlm_crypto_form_pkt_desc3(0, ctrl->taglen, 0, 0,
522 * @brief Top level function for generation pkt desc 0 to 3 for cipher operation
524 * ctrl : pointer to control structure
525 * param : pointer to the param structure
526 * encrypt : 1(for encrypt) 0(for decrypt)
527 * ivoff : iv offset from start of data
528 * ivlen : iv length in bytes
529 * cipheroff : cipher offset from start of data
530 * cipherlen : cipher length in bytes
534 static __inline__ void
535 nlm_crypto_fill_cipher_pkt_param(struct nlm_crypto_pkt_ctrl *ctrl,
536 struct nlm_crypto_pkt_param *param, unsigned int encrypt,
537 unsigned int ivoff, unsigned int ivlen, unsigned int cipheroff,
538 unsigned int cipherlen)
540 param->desc0 = nlm_crypto_form_pkt_desc0(0, 0, 0, encrypt, ivlen, 0ULL);
541 param->desc1 = nlm_crypto_form_pkt_desc1(cipherlen, 1);
542 param->desc2 = nlm_crypto_form_pkt_desc2(ivoff, 0, cipheroff, 0, 0, 0);
543 param->desc3 = nlm_crypto_form_pkt_desc3(0, ctrl->taglen, 0, 0, 0);
547 * @brief Top level function for generation pkt desc 0 to 3 for auth operation
549 * ctrl : pointer to control structure
550 * param : pointer to the param structure
551 * hashoff : hash offset from start of data
552 * hashlen : hash length in bytes
553 * hmacpad : hmac padding required or not, 1 if already padded
554 * hashdst_addr : hash destination physical address
556 static __inline__ void
557 nlm_crypto_fill_auth_pkt_param(struct nlm_crypto_pkt_ctrl *ctrl,
558 struct nlm_crypto_pkt_param *param, unsigned int hashoff,
559 unsigned int hashlen, unsigned int hmacpad, unsigned char *hashdst_addr)
561 param->desc0 = nlm_crypto_form_pkt_desc0(0, 0, 1, 0, 1,
562 vtophys(hashdst_addr));
563 param->desc1 = nlm_crypto_form_pkt_desc1(1, hashlen);
564 param->desc2 = nlm_crypto_form_pkt_desc2(0, 0, 0, 0, 0, hashoff);
565 param->desc3 = nlm_crypto_form_pkt_desc3(0, ctrl->taglen, 0, 0,
569 static __inline__ unsigned int
570 nlm_crypto_fill_src_seg(struct nlm_crypto_pkt_param *param, int seg,
571 unsigned char *input, unsigned int inlen)
573 unsigned off = 0, len = 0;
574 unsigned int remlen = inlen;
576 for (; remlen > 0;) {
577 len = remlen > NLM_CRYPTO_MAX_SEG_LEN ?
578 NLM_CRYPTO_MAX_SEG_LEN : remlen;
579 param->segment[seg][0] = nlm_crypto_form_pkt_desc4(len,
580 vtophys(input + off));
588 static __inline__ unsigned int
589 nlm_crypto_fill_dst_seg(struct nlm_crypto_pkt_param *param,
590 int seg, unsigned char *output, unsigned int outlen)
592 unsigned off = 0, len = 0;
593 unsigned int remlen = outlen;
595 for (; remlen > 0;) {
596 len = remlen > NLM_CRYPTO_MAX_SEG_LEN ?
597 NLM_CRYPTO_MAX_SEG_LEN : remlen;
598 param->segment[seg][1] = nlm_crypto_form_pkt_desc5(len, 1, 0,
599 vtophys(output + off));