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