Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * alg2268.c - implementation of the algorithm in RFC 2268 |
michael@0 | 3 | * |
michael@0 | 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #ifdef FREEBL_NO_DEPEND |
michael@0 | 9 | #include "stubs.h" |
michael@0 | 10 | #endif |
michael@0 | 11 | |
michael@0 | 12 | #include "blapi.h" |
michael@0 | 13 | #include "secerr.h" |
michael@0 | 14 | #ifdef XP_UNIX_XXX |
michael@0 | 15 | #include <stddef.h> /* for ptrdiff_t */ |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | /* |
michael@0 | 19 | ** RC2 symmetric block cypher |
michael@0 | 20 | */ |
michael@0 | 21 | |
michael@0 | 22 | typedef SECStatus (rc2Func)(RC2Context *cx, unsigned char *output, |
michael@0 | 23 | const unsigned char *input, unsigned int inputLen); |
michael@0 | 24 | |
michael@0 | 25 | /* forward declarations */ |
michael@0 | 26 | static rc2Func rc2_EncryptECB; |
michael@0 | 27 | static rc2Func rc2_DecryptECB; |
michael@0 | 28 | static rc2Func rc2_EncryptCBC; |
michael@0 | 29 | static rc2Func rc2_DecryptCBC; |
michael@0 | 30 | |
michael@0 | 31 | typedef union { |
michael@0 | 32 | PRUint32 l[2]; |
michael@0 | 33 | PRUint16 s[4]; |
michael@0 | 34 | PRUint8 b[8]; |
michael@0 | 35 | } RC2Block; |
michael@0 | 36 | |
michael@0 | 37 | struct RC2ContextStr { |
michael@0 | 38 | union { |
michael@0 | 39 | PRUint8 Kb[128]; |
michael@0 | 40 | PRUint16 Kw[64]; |
michael@0 | 41 | } u; |
michael@0 | 42 | RC2Block iv; |
michael@0 | 43 | rc2Func *enc; |
michael@0 | 44 | rc2Func *dec; |
michael@0 | 45 | }; |
michael@0 | 46 | |
michael@0 | 47 | #define B u.Kb |
michael@0 | 48 | #define K u.Kw |
michael@0 | 49 | #define BYTESWAP(x) ((x) << 8 | (x) >> 8) |
michael@0 | 50 | #define SWAPK(i) cx->K[i] = (tmpS = cx->K[i], BYTESWAP(tmpS)) |
michael@0 | 51 | #define RC2_BLOCK_SIZE 8 |
michael@0 | 52 | |
michael@0 | 53 | #define LOAD_HARD(R) \ |
michael@0 | 54 | R[0] = (PRUint16)input[1] << 8 | input[0]; \ |
michael@0 | 55 | R[1] = (PRUint16)input[3] << 8 | input[2]; \ |
michael@0 | 56 | R[2] = (PRUint16)input[5] << 8 | input[4]; \ |
michael@0 | 57 | R[3] = (PRUint16)input[7] << 8 | input[6]; |
michael@0 | 58 | #define LOAD_EASY(R) \ |
michael@0 | 59 | R[0] = ((PRUint16 *)input)[0]; \ |
michael@0 | 60 | R[1] = ((PRUint16 *)input)[1]; \ |
michael@0 | 61 | R[2] = ((PRUint16 *)input)[2]; \ |
michael@0 | 62 | R[3] = ((PRUint16 *)input)[3]; |
michael@0 | 63 | #define STORE_HARD(R) \ |
michael@0 | 64 | output[0] = (PRUint8)(R[0]); output[1] = (PRUint8)(R[0] >> 8); \ |
michael@0 | 65 | output[2] = (PRUint8)(R[1]); output[3] = (PRUint8)(R[1] >> 8); \ |
michael@0 | 66 | output[4] = (PRUint8)(R[2]); output[5] = (PRUint8)(R[2] >> 8); \ |
michael@0 | 67 | output[6] = (PRUint8)(R[3]); output[7] = (PRUint8)(R[3] >> 8); |
michael@0 | 68 | #define STORE_EASY(R) \ |
michael@0 | 69 | ((PRUint16 *)output)[0] = R[0]; \ |
michael@0 | 70 | ((PRUint16 *)output)[1] = R[1]; \ |
michael@0 | 71 | ((PRUint16 *)output)[2] = R[2]; \ |
michael@0 | 72 | ((PRUint16 *)output)[3] = R[3]; |
michael@0 | 73 | |
michael@0 | 74 | #if defined (NSS_X86_OR_X64) |
michael@0 | 75 | #define LOAD(R) LOAD_EASY(R) |
michael@0 | 76 | #define STORE(R) STORE_EASY(R) |
michael@0 | 77 | #elif !defined(IS_LITTLE_ENDIAN) |
michael@0 | 78 | #define LOAD(R) LOAD_HARD(R) |
michael@0 | 79 | #define STORE(R) STORE_HARD(R) |
michael@0 | 80 | #else |
michael@0 | 81 | #define LOAD(R) if ((ptrdiff_t)input & 1) { LOAD_HARD(R) } else { LOAD_EASY(R) } |
michael@0 | 82 | #define STORE(R) if ((ptrdiff_t)input & 1) { STORE_HARD(R) } else { STORE_EASY(R) } |
michael@0 | 83 | #endif |
michael@0 | 84 | |
michael@0 | 85 | static const PRUint8 S[256] = { |
michael@0 | 86 | 0331,0170,0371,0304,0031,0335,0265,0355,0050,0351,0375,0171,0112,0240,0330,0235, |
michael@0 | 87 | 0306,0176,0067,0203,0053,0166,0123,0216,0142,0114,0144,0210,0104,0213,0373,0242, |
michael@0 | 88 | 0027,0232,0131,0365,0207,0263,0117,0023,0141,0105,0155,0215,0011,0201,0175,0062, |
michael@0 | 89 | 0275,0217,0100,0353,0206,0267,0173,0013,0360,0225,0041,0042,0134,0153,0116,0202, |
michael@0 | 90 | 0124,0326,0145,0223,0316,0140,0262,0034,0163,0126,0300,0024,0247,0214,0361,0334, |
michael@0 | 91 | 0022,0165,0312,0037,0073,0276,0344,0321,0102,0075,0324,0060,0243,0074,0266,0046, |
michael@0 | 92 | 0157,0277,0016,0332,0106,0151,0007,0127,0047,0362,0035,0233,0274,0224,0103,0003, |
michael@0 | 93 | 0370,0021,0307,0366,0220,0357,0076,0347,0006,0303,0325,0057,0310,0146,0036,0327, |
michael@0 | 94 | 0010,0350,0352,0336,0200,0122,0356,0367,0204,0252,0162,0254,0065,0115,0152,0052, |
michael@0 | 95 | 0226,0032,0322,0161,0132,0025,0111,0164,0113,0237,0320,0136,0004,0030,0244,0354, |
michael@0 | 96 | 0302,0340,0101,0156,0017,0121,0313,0314,0044,0221,0257,0120,0241,0364,0160,0071, |
michael@0 | 97 | 0231,0174,0072,0205,0043,0270,0264,0172,0374,0002,0066,0133,0045,0125,0227,0061, |
michael@0 | 98 | 0055,0135,0372,0230,0343,0212,0222,0256,0005,0337,0051,0020,0147,0154,0272,0311, |
michael@0 | 99 | 0323,0000,0346,0317,0341,0236,0250,0054,0143,0026,0001,0077,0130,0342,0211,0251, |
michael@0 | 100 | 0015,0070,0064,0033,0253,0063,0377,0260,0273,0110,0014,0137,0271,0261,0315,0056, |
michael@0 | 101 | 0305,0363,0333,0107,0345,0245,0234,0167,0012,0246,0040,0150,0376,0177,0301,0255 |
michael@0 | 102 | }; |
michael@0 | 103 | |
michael@0 | 104 | RC2Context * RC2_AllocateContext(void) |
michael@0 | 105 | { |
michael@0 | 106 | return PORT_ZNew(RC2Context); |
michael@0 | 107 | } |
michael@0 | 108 | SECStatus |
michael@0 | 109 | RC2_InitContext(RC2Context *cx, const unsigned char *key, unsigned int len, |
michael@0 | 110 | const unsigned char *input, int mode, unsigned int efLen8, |
michael@0 | 111 | unsigned int unused) |
michael@0 | 112 | { |
michael@0 | 113 | PRUint8 *L,*L2; |
michael@0 | 114 | int i; |
michael@0 | 115 | #if !defined(IS_LITTLE_ENDIAN) |
michael@0 | 116 | PRUint16 tmpS; |
michael@0 | 117 | #endif |
michael@0 | 118 | PRUint8 tmpB; |
michael@0 | 119 | |
michael@0 | 120 | if (!key || !cx || !len || len > (sizeof cx->B) || |
michael@0 | 121 | efLen8 > (sizeof cx->B)) { |
michael@0 | 122 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
michael@0 | 123 | return SECFailure; |
michael@0 | 124 | } |
michael@0 | 125 | if (mode == NSS_RC2) { |
michael@0 | 126 | /* groovy */ |
michael@0 | 127 | } else if (mode == NSS_RC2_CBC) { |
michael@0 | 128 | if (!input) { |
michael@0 | 129 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
michael@0 | 130 | return SECFailure; |
michael@0 | 131 | } |
michael@0 | 132 | } else { |
michael@0 | 133 | PORT_SetError(SEC_ERROR_INVALID_ARGS); |
michael@0 | 134 | return SECFailure; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | if (mode == NSS_RC2_CBC) { |
michael@0 | 138 | cx->enc = & rc2_EncryptCBC; |
michael@0 | 139 | cx->dec = & rc2_DecryptCBC; |
michael@0 | 140 | LOAD(cx->iv.s); |
michael@0 | 141 | } else { |
michael@0 | 142 | cx->enc = & rc2_EncryptECB; |
michael@0 | 143 | cx->dec = & rc2_DecryptECB; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | /* Step 0. Copy key into table. */ |
michael@0 | 147 | memcpy(cx->B, key, len); |
michael@0 | 148 | |
michael@0 | 149 | /* Step 1. Compute all values to the right of the key. */ |
michael@0 | 150 | L2 = cx->B; |
michael@0 | 151 | L = L2 + len; |
michael@0 | 152 | tmpB = L[-1]; |
michael@0 | 153 | for (i = (sizeof cx->B) - len; i > 0; --i) { |
michael@0 | 154 | *L++ = tmpB = S[ (PRUint8)(tmpB + *L2++) ]; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | /* step 2. Adjust left most byte of effective key. */ |
michael@0 | 158 | i = (sizeof cx->B) - efLen8; |
michael@0 | 159 | L = cx->B + i; |
michael@0 | 160 | *L = tmpB = S[*L]; /* mask is always 0xff */ |
michael@0 | 161 | |
michael@0 | 162 | /* step 3. Recompute all values to the left of effective key. */ |
michael@0 | 163 | L2 = --L + efLen8; |
michael@0 | 164 | while(L >= cx->B) { |
michael@0 | 165 | *L-- = tmpB = S[ tmpB ^ *L2-- ]; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | #if !defined(IS_LITTLE_ENDIAN) |
michael@0 | 169 | for (i = 63; i >= 0; --i) { |
michael@0 | 170 | SWAPK(i); /* candidate for unrolling */ |
michael@0 | 171 | } |
michael@0 | 172 | #endif |
michael@0 | 173 | return SECSuccess; |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | /* |
michael@0 | 177 | ** Create a new RC2 context suitable for RC2 encryption/decryption. |
michael@0 | 178 | ** "key" raw key data |
michael@0 | 179 | ** "len" the number of bytes of key data |
michael@0 | 180 | ** "iv" is the CBC initialization vector (if mode is NSS_RC2_CBC) |
michael@0 | 181 | ** "mode" one of NSS_RC2 or NSS_RC2_CBC |
michael@0 | 182 | ** "effectiveKeyLen" in bytes, not bits. |
michael@0 | 183 | ** |
michael@0 | 184 | ** When mode is set to NSS_RC2_CBC the RC2 cipher is run in "cipher block |
michael@0 | 185 | ** chaining" mode. |
michael@0 | 186 | */ |
michael@0 | 187 | RC2Context * |
michael@0 | 188 | RC2_CreateContext(const unsigned char *key, unsigned int len, |
michael@0 | 189 | const unsigned char *iv, int mode, unsigned efLen8) |
michael@0 | 190 | { |
michael@0 | 191 | RC2Context *cx = PORT_ZNew(RC2Context); |
michael@0 | 192 | if (cx) { |
michael@0 | 193 | SECStatus rv = RC2_InitContext(cx, key, len, iv, mode, efLen8, 0); |
michael@0 | 194 | if (rv != SECSuccess) { |
michael@0 | 195 | RC2_DestroyContext(cx, PR_TRUE); |
michael@0 | 196 | cx = NULL; |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | return cx; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | /* |
michael@0 | 203 | ** Destroy an RC2 encryption/decryption context. |
michael@0 | 204 | ** "cx" the context |
michael@0 | 205 | ** "freeit" if PR_TRUE then free the object as well as its sub-objects |
michael@0 | 206 | */ |
michael@0 | 207 | void |
michael@0 | 208 | RC2_DestroyContext(RC2Context *cx, PRBool freeit) |
michael@0 | 209 | { |
michael@0 | 210 | if (cx) { |
michael@0 | 211 | memset(cx, 0, sizeof *cx); |
michael@0 | 212 | if (freeit) { |
michael@0 | 213 | PORT_Free(cx); |
michael@0 | 214 | } |
michael@0 | 215 | } |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | #define ROL(x,k) (x << k | x >> (16-k)) |
michael@0 | 219 | #define MIX(j) \ |
michael@0 | 220 | R0 = R0 + cx->K[ 4*j+0] + (R3 & R2) + (~R3 & R1); R0 = ROL(R0,1);\ |
michael@0 | 221 | R1 = R1 + cx->K[ 4*j+1] + (R0 & R3) + (~R0 & R2); R1 = ROL(R1,2);\ |
michael@0 | 222 | R2 = R2 + cx->K[ 4*j+2] + (R1 & R0) + (~R1 & R3); R2 = ROL(R2,3);\ |
michael@0 | 223 | R3 = R3 + cx->K[ 4*j+3] + (R2 & R1) + (~R2 & R0); R3 = ROL(R3,5) |
michael@0 | 224 | #define MASH \ |
michael@0 | 225 | R0 = R0 + cx->K[R3 & 63];\ |
michael@0 | 226 | R1 = R1 + cx->K[R0 & 63];\ |
michael@0 | 227 | R2 = R2 + cx->K[R1 & 63];\ |
michael@0 | 228 | R3 = R3 + cx->K[R2 & 63] |
michael@0 | 229 | |
michael@0 | 230 | /* Encrypt one block */ |
michael@0 | 231 | static void |
michael@0 | 232 | rc2_Encrypt1Block(RC2Context *cx, RC2Block *output, RC2Block *input) |
michael@0 | 233 | { |
michael@0 | 234 | register PRUint16 R0, R1, R2, R3; |
michael@0 | 235 | |
michael@0 | 236 | /* step 1. Initialize input. */ |
michael@0 | 237 | R0 = input->s[0]; |
michael@0 | 238 | R1 = input->s[1]; |
michael@0 | 239 | R2 = input->s[2]; |
michael@0 | 240 | R3 = input->s[3]; |
michael@0 | 241 | |
michael@0 | 242 | /* step 2. Expand Key (already done, in context) */ |
michael@0 | 243 | /* step 3. j = 0 */ |
michael@0 | 244 | /* step 4. Perform 5 mixing rounds. */ |
michael@0 | 245 | |
michael@0 | 246 | MIX(0); |
michael@0 | 247 | MIX(1); |
michael@0 | 248 | MIX(2); |
michael@0 | 249 | MIX(3); |
michael@0 | 250 | MIX(4); |
michael@0 | 251 | |
michael@0 | 252 | /* step 5. Perform 1 mashing round. */ |
michael@0 | 253 | MASH; |
michael@0 | 254 | |
michael@0 | 255 | /* step 6. Perform 6 mixing rounds. */ |
michael@0 | 256 | |
michael@0 | 257 | MIX(5); |
michael@0 | 258 | MIX(6); |
michael@0 | 259 | MIX(7); |
michael@0 | 260 | MIX(8); |
michael@0 | 261 | MIX(9); |
michael@0 | 262 | MIX(10); |
michael@0 | 263 | |
michael@0 | 264 | /* step 7. Perform 1 mashing round. */ |
michael@0 | 265 | MASH; |
michael@0 | 266 | |
michael@0 | 267 | /* step 8. Perform 5 mixing rounds. */ |
michael@0 | 268 | |
michael@0 | 269 | MIX(11); |
michael@0 | 270 | MIX(12); |
michael@0 | 271 | MIX(13); |
michael@0 | 272 | MIX(14); |
michael@0 | 273 | MIX(15); |
michael@0 | 274 | |
michael@0 | 275 | /* output results */ |
michael@0 | 276 | output->s[0] = R0; |
michael@0 | 277 | output->s[1] = R1; |
michael@0 | 278 | output->s[2] = R2; |
michael@0 | 279 | output->s[3] = R3; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | #define ROR(x,k) (x >> k | x << (16-k)) |
michael@0 | 283 | #define R_MIX(j) \ |
michael@0 | 284 | R3 = ROR(R3,5); R3 = R3 - cx->K[ 4*j+3] - (R2 & R1) - (~R2 & R0); \ |
michael@0 | 285 | R2 = ROR(R2,3); R2 = R2 - cx->K[ 4*j+2] - (R1 & R0) - (~R1 & R3); \ |
michael@0 | 286 | R1 = ROR(R1,2); R1 = R1 - cx->K[ 4*j+1] - (R0 & R3) - (~R0 & R2); \ |
michael@0 | 287 | R0 = ROR(R0,1); R0 = R0 - cx->K[ 4*j+0] - (R3 & R2) - (~R3 & R1) |
michael@0 | 288 | #define R_MASH \ |
michael@0 | 289 | R3 = R3 - cx->K[R2 & 63];\ |
michael@0 | 290 | R2 = R2 - cx->K[R1 & 63];\ |
michael@0 | 291 | R1 = R1 - cx->K[R0 & 63];\ |
michael@0 | 292 | R0 = R0 - cx->K[R3 & 63] |
michael@0 | 293 | |
michael@0 | 294 | /* Encrypt one block */ |
michael@0 | 295 | static void |
michael@0 | 296 | rc2_Decrypt1Block(RC2Context *cx, RC2Block *output, RC2Block *input) |
michael@0 | 297 | { |
michael@0 | 298 | register PRUint16 R0, R1, R2, R3; |
michael@0 | 299 | |
michael@0 | 300 | /* step 1. Initialize input. */ |
michael@0 | 301 | R0 = input->s[0]; |
michael@0 | 302 | R1 = input->s[1]; |
michael@0 | 303 | R2 = input->s[2]; |
michael@0 | 304 | R3 = input->s[3]; |
michael@0 | 305 | |
michael@0 | 306 | /* step 2. Expand Key (already done, in context) */ |
michael@0 | 307 | /* step 3. j = 63 */ |
michael@0 | 308 | /* step 4. Perform 5 r_mixing rounds. */ |
michael@0 | 309 | R_MIX(15); |
michael@0 | 310 | R_MIX(14); |
michael@0 | 311 | R_MIX(13); |
michael@0 | 312 | R_MIX(12); |
michael@0 | 313 | R_MIX(11); |
michael@0 | 314 | |
michael@0 | 315 | /* step 5. Perform 1 r_mashing round. */ |
michael@0 | 316 | R_MASH; |
michael@0 | 317 | |
michael@0 | 318 | /* step 6. Perform 6 r_mixing rounds. */ |
michael@0 | 319 | R_MIX(10); |
michael@0 | 320 | R_MIX(9); |
michael@0 | 321 | R_MIX(8); |
michael@0 | 322 | R_MIX(7); |
michael@0 | 323 | R_MIX(6); |
michael@0 | 324 | R_MIX(5); |
michael@0 | 325 | |
michael@0 | 326 | /* step 7. Perform 1 r_mashing round. */ |
michael@0 | 327 | R_MASH; |
michael@0 | 328 | |
michael@0 | 329 | /* step 8. Perform 5 r_mixing rounds. */ |
michael@0 | 330 | R_MIX(4); |
michael@0 | 331 | R_MIX(3); |
michael@0 | 332 | R_MIX(2); |
michael@0 | 333 | R_MIX(1); |
michael@0 | 334 | R_MIX(0); |
michael@0 | 335 | |
michael@0 | 336 | /* output results */ |
michael@0 | 337 | output->s[0] = R0; |
michael@0 | 338 | output->s[1] = R1; |
michael@0 | 339 | output->s[2] = R2; |
michael@0 | 340 | output->s[3] = R3; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | static SECStatus |
michael@0 | 344 | rc2_EncryptECB(RC2Context *cx, unsigned char *output, |
michael@0 | 345 | const unsigned char *input, unsigned int inputLen) |
michael@0 | 346 | { |
michael@0 | 347 | RC2Block iBlock; |
michael@0 | 348 | |
michael@0 | 349 | while (inputLen > 0) { |
michael@0 | 350 | LOAD(iBlock.s) |
michael@0 | 351 | rc2_Encrypt1Block(cx, &iBlock, &iBlock); |
michael@0 | 352 | STORE(iBlock.s) |
michael@0 | 353 | output += RC2_BLOCK_SIZE; |
michael@0 | 354 | input += RC2_BLOCK_SIZE; |
michael@0 | 355 | inputLen -= RC2_BLOCK_SIZE; |
michael@0 | 356 | } |
michael@0 | 357 | return SECSuccess; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | static SECStatus |
michael@0 | 361 | rc2_DecryptECB(RC2Context *cx, unsigned char *output, |
michael@0 | 362 | const unsigned char *input, unsigned int inputLen) |
michael@0 | 363 | { |
michael@0 | 364 | RC2Block iBlock; |
michael@0 | 365 | |
michael@0 | 366 | while (inputLen > 0) { |
michael@0 | 367 | LOAD(iBlock.s) |
michael@0 | 368 | rc2_Decrypt1Block(cx, &iBlock, &iBlock); |
michael@0 | 369 | STORE(iBlock.s) |
michael@0 | 370 | output += RC2_BLOCK_SIZE; |
michael@0 | 371 | input += RC2_BLOCK_SIZE; |
michael@0 | 372 | inputLen -= RC2_BLOCK_SIZE; |
michael@0 | 373 | } |
michael@0 | 374 | return SECSuccess; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | static SECStatus |
michael@0 | 378 | rc2_EncryptCBC(RC2Context *cx, unsigned char *output, |
michael@0 | 379 | const unsigned char *input, unsigned int inputLen) |
michael@0 | 380 | { |
michael@0 | 381 | RC2Block iBlock; |
michael@0 | 382 | |
michael@0 | 383 | while (inputLen > 0) { |
michael@0 | 384 | |
michael@0 | 385 | LOAD(iBlock.s) |
michael@0 | 386 | iBlock.l[0] ^= cx->iv.l[0]; |
michael@0 | 387 | iBlock.l[1] ^= cx->iv.l[1]; |
michael@0 | 388 | rc2_Encrypt1Block(cx, &iBlock, &iBlock); |
michael@0 | 389 | cx->iv = iBlock; |
michael@0 | 390 | STORE(iBlock.s) |
michael@0 | 391 | output += RC2_BLOCK_SIZE; |
michael@0 | 392 | input += RC2_BLOCK_SIZE; |
michael@0 | 393 | inputLen -= RC2_BLOCK_SIZE; |
michael@0 | 394 | } |
michael@0 | 395 | return SECSuccess; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | static SECStatus |
michael@0 | 399 | rc2_DecryptCBC(RC2Context *cx, unsigned char *output, |
michael@0 | 400 | const unsigned char *input, unsigned int inputLen) |
michael@0 | 401 | { |
michael@0 | 402 | RC2Block iBlock; |
michael@0 | 403 | RC2Block oBlock; |
michael@0 | 404 | |
michael@0 | 405 | while (inputLen > 0) { |
michael@0 | 406 | LOAD(iBlock.s) |
michael@0 | 407 | rc2_Decrypt1Block(cx, &oBlock, &iBlock); |
michael@0 | 408 | oBlock.l[0] ^= cx->iv.l[0]; |
michael@0 | 409 | oBlock.l[1] ^= cx->iv.l[1]; |
michael@0 | 410 | cx->iv = iBlock; |
michael@0 | 411 | STORE(oBlock.s) |
michael@0 | 412 | output += RC2_BLOCK_SIZE; |
michael@0 | 413 | input += RC2_BLOCK_SIZE; |
michael@0 | 414 | inputLen -= RC2_BLOCK_SIZE; |
michael@0 | 415 | } |
michael@0 | 416 | return SECSuccess; |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | |
michael@0 | 420 | /* |
michael@0 | 421 | ** Perform RC2 encryption. |
michael@0 | 422 | ** "cx" the context |
michael@0 | 423 | ** "output" the output buffer to store the encrypted data. |
michael@0 | 424 | ** "outputLen" how much data is stored in "output". Set by the routine |
michael@0 | 425 | ** after some data is stored in output. |
michael@0 | 426 | ** "maxOutputLen" the maximum amount of data that can ever be |
michael@0 | 427 | ** stored in "output" |
michael@0 | 428 | ** "input" the input data |
michael@0 | 429 | ** "inputLen" the amount of input data |
michael@0 | 430 | */ |
michael@0 | 431 | SECStatus RC2_Encrypt(RC2Context *cx, unsigned char *output, |
michael@0 | 432 | unsigned int *outputLen, unsigned int maxOutputLen, |
michael@0 | 433 | const unsigned char *input, unsigned int inputLen) |
michael@0 | 434 | { |
michael@0 | 435 | SECStatus rv = SECSuccess; |
michael@0 | 436 | if (inputLen) { |
michael@0 | 437 | if (inputLen % RC2_BLOCK_SIZE) { |
michael@0 | 438 | PORT_SetError(SEC_ERROR_INPUT_LEN); |
michael@0 | 439 | return SECFailure; |
michael@0 | 440 | } |
michael@0 | 441 | if (maxOutputLen < inputLen) { |
michael@0 | 442 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
michael@0 | 443 | return SECFailure; |
michael@0 | 444 | } |
michael@0 | 445 | rv = (*cx->enc)(cx, output, input, inputLen); |
michael@0 | 446 | } |
michael@0 | 447 | if (rv == SECSuccess) { |
michael@0 | 448 | *outputLen = inputLen; |
michael@0 | 449 | } |
michael@0 | 450 | return rv; |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | /* |
michael@0 | 454 | ** Perform RC2 decryption. |
michael@0 | 455 | ** "cx" the context |
michael@0 | 456 | ** "output" the output buffer to store the decrypted data. |
michael@0 | 457 | ** "outputLen" how much data is stored in "output". Set by the routine |
michael@0 | 458 | ** after some data is stored in output. |
michael@0 | 459 | ** "maxOutputLen" the maximum amount of data that can ever be |
michael@0 | 460 | ** stored in "output" |
michael@0 | 461 | ** "input" the input data |
michael@0 | 462 | ** "inputLen" the amount of input data |
michael@0 | 463 | */ |
michael@0 | 464 | SECStatus RC2_Decrypt(RC2Context *cx, unsigned char *output, |
michael@0 | 465 | unsigned int *outputLen, unsigned int maxOutputLen, |
michael@0 | 466 | const unsigned char *input, unsigned int inputLen) |
michael@0 | 467 | { |
michael@0 | 468 | SECStatus rv = SECSuccess; |
michael@0 | 469 | if (inputLen) { |
michael@0 | 470 | if (inputLen % RC2_BLOCK_SIZE) { |
michael@0 | 471 | PORT_SetError(SEC_ERROR_INPUT_LEN); |
michael@0 | 472 | return SECFailure; |
michael@0 | 473 | } |
michael@0 | 474 | if (maxOutputLen < inputLen) { |
michael@0 | 475 | PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
michael@0 | 476 | return SECFailure; |
michael@0 | 477 | } |
michael@0 | 478 | rv = (*cx->dec)(cx, output, input, inputLen); |
michael@0 | 479 | } |
michael@0 | 480 | if (rv == SECSuccess) { |
michael@0 | 481 | *outputLen = inputLen; |
michael@0 | 482 | } |
michael@0 | 483 | return rv; |
michael@0 | 484 | } |
michael@0 | 485 |