xref: /freebsd-src/crypto/openssl/providers/implementations/ciphers/ciphercommon_gcm_hw.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery #include "prov/ciphercommon.h"
11*b077aed3SPierre Pronchery #include "prov/ciphercommon_gcm.h"
12*b077aed3SPierre Pronchery 
13*b077aed3SPierre Pronchery 
ossl_gcm_setiv(PROV_GCM_CTX * ctx,const unsigned char * iv,size_t ivlen)14*b077aed3SPierre Pronchery int ossl_gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv, size_t ivlen)
15*b077aed3SPierre Pronchery {
16*b077aed3SPierre Pronchery     CRYPTO_gcm128_setiv(&ctx->gcm, iv, ivlen);
17*b077aed3SPierre Pronchery     return 1;
18*b077aed3SPierre Pronchery }
19*b077aed3SPierre Pronchery 
ossl_gcm_aad_update(PROV_GCM_CTX * ctx,const unsigned char * aad,size_t aad_len)20*b077aed3SPierre Pronchery int ossl_gcm_aad_update(PROV_GCM_CTX *ctx, const unsigned char *aad,
21*b077aed3SPierre Pronchery                         size_t aad_len)
22*b077aed3SPierre Pronchery {
23*b077aed3SPierre Pronchery     return CRYPTO_gcm128_aad(&ctx->gcm, aad, aad_len) == 0;
24*b077aed3SPierre Pronchery }
25*b077aed3SPierre Pronchery 
ossl_gcm_cipher_update(PROV_GCM_CTX * ctx,const unsigned char * in,size_t len,unsigned char * out)26*b077aed3SPierre Pronchery int ossl_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
27*b077aed3SPierre Pronchery                            size_t len, unsigned char *out)
28*b077aed3SPierre Pronchery {
29*b077aed3SPierre Pronchery     if (ctx->enc) {
30*b077aed3SPierre Pronchery         if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
31*b077aed3SPierre Pronchery             return 0;
32*b077aed3SPierre Pronchery     } else {
33*b077aed3SPierre Pronchery         if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
34*b077aed3SPierre Pronchery             return 0;
35*b077aed3SPierre Pronchery     }
36*b077aed3SPierre Pronchery     return 1;
37*b077aed3SPierre Pronchery }
38*b077aed3SPierre Pronchery 
ossl_gcm_cipher_final(PROV_GCM_CTX * ctx,unsigned char * tag)39*b077aed3SPierre Pronchery int ossl_gcm_cipher_final(PROV_GCM_CTX *ctx, unsigned char *tag)
40*b077aed3SPierre Pronchery {
41*b077aed3SPierre Pronchery     if (ctx->enc) {
42*b077aed3SPierre Pronchery         CRYPTO_gcm128_tag(&ctx->gcm, tag, GCM_TAG_MAX_SIZE);
43*b077aed3SPierre Pronchery         ctx->taglen = GCM_TAG_MAX_SIZE;
44*b077aed3SPierre Pronchery     } else {
45*b077aed3SPierre Pronchery         if (CRYPTO_gcm128_finish(&ctx->gcm, tag, ctx->taglen) != 0)
46*b077aed3SPierre Pronchery             return 0;
47*b077aed3SPierre Pronchery     }
48*b077aed3SPierre Pronchery     return 1;
49*b077aed3SPierre Pronchery }
50*b077aed3SPierre Pronchery 
ossl_gcm_one_shot(PROV_GCM_CTX * ctx,unsigned char * aad,size_t aad_len,const unsigned char * in,size_t in_len,unsigned char * out,unsigned char * tag,size_t tag_len)51*b077aed3SPierre Pronchery int ossl_gcm_one_shot(PROV_GCM_CTX *ctx, unsigned char *aad, size_t aad_len,
52*b077aed3SPierre Pronchery                       const unsigned char *in, size_t in_len,
53*b077aed3SPierre Pronchery                       unsigned char *out, unsigned char *tag, size_t tag_len)
54*b077aed3SPierre Pronchery {
55*b077aed3SPierre Pronchery     int ret = 0;
56*b077aed3SPierre Pronchery 
57*b077aed3SPierre Pronchery     /* Use saved AAD */
58*b077aed3SPierre Pronchery     if (!ctx->hw->aadupdate(ctx, aad, aad_len))
59*b077aed3SPierre Pronchery         goto err;
60*b077aed3SPierre Pronchery     if (!ctx->hw->cipherupdate(ctx, in, in_len, out))
61*b077aed3SPierre Pronchery         goto err;
62*b077aed3SPierre Pronchery     ctx->taglen = GCM_TAG_MAX_SIZE;
63*b077aed3SPierre Pronchery     if (!ctx->hw->cipherfinal(ctx, tag))
64*b077aed3SPierre Pronchery         goto err;
65*b077aed3SPierre Pronchery     ret = 1;
66*b077aed3SPierre Pronchery 
67*b077aed3SPierre Pronchery err:
68*b077aed3SPierre Pronchery     return ret;
69*b077aed3SPierre Pronchery }
70