summaryrefslogtreecommitdiff
path: root/cipher/rijndael-p10le.c
blob: b16ebe1b7c514e9069a1805f356d5740b9b5fd32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Rijndael (AES) for GnuPG - PowerPC Vector Crypto AES implementation
 * Copyright 2021- IBM Inc. All rights reserved
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Alternatively, this code may be used in OpenSSL from The OpenSSL Project,
 * and Cryptogams by Andy Polyakov, and if made part of a release of either
 * or both projects, is thereafter dual-licensed under the license said project
 * is released under.
 */

#include <config.h>

#include "rijndael-internal.h"
#include "cipher-internal.h"
#include "bufhelp.h"

#ifdef USE_PPC_CRYPTO_WITH_PPC9LE


extern size_t _gcry_ppc10_aes_gcm_encrypt (const void *inp, void *out,
                                           size_t len,
                                           const unsigned char *key,
                                           unsigned char iv[16], void *Xip);
extern size_t _gcry_ppc10_aes_gcm_decrypt (const void *inp, void *out,
                                           size_t len,
                                           const unsigned char *key,
                                           unsigned char iv[16], void *Xip);

size_t
_gcry_aes_p10le_gcm_crypt(gcry_cipher_hd_t c, void *outbuf_arg,
                          const void *inbuf_arg, size_t nblocks, int encrypt)
{
  RIJNDAEL_context *ctx = (RIJNDAEL_context *) &c->context.c;
  unsigned char *rk = (unsigned char *) ctx->u1.keyschedule;
  unsigned char *gcm_table = (unsigned char *) c->u_mode.gcm.gcm_table;
  unsigned char *iv = c->u_ctr.ctr;
  unsigned char *Xi = c->u_mode.gcm.u_tag.tag;
  int s = 0;
  int ndone = 0;
  int ctr_reset = 0;
  size_t len = nblocks * GCRY_GCM_BLOCK_LEN;
  u64 blocks_unused;
  u64 nb = nblocks;
  u64 next_ctr = 0;
  unsigned char ctr_saved[12];
  unsigned char *inp = (unsigned char *) inbuf_arg;
  unsigned char *out = (unsigned char *) outbuf_arg;

  /*
   * This is what the aes-gcm asembly code expects some input parameters.
   *
   *   - Number of rounds is at 480 offset from rk (rk->rounds)
   *   - Xi at 256 offset from gcm_table
   */
  gcry_assert (sizeof(c->u_mode.gcm.gcm_table) >= 256 + 16);
  buf_cpy (gcm_table+256, Xi, 16);
  buf_cpy (ctr_saved, c->u_ctr.ctr, 12);

  while (nb)
    {
      blocks_unused = (u64) 0xffffffffU + 1 - (u64) buf_get_be32 (iv + 12);
      if (nb > blocks_unused)
        {
          len = blocks_unused * GCRY_GCM_BLOCK_LEN;
          nb -= blocks_unused;
          next_ctr = blocks_unused;
          ctr_reset = 1;
        }
      else
        {
          len = nb * GCRY_GCM_BLOCK_LEN;
          next_ctr = nb;
          nb = 0;
        }

      if (encrypt)
        s = _gcry_ppc10_aes_gcm_encrypt((const void *) inp, (void *) out, len,
                                        (const unsigned char *) rk, iv,
                                        (void *) gcm_table);
      else
        s = _gcry_ppc10_aes_gcm_decrypt((const void *) inp, (void *) out, len,
                                        (const unsigned char *) rk, iv,
                                        (void *) gcm_table);

      cipher_block_add(c->u_ctr.ctr, next_ctr, GCRY_GCM_BLOCK_LEN);
      if (ctr_reset)
        {
          ctr_reset = 0;
          inp += len;
          out += len;
        }
      buf_cpy (c->u_ctr.ctr, ctr_saved, 12);
      ndone += s;
    }
  buf_cpy (Xi, gcm_table+256, 16);

  /*
   * Return number of blocks done.
   */
  s = ndone / GCRY_GCM_BLOCK_LEN;
  s = nblocks - s;
  return ( s );
}

#endif /* USE_PPC_CRYPTO_WITH_PPC9LE */