Skip to content

Commit 5aecaeb

Browse files
committed
- Move crypto-accelerator.p4 to include dir (clean runp4tests.sh execution)
- Comments to describe assumed crypto engine functionality - set_error_action() method to control disposition of packets on error
1 parent b392e1f commit 5aecaeb

File tree

3 files changed

+139
-80
lines changed

3 files changed

+139
-80
lines changed

examples/crypto-accelerator.p4

-78
This file was deleted.
+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Copyright 2022 Advanced Micro Devices, Inc
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/// Crypto accelerator Extern
18+
enum bit<8> crypto_algorithm_e {
19+
AES_GCM = 1
20+
}
21+
enum bit<8> crypto_results_e {
22+
SUCCESS = 0,
23+
AUTH_FAILURE = 1,
24+
HW_ERROR = 2
25+
}
26+
27+
enum bit<8> crypto_error_action_e {
28+
NO_ACTION = 0,
29+
DROP_PACKET = 1,
30+
SEND_TO_PORT = 2
31+
}
32+
33+
#define ICV_AFTER_PAYLOAD ((int<32>)-1)
34+
35+
/// The crypto_accelerator engine used in this example uses AES-GCM algorithm.
36+
/// It is assumed to be agnostic to wire protocols i.e. does not understand protocol
37+
/// specific headers like ESP, AH etc
38+
///
39+
/// Note that crypto accelerator does not modify the packet outside the payload area and ICV
40+
/// Any wire-protocol header, trailer add/remove is handled by P4 pipeline
41+
/// The engine does not perform additional functions such as anti-replay protection, it
42+
/// is done in P4 pipeline
43+
///
44+
/// Crypto Engine takes the following inputs:
45+
/// - key, iv, icv_location/size, enable_auth, auth_data (aka AAD), payload location
46+
/// In some protocols AAD can be present in the packet, in that case AAD can be specified
47+
/// as offset/len within the packet.
48+
/// On encrypt operation, icv_location/size indicates that icv is inserted in the
49+
/// packet at the specified packet offset
50+
/// On decrypt operation, icv_location and size is used for auth validation
51+
///
52+
/// Example:
53+
/// Encrypt operation:
54+
/// Parameters passed : key, iv, icv_location/size, enable_auth, auth_data
55+
/// Packet presented to the engine -
56+
/// +------------------+------------------------+-----------+
57+
/// | Headers not to | Encryption protocol | payload |
58+
/// | Encrypted | headers (E.g Esp, AH) | |
59+
/// +------------------+------------------------+-----------+
60+
/// Packet after Encryption:
61+
/// +------------------+------------------------+-----------+-----------+
62+
/// | Headers not to | Encryption protocol | Encrypted | ICV (opt) |
63+
/// | Encrypted | headers (E.g Esp, AH) | Payload | |
64+
/// +------------------+------------------------+-----------+-----------+
65+
/// ICV can be inserted either before or right after the encrypted payload
66+
/// as specified by icv_location/size
67+
/// Results: Success, Hardware Error
68+
///
69+
/// Decrypt operation:
70+
/// Parameters passed : key, iv, icv_location/size, enable_auth, auth_data
71+
/// Packet presented to the engine -
72+
/// +------------------+------------------------+-----------+-----+
73+
/// | Headers not to | Encryption protocol | Encrypted | ICV |
74+
/// | Encrypted | headers (E.g Esp, AH) | Payload | |
75+
/// +------------------+------------------------+-----------+-----+
76+
/// Packet after decrytion:
77+
/// +------------------+------------------------+-----------+-----+
78+
/// | Headers not to | Encryption protocol | cleartext | ICV |
79+
/// | Encrypted | headers (E.g Esp, AH) | Payload | |
80+
/// +------------------+------------------------+-----------+-----+
81+
/// Results: Success, Auth Failure, Hardware Error
82+
///
83+
extern crypto_accelerator {
84+
/// constructor
85+
/// Some methods provided in this object may be specific to an algorithm used.
86+
/// Compiler may be able to check and warn/error when incorrect methods are used
87+
crypto_accelerator(crypto_algorithm_e algo);
88+
89+
90+
// security association index for this security session
91+
// Some implementations do not need it.. in that case this method should result in no-op
92+
void set_sa_index<T>(in T sa_index);
93+
94+
// Set the initialization data based on protocol used. E.g. salt, random number/ counter for ipsec
95+
void set_iv<T>(in T iv);
96+
void set_key<T,S>(in T key, in S key_size); // 128, 192, 256
97+
98+
// The format of the auth data is not specified/mandated by this object definition
99+
// If it is part of the packet, it can be specified using offset/len mothods below
100+
void set_auth_data_offset<T>(in T offset);
101+
void set_auth_data_len<T>(in T len);
102+
103+
// Alternatively: Following API can be used to consturct the auth_data and
104+
// provide it to the engine.
105+
void add_auth_data<H>(in H auth_data);
106+
107+
// Auth trailer aka ICV is added by the engine after doing encryption operation
108+
// Specify icv location - when a wire protocol wants to add ICV in a specific location (e.g. AH)
109+
// The following apis can be used to specify the location of ICV in the packet
110+
// A special offset indicates ICV is after the payload
111+
void set_icv_offset<T>(in T offset);
112+
void set_icv_len<L>(in L len);
113+
114+
// setup payload to be encrypted/decrypted
115+
void set_payload_offset<T>(in T offset);
116+
void set_payload_len<T>(in T len);
117+
118+
// crypto accelerator runs at the end of the pipeline (after deparser), the following
119+
// methods will enable the accelerator to run encrypt/decrypt operations
120+
// enable_auth flag enables authentication check for decrypt. For encrypt operation,
121+
// auth data computed, is added to specified icv_offset/len
122+
void enable_encrypt<T>(in T enable_auth);
123+
void enable_decrypt<T>(in T enable_auth);
124+
125+
// disable crypto engine. Between enable and disable methods,
126+
// whichever method is called last overrides the previous calls
127+
void disable();
128+
129+
crypto_results_e get_results(); // get results of the previous operation
130+
131+
// set_error_action() indicates behavior on encoutering an error
132+
// Default action is NO_ACTION i.e. report error via get_results()
133+
// Certain actions may need action parameters, e.g send_to_port
134+
void set_error_action<T>(crypto_error_action_e error_action, in T action_param);
135+
}

examples/ipsec-acc.p4

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
/// p4test --Wdisable='unused' --top4 MidEndLast ./ipsec-acc.p4 2>&1 | tee make.out
17+
/// p4test --Wdisable='unused' --excludeMidendPasses Predication ./ipsec-acc.p4 2>&1 | tee make.out
1818

1919
/// IPSec tunnel mode example using crypto-accelerator extern object
20-
#include "./crypto-accelerator.p4"
20+
#include <core.p4>
21+
#include "../pna.p4"
22+
#include "./include/crypto-accelerator.p4"
2123

2224
// Helper Externs (could not find it in pna spec/existign code)
2325
// Vendor specific implementation to cause a packet to get recirculated

0 commit comments

Comments
 (0)