Update copyright year
[openssl.git] / providers / implementations / ciphers / cipher_aes_gcm_hw_ppc.inc
1 /*
2  * Copyright 2001-2022 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 /*-
11  * PPC support for AES GCM.
12  * This file is included by cipher_aes_gcm_hw.c
13  */
14
15 static int aes_ppc_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
16                                size_t keylen)
17 {
18     PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
19     AES_KEY *ks = &actx->ks.ks;
20
21     GCM_HW_SET_KEY_CTR_FN(ks, aes_p8_set_encrypt_key, aes_p8_encrypt,
22                           aes_p8_ctr32_encrypt_blocks);
23     return 1;
24 }
25
26
27 extern size_t ppc_aes_gcm_encrypt(const unsigned char *in, unsigned char *out, size_t len,
28                                   const void *key, unsigned char ivec[16], u64 *Xi);
29 extern size_t ppc_aes_gcm_decrypt(const unsigned char *in, unsigned char *out, size_t len,
30                                   const void *key, unsigned char ivec[16], u64 *Xi);
31
32 static inline u32 UTO32(unsigned char *buf)
33 {
34     return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) | ((u32) buf[2] << 8) | ((u32) buf[3]);
35 }
36
37 static inline u32 add32TOU(unsigned char buf[4], u32 n)
38 {
39     u32 r;
40
41     r = UTO32(buf);
42     r += n;
43     buf[0] = (unsigned char) (r >> 24) & 0xFF;
44     buf[1] = (unsigned char) (r >> 16) & 0xFF;
45     buf[2] = (unsigned char) (r >> 8) & 0xFF;
46     buf[3] = (unsigned char) r & 0xFF;
47     return r;
48 }
49
50 static size_t aes_p10_gcm_crypt(const unsigned char *in, unsigned char *out, size_t len,
51                                 const void *key, unsigned char ivec[16], u64 *Xi, int encrypt)
52 {
53     int s = 0;
54     int ndone = 0;
55     int ctr_reset = 0;
56     u64 blocks_unused;
57     u64 nb = len / 16;
58     u64 next_ctr = 0;
59     unsigned char ctr_saved[12];
60
61     memcpy(ctr_saved, ivec, 12);
62
63     while (nb) {
64         blocks_unused = (u64) 0xffffffffU + 1 - (u64) UTO32 (ivec + 12);
65         if (nb > blocks_unused) {
66             len = blocks_unused * 16;
67             nb -= blocks_unused;
68             next_ctr = blocks_unused;
69             ctr_reset = 1;
70         } else {
71             len = nb * 16;
72             next_ctr = nb;
73             nb = 0;
74         }
75
76         s = encrypt ? ppc_aes_gcm_encrypt(in, out, len, key, ivec, Xi)
77                     : ppc_aes_gcm_decrypt(in, out, len, key, ivec, Xi);
78
79         /* add counter to ivec */
80         add32TOU(ivec + 12, (u32) next_ctr);
81         if (ctr_reset) {
82             ctr_reset = 0;
83             in += len;
84             out += len;
85         }
86         memcpy(ivec, ctr_saved, 12);
87         ndone += s;
88     }
89
90     return ndone;
91 }
92
93 size_t ppc_aes_gcm_encrypt_wrap(const unsigned char *in, unsigned char *out, size_t len,
94                                 const void *key, unsigned char ivec[16], u64 *Xi)
95 {
96     return aes_p10_gcm_crypt(in, out, len, key, ivec, Xi, 1);
97 }
98
99 size_t ppc_aes_gcm_decrypt_wrap(const unsigned char *in, unsigned char *out, size_t len,
100                                 const void *key, unsigned char ivec[16], u64 *Xi)
101 {
102     return aes_p10_gcm_crypt(in, out, len, key, ivec, Xi, 0);
103 }
104
105
106 static const PROV_GCM_HW aes_ppc_gcm = {
107     aes_ppc_gcm_initkey,
108     ossl_gcm_setiv,
109     ossl_gcm_aad_update,
110     generic_aes_gcm_cipher_update,
111     ossl_gcm_cipher_final,
112     ossl_gcm_one_shot
113 };
114
115 const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
116 {
117     return PPC_AES_GCM_CAPABLE ? &aes_ppc_gcm : &aes_gcm;
118 }
119