xref: /openbmc/qemu/tests/tcg/aarch64/pauth-2.c (revision e346bcbf)
1 #include <stdint.h>
2 #include <assert.h>
3 
4 asm(".arch armv8.4-a");
5 
6 void do_test(uint64_t value)
7 {
8     uint64_t salt1, salt2;
9     uint64_t encode, decode;
10 
11     /*
12      * With TBI enabled and a 48-bit VA, there are 7 bits of auth,
13      * and so a 1/128 chance of encode = pac(value,key,salt) producing
14      * an auth for which leaves value unchanged.
15      * Iterate until we find a salt for which encode != value.
16      */
17     for (salt1 = 1; ; salt1++) {
18         asm volatile("pacda %0, %2" : "=r"(encode) : "0"(value), "r"(salt1));
19         if (encode != value) {
20             break;
21         }
22     }
23 
24     /* A valid salt must produce a valid authorization.  */
25     asm volatile("autda %0, %2" : "=r"(decode) : "0"(encode), "r"(salt1));
26     assert(decode == value);
27 
28     /*
29      * An invalid salt usually fails authorization, but again there
30      * is a chance of choosing another salt that works.
31      * Iterate until we find another salt which does fail.
32      */
33     for (salt2 = salt1 + 1; ; salt2++) {
34         asm volatile("autda %0, %2" : "=r"(decode) : "0"(encode), "r"(salt2));
35         if (decode != value) {
36             break;
37         }
38     }
39 
40     /* The VA bits, bit 55, and the TBI bits, should be unchanged.  */
41     assert(((decode ^ value) & 0xff80ffffffffffffull) == 0);
42 
43     /*
44      * Bits [54:53] are an error indicator based on the key used;
45      * the DA key above is keynumber 0, so error == 0b01.  Otherwise
46      * bit 55 of the original is sign-extended into the rest of the auth.
47      */
48     if ((value >> 55) & 1) {
49         assert(((decode >> 48) & 0xff) == 0b10111111);
50     } else {
51         assert(((decode >> 48) & 0xff) == 0b00100000);
52     }
53 }
54 
55 int main()
56 {
57     do_test(0);
58     do_test(-1);
59     do_test(0xda004acedeadbeefull);
60     return 0;
61 }
62