Webserver+3d print
cyclone_crypto/des3.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file des3.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief Triple DES (Triple Data Encryption Algorithm) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneCrypto Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @section Description |
Sergunb | 0:8918a71cdbe9 | 26 | * |
Sergunb | 0:8918a71cdbe9 | 27 | * Triple DES is an encryption algorithm designed to encipher and decipher blocks |
Sergunb | 0:8918a71cdbe9 | 28 | * of 64 bits under control of a 192-bit key. Refer to FIPS 46-3 for more details |
Sergunb | 0:8918a71cdbe9 | 29 | * |
Sergunb | 0:8918a71cdbe9 | 30 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 31 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 32 | **/ |
Sergunb | 0:8918a71cdbe9 | 33 | |
Sergunb | 0:8918a71cdbe9 | 34 | //Switch to the appropriate trace level |
Sergunb | 0:8918a71cdbe9 | 35 | #define TRACE_LEVEL CRYPTO_TRACE_LEVEL |
Sergunb | 0:8918a71cdbe9 | 36 | |
Sergunb | 0:8918a71cdbe9 | 37 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 38 | #include <string.h> |
Sergunb | 0:8918a71cdbe9 | 39 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 40 | #include "des3.h" |
Sergunb | 0:8918a71cdbe9 | 41 | #include "des.h" |
Sergunb | 0:8918a71cdbe9 | 42 | |
Sergunb | 0:8918a71cdbe9 | 43 | //Check crypto library configuration |
Sergunb | 0:8918a71cdbe9 | 44 | #if (DES3_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 45 | |
Sergunb | 0:8918a71cdbe9 | 46 | //Common interface for encryption algorithms |
Sergunb | 0:8918a71cdbe9 | 47 | const CipherAlgo des3CipherAlgo = |
Sergunb | 0:8918a71cdbe9 | 48 | { |
Sergunb | 0:8918a71cdbe9 | 49 | "3DES", |
Sergunb | 0:8918a71cdbe9 | 50 | sizeof(Des3Context), |
Sergunb | 0:8918a71cdbe9 | 51 | CIPHER_ALGO_TYPE_BLOCK, |
Sergunb | 0:8918a71cdbe9 | 52 | DES3_BLOCK_SIZE, |
Sergunb | 0:8918a71cdbe9 | 53 | (CipherAlgoInit) des3Init, |
Sergunb | 0:8918a71cdbe9 | 54 | NULL, |
Sergunb | 0:8918a71cdbe9 | 55 | NULL, |
Sergunb | 0:8918a71cdbe9 | 56 | (CipherAlgoEncryptBlock) des3EncryptBlock, |
Sergunb | 0:8918a71cdbe9 | 57 | (CipherAlgoDecryptBlock) des3DecryptBlock |
Sergunb | 0:8918a71cdbe9 | 58 | }; |
Sergunb | 0:8918a71cdbe9 | 59 | |
Sergunb | 0:8918a71cdbe9 | 60 | |
Sergunb | 0:8918a71cdbe9 | 61 | /** |
Sergunb | 0:8918a71cdbe9 | 62 | * @brief Initialize a Triple DES context using the supplied key |
Sergunb | 0:8918a71cdbe9 | 63 | * @param[in] context Pointer to the Triple DES context to initialize |
Sergunb | 0:8918a71cdbe9 | 64 | * @param[in] key Pointer to the key |
Sergunb | 0:8918a71cdbe9 | 65 | * @param[in] keyLength Length of the key |
Sergunb | 0:8918a71cdbe9 | 66 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 67 | **/ |
Sergunb | 0:8918a71cdbe9 | 68 | |
Sergunb | 0:8918a71cdbe9 | 69 | error_t des3Init(Des3Context *context, const uint8_t *key, size_t keyLength) |
Sergunb | 0:8918a71cdbe9 | 70 | { |
Sergunb | 0:8918a71cdbe9 | 71 | //Check key length |
Sergunb | 0:8918a71cdbe9 | 72 | if(keyLength == 8) |
Sergunb | 0:8918a71cdbe9 | 73 | { |
Sergunb | 0:8918a71cdbe9 | 74 | //This option provides backward compatibility with DES, because the |
Sergunb | 0:8918a71cdbe9 | 75 | //first and second DES operations cancel out |
Sergunb | 0:8918a71cdbe9 | 76 | desInit(&context->k1, key, 8); |
Sergunb | 0:8918a71cdbe9 | 77 | desInit(&context->k2, key, 8); |
Sergunb | 0:8918a71cdbe9 | 78 | desInit(&context->k3, key, 8); |
Sergunb | 0:8918a71cdbe9 | 79 | } |
Sergunb | 0:8918a71cdbe9 | 80 | else if(keyLength == 16) |
Sergunb | 0:8918a71cdbe9 | 81 | { |
Sergunb | 0:8918a71cdbe9 | 82 | //If the key length is 128 bits including parity, the first 8 bytes of the |
Sergunb | 0:8918a71cdbe9 | 83 | //encoding represent the key used for the two outer DES operations, and |
Sergunb | 0:8918a71cdbe9 | 84 | //the second 8 bytes represent the key used for the inner DES operation |
Sergunb | 0:8918a71cdbe9 | 85 | desInit(&context->k1, key, 8); |
Sergunb | 0:8918a71cdbe9 | 86 | desInit(&context->k2, key + 8, 8); |
Sergunb | 0:8918a71cdbe9 | 87 | desInit(&context->k3, key, 8); |
Sergunb | 0:8918a71cdbe9 | 88 | } |
Sergunb | 0:8918a71cdbe9 | 89 | else if(keyLength == 24) |
Sergunb | 0:8918a71cdbe9 | 90 | { |
Sergunb | 0:8918a71cdbe9 | 91 | //If the key length is 192 bits including parity, then three independent DES |
Sergunb | 0:8918a71cdbe9 | 92 | //keys are represented, in the order in which they are used for encryption |
Sergunb | 0:8918a71cdbe9 | 93 | desInit(&context->k1, key, 8); |
Sergunb | 0:8918a71cdbe9 | 94 | desInit(&context->k2, key + 8, 8); |
Sergunb | 0:8918a71cdbe9 | 95 | desInit(&context->k3, key + 16, 8); |
Sergunb | 0:8918a71cdbe9 | 96 | } |
Sergunb | 0:8918a71cdbe9 | 97 | else |
Sergunb | 0:8918a71cdbe9 | 98 | { |
Sergunb | 0:8918a71cdbe9 | 99 | //Invalid key length... |
Sergunb | 0:8918a71cdbe9 | 100 | return ERROR_INVALID_KEY_LENGTH; |
Sergunb | 0:8918a71cdbe9 | 101 | } |
Sergunb | 0:8918a71cdbe9 | 102 | |
Sergunb | 0:8918a71cdbe9 | 103 | //No error to report |
Sergunb | 0:8918a71cdbe9 | 104 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 105 | } |
Sergunb | 0:8918a71cdbe9 | 106 | |
Sergunb | 0:8918a71cdbe9 | 107 | |
Sergunb | 0:8918a71cdbe9 | 108 | /** |
Sergunb | 0:8918a71cdbe9 | 109 | * @brief Encrypt a 8-byte block using Triple DES algorithm |
Sergunb | 0:8918a71cdbe9 | 110 | * @param[in] context Pointer to the Triple DES context |
Sergunb | 0:8918a71cdbe9 | 111 | * @param[in] input Plaintext block to encrypt |
Sergunb | 0:8918a71cdbe9 | 112 | * @param[out] output Ciphertext block resulting from encryption |
Sergunb | 0:8918a71cdbe9 | 113 | **/ |
Sergunb | 0:8918a71cdbe9 | 114 | |
Sergunb | 0:8918a71cdbe9 | 115 | void des3EncryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output) |
Sergunb | 0:8918a71cdbe9 | 116 | { |
Sergunb | 0:8918a71cdbe9 | 117 | //The first pass is a DES encryption |
Sergunb | 0:8918a71cdbe9 | 118 | desEncryptBlock(&context->k1, input, output); |
Sergunb | 0:8918a71cdbe9 | 119 | //The second pass is a DES decryption of the first ciphertext result |
Sergunb | 0:8918a71cdbe9 | 120 | desDecryptBlock(&context->k2, output, output); |
Sergunb | 0:8918a71cdbe9 | 121 | //The third pass is a DES encryption of the second pass result |
Sergunb | 0:8918a71cdbe9 | 122 | desEncryptBlock(&context->k3, output, output); |
Sergunb | 0:8918a71cdbe9 | 123 | } |
Sergunb | 0:8918a71cdbe9 | 124 | |
Sergunb | 0:8918a71cdbe9 | 125 | |
Sergunb | 0:8918a71cdbe9 | 126 | /** |
Sergunb | 0:8918a71cdbe9 | 127 | * @brief Decrypt a 8-byte block using Triple DES algorithm |
Sergunb | 0:8918a71cdbe9 | 128 | * @param[in] context Pointer to the Triple DES context |
Sergunb | 0:8918a71cdbe9 | 129 | * @param[in] input Ciphertext block to decrypt |
Sergunb | 0:8918a71cdbe9 | 130 | * @param[out] output Plaintext block resulting from decryption |
Sergunb | 0:8918a71cdbe9 | 131 | **/ |
Sergunb | 0:8918a71cdbe9 | 132 | |
Sergunb | 0:8918a71cdbe9 | 133 | void des3DecryptBlock(Des3Context *context, const uint8_t *input, uint8_t *output) |
Sergunb | 0:8918a71cdbe9 | 134 | { |
Sergunb | 0:8918a71cdbe9 | 135 | //The first pass is a DES decryption |
Sergunb | 0:8918a71cdbe9 | 136 | desDecryptBlock(&context->k3, input, output); |
Sergunb | 0:8918a71cdbe9 | 137 | //The second pass is a DES encryption of the first pass result |
Sergunb | 0:8918a71cdbe9 | 138 | desEncryptBlock(&context->k2, output, output); |
Sergunb | 0:8918a71cdbe9 | 139 | //The third pass is a DES decryption of the second ciphertext result |
Sergunb | 0:8918a71cdbe9 | 140 | desDecryptBlock(&context->k1, output, output); |
Sergunb | 0:8918a71cdbe9 | 141 | } |
Sergunb | 0:8918a71cdbe9 | 142 | |
Sergunb | 0:8918a71cdbe9 | 143 | #endif |
Sergunb | 0:8918a71cdbe9 | 144 |