1 | /* |
2 | * Copyright 2019 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 "cipher_aes_siv.h" |
11 | |
12 | static int aes_siv_initkey(void *vctx, const unsigned char *key, size_t keylen) |
13 | { |
14 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
15 | SIV128_CONTEXT *sctx = &ctx->siv; |
16 | size_t klen = keylen / 2; |
17 | |
18 | switch (klen) { |
19 | case 16: |
20 | ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-128-CBC" , "" ); |
21 | ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-128-CTR" , "" ); |
22 | break; |
23 | case 24: |
24 | ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-192-CBC" , "" ); |
25 | ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-192-CTR" , "" ); |
26 | break; |
27 | case 32: |
28 | ctx->cbc = EVP_CIPHER_fetch(NULL, "AES-256-CBC" , "" ); |
29 | ctx->ctr = EVP_CIPHER_fetch(NULL, "AES-256-CTR" , "" ); |
30 | break; |
31 | default: |
32 | return 0; |
33 | } |
34 | /* |
35 | * klen is the length of the underlying cipher, not the input key, |
36 | * which should be twice as long |
37 | */ |
38 | return CRYPTO_siv128_init(sctx, key, klen, ctx->cbc, ctx->ctr); |
39 | } |
40 | |
41 | static int aes_siv_settag(void *vctx, const unsigned char *tag, size_t tagl) |
42 | { |
43 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
44 | SIV128_CONTEXT *sctx = &ctx->siv; |
45 | |
46 | return CRYPTO_siv128_set_tag(sctx, tag, tagl); |
47 | } |
48 | |
49 | static void aes_siv_setspeed(void *vctx, int speed) |
50 | { |
51 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
52 | SIV128_CONTEXT *sctx = &ctx->siv; |
53 | |
54 | CRYPTO_siv128_speed(sctx, (int)speed); |
55 | } |
56 | |
57 | static void aes_siv_cleanup(void *vctx) |
58 | { |
59 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
60 | SIV128_CONTEXT *sctx = &ctx->siv; |
61 | |
62 | CRYPTO_siv128_cleanup(sctx); |
63 | EVP_CIPHER_free(ctx->cbc); |
64 | EVP_CIPHER_free(ctx->ctr); |
65 | } |
66 | |
67 | static int aes_siv_cipher(void *vctx, unsigned char *out, |
68 | const unsigned char *in, size_t len) |
69 | { |
70 | PROV_AES_SIV_CTX *ctx = (PROV_AES_SIV_CTX *)vctx; |
71 | SIV128_CONTEXT *sctx = &ctx->siv; |
72 | |
73 | /* EncryptFinal or DecryptFinal */ |
74 | if (in == NULL) |
75 | return CRYPTO_siv128_finish(sctx) == 0; |
76 | |
77 | /* Deal with associated data */ |
78 | if (out == NULL) |
79 | return (CRYPTO_siv128_aad(sctx, in, len) == 1); |
80 | |
81 | if (ctx->enc) |
82 | return CRYPTO_siv128_encrypt(sctx, in, out, len) > 0; |
83 | |
84 | return CRYPTO_siv128_decrypt(sctx, in, out, len) > 0; |
85 | } |
86 | |
87 | static const PROV_CIPHER_HW_AES_SIV aes_siv_hw = |
88 | { |
89 | aes_siv_initkey, |
90 | aes_siv_cipher, |
91 | aes_siv_setspeed, |
92 | aes_siv_settag, |
93 | aes_siv_cleanup |
94 | }; |
95 | |
96 | const PROV_CIPHER_HW_AES_SIV *PROV_CIPHER_HW_aes_siv(size_t keybits) |
97 | { |
98 | return &aes_siv_hw; |
99 | } |
100 | |