andre@0: /* andre@0: * desblapi.c andre@0: * andre@0: * core source file for DES-150 library andre@0: * Implement DES Modes of Operation and Triple-DES. andre@0: * Adapt DES-150 to blapi API. andre@0: * andre@0: * This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifdef FREEBL_NO_DEPEND andre@0: #include "stubs.h" andre@0: #endif andre@0: andre@0: #include "des.h" andre@0: #include andre@0: #include "secerr.h" andre@0: andre@0: #if defined(NSS_X86_OR_X64) andre@0: /* Intel X86 CPUs do unaligned loads and stores without complaint. */ andre@0: #define COPY8B(to, from, ptr) \ andre@0: HALFPTR(to)[0] = HALFPTR(from)[0]; \ andre@0: HALFPTR(to)[1] = HALFPTR(from)[1]; andre@0: #elif defined(USE_MEMCPY) andre@0: #define COPY8B(to, from, ptr) memcpy(to, from, 8) andre@0: #else andre@0: #define COPY8B(to, from, ptr) \ andre@0: if (((ptrdiff_t)(ptr) & 0x3) == 0) { \ andre@0: HALFPTR(to)[0] = HALFPTR(from)[0]; \ andre@0: HALFPTR(to)[1] = HALFPTR(from)[1]; \ andre@0: } else if (((ptrdiff_t)(ptr) & 0x1) == 0) { \ andre@0: SHORTPTR(to)[0] = SHORTPTR(from)[0]; \ andre@0: SHORTPTR(to)[1] = SHORTPTR(from)[1]; \ andre@0: SHORTPTR(to)[2] = SHORTPTR(from)[2]; \ andre@0: SHORTPTR(to)[3] = SHORTPTR(from)[3]; \ andre@0: } else { \ andre@0: BYTEPTR(to)[0] = BYTEPTR(from)[0]; \ andre@0: BYTEPTR(to)[1] = BYTEPTR(from)[1]; \ andre@0: BYTEPTR(to)[2] = BYTEPTR(from)[2]; \ andre@0: BYTEPTR(to)[3] = BYTEPTR(from)[3]; \ andre@0: BYTEPTR(to)[4] = BYTEPTR(from)[4]; \ andre@0: BYTEPTR(to)[5] = BYTEPTR(from)[5]; \ andre@0: BYTEPTR(to)[6] = BYTEPTR(from)[6]; \ andre@0: BYTEPTR(to)[7] = BYTEPTR(from)[7]; \ andre@0: } andre@0: #endif andre@0: #define COPY8BTOHALF(to, from) COPY8B(to, from, from) andre@0: #define COPY8BFROMHALF(to, from) COPY8B(to, from, to) andre@0: andre@0: static void andre@0: DES_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) andre@0: { andre@0: while (len) { andre@0: DES_Do1Block(cx->ks0, in, out); andre@0: len -= 8; andre@0: in += 8; andre@0: out += 8; andre@0: } andre@0: } andre@0: andre@0: static void andre@0: DES_EDE3_ECB(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) andre@0: { andre@0: while (len) { andre@0: DES_Do1Block(cx->ks0, in, out); andre@0: len -= 8; andre@0: in += 8; andre@0: DES_Do1Block(cx->ks1, out, out); andre@0: DES_Do1Block(cx->ks2, out, out); andre@0: out += 8; andre@0: } andre@0: } andre@0: andre@0: static void andre@0: DES_CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) andre@0: { andre@0: const BYTE * bufend = in + len; andre@0: HALF vec[2]; andre@0: andre@0: while (in != bufend) { andre@0: COPY8BTOHALF(vec, in); andre@0: in += 8; andre@0: vec[0] ^= cx->iv[0]; andre@0: vec[1] ^= cx->iv[1]; andre@0: DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv); andre@0: COPY8BFROMHALF(out, cx->iv); andre@0: out += 8; andre@0: } andre@0: } andre@0: andre@0: static void andre@0: DES_CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) andre@0: { andre@0: const BYTE * bufend; andre@0: HALF oldciphertext[2]; andre@0: HALF plaintext [2]; andre@0: andre@0: for (bufend = in + len; in != bufend; ) { andre@0: oldciphertext[0] = cx->iv[0]; andre@0: oldciphertext[1] = cx->iv[1]; andre@0: COPY8BTOHALF(cx->iv, in); andre@0: in += 8; andre@0: DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext); andre@0: plaintext[0] ^= oldciphertext[0]; andre@0: plaintext[1] ^= oldciphertext[1]; andre@0: COPY8BFROMHALF(out, plaintext); andre@0: out += 8; andre@0: } andre@0: } andre@0: andre@0: static void andre@0: DES_EDE3CBCEn(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) andre@0: { andre@0: const BYTE * bufend = in + len; andre@0: HALF vec[2]; andre@0: andre@0: while (in != bufend) { andre@0: COPY8BTOHALF(vec, in); andre@0: in += 8; andre@0: vec[0] ^= cx->iv[0]; andre@0: vec[1] ^= cx->iv[1]; andre@0: DES_Do1Block( cx->ks0, (BYTE *)vec, (BYTE *)cx->iv); andre@0: DES_Do1Block( cx->ks1, (BYTE *)cx->iv, (BYTE *)cx->iv); andre@0: DES_Do1Block( cx->ks2, (BYTE *)cx->iv, (BYTE *)cx->iv); andre@0: COPY8BFROMHALF(out, cx->iv); andre@0: out += 8; andre@0: } andre@0: } andre@0: andre@0: static void andre@0: DES_EDE3CBCDe(DESContext *cx, BYTE *out, const BYTE *in, unsigned int len) andre@0: { andre@0: const BYTE * bufend; andre@0: HALF oldciphertext[2]; andre@0: HALF plaintext [2]; andre@0: andre@0: for (bufend = in + len; in != bufend; ) { andre@0: oldciphertext[0] = cx->iv[0]; andre@0: oldciphertext[1] = cx->iv[1]; andre@0: COPY8BTOHALF(cx->iv, in); andre@0: in += 8; andre@0: DES_Do1Block(cx->ks0, (BYTE *)cx->iv, (BYTE *)plaintext); andre@0: DES_Do1Block(cx->ks1, (BYTE *)plaintext, (BYTE *)plaintext); andre@0: DES_Do1Block(cx->ks2, (BYTE *)plaintext, (BYTE *)plaintext); andre@0: plaintext[0] ^= oldciphertext[0]; andre@0: plaintext[1] ^= oldciphertext[1]; andre@0: COPY8BFROMHALF(out, plaintext); andre@0: out += 8; andre@0: } andre@0: } andre@0: andre@0: DESContext * andre@0: DES_AllocateContext(void) andre@0: { andre@0: return PORT_ZNew(DESContext); andre@0: } andre@0: andre@0: SECStatus andre@0: DES_InitContext(DESContext *cx, const unsigned char *key, unsigned int keylen, andre@0: const unsigned char *iv, int mode, unsigned int encrypt, andre@0: unsigned int unused) andre@0: { andre@0: DESDirection opposite; andre@0: if (!cx) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: cx->direction = encrypt ? DES_ENCRYPT : DES_DECRYPT; andre@0: opposite = encrypt ? DES_DECRYPT : DES_ENCRYPT; andre@0: switch (mode) { andre@0: case NSS_DES: /* DES ECB */ andre@0: DES_MakeSchedule( cx->ks0, key, cx->direction); andre@0: cx->worker = &DES_ECB; andre@0: break; andre@0: andre@0: case NSS_DES_EDE3: /* DES EDE ECB */ andre@0: cx->worker = &DES_EDE3_ECB; andre@0: if (encrypt) { andre@0: DES_MakeSchedule(cx->ks0, key, cx->direction); andre@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); andre@0: DES_MakeSchedule(cx->ks2, key + 16, cx->direction); andre@0: } else { andre@0: DES_MakeSchedule(cx->ks2, key, cx->direction); andre@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); andre@0: DES_MakeSchedule(cx->ks0, key + 16, cx->direction); andre@0: } andre@0: break; andre@0: andre@0: case NSS_DES_CBC: /* DES CBC */ andre@0: COPY8BTOHALF(cx->iv, iv); andre@0: cx->worker = encrypt ? &DES_CBCEn : &DES_CBCDe; andre@0: DES_MakeSchedule(cx->ks0, key, cx->direction); andre@0: break; andre@0: andre@0: case NSS_DES_EDE3_CBC: /* DES EDE CBC */ andre@0: COPY8BTOHALF(cx->iv, iv); andre@0: if (encrypt) { andre@0: cx->worker = &DES_EDE3CBCEn; andre@0: DES_MakeSchedule(cx->ks0, key, cx->direction); andre@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); andre@0: DES_MakeSchedule(cx->ks2, key + 16, cx->direction); andre@0: } else { andre@0: cx->worker = &DES_EDE3CBCDe; andre@0: DES_MakeSchedule(cx->ks2, key, cx->direction); andre@0: DES_MakeSchedule(cx->ks1, key + 8, opposite); andre@0: DES_MakeSchedule(cx->ks0, key + 16, cx->direction); andre@0: } andre@0: break; andre@0: andre@0: default: andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: return SECSuccess; andre@0: } andre@0: andre@0: DESContext * andre@0: DES_CreateContext(const BYTE * key, const BYTE *iv, int mode, PRBool encrypt) andre@0: { andre@0: DESContext *cx = PORT_ZNew(DESContext); andre@0: SECStatus rv = DES_InitContext(cx, key, 0, iv, mode, encrypt, 0); andre@0: andre@0: if (rv != SECSuccess) { andre@0: PORT_ZFree(cx, sizeof *cx); andre@0: cx = NULL; andre@0: } andre@0: return cx; andre@0: } andre@0: andre@0: void andre@0: DES_DestroyContext(DESContext *cx, PRBool freeit) andre@0: { andre@0: if (cx) { andre@0: memset(cx, 0, sizeof *cx); andre@0: if (freeit) andre@0: PORT_Free(cx); andre@0: } andre@0: } andre@0: andre@0: SECStatus andre@0: DES_Encrypt(DESContext *cx, BYTE *out, unsigned int *outLen, andre@0: unsigned int maxOutLen, const BYTE *in, unsigned int inLen) andre@0: { andre@0: andre@0: if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || andre@0: cx->direction != DES_ENCRYPT) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: cx->worker(cx, out, in, inLen); andre@0: if (outLen) andre@0: *outLen = inLen; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: DES_Decrypt(DESContext *cx, BYTE *out, unsigned int *outLen, andre@0: unsigned int maxOutLen, const BYTE *in, unsigned int inLen) andre@0: { andre@0: andre@0: if ((inLen % 8) != 0 || maxOutLen < inLen || !cx || andre@0: cx->direction != DES_DECRYPT) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: cx->worker(cx, out, in, inLen); andre@0: if (outLen) andre@0: *outLen = inLen; andre@0: return SECSuccess; andre@0: }