A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Committer:
Mike Fiore
Date:
Mon Mar 23 16:51:07 2015 -0500
Revision:
6:cf58d49e1a86
Parent:
0:b86d15c6ba29
fix whitespace in sha512.c

Who changed what in which revision?

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