build checksums for charon plugins
[strongswan.git] / src / libstrongswan / fips / fips_canister_start.c
1 /* ====================================================================
2 * Copyright (c) 2005 The OpenSSL Project. Rights for redistribution
3 * and usage in source and binary forms are granted according to the
4 * OpenSSL license.
5 */
6
7 #include <stdio.h>
8 #if defined(__DECC)
9 # include <c_asm.h>
10 # pragma __nostandard
11 #endif
12
13 #if !defined(POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION)
14 # if (defined(__sun) && (defined(__sparc) || defined(__sparcv9))) || \
15 (defined(__sgi) && (defined(__mips) || defined(mips))) || \
16 (defined(__osf__) && defined(__alpha)) || \
17 (defined(__linux) && (defined(__arm) || defined(__arm__))) || \
18 (defined(__i386) || defined(__i386__)) || \
19 (defined(__x86_64) || defined(__x86_64__)) || \
20 (defined(vax) || defined(__vax__))
21 # define POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION
22 # endif
23 #endif
24
25
26 #define FIPS_ref_point FIPS_text_start
27 /* Some compilers put string literals into a separate segment. As we
28 * are mostly interested to hash AES tables in .rodata, we declare
29 * reference points accordingly. In case you wonder, the values are
30 * big-endian encoded variable names, just to prevent these arrays
31 * from being merged by linker. */
32 const unsigned int FIPS_rodata_start[]=
33 { 0x46495053, 0x5f726f64, 0x6174615f, 0x73746172 };
34
35
36 /*
37 * I declare reference function as static in order to avoid certain
38 * pitfalls in -dynamic linker behaviour...
39 */
40 static void *instruction_pointer(void)
41 {
42 void *ret = NULL;
43
44 /* These are ABI-neutral CPU-specific snippets. ABI-neutrality means
45 * that they are designed to work under any OS running on particular
46 * CPU, which is why you don't find any #ifdef THIS_OR_THAT_OS in
47 * this function. */
48 #if defined(INSTRUCTION_POINTER_IMPLEMENTED)
49 INSTRUCTION_POINTER_IMPLEMENTED(ret);
50 #elif defined(__GNUC__) && __GNUC__>=2
51 # if defined(__alpha) || defined(__alpha__)
52 # define INSTRUCTION_POINTER_IMPLEMENTED
53 __asm __volatile ( "br %0,1f\n1:" : "=r"(ret) );
54 # elif defined(__i386) || defined(__i386__)
55 # define INSTRUCTION_POINTER_IMPLEMENTED
56 __asm __volatile ( "call 1f\n1: popl %0" : "=r"(ret) );
57 ret = (void *)((size_t)ret&~3UL); /* align for better performance */
58 # elif defined(__ia64) || defined(__ia64__)
59 # define INSTRUCTION_POINTER_IMPLEMENTED
60 __asm __volatile ( "mov %0=ip" : "=r"(ret) );
61 # elif defined(__hppa) || defined(__hppa__) || defined(__pa_risc)
62 # define INSTRUCTION_POINTER_IMPLEMENTED
63 __asm __volatile ( "blr %%r0,%0\n\tnop" : "=r"(ret) );
64 ret = (void *)((size_t)ret&~3UL); /* mask privilege level */
65 # elif defined(__mips) || defined(__mips__)
66 # define INSTRUCTION_POINTER_IMPLEMENTED
67 void *scratch;
68 __asm __volatile ( "move %1,$31\n\t" /* save ra */
69 "bal .+8; nop\n\t"
70 "move %0,$31\n\t"
71 "move $31,%1" /* restore ra */
72 : "=r"(ret),"=r"(scratch) );
73 # elif defined(__ppc__) || defined(__powerpc) || defined(__powerpc__) || \
74 defined(__POWERPC__) || defined(_POWER) || defined(__PPC__) || \
75 defined(__PPC64__) || defined(__powerpc64__)
76 # define INSTRUCTION_POINTER_IMPLEMENTED
77 void *scratch;
78 __asm __volatile ( "mfspr %1,8\n\t" /* save lr */
79 "bl .+4\n\t"
80 "mfspr %0,8\n\t" /* mflr ret */
81 "mtspr 8,%1" /* restore lr */
82 : "=r"(ret),"=r"(scratch) );
83 # elif defined(__sparc) || defined(__sparc__) || defined(__sparcv9)
84 # define INSTRUCTION_POINTER_IMPLEMENTED
85 void *scratch;
86 __asm __volatile ( "mov %%o7,%1\n\t"
87 "call .+8; nop\n\t"
88 "mov %%o7,%0\n\t"
89 "mov %1,%%o7"
90 : "=r"(ret),"=r"(scratch) );
91 # elif defined(__x86_64) || defined(__x86_64__)
92 # define INSTRUCTION_POINTER_IMPLEMENTED
93 __asm __volatile ( "leaq 0(%%rip),%0" : "=r"(ret) );
94 ret = (void *)((size_t)ret&~3UL); /* align for better performance */
95 # endif
96 #elif defined(__DECC) && defined(__alpha)
97 # define INSTRUCTION_POINTER_IMPLEMENTED
98 ret = (void *)(size_t)asm("br %v0,1f\n1:");
99 #elif defined(_MSC_VER) && defined(_M_IX86)
100 # undef INSTRUCTION_POINTER_IMPLEMENTED
101 void *scratch;
102 _asm {
103 call self
104 self: pop eax
105 mov scratch,eax
106 }
107 ret = (void *)((size_t)scratch&~3UL);
108 #endif
109 return ret;
110 }
111
112 /*
113 * This function returns pointer to an instruction in the vicinity of
114 * its entry point, but not outside this object module. This guarantees
115 * that sequestered code is covered...
116 */
117 void *FIPS_ref_point()
118 {
119 #if defined(INSTRUCTION_POINTER_IMPLEMENTED)
120 return instruction_pointer();
121 /* Below we essentially cover vendor compilers which do not support
122 * inline assembler... */
123 #elif defined(_AIX)
124 struct { void *ip,*gp,*env; } *p = (void *)instruction_pointer;
125 return p->ip;
126 #elif defined(_HPUX_SOURCE)
127 # if defined(__hppa) || defined(__hppa__)
128 struct { void *i[4]; } *p = (void *)FIPS_ref_point;
129
130 if (sizeof(p) == 8) /* 64-bit */
131 return p->i[2];
132 else if ((size_t)p & 2)
133 { p = (void *)((size_t)p&~3UL);
134 return p->i[0];
135 }
136 else
137 return (void *)p;
138 # elif defined(__ia64) || defined(__ia64__)
139 struct { unsigned long long ip,gp; } *p=(void *)instruction_pointer;
140 return (void *)(size_t)p->ip;
141 # endif
142 #elif (defined(__VMS) || defined(VMS)) && !(defined(vax) || defined(__vax__))
143 /* applies to both alpha and ia64 */
144 struct { unsigned __int64 opaque,ip; } *p=(void *)instruction_pointer;
145 return (void *)(size_t)p->ip;
146 #elif defined(__VOS__)
147 /* applies to both pa-risc and ia32 */
148 struct { void *dp,*ip,*gp; } *p = (void *)instruction_pointer;
149 return p->ip;
150 #elif defined(_WIN32)
151 # if defined(_WIN64) && defined(_M_IA64)
152 struct { void *ip,*gp; } *p = (void *)FIPS_ref_point;
153 return p->ip;
154 # else
155 return (void *)FIPS_ref_point;
156 # endif
157 /*
158 * In case you wonder why there is no #ifdef __linux. All Linux targets
159 * are GCC-based and therefore are covered by instruction_pointer above
160 * [well, some are covered by by the one below]...
161 */
162 #elif defined(POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION)
163 return (void *)instruction_pointer;
164 #else
165 return NULL;
166 #endif
167 }