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