CyaSSL is an SSL library for devices like mbed.

Dependents:   cyassl-client Sync

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pwdbased.c Source File

pwdbased.c

00001 /* pwdbased.c
00002  *
00003  * Copyright (C) 2006-2010 Sawtooth Consulting Ltd.
00004  *
00005  * This file is part of CyaSSL.
00006  *
00007  * CyaSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * CyaSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00020  */
00021 
00022 
00023 #ifndef NO_PWDBASED
00024 
00025 #include "pwdbased.h"
00026 
00027 
00028 int PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt,
00029            int sLen, int iterations, int kLen, int hashType)
00030 {
00031     Md5  md5;
00032     Sha  sha;
00033     int  hLen = (hashType == MD5) ? MD5_DIGEST_SIZE : SHA_DIGEST_SIZE;
00034     int  i;
00035     byte buffer[SHA_DIGEST_SIZE];  /* max size */
00036 
00037     if (hashType != MD5 && hashType != SHA)
00038         return -1;
00039 
00040     if (kLen > hLen)
00041         return -1;
00042 
00043     if (iterations < 1)
00044         return -1;
00045 
00046     if (hashType == MD5) {
00047         InitMd5(&md5);
00048         Md5Update(&md5, passwd, pLen);
00049         Md5Update(&md5, salt,   sLen);
00050         Md5Final(&md5,  buffer);
00051     }
00052     else {
00053         InitSha(&sha);
00054         ShaUpdate(&sha, passwd, pLen);
00055         ShaUpdate(&sha, salt,   sLen);
00056         ShaFinal(&sha,  buffer);
00057     }
00058 
00059     for (i = 1; i < iterations; i++) {
00060         if (hashType == MD5) {
00061             Md5Update(&md5, buffer, hLen);
00062             Md5Final(&md5,  buffer);
00063         }
00064         else {
00065             ShaUpdate(&sha, buffer, hLen);
00066             ShaFinal(&sha,  buffer);
00067         }
00068     }
00069     XMEMCPY(output, buffer, kLen);
00070 
00071     return 0;
00072 }
00073 
00074 
00075 #endif /* NO_PWDBASED */
00076