wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 02 08:44:47 2017 +0000
Revision:
7:481bce714567
wolfSSL3.10.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 7:481bce714567 1 /* hc128.c
wolfSSL 7:481bce714567 2 *
wolfSSL 7:481bce714567 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 7:481bce714567 4 *
wolfSSL 7:481bce714567 5 * This file is part of wolfSSL.
wolfSSL 7:481bce714567 6 *
wolfSSL 7:481bce714567 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 7:481bce714567 8 * it under the terms of the GNU General Public License as published by
wolfSSL 7:481bce714567 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 7:481bce714567 10 * (at your option) any later version.
wolfSSL 7:481bce714567 11 *
wolfSSL 7:481bce714567 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 7:481bce714567 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 7:481bce714567 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 7:481bce714567 15 * GNU General Public License for more details.
wolfSSL 7:481bce714567 16 *
wolfSSL 7:481bce714567 17 * You should have received a copy of the GNU General Public License
wolfSSL 7:481bce714567 18 * along with this program; if not, write to the Free Software
wolfSSL 7:481bce714567 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 7:481bce714567 20 */
wolfSSL 7:481bce714567 21
wolfSSL 7:481bce714567 22
wolfSSL 7:481bce714567 23 #ifdef HAVE_CONFIG_H
wolfSSL 7:481bce714567 24 #include <config.h>
wolfSSL 7:481bce714567 25 #endif
wolfSSL 7:481bce714567 26
wolfSSL 7:481bce714567 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 7:481bce714567 28
wolfSSL 7:481bce714567 29 #ifdef HAVE_HC128
wolfSSL 7:481bce714567 30
wolfSSL 7:481bce714567 31 #include <wolfssl/wolfcrypt/hc128.h>
wolfSSL 7:481bce714567 32 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 7:481bce714567 33 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 7:481bce714567 34 #ifdef NO_INLINE
wolfSSL 7:481bce714567 35 #include <wolfssl/wolfcrypt/hc128.h>
wolfSSL 7:481bce714567 36 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 7:481bce714567 37 #else
wolfSSL 7:481bce714567 38 #define WOLFSSL_MISC_INCLUDED
wolfSSL 7:481bce714567 39 #include <wolfcrypt/src/misc.c>
wolfSSL 7:481bce714567 40 #endif
wolfSSL 7:481bce714567 41
wolfSSL 7:481bce714567 42
wolfSSL 7:481bce714567 43 #ifdef BIG_ENDIAN_ORDER
wolfSSL 7:481bce714567 44 #define LITTLE32(x) ByteReverseWord32(x)
wolfSSL 7:481bce714567 45 #else
wolfSSL 7:481bce714567 46 #define LITTLE32(x) (x)
wolfSSL 7:481bce714567 47 #endif
wolfSSL 7:481bce714567 48
wolfSSL 7:481bce714567 49
wolfSSL 7:481bce714567 50 /*h1 function*/
wolfSSL 7:481bce714567 51 #define h1(ctx, x, y) { \
wolfSSL 7:481bce714567 52 byte a,c; \
wolfSSL 7:481bce714567 53 a = (byte) (x); \
wolfSSL 7:481bce714567 54 c = (byte) ((x) >> 16); \
wolfSSL 7:481bce714567 55 y = (ctx->T[512+a])+(ctx->T[512+256+c]); \
wolfSSL 7:481bce714567 56 }
wolfSSL 7:481bce714567 57
wolfSSL 7:481bce714567 58 /*h2 function*/
wolfSSL 7:481bce714567 59 #define h2(ctx, x, y) { \
wolfSSL 7:481bce714567 60 byte a,c; \
wolfSSL 7:481bce714567 61 a = (byte) (x); \
wolfSSL 7:481bce714567 62 c = (byte) ((x) >> 16); \
wolfSSL 7:481bce714567 63 y = (ctx->T[a])+(ctx->T[256+c]); \
wolfSSL 7:481bce714567 64 }
wolfSSL 7:481bce714567 65
wolfSSL 7:481bce714567 66 /*one step of HC-128, update P and generate 32 bits keystream*/
wolfSSL 7:481bce714567 67 #define step_P(ctx,u,v,a,b,c,d,n){ \
wolfSSL 7:481bce714567 68 word32 tem0,tem1,tem2,tem3; \
wolfSSL 7:481bce714567 69 h1((ctx),(ctx->X[(d)]),tem3); \
wolfSSL 7:481bce714567 70 tem0 = rotrFixed((ctx->T[(v)]),23); \
wolfSSL 7:481bce714567 71 tem1 = rotrFixed((ctx->X[(c)]),10); \
wolfSSL 7:481bce714567 72 tem2 = rotrFixed((ctx->X[(b)]),8); \
wolfSSL 7:481bce714567 73 (ctx->T[(u)]) += tem2+(tem0 ^ tem1); \
wolfSSL 7:481bce714567 74 (ctx->X[(a)]) = (ctx->T[(u)]); \
wolfSSL 7:481bce714567 75 (n) = tem3 ^ (ctx->T[(u)]) ; \
wolfSSL 7:481bce714567 76 }
wolfSSL 7:481bce714567 77
wolfSSL 7:481bce714567 78 /*one step of HC-128, update Q and generate 32 bits keystream*/
wolfSSL 7:481bce714567 79 #define step_Q(ctx,u,v,a,b,c,d,n){ \
wolfSSL 7:481bce714567 80 word32 tem0,tem1,tem2,tem3; \
wolfSSL 7:481bce714567 81 h2((ctx),(ctx->Y[(d)]),tem3); \
wolfSSL 7:481bce714567 82 tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
wolfSSL 7:481bce714567 83 tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
wolfSSL 7:481bce714567 84 tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
wolfSSL 7:481bce714567 85 (ctx->T[(u)]) += tem2 + (tem0 ^ tem1); \
wolfSSL 7:481bce714567 86 (ctx->Y[(a)]) = (ctx->T[(u)]); \
wolfSSL 7:481bce714567 87 (n) = tem3 ^ (ctx->T[(u)]) ; \
wolfSSL 7:481bce714567 88 }
wolfSSL 7:481bce714567 89
wolfSSL 7:481bce714567 90 /*16 steps of HC-128, generate 512 bits keystream*/
wolfSSL 7:481bce714567 91 static void generate_keystream(HC128* ctx, word32* keystream)
wolfSSL 7:481bce714567 92 {
wolfSSL 7:481bce714567 93 word32 cc,dd;
wolfSSL 7:481bce714567 94 cc = ctx->counter1024 & 0x1ff;
wolfSSL 7:481bce714567 95 dd = (cc+16)&0x1ff;
wolfSSL 7:481bce714567 96
wolfSSL 7:481bce714567 97 if (ctx->counter1024 < 512)
wolfSSL 7:481bce714567 98 {
wolfSSL 7:481bce714567 99 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 7:481bce714567 100 step_P(ctx, cc+0, cc+1, 0, 6, 13,4, keystream[0]);
wolfSSL 7:481bce714567 101 step_P(ctx, cc+1, cc+2, 1, 7, 14,5, keystream[1]);
wolfSSL 7:481bce714567 102 step_P(ctx, cc+2, cc+3, 2, 8, 15,6, keystream[2]);
wolfSSL 7:481bce714567 103 step_P(ctx, cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
wolfSSL 7:481bce714567 104 step_P(ctx, cc+4, cc+5, 4, 10,1, 8, keystream[4]);
wolfSSL 7:481bce714567 105 step_P(ctx, cc+5, cc+6, 5, 11,2, 9, keystream[5]);
wolfSSL 7:481bce714567 106 step_P(ctx, cc+6, cc+7, 6, 12,3, 10,keystream[6]);
wolfSSL 7:481bce714567 107 step_P(ctx, cc+7, cc+8, 7, 13,4, 11,keystream[7]);
wolfSSL 7:481bce714567 108 step_P(ctx, cc+8, cc+9, 8, 14,5, 12,keystream[8]);
wolfSSL 7:481bce714567 109 step_P(ctx, cc+9, cc+10,9, 15,6, 13,keystream[9]);
wolfSSL 7:481bce714567 110 step_P(ctx, cc+10,cc+11,10,0, 7, 14,keystream[10]);
wolfSSL 7:481bce714567 111 step_P(ctx, cc+11,cc+12,11,1, 8, 15,keystream[11]);
wolfSSL 7:481bce714567 112 step_P(ctx, cc+12,cc+13,12,2, 9, 0, keystream[12]);
wolfSSL 7:481bce714567 113 step_P(ctx, cc+13,cc+14,13,3, 10,1, keystream[13]);
wolfSSL 7:481bce714567 114 step_P(ctx, cc+14,cc+15,14,4, 11,2, keystream[14]);
wolfSSL 7:481bce714567 115 step_P(ctx, cc+15,dd+0, 15,5, 12,3, keystream[15]);
wolfSSL 7:481bce714567 116 }
wolfSSL 7:481bce714567 117 else
wolfSSL 7:481bce714567 118 {
wolfSSL 7:481bce714567 119 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 7:481bce714567 120 step_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
wolfSSL 7:481bce714567 121 step_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
wolfSSL 7:481bce714567 122 step_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
wolfSSL 7:481bce714567 123 step_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
wolfSSL 7:481bce714567 124 step_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
wolfSSL 7:481bce714567 125 step_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
wolfSSL 7:481bce714567 126 step_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
wolfSSL 7:481bce714567 127 step_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
wolfSSL 7:481bce714567 128 step_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
wolfSSL 7:481bce714567 129 step_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
wolfSSL 7:481bce714567 130 step_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
wolfSSL 7:481bce714567 131 step_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
wolfSSL 7:481bce714567 132 step_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
wolfSSL 7:481bce714567 133 step_Q(ctx, 512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
wolfSSL 7:481bce714567 134 step_Q(ctx, 512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
wolfSSL 7:481bce714567 135 step_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
wolfSSL 7:481bce714567 136 }
wolfSSL 7:481bce714567 137 }
wolfSSL 7:481bce714567 138
wolfSSL 7:481bce714567 139
wolfSSL 7:481bce714567 140 /* The following defines the initialization functions */
wolfSSL 7:481bce714567 141 #define f1(x) (rotrFixed((x),7) ^ rotrFixed((x),18) ^ ((x) >> 3))
wolfSSL 7:481bce714567 142 #define f2(x) (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
wolfSSL 7:481bce714567 143
wolfSSL 7:481bce714567 144 /*update table P*/
wolfSSL 7:481bce714567 145 #define update_P(ctx,u,v,a,b,c,d){ \
wolfSSL 7:481bce714567 146 word32 tem0,tem1,tem2,tem3; \
wolfSSL 7:481bce714567 147 tem0 = rotrFixed((ctx->T[(v)]),23); \
wolfSSL 7:481bce714567 148 tem1 = rotrFixed((ctx->X[(c)]),10); \
wolfSSL 7:481bce714567 149 tem2 = rotrFixed((ctx->X[(b)]),8); \
wolfSSL 7:481bce714567 150 h1((ctx),(ctx->X[(d)]),tem3); \
wolfSSL 7:481bce714567 151 (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
wolfSSL 7:481bce714567 152 (ctx->X[(a)]) = (ctx->T[(u)]); \
wolfSSL 7:481bce714567 153 }
wolfSSL 7:481bce714567 154
wolfSSL 7:481bce714567 155 /*update table Q*/
wolfSSL 7:481bce714567 156 #define update_Q(ctx,u,v,a,b,c,d){ \
wolfSSL 7:481bce714567 157 word32 tem0,tem1,tem2,tem3; \
wolfSSL 7:481bce714567 158 tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
wolfSSL 7:481bce714567 159 tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
wolfSSL 7:481bce714567 160 tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
wolfSSL 7:481bce714567 161 h2((ctx),(ctx->Y[(d)]),tem3); \
wolfSSL 7:481bce714567 162 (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
wolfSSL 7:481bce714567 163 (ctx->Y[(a)]) = (ctx->T[(u)]); \
wolfSSL 7:481bce714567 164 }
wolfSSL 7:481bce714567 165
wolfSSL 7:481bce714567 166 /*16 steps of HC-128, without generating keystream, */
wolfSSL 7:481bce714567 167 /*but use the outputs to update P and Q*/
wolfSSL 7:481bce714567 168 static void setup_update(HC128* ctx) /*each time 16 steps*/
wolfSSL 7:481bce714567 169 {
wolfSSL 7:481bce714567 170 word32 cc,dd;
wolfSSL 7:481bce714567 171 cc = ctx->counter1024 & 0x1ff;
wolfSSL 7:481bce714567 172 dd = (cc+16)&0x1ff;
wolfSSL 7:481bce714567 173
wolfSSL 7:481bce714567 174 if (ctx->counter1024 < 512)
wolfSSL 7:481bce714567 175 {
wolfSSL 7:481bce714567 176 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 7:481bce714567 177 update_P(ctx, cc+0, cc+1, 0, 6, 13, 4);
wolfSSL 7:481bce714567 178 update_P(ctx, cc+1, cc+2, 1, 7, 14, 5);
wolfSSL 7:481bce714567 179 update_P(ctx, cc+2, cc+3, 2, 8, 15, 6);
wolfSSL 7:481bce714567 180 update_P(ctx, cc+3, cc+4, 3, 9, 0, 7);
wolfSSL 7:481bce714567 181 update_P(ctx, cc+4, cc+5, 4, 10,1, 8);
wolfSSL 7:481bce714567 182 update_P(ctx, cc+5, cc+6, 5, 11,2, 9);
wolfSSL 7:481bce714567 183 update_P(ctx, cc+6, cc+7, 6, 12,3, 10);
wolfSSL 7:481bce714567 184 update_P(ctx, cc+7, cc+8, 7, 13,4, 11);
wolfSSL 7:481bce714567 185 update_P(ctx, cc+8, cc+9, 8, 14,5, 12);
wolfSSL 7:481bce714567 186 update_P(ctx, cc+9, cc+10,9, 15,6, 13);
wolfSSL 7:481bce714567 187 update_P(ctx, cc+10,cc+11,10,0, 7, 14);
wolfSSL 7:481bce714567 188 update_P(ctx, cc+11,cc+12,11,1, 8, 15);
wolfSSL 7:481bce714567 189 update_P(ctx, cc+12,cc+13,12,2, 9, 0);
wolfSSL 7:481bce714567 190 update_P(ctx, cc+13,cc+14,13,3, 10, 1);
wolfSSL 7:481bce714567 191 update_P(ctx, cc+14,cc+15,14,4, 11, 2);
wolfSSL 7:481bce714567 192 update_P(ctx, cc+15,dd+0, 15,5, 12, 3);
wolfSSL 7:481bce714567 193 }
wolfSSL 7:481bce714567 194 else
wolfSSL 7:481bce714567 195 {
wolfSSL 7:481bce714567 196 ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
wolfSSL 7:481bce714567 197 update_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13, 4);
wolfSSL 7:481bce714567 198 update_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14, 5);
wolfSSL 7:481bce714567 199 update_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15, 6);
wolfSSL 7:481bce714567 200 update_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7);
wolfSSL 7:481bce714567 201 update_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8);
wolfSSL 7:481bce714567 202 update_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9);
wolfSSL 7:481bce714567 203 update_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10);
wolfSSL 7:481bce714567 204 update_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11);
wolfSSL 7:481bce714567 205 update_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12);
wolfSSL 7:481bce714567 206 update_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13);
wolfSSL 7:481bce714567 207 update_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14);
wolfSSL 7:481bce714567 208 update_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15);
wolfSSL 7:481bce714567 209 update_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0);
wolfSSL 7:481bce714567 210 update_Q(ctx, 512+cc+13,512+cc+14,13,3, 10, 1);
wolfSSL 7:481bce714567 211 update_Q(ctx, 512+cc+14,512+cc+15,14,4, 11, 2);
wolfSSL 7:481bce714567 212 update_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12, 3);
wolfSSL 7:481bce714567 213 }
wolfSSL 7:481bce714567 214 }
wolfSSL 7:481bce714567 215
wolfSSL 7:481bce714567 216
wolfSSL 7:481bce714567 217 /* for the 128-bit key: key[0]...key[15]
wolfSSL 7:481bce714567 218 * key[0] is the least significant byte of ctx->key[0] (K_0);
wolfSSL 7:481bce714567 219 * key[3] is the most significant byte of ctx->key[0] (K_0);
wolfSSL 7:481bce714567 220 * ...
wolfSSL 7:481bce714567 221 * key[12] is the least significant byte of ctx->key[3] (K_3)
wolfSSL 7:481bce714567 222 * key[15] is the most significant byte of ctx->key[3] (K_3)
wolfSSL 7:481bce714567 223 *
wolfSSL 7:481bce714567 224 * for the 128-bit iv: iv[0]...iv[15]
wolfSSL 7:481bce714567 225 * iv[0] is the least significant byte of ctx->iv[0] (IV_0);
wolfSSL 7:481bce714567 226 * iv[3] is the most significant byte of ctx->iv[0] (IV_0);
wolfSSL 7:481bce714567 227 * ...
wolfSSL 7:481bce714567 228 * iv[12] is the least significant byte of ctx->iv[3] (IV_3)
wolfSSL 7:481bce714567 229 * iv[15] is the most significant byte of ctx->iv[3] (IV_3)
wolfSSL 7:481bce714567 230 */
wolfSSL 7:481bce714567 231
wolfSSL 7:481bce714567 232
wolfSSL 7:481bce714567 233
wolfSSL 7:481bce714567 234 static void Hc128_SetIV(HC128* ctx, const byte* inIv)
wolfSSL 7:481bce714567 235 {
wolfSSL 7:481bce714567 236 word32 i;
wolfSSL 7:481bce714567 237 word32 iv[4];
wolfSSL 7:481bce714567 238
wolfSSL 7:481bce714567 239 if (inIv)
wolfSSL 7:481bce714567 240 XMEMCPY(iv, inIv, sizeof(iv));
wolfSSL 7:481bce714567 241 else
wolfSSL 7:481bce714567 242 XMEMSET(iv, 0, sizeof(iv));
wolfSSL 7:481bce714567 243
wolfSSL 7:481bce714567 244 for (i = 0; i < (128 >> 5); i++)
wolfSSL 7:481bce714567 245 ctx->iv[i] = LITTLE32(iv[i]);
wolfSSL 7:481bce714567 246
wolfSSL 7:481bce714567 247 for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4];
wolfSSL 7:481bce714567 248
wolfSSL 7:481bce714567 249 /* expand the key and IV into the table T */
wolfSSL 7:481bce714567 250 /* (expand the key and IV into the table P and Q) */
wolfSSL 7:481bce714567 251
wolfSSL 7:481bce714567 252 for (i = 0; i < 8; i++) ctx->T[i] = ctx->key[i];
wolfSSL 7:481bce714567 253 for (i = 8; i < 16; i++) ctx->T[i] = ctx->iv[i-8];
wolfSSL 7:481bce714567 254
wolfSSL 7:481bce714567 255 for (i = 16; i < (256+16); i++)
wolfSSL 7:481bce714567 256 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
wolfSSL 7:481bce714567 257 ctx->T[i-16]+i;
wolfSSL 7:481bce714567 258
wolfSSL 7:481bce714567 259 for (i = 0; i < 16; i++) ctx->T[i] = ctx->T[256+i];
wolfSSL 7:481bce714567 260
wolfSSL 7:481bce714567 261 for (i = 16; i < 1024; i++)
wolfSSL 7:481bce714567 262 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
wolfSSL 7:481bce714567 263 ctx->T[i-16]+256+i;
wolfSSL 7:481bce714567 264
wolfSSL 7:481bce714567 265 /* initialize counter1024, X and Y */
wolfSSL 7:481bce714567 266 ctx->counter1024 = 0;
wolfSSL 7:481bce714567 267 for (i = 0; i < 16; i++) ctx->X[i] = ctx->T[512-16+i];
wolfSSL 7:481bce714567 268 for (i = 0; i < 16; i++) ctx->Y[i] = ctx->T[512+512-16+i];
wolfSSL 7:481bce714567 269
wolfSSL 7:481bce714567 270 /* run the cipher 1024 steps before generating the output */
wolfSSL 7:481bce714567 271 for (i = 0; i < 64; i++) setup_update(ctx);
wolfSSL 7:481bce714567 272 }
wolfSSL 7:481bce714567 273
wolfSSL 7:481bce714567 274
wolfSSL 7:481bce714567 275 static INLINE int DoKey(HC128* ctx, const byte* key, const byte* iv)
wolfSSL 7:481bce714567 276 {
wolfSSL 7:481bce714567 277 word32 i;
wolfSSL 7:481bce714567 278
wolfSSL 7:481bce714567 279 /* Key size in bits 128 */
wolfSSL 7:481bce714567 280 for (i = 0; i < (128 >> 5); i++)
wolfSSL 7:481bce714567 281 ctx->key[i] = LITTLE32(((word32*)key)[i]);
wolfSSL 7:481bce714567 282
wolfSSL 7:481bce714567 283 for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
wolfSSL 7:481bce714567 284
wolfSSL 7:481bce714567 285 Hc128_SetIV(ctx, iv);
wolfSSL 7:481bce714567 286
wolfSSL 7:481bce714567 287 return 0;
wolfSSL 7:481bce714567 288 }
wolfSSL 7:481bce714567 289
wolfSSL 7:481bce714567 290
wolfSSL 7:481bce714567 291 int wc_Hc128_SetHeap(HC128* ctx, void* heap)
wolfSSL 7:481bce714567 292 {
wolfSSL 7:481bce714567 293 if (ctx == NULL) {
wolfSSL 7:481bce714567 294 return BAD_FUNC_ARG;
wolfSSL 7:481bce714567 295 }
wolfSSL 7:481bce714567 296
wolfSSL 7:481bce714567 297 #ifdef XSTREAM_ALIGN
wolfSSL 7:481bce714567 298 ctx->heap = heap;
wolfSSL 7:481bce714567 299 #endif
wolfSSL 7:481bce714567 300
wolfSSL 7:481bce714567 301 (void)heap;
wolfSSL 7:481bce714567 302 return 0;
wolfSSL 7:481bce714567 303 }
wolfSSL 7:481bce714567 304
wolfSSL 7:481bce714567 305 /* Key setup */
wolfSSL 7:481bce714567 306 int wc_Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
wolfSSL 7:481bce714567 307 {
wolfSSL 7:481bce714567 308 #ifdef XSTREAM_ALIGN
wolfSSL 7:481bce714567 309 /* default heap to NULL or heap test value */
wolfSSL 7:481bce714567 310 #ifdef WOLFSSL_HEAP_TEST
wolfSSL 7:481bce714567 311 ctx->heap = (void*)WOLFSSL_HEAP_TEST;
wolfSSL 7:481bce714567 312 #else
wolfSSL 7:481bce714567 313 ctx->heap = NULL;
wolfSSL 7:481bce714567 314 #endif /* WOLFSSL_HEAP_TEST */
wolfSSL 7:481bce714567 315
wolfSSL 7:481bce714567 316 if ((wolfssl_word)key % 4) {
wolfSSL 7:481bce714567 317 int alignKey[4];
wolfSSL 7:481bce714567 318
wolfSSL 7:481bce714567 319 /* iv gets aligned in SetIV */
wolfSSL 7:481bce714567 320 WOLFSSL_MSG("Hc128SetKey unaligned key");
wolfSSL 7:481bce714567 321
wolfSSL 7:481bce714567 322 XMEMCPY(alignKey, key, sizeof(alignKey));
wolfSSL 7:481bce714567 323
wolfSSL 7:481bce714567 324 return DoKey(ctx, (const byte*)alignKey, iv);
wolfSSL 7:481bce714567 325 }
wolfSSL 7:481bce714567 326 #endif /* XSTREAM_ALIGN */
wolfSSL 7:481bce714567 327
wolfSSL 7:481bce714567 328 return DoKey(ctx, key, iv);
wolfSSL 7:481bce714567 329 }
wolfSSL 7:481bce714567 330
wolfSSL 7:481bce714567 331
wolfSSL 7:481bce714567 332
wolfSSL 7:481bce714567 333 /* The following defines the encryption of data stream */
wolfSSL 7:481bce714567 334 static INLINE int DoProcess(HC128* ctx, byte* output, const byte* input,
wolfSSL 7:481bce714567 335 word32 msglen)
wolfSSL 7:481bce714567 336 {
wolfSSL 7:481bce714567 337 word32 i, keystream[16];
wolfSSL 7:481bce714567 338
wolfSSL 7:481bce714567 339 for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
wolfSSL 7:481bce714567 340 {
wolfSSL 7:481bce714567 341 generate_keystream(ctx, keystream);
wolfSSL 7:481bce714567 342
wolfSSL 7:481bce714567 343 /* unroll loop */
wolfSSL 7:481bce714567 344 ((word32*)output)[0] = ((word32*)input)[0] ^ LITTLE32(keystream[0]);
wolfSSL 7:481bce714567 345 ((word32*)output)[1] = ((word32*)input)[1] ^ LITTLE32(keystream[1]);
wolfSSL 7:481bce714567 346 ((word32*)output)[2] = ((word32*)input)[2] ^ LITTLE32(keystream[2]);
wolfSSL 7:481bce714567 347 ((word32*)output)[3] = ((word32*)input)[3] ^ LITTLE32(keystream[3]);
wolfSSL 7:481bce714567 348 ((word32*)output)[4] = ((word32*)input)[4] ^ LITTLE32(keystream[4]);
wolfSSL 7:481bce714567 349 ((word32*)output)[5] = ((word32*)input)[5] ^ LITTLE32(keystream[5]);
wolfSSL 7:481bce714567 350 ((word32*)output)[6] = ((word32*)input)[6] ^ LITTLE32(keystream[6]);
wolfSSL 7:481bce714567 351 ((word32*)output)[7] = ((word32*)input)[7] ^ LITTLE32(keystream[7]);
wolfSSL 7:481bce714567 352 ((word32*)output)[8] = ((word32*)input)[8] ^ LITTLE32(keystream[8]);
wolfSSL 7:481bce714567 353 ((word32*)output)[9] = ((word32*)input)[9] ^ LITTLE32(keystream[9]);
wolfSSL 7:481bce714567 354 ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
wolfSSL 7:481bce714567 355 ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
wolfSSL 7:481bce714567 356 ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
wolfSSL 7:481bce714567 357 ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
wolfSSL 7:481bce714567 358 ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
wolfSSL 7:481bce714567 359 ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
wolfSSL 7:481bce714567 360 }
wolfSSL 7:481bce714567 361
wolfSSL 7:481bce714567 362 if (msglen > 0)
wolfSSL 7:481bce714567 363 {
wolfSSL 7:481bce714567 364 XMEMSET(keystream, 0, sizeof(keystream)); /* hush the static analysis */
wolfSSL 7:481bce714567 365 generate_keystream(ctx, keystream);
wolfSSL 7:481bce714567 366
wolfSSL 7:481bce714567 367 #ifdef BIG_ENDIAN_ORDER
wolfSSL 7:481bce714567 368 {
wolfSSL 7:481bce714567 369 word32 wordsLeft = msglen / sizeof(word32);
wolfSSL 7:481bce714567 370 if (msglen % sizeof(word32)) wordsLeft++;
wolfSSL 7:481bce714567 371
wolfSSL 7:481bce714567 372 ByteReverseWords(keystream, keystream, wordsLeft * sizeof(word32));
wolfSSL 7:481bce714567 373 }
wolfSSL 7:481bce714567 374 #endif
wolfSSL 7:481bce714567 375
wolfSSL 7:481bce714567 376 for (i = 0; i < msglen; i++)
wolfSSL 7:481bce714567 377 output[i] = input[i] ^ ((byte*)keystream)[i];
wolfSSL 7:481bce714567 378 }
wolfSSL 7:481bce714567 379
wolfSSL 7:481bce714567 380 return 0;
wolfSSL 7:481bce714567 381 }
wolfSSL 7:481bce714567 382
wolfSSL 7:481bce714567 383
wolfSSL 7:481bce714567 384 /* Encrypt/decrypt a message of any size */
wolfSSL 7:481bce714567 385 int wc_Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
wolfSSL 7:481bce714567 386 {
wolfSSL 7:481bce714567 387 #ifdef XSTREAM_ALIGN
wolfSSL 7:481bce714567 388 if ((wolfssl_word)input % 4 || (wolfssl_word)output % 4) {
wolfSSL 7:481bce714567 389 #ifndef NO_WOLFSSL_ALLOC_ALIGN
wolfSSL 7:481bce714567 390 byte* tmp;
wolfSSL 7:481bce714567 391 WOLFSSL_MSG("Hc128Process unaligned");
wolfSSL 7:481bce714567 392
wolfSSL 7:481bce714567 393 tmp = (byte*)XMALLOC(msglen, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 394 if (tmp == NULL) return MEMORY_E;
wolfSSL 7:481bce714567 395
wolfSSL 7:481bce714567 396 XMEMCPY(tmp, input, msglen);
wolfSSL 7:481bce714567 397 DoProcess(ctx, tmp, tmp, msglen);
wolfSSL 7:481bce714567 398 XMEMCPY(output, tmp, msglen);
wolfSSL 7:481bce714567 399
wolfSSL 7:481bce714567 400 XFREE(tmp, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 7:481bce714567 401
wolfSSL 7:481bce714567 402 return 0;
wolfSSL 7:481bce714567 403 #else
wolfSSL 7:481bce714567 404 return BAD_ALIGN_E;
wolfSSL 7:481bce714567 405 #endif
wolfSSL 7:481bce714567 406 }
wolfSSL 7:481bce714567 407 #endif /* XSTREAM_ALIGN */
wolfSSL 7:481bce714567 408
wolfSSL 7:481bce714567 409 return DoProcess(ctx, output, input, msglen);
wolfSSL 7:481bce714567 410 }
wolfSSL 7:481bce714567 411
wolfSSL 7:481bce714567 412
wolfSSL 7:481bce714567 413 #else /* HAVE_HC128 */
wolfSSL 7:481bce714567 414
wolfSSL 7:481bce714567 415
wolfSSL 7:481bce714567 416 #ifdef _MSC_VER
wolfSSL 7:481bce714567 417 /* 4206 warning for blank file */
wolfSSL 7:481bce714567 418 #pragma warning(disable: 4206)
wolfSSL 7:481bce714567 419 #endif
wolfSSL 7:481bce714567 420
wolfSSL 7:481bce714567 421
wolfSSL 7:481bce714567 422 #endif /* HAVE_HC128 */
wolfSSL 7:481bce714567 423