summaryrefslogtreecommitdiff
path: root/sdk/recipes/files/openssl/m68k-unknown-amigaos/providers/implementations/rands/seeding/rand_amiga.c
blob: 205d52729aa31bb1d478485237807e8c68b9fee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "internal/cryptlib.h"
#include <openssl/rand.h>
#include <openssl/sha.h>
#include "crypto/rand_pool.h"
#include "crypto/rand.h"
#include "prov/seeding.h"

#if defined(OPENSSL_SYS_AMIGAOS3) || defined(OPENSSL_SYS_AMIGAOS4)

# ifndef OPENSSL_RAND_SEED_OS
#  error "Unsupported seeding method configured; must be os"
# endif

#define __USE_INLINE__ 1

#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include <proto/exec.h>
#include <proto/timer.h>
#include <devices/timer.h>

/* Maximum number of attempts to get a delay of 1 microsecond that is not equal to 0 */
#define MAX_ATTEMPTS 1000

#ifdef __amigaos4__

#ifdef CreateMsgPort
#undef CreateMsgPort
#endif
#define CreateMsgPort() AllocSysObject(ASOT_PORT,NULL)
#ifdef DeleteMsgPort
#undef DeleteMsgPort
#endif
#define DeleteMsgPort(msgPort) FreeSysObject(ASOT_PORT,msgPort)
#ifdef CreateIORequest
#undef CreateIORequest
#endif
#define CreateIORequest(ioReplyPort,size) AllocSysObjectTags(ASOT_IOREQUEST,ASOIOR_ReplyPort,ioReplyPort,ASOIOR_Size,size,TAG_DONE)
#ifdef DeleteIORequest
#undef DeleteIORequest
#endif
#define DeleteIORequest(ioReq) FreeSysObject(ASOT_IOREQUEST,ioReq)

#else

#define GetInterface(a, b, c, d) 1
#define DropInterface(x)

/* OS3 has a different but compatible TimeVal definition */
struct TimeVal
{
    ULONG Seconds;
    ULONG Microseconds;
};

/* ... and ditto for TimeRequest */
struct TimeRequest
{
    struct IORequest Request;
    struct TimeVal Time;
};

#endif /* !__amigaos4__ */

size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
{
    unsigned char temp_buffer[SHA_DIGEST_LENGTH], data_buffer[SHA_DIGEST_LENGTH];
    struct MsgPort *port = NULL;
    size_t bytes_needed;
    size_t entropy_available = 0;
    struct TimeRequest *time_request = NULL;
#ifdef __amigaos4__
    struct IOStdReq *entropy_request = NULL;

    bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);

    if ((port = CreateMsgPort())
        && (entropy_request = (struct IOStdReq *)CreateIORequest(port, sizeof(*entropy_request))))
    {
        if (OpenDevice(TIMERNAME, UNIT_ENTROPY, (struct IORequest *)entropy_request, 0) == 0)
        {
            while(bytes_needed > 0)
            {
                entropy_request->io_Command = TR_READENTROPY;
                entropy_request->io_Data = &temp_buffer[0];
                entropy_request->io_Length = sizeof(temp_buffer);

                if (DoIO((struct IORequest *)entropy_request) == 0)
                {
                    unsigned char *buffer;
                    size_t bytes = (bytes_needed < sizeof(data_buffer) ? bytes_needed : sizeof(data_buffer));

                    buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
                    SHA1(&temp_buffer[0], sizeof(temp_buffer), &data_buffer[0]);
                    memcpy(buffer, data_buffer, bytes);
                    ossl_rand_pool_add_end(pool, bytes, 8 * bytes);
                    bytes_needed -= bytes;
                }
                else
                    break;
            }

            CloseDevice((struct IORequest *)entropy_request);
        }
    }

    DeleteIORequest((struct IORequest *)entropy_request);

    entropy_available = ossl_rand_pool_entropy_available(pool);
    if (entropy_available > 0)
        return entropy_available;
