Debian Codegraph @

cipher/rijndael-p10le.c

raw
1 /* Rijndael (AES) for GnuPG - PowerPC Vector Crypto AES implementation
2 * Copyright 2021- IBM Inc. All rights reserved
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Alternatively, this code may be used in OpenSSL from The OpenSSL Project,
20 * and Cryptogams by Andy Polyakov, and if made part of a release of either
21 * or both projects, is thereafter dual-licensed under the license said project
22 * is released under.
23 */
24
25 #include <config.h>
26
27 #include "rijndael-internal.h"
28 #include "cipher-internal.h"
29 #include "bufhelp.h"
30
31 #ifdef USE_PPC_CRYPTO_WITH_PPC9LE
32
33 #include "simd-common-ppc.h"
34
35
36 extern size_t _gcry_ppc10_aes_gcm_encrypt (const void *inp, void *out,
37 size_t len,
38 const unsigned char *key,
39 unsigned char iv[16], void *Xip);
40 extern size_t _gcry_ppc10_aes_gcm_decrypt (const void *inp, void *out,
41 size_t len,
42 const unsigned char *key,
43 unsigned char iv[16], void *Xip);
44
45 size_t
46 _gcry_aes_p10le_gcm_crypt(gcry_cipher_hd_t c, void *outbuf_arg,
47 const void *inbuf_arg, size_t nblocks, int encrypt)
48 {
49 RIJNDAEL_context *ctx = (void *)&c->context.c;
50 unsigned char *rk = (unsigned char *) ctx->u1.keyschedule;
51 unsigned char *gcm_table = (unsigned char *) c->u_mode.gcm.gcm_table;
52 unsigned char *iv = c->u_ctr.ctr;
53 unsigned char *Xi = c->u_mode.gcm.u_tag.tag;
54 int s = 0;
55 int ndone = 0;
56 int ctr_reset = 0;
57 size_t len = nblocks * GCRY_GCM_BLOCK_LEN;
58 u64 blocks_unused;
59 u64 nb = nblocks;
60 u64 next_ctr = 0;
61 unsigned char ctr_saved[12];
62 unsigned char *inp = (unsigned char *) inbuf_arg;
63 unsigned char *out = (unsigned char *) outbuf_arg;
64
65 /*
66 * This is what the aes-gcm asembly code expects some input parameters.
67 *
68 * - Number of rounds is at 480 offset from rk (rk->rounds)
69 * - Xi at 256 offset from gcm_table
70 */
71 gcry_assert (sizeof(c->u_mode.gcm.gcm_table) >= 256 + 16);
72 buf_cpy (gcm_table+256, Xi, 16);
73 buf_cpy (ctr_saved, c->u_ctr.ctr, 12);
74
75 while (nb)
76 {
77 blocks_unused = (u64) 0xffffffffU + 1 - (u64) buf_get_be32 (iv + 12);
78 if (nb > blocks_unused)
79 {
80 len = blocks_unused * GCRY_GCM_BLOCK_LEN;
81 nb -= blocks_unused;
82 next_ctr = blocks_unused;
83 ctr_reset = 1;
84 }
85 else
86 {
87 len = nb * GCRY_GCM_BLOCK_LEN;
88 next_ctr = nb;
89 nb = 0;
90 }
91
92 if (encrypt)
93 s = _gcry_ppc10_aes_gcm_encrypt((const void *) inp, (void *) out, len,
94 (const unsigned char *) rk, iv,
95 (void *) gcm_table);
96 else
97 s = _gcry_ppc10_aes_gcm_decrypt((const void *) inp, (void *) out, len,
98 (const unsigned char *) rk, iv,
99 (void *) gcm_table);
100
101 cipher_block_add(c->u_ctr.ctr, next_ctr, GCRY_GCM_BLOCK_LEN);
102 if (ctr_reset)
103 {
104 ctr_reset = 0;
105 inp += len;
106 out += len;
107 }
108 buf_cpy (c->u_ctr.ctr, ctr_saved, 12);
109 ndone += s;
110 }
111 buf_cpy (Xi, gcm_table+256, 16);
112
113 /*
114 * Return number of blocks done.
115 */
116 s = ndone / GCRY_GCM_BLOCK_LEN;
117 s = nblocks - s;
118
119 clear_vec_regs();
120
121 return ( s );
122 }
123
124 #endif /* USE_PPC_CRYPTO_WITH_PPC9LE */