Skip to content

Commit c005d11

Browse files
committed
This example expands on the definition of crypto_accelerator (p4lang#53).
The example adds two methods for encrypt/decrypt that assumes that inline accelerators operate immediately on the packet (e.g. deparse, decrypt and reparse). Packet recirculation is not necessary for either inline method. The example shows the use of inline encrypt and decrypt, as well as how the crypto accelerator results can be used.
1 parent 8ecd83f commit c005d11

File tree

2 files changed

+478
-0
lines changed

2 files changed

+478
-0
lines changed

examples/crypto-inline.p4

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
#include <core.p4>
18+
#include "../pna.p4"
19+
20+
/// Crypto accelerator Extern
21+
enum bit<8> crypto_algorithm_e {
22+
AES_GCM = 1
23+
}
24+
enum bit<8> crypto_results_e {
25+
SUCCESS = 0,
26+
AUTH_FAILURE = 1,
27+
HW_ERROR = 2
28+
}
29+
30+
enum bit<2> crypto_mode_e {
31+
TUNNEL = 0,
32+
TRANSPORT = 1,
33+
TRANSPORT_NAT_T = 2
34+
}
35+
36+
// special offset value to indicate that ICV is after payload
37+
#define ICV_AFTER_PAYLOAD ((int<32>)-1)
38+
39+
extern crypto_accelerator {
40+
/// constructor
41+
/// Some methods provided in this object may be specific to an algorithm used.
42+
/// Compiler may be able to check and warn/error when incorrect methods are used
43+
crypto_accelerator(crypto_algorithm_e algo);
44+
45+
46+
// security association index for this security session
47+
// Some implementations do not need it.. in that case this method should result in no-op
48+
void set_sa_index<T>(in T sa_index);
49+
50+
// Set the initialization data based on protocol used. E.g. salt, random number/ counter for ipsec
51+
void set_iv<T>(in T iv);
52+
void set_key<T,S>(in T key, in S key_size); // 128, 192, 256
53+
54+
// authentication data format is protocol specific
55+
// Add this data as a header into the packet and provide its offset and length using the
56+
// following APIs
57+
// The format of the auth data is not specified/mandated by this object definition
58+
void set_auth_data_offset<T>(in T offset);
59+
void set_auth_data_len<T>(in T len);
60+
61+
// Alternatively: Following API can be used to consturct protocol specific auth_data and
62+
// provide it to the engine.
63+
void add_auth_data<H>(in H auth_data);
64+
65+
// Auth trailer aka ICV is added by the engine after doing encryption operation
66+
// Specify icv location - when a wire protocol wants to add ICV in a specific location (e.g. AH)
67+
// The following apis can be used to specify the location of ICV in the packet
68+
// special offset (TBD) indicates ICV is after the payload
69+
void set_icv_offset<T>(in T offset);
70+
void set_icv_len<L>(in L len);
71+
72+
// setup payload to be encrypted/decrypted
73+
void set_payload_offset<T>(in T offset);
74+
void set_payload_len<T>(in T len);
75+
76+
// operation
77+
// crypto accelerator runs at the end of the pipeline (after deparser), the following
78+
// methods will enable the accelerator to run encrypt/decrypt operations
79+
void enable_encrypt<T>(in T enable_auth);
80+
void enable_decrypt<T>(in T enable_auth);
81+
82+
// encrypt_inline runs immediately and returns control flow to the current pipeline
83+
// stage. The method is responsible for encrypting the payload appropriately, creating
84+
// the ESP header, calculating the payload offset and lengths, and reparsing the packet
85+
crypto_results_e encrypt_inline<T,S,I>(in crypto_mode_e mode,
86+
in T enable_auth,
87+
in bit<32> spi,
88+
in S seq,
89+
in I iv);
90+
91+
// decrypt_inline runs immediately and returns control flow to the current pipeline
92+
// stage. The method is responsible for decrypting the payload appropriately, removing
93+
// the ESP header, calculating the payload offset and lengths, and reparsing the packet
94+
crypto_results_e decrypt_inline<T,S>(in crypto_mode_e mode,
95+
in T enable_auth,
96+
in S seq);
97+
98+
// disable crypto engine
99+
void disable();
100+
101+
crypto_results_e get_results(); // get results of the previous operation
102+
}

0 commit comments

Comments
 (0)