5fcff2d1efca96d9a9f295903bb70c197a8d6f76
[openssl.git] / crypto / siphash / siphash_meth.c
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <stdarg.h>
11 #include <string.h>
12 #include <openssl/evp.h>
13 #include <openssl/err.h>
14 #include "internal/siphash.h"
15 #include "siphash_local.h"
16 #include "internal/evp_int.h"
17
18 /* local SIPHASH structure is actually a SIPHASH */
19
20 struct evp_mac_impl_st {
21     SIPHASH ctx;
22 };
23
24 static EVP_MAC_IMPL *siphash_new(void)
25 {
26     return OPENSSL_zalloc(sizeof(EVP_MAC_IMPL));
27 }
28
29 static void siphash_free(EVP_MAC_IMPL *sctx)
30 {
31     OPENSSL_free(sctx);
32 }
33
34 static EVP_MAC_IMPL *siphash_dup(const EVP_MAC_IMPL *ssrc)
35 {
36     EVP_MAC_IMPL *sdst;
37
38     sdst = siphash_new();
39     if (sdst == NULL)
40         return NULL;
41
42     *sdst = *ssrc;
43
44     return sdst;
45 }
46
47 static size_t siphash_size(EVP_MAC_IMPL *sctx)
48 {
49     return SipHash_hash_size(&sctx->ctx);
50 }
51
52 static int siphash_init(EVP_MAC_IMPL *sctx)
53 {
54     /* Not much to do here, actual initialization happens through controls */
55     return 1;
56 }
57
58 static int siphash_update(EVP_MAC_IMPL *sctx, const unsigned char *data,
59                        size_t datalen)
60 {
61     SipHash_Update(&sctx->ctx, data, datalen);
62     return 1;
63 }
64
65 static int siphash_final(EVP_MAC_IMPL *sctx, unsigned char *out)
66 {
67     size_t hlen = siphash_size(sctx);
68
69     return SipHash_Final(&sctx->ctx, out, hlen);
70 }
71
72 static int siphash_ctrl(EVP_MAC_IMPL *sctx, int cmd, va_list args)
73 {
74     switch (cmd) {
75     case EVP_MAC_CTRL_SET_SIZE:
76         {
77             size_t size = va_arg(args, size_t);
78
79             return SipHash_set_hash_size(&sctx->ctx, size);
80         }
81         break;
82     case EVP_MAC_CTRL_SET_KEY:
83         {
84             const unsigned char *key = va_arg(args, const unsigned char *);
85             size_t keylen = va_arg(args, size_t);
86
87             if (key == NULL || keylen != SIPHASH_KEY_SIZE)
88                 return 0;
89
90             return SipHash_Init(&sctx->ctx, key, 0, 0);
91         }
92         break;
93     default:
94         return -2;
95     }
96     return 1;
97 }
98
99 static int siphash_ctrl_int(EVP_MAC_IMPL *sctx, int cmd, ...)
100 {
101     int rv;
102     va_list args;
103
104     va_start(args, cmd);
105     rv = siphash_ctrl(sctx, cmd, args);
106     va_end(args);
107
108     return rv;
109 }
110
111 static int siphash_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
112 {
113     return siphash_ctrl_int(ctx, cmd, buf, buflen);
114 }
115
116 static int siphash_ctrl_str(EVP_MAC_IMPL *ctx,
117                             const char *type, const char *value)
118 {
119     if (value == NULL)
120         return 0;
121     if (strcmp(type, "digestsize") == 0) {
122         size_t hash_size = atoi(value);
123
124         return siphash_ctrl_int(ctx, EVP_MAC_CTRL_SET_SIZE, hash_size);
125     }
126     if (strcmp(type, "key") == 0)
127         return EVP_str2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
128                             value);
129     if (strcmp(type, "hexkey") == 0)
130         return EVP_hex2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
131                             value);
132     return -2;
133 }
134
135 const EVP_MAC siphash_meth = {
136     EVP_MAC_SIPHASH,
137     siphash_new,
138     siphash_dup,
139     siphash_free,
140     siphash_size,
141     siphash_init,
142     siphash_update,
143     siphash_final,
144     siphash_ctrl,
145     siphash_ctrl_str
146 };