From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Leo Newsgroups: gmane.emacs.devel Subject: Re: Adding sha256 and sha512 to C? Date: Sat, 11 Jun 2011 13:43:00 +0800 Message-ID: References: <19936.26998.559386.371174@priss.frightenedpiglet.com> <4DE072EE.4030303@cs.ucla.edu> <83mxi7b82v.fsf@gnu.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1307771015 11312 80.91.229.12 (11 Jun 2011 05:43:35 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 11 Jun 2011 05:43:35 +0000 (UTC) Cc: Eli Zaretskii , Paul Eggert , sand@blarg.net, emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Jun 11 07:43:30 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QVGym-0000fu-FW for ged-emacs-devel@m.gmane.org; Sat, 11 Jun 2011 07:43:28 +0200 Original-Received: from localhost ([::1]:54286 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QVGyl-0005tx-OY for ged-emacs-devel@m.gmane.org; Sat, 11 Jun 2011 01:43:27 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:58416) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QVGyW-0005to-Bt for emacs-devel@gnu.org; Sat, 11 Jun 2011 01:43:13 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QVGyV-0006FV-0U for emacs-devel@gnu.org; Sat, 11 Jun 2011 01:43:12 -0400 Original-Received: from mail-pz0-f41.google.com ([209.85.210.41]:35286) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QVGyU-0006FP-Mn; Sat, 11 Jun 2011 01:43:10 -0400 Original-Received: by pzk4 with SMTP id 4so1820661pzk.0 for ; Fri, 10 Jun 2011 22:43:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=le/Uj64vd0/644z3Yl1HDes8vxwbSXRbV/zyb0duqkk=; b=autxOUosAX08mLXEOBziiph+KEGSIhwrGRG/EJ5tX8xE+nWhM7w5ivHS1vaWH7650m xZtzU7EPQhIhCZ/nF2P3xjZwt0IgotOD8crlK8ABj9pyaD9AbNJ9NLpoELdXXNMOV9Re TBS65viuc7zSESg9js+jVIBaLsQIumqWxU7lw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; b=Pbd2Ywnw/j1VSoBDgy8s40nUotyrTDxWitECDg4DKY2NC+Gr+JoIX3KTWQWjtgawac pK6PkcIfvsZ+Ywy1pEgejAjE4G2Ti3f1VtqZA4tv1kM7Nmtkoufv29dRnnnm4wTM7tin JUFCsRlYOUQxrnxrfV3B1ufOw3a4Au25qXPkg= Original-Received: by 10.68.65.76 with SMTP id v12mr818720pbs.94.1307770989111; Fri, 10 Jun 2011 22:43:09 -0700 (PDT) Original-Received: from th041090.ip.tsinghua.edu.cn ([123.123.250.119]) by mx.google.com with ESMTPS id p5sm2895535pbk.36.2011.06.10.22.43.03 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 10 Jun 2011 22:43:07 -0700 (PDT) In-Reply-To: (Stefan Monnier's message of "Mon, 30 May 2011 01:06:10 -0300") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3.50 (Mac OS X 10.6.7) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.210.41 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:140364 Archived-At: --=-=-= Content-Type: text/plain Could people comment on the attached patch which implements sha-2? Thanks in advance. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=sha.diff Content-Description: sha.diff === modified file 'src/deps.mk' --- src/deps.mk 2011-05-24 08:22:58 +0000 +++ src/deps.mk 2011-06-11 05:29:51 +0000 @@ -284,8 +284,8 @@ floatfns.o: floatfns.c syssignal.h lisp.h globals.h $(config_h) fns.o: fns.c commands.h lisp.h $(config_h) frame.h buffer.h character.h \ keyboard.h keymap.h window.h $(INTERVALS_H) coding.h ../lib/md5.h \ - ../lib/sha1.h blockinput.h atimer.h systime.h xterm.h ../lib/unistd.h \ - globals.h + ../lib/sha1.h ../lib/sha256.h ../lib/sha512.h blockinput.h atimer.h \ + systime.h xterm.h ../lib/unistd.h globals.h print.o: print.c process.h frame.h window.h buffer.h keyboard.h character.h \ lisp.h globals.h $(config_h) termchar.h $(INTERVALS_H) msdos.h termhooks.h \ blockinput.h atimer.h systime.h font.h charset.h coding.h ccl.h \ === modified file 'src/fns.c' --- src/fns.c 2011-06-07 01:39:26 +0000 +++ src/fns.c 2011-06-11 05:41:20 +0000 @@ -4538,21 +4538,21 @@ /************************************************************************ - MD5 and SHA1 + MD5, SHA-1, and SHA-2 ************************************************************************/ #include "md5.h" #include "sha1.h" +#include "sha256.h" +#include "sha512.h" /* Convert a possibly-signed character to an unsigned character. This is a bit safer than casting to unsigned char, since it catches some type errors that the cast doesn't. */ static inline unsigned char to_uchar (char ch) { return ch; } -/* TYPE: 0 for md5, 1 for sha1. */ - static Lisp_Object -crypto_hash_function (int type, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary) +crypto_hash_function (void (*hash_func) (const char *buffer, size_t len, void *resblock), EMACS_UINT digest_size, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary) { int i; EMACS_INT size; @@ -4562,6 +4562,7 @@ register EMACS_INT b, e; register struct buffer *bp; EMACS_INT temp; + char *digest; Lisp_Object res=Qnil; if (STRINGP (object)) @@ -4733,46 +4734,24 @@ object = code_convert_string (object, coding_system, Qnil, 1, 0, 0); } - switch (type) + digest = xmalloc(digest_size * sizeof(char)); + + hash_func(SSDATA (object) + start_byte, + SBYTES (object) - (size_byte - end_byte), + digest); + + if (NILP(binary)) { - case 0: /* MD5 */ - { - char digest[16]; - md5_buffer (SSDATA (object) + start_byte, - SBYTES (object) - (size_byte - end_byte), - digest); - - if (NILP (binary)) - { - char value[33]; - for (i = 0; i < 16; i++) - sprintf (&value[2 * i], "%02x", to_uchar (digest[i])); - res = make_string (value, 32); - } - else - res = make_string (digest, 16); - break; - } - - case 1: /* SHA1 */ - { - char digest[20]; - sha1_buffer (SSDATA (object) + start_byte, - SBYTES (object) - (size_byte - end_byte), - digest); - if (NILP (binary)) - { - char value[41]; - for (i = 0; i < 20; i++) - sprintf (&value[2 * i], "%02x", to_uchar (digest[i])); - res = make_string (value, 40); - } - else - res = make_string (digest, 20); - break; - } + char *value = xmalloc((2 * digest_size + 1) * sizeof(char)); + for (i = 0; i < digest_size; i++) + sprintf (&value[2 * i], "%02x", to_uchar (digest[i])); + res = make_string(value, digest_size * 2); + xfree(value); } + else + res = make_string(digest, digest_size); + xfree(digest); return res; } @@ -4805,7 +4784,7 @@ guesswork fails. Normally, an error is signaled in such case. */) (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror) { - return crypto_hash_function (0, object, start, end, coding_system, noerror, Qnil); + return crypto_hash_function ((void *) &md5_buffer, MD5_DIGEST_SIZE, object, start, end, coding_system, noerror, Qnil); } DEFUN ("sha1", Fsha1, Ssha1, 1, 4, 0, @@ -4817,7 +4796,39 @@ form. */) (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary) { - return crypto_hash_function (1, object, start, end, Qnil, Qnil, binary); + return crypto_hash_function ((void *) &sha1_buffer, SHA1_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary); +} + +DEFUN ("sha224", Fsha224, Ssha224, 1, 4, 0, + doc: /* Return the SHA-224 (Secure Hash Algorithm) of an OBJECT. +See `sha1' for explanation of the arguments. */) + (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary) +{ + return crypto_hash_function ((void *) &sha224_buffer, SHA224_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary); +} + +DEFUN ("sha256", Fsha256, Ssha256, 1, 4, 0, + doc: /* Return the SHA-256 (Secure Hash Algorithm) of an OBJECT. +See `sha1' for explanation of the arguments. */) + (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary) +{ + return crypto_hash_function ((void *) &sha256_buffer, SHA256_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary); +} + +DEFUN ("sha384", Fsha384, Ssha384, 1, 4, 0, + doc: /* Return the SHA-384 (Secure Hash Algorithm) of an OBJECT. +See `sha1' for explanation of the arguments. */) + (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary) +{ + return crypto_hash_function ((void *) &sha384_buffer, SHA384_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary); +} + +DEFUN ("sha512", Fsha512, Ssha512, 1, 4, 0, + doc: /* Return the SHA-512 (Secure Hash Algorithm) of an OBJECT. +See `sha1' for explanation of the arguments. */) + (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary) +{ + return crypto_hash_function ((void *) &sha512_buffer, SHA512_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary); } @@ -4993,6 +5004,10 @@ defsubr (&Sbase64_decode_string); defsubr (&Smd5); defsubr (&Ssha1); + defsubr (&Ssha224); + defsubr (&Ssha256); + defsubr (&Ssha384); + defsubr (&Ssha512); defsubr (&Slocale_info); } === modified file 'src/makefile.w32-in' --- src/makefile.w32-in 2011-05-31 17:03:24 +0000 +++ src/makefile.w32-in 2011-06-11 05:30:23 +0000 @@ -869,6 +869,8 @@ $(EMACS_ROOT)/nt/inc/sys/time.h \ $(EMACS_ROOT)/lib/md5.h \ $(EMACS_ROOT)/lib/sha1.h \ + $(EMACS_ROOT)/lib/sha256.h \ + $(EMACS_ROOT)/lib/sha512.h \ $(LISP_H) \ $(SRC)/atimer.h \ $(SRC)/blockinput.h \ --=-=-=--