cyassl re-port with cellular comms, PSK test
Dependencies: VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src
cyassllib/ctaocrypt/src/md2.c@0:e979170e02e7, 2013-04-26 (annotated)
- Committer:
- ashleymills
- Date:
- Fri Apr 26 16:54:58 2013 +0000
- Revision:
- 0:e979170e02e7
Basic operation of SSL with PSK working for cellular.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:e979170e02e7 | 1 | /* md2.c |
ashleymills | 0:e979170e02e7 | 2 | * |
ashleymills | 0:e979170e02e7 | 3 | * Copyright (C) 2006-2012 Sawtooth Consulting Ltd. |
ashleymills | 0:e979170e02e7 | 4 | * |
ashleymills | 0:e979170e02e7 | 5 | * This file is part of CyaSSL. |
ashleymills | 0:e979170e02e7 | 6 | * |
ashleymills | 0:e979170e02e7 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
ashleymills | 0:e979170e02e7 | 8 | * it under the terms of the GNU General Public License as published by |
ashleymills | 0:e979170e02e7 | 9 | * the Free Software Foundation; either version 2 of the License, or |
ashleymills | 0:e979170e02e7 | 10 | * (at your option) any later version. |
ashleymills | 0:e979170e02e7 | 11 | * |
ashleymills | 0:e979170e02e7 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
ashleymills | 0:e979170e02e7 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ashleymills | 0:e979170e02e7 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ashleymills | 0:e979170e02e7 | 15 | * GNU General Public License for more details. |
ashleymills | 0:e979170e02e7 | 16 | * |
ashleymills | 0:e979170e02e7 | 17 | * You should have received a copy of the GNU General Public License |
ashleymills | 0:e979170e02e7 | 18 | * along with this program; if not, write to the Free Software |
ashleymills | 0:e979170e02e7 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
ashleymills | 0:e979170e02e7 | 20 | */ |
ashleymills | 0:e979170e02e7 | 21 | |
ashleymills | 0:e979170e02e7 | 22 | |
ashleymills | 0:e979170e02e7 | 23 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:e979170e02e7 | 24 | #include <config.h> |
ashleymills | 0:e979170e02e7 | 25 | #endif |
ashleymills | 0:e979170e02e7 | 26 | |
ashleymills | 0:e979170e02e7 | 27 | #ifdef CYASSL_MD2 |
ashleymills | 0:e979170e02e7 | 28 | |
ashleymills | 0:e979170e02e7 | 29 | #include <cyassl/ctaocrypt/md2.h> |
ashleymills | 0:e979170e02e7 | 30 | #ifdef NO_INLINE |
ashleymills | 0:e979170e02e7 | 31 | #include <cyassl/ctaocrypt/misc.h> |
ashleymills | 0:e979170e02e7 | 32 | #else |
ashleymills | 0:e979170e02e7 | 33 | #include <ctaocrypt/src/misc.c> |
ashleymills | 0:e979170e02e7 | 34 | #endif |
ashleymills | 0:e979170e02e7 | 35 | |
ashleymills | 0:e979170e02e7 | 36 | |
ashleymills | 0:e979170e02e7 | 37 | void InitMd2(Md2* md2) |
ashleymills | 0:e979170e02e7 | 38 | { |
ashleymills | 0:e979170e02e7 | 39 | XMEMSET(md2->X, 0, MD2_X_SIZE); |
ashleymills | 0:e979170e02e7 | 40 | XMEMSET(md2->C, 0, MD2_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 41 | XMEMSET(md2->buffer, 0, MD2_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 42 | md2->count = 0; |
ashleymills | 0:e979170e02e7 | 43 | } |
ashleymills | 0:e979170e02e7 | 44 | |
ashleymills | 0:e979170e02e7 | 45 | |
ashleymills | 0:e979170e02e7 | 46 | void Md2Update(Md2* md2, const byte* data, word32 len) |
ashleymills | 0:e979170e02e7 | 47 | { |
ashleymills | 0:e979170e02e7 | 48 | static const byte S[256] = |
ashleymills | 0:e979170e02e7 | 49 | { |
ashleymills | 0:e979170e02e7 | 50 | 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, |
ashleymills | 0:e979170e02e7 | 51 | 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, |
ashleymills | 0:e979170e02e7 | 52 | 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, |
ashleymills | 0:e979170e02e7 | 53 | 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251, |
ashleymills | 0:e979170e02e7 | 54 | 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63, |
ashleymills | 0:e979170e02e7 | 55 | 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50, |
ashleymills | 0:e979170e02e7 | 56 | 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165, |
ashleymills | 0:e979170e02e7 | 57 | 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210, |
ashleymills | 0:e979170e02e7 | 58 | 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157, |
ashleymills | 0:e979170e02e7 | 59 | 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27, |
ashleymills | 0:e979170e02e7 | 60 | 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15, |
ashleymills | 0:e979170e02e7 | 61 | 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, |
ashleymills | 0:e979170e02e7 | 62 | 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, |
ashleymills | 0:e979170e02e7 | 63 | 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, |
ashleymills | 0:e979170e02e7 | 64 | 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233, |
ashleymills | 0:e979170e02e7 | 65 | 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228, |
ashleymills | 0:e979170e02e7 | 66 | 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237, |
ashleymills | 0:e979170e02e7 | 67 | 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 |
ashleymills | 0:e979170e02e7 | 68 | }; |
ashleymills | 0:e979170e02e7 | 69 | |
ashleymills | 0:e979170e02e7 | 70 | while (len) { |
ashleymills | 0:e979170e02e7 | 71 | word32 L = (MD2_PAD_SIZE - md2->count) < len ? |
ashleymills | 0:e979170e02e7 | 72 | (MD2_PAD_SIZE - md2->count) : len; |
ashleymills | 0:e979170e02e7 | 73 | XMEMCPY(md2->buffer + md2->count, data, L); |
ashleymills | 0:e979170e02e7 | 74 | md2->count += L; |
ashleymills | 0:e979170e02e7 | 75 | data += L; |
ashleymills | 0:e979170e02e7 | 76 | len -= L; |
ashleymills | 0:e979170e02e7 | 77 | |
ashleymills | 0:e979170e02e7 | 78 | if (md2->count == MD2_PAD_SIZE) { |
ashleymills | 0:e979170e02e7 | 79 | int i; |
ashleymills | 0:e979170e02e7 | 80 | byte t; |
ashleymills | 0:e979170e02e7 | 81 | |
ashleymills | 0:e979170e02e7 | 82 | md2->count = 0; |
ashleymills | 0:e979170e02e7 | 83 | XMEMCPY(md2->X + MD2_PAD_SIZE, md2->buffer, MD2_PAD_SIZE); |
ashleymills | 0:e979170e02e7 | 84 | t = md2->C[15]; |
ashleymills | 0:e979170e02e7 | 85 | |
ashleymills | 0:e979170e02e7 | 86 | for(i = 0; i < MD2_PAD_SIZE; i++) { |
ashleymills | 0:e979170e02e7 | 87 | md2->X[32 + i] = md2->X[MD2_PAD_SIZE + i] ^ md2->X[i]; |
ashleymills | 0:e979170e02e7 | 88 | t = md2->C[i] ^= S[md2->buffer[i] ^ t]; |
ashleymills | 0:e979170e02e7 | 89 | } |
ashleymills | 0:e979170e02e7 | 90 | |
ashleymills | 0:e979170e02e7 | 91 | t=0; |
ashleymills | 0:e979170e02e7 | 92 | for(i = 0; i < 18; i++) { |
ashleymills | 0:e979170e02e7 | 93 | int j; |
ashleymills | 0:e979170e02e7 | 94 | for(j = 0; j < MD2_X_SIZE; j += 8) { |
ashleymills | 0:e979170e02e7 | 95 | t = md2->X[j+0] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 96 | t = md2->X[j+1] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 97 | t = md2->X[j+2] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 98 | t = md2->X[j+3] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 99 | t = md2->X[j+4] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 100 | t = md2->X[j+5] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 101 | t = md2->X[j+6] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 102 | t = md2->X[j+7] ^= S[t]; |
ashleymills | 0:e979170e02e7 | 103 | } |
ashleymills | 0:e979170e02e7 | 104 | t = (t + i) & 0xFF; |
ashleymills | 0:e979170e02e7 | 105 | } |
ashleymills | 0:e979170e02e7 | 106 | } |
ashleymills | 0:e979170e02e7 | 107 | } |
ashleymills | 0:e979170e02e7 | 108 | } |
ashleymills | 0:e979170e02e7 | 109 | |
ashleymills | 0:e979170e02e7 | 110 | |
ashleymills | 0:e979170e02e7 | 111 | void Md2Final(Md2* md2, byte* hash) |
ashleymills | 0:e979170e02e7 | 112 | { |
ashleymills | 0:e979170e02e7 | 113 | byte padding[MD2_BLOCK_SIZE]; |
ashleymills | 0:e979170e02e7 | 114 | word32 padLen = MD2_PAD_SIZE - md2->count; |
ashleymills | 0:e979170e02e7 | 115 | word32 i; |
ashleymills | 0:e979170e02e7 | 116 | |
ashleymills | 0:e979170e02e7 | 117 | for (i = 0; i < padLen; i++) |
ashleymills | 0:e979170e02e7 | 118 | padding[i] = (byte)padLen; |
ashleymills | 0:e979170e02e7 | 119 | |
ashleymills | 0:e979170e02e7 | 120 | Md2Update(md2, padding, padLen); |
ashleymills | 0:e979170e02e7 | 121 | Md2Update(md2, md2->C, MD2_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 122 | |
ashleymills | 0:e979170e02e7 | 123 | XMEMCPY(hash, md2->X, MD2_DIGEST_SIZE); |
ashleymills | 0:e979170e02e7 | 124 | |
ashleymills | 0:e979170e02e7 | 125 | InitMd2(md2); |
ashleymills | 0:e979170e02e7 | 126 | } |
ashleymills | 0:e979170e02e7 | 127 | |
ashleymills | 0:e979170e02e7 | 128 | |
ashleymills | 0:e979170e02e7 | 129 | #endif /* CYASSL_MD2 */ |