#endif /* __amigaos4__ */

    /* The following block will be used on "classic" machines. It does not generate
     * a high degree of randomness, but it does the job since RAND_poll is
     * called only once by OpenSSL to generate a 32 byte seed.
     */

    bytes_needed = ossl_rand_pool_bytes_needed(pool, 1);

    if (bytes_needed > 0
        && (port || (port = CreateMsgPort()))
        && (time_request = (struct TimeRequest *)CreateIORequest(port, sizeof(*time_request))))
    {
        if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)time_request, 0) == 0)
        {
            #if defined(__amigaos4__)
            struct TimerIFace *ITimer = NULL;
            #endif
            struct Device *TimerBase;

            if ((TimerBase = time_request->Request.io_Device)
            #if defined(__amigaos4__)
                && (ITimer = (struct TimerIFace *)GetInterface((struct Library *)TimerBase, "main", 1, NULL))
            #endif
            )
            {
                struct EClockVal curr_eclock;
                ULONG prev_ev_lo = 0;
                struct TimeVal tv;
                int i, attempt;
                BOOL aborted;

                ReadEClock(&curr_eclock);
                aborted = FALSE;

                while(!aborted && bytes_needed > 0)
                {
                    for(i = 0;
                        !aborted && i < (int)sizeof(temp_buffer) - (int)sizeof(ULONG);
                        i++)
                    {
                        attempt = 0;

                        /* Ask for a one microsecond delay and measure the time
                         * the delay actually took.
                         */
                        do
                        {
                            time_request->Request.io_Command = TR_ADDREQUEST;
                            time_request->Time.Seconds = 0;
                            time_request->Time.Microseconds = 1;

                            if (DoIO((struct IORequest *)time_request) == 0)
                            {
                                prev_ev_lo = curr_eclock.ev_lo;
                                ReadEClock(&curr_eclock);

                                attempt++;
                            }
                            else
                                aborted = TRUE;
                        } while(!aborted && prev_ev_lo == 0 && attempt < MAX_ATTEMPTS);

                        if (attempt >= MAX_ATTEMPTS)
                            aborted = TRUE;

                        /* Since we are going for randomness, ev_hi is irrelevant */
                        temp_buffer[i] = (unsigned char)(curr_eclock.ev_lo - prev_ev_lo);
                    }

                    GetSysTime(&tv);

                    if (sizeof(temp_buffer) > sizeof(ULONG))
                        *(ULONG *)&temp_buffer[sizeof(temp_buffer) - sizeof(ULONG)]
                            = tv.Microseconds;

                    /* Shuffle the bits around and specify that about
                     * one fourth of it adds to the entropy.
                     */
                    if (!aborted)
                    {
                        unsigned char *buffer;
                        size_t bytes = (bytes_needed < sizeof(data_buffer) ? bytes_needed : sizeof(data_buffer));

                        SHA1(&temp_buffer[0], sizeof(temp_buffer), &data_buffer[0]);

                        ossl_rand_pool_add_end(pool, bytes, (sizeof(data_buffer) / 4) * 8);
                        bytes_needed -= bytes;
                    }
                }
            }

            #if defined(__amigaos4__)
            DropInterface((struct Interface *)ITimer);
            #endif
            CloseDevice((struct IORequest *)time_request);
        }
    }

    DeleteIORequest((struct IORequest *)time_request);

    DeleteMsgPort(port);

    return ossl_rand_pool_entropy_available(pool);
}

int ossl_pool_add_nonce_data(RAND_POOL *pool)
{
    struct {
        pid_t pid;
        CRYPTO_THREAD_ID tid;
        uint64_t time;
    } data;

    /* Erase the entire structure including any padding */
    memset(&data, 0, sizeof(data));

    /*
     * Add process id, thread id, and a high resolution timestamp to
     * ensure that the nonce is unique with high probability for
     * different process instances.
     */
    data.pid = getpid();
    data.tid = CRYPTO_THREAD_get_current_id();
    /*XXX: can this be improved? */
    data.time = time(NULL);

    return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
}

int ossl_rand_pool_add_additional_data(RAND_POOL *pool)
{
    struct {
        int fork_id;
        CRYPTO_THREAD_ID tid;
        uint64_t time;
    } data;

    /* Erase the entire structure including any padding */
    memset(&data, 0, sizeof(data));

    /*
     * Add some noise from the thread id and a high resolution timer.
     * The fork_id adds some extra fork-safety.
     * The thread id adds a little randomness if the drbg is accessed
     * concurrently (which is the case for the <master> drbg).
     */
    data.fork_id = openssl_get_fork_id();
    data.tid = CRYPTO_THREAD_get_current_id();
    /*XXX: can this be improved? */
    data.time = (uint64_t) time(NULL);

    return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
}

int ossl_rand_pool_init(void)
{
    return 1;
}

void ossl_rand_pool_cleanup(void)
{
}

void ossl_rand_pool_keep_random_devices_open(int keep)
{
}

#endif /* OPENSSL_SYS_AMIGA */