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