7d67d32d6c4f11431419bc04e9bad956c7307333
[openssl.git] / fips / fips_canister.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 const void         *FIPS_text_start(void);
14 const void         *FIPS_text_end(void);
15
16 #include "e_os.h"
17
18 #if !defined(POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION)
19 # if    (defined(__sun) && (defined(__sparc) || defined(__sparcv9)))    || \
20         (defined(__sgi) && (defined(__mips) || defined(mips)))          || \
21         (defined(__osf__) && defined(__alpha))                          || \
22         (defined(__linux) && (defined(__arm) || defined(__arm__)))      || \
23         (defined(__i386) || defined(__i386__))                          || \
24         (defined(__x86_64) || defined(__x86_64__))                      || \
25         (defined(vax) || defined(__vax__))
26 #  define POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION
27 # endif
28 #endif
29
30 #if !defined(FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE)
31 # if    (defined(__ANDROID__) && (defined(__arm__) || defined(__arm)    || \
32                                   defined(__i386__)|| defined(__i386))) || \
33         (defined(__vxworks)   && (defined(__ppc__) || defined(__ppc)    || \
34                                   defined(__mips__)|| defined(__mips))) || \
35         (defined(__linux)     && ((defined(__PPC__) && !defined(__PPC64__)) || \
36                                   defined(__arm__) || defined(__arm)))  || \
37         (defined(_WIN32)      && defined(_MSC_VER))
38 #  define FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE
39 # endif
40 #endif
41
42 #if defined(__xlC__) && __xlC__>=0x600 && (defined(_POWER) || defined(_ARCH_PPC))
43 static void *instruction_pointer_xlc(void);
44 # pragma mc_func instruction_pointer_xlc {\
45         "7c0802a6"      /* mflr r0  */  \
46         "48000005"      /* bl   $+4 */  \
47         "7c6802a6"      /* mflr r3  */  \
48         "7c0803a6"      /* mtlr r0  */  }
49 # pragma reg_killed_by instruction_pointer_xlc gr0 gr3
50 # define INSTRUCTION_POINTER_IMPLEMENTED(ret) (ret=instruction_pointer_xlc());
51 #endif
52
53 #ifdef FIPS_START
54 # define FIPS_ref_point FIPS_text_start
55 # ifdef FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE
56 #  define instruction_pointer   FIPS_text_startX
57 # endif
58 /* Some compilers put string literals into a separate segment. As we
59  * are mostly interested to hash AES tables in .rodata, we declare
60  * reference points accordingly. In case you wonder, the values are
61  * big-endian encoded variable names, just to prevent these arrays
62  * from being merged by linker. */
63 # if defined(_MSC_VER)
64 #  pragma code_seg("fipstx")
65 #  pragma code_seg()
66    __declspec(allocate("fipstx"))
67 const unsigned int FIPS_text_startX[]=
68         { 0x46495053, 0x5f746578, 0x745f7374, 0x61727458 };
69 #  pragma const_seg("fipsro$a")
70 #  pragma const_seg()
71    __declspec(allocate("fipsro$a"))
72 # endif
73 const unsigned int FIPS_rodata_start[]=
74         { 0x46495053, 0x5f726f64, 0x6174615f, 0x73746172 };
75 #else
76 # define FIPS_ref_point FIPS_text_end
77 # ifdef FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE
78 #  define instruction_pointer   FIPS_text_endX
79 # endif
80 # if defined(_MSC_VER)
81 #  pragma code_seg("fipstx$z")
82 #  pragma code_seg()
83    __declspec(allocate("fipstx$z"))
84 const unsigned int FIPS_text_endX[]=
85         { 0x46495053, 0x5f746578, 0x745f656e, 0x64585b5d };
86 #  pragma const_seg("fipsro$z")
87 #  pragma const_seg()
88    __declspec(allocate("fipsro$z"))
89 # endif
90 const unsigned int FIPS_rodata_end[]=
91         { 0x46495053, 0x5f726f64, 0x6174615f, 0x656e645b };
92 #endif
93
94 #if !defined(_MSC_VER) || !defined(instruction_pointer)
95 /*
96  * I declare reference function as static in order to avoid certain
97  * pitfalls in -dynamic linker behaviour...
98  */
99 static void *instruction_pointer(void)
100 { void *ret=NULL;
101 /* These are ABI-neutral CPU-specific snippets. ABI-neutrality means
102  * that they are designed to work under any OS running on particular
103  * CPU, which is why you don't find any #ifdef THIS_OR_THAT_OS in
104  * this function. */
105 #if     defined(INSTRUCTION_POINTER_IMPLEMENTED)
106     INSTRUCTION_POINTER_IMPLEMENTED(ret);
107 #elif   defined(__GNUC__) && __GNUC__>=2
108 # if    defined(__alpha) || defined(__alpha__)
109 #   define INSTRUCTION_POINTER_IMPLEMENTED
110     __asm __volatile (  "br     %0,1f\n1:" : "=r"(ret) );
111 # elif  defined(__i386) || defined(__i386__)
112 #   define INSTRUCTION_POINTER_IMPLEMENTED
113     __asm __volatile (  "call 1f\n1:    popl %0" : "=r"(ret) );
114     ret = (void *)((size_t)ret&~3UL); /* align for better performance */
115 # elif  defined(__ia64) || defined(__ia64__)
116 #   define INSTRUCTION_POINTER_IMPLEMENTED
117     __asm __volatile (  "mov    %0=ip" : "=r"(ret) );
118 # elif  defined(__hppa) || defined(__hppa__) || defined(__pa_risc)
119 #   define INSTRUCTION_POINTER_IMPLEMENTED
120     __asm __volatile (  "blr    %%r0,%0\n\tnop" : "=r"(ret) );
121     ret = (void *)((size_t)ret&~3UL); /* mask privilege level */
122 # elif  defined(__mips) || defined(__mips__)
123 #   define INSTRUCTION_POINTER_IMPLEMENTED
124     void *scratch;
125     __asm __volatile (  "move   %1,$31\n\t"     /* save ra */
126                         "bal    .+8; nop\n\t"
127                         "move   %0,$31\n\t"
128                         "move   $31,%1"         /* restore ra */
129                         : "=r"(ret),"=r"(scratch) );
130 # elif  defined(__ppc__) || defined(__ppc) || \
131         defined(__powerpc) || defined(__powerpc__) || \
132         defined(__POWERPC__) || defined(_POWER) || defined(__PPC__) || \
133         defined(__PPC64__) || defined(__ppc64__) || defined(__powerpc64__)
134 #   define INSTRUCTION_POINTER_IMPLEMENTED
135     void *scratch;
136     __asm __volatile (  "mfspr  %1,8\n\t"       /* save lr */
137                         "bl     $+4\n\t"
138                         "mfspr  %0,8\n\t"       /* mflr ret */
139                         "mtspr  8,%1"           /* restore lr */
140                         : "=r"(ret),"=r"(scratch) );
141 # elif  defined(__s390__) || defined(__s390x__)
142 #   define INSTRUCTION_POINTER_IMPLEMENTED
143     __asm __volatile (  "bras   %0,1f\n1:" : "=r"(ret) );
144     ret = (void *)((size_t)ret&~3UL);
145 # elif  defined(__sparc) || defined(__sparc__) || defined(__sparcv9)
146 #   define INSTRUCTION_POINTER_IMPLEMENTED
147     void *scratch;
148     __asm __volatile (  "mov    %%o7,%1\n\t"
149                         "call   .+8; nop\n\t"
150                         "mov    %%o7,%0\n\t"
151                         "mov    %1,%%o7"
152                         : "=r"(ret),"=r"(scratch) );
153 # elif  defined(__x86_64) || defined(__x86_64__)
154 #   define INSTRUCTION_POINTER_IMPLEMENTED
155     __asm __volatile (  "leaq   0(%%rip),%0" : "=r"(ret) );
156     ret = (void *)((size_t)ret&~3UL); /* align for better performance */
157 # elif defined(__arm) || defined(__arm__)
158 #   define INSTRUCTION_POINTER_IMPLEMENTED
159     __asm __volatile (  "sub    %0,pc,#8" : "=r"(ret) );
160 # endif
161 #elif   defined(__DECC) && defined(__alpha)
162 #   define INSTRUCTION_POINTER_IMPLEMENTED
163     ret = (void *)(size_t)asm("br %v0,1f\n1:");
164 #elif   defined(_MSC_VER) && defined(_M_IX86)
165 #   define INSTRUCTION_POINTER_IMPLEMENTED
166     void *scratch;
167     _asm {
168             call    self
169     self:   pop     eax
170             mov     scratch,eax
171          }
172     ret = (void *)((size_t)scratch&~3UL);
173 #endif
174   return ret;
175 }
176 #endif
177
178 /*
179  * This function returns pointer to an instruction in the vicinity of
180  * its entry point, but not outside this object module. This guarantees
181  * that sequestered code is covered...
182  */
183 const void *FIPS_ref_point()
184 {
185 #if     defined(FIPS_REF_POINT_IS_CROSS_COMPILER_AWARE)
186 # if defined(__thumb__) || defined(__thumb)
187     return (void *)((size_t)instruction_pointer&~1);
188 # else
189     return (void *)instruction_pointer;
190 # endif
191 #elif   defined(INSTRUCTION_POINTER_IMPLEMENTED)
192     return instruction_pointer();
193 /* Below we essentially cover vendor compilers which do not support
194  * inline assembler... */
195 #elif   defined(_AIX)
196     struct { void *ip,*gp,*env; } *p = (void *)instruction_pointer;
197     return p->ip;
198 #elif   defined(_HPUX_SOURCE)
199 # if    defined(__hppa) || defined(__hppa__)
200     struct { void *i[4]; } *p = (void *)FIPS_ref_point;
201
202     if (sizeof(p) == 8) /* 64-bit */
203         return p->i[2];
204     else if ((size_t)p & 2)
205     {   p = (void *)((size_t)p&~3UL);
206         return p->i[0];
207     }
208     else
209         return (void *)p;
210 # elif  defined(__ia64) || defined(__ia64__)
211     struct { unsigned long long ip,gp; } *p=(void *)instruction_pointer;
212     return (void *)(size_t)p->ip;
213 # endif
214 #elif   (defined(__VMS) || defined(VMS)) && !(defined(vax) || defined(__vax__))
215     /* applies to both alpha and ia64 */
216     struct { unsigned __int64 opaque,ip; } *p=(void *)instruction_pointer;
217     return (void *)(size_t)p->ip;
218 #elif   defined(__VOS__)
219     /* applies to both pa-risc and ia32 */
220     struct { void *dp,*ip,*gp; } *p = (void *)instruction_pointer;
221     return p->ip;
222 #elif   defined(_WIN32)
223 # if    defined(_WIN64) && defined(_M_IA64)
224     struct { void *ip,*gp; } *p = (void *)FIPS_ref_point;
225     return p->ip;
226 # else
227     return (void *)FIPS_ref_point;
228 # endif
229 /*
230  * In case you wonder why there is no #ifdef __linux. All Linux targets
231  * are GCC-based and therefore are covered by instruction_pointer above
232  * [well, some are covered by by the one below]...
233  */ 
234 #elif   defined(POINTER_TO_FUNCTION_IS_POINTER_TO_1ST_INSTRUCTION)
235     return (void *)instruction_pointer;
236 #else
237     return NULL;
238 #endif
239 }