1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
3 * Copyright 2008-2016 Freescale Semiconductor Inc.
4 * Copyright 2016,2019-2020 NXP
8 #ifndef __DESC_IPSEC_H__
9 #define __DESC_IPSEC_H__
15 * DOC: IPsec Shared Descriptor Constructors
17 * Shared descriptors for IPsec protocol.
20 /* General IPSec ESP encap / decap PDB options */
23 * PDBOPTS_ESP_ESN - Extended sequence included
25 #define PDBOPTS_ESP_ESN 0x10
28 * PDBOPTS_ESP_IPVSN - Process IPv6 header
30 * Valid only for IPsec legacy mode.
32 #define PDBOPTS_ESP_IPVSN 0x02
35 * PDBOPTS_ESP_TUNNEL - Tunnel mode next-header byte
37 * Valid only for IPsec legacy mode.
39 #define PDBOPTS_ESP_TUNNEL 0x01
41 /* IPSec ESP Encap PDB options */
44 * PDBOPTS_ESP_UPDATE_CSUM - Update ip header checksum
46 * Valid only for IPsec legacy mode.
48 #define PDBOPTS_ESP_UPDATE_CSUM 0x80
51 * PDBOPTS_ESP_DIFFSERV - Copy TOS/TC from inner iphdr
53 * Valid only for IPsec legacy mode.
55 #define PDBOPTS_ESP_DIFFSERV 0x40
58 * PDBOPTS_ESP_IVSRC - IV comes from internal random gen
60 #define PDBOPTS_ESP_IVSRC 0x20
63 * PDBOPTS_ESP_IPHDRSRC - IP header comes from PDB
65 * Valid only for IPsec legacy mode.
67 #define PDBOPTS_ESP_IPHDRSRC 0x08
70 * PDBOPTS_ESP_INCIPHDR - Prepend IP header to output frame
72 * Valid only for IPsec legacy mode.
74 #define PDBOPTS_ESP_INCIPHDR 0x04
77 * PDBOPTS_ESP_OIHI_MASK - Mask for Outer IP Header Included
79 * Valid only for IPsec new mode.
81 #define PDBOPTS_ESP_OIHI_MASK 0x0c
84 * PDBOPTS_ESP_OIHI_PDB_INL - Prepend IP header to output frame from PDB (where
87 * Valid only for IPsec new mode.
89 #define PDBOPTS_ESP_OIHI_PDB_INL 0x0c
92 * PDBOPTS_ESP_OIHI_PDB_REF - Prepend IP header to output frame from PDB
93 * (referenced by pointer).
95 * Vlid only for IPsec new mode.
97 #define PDBOPTS_ESP_OIHI_PDB_REF 0x08
100 * PDBOPTS_ESP_OIHI_IF - Prepend IP header to output frame from input frame
102 * Valid only for IPsec new mode.
104 #define PDBOPTS_ESP_OIHI_IF 0x04
107 * PDBOPTS_ESP_NAT - Enable RFC 3948 UDP-encapsulated-ESP
109 * Valid only for IPsec new mode.
111 #define PDBOPTS_ESP_NAT 0x02
114 * PDBOPTS_ESP_NUC - Enable NAT UDP Checksum
116 * Valid only for IPsec new mode.
118 #define PDBOPTS_ESP_NUC 0x01
120 /* IPSec ESP Decap PDB options */
123 * PDBOPTS_ESP_ARS_MASK_ERA10 - antireplay window mask
126 #define PDBOPTS_ESP_ARS_MASK_ERA10 0xc8
129 * PDBOPTS_ESP_ARS_MASK - antireplay window mask
132 #define PDBOPTS_ESP_ARS_MASK 0xc0
135 * PDBOPTS_ESP_ARSNONE - No antireplay window
137 #define PDBOPTS_ESP_ARSNONE 0x00
140 * PDBOPTS_ESP_ARS64 - 64-entry antireplay window
142 #define PDBOPTS_ESP_ARS64 0xc0
145 * PDBOPTS_ESP_ARS128 - 128-entry antireplay window
147 * Valid only for IPsec new mode.
149 #define PDBOPTS_ESP_ARS128 0x80
152 * PDBOPTS_ESP_ARS256 - 256-entry antireplay window
154 * Valid only for IPsec new mode.
156 #define PDBOPTS_ESP_ARS256 0x08
159 * PDBOPTS_ESP_ARS512 - 512-entry antireplay window
161 * Valid only for IPsec new mode.
163 #define PDBOPTS_ESP_ARS512 0x48
166 * PDBOPTS_ESP_ARS1024 - 1024-entry antireplay window
168 * Valid only for IPsec new mode.
170 #define PDBOPTS_ESP_ARS1024 0x88
173 * PDBOPTS_ESP_ARS32 - 32-entry antireplay window
175 #define PDBOPTS_ESP_ARS32 0x40
178 * PDBOPTS_ESP_VERIFY_CSUM - Validate ip header checksum
180 * Valid only for IPsec legacy mode.
182 #define PDBOPTS_ESP_VERIFY_CSUM 0x20
185 * PDBOPTS_ESP_TECN - Implement RRFC6040 ECN tunneling from outer header to
188 * Valid only for IPsec new mode.
190 #define PDBOPTS_ESP_TECN 0x20
193 * PDBOPTS_ESP_OUTFMT - Output only decapsulation
195 * Valid only for IPsec legacy mode.
197 #define PDBOPTS_ESP_OUTFMT 0x08
200 * PDBOPTS_ESP_AOFL - Adjust out frame len
202 * Valid only for IPsec legacy mode and for SEC >= 5.3.
204 #define PDBOPTS_ESP_AOFL 0x04
207 * PDBOPTS_ESP_ETU - EtherType Update
209 * Add corresponding ethertype (0x0800 for IPv4, 0x86dd for IPv6) in the output
211 * Valid only for IPsec new mode.
213 #define PDBOPTS_ESP_ETU 0x01
215 #define PDBHMO_ESP_DECAP_SHIFT 28
216 #define PDBHMO_ESP_ENCAP_SHIFT 28
217 #define PDBNH_ESP_ENCAP_SHIFT 16
218 #define PDBNH_ESP_ENCAP_MASK (0xff << PDBNH_ESP_ENCAP_SHIFT)
219 #define PDBHDRLEN_ESP_DECAP_SHIFT 16
220 #define PDBHDRLEN_MASK (0x0fff << PDBHDRLEN_ESP_DECAP_SHIFT)
221 #define PDB_NH_OFFSET_SHIFT 8
222 #define PDB_NH_OFFSET_MASK (0xff << PDB_NH_OFFSET_SHIFT)
225 * PDBHMO_ESP_DECAP_DTTL - IPsec ESP decrement TTL (IPv4) / Hop limit (IPv6)
228 #define PDBHMO_ESP_DECAP_DTTL (0x02 << PDBHMO_ESP_DECAP_SHIFT)
231 * PDBHMO_ESP_ENCAP_DTTL - IPsec ESP increment TTL (IPv4) / Hop limit (IPv6)
234 #define PDBHMO_ESP_ENCAP_DTTL (0x02 << PDBHMO_ESP_ENCAP_SHIFT)
237 * PDBHMO_ESP_DIFFSERV - (Decap) DiffServ Copy - Copy the IPv4 TOS or IPv6
238 * Traffic Class byte from the outer IP header to the
241 #define PDBHMO_ESP_DIFFSERV (0x01 << PDBHMO_ESP_DECAP_SHIFT)
244 * PDBHMO_ESP_SNR - (Encap) - Sequence Number Rollover control
246 * Configures behaviour in case of SN / ESN rollover:
247 * error if SNR = 1, rollover allowed if SNR = 0.
248 * Valid only for IPsec new mode.
250 #define PDBHMO_ESP_SNR (0x01 << PDBHMO_ESP_ENCAP_SHIFT)
253 * PDBHMO_ESP_DFBIT - (Encap) Copy DF bit - if an IPv4 tunnel mode outer IP
254 * header is coming from the PDB, copy the DF bit from the
255 * inner IP header to the outer IP header.
257 #define PDBHMO_ESP_DFBIT (0x04 << PDBHMO_ESP_ENCAP_SHIFT)
260 * PDBHMO_ESP_DFV - (Decap) - DF bit value
262 * If ODF = 1, DF bit in output frame is replaced by DFV.
263 * Valid only from SEC Era 5 onwards.
265 #define PDBHMO_ESP_DFV (0x04 << PDBHMO_ESP_DECAP_SHIFT)
268 * PDBHMO_ESP_ODF - (Decap) Override DF bit in IPv4 header of decapsulated
271 * If ODF = 1, DF is replaced with the value of DFV bit.
272 * Valid only from SEC Era 5 onwards.
274 #define PDBHMO_ESP_ODF (0x08 << PDBHMO_ESP_DECAP_SHIFT)
277 * struct ipsec_encap_cbc - PDB part for IPsec CBC encapsulation
278 * @iv: 16-byte array initialization vector
280 struct ipsec_encap_cbc {
286 * struct ipsec_encap_ctr - PDB part for IPsec CTR encapsulation
287 * @ctr_nonce: 4-byte nonce
288 * @ctr_initial: initial count constant
289 * @iv: initialization vector
291 struct ipsec_encap_ctr {
293 uint32_t ctr_initial;
298 * struct ipsec_encap_ccm - PDB part for IPsec CCM encapsulation
299 * @salt: 3-byte array salt (lower 24 bits)
300 * @ccm_opt: CCM algorithm options - MSB-LSB description:
301 * b0_flags (8b) - CCM B0; use 0x5B for 8-byte ICV, 0x6B for 12-byte ICV,
302 * 0x7B for 16-byte ICV (cf. RFC4309, RFC3610)
303 * ctr_flags (8b) - counter flags; constant equal to 0x3
304 * ctr_initial (16b) - initial count constant
305 * @iv: initialization vector
307 struct ipsec_encap_ccm {
314 * struct ipsec_encap_gcm - PDB part for IPsec GCM encapsulation
315 * @salt: 3-byte array salt (lower 24 bits)
316 * @rsvd: reserved, do not use
317 * @iv: initialization vector
319 struct ipsec_encap_gcm {
326 * struct ipsec_encap_pdb - PDB for IPsec encapsulation
327 * @options: MSB-LSB description (both for legacy and new modes)
328 * hmo (header manipulation options) - 4b
330 * next header (legacy) / reserved (new) - 8b
331 * next header offset (legacy) / AOIPHO (actual outer IP header offset) - 8b
332 * option flags (depend on selected algorithm) - 8b
333 * @seq_num_ext_hi: (optional) IPsec Extended Sequence Number (ESN)
334 * @seq_num: IPsec sequence number
335 * @spi: IPsec SPI (Security Parameters Index)
336 * @ip_hdr_len: optional IP Header length (in bytes)
338 * Opt. IP Hdr Len - 16b
339 * @ip_hdr: optional IP Header content (only for IPsec legacy mode)
341 struct ipsec_encap_pdb {
343 uint32_t seq_num_ext_hi;
346 struct ipsec_encap_cbc cbc;
347 struct ipsec_encap_ctr ctr;
348 struct ipsec_encap_ccm ccm;
349 struct ipsec_encap_gcm gcm;
356 static inline unsigned int
357 __rta_copy_ipsec_encap_pdb(struct program *program,
358 struct ipsec_encap_pdb *pdb,
361 unsigned int start_pc = program->current_pc;
363 __rta_out32(program, pdb->options);
364 __rta_out32(program, pdb->seq_num_ext_hi);
365 __rta_out32(program, pdb->seq_num);
367 switch (algtype & OP_PCL_IPSEC_CIPHER_MASK) {
368 case OP_PCL_IPSEC_DES_IV64:
369 case OP_PCL_IPSEC_DES:
370 case OP_PCL_IPSEC_3DES:
371 case OP_PCL_IPSEC_AES_CBC:
372 case OP_PCL_IPSEC_NULL:
373 rta_copy_data(program, pdb->cbc.iv, sizeof(pdb->cbc.iv));
376 case OP_PCL_IPSEC_AES_CTR:
377 rta_copy_data(program, (uint8_t *)&pdb->ctr.ctr_nonce, 4);
378 __rta_out32(program, pdb->ctr.ctr_initial);
379 rta_copy_data(program, pdb->ctr.iv, sizeof(pdb->ctr.iv));
382 case OP_PCL_IPSEC_AES_CCM8:
383 case OP_PCL_IPSEC_AES_CCM12:
384 case OP_PCL_IPSEC_AES_CCM16:
385 rta_copy_data(program, pdb->ccm.salt, sizeof(pdb->ccm.salt));
386 __rta_out32(program, pdb->ccm.ccm_opt);
387 __rta_out64(program, true, pdb->ccm.iv);
390 case OP_PCL_IPSEC_AES_GCM8:
391 case OP_PCL_IPSEC_AES_GCM12:
392 case OP_PCL_IPSEC_AES_GCM16:
393 case OP_PCL_IPSEC_AES_NULL_WITH_GMAC:
394 rta_copy_data(program, pdb->gcm.salt, sizeof(pdb->gcm.salt));
395 __rta_out32(program, pdb->gcm.rsvd);
396 __rta_out64(program, true, pdb->gcm.iv);
400 __rta_out32(program, pdb->spi);
401 __rta_out32(program, pdb->ip_hdr_len);
407 * struct ipsec_decap_cbc - PDB part for IPsec CBC decapsulation
408 * @rsvd: reserved, do not use
410 struct ipsec_decap_cbc {
415 * struct ipsec_decap_ctr - PDB part for IPsec CTR decapsulation
416 * @ctr_nonce: 4-byte nonce
417 * @ctr_initial: initial count constant
419 struct ipsec_decap_ctr {
421 uint32_t ctr_initial;
425 * struct ipsec_decap_ccm - PDB part for IPsec CCM decapsulation
426 * @salt: 3-byte salt (lower 24 bits)
427 * @ccm_opt: CCM algorithm options - MSB-LSB description:
428 * b0_flags (8b) - CCM B0; use 0x5B for 8-byte ICV, 0x6B for 12-byte ICV,
429 * 0x7B for 16-byte ICV (cf. RFC4309, RFC3610)
430 * ctr_flags (8b) - counter flags; constant equal to 0x3
431 * ctr_initial (16b) - initial count constant
433 struct ipsec_decap_ccm {
439 * struct ipsec_decap_gcm - PDB part for IPsec GCN decapsulation
441 * @rsvd: reserved, do not use
443 struct ipsec_decap_gcm {
449 * struct ipsec_decap_pdb - PDB for IPsec decapsulation
450 * @options: MSB-LSB description (both for legacy and new modes)
451 * hmo (header manipulation options) - 4b
452 * IP header length - 12b
453 * next header offset (legacy) / AOIPHO (actual outer IP header offset) - 8b
454 * option flags (depend on selected algorithm) - 8b
455 * @seq_num_ext_hi: (optional) IPsec Extended Sequence Number (ESN)
456 * @seq_num: IPsec sequence number
457 * @anti_replay: Anti-replay window; size depends on ARS (option flags);
458 * format must be Big Endian, irrespective of platform
460 struct ipsec_decap_pdb {
463 struct ipsec_decap_cbc cbc;
464 struct ipsec_decap_ctr ctr;
465 struct ipsec_decap_ccm ccm;
466 struct ipsec_decap_gcm gcm;
468 uint32_t seq_num_ext_hi;
470 uint32_t anti_replay[32];
473 static inline unsigned int
474 __rta_copy_ipsec_decap_pdb(struct program *program,
475 struct ipsec_decap_pdb *pdb,
478 unsigned int start_pc = program->current_pc;
482 __rta_out32(program, pdb->options);
484 switch (algtype & OP_PCL_IPSEC_CIPHER_MASK) {
485 case OP_PCL_IPSEC_DES_IV64:
486 case OP_PCL_IPSEC_DES:
487 case OP_PCL_IPSEC_3DES:
488 case OP_PCL_IPSEC_AES_CBC:
489 case OP_PCL_IPSEC_NULL:
490 __rta_out32(program, pdb->cbc.rsvd[0]);
491 __rta_out32(program, pdb->cbc.rsvd[1]);
494 case OP_PCL_IPSEC_AES_CTR:
495 rta_copy_data(program, (uint8_t *)&pdb->ctr.ctr_nonce, 4);
496 __rta_out32(program, pdb->ctr.ctr_initial);
499 case OP_PCL_IPSEC_AES_CCM8:
500 case OP_PCL_IPSEC_AES_CCM12:
501 case OP_PCL_IPSEC_AES_CCM16:
502 rta_copy_data(program, pdb->ccm.salt, sizeof(pdb->ccm.salt));
503 __rta_out32(program, pdb->ccm.ccm_opt);
506 case OP_PCL_IPSEC_AES_GCM8:
507 case OP_PCL_IPSEC_AES_GCM12:
508 case OP_PCL_IPSEC_AES_GCM16:
509 case OP_PCL_IPSEC_AES_NULL_WITH_GMAC:
510 rta_copy_data(program, pdb->gcm.salt, sizeof(pdb->gcm.salt));
511 __rta_out32(program, pdb->gcm.rsvd);
515 __rta_out32(program, pdb->seq_num_ext_hi);
516 __rta_out32(program, pdb->seq_num);
518 if (rta_sec_era < RTA_SEC_ERA_10)
519 mask = PDBOPTS_ESP_ARS_MASK;
521 mask = PDBOPTS_ESP_ARS_MASK_ERA10;
522 switch (pdb->options & mask) {
523 case PDBOPTS_ESP_ARS1024:
526 case PDBOPTS_ESP_ARS512:
529 case PDBOPTS_ESP_ARS256:
532 case PDBOPTS_ESP_ARS128:
535 case PDBOPTS_ESP_ARS64:
538 case PDBOPTS_ESP_ARS32:
541 case PDBOPTS_ESP_ARSNONE:
547 for (i = 0; i < ars; i++)
548 __rta_out_be32(program, pdb->anti_replay[i]);
554 * enum ipsec_icv_size - Type selectors for icv size in IPsec protocol
555 * @IPSEC_ICV_MD5_SIZE: full-length MD5 ICV
556 * @IPSEC_ICV_MD5_TRUNC_SIZE: truncated MD5 ICV
558 enum ipsec_icv_size {
559 IPSEC_ICV_MD5_SIZE = 16,
560 IPSEC_ICV_MD5_TRUNC_SIZE = 12
564 * IPSec ESP Datapath Protocol Override Register (DPOVRD)
565 * IPSEC_N_* defines are for IPsec new mode.
569 * IPSEC_DPOVRD_USE - DPOVRD will override values specified in the PDB
571 #define IPSEC_DPOVRD_USE BIT(31)
574 * IPSEC_DPOVRD_ECN_SHIFT - Explicit Congestion Notification
576 * If set, MSB of the 4 bits indicates that the 2 LSBs will replace the ECN bits
579 #define IPSEC_DPOVRD_ECN_SHIFT 24
582 * IPSEC_DPOVRD_ECN_MASK - See IPSEC_DPOVRD_ECN_SHIFT
584 #define IPSEC_DPOVRD_ECN_MASK (0xf << IPSEC_ENCAP_DPOVRD_ECN_SHIFT)
587 * IPSEC_DPOVRD_IP_HDR_LEN_SHIFT - The length (in bytes) of the portion of the
588 * IP header that is not encrypted
590 #define IPSEC_DPOVRD_IP_HDR_LEN_SHIFT 16
593 * IPSEC_DPOVRD_IP_HDR_LEN_MASK - See IPSEC_DPOVRD_IP_HDR_LEN_SHIFT
595 #define IPSEC_DPOVRD_IP_HDR_LEN_MASK (0xff << IPSEC_DPOVRD_IP_HDR_LEN_SHIFT)
598 * IPSEC_DPOVRD_NH_OFFSET_SHIFT - The location of the next header field within
599 * the IP header of the transport mode packet
602 * ESP_Trailer_NH <-- IP_Hdr[DPOVRD[NH_OFFSET]]
603 * IP_Hdr[DPOVRD[NH_OFFSET]] <-- DPOVRD[NH]
605 * IP_Hdr[DPOVRD[NH_OFFSET]] <-- ESP_Trailer_NH
607 #define IPSEC_DPOVRD_NH_OFFSET_SHIFT 8
610 * IPSEC_DPOVRD_NH_OFFSET_MASK - See IPSEC_DPOVRD_NH_OFFSET_SHIFT
612 #define IPSEC_DPOVRD_NH_OFFSET_MASK (0xff << IPSEC_DPOVRD_NH_OFFSET_SHIFT)
615 * IPSEC_DPOVRD_NH_MASK - See IPSEC_DPOVRD_NH_OFFSET_SHIFT
616 * Valid only for encapsulation.
618 #define IPSEC_DPOVRD_NH_MASK 0xff
621 * IPSEC_N_ENCAP_DPOVRD_OIM_LEN_SHIFT - Outer IP header Material length (encap)
622 * Valid only if L2_COPY is not set.
624 #define IPSEC_N_ENCAP_DPOVRD_OIM_LEN_SHIFT 16
627 * IPSEC_N_ENCAP_DPOVRD_OIM_LEN_MASK - See IPSEC_N_ENCAP_DPOVRD_OIM_LEN_SHIFT
629 #define IPSEC_N_ENCAP_DPOVRD_OIM_LEN_MASK \
630 (0xfff << IPSEC_N_ENCAP_DPOVRD_OIM_LEN_SHIFT)
633 * IPSEC_N_ENCAP_DPOVRD_L2_LEN_SHIFT - L2 header length
634 * Valid only if L2_COPY is set.
636 #define IPSEC_N_ENCAP_DPOVRD_L2_LEN_SHIFT 16
639 * IPSEC_N_ENCAP_DPOVRD_L2_LEN_MASK - See IPSEC_N_ENCAP_DPOVRD_L2_LEN_SHIFT
641 #define IPSEC_N_ENCAP_DPOVRD_L2_LEN_MASK \
642 (0xff << IPSEC_N_ENCAP_DPOVRD_L2_LEN_SHIFT)
645 * IPSEC_N_ENCAP_DPOVRD_OIMIF - Outer IP header Material in Input Frame
647 #define IPSEC_N_ENCAP_DPOVRD_OIMIF BIT(15)
650 * IPSEC_N_ENCAP_DPOVRD_L2_COPY - L2 header present in input frame
652 * Note: For Era <= 8, this bit is reserved (not used) by HW.
654 #define IPSEC_N_ENCAP_DPOVRD_L2_COPY BIT(14)
657 * IPSEC_N_ENCAP_DPOVRD_AOIPHO_SHIFT - Actual Outer IP Header Offset (encap)
659 #define IPSEC_N_ENCAP_DPOVRD_AOIPHO_SHIFT 8
662 * IPSEC_N_ENCAP_DPOVRD_AOIPHO_MASK - See IPSEC_N_ENCAP_DPOVRD_AOIPHO_SHIFT
664 #define IPSEC_N_ENCAP_DPOVRD_AOIPHO_MASK \
665 (0x3c << IPSEC_N_ENCAP_DPOVRD_AOIPHO_SHIFT)
668 * IPSEC_N_ENCAP_DPOVRD_NH_MASK - Next Header
670 * Used in the Next Header field of the encapsulated payload.
672 #define IPSEC_N_ENCAP_DPOVRD_NH_MASK 0xff
675 * IPSEC_N_DECAP_DPOVRD_AOIPHO_SHIFT - Actual Outer IP Header Offset (decap)
677 #define IPSEC_N_DECAP_DPOVRD_AOIPHO_SHIFT 12
680 * IPSEC_N_DECAP_DPOVRD_AOIPHO_MASK - See IPSEC_N_DECAP_DPOVRD_AOIPHO_SHIFT
682 #define IPSEC_N_DECAP_DPOVRD_AOIPHO_MASK \
683 (0xff << IPSEC_N_DECAP_DPOVRD_AOIPHO_SHIFT)
686 * IPSEC_N_DECAP_DPOVRD_OIM_LEN_MASK - Outer IP header Material length (decap)
688 #define IPSEC_N_DECAP_DPOVRD_OIM_LEN_MASK 0xfff
690 static inline void __gen_auth_key(struct program *program,
691 struct alginfo *authdata)
695 switch (authdata->algtype & OP_PCL_IPSEC_AUTH_MASK) {
696 case OP_PCL_IPSEC_HMAC_MD5_96:
697 case OP_PCL_IPSEC_HMAC_MD5_128:
698 dkp_protid = OP_PCLID_DKP_MD5;
700 case OP_PCL_IPSEC_HMAC_SHA1_96:
701 case OP_PCL_IPSEC_HMAC_SHA1_160:
702 dkp_protid = OP_PCLID_DKP_SHA1;
704 case OP_PCL_IPSEC_HMAC_SHA2_256_128:
705 dkp_protid = OP_PCLID_DKP_SHA256;
707 case OP_PCL_IPSEC_HMAC_SHA2_384_192:
708 dkp_protid = OP_PCLID_DKP_SHA384;
710 case OP_PCL_IPSEC_HMAC_SHA2_512_256:
711 dkp_protid = OP_PCLID_DKP_SHA512;
714 KEY(program, KEY2, authdata->key_enc_flags, authdata->key,
715 authdata->keylen, INLINE_KEY(authdata));
719 if (authdata->key_type == RTA_DATA_PTR)
720 DKP_PROTOCOL(program, dkp_protid, OP_PCL_DKP_SRC_PTR,
721 OP_PCL_DKP_DST_PTR, (uint16_t)authdata->keylen,
722 authdata->key, authdata->key_type);
724 DKP_PROTOCOL(program, dkp_protid, OP_PCL_DKP_SRC_IMM,
725 OP_PCL_DKP_DST_IMM, (uint16_t)authdata->keylen,
726 authdata->key, authdata->key_type);
730 * cnstr_shdsc_ipsec_encap - IPSec ESP encapsulation protocol-level shared
732 * @descbuf: pointer to buffer used for descriptor construction
733 * @ps: if 36/40bit addressing is desired, this parameter must be true
734 * @swap: if true, perform descriptor byte swapping on a 4-byte boundary
735 * @share: sharing type of shared descriptor
736 * @pdb: pointer to the PDB to be used with this descriptor
737 * This structure will be copied inline to the descriptor under
738 * construction. No error checking will be made. Refer to the
739 * block guide for a details of the encapsulation PDB.
740 * @cipherdata: pointer to block cipher transform definitions
741 * Valid algorithm values - one of OP_PCL_IPSEC_*
742 * @authdata: pointer to authentication transform definitions
743 * If an authentication key is required by the protocol:
744 * -For SEC Eras 1-5, an MDHA split key must be provided;
745 * Note that the size of the split key itself must be specified.
746 * -For SEC Eras 6+, a "normal" key must be provided; DKP (Derived
747 * Key Protocol) will be used to compute MDHA on the fly in HW.
748 * Valid algorithm values - one of OP_PCL_IPSEC_*
750 * Return: size of descriptor written in words or negative number on error
753 cnstr_shdsc_ipsec_encap(uint32_t *descbuf, bool ps, bool swap,
754 enum rta_share_type share,
755 struct ipsec_encap_pdb *pdb,
756 struct alginfo *cipherdata,
757 struct alginfo *authdata)
760 struct program *p = &prg;
767 PROGRAM_CNTXT_INIT(p, descbuf, 0);
769 PROGRAM_SET_BSWAP(p);
771 PROGRAM_SET_36BIT_ADDR(p);
772 phdr = SHR_HDR(p, share, hdr, 0);
773 __rta_copy_ipsec_encap_pdb(p, pdb, cipherdata->algtype);
774 COPY_DATA(p, pdb->ip_hdr, pdb->ip_hdr_len);
776 pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, BOTH|SHRD);
777 if (authdata->keylen) {
778 if (rta_sec_era < RTA_SEC_ERA_6)
779 KEY(p, MDHA_SPLIT_KEY, authdata->key_enc_flags,
780 authdata->key, authdata->keylen,
781 INLINE_KEY(authdata));
783 __gen_auth_key(p, authdata);
785 if (cipherdata->keylen)
786 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
787 cipherdata->keylen, INLINE_KEY(cipherdata));
788 SET_LABEL(p, keyjmp);
789 PROTOCOL(p, OP_TYPE_ENCAP_PROTOCOL,
791 (uint16_t)(cipherdata->algtype | authdata->algtype));
792 PATCH_JUMP(p, pkeyjmp, keyjmp);
793 PATCH_HDR(p, phdr, hdr);
794 return PROGRAM_FINALIZE(p);
798 * cnstr_shdsc_ipsec_decap - IPSec ESP decapsulation protocol-level shared
800 * @descbuf: pointer to buffer used for descriptor construction
801 * @ps: if 36/40bit addressing is desired, this parameter must be true
802 * @swap: if true, perform descriptor byte swapping on a 4-byte boundary
803 * @share: sharing type of shared descriptor
804 * @pdb: pointer to the PDB to be used with this descriptor
805 * This structure will be copied inline to the descriptor under
806 * construction. No error checking will be made. Refer to the
807 * block guide for details about the decapsulation PDB.
808 * @cipherdata: pointer to block cipher transform definitions.
809 * Valid algorithm values - one of OP_PCL_IPSEC_*
810 * @authdata: pointer to authentication transform definitions
811 * If an authentication key is required by the protocol:
812 * -For SEC Eras 1-5, an MDHA split key must be provided;
813 * Note that the size of the split key itself must be specified.
814 * -For SEC Eras 6+, a "normal" key must be provided; DKP (Derived
815 * Key Protocol) will be used to compute MDHA on the fly in HW.
816 * Valid algorithm values - one of OP_PCL_IPSEC_*
818 * Return: size of descriptor written in words or negative number on error
821 cnstr_shdsc_ipsec_decap(uint32_t *descbuf, bool ps, bool swap,
822 enum rta_share_type share,
823 struct ipsec_decap_pdb *pdb,
824 struct alginfo *cipherdata,
825 struct alginfo *authdata)
828 struct program *p = &prg;
835 PROGRAM_CNTXT_INIT(p, descbuf, 0);
837 PROGRAM_SET_BSWAP(p);
839 PROGRAM_SET_36BIT_ADDR(p);
840 phdr = SHR_HDR(p, share, hdr, 0);
841 __rta_copy_ipsec_decap_pdb(p, pdb, cipherdata->algtype);
843 pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, BOTH|SHRD);
844 if (authdata->keylen) {
845 if (rta_sec_era < RTA_SEC_ERA_6)
846 KEY(p, MDHA_SPLIT_KEY, authdata->key_enc_flags,
847 authdata->key, authdata->keylen,
848 INLINE_KEY(authdata));
850 __gen_auth_key(p, authdata);
852 if (cipherdata->keylen)
853 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
854 cipherdata->keylen, INLINE_KEY(cipherdata));
855 SET_LABEL(p, keyjmp);
856 PROTOCOL(p, OP_TYPE_DECAP_PROTOCOL,
858 (uint16_t)(cipherdata->algtype | authdata->algtype));
859 PATCH_JUMP(p, pkeyjmp, keyjmp);
860 PATCH_HDR(p, phdr, hdr);
861 return PROGRAM_FINALIZE(p);
865 * cnstr_shdsc_ipsec_encap_des_aes_xcbc - IPSec DES-CBC/3DES-CBC and
866 * AES-XCBC-MAC-96 ESP encapsulation shared descriptor.
867 * @descbuf: pointer to buffer used for descriptor construction
868 * @pdb: pointer to the PDB to be used with this descriptor
869 * This structure will be copied inline to the descriptor under
870 * construction. No error checking will be made. Refer to the
871 * block guide for a details of the encapsulation PDB.
872 * @cipherdata: pointer to block cipher transform definitions
873 * Valid algorithm values - OP_PCL_IPSEC_DES, OP_PCL_IPSEC_3DES.
874 * @authdata: pointer to authentication transform definitions
875 * Valid algorithm value: OP_PCL_IPSEC_AES_XCBC_MAC_96.
877 * Supported only for platforms with 32-bit address pointers and SEC ERA 4 or
878 * higher. The tunnel/transport mode of the IPsec ESP is supported only if the
879 * Outer/Transport IP Header is present in the encapsulation output packet.
880 * The descriptor performs DES-CBC/3DES-CBC & HMAC-MD5-96 and then rereads
881 * the input packet to do the AES-XCBC-MAC-96 calculation and to overwrite
883 * The descriptor uses all the benefits of the built-in protocol by computing
884 * the IPsec ESP with a hardware supported algorithms combination
885 * (DES-CBC/3DES-CBC & HMAC-MD5-96). The HMAC-MD5 authentication algorithm
886 * was chosen in order to speed up the computational time for this intermediate
888 * Warning: The user must allocate at least 32 bytes for the authentication key
889 * (in order to use it also with HMAC-MD5-96),even when using a shorter key
890 * for the AES-XCBC-MAC-96.
892 * Return: size of descriptor written in words or negative number on error
895 cnstr_shdsc_ipsec_encap_des_aes_xcbc(uint32_t *descbuf,
896 struct ipsec_encap_pdb *pdb,
897 struct alginfo *cipherdata,
898 struct alginfo *authdata)
901 struct program *p = &prg;
907 LABEL(swapped_seqin_fields);
908 LABEL(swapped_seqin_ptr);
911 REFERENCE(move_outlen);
912 REFERENCE(move_seqout_ptr);
913 REFERENCE(swapped_seqin_ptr_jump);
914 REFERENCE(write_swapped_seqin_ptr);
916 PROGRAM_CNTXT_INIT(p, descbuf, 0);
917 phdr = SHR_HDR(p, SHR_SERIAL, hdr, 0);
918 __rta_copy_ipsec_encap_pdb(p, pdb, cipherdata->algtype);
919 COPY_DATA(p, pdb->ip_hdr, pdb->ip_hdr_len);
921 pkeyjump = JUMP(p, keyjump, LOCAL_JUMP, ALL_TRUE, SHRD | SELF);
923 * Hard-coded KEY arguments. The descriptor uses all the benefits of
924 * the built-in protocol by computing the IPsec ESP with a hardware
925 * supported algorithms combination (DES-CBC/3DES-CBC & HMAC-MD5-96).
926 * The HMAC-MD5 authentication algorithm was chosen with
927 * the keys options from below in order to speed up the computational
928 * time for this intermediate step.
929 * Warning: The user must allocate at least 32 bytes for
930 * the authentication key (in order to use it also with HMAC-MD5-96),
931 * even when using a shorter key for the AES-XCBC-MAC-96.
933 KEY(p, MDHA_SPLIT_KEY, 0, authdata->key, 32, INLINE_KEY(authdata));
934 SET_LABEL(p, keyjump);
935 LOAD(p, LDST_SRCDST_WORD_CLRW | CLRW_CLR_C1MODE | CLRW_CLR_C1DATAS |
936 CLRW_CLR_C1CTX | CLRW_CLR_C1KEY | CLRW_RESET_CLS1_CHA, CLRW, 0, 4,
938 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
939 cipherdata->keylen, INLINE_KEY(cipherdata));
940 PROTOCOL(p, OP_TYPE_ENCAP_PROTOCOL, OP_PCLID_IPSEC,
941 (uint16_t)(cipherdata->algtype | OP_PCL_IPSEC_HMAC_MD5_96));
942 /* Swap SEQINPTR to SEQOUTPTR. */
943 move_seqout_ptr = MOVE(p, DESCBUF, 0, MATH1, 0, 16, WAITCOMP | IMMED);
944 MATHB(p, MATH1, AND, ~(CMD_SEQ_IN_PTR ^ CMD_SEQ_OUT_PTR), MATH1,
947 * TODO: RTA currently doesn't support creating a LOAD command
948 * with another command as IMM.
949 * To be changed when proper support is added in RTA.
951 LOAD(p, 0xa00000e5, MATH3, 4, 4, IMMED);
952 MATHB(p, MATH3, SHLD, MATH3, MATH3, 8, 0);
953 write_swapped_seqin_ptr = MOVE(p, MATH1, 0, DESCBUF, 0, 20, WAITCOMP |
955 swapped_seqin_ptr_jump = JUMP(p, swapped_seqin_ptr, LOCAL_JUMP,
957 LOAD(p, LDST_SRCDST_WORD_CLRW | CLRW_CLR_C1MODE | CLRW_CLR_C1DATAS |
958 CLRW_CLR_C1CTX | CLRW_CLR_C1KEY | CLRW_RESET_CLS1_CHA, CLRW, 0, 4,
960 SEQOUTPTR(p, 0, 65535, RTO);
961 move_outlen = MOVE(p, DESCBUF, 0, MATH0, 4, 8, WAITCOMP | IMMED);
963 (uint64_t)(pdb->ip_hdr_len + IPSEC_ICV_MD5_TRUNC_SIZE),
964 VSEQINSZ, 4, IMMED2);
965 MATHB(p, MATH0, SUB, IPSEC_ICV_MD5_TRUNC_SIZE, VSEQOUTSZ, 4, IMMED2);
966 KEY(p, KEY1, authdata->key_enc_flags, authdata->key, authdata->keylen,
968 ALG_OPERATION(p, OP_ALG_ALGSEL_AES, OP_ALG_AAI_XCBC_MAC,
969 OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, DIR_ENC);
970 SEQFIFOLOAD(p, SKIP, pdb->ip_hdr_len, 0);
971 SEQFIFOLOAD(p, MSG1, 0, VLF | FLUSH1 | LAST1);
972 SEQFIFOSTORE(p, SKIP, 0, 0, VLF);
973 SEQSTORE(p, CONTEXT1, 0, IPSEC_ICV_MD5_TRUNC_SIZE, 0);
975 * TODO: RTA currently doesn't support adding labels in or after Job Descriptor.
976 * To be changed when proper support is added in RTA.
978 /* Label the Shared Descriptor Pointer */
979 SET_LABEL(p, shd_ptr);
981 /* Label the Output Pointer */
982 SET_LABEL(p, outptr);
984 /* Label the first word after JD */
985 SET_LABEL(p, swapped_seqin_fields);
986 swapped_seqin_fields += 8;
987 /* Label the second word after JD */
988 SET_LABEL(p, swapped_seqin_ptr);
989 swapped_seqin_ptr += 9;
991 PATCH_HDR(p, phdr, hdr);
992 PATCH_JUMP(p, pkeyjump, keyjump);
993 PATCH_JUMP(p, swapped_seqin_ptr_jump, swapped_seqin_ptr);
994 PATCH_MOVE(p, move_outlen, outptr);
995 PATCH_MOVE(p, move_seqout_ptr, shd_ptr);
996 PATCH_MOVE(p, write_swapped_seqin_ptr, swapped_seqin_fields);
997 return PROGRAM_FINALIZE(p);
1001 * cnstr_shdsc_ipsec_decap_des_aes_xcbc - IPSec DES-CBC/3DES-CBC and
1002 * AES-XCBC-MAC-96 ESP decapsulation shared descriptor.
1003 * @descbuf: pointer to buffer used for descriptor construction
1004 * @pdb: pointer to the PDB to be used with this descriptor
1005 * This structure will be copied inline to the descriptor under
1006 * construction. No error checking will be made. Refer to the
1007 * block guide for a details of the encapsulation PDB.
1008 * @cipherdata: pointer to block cipher transform definitions
1009 * Valid algorithm values - OP_PCL_IPSEC_DES, OP_PCL_IPSEC_3DES.
1010 * @authdata: pointer to authentication transform definitions
1011 * Valid algorithm value: OP_PCL_IPSEC_AES_XCBC_MAC_96.
1013 * Supported only for platforms with 32-bit address pointers and SEC ERA 4 or
1014 * higher. The tunnel/transport mode of the IPsec ESP is supported only if the
1015 * Outer/Transport IP Header is present in the decapsulation input packet.
1016 * The descriptor computes the AES-XCBC-MAC-96 to check if the received ICV
1017 * is correct, rereads the input packet to compute the MD5 ICV, overwrites
1018 * the XCBC ICV, and then sends the modified input packet to the
1019 * DES-CBC/3DES-CBC & HMAC-MD5-96 IPsec.
1020 * The descriptor uses all the benefits of the built-in protocol by computing
1021 * the IPsec ESP with a hardware supported algorithms combination
1022 * (DES-CBC/3DES-CBC & HMAC-MD5-96). The HMAC-MD5 authentication algorithm
1023 * was chosen in order to speed up the computational time for this intermediate
1025 * Warning: The user must allocate at least 32 bytes for the authentication key
1026 * (in order to use it also with HMAC-MD5-96),even when using a shorter key
1027 * for the AES-XCBC-MAC-96.
1029 * Return: size of descriptor written in words or negative number on error
1032 cnstr_shdsc_ipsec_decap_des_aes_xcbc(uint32_t *descbuf,
1033 struct ipsec_decap_pdb *pdb,
1034 struct alginfo *cipherdata,
1035 struct alginfo *authdata)
1038 struct program *p = &prg;
1039 uint32_t ip_hdr_len = (pdb->options & PDBHDRLEN_MASK) >>
1040 PDBHDRLEN_ESP_DECAP_SHIFT;
1048 LABEL(swapped_seqout_fields);
1049 LABEL(swapped_seqout_ptr);
1050 REFERENCE(seqout_ptr_jump);
1052 REFERENCE(pkeyjump);
1053 REFERENCE(move_jump);
1054 REFERENCE(move_jump_back);
1055 REFERENCE(move_seqin_ptr);
1056 REFERENCE(swapped_seqout_ptr_jump);
1057 REFERENCE(write_swapped_seqout_ptr);
1059 PROGRAM_CNTXT_INIT(p, descbuf, 0);
1060 phdr = SHR_HDR(p, SHR_SERIAL, hdr, 0);
1061 __rta_copy_ipsec_decap_pdb(p, pdb, cipherdata->algtype);
1063 pkeyjump = JUMP(p, keyjump, LOCAL_JUMP, ALL_TRUE, SHRD | SELF);
1065 * Hard-coded KEY arguments. The descriptor uses all the benefits of
1066 * the built-in protocol by computing the IPsec ESP with a hardware
1067 * supported algorithms combination (DES-CBC/3DES-CBC & HMAC-MD5-96).
1068 * The HMAC-MD5 authentication algorithm was chosen with
1069 * the keys options from bellow in order to speed up the computational
1070 * time for this intermediate step.
1071 * Warning: The user must allocate at least 32 bytes for
1072 * the authentication key (in order to use it also with HMAC-MD5-96),
1073 * even when using a shorter key for the AES-XCBC-MAC-96.
1075 KEY(p, MDHA_SPLIT_KEY, 0, authdata->key, 32, INLINE_KEY(authdata));
1076 SET_LABEL(p, keyjump);
1077 LOAD(p, LDST_SRCDST_WORD_CLRW | CLRW_CLR_C1MODE | CLRW_CLR_C1DATAS |
1078 CLRW_CLR_C1CTX | CLRW_CLR_C1KEY | CLRW_RESET_CLS1_CHA, CLRW, 0, 4,
1080 KEY(p, KEY1, authdata->key_enc_flags, authdata->key, authdata->keylen,
1081 INLINE_KEY(authdata));
1082 MATHB(p, SEQINSZ, SUB,
1083 (uint64_t)(ip_hdr_len + IPSEC_ICV_MD5_TRUNC_SIZE), MATH0, 4,
1085 MATHB(p, MATH0, SUB, ZERO, VSEQINSZ, 4, 0);
1086 ALG_OPERATION(p, OP_ALG_ALGSEL_MD5, OP_ALG_AAI_HMAC_PRECOMP,
1087 OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, DIR_ENC);
1088 ALG_OPERATION(p, OP_ALG_ALGSEL_AES, OP_ALG_AAI_XCBC_MAC,
1089 OP_ALG_AS_INITFINAL, ICV_CHECK_ENABLE, DIR_DEC);
1090 SEQFIFOLOAD(p, SKIP, ip_hdr_len, 0);
1091 SEQFIFOLOAD(p, MSG1, 0, VLF | FLUSH1);
1092 SEQFIFOLOAD(p, ICV1, IPSEC_ICV_MD5_TRUNC_SIZE, FLUSH1 | LAST1);
1093 /* Swap SEQOUTPTR to SEQINPTR. */
1094 move_seqin_ptr = MOVE(p, DESCBUF, 0, MATH1, 0, 16, WAITCOMP | IMMED);
1095 MATHB(p, MATH1, OR, CMD_SEQ_IN_PTR ^ CMD_SEQ_OUT_PTR, MATH1, 8,
1098 * TODO: RTA currently doesn't support creating a LOAD command
1099 * with another command as IMM.
1100 * To be changed when proper support is added in RTA.
1102 LOAD(p, 0xA00000e1, MATH3, 4, 4, IMMED);
1103 MATHB(p, MATH3, SHLD, MATH3, MATH3, 8, 0);
1104 write_swapped_seqout_ptr = MOVE(p, MATH1, 0, DESCBUF, 0, 20, WAITCOMP |
1106 swapped_seqout_ptr_jump = JUMP(p, swapped_seqout_ptr, LOCAL_JUMP,
1109 * TODO: To be changed when proper support is added in RTA (can't load
1110 * a command that is also written by RTA).
1111 * Change when proper RTA support is added.
1113 SET_LABEL(p, jump_cmd);
1114 WORD(p, 0xA00000f3);
1115 SEQINPTR(p, 0, 65535, RTO);
1116 MATHB(p, MATH0, SUB, ZERO, VSEQINSZ, 4, 0);
1117 MATHB(p, MATH0, ADD, ip_hdr_len, VSEQOUTSZ, 4, IMMED2);
1118 move_jump = MOVE(p, DESCBUF, 0, OFIFO, 0, 8, WAITCOMP | IMMED);
1119 move_jump_back = MOVE(p, OFIFO, 0, DESCBUF, 0, 8, IMMED);
1120 SEQFIFOLOAD(p, SKIP, ip_hdr_len, 0);
1121 SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
1122 SEQFIFOSTORE(p, SKIP, 0, 0, VLF);
1123 SEQSTORE(p, CONTEXT2, 0, IPSEC_ICV_MD5_TRUNC_SIZE, 0);
1124 seqout_ptr_jump = JUMP(p, seqout_ptr, LOCAL_JUMP, ALL_TRUE, CALM);
1126 LOAD(p, LDST_SRCDST_WORD_CLRW | CLRW_CLR_C1MODE | CLRW_CLR_C1DATAS |
1127 CLRW_CLR_C1CTX | CLRW_CLR_C1KEY | CLRW_CLR_C2MODE |
1128 CLRW_CLR_C2DATAS | CLRW_CLR_C2CTX | CLRW_RESET_CLS1_CHA, CLRW, 0,
1130 SEQINPTR(p, 0, 65535, RTO);
1131 MATHB(p, MATH0, ADD,
1132 (uint64_t)(ip_hdr_len + IPSEC_ICV_MD5_TRUNC_SIZE), SEQINSZ, 4,
1134 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
1135 cipherdata->keylen, INLINE_KEY(cipherdata));
1136 PROTOCOL(p, OP_TYPE_DECAP_PROTOCOL, OP_PCLID_IPSEC,
1137 (uint16_t)(cipherdata->algtype | OP_PCL_IPSEC_HMAC_MD5_96));
1139 * TODO: RTA currently doesn't support adding labels in or after Job Descriptor.
1140 * To be changed when proper support is added in RTA.
1142 /* Label the SEQ OUT PTR */
1143 SET_LABEL(p, seqout_ptr);
1145 /* Label the Output Length */
1146 SET_LABEL(p, outlen);
1148 /* Label the SEQ IN PTR */
1149 SET_LABEL(p, seqin_ptr);
1151 /* Label the first word after JD */
1152 SET_LABEL(p, swapped_seqout_fields);
1153 swapped_seqout_fields += 8;
1154 /* Label the second word after JD */
1155 SET_LABEL(p, swapped_seqout_ptr);
1156 swapped_seqout_ptr += 9;
1158 PATCH_HDR(p, phdr, hdr);
1159 PATCH_JUMP(p, pkeyjump, keyjump);
1160 PATCH_JUMP(p, seqout_ptr_jump, seqout_ptr);
1161 PATCH_JUMP(p, swapped_seqout_ptr_jump, swapped_seqout_ptr);
1162 PATCH_MOVE(p, move_jump, jump_cmd);
1163 PATCH_MOVE(p, move_jump_back, seqin_ptr);
1164 PATCH_MOVE(p, move_seqin_ptr, outlen);
1165 PATCH_MOVE(p, write_swapped_seqout_ptr, swapped_seqout_fields);
1166 return PROGRAM_FINALIZE(p);
1170 * IPSEC_NEW_ENC_BASE_DESC_LEN - IPsec new mode encap shared descriptor length
1172 * Accounts only for the "base" commands and is intended to be used by upper
1173 * layers to determine whether Outer IP Header and/or keys can be inlined or
1174 * not. To be used as first parameter of rta_inline_query().
1176 #define IPSEC_NEW_ENC_BASE_DESC_LEN (12 * CAAM_CMD_SZ + \
1177 sizeof(struct ipsec_encap_pdb))
1180 * IPSEC_NEW_NULL_ENC_BASE_DESC_LEN - IPsec new mode encap shared descriptor
1181 * length for the case of
1182 * NULL encryption / authentication
1184 * Accounts only for the "base" commands and is intended to be used by upper
1185 * layers to determine whether Outer IP Header and/or key can be inlined or
1186 * not. To be used as first parameter of rta_inline_query().
1188 #define IPSEC_NEW_NULL_ENC_BASE_DESC_LEN (11 * CAAM_CMD_SZ + \
1189 sizeof(struct ipsec_encap_pdb))
1192 * cnstr_shdsc_ipsec_new_encap - IPSec new mode ESP encapsulation
1193 * protocol-level shared descriptor.
1194 * @descbuf: pointer to buffer used for descriptor construction
1195 * @ps: if 36/40bit addressing is desired, this parameter must be true
1196 * @swap: must be true when core endianness doesn't match SEC endianness
1197 * @share: sharing type of shared descriptor
1198 * @pdb: pointer to the PDB to be used with this descriptor
1199 * This structure will be copied inline to the descriptor under
1200 * construction. No error checking will be made. Refer to the
1201 * block guide for details about the encapsulation PDB.
1202 * @opt_ip_hdr: pointer to Optional IP Header
1203 * -if OIHI = PDBOPTS_ESP_OIHI_PDB_INL, opt_ip_hdr points to the buffer to
1204 * be inlined in the PDB. Number of bytes (buffer size) copied is provided
1205 * in pdb->ip_hdr_len.
1206 * -if OIHI = PDBOPTS_ESP_OIHI_PDB_REF, opt_ip_hdr points to the address of
1207 * the Optional IP Header. The address will be inlined in the PDB verbatim.
1208 * -for other values of OIHI options field, opt_ip_hdr is not used.
1209 * @cipherdata: pointer to block cipher transform definitions
1210 * Valid algorithm values - one of OP_PCL_IPSEC_*
1211 * @authdata: pointer to authentication transform definitions.
1212 * If an authentication key is required by the protocol, a "normal"
1213 * key must be provided; DKP (Derived Key Protocol) will be used to
1214 * compute MDHA on the fly in HW.
1215 * Valid algorithm values - one of OP_PCL_IPSEC_*
1217 * Note: L2 header copy functionality is implemented assuming that bits 14
1218 * (currently reserved) and 16-23 (part of Outer IP Header Material Length)
1219 * in DPOVRD register are not used (which is usually the case when L3 header
1220 * is provided in PDB).
1221 * When DPOVRD[14] is set, frame starts with an L2 header; in this case, the
1222 * L2 header length is found at DPOVRD[23:16]. SEC uses this length to copy
1223 * the header and then it deletes DPOVRD[23:16] (so there is no side effect
1224 * when later running IPsec protocol).
1226 * Return: size of descriptor written in words or negative number on error
1229 cnstr_shdsc_ipsec_new_encap(uint32_t *descbuf, bool ps,
1231 enum rta_share_type share,
1232 struct ipsec_encap_pdb *pdb,
1233 uint8_t *opt_ip_hdr,
1234 struct alginfo *cipherdata,
1235 struct alginfo *authdata)
1238 struct program *p = &prg;
1247 if (rta_sec_era < RTA_SEC_ERA_8) {
1248 pr_err("IPsec new mode encap: available only for Era %d or above\n",
1249 USER_SEC_ERA(RTA_SEC_ERA_8));
1253 PROGRAM_CNTXT_INIT(p, descbuf, 0);
1255 PROGRAM_SET_BSWAP(p);
1257 PROGRAM_SET_36BIT_ADDR(p);
1258 phdr = SHR_HDR(p, share, hdr, 0);
1260 __rta_copy_ipsec_encap_pdb(p, pdb, cipherdata->algtype);
1262 switch (pdb->options & PDBOPTS_ESP_OIHI_MASK) {
1263 case PDBOPTS_ESP_OIHI_PDB_INL:
1264 COPY_DATA(p, opt_ip_hdr, pdb->ip_hdr_len);
1266 case PDBOPTS_ESP_OIHI_PDB_REF:
1268 COPY_DATA(p, opt_ip_hdr, 8);
1270 COPY_DATA(p, opt_ip_hdr, 4);
1277 MATHB(p, DPOVRD, AND, IPSEC_N_ENCAP_DPOVRD_L2_COPY, NONE, 4, IMMED2);
1278 pl2copy = JUMP(p, l2copy, LOCAL_JUMP, ALL_TRUE, MATH_Z);
1279 MATHI(p, DPOVRD, RSHIFT, IPSEC_N_ENCAP_DPOVRD_L2_LEN_SHIFT, VSEQOUTSZ,
1281 MATHB(p, DPOVRD, AND, ~IPSEC_N_ENCAP_DPOVRD_L2_LEN_MASK, DPOVRD, 4,
1283 /* TODO: CLASS2 corresponds to AUX=2'b10; add more intuitive defines */
1284 SEQFIFOSTORE(p, METADATA, 0, 0, CLASS2 | VLF);
1285 SET_LABEL(p, l2copy);
1287 pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
1288 if (authdata->keylen)
1289 __gen_auth_key(p, authdata);
1290 if (cipherdata->keylen)
1291 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
1292 cipherdata->keylen, INLINE_KEY(cipherdata));
1293 SET_LABEL(p, keyjmp);
1294 PROTOCOL(p, OP_TYPE_ENCAP_PROTOCOL,
1296 (uint16_t)(cipherdata->algtype | authdata->algtype));
1297 PATCH_JUMP(p, pl2copy, l2copy);
1298 PATCH_JUMP(p, pkeyjmp, keyjmp);
1299 PATCH_HDR(p, phdr, hdr);
1300 return PROGRAM_FINALIZE(p);
1304 * IPSEC_NEW_DEC_BASE_DESC_LEN - IPsec new mode decap shared descriptor length
1306 * Accounts only for the "base" commands and is intended to be used by upper
1307 * layers to determine whether keys can be inlined or not. To be used as first
1308 * parameter of rta_inline_query().
1310 #define IPSEC_NEW_DEC_BASE_DESC_LEN (5 * CAAM_CMD_SZ + \
1311 sizeof(struct ipsec_decap_pdb))
1314 * IPSEC_NEW_NULL_DEC_BASE_DESC_LEN - IPsec new mode decap shared descriptor
1315 * length for the case of
1316 * NULL decryption / authentication
1318 * Accounts only for the "base" commands and is intended to be used by upper
1319 * layers to determine whether key can be inlined or not. To be used as first
1320 * parameter of rta_inline_query().
1322 #define IPSEC_NEW_NULL_DEC_BASE_DESC_LEN (4 * CAAM_CMD_SZ + \
1323 sizeof(struct ipsec_decap_pdb))
1326 * cnstr_shdsc_ipsec_new_decap - IPSec new mode ESP decapsulation protocol-level
1327 * shared descriptor.
1328 * @descbuf: pointer to buffer used for descriptor construction
1329 * @ps: if 36/40bit addressing is desired, this parameter must be true
1330 * @swap: must be true when core endianness doesn't match SEC endianness
1331 * @share: sharing type of shared descriptor
1332 * @pdb: pointer to the PDB to be used with this descriptor
1333 * This structure will be copied inline to the descriptor under
1334 * construction. No error checking will be made. Refer to the
1335 * block guide for details about the decapsulation PDB.
1336 * @cipherdata: pointer to block cipher transform definitions
1337 * Valid algorithm values 0 one of OP_PCL_IPSEC_*
1338 * @authdata: pointer to authentication transform definitions.
1339 * If an authentication key is required by the protocol, a "normal"
1340 * key must be provided; DKP (Derived Key Protocol) will be used to
1341 * compute MDHA on the fly in HW.
1342 * Valid algorithm values - one of OP_PCL_IPSEC_*
1344 * Return: size of descriptor written in words or negative number on error
1347 cnstr_shdsc_ipsec_new_decap(uint32_t *descbuf, bool ps,
1349 enum rta_share_type share,
1350 struct ipsec_decap_pdb *pdb,
1351 struct alginfo *cipherdata,
1352 struct alginfo *authdata)
1355 struct program *p = &prg;
1362 if (rta_sec_era < RTA_SEC_ERA_8) {
1363 pr_err("IPsec new mode decap: available only for Era %d or above\n",
1364 USER_SEC_ERA(RTA_SEC_ERA_8));
1368 PROGRAM_CNTXT_INIT(p, descbuf, 0);
1370 PROGRAM_SET_BSWAP(p);
1372 PROGRAM_SET_36BIT_ADDR(p);
1373 phdr = SHR_HDR(p, share, hdr, 0);
1374 __rta_copy_ipsec_decap_pdb(p, pdb, cipherdata->algtype);
1376 pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
1377 if (authdata->keylen)
1378 __gen_auth_key(p, authdata);
1379 if (cipherdata->keylen)
1380 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
1381 cipherdata->keylen, INLINE_KEY(cipherdata));
1382 SET_LABEL(p, keyjmp);
1383 PROTOCOL(p, OP_TYPE_DECAP_PROTOCOL,
1385 (uint16_t)(cipherdata->algtype | authdata->algtype));
1386 PATCH_JUMP(p, pkeyjmp, keyjmp);
1387 PATCH_HDR(p, phdr, hdr);
1388 return PROGRAM_FINALIZE(p);
1392 * IPSEC_AUTH_VAR_BASE_DESC_LEN - IPsec encap/decap shared descriptor length
1393 * for the case of variable-length authentication
1395 * Note: Only for SoCs with SEC_ERA >= 3.
1397 * Accounts only for the "base" commands and is intended to be used by upper
1398 * layers to determine whether keys can be inlined or not. To be used as first
1399 * parameter of rta_inline_query().
1401 #define IPSEC_AUTH_VAR_BASE_DESC_LEN (27 * CAAM_CMD_SZ)
1404 * IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN - IPsec AES decap shared descriptor
1405 * length for variable-length authentication only
1407 * Note: Only for SoCs with SEC_ERA >= 3.
1409 * Accounts only for the "base" commands and is intended to be used by upper
1410 * layers to determine whether key can be inlined or not. To be used as first
1411 * parameter of rta_inline_query().
1413 #define IPSEC_AUTH_VAR_AES_DEC_BASE_DESC_LEN \
1414 (IPSEC_AUTH_VAR_BASE_DESC_LEN + CAAM_CMD_SZ)
1417 * IPSEC_AUTH_BASE_DESC_LEN - IPsec encap/decap shared descriptor length
1419 * Accounts only for the "base" commands and is intended to be used by upper
1420 * layers to determine whether key can be inlined or not. To be used as first
1421 * parameter of rta_inline_query().
1423 #define IPSEC_AUTH_BASE_DESC_LEN (19 * CAAM_CMD_SZ)
1426 * IPSEC_AUTH_AES_DEC_BASE_DESC_LEN - IPsec AES decap shared descriptor length
1428 * Accounts only for the "base" commands and is intended to be used by upper
1429 * layers to determine whether key can be inlined or not. To be used as first
1430 * parameter of rta_inline_query().
1432 #define IPSEC_AUTH_AES_DEC_BASE_DESC_LEN (IPSEC_AUTH_BASE_DESC_LEN + \
1436 * cnstr_shdsc_authenc - authenc-like descriptor
1437 * @descbuf: pointer to buffer used for descriptor construction
1438 * @ps: if 36/40bit addressing is desired, this parameter must be true
1439 * @swap: if true, perform descriptor byte swapping on a 4-byte boundary
1440 * @share: sharing type of shared descriptor
1441 * @cipherdata: pointer to block cipher transform definitions.
1442 * Valid algorithm values one of OP_ALG_ALGSEL_* {DES, 3DES, AES}
1444 * AES: OP_ALG_AAI_* {CBC, CTR}
1445 * DES, 3DES: OP_ALG_AAI_CBC
1446 * @authdata: pointer to authentication transform definitions.
1447 * Valid algorithm values - one of OP_ALG_ALGSEL_* {MD5, SHA1,
1448 * SHA224, SHA256, SHA384, SHA512}
1449 * Note: The key for authentication is supposed to be given as plain text.
1450 * Note: There's no support for keys longer than the block size of the
1451 * underlying hash function, according to the selected algorithm.
1453 * @ivlen: length of the IV to be read from the input frame, before any data
1456 * @trunc_len: the length of the ICV to be written to the output frame. If 0,
1457 * then the corresponding length of the digest, according to the
1458 * selected algorithm shall be used.
1459 * @dir: Protocol direction, encapsulation or decapsulation (DIR_ENC/DIR_DEC)
1461 * Note: Here's how the input frame needs to be formatted so that the processing
1462 * will be done correctly:
1463 * For encapsulation:
1465 * +----+----------------+-----------------------------------------------+
1466 * | IV | Auth-only head | Padded data to be auth & Enc | Auth-only tail |
1467 * +----+----------------+-----------------------------------------------+
1469 * +--------------------------------------+
1470 * | Authenticated & Encrypted data | ICV |
1471 * +--------------------------------+-----+
1473 * For decapsulation:
1475 * +----+----------------+-----------------+----------------------+
1476 * | IV | Auth-only head | Auth & Enc data | Auth-only tail | ICV |
1477 * +----+----------------+-----------------+----------------------+
1479 * +----+---------------------------+
1480 * | Decrypted & authenticated data |
1481 * +----+---------------------------+
1483 * Note: This descriptor can use per-packet commands, encoded as below in the
1486 * +------+------------------------------+
1487 * | 0x8 | auth_tail_len | auth_hdr_len |
1488 * +------+------------------------------+
1490 * This mechanism is available only for SoCs having SEC ERA >= 3. In other
1491 * words, this will not work for P4080TO2
1493 * Note: The descriptor does not add any kind of padding to the input data,
1494 * so the upper layer needs to ensure that the data is padded properly,
1495 * according to the selected cipher. Failure to do so will result in
1496 * the descriptor failing with a data-size error.
1498 * Return: size of descriptor written in words or negative number on error
1501 cnstr_shdsc_authenc(uint32_t *descbuf, bool ps, bool swap,
1502 enum rta_share_type share,
1503 struct alginfo *cipherdata,
1504 struct alginfo *authdata,
1506 uint8_t trunc_len, uint8_t dir)
1509 struct program *p = &prg;
1510 const bool need_dk = (dir == DIR_DEC) &&
1511 (cipherdata->algtype == OP_ALG_ALGSEL_AES) &&
1512 (cipherdata->algmode == OP_ALG_AAI_CBC);
1518 LABEL(no_auth_tail);
1520 REFERENCE(pskipkeys);
1521 REFERENCE(p_proc_icv);
1522 REFERENCE(p_no_auth_tail);
1524 PROGRAM_CNTXT_INIT(p, descbuf, 0);
1527 PROGRAM_SET_BSWAP(p);
1529 PROGRAM_SET_36BIT_ADDR(p);
1532 * Since we currently assume that key length is equal to hash digest
1533 * size, it's ok to truncate keylen value.
1535 trunc_len = trunc_len && (trunc_len < authdata->keylen) ?
1536 trunc_len : (uint8_t)authdata->keylen;
1538 SHR_HDR(p, share, 1, SC);
1540 /* Collect the (auth_tail || auth_hdr) len from DPOVRD */
1541 MATHB(p, DPOVRD, ADD, 0x80000000, MATH2, 4, IMMED2);
1543 /* Get auth_hdr len in MATH0 */
1544 MATHB(p, MATH2, AND, 0xFFFF, MATH0, 4, IMMED2);
1546 /* Get auth_tail len in MATH2 */
1547 MATHB(p, MATH2, AND, 0xFFF0000, MATH2, 4, IMMED2);
1548 MATHI(p, MATH2, RSHIFT, 16, MATH2, 4, IMMED2);
1550 pkeyjmp = JUMP(p, keyjmp, LOCAL_JUMP, ALL_TRUE, SHRD);
1552 KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
1553 INLINE_KEY(authdata));
1556 KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
1557 cipherdata->keylen, INLINE_KEY(cipherdata));
1560 ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC,
1561 OP_ALG_AS_INITFINAL,
1562 dir == DIR_ENC ? ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
1566 ALG_OPERATION(p, OP_ALG_ALGSEL_AES, cipherdata->algmode,
1567 OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
1568 pskipkeys = JUMP(p, skipkeys, LOCAL_JUMP, ALL_TRUE, 0);
1570 SET_LABEL(p, keyjmp);
1572 ALG_OPERATION(p, authdata->algtype, OP_ALG_AAI_HMAC_PRECOMP,
1573 OP_ALG_AS_INITFINAL,
1574 dir == DIR_ENC ? ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
1578 ALG_OPERATION(p, OP_ALG_ALGSEL_AES, cipherdata->algmode |
1579 OP_ALG_AAI_DK, OP_ALG_AS_INITFINAL,
1580 ICV_CHECK_DISABLE, dir);
1581 SET_LABEL(p, skipkeys);
1583 SET_LABEL(p, skipkeys);
1584 ALG_OPERATION(p, cipherdata->algtype, cipherdata->algmode,
1585 OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE, dir);
1589 if (cipherdata->algmode == OP_ALG_AAI_CTR)
1590 SEQLOAD(p, CONTEXT1, 16, ivlen, 0);
1592 SEQLOAD(p, CONTEXT1, 0, ivlen, 0);
1595 * authenticate auth_hdr data
1597 MATHB(p, MATH0, ADD, ZERO, VSEQINSZ, 4, 0);
1598 SEQFIFOLOAD(p, MSG2, 0, VLF);
1601 * Prepare the length of the data to be both encrypted/decrypted
1602 * and authenticated/checked
1604 MATHB(p, SEQINSZ, SUB, MATH2, VSEQINSZ, 4, 0);
1605 if (dir == DIR_DEC) {
1606 MATHB(p, VSEQINSZ, SUB, trunc_len, VSEQINSZ, 4, IMMED2);
1607 data_type = MSGINSNOOP;
1609 data_type = MSGOUTSNOOP;
1612 MATHB(p, VSEQINSZ, ADD, ZERO, VSEQOUTSZ, 4, 0);
1614 /* Prepare for writing the output frame */
1615 SEQFIFOSTORE(p, MSG, 0, 0, VLF);
1618 /* Check if there is no auth-tail */
1619 MATHB(p, MATH2, ADD, ZERO, MATH2, 4, 0);
1620 p_no_auth_tail = JUMP(p, no_auth_tail, LOCAL_JUMP, ALL_TRUE, MATH_Z);
1623 * Read input plain/cipher text, encrypt/decrypt & auth & write
1626 SEQFIFOLOAD(p, data_type, 0, VLF | LAST1 | FLUSH1);
1628 /* Authenticate auth tail */
1629 MATHB(p, MATH2, ADD, ZERO, VSEQINSZ, 4, 0);
1630 SEQFIFOLOAD(p, MSG2, 0, VLF | LAST2);
1632 /* Jump to process icv */
1633 p_proc_icv = JUMP(p, proc_icv, LOCAL_JUMP, ALL_FALSE, MATH_Z);
1635 SET_LABEL(p, no_auth_tail);
1637 SEQFIFOLOAD(p, data_type, 0, VLF | LAST1 | LAST2 | FLUSH1);
1639 SET_LABEL(p, proc_icv);
1642 /* Finally, write the ICV */
1643 SEQSTORE(p, CONTEXT2, 0, trunc_len, 0);
1645 /* Read the ICV to check */
1646 SEQFIFOLOAD(p, ICV2, trunc_len, LAST2);
1648 PATCH_JUMP(p, pkeyjmp, keyjmp);
1649 PATCH_JUMP(p, pskipkeys, skipkeys);
1650 PATCH_JUMP(p, p_no_auth_tail, no_auth_tail);
1651 PATCH_JUMP(p, p_proc_icv, proc_icv);
1652 return PROGRAM_FINALIZE(p);
1655 #endif /* __DESC_IPSEC_H__ */