Add support for parameterized SipHash
[openssl.git] / crypto / o_time.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/e_os2.h>
11 #include <string.h>
12 #include <openssl/crypto.h>
13
14 #ifdef OPENSSL_SYS_VMS
15 # if __CRTL_VER >= 70000000 && \
16      (defined _POSIX_C_SOURCE || !defined _ANSI_C_SOURCE)
17 #  define VMS_GMTIME_OK
18 # endif
19 # ifndef VMS_GMTIME_OK
20 #  include <libdtdef.h>
21 #  include <lib$routines.h>
22 #  include <lnmdef.h>
23 #  include <starlet.h>
24 #  include <descrip.h>
25 #  include <stdlib.h>
26 # endif                         /* ndef VMS_GMTIME_OK */
27
28
29 /*
30  * Needed to pick up the correct definitions and declarations in some of the
31  * DEC C Header Files (*.H).
32  */
33 # define __NEW_STARLET 1
34
35 # if (defined(__alpha) || defined(__ia64))
36 #  include <iledef.h>
37 # else
38
39 /* VAX */
40 typedef struct _ile3 {          /* Copied from ILEDEF.H for Alpha   */
41 #  pragma __nomember_alignment
42     unsigned short int ile3$w_length;        /* Length of buffer in bytes */
43     unsigned short int ile3$w_code;          /* Item code value */
44     void *ile3$ps_bufaddr;                   /* Buffer address */
45     unsigned short int *ile3$ps_retlen_addr; /* Address of word for returned length */
46 } ILE3;
47 # endif   /* alpha || ia64    */
48 #endif    /* OPENSSL_SYS_VMS  */
49
50 struct tm *OPENSSL_gmtime(const time_t *timer, struct tm *result)
51 {
52     struct tm *ts = NULL;
53
54 #if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && (!defined(OPENSSL_SYS_VMS) || defined(gmtime_r)) && !defined(OPENSSL_SYS_MACOSX)
55     /*
56      * should return &data, but doesn't on some systems, so we don't even
57      * look at the return value
58      */
59     gmtime_r(timer, result);
60     ts = result;
61 #elif !defined(OPENSSL_SYS_VMS) || defined(VMS_GMTIME_OK)
62     ts = gmtime(timer);
63     if (ts == NULL)
64         return NULL;
65
66     memcpy(result, ts, sizeof(struct tm));
67     ts = result;
68 #endif
69 #if defined( OPENSSL_SYS_VMS) && !defined( VMS_GMTIME_OK)
70     if (ts == NULL) {
71         static $DESCRIPTOR(tabnam, "LNM$DCL_LOGICAL");
72         static $DESCRIPTOR(lognam, "SYS$TIMEZONE_DIFFERENTIAL");
73         char logvalue[256];
74         unsigned int reslen = 0;
75 # if __INITIAL_POINTER_SIZE == 64
76         ILEB_64 itemlist[2], *pitem;
77 # else
78         ILE3 itemlist[2], *pitem;
79 # endif
80         int status;
81         time_t t;
82
83
84         /*
85          * Setup an itemlist for the call to $TRNLNM - Translate Logical Name.
86          */
87         pitem = itemlist;
88
89 # if __INITIAL_POINTER_SIZE == 64
90         pitem->ileb_64$w_mbo = 1;
91         pitem->ileb_64$w_code = LNM$_STRING;
92         pitem->ileb_64$l_mbmo = -1;
93         pitem->ileb_64$q_length = sizeof (logvalue);
94         pitem->ileb_64$pq_bufaddr = logvalue;
95         pitem->ileb_64$pq_retlen_addr = (unsigned __int64 *) &reslen;
96         pitem++;
97         /* Last item of the item list is null terminated */
98         pitem->ileb_64$q_length = pitem->ileb_64$w_code = 0;
99 # else
100         pitem->ile3$w_length = sizeof (logvalue);
101         pitem->ile3$w_code = LNM$_STRING;
102         pitem->ile3$ps_bufaddr = logvalue;
103         pitem->ile3$ps_retlen_addr = (unsigned short int *) &reslen;
104         pitem++;
105         /* Last item of the item list is null terminated */
106         pitem->ile3$w_length = pitem->ile3$w_code = 0;
107 # endif
108
109
110         /* Get the value for SYS$TIMEZONE_DIFFERENTIAL */
111         status = sys$trnlnm(0, &tabnam, &lognam, 0, itemlist);
112         if (!(status & 1))
113             return NULL;
114         logvalue[reslen] = '\0';
115
116         t = *timer;
117
118         /* The following is extracted from the DEC C header time.h */
119         /*
120          **  Beginning in OpenVMS Version 7.0 mktime, time, ctime, strftime
121          **  have two implementations.  One implementation is provided
122          **  for compatibility and deals with time in terms of local time,
123          **  the other __utc_* deals with time in terms of UTC.
124          */
125         /*
126          * We use the same conditions as in said time.h to check if we should
127          * assume that t contains local time (and should therefore be
128          * adjusted) or UTC (and should therefore be left untouched).
129          */
130 # if __CRTL_VER < 70000000 || defined _VMS_V6_SOURCE
131         /* Get the numerical value of the equivalence string */
132         status = atoi(logvalue);
133
134         /* and use it to move time to GMT */
135         t -= status;
136 # endif
137
138         /* then convert the result to the time structure */
139
140         /*
141          * Since there was no gmtime_r() to do this stuff for us, we have to
142          * do it the hard way.
143          */
144         {
145             /*-
146              * The VMS epoch is the astronomical Smithsonian date,
147                if I remember correctly, which is November 17, 1858.
148                Furthermore, time is measure in tenths of microseconds
149                and stored in quadwords (64 bit integers).  unix_epoch
150                below is January 1st 1970 expressed as a VMS time.  The
151                following code was used to get this number:
152
153                #include <stdio.h>
154                #include <stdlib.h>
155                #include <lib$routines.h>
156                #include <starlet.h>
157
158                main()
159                {
160                  unsigned long systime[2];
161                  unsigned short epoch_values[7] =
162                    { 1970, 1, 1, 0, 0, 0, 0 };
163
164                  lib$cvt_vectim(epoch_values, systime);
165
166                  printf("%u %u", systime[0], systime[1]);
167                }
168             */
169             unsigned long unix_epoch[2] = { 1273708544, 8164711 };
170             unsigned long deltatime[2];
171             unsigned long systime[2];
172             struct vms_vectime {
173                 short year, month, day, hour, minute, second, centi_second;
174             } time_values;
175             long operation;
176
177             /*
178              * Turn the number of seconds since January 1st 1970 to an
179              * internal delta time. Note that lib$cvt_to_internal_time() will
180              * assume that t is signed, and will therefore break on 32-bit
181              * systems some time in 2038.
182              */
183             operation = LIB$K_DELTA_SECONDS;
184             status = lib$cvt_to_internal_time(&operation, &t, deltatime);
185
186             /*
187              * Add the delta time with the Unix epoch and we have the current
188              * UTC time in internal format
189              */
190             status = lib$add_times(unix_epoch, deltatime, systime);
191
192             /* Turn the internal time into a time vector */
193             status = sys$numtim(&time_values, systime);
194
195             /* Fill in the struct tm with the result */
196             result->tm_sec = time_values.second;
197             result->tm_min = time_values.minute;
198             result->tm_hour = time_values.hour;
199             result->tm_mday = time_values.day;
200             result->tm_mon = time_values.month - 1;
201             result->tm_year = time_values.year - 1900;
202
203             operation = LIB$K_DAY_OF_WEEK;
204             status = lib$cvt_from_internal_time(&operation,
205                                                 &result->tm_wday, systime);
206             result->tm_wday %= 7;
207
208             operation = LIB$K_DAY_OF_YEAR;
209             status = lib$cvt_from_internal_time(&operation,
210                                                 &result->tm_yday, systime);
211             result->tm_yday--;
212
213             result->tm_isdst = 0; /* There's no way to know... */
214
215             ts = result;
216         }
217     }
218 #endif
219     return ts;
220 }
221
222 /*
223  * Take a tm structure and add an offset to it. This avoids any OS issues
224  * with restricted date types and overflows which cause the year 2038
225  * problem.
226  */
227
228 #define SECS_PER_DAY (24 * 60 * 60)
229
230 static long date_to_julian(int y, int m, int d);
231 static void julian_to_date(long jd, int *y, int *m, int *d);
232 static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
233                       long *pday, int *psec);
234
235 int OPENSSL_gmtime_adj(struct tm *tm, int off_day, long offset_sec)
236 {
237     int time_sec, time_year, time_month, time_day;
238     long time_jd;
239
240     /* Convert time and offset into Julian day and seconds */
241     if (!julian_adj(tm, off_day, offset_sec, &time_jd, &time_sec))
242         return 0;
243
244     /* Convert Julian day back to date */
245
246     julian_to_date(time_jd, &time_year, &time_month, &time_day);
247
248     if (time_year < 1900 || time_year > 9999)
249         return 0;
250
251     /* Update tm structure */
252
253     tm->tm_year = time_year - 1900;
254     tm->tm_mon = time_month - 1;
255     tm->tm_mday = time_day;
256
257     tm->tm_hour = time_sec / 3600;
258     tm->tm_min = (time_sec / 60) % 60;
259     tm->tm_sec = time_sec % 60;
260
261     return 1;
262
263 }
264
265 int OPENSSL_gmtime_diff(int *pday, int *psec,
266                         const struct tm *from, const struct tm *to)
267 {
268     int from_sec, to_sec, diff_sec;
269     long from_jd, to_jd, diff_day;
270     if (!julian_adj(from, 0, 0, &from_jd, &from_sec))
271         return 0;
272     if (!julian_adj(to, 0, 0, &to_jd, &to_sec))
273         return 0;
274     diff_day = to_jd - from_jd;
275     diff_sec = to_sec - from_sec;
276     /* Adjust differences so both positive or both negative */
277     if (diff_day > 0 && diff_sec < 0) {
278         diff_day--;
279         diff_sec += SECS_PER_DAY;
280     }
281     if (diff_day < 0 && diff_sec > 0) {
282         diff_day++;
283         diff_sec -= SECS_PER_DAY;
284     }
285
286     if (pday)
287         *pday = (int)diff_day;
288     if (psec)
289         *psec = diff_sec;
290
291     return 1;
292
293 }
294
295 /* Convert tm structure and offset into julian day and seconds */
296 static int julian_adj(const struct tm *tm, int off_day, long offset_sec,
297                       long *pday, int *psec)
298 {
299     int offset_hms, offset_day;
300     long time_jd;
301     int time_year, time_month, time_day;
302     /* split offset into days and day seconds */
303     offset_day = offset_sec / SECS_PER_DAY;
304     /* Avoid sign issues with % operator */
305     offset_hms = offset_sec - (offset_day * SECS_PER_DAY);
306     offset_day += off_day;
307     /* Add current time seconds to offset */
308     offset_hms += tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;
309     /* Adjust day seconds if overflow */
310     if (offset_hms >= SECS_PER_DAY) {
311         offset_day++;
312         offset_hms -= SECS_PER_DAY;
313     } else if (offset_hms < 0) {
314         offset_day--;
315         offset_hms += SECS_PER_DAY;
316     }
317
318     /*
319      * Convert date of time structure into a Julian day number.
320      */
321
322     time_year = tm->tm_year + 1900;
323     time_month = tm->tm_mon + 1;
324     time_day = tm->tm_mday;
325
326     time_jd = date_to_julian(time_year, time_month, time_day);
327
328     /* Work out Julian day of new date */
329     time_jd += offset_day;
330
331     if (time_jd < 0)
332         return 0;
333
334     *pday = time_jd;
335     *psec = offset_hms;
336     return 1;
337 }
338
339 /*
340  * Convert date to and from julian day Uses Fliegel & Van Flandern algorithm
341  */
342 static long date_to_julian(int y, int m, int d)
343 {
344     return (1461 * (y + 4800 + (m - 14) / 12)) / 4 +
345         (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 -
346         (3 * ((y + 4900 + (m - 14) / 12) / 100)) / 4 + d - 32075;
347 }
348
349 static void julian_to_date(long jd, int *y, int *m, int *d)
350 {
351     long L = jd + 68569;
352     long n = (4 * L) / 146097;
353     long i, j;
354
355     L = L - (146097 * n + 3) / 4;
356     i = (4000 * (L + 1)) / 1461001;
357     L = L - (1461 * i) / 4 + 31;
358     j = (80 * L) / 2447;
359     *d = L - (2447 * j) / 80;
360     L = j / 11;
361     *m = j + 2 - (12 * L);
362     *y = 100 * (n - 49) + i + L;
363 }