2 * Copyright 2001-2022 The OpenSSL Project Authors. All Rights Reserved.
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
11 * PPC support for AES GCM.
12 * This file is included by cipher_aes_gcm_hw.c
15 static int aes_ppc_gcm_initkey(PROV_GCM_CTX *ctx, const unsigned char *key,
18 PROV_AES_GCM_CTX *actx = (PROV_AES_GCM_CTX *)ctx;
19 AES_KEY *ks = &actx->ks.ks;
21 GCM_HW_SET_KEY_CTR_FN(ks, aes_p8_set_encrypt_key, aes_p8_encrypt,
22 aes_p8_ctr32_encrypt_blocks);
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);
32 static inline u32 UTO32(unsigned char *buf)
34 return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) | ((u32) buf[2] << 8) | ((u32) buf[3]);
37 static inline u32 add32TOU(unsigned char buf[4], u32 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;
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)
59 unsigned char ctr_saved[12];
61 memcpy(ctr_saved, ivec, 12);
64 blocks_unused = (u64) 0xffffffffU + 1 - (u64) UTO32 (ivec + 12);
65 if (nb > blocks_unused) {
66 len = blocks_unused * 16;
68 next_ctr = blocks_unused;
76 s = encrypt ? ppc_aes_gcm_encrypt(in, out, len, key, ivec, Xi)
77 : ppc_aes_gcm_decrypt(in, out, len, key, ivec, Xi);
79 /* add counter to ivec */
80 add32TOU(ivec + 12, (u32) next_ctr);
86 memcpy(ivec, ctr_saved, 12);
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)
96 return aes_p10_gcm_crypt(in, out, len, key, ivec, Xi, 1);
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)
102 return aes_p10_gcm_crypt(in, out, len, key, ivec, Xi, 0);
106 static const PROV_GCM_HW aes_ppc_gcm = {
110 generic_aes_gcm_cipher_update,
111 ossl_gcm_cipher_final,
115 const PROV_GCM_HW *ossl_prov_aes_hw_gcm(size_t keybits)
117 return PPC_AES_GCM_CAPABLE ? &aes_ppc_gcm : &aes_gcm;