Skinny-C
 All Data Structures Files Functions Variables Groups Pages
mantis-parallel.c
1 /*
2  * Copyright (C) 2017 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "mantis-parallel.h"
24 #include "skinny-internal.h"
25 #include <stdlib.h>
26 
33 typedef struct
34 {
35  void (*crypt)(void *output, const void *input, const void *tweak,
36  const MantisKey_t *ks);
37 
38 } MantisParallelECBVtable_t;
39 
40 void _mantis_parallel_crypt_vec128
41  (void *output, const void *input, const void *tweak, const MantisKey_t *ks);
42 
43 static MantisParallelECBVtable_t const mantis_parallel_ecb_vec128 = {
44  _mantis_parallel_crypt_vec128
45 };
46 
50 {
51  MantisKey_t *ctx;
52  if ((ctx = calloc(1, sizeof(MantisKey_t))) == NULL)
53  return 0;
54  ecb->vtable = 0;
55  ecb->ctx = ctx;
57  if (_skinny_has_vec128())
58  ecb->vtable = &mantis_parallel_ecb_vec128;
59  return 1;
60 }
61 
63 {
64  if (ecb && ecb->ctx) {
65  skinny_cleanse(ecb->ctx, sizeof(MantisKey_t));
66  free(ecb->ctx);
67  ecb->ctx = 0;
68  }
69 }
70 
72  (MantisParallelECB_t *ecb, const void *key, unsigned size,
73  unsigned rounds, int mode)
74 {
75  MantisKey_t *ks;
76  if (!ecb || !ecb->ctx)
77  return 0;
78  ks = ecb->ctx;
79  return mantis_set_key(ks, key, size, rounds, mode);
80 }
81 
83 {
84  MantisKey_t *ks;
85  if (!ecb || !ecb->ctx)
86  return;
87  ks = ecb->ctx;
89 }
90 
92  (void *output, const void *input, const void *tweak, size_t size,
93  const MantisParallelECB_t *ecb)
94 {
95  const MantisKey_t *ks;
96  const MantisParallelECBVtable_t *vtable;
97 
98  /* Validate the parameters */
99  if (!ecb || !ecb->ctx || (size % MANTIS_BLOCK_SIZE) != 0)
100  return 0;
101  ks = ecb->ctx;
102 
103  /* Process major blocks with the vectorized back end */
104  vtable = ecb->vtable;
105  if (vtable) {
106  size_t psize = ecb->parallel_size;
107  while (size >= psize) {
108  (*(vtable->crypt))(output, input, tweak, ks);
109  output += psize;
110  input += psize;
111  tweak += psize;
112  size -= psize;
113  }
114  }
115 
116  /* Process any left-over blocks with the non-parallel implementation */
117  while (size >= MANTIS_BLOCK_SIZE) {
118  mantis_ecb_crypt_tweaked(output, input, tweak, ks);
119  output += MANTIS_BLOCK_SIZE;
120  input += MANTIS_BLOCK_SIZE;
121  tweak += MANTIS_BLOCK_SIZE;
122  size -= MANTIS_BLOCK_SIZE;
123  }
124  return 1;
125 }
State information for Mantis in parallel ECB mode.
void mantis_swap_modes(MantisKey_t *ks)
Swaps the encryption and decryption modes on a Mantis key schedule.
void mantis_parallel_ecb_cleanup(MantisParallelECB_t *ecb)
Cleans up a parallel ECB control block for Mantis.
int mantis_parallel_ecb_init(MantisParallelECB_t *ecb)
Initializes Mantis in parallel ECB mode.
void mantis_parallel_ecb_swap_modes(MantisParallelECB_t *ecb)
Swaps the encryption and decryption modes on a parallel Mantis key schedule.
#define MANTIS_BLOCK_SIZE
Size of a block for Mantis block ciphers.
Definition: mantis-cipher.h:59
int mantis_set_key(MantisKey_t *ks, const void *key, unsigned size, unsigned rounds, int mode)
Sets the key schedule for a Mantis block cipher.
int mantis_parallel_ecb_crypt(void *output, const void *input, const void *tweak, size_t size, const MantisParallelECB_t *ecb)
Encrypts or decrypts a block of data using Mantis in parallel ECB mode.
Key schedule for Mantis block ciphers.
void mantis_ecb_crypt_tweaked(void *output, const void *input, const void *tweak, const MantisKey_t *ks)
Encrypts or decrypts a single block using the Mantis block cipher in ECB mode, with the tweak supplie...
int mantis_parallel_ecb_set_key(MantisParallelECB_t *ecb, const void *key, unsigned size, unsigned rounds, int mode)
Sets the key schedule for a Mantis block cipher in parallel ECB mode.