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