cya_u
Fork of CyaSSL-forEncrypt by
pwdbased.c@0:5045d2638c29, 2011-02-05 (annotated)
- Committer:
- toddouska
- Date:
- Sat Feb 05 01:09:17 2011 +0000
- Revision:
- 0:5045d2638c29
Beta Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
toddouska | 0:5045d2638c29 | 1 | /* pwdbased.c |
toddouska | 0:5045d2638c29 | 2 | * |
toddouska | 0:5045d2638c29 | 3 | * Copyright (C) 2006-2010 Sawtooth Consulting Ltd. |
toddouska | 0:5045d2638c29 | 4 | * |
toddouska | 0:5045d2638c29 | 5 | * This file is part of CyaSSL. |
toddouska | 0:5045d2638c29 | 6 | * |
toddouska | 0:5045d2638c29 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
toddouska | 0:5045d2638c29 | 8 | * it under the terms of the GNU General Public License as published by |
toddouska | 0:5045d2638c29 | 9 | * the Free Software Foundation; either version 2 of the License, or |
toddouska | 0:5045d2638c29 | 10 | * (at your option) any later version. |
toddouska | 0:5045d2638c29 | 11 | * |
toddouska | 0:5045d2638c29 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
toddouska | 0:5045d2638c29 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
toddouska | 0:5045d2638c29 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
toddouska | 0:5045d2638c29 | 15 | * GNU General Public License for more details. |
toddouska | 0:5045d2638c29 | 16 | * |
toddouska | 0:5045d2638c29 | 17 | * You should have received a copy of the GNU General Public License |
toddouska | 0:5045d2638c29 | 18 | * along with this program; if not, write to the Free Software |
toddouska | 0:5045d2638c29 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
toddouska | 0:5045d2638c29 | 20 | */ |
toddouska | 0:5045d2638c29 | 21 | |
toddouska | 0:5045d2638c29 | 22 | |
toddouska | 0:5045d2638c29 | 23 | #ifndef NO_PWDBASED |
toddouska | 0:5045d2638c29 | 24 | |
toddouska | 0:5045d2638c29 | 25 | #include "pwdbased.h" |
toddouska | 0:5045d2638c29 | 26 | |
toddouska | 0:5045d2638c29 | 27 | |
toddouska | 0:5045d2638c29 | 28 | int PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt, |
toddouska | 0:5045d2638c29 | 29 | int sLen, int iterations, int kLen, int hashType) |
toddouska | 0:5045d2638c29 | 30 | { |
toddouska | 0:5045d2638c29 | 31 | Md5 md5; |
toddouska | 0:5045d2638c29 | 32 | Sha sha; |
toddouska | 0:5045d2638c29 | 33 | int hLen = (hashType == MD5) ? MD5_DIGEST_SIZE : SHA_DIGEST_SIZE; |
toddouska | 0:5045d2638c29 | 34 | int i; |
toddouska | 0:5045d2638c29 | 35 | byte buffer[SHA_DIGEST_SIZE]; /* max size */ |
toddouska | 0:5045d2638c29 | 36 | |
toddouska | 0:5045d2638c29 | 37 | if (hashType != MD5 && hashType != SHA) |
toddouska | 0:5045d2638c29 | 38 | return -1; |
toddouska | 0:5045d2638c29 | 39 | |
toddouska | 0:5045d2638c29 | 40 | if (kLen > hLen) |
toddouska | 0:5045d2638c29 | 41 | return -1; |
toddouska | 0:5045d2638c29 | 42 | |
toddouska | 0:5045d2638c29 | 43 | if (iterations < 1) |
toddouska | 0:5045d2638c29 | 44 | return -1; |
toddouska | 0:5045d2638c29 | 45 | |
toddouska | 0:5045d2638c29 | 46 | if (hashType == MD5) { |
toddouska | 0:5045d2638c29 | 47 | InitMd5(&md5); |
toddouska | 0:5045d2638c29 | 48 | Md5Update(&md5, passwd, pLen); |
toddouska | 0:5045d2638c29 | 49 | Md5Update(&md5, salt, sLen); |
toddouska | 0:5045d2638c29 | 50 | Md5Final(&md5, buffer); |
toddouska | 0:5045d2638c29 | 51 | } |
toddouska | 0:5045d2638c29 | 52 | else { |
toddouska | 0:5045d2638c29 | 53 | InitSha(&sha); |
toddouska | 0:5045d2638c29 | 54 | ShaUpdate(&sha, passwd, pLen); |
toddouska | 0:5045d2638c29 | 55 | ShaUpdate(&sha, salt, sLen); |
toddouska | 0:5045d2638c29 | 56 | ShaFinal(&sha, buffer); |
toddouska | 0:5045d2638c29 | 57 | } |
toddouska | 0:5045d2638c29 | 58 | |
toddouska | 0:5045d2638c29 | 59 | for (i = 1; i < iterations; i++) { |
toddouska | 0:5045d2638c29 | 60 | if (hashType == MD5) { |
toddouska | 0:5045d2638c29 | 61 | Md5Update(&md5, buffer, hLen); |
toddouska | 0:5045d2638c29 | 62 | Md5Final(&md5, buffer); |
toddouska | 0:5045d2638c29 | 63 | } |
toddouska | 0:5045d2638c29 | 64 | else { |
toddouska | 0:5045d2638c29 | 65 | ShaUpdate(&sha, buffer, hLen); |
toddouska | 0:5045d2638c29 | 66 | ShaFinal(&sha, buffer); |
toddouska | 0:5045d2638c29 | 67 | } |
toddouska | 0:5045d2638c29 | 68 | } |
toddouska | 0:5045d2638c29 | 69 | XMEMCPY(output, buffer, kLen); |
toddouska | 0:5045d2638c29 | 70 | |
toddouska | 0:5045d2638c29 | 71 | return 0; |
toddouska | 0:5045d2638c29 | 72 | } |
toddouska | 0:5045d2638c29 | 73 | |
toddouska | 0:5045d2638c29 | 74 | |
toddouska | 0:5045d2638c29 | 75 | #endif /* NO_PWDBASED */ |
toddouska | 0:5045d2638c29 | 76 |