wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 06:16:19 2017 +0000
Revision:
13:80fb167dafdf
Parent:
11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta

Who changed what in which revision?

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