37cb286ebf37216e5f84b6501f113d4ba2aba93e
[oweals/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 int siphash_copy(EVP_MAC_IMPL *sdst, EVP_MAC_IMPL *ssrc)
35 {
36     *sdst = *ssrc;
37     return 1;
38 }
39
40 static size_t siphash_size(EVP_MAC_IMPL *sctx)
41 {
42     return SipHash_hash_size(&sctx->ctx);
43 }
44
45 static int siphash_init(EVP_MAC_IMPL *sctx)
46 {
47     /* Not much to do here, actual initialization happens through controls */
48     return 1;
49 }
50
51 static int siphash_update(EVP_MAC_IMPL *sctx, const unsigned char *data,
52                        size_t datalen)
53 {
54     SipHash_Update(&sctx->ctx, data, datalen);
55     return 1;
56 }
57
58 static int siphash_final(EVP_MAC_IMPL *sctx, unsigned char *out)
59 {
60     size_t hlen = siphash_size(sctx);
61
62     return SipHash_Final(&sctx->ctx, out, hlen);
63 }
64
65 static int siphash_ctrl(EVP_MAC_IMPL *sctx, int cmd, va_list args)
66 {
67     switch (cmd) {
68     case EVP_MAC_CTRL_SET_SIZE:
69         {
70             size_t size = va_arg(args, size_t);
71
72             return SipHash_set_hash_size(&sctx->ctx, size);
73         }
74         break;
75     case EVP_MAC_CTRL_SET_KEY:
76         {
77             const unsigned char *key = va_arg(args, const unsigned char *);
78             size_t keylen = va_arg(args, size_t);
79
80             if (key == NULL || keylen != SIPHASH_KEY_SIZE)
81                 return 0;
82
83             return SipHash_Init(&sctx->ctx, key, 0, 0);
84         }
85         break;
86     default:
87         return -2;
88     }
89     return 1;
90 }
91
92 static int siphash_ctrl_int(EVP_MAC_IMPL *sctx, int cmd, ...)
93 {
94     int rv;
95     va_list args;
96
97     va_start(args, cmd);
98     rv = siphash_ctrl(sctx, cmd, args);
99     va_end(args);
100
101     return rv;
102 }
103
104 static int siphash_ctrl_str_cb(void *ctx, int cmd, void *buf, size_t buflen)
105 {
106     return siphash_ctrl_int(ctx, cmd, buf, buflen);
107 }
108
109 static int siphash_ctrl_str(EVP_MAC_IMPL *ctx,
110                             const char *type, const char *value)
111 {
112     if (value == NULL)
113         return 0;
114     if (strcmp(type, "digestsize") == 0) {
115         size_t hash_size = atoi(value);
116
117         return siphash_ctrl_int(ctx, EVP_MAC_CTRL_SET_SIZE, hash_size);
118     }
119     if (strcmp(type, "key") == 0)
120         return EVP_str2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
121                             value);
122     if (strcmp(type, "hexkey") == 0)
123         return EVP_hex2ctrl(siphash_ctrl_str_cb, ctx, EVP_MAC_CTRL_SET_KEY,
124                             value);
125     return -2;
126 }
127
128 const EVP_MAC siphash_meth = {
129     EVP_MAC_SIPHASH,
130     siphash_new,
131     siphash_copy,
132     siphash_free,
133     siphash_size,
134     siphash_init,
135     siphash_update,
136     siphash_final,
137     siphash_ctrl,
138     siphash_ctrl_str
139 };