Browse Source

Add Pad and FileChunk functions for each family of hash functions.

The Pad function does padding like in Final but does not finish processing.
The FileChunk function creates a digest from a portion of a file.
Also made the length parameters consistent (and size_t).
OPENBSD_3_6
millert 20 years ago
parent
commit
e2e5c3d50c
16 changed files with 343 additions and 148 deletions
  1. +4
    -1
      src/include/md4.h
  2. +4
    -1
      src/include/md5.h
  3. +5
    -2
      src/include/rmd160.h
  4. +5
    -2
      src/include/sha1.h
  5. +10
    -1
      src/include/sha2.h
  6. +17
    -13
      src/lib/libc/hash/Makefile.inc
  7. +20
    -8
      src/lib/libc/hash/helper.c
  8. +17
    -8
      src/lib/libc/hash/md4.c
  9. +17
    -8
      src/lib/libc/hash/md5.c
  10. +34
    -7
      src/lib/libc/hash/mdX.3
  11. +31
    -5
      src/lib/libc/hash/rmd160.3
  12. +13
    -7
      src/lib/libc/hash/rmd160.c
  13. +31
    -5
      src/lib/libc/hash/sha1.3
  14. +15
    -7
      src/lib/libc/hash/sha1.c
  15. +40
    -5
      src/lib/libc/hash/sha2.3
  16. +80
    -68
      src/lib/libc/hash/sha2.c

+ 4
- 1
src/include/md4.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: md4.h,v 1.13 2004/04/29 15:51:16 millert Exp $ */
/* $OpenBSD: md4.h,v 1.14 2004/05/03 17:30:14 millert Exp $ */
/* /*
* This code implements the MD4 message-digest algorithm. * This code implements the MD4 message-digest algorithm.
@ -32,6 +32,7 @@ __BEGIN_DECLS
void MD4Init(MD4_CTX *); void MD4Init(MD4_CTX *);
void MD4Update(MD4_CTX *, const u_int8_t *, size_t) void MD4Update(MD4_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void MD4Pad(MD4_CTX *);
void MD4Final(u_int8_t [MD4_DIGEST_LENGTH], MD4_CTX *) void MD4Final(u_int8_t [MD4_DIGEST_LENGTH], MD4_CTX *)
__attribute__((__bounded__(__minbytes__,1,MD4_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,MD4_DIGEST_LENGTH)));
void MD4Transform(u_int32_t [4], const u_int8_t [MD4_BLOCK_LENGTH]) void MD4Transform(u_int32_t [4], const u_int8_t [MD4_BLOCK_LENGTH])
@ -41,6 +42,8 @@ char *MD4End(MD4_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,MD4_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,MD4_DIGEST_STRING_LENGTH)));
char *MD4File(char *, char *) char *MD4File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,MD4_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,MD4_DIGEST_STRING_LENGTH)));
char *MD4FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,MD4_DIGEST_STRING_LENGTH)));
char *MD4Data(const u_int8_t *, size_t, char *) char *MD4Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,MD4_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,MD4_DIGEST_STRING_LENGTH)));


+ 4
- 1
src/include/md5.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: md5.h,v 1.14 2004/04/29 15:51:16 millert Exp $ */
/* $OpenBSD: md5.h,v 1.15 2004/05/03 17:30:14 millert Exp $ */
/* /*
* This code implements the MD5 message-digest algorithm. * This code implements the MD5 message-digest algorithm.
@ -31,6 +31,7 @@ __BEGIN_DECLS
void MD5Init(MD5_CTX *); void MD5Init(MD5_CTX *);
void MD5Update(MD5_CTX *, const u_int8_t *, size_t) void MD5Update(MD5_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void MD5Pad(MD5_CTX *);
void MD5Final(u_int8_t [MD5_DIGEST_LENGTH], MD5_CTX *) void MD5Final(u_int8_t [MD5_DIGEST_LENGTH], MD5_CTX *)
__attribute__((__bounded__(__minbytes__,1,MD5_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,MD5_DIGEST_LENGTH)));
void MD5Transform(u_int32_t [4], const u_int8_t [MD5_BLOCK_LENGTH]) void MD5Transform(u_int32_t [4], const u_int8_t [MD5_BLOCK_LENGTH])
@ -40,6 +41,8 @@ char *MD5End(MD5_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
char *MD5File(char *, char *) char *MD5File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
char *MD5FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
char *MD5Data(const u_int8_t *, size_t, char *) char *MD5Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,MD5_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,MD5_DIGEST_STRING_LENGTH)));


+ 5
- 2
src/include/rmd160.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: rmd160.h,v 1.14 2004/04/29 15:51:16 millert Exp $ */
/* $OpenBSD: rmd160.h,v 1.15 2004/05/03 17:30:14 millert Exp $ */
/* /*
* Copyright (c) 2001 Markus Friedl. All rights reserved. * Copyright (c) 2001 Markus Friedl. All rights reserved.
* *
@ -43,14 +43,17 @@ void RMD160Init(RMD160_CTX *);
void RMD160Transform(u_int32_t [5], const u_int8_t [RMD160_BLOCK_LENGTH]) void RMD160Transform(u_int32_t [5], const u_int8_t [RMD160_BLOCK_LENGTH])
__attribute__((__bounded__(__minbytes__,1,5))) __attribute__((__bounded__(__minbytes__,1,5)))
__attribute__((__bounded__(__minbytes__,2,RMD160_BLOCK_LENGTH))); __attribute__((__bounded__(__minbytes__,2,RMD160_BLOCK_LENGTH)));
void RMD160Update(RMD160_CTX *, const u_int8_t *, u_int32_t)
void RMD160Update(RMD160_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void RMD160Pad(RMD160_CTX *);
void RMD160Final(u_int8_t [RMD160_DIGEST_LENGTH], RMD160_CTX *) void RMD160Final(u_int8_t [RMD160_DIGEST_LENGTH], RMD160_CTX *)
__attribute__((__bounded__(__minbytes__,1,RMD160_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,RMD160_DIGEST_LENGTH)));
char *RMD160End(RMD160_CTX *, char *) char *RMD160End(RMD160_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
char *RMD160File(char *, char *) char *RMD160File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
char *RMD160FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,RMD160_DIGEST_STRING_LENGTH)));
char *RMD160Data(const u_int8_t *, size_t, char *) char *RMD160Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,RMD160_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,RMD160_DIGEST_STRING_LENGTH)));


+ 5
- 2
src/include/sha1.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: sha1.h,v 1.19 2004/04/29 15:51:16 millert Exp $ */
/* $OpenBSD: sha1.h,v 1.20 2004/05/03 17:30:14 millert Exp $ */
/* /*
* SHA-1 in C * SHA-1 in C
@ -23,10 +23,11 @@ typedef struct {
__BEGIN_DECLS __BEGIN_DECLS
void SHA1Init(SHA1_CTX *); void SHA1Init(SHA1_CTX *);
void SHA1Pad(SHA1_CTX *);
void SHA1Transform(u_int32_t [5], const u_int8_t [SHA1_BLOCK_LENGTH]) void SHA1Transform(u_int32_t [5], const u_int8_t [SHA1_BLOCK_LENGTH])
__attribute__((__bounded__(__minbytes__,1,5))) __attribute__((__bounded__(__minbytes__,1,5)))
__attribute__((__bounded__(__minbytes__,2,SHA1_BLOCK_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA1_BLOCK_LENGTH)));
void SHA1Update(SHA1_CTX *, const u_int8_t *, unsigned int)
void SHA1Update(SHA1_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void SHA1Final(u_int8_t [SHA1_DIGEST_LENGTH], SHA1_CTX *) void SHA1Final(u_int8_t [SHA1_DIGEST_LENGTH], SHA1_CTX *)
__attribute__((__bounded__(__minbytes__,1,SHA1_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,SHA1_DIGEST_LENGTH)));
@ -34,6 +35,8 @@ char *SHA1End(SHA1_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH)));
char *SHA1File(char *, char *) char *SHA1File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH)));
char *SHA1FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,SHA1_DIGEST_STRING_LENGTH)));
char *SHA1Data(const u_int8_t *, size_t, char *) char *SHA1Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,SHA1_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,SHA1_DIGEST_STRING_LENGTH)));


+ 10
- 1
src/include/sha2.h View File

@ -1,4 +1,4 @@
/* $OpenBSD: sha2.h,v 1.3 2004/04/29 15:51:16 millert Exp $ */
/* $OpenBSD: sha2.h,v 1.4 2004/05/03 17:30:14 millert Exp $ */
/* /*
* FILE: sha2.h * FILE: sha2.h
@ -70,12 +70,15 @@ __BEGIN_DECLS
void SHA256_Init(SHA256_CTX *); void SHA256_Init(SHA256_CTX *);
void SHA256_Update(SHA256_CTX *, const u_int8_t *, size_t) void SHA256_Update(SHA256_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void SHA256_Pad(SHA256_CTX *);
void SHA256_Final(u_int8_t [SHA256_DIGEST_LENGTH], SHA256_CTX *) void SHA256_Final(u_int8_t [SHA256_DIGEST_LENGTH], SHA256_CTX *)
__attribute__((__bounded__(__minbytes__,1,SHA256_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,SHA256_DIGEST_LENGTH)));
char *SHA256_End(SHA256_CTX *, char *) char *SHA256_End(SHA256_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
char *SHA256_File(char *, char *) char *SHA256_File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
char *SHA256_FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,SHA256_DIGEST_STRING_LENGTH)));
char *SHA256_Data(const u_int8_t *, size_t, char *) char *SHA256_Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,SHA256_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,SHA256_DIGEST_STRING_LENGTH)));
@ -83,12 +86,15 @@ char *SHA256_Data(const u_int8_t *, size_t, char *)
void SHA384_Init(SHA384_CTX *); void SHA384_Init(SHA384_CTX *);
void SHA384_Update(SHA384_CTX *, const u_int8_t *, size_t) void SHA384_Update(SHA384_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void SHA384_Pad(SHA384_CTX *);
void SHA384_Final(u_int8_t [SHA384_DIGEST_LENGTH], SHA384_CTX *) void SHA384_Final(u_int8_t [SHA384_DIGEST_LENGTH], SHA384_CTX *)
__attribute__((__bounded__(__minbytes__,1,SHA384_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,SHA384_DIGEST_LENGTH)));
char *SHA384_End(SHA384_CTX *, char *) char *SHA384_End(SHA384_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
char *SHA384_File(char *, char *) char *SHA384_File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
char *SHA384_FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,SHA384_DIGEST_STRING_LENGTH)));
char *SHA384_Data(const u_int8_t *, size_t, char *) char *SHA384_Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,SHA384_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,SHA384_DIGEST_STRING_LENGTH)));
@ -96,12 +102,15 @@ char *SHA384_Data(const u_int8_t *, size_t, char *)
void SHA512_Init(SHA512_CTX *); void SHA512_Init(SHA512_CTX *);
void SHA512_Update(SHA512_CTX *, const u_int8_t *, size_t) void SHA512_Update(SHA512_CTX *, const u_int8_t *, size_t)
__attribute__((__bounded__(__string__,2,3))); __attribute__((__bounded__(__string__,2,3)));
void SHA512_Pad(SHA512_CTX *);
void SHA512_Final(u_int8_t [SHA512_DIGEST_LENGTH], SHA512_CTX *) void SHA512_Final(u_int8_t [SHA512_DIGEST_LENGTH], SHA512_CTX *)
__attribute__((__bounded__(__minbytes__,1,SHA512_DIGEST_LENGTH))); __attribute__((__bounded__(__minbytes__,1,SHA512_DIGEST_LENGTH)));
char *SHA512_End(SHA512_CTX *, char *) char *SHA512_End(SHA512_CTX *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
char *SHA512_File(char *, char *) char *SHA512_File(char *, char *)
__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
char *SHA512_FileChunk(char *, char *, off_t, off_t)
__attribute__((__bounded__(__minbytes__,2,SHA512_DIGEST_STRING_LENGTH)));
char *SHA512_Data(const u_int8_t *, size_t, char *) char *SHA512_Data(const u_int8_t *, size_t, char *)
__attribute__((__bounded__(__string__,1,2))) __attribute__((__bounded__(__string__,1,2)))
__attribute__((__bounded__(__minbytes__,3,SHA512_DIGEST_STRING_LENGTH))); __attribute__((__bounded__(__minbytes__,3,SHA512_DIGEST_STRING_LENGTH)));


+ 17
- 13
src/lib/libc/hash/Makefile.inc View File

@ -1,4 +1,4 @@
# $OpenBSD: Makefile.inc,v 1.16 2004/04/28 16:56:49 millert Exp $
# $OpenBSD: Makefile.inc,v 1.17 2004/05/03 17:30:14 millert Exp $
# hash functions # hash functions
.PATH: ${LIBCSRCDIR}/hash .PATH: ${LIBCSRCDIR}/hash
@ -6,24 +6,28 @@
HELPER= md4hl.c md5hl.c rmd160hl.c sha1hl.c sha256hl.c sha384hl.c sha512hl.c HELPER= md4hl.c md5hl.c rmd160hl.c sha1hl.c sha256hl.c sha384hl.c sha512hl.c
SRCS+= md4.c md5.c rmd160.c sha1.c sha2.c ${HELPER} SRCS+= md4.c md5.c rmd160.c sha1.c sha2.c ${HELPER}
MAN+= md4.3 md5.3 rmd160.3 sha1.3 sha2.3 MAN+= md4.3 md5.3 rmd160.3 sha1.3 sha2.3
MLINKS+=md4.3 MD4Init.3 md4.3 MD4Update.3 md4.3 MD4Final.3
MLINKS+=md4.3 MD4End.3 md4.3 MD4File.3 md4.3 MD4Data.3
MLINKS+=md4.3 MD4Transform.3
MLINKS+=md5.3 MD5Init.3 md5.3 MD5Update.3 md5.3 MD5Final.3
MLINKS+=md5.3 MD5End.3 md5.3 MD5File.3 md5.3 MD5Data.3
MLINKS+=md5.3 MD5Transform.3
MLINKS+=rmd160.3 RMD160Init.3 rmd160.3 RMD160Update.3 rmd160.3 RMD160Final.3
MLINKS+=rmd160.3 RMD160End.3 rmd160.3 RMD160File.3 rmd160.3 RMD160Data.3
MLINKS+=rmd160.3 RMD160Transform.3
MLINKS+=sha1.3 SHA1Init.3 sha1.3 SHA1Update.3 sha1.3 SHA1Final.3
MLINKS+=sha1.3 SHA1End.3 sha1.3 SHA1File.3 sha1.3 SHA1Data.3
MLINKS+=sha1.3 SHA1Transform.3
MLINKS+=md4.3 MD4Transform.3 md4.3 MD4Init.3 md4.3 MD4Final.3
MLINKS+=md4.3 MD4Update.3 md4.3 MD4End.3 md4.3 MD4File.3
MLINKS+=md4.3 MD4FileChunk.3 md4.3 MD4Pad.3 md4.3 MD4Data.3
MLINKS+=md5.3 MD5Transform.3 md5.3 MD5Init.3 md5.3 MD5Final.3
MLINKS+=md5.3 MD5Update.3 md5.3 MD5End.3 md5.3 MD5File.3
MLINKS+=md5.3 MD5FileChunk.3 md5.3 MD5Pad.3 md5.3 MD5Data.3
MLINKS+=rmd160.3 RMD160Transform.3 rmd160.3 RMD160Init.3 rmd160.3 RMD160Final.3
MLINKS+=rmd160.3 RMD160Update.3 rmd160.3 RMD160End.3 rmd160.3 RMD160File.3
MLINKS+=rmd160.3 RMD160FileChunk.3 rmd160.3 RMD160Pad.3 rmd160.3 RMD160Data.3
MLINKS+=sha1.3 SHA1Transform.3 sha1.3 SHA1Init.3 sha1.3 SHA1Final.3
MLINKS+=sha1.3 SHA1Update.3 sha1.3 SHA1End.3 sha1.3 SHA1File.3
MLINKS+=sha1.3 SHA1FileChunk.3 sha1.3 SHA1Pad.3 sha1.3 SHA1Data.3
MLINKS+=sha2.3 SHA256_Init.3 sha2.3 SHA256_Update.3 sha2.3 SHA256_Final.3 MLINKS+=sha2.3 SHA256_Init.3 sha2.3 SHA256_Update.3 sha2.3 SHA256_Final.3
MLINKS+=sha2.3 SHA256_End.3 sha2.3 SHA256_File.3 sha2.3 SHA256_Data.3 MLINKS+=sha2.3 SHA256_End.3 sha2.3 SHA256_File.3 sha2.3 SHA256_Data.3
MLINKS+=sha2.3 SHA256_Pad.3 sha2.3 SHA256_FileChunk.3
MLINKS+=sha2.3 SHA384_Init.3 sha2.3 SHA384_Update.3 sha2.3 SHA384_Final.3 MLINKS+=sha2.3 SHA384_Init.3 sha2.3 SHA384_Update.3 sha2.3 SHA384_Final.3
MLINKS+=sha2.3 SHA384_End.3 sha2.3 SHA384_File.3 sha2.3 SHA384_Data.3 MLINKS+=sha2.3 SHA384_End.3 sha2.3 SHA384_File.3 sha2.3 SHA384_Data.3
MLINKS+=sha2.3 SHA384_Pad.3 sha2.3 SHA384_FileChunk.3
MLINKS+=sha2.3 SHA512_Init.3 sha2.3 SHA512_Update.3 sha2.3 SHA512_Final.3 MLINKS+=sha2.3 SHA512_Init.3 sha2.3 SHA512_Update.3 sha2.3 SHA512_Final.3
MLINKS+=sha2.3 SHA512_End.3 sha2.3 SHA512_File.3 sha2.3 SHA512_Data.3 MLINKS+=sha2.3 SHA512_End.3 sha2.3 SHA512_File.3 sha2.3 SHA512_Data.3
MLINKS+=sha2.3 SHA512_Pad.3 sha2.3 SHA512_FileChunk.3
CLEANFILES+= ${HELPER} md[45].3 CLEANFILES+= ${HELPER} md[45].3
.ifndef NOMAN .ifndef NOMAN


+ 20
- 8
src/lib/libc/hash/helper.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: helper.c,v 1.4 2004/04/29 15:51:16 millert Exp $ */
/* $OpenBSD: helper.c,v 1.5 2004/05/03 17:30:14 millert Exp $ */
/* /*
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
@ -10,10 +10,10 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$OpenBSD: helper.c,v 1.4 2004/04/29 15:51:16 millert Exp $";
static const char rcsid[] = "$OpenBSD: helper.c,v 1.5 2004/05/03 17:30:14 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -46,24 +46,36 @@ HASHEnd(HASH_CTX *ctx, char *buf)
} }
char * char *
HASHFile(char *filename, char *buf)
HASHFileChunk(char *filename, char *buf, off_t off, off_t len)
{ {
u_char buffer[BUFSIZ]; u_char buffer[BUFSIZ];
HASH_CTX ctx; HASH_CTX ctx;
int fd, num, save_errno;
int fd, save_errno;
ssize_t nr;
HASHInit(&ctx); HASHInit(&ctx);
if ((fd = open(filename, O_RDONLY)) < 0) if ((fd = open(filename, O_RDONLY)) < 0)
return (NULL); return (NULL);
if (off > 0 && lseek(fd, off, SEEK_SET) < 0)
return (NULL);
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
HASHUpdate(&ctx, buffer, num);
while ((nr = read(fd, buffer, MIN(sizeof(buffer), len))) > 0) {
HASHUpdate(&ctx, buffer, (size_t)nr);
if (len > 0 && (len -= nr) == 0)
break;
}
save_errno = errno; save_errno = errno;
close(fd); close(fd);
errno = save_errno; errno = save_errno;
return (num < 0 ? NULL : HASHEnd(&ctx, buf));
return (nr < 0 ? NULL : HASHEnd(&ctx, buf));
}
char *
HASHFile(char *filename, char *buf)
{
return (HASHFileChunk(filename, buf, (off_t)0, (off_t)0));
} }
char * char *


+ 17
- 8
src/lib/libc/hash/md4.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: md4.c,v 1.3 2004/04/29 18:45:39 millert Exp $ */
/* $OpenBSD: md4.c,v 1.4 2004/05/03 17:30:14 millert Exp $ */
/* /*
* This code implements the MD4 message-digest algorithm. * This code implements the MD4 message-digest algorithm.
@ -19,7 +19,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$OpenBSD: md4.c,v 1.3 2004/04/29 18:45:39 millert Exp $";
static const char rcsid[] = "$OpenBSD: md4.c,v 1.4 2004/05/03 17:30:14 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
@ -69,10 +69,10 @@ MD4Init(MD4_CTX *ctx)
void void
MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len) MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len)
{ {
u_int32_t have, need;
size_t have, need;
/* Check how many bytes we already have and how many more we need. */ /* Check how many bytes we already have and how many more we need. */
have = (u_int32_t)((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
have = (size_t)((ctx->count >> 3) & (MD4_BLOCK_LENGTH - 1));
need = MD4_BLOCK_LENGTH - have; need = MD4_BLOCK_LENGTH - have;
/* Update bitcount */ /* Update bitcount */
@ -101,15 +101,14 @@ MD4Update(MD4_CTX *ctx, const unsigned char *input, size_t len)
} }
/* /*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* Pad pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first) * 1 0* (64-bit count of bits processed, MSB-first)
*/ */
void void
MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx)
MD4Pad(MD4_CTX *ctx)
{ {
u_int8_t count[8]; u_int8_t count[8];
u_int32_t padlen;
int i;
size_t padlen;
/* Convert count to 8 bytes in little endian order. */ /* Convert count to 8 bytes in little endian order. */
PUT_64BIT_LE(count, ctx->count); PUT_64BIT_LE(count, ctx->count);
@ -121,7 +120,17 @@ MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx)
padlen += MD4_BLOCK_LENGTH; padlen += MD4_BLOCK_LENGTH;
MD4Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ MD4Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
MD4Update(ctx, count, 8); MD4Update(ctx, count, 8);
}
/*
* Final wrapup--call MD4Pad, fill in digest and zero out ctx.
*/
void
MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], MD4_CTX *ctx)
{
int i;
MD4Pad(ctx);
if (digest != NULL) { if (digest != NULL) {
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
PUT_32BIT_LE(digest + i * 4, ctx->state[i]); PUT_32BIT_LE(digest + i * 4, ctx->state[i]);


+ 17
- 8
src/lib/libc/hash/md5.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: md5.c,v 1.4 2004/04/29 18:38:23 millert Exp $ */
/* $OpenBSD: md5.c,v 1.5 2004/05/03 17:30:14 millert Exp $ */
/* /*
* This code implements the MD5 message-digest algorithm. * This code implements the MD5 message-digest algorithm.
@ -18,7 +18,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$OpenBSD: md5.c,v 1.4 2004/04/29 18:38:23 millert Exp $";
static const char rcsid[] = "$OpenBSD: md5.c,v 1.5 2004/05/03 17:30:14 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
@ -68,10 +68,10 @@ MD5Init(MD5_CTX *ctx)
void void
MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len) MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
{ {
u_int32_t have, need;
size_t have, need;
/* Check how many bytes we already have and how many more we need. */ /* Check how many bytes we already have and how many more we need. */
have = (u_int32_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
need = MD5_BLOCK_LENGTH - have; need = MD5_BLOCK_LENGTH - have;
/* Update bitcount */ /* Update bitcount */
@ -100,15 +100,14 @@ MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
} }
/* /*
* Final wrapup - pad to 64-byte boundary with the bit pattern
* Pad pad to 64-byte boundary with the bit pattern
* 1 0* (64-bit count of bits processed, MSB-first) * 1 0* (64-bit count of bits processed, MSB-first)
*/ */
void void
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
MD5Pad(MD5_CTX *ctx)
{ {
u_int8_t count[8]; u_int8_t count[8];
u_int32_t padlen;
int i;
size_t padlen;
/* Convert count to 8 bytes in little endian order. */ /* Convert count to 8 bytes in little endian order. */
PUT_64BIT_LE(count, ctx->count); PUT_64BIT_LE(count, ctx->count);
@ -120,7 +119,17 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
padlen += MD5_BLOCK_LENGTH; padlen += MD5_BLOCK_LENGTH;
MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
MD5Update(ctx, count, 8); MD5Update(ctx, count, 8);
}
/*
* Final wrapup--call MD5Pad, fill in digest and zero out ctx.
*/
void
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
{
int i;
MD5Pad(ctx);
if (digest != NULL) { if (digest != NULL) {
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
PUT_32BIT_LE(digest + i * 4, ctx->state[i]); PUT_32BIT_LE(digest + i * 4, ctx->state[i]);


+ 34
- 7
src/lib/libc/hash/mdX.3 View File

@ -6,7 +6,7 @@
.\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp .\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
.\" ---------------------------------------------------------------------------- .\" ----------------------------------------------------------------------------
.\" .\"
.\" $OpenBSD: mdX.3,v 1.3 2004/04/29 15:51:16 millert Exp $
.\" $OpenBSD: mdX.3,v 1.4 2004/05/03 17:30:14 millert Exp $
.\" .\"
.Dd April 29, 2004 .Dd April 29, 2004
.Dt MDX 3 .Dt MDX 3
@ -14,10 +14,12 @@
.Sh NAME .Sh NAME
.Nm MDXInit , .Nm MDXInit ,
.Nm MDXUpdate , .Nm MDXUpdate ,
.Nm MDXPad ,
.Nm MDXFinal , .Nm MDXFinal ,
.Nm MDXTransform , .Nm MDXTransform ,
.Nm MDXEnd , .Nm MDXEnd ,
.Nm MDXFile , .Nm MDXFile ,
.Nm MDXFileChunk ,
.Nm MDXData .Nm MDXData
.Nd calculate the RSA Data Security, Inc., ``MDX'' message digest .Nd calculate the RSA Data Security, Inc., ``MDX'' message digest
.Sh SYNOPSIS .Sh SYNOPSIS
@ -28,6 +30,8 @@
.Ft void .Ft void
.Fn MDXUpdate "MDX_CTX *context" "const unsigned char *data" "unsigned int len" .Fn MDXUpdate "MDX_CTX *context" "const unsigned char *data" "unsigned int len"
.Ft void .Ft void
.Fn MDXPad "MDX_CTX *context"
.Ft void
.Fn MDXFinal "u_int8_t digest[MDX_DIGEST_LENGTH]" "MDX_CTX *context" .Fn MDXFinal "u_int8_t digest[MDX_DIGEST_LENGTH]" "MDX_CTX *context"
.Ft void .Ft void
.Fn MDXTransform "u_int32_t state[4]" "u_int8_t block[MDX_BLOCK_LENGTH]" .Fn MDXTransform "u_int32_t state[4]" "u_int8_t block[MDX_BLOCK_LENGTH]"
@ -36,6 +40,8 @@
.Ft "char *" .Ft "char *"
.Fn MDXFile "char *filename" "char *buf" .Fn MDXFile "char *filename" "char *buf"
.Ft "char *" .Ft "char *"
.Fn MDXFileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn MDXData "const u_int8_t *data" "size_t len" "char *buf" .Fn MDXData "const u_int8_t *data" "size_t len" "char *buf"
.Sh DESCRIPTION .Sh DESCRIPTION
The MDX functions calculate a 128-bit cryptographic checksum (digest) The MDX functions calculate a 128-bit cryptographic checksum (digest)
@ -70,10 +76,12 @@ run over the data with
.Fn MDXUpdate , .Fn MDXUpdate ,
and finally extract the result using and finally extract the result using
.Fn MDXFinal . .Fn MDXFinal .
When a null pointer is passed to
.Fn MDXFinal
as first argument only the final padding will be applied and the
current context can still be used with
.Pp
The
.Fn MDXPad
function can be used to apply padding to the message digest as in
.Fn MDXFinal ,
but the current context can still be used with
.Fn MDXUpdate . .Fn MDXUpdate .
.Pp .Pp
The The
@ -103,6 +111,23 @@ calculates the digest of a file, and uses
.Fn MDXEnd .Fn MDXEnd
to return the result. to return the result.
If the file cannot be opened, a null pointer is returned. If the file cannot be opened, a null pointer is returned.
.Pp
.Fn MDXFileChunk
behaves like
.Fn MDXFile
but calculates the digest only for that portion of the file starting at
.Fa offset
and continuing for
.Fa length
bytes or until end of file is reached, whichever comes first.
A zero
.Fa length
can be specified to read until end of file.
A negative
.Fa length
or
.Fa offset
will be ignored.
.Fn MDXData .Fn MDXData
calculates the digest of a chunk of data in memory, and uses calculates the digest of a chunk of data in memory, and uses
.Fn MDXEnd .Fn MDXEnd
@ -111,6 +136,7 @@ to return the result.
When using When using
.Fn MDXEnd , .Fn MDXEnd ,
.Fn MDXFile , .Fn MDXFile ,
.Fn MDXFileChunk ,
or or
.Fn MDXData , .Fn MDXData ,
the the
@ -170,14 +196,15 @@ These functions appeared in
The original MDX routines were developed by The original MDX routines were developed by
.Tn RSA .Tn RSA
Data Security, Inc., and published in the above references. Data Security, Inc., and published in the above references.
This code is derived from a public domain implementation by Colin Plumb.
This code is derived from a public domain implementation written by Colin Plumb.
.Pp .Pp
The The
.Fn MDXEnd , .Fn MDXEnd ,
.Fn MDXFile , .Fn MDXFile ,
.Fn MDXFileChunk ,
and and
.Fn MDXData .Fn MDXData
helper functions were written by Poul-Henning Kamp.
helper functions are derived from code written by Poul-Henning Kamp.
.Sh BUGS .Sh BUGS
Hans Dobbertin has shown collisions for the full version of MD4 and Hans Dobbertin has shown collisions for the full version of MD4 and
found a collision in the compress function of MD5. found a collision in the compress function of MD5.


+ 31
- 5
src/lib/libc/hash/rmd160.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: rmd160.3,v 1.23 2004/04/29 15:51:16 millert Exp $
.\" $OpenBSD: rmd160.3,v 1.24 2004/05/03 17:30:14 millert Exp $
.\" .\"
.\" Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com> .\" Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
.\" .\"
@ -23,10 +23,12 @@
.Sh NAME .Sh NAME
.Nm RMD160Init , .Nm RMD160Init ,
.Nm RMD160Update , .Nm RMD160Update ,
.Nm RMD160Pad ,
.Nm RMD160Final , .Nm RMD160Final ,
.Nm RMD160Transform , .Nm RMD160Transform ,
.Nm RMD160End , .Nm RMD160End ,
.Nm RMD160File , .Nm RMD160File ,
.Nm RMD160FileChunk ,
.Nm RMD160Data .Nm RMD160Data
.Nd calculate the ``RIPEMD-160'' message digest .Nd calculate the ``RIPEMD-160'' message digest
.Sh SYNOPSIS .Sh SYNOPSIS
@ -37,6 +39,8 @@
.Ft void .Ft void
.Fn RMD160Update "RMD160_CTX *context" "const u_int8_t *data" "u_int32_t nbytes" .Fn RMD160Update "RMD160_CTX *context" "const u_int8_t *data" "u_int32_t nbytes"
.Ft void .Ft void
.Fn RMD160Pad "RMD160_CTX *context"
.Ft void
.Fn RMD160Final "u_int8_t digest[RMD160_DIGEST_LENGTH]" "RMD160_CTX *context" .Fn RMD160Final "u_int8_t digest[RMD160_DIGEST_LENGTH]" "RMD160_CTX *context"
.Ft void .Ft void
.Fn RMD160Transform "u_int32_t state[5]" "const u_int8_t block[RMD160_BLOCK_LENGTH]" .Fn RMD160Transform "u_int32_t state[5]" "const u_int8_t block[RMD160_BLOCK_LENGTH]"
@ -45,6 +49,8 @@
.Ft "char *" .Ft "char *"
.Fn RMD160File "char *filename" "char *buf" .Fn RMD160File "char *filename" "char *buf"
.Ft "char *" .Ft "char *"
.Fn RMD160FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn RMD160Data "const u_int8_t *data" "size_t len" "char *buf" .Fn RMD160Data "const u_int8_t *data" "size_t len" "char *buf"
.Sh DESCRIPTION .Sh DESCRIPTION
The RMD160 functions implement the 160-bit RIPE message digest hash algorithm The RMD160 functions implement the 160-bit RIPE message digest hash algorithm
@ -86,10 +92,12 @@ is called when all data has been added via
and stores a message digest in the and stores a message digest in the
.Ar digest .Ar digest
parameter. parameter.
When a null pointer is passed to
.Fn RMD160Final
as first argument only the final padding will be applied and the
current context can still be used with
.Pp
The
.Fn RMD160Pad
function can be used to apply padding to the message digest as in
.Fn RMD160Final ,
but the current context can still be used with
.Fn RMD160Update . .Fn RMD160Update .
.Pp .Pp
The The
@ -122,6 +130,23 @@ If
.Fn RMD160File .Fn RMD160File
is unable to open the file a NULL pointer is returned. is unable to open the file a NULL pointer is returned.
.Pp .Pp
.Fn RMD160FileChunk
behaves like
.Fn RMD160File
but calculates the digest only for that portion of the file starting at
.Fa offset
and continuing for
.Fa length
bytes or until end of file is reached, whichever comes first.
A zero
.Fa length
can be specified to read until end of file.
A negative
.Fa length
or
.Fa offset
will be ignored.
.Pp
The The
.Fn RMD160Data .Fn RMD160Data
function function
@ -203,6 +228,7 @@ This implementation of RMD-160 was written by Markus Friedl.
The The
.Fn RMD160End , .Fn RMD160End ,
.Fn RMD160File , .Fn RMD160File ,
.Fn RMD160FileChunk ,
and and
.Fn RMD160Data .Fn RMD160Data
helper functions are derived from code written by Poul-Henning Kamp. helper functions are derived from code written by Poul-Henning Kamp.


+ 13
- 7
src/lib/libc/hash/rmd160.c View File

@ -32,7 +32,7 @@
#include <rmd160.h> #include <rmd160.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: rmd160.c,v 1.14 2004/04/26 19:38:12 millert Exp $";
static char rcsid[] = "$OpenBSD: rmd160.c,v 1.15 2004/05/03 17:30:15 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#define PUT_64BIT_LE(cp, value) do { \ #define PUT_64BIT_LE(cp, value) do { \
@ -104,9 +104,9 @@ RMD160Init(RMD160_CTX *ctx)
} }
void void
RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
RMD160Update(RMD160_CTX *ctx, const u_char *input, size_t len)
{ {
u_int32_t have, off, need;
size_t have, off, need;
have = (ctx->count / 8) % RMD160_BLOCK_LENGTH; have = (ctx->count / 8) % RMD160_BLOCK_LENGTH;
need = RMD160_BLOCK_LENGTH - have; need = RMD160_BLOCK_LENGTH - have;
@ -131,11 +131,10 @@ RMD160Update(RMD160_CTX *ctx, const u_char *input, u_int32_t len)
} }
void void
RMD160Final(u_char digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx)
RMD160Pad(RMD160_CTX *ctx)
{ {
int i;
u_char size[8];
u_int32_t padlen;
u_int8_t size[8];
size_t padlen;
PUT_64BIT_LE(size, ctx->count); PUT_64BIT_LE(size, ctx->count);
@ -148,7 +147,14 @@ RMD160Final(u_char digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx)
padlen += RMD160_BLOCK_LENGTH; padlen += RMD160_BLOCK_LENGTH;
RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ RMD160Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
RMD160Update(ctx, size, 8); RMD160Update(ctx, size, 8);
}
void
RMD160Final(u_char digest[RMD160_DIGEST_LENGTH], RMD160_CTX *ctx)
{
int i;
RMD160Pad(ctx);
if (digest != NULL) if (digest != NULL)
for (i = 0; i < 5; i++) for (i = 0; i < 5; i++)
PUT_32BIT_LE(digest + i*4, ctx->state[i]); PUT_32BIT_LE(digest + i*4, ctx->state[i]);


+ 31
- 5
src/lib/libc/hash/sha1.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: sha1.3,v 1.29 2004/04/29 15:51:16 millert Exp $
.\" $OpenBSD: sha1.3,v 1.30 2004/05/03 17:30:15 millert Exp $
.\" .\"
.\" Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com> .\" Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
.\" .\"
@ -23,10 +23,12 @@
.Sh NAME .Sh NAME
.Nm SHA1Init , .Nm SHA1Init ,
.Nm SHA1Update , .Nm SHA1Update ,
.Nm SHA1Pad ,
.Nm SHA1Final , .Nm SHA1Final ,
.Nm SHA1Transform , .Nm SHA1Transform ,
.Nm SHA1End , .Nm SHA1End ,
.Nm SHA1File , .Nm SHA1File ,
.Nm SHA1FileChunk ,
.Nm SHA1Data .Nm SHA1Data
.Nd calculate the NIST Secure Hash Algorithm .Nd calculate the NIST Secure Hash Algorithm
.Sh SYNOPSIS .Sh SYNOPSIS
@ -37,6 +39,8 @@
.Ft void .Ft void
.Fn SHA1Update "SHA1_CTX *context" "const u_int8_t *data" "u_int len" .Fn SHA1Update "SHA1_CTX *context" "const u_int8_t *data" "u_int len"
.Ft void .Ft void
.Fn SHA1Pad "SHA1_CTX *context"
.Ft void
.Fn SHA1Final "u_int8_t digest[SHA1_DIGEST_LENGTH]" "SHA1_CTX *context" .Fn SHA1Final "u_int8_t digest[SHA1_DIGEST_LENGTH]" "SHA1_CTX *context"
.Ft void .Ft void
.Fn SHA1Transform "u_int32_t state[5]" "u_int8_t buffer[SHA1_BLOCK_LENGTH]" .Fn SHA1Transform "u_int32_t state[5]" "u_int8_t buffer[SHA1_BLOCK_LENGTH]"
@ -45,6 +49,8 @@
.Ft "char *" .Ft "char *"
.Fn SHA1File "char *filename" "char *buf" .Fn SHA1File "char *filename" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA1FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn SHA1Data "const u_int8_t *data" "size_t len" "char *buf" .Fn SHA1Data "const u_int8_t *data" "size_t len" "char *buf"
.Sh DESCRIPTION .Sh DESCRIPTION
The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1), The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1),
@ -83,10 +89,12 @@ is called when all data has been added via
and stores a message digest in the and stores a message digest in the
.Ar digest .Ar digest
parameter. parameter.
When a null pointer is passed to
.Fn SHA1Final
as first argument only the final padding will be applied and the
current context can still be used with
.Pp
The
.Fn SHA1Pad
function can be used to apply padding to the message digest as in
.Fn SHA1Final ,
but the current context can still be used with
.Fn SHA1Update . .Fn SHA1Update .
.Pp .Pp
The The
@ -119,6 +127,23 @@ If
.Fn SHA1File .Fn SHA1File
is unable to open the file a NULL pointer is returned. is unable to open the file a NULL pointer is returned.
.Pp .Pp
.Fn SHA1FileChunk
behaves like
.Fn SHA1File
but calculates the digest only for that portion of the file starting at
.Fa offset
and continuing for
.Fa length
bytes or until end of file is reached, whichever comes first.
A zero
.Fa length
can be specified to read until end of file.
A negative
.Fa length
or
.Fa offset
will be ignored.
.Pp
The The
.Fn SHA1Data .Fn SHA1Data
function function
@ -194,6 +219,7 @@ This implementation of SHA-1 was written by Steve Reid.
The The
.Fn SHA1End , .Fn SHA1End ,
.Fn SHA1File , .Fn SHA1File ,
.Fn SHA1FileChunk ,
and and
.Fn SHA1Data .Fn SHA1Data
helper functions are derived from code written by Poul-Henning Kamp. helper functions are derived from code written by Poul-Henning Kamp.


+ 15
- 7
src/lib/libc/hash/sha1.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: sha1.c,v 1.15 2004/04/27 15:54:56 millert Exp $ */
/* $OpenBSD: sha1.c,v 1.16 2004/05/03 17:30:15 millert Exp $ */
/* /*
* SHA-1 in C * SHA-1 in C
@ -15,7 +15,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static char rcsid[] = "$OpenBSD: sha1.c,v 1.15 2004/04/27 15:54:56 millert Exp $";
static char rcsid[] = "$OpenBSD: sha1.c,v 1.16 2004/05/03 17:30:15 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#define SHA1HANDSOFF /* Copies data before messing with it. */ #define SHA1HANDSOFF /* Copies data before messing with it. */
@ -131,11 +131,11 @@ SHA1Init(SHA1_CTX *context)
* Run your data through this. * Run your data through this.
*/ */
void void
SHA1Update(SHA1_CTX *context, const u_char *data, u_int len)
SHA1Update(SHA1_CTX *context, const u_char *data, size_t len)
{ {
u_int i, j;
size_t i, j;
j = (u_int32_t)((context->count >> 3) & 63);
j = (size_t)((context->count >> 3) & 63);
context->count += (len << 3); context->count += (len << 3);
if ((j + len) > 63) { if ((j + len) > 63) {
(void)memcpy(&context->buffer[j], data, (i = 64-j)); (void)memcpy(&context->buffer[j], data, (i = 64-j));
@ -154,10 +154,10 @@ SHA1Update(SHA1_CTX *context, const u_char *data, u_int len)
* Add padding and return the message digest. * Add padding and return the message digest.
*/ */
void void
SHA1Final(u_char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
SHA1Pad(SHA1_CTX *context)
{ {
u_int8_t finalcount[8];
u_int i; u_int i;
u_char finalcount[8];
for (i = 0; i < 8; i++) { for (i = 0; i < 8; i++) {
finalcount[i] = (u_char)((context->count >> finalcount[i] = (u_char)((context->count >>
@ -167,10 +167,18 @@ SHA1Final(u_char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
while ((context->count & 504) != 448) while ((context->count & 504) != 448)
SHA1Update(context, (u_char *)"\0", 1); SHA1Update(context, (u_char *)"\0", 1);
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
}
void
SHA1Final(u_char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context)
{
u_int i;
SHA1Pad(context);
if (digest) { if (digest) {
for (i = 0; i < SHA1_DIGEST_LENGTH; i++) for (i = 0; i < SHA1_DIGEST_LENGTH; i++)
digest[i] = (u_char) digest[i] = (u_char)
((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
} }
memset(context, 0, sizeof(*context));
} }

+ 40
- 5
src/lib/libc/hash/sha2.3 View File

@ -1,4 +1,4 @@
.\" $OpenBSD: sha2.3,v 1.6 2004/04/29 15:51:16 millert Exp $
.\" $OpenBSD: sha2.3,v 1.7 2004/05/03 17:30:15 millert Exp $
.\" .\"
.\" Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com> .\" Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
.\" .\"
@ -26,9 +26,11 @@
.Sh NAME .Sh NAME
.Nm SHA256_Init , .Nm SHA256_Init ,
.Nm SHA256_Update , .Nm SHA256_Update ,
.Nm SHA256_Pad ,
.Nm SHA256_Final , .Nm SHA256_Final ,
.Nm SHA256_End , .Nm SHA256_End ,
.Nm SHA256_File , .Nm SHA256_File ,
.Nm SHA256_FileChunk ,
.Nm SHA256_Data .Nm SHA256_Data
.Nd calculate the NIST Secure Hash Standard (version 2) .Nd calculate the NIST Secure Hash Standard (version 2)
.Sh SYNOPSIS .Sh SYNOPSIS
@ -39,36 +41,48 @@
.Ft void .Ft void
.Fn SHA256_Update "SHA256_CTX *context" "const u_int8_t *data" "size_t len" .Fn SHA256_Update "SHA256_CTX *context" "const u_int8_t *data" "size_t len"
.Ft void .Ft void
.Fn SHA256_Pad "SHA256_CTX *context"
.Ft void
.Fn SHA256_Final "u_int8_t digest[SHA256_DIGEST_LENGTH]" "SHA256_CTX *context" .Fn SHA256_Final "u_int8_t digest[SHA256_DIGEST_LENGTH]" "SHA256_CTX *context"
.Ft "char *" .Ft "char *"
.Fn SHA256_End "SHA256_CTX *context" "char *buf" .Fn SHA256_End "SHA256_CTX *context" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA256_File "char *filename" "char *buf" .Fn SHA256_File "char *filename" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA256_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn SHA256_Data "u_int8_t *data" "u_int len" "char *buf" .Fn SHA256_Data "u_int8_t *data" "u_int len" "char *buf"
.Ft void .Ft void
.Fn SHA384_Init "SHA384_CTX *context" .Fn SHA384_Init "SHA384_CTX *context"
.Ft void .Ft void
.Fn SHA384_Update "SHA384_CTX *context" "const u_int8_t *data" "size_t len" .Fn SHA384_Update "SHA384_CTX *context" "const u_int8_t *data" "size_t len"
.Ft void .Ft void
.Fn SHA384_Pad "SHA384_CTX *context"
.Ft void
.Fn SHA384_Final "u_int8_t digest[SHA384_DIGEST_LENGTH]" "SHA384_CTX *context" .Fn SHA384_Final "u_int8_t digest[SHA384_DIGEST_LENGTH]" "SHA384_CTX *context"
.Ft "char *" .Ft "char *"
.Fn SHA384_End "SHA384_CTX *context" "char *buf" .Fn SHA384_End "SHA384_CTX *context" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA384_File "char *filename" "char *buf" .Fn SHA384_File "char *filename" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA384_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn SHA384_Data "u_int8_t *data" "size_t len" "char *buf" .Fn SHA384_Data "u_int8_t *data" "size_t len" "char *buf"
.Ft void .Ft void
.Fn SHA512_Init "SHA512_CTX *context" .Fn SHA512_Init "SHA512_CTX *context"
.Ft void .Ft void
.Fn SHA512_Update "SHA512_CTX *context" "const u_int8_t *data" "size_t len" .Fn SHA512_Update "SHA512_CTX *context" "const u_int8_t *data" "size_t len"
.Ft void .Ft void
.Fn SHA512_Pad "SHA512_CTX *context"
.Ft void
.Fn SHA512_Final "u_int8_t digest[SHA512_DIGEST_LENGTH]" "SHA512_CTX *context" .Fn SHA512_Final "u_int8_t digest[SHA512_DIGEST_LENGTH]" "SHA512_CTX *context"
.Ft "char *" .Ft "char *"
.Fn SHA512_End "SHA512_CTX *context" "char *buf" .Fn SHA512_End "SHA512_CTX *context" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA512_File "char *filename" "char *buf" .Fn SHA512_File "char *filename" "char *buf"
.Ft "char *" .Ft "char *"
.Fn SHA512_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
.Ft "char *"
.Fn SHA512_Data "u_int8_t *data" "size_t len" "char *buf" .Fn SHA512_Data "u_int8_t *data" "size_t len" "char *buf"
.Sh DESCRIPTION .Sh DESCRIPTION
The SHA2 functions implement the NIST Secure Hash Standard, The SHA2 functions implement the NIST Secure Hash Standard,
@ -110,10 +124,12 @@ is called when all data has been added via
and stores a message digest in the and stores a message digest in the
.Ar digest .Ar digest
parameter. parameter.
When a null pointer is passed to
.Fn SHA256_Final
as first argument only the final padding will be applied and the
current context can still be used with
.Pp
The
.Fn SHA256_Pad
function can be used to apply padding to the message digest as in
.Fn SHA256_Final ,
but the current context can still be used with
.Fn SHA256_Update . .Fn SHA256_Update .
.Pp .Pp
The The
@ -132,6 +148,23 @@ If
.Fn SHA256_File .Fn SHA256_File
is unable to open the file, a NULL pointer is returned. is unable to open the file, a NULL pointer is returned.
.Pp .Pp
.Fn SHA256_FileChunk
behaves like
.Fn SHA256_File
but calculates the digest only for that portion of the file starting at
.Fa offset
and continuing for
.Fa length
bytes or until end of file is reached, whichever comes first.
A zero
.Fa length
can be specified to read until end of file.
A negative
.Fa length
or
.Fa offset
will be ignored.
.Pp
The The
.Fn SHA256_Data .Fn SHA256_Data
function function
@ -141,6 +174,7 @@ calculates the digest of an arbitrary string and returns the result via
For each of the For each of the
.Fn SHA256_End , .Fn SHA256_End ,
.Fn SHA256_File , .Fn SHA256_File ,
.Fn SHA256_FileChunk ,
and and
.Fn SHA256_Data .Fn SHA256_Data
functions the functions the
@ -208,6 +242,7 @@ This implementation of the SHA functions was written by Aaron D. Gifford.
The The
.Fn SHA256_End , .Fn SHA256_End ,
.Fn SHA256_File , .Fn SHA256_File ,
.Fn SHA256_FileChunk ,
and and
.Fn SHA256_Data .Fn SHA256_Data
helper functions are derived from code written by Poul-Henning Kamp. helper functions are derived from code written by Poul-Henning Kamp.


+ 80
- 68
src/lib/libc/hash/sha2.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: sha2.c,v 1.6 2004/05/03 02:57:47 millert Exp $ */
/* $OpenBSD: sha2.c,v 1.7 2004/05/03 17:30:15 millert Exp $ */
/* /*
* FILE: sha2.c * FILE: sha2.c
@ -35,7 +35,7 @@
*/ */
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$OpenBSD: sha2.c,v 1.6 2004/05/03 02:57:47 millert Exp $";
static const char rcsid[] = "$OpenBSD: sha2.c,v 1.7 2004/05/03 17:30:15 millert Exp $";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/types.h> #include <sys/types.h>
@ -164,7 +164,6 @@ static const char rcsid[] = "$OpenBSD: sha2.c,v 1.6 2004/05/03 02:57:47 millert
* library -- they are intended for private internal visibility/use * library -- they are intended for private internal visibility/use
* only. * only.
*/ */
void SHA512_Last(SHA512_CTX *);
void SHA256_Transform(SHA256_CTX *, const u_int8_t *); void SHA256_Transform(SHA256_CTX *, const u_int8_t *);
void SHA512_Transform(SHA512_CTX *, const u_int8_t *); void SHA512_Transform(SHA512_CTX *, const u_int8_t *);
@ -340,7 +339,7 @@ SHA256_Transform(SHA256_CTX *context, const u_int8_t *data)
ROUND256_0_TO_15(b,c,d,e,f,g,h,a); ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
} while (j < 16); } while (j < 16);
/* Now for the remaining rounds to 64: */
/* Now for the remaining rounds up to 63: */
do { do {
ROUND256(a,b,c,d,e,f,g,h); ROUND256(a,b,c,d,e,f,g,h);
ROUND256(h,a,b,c,d,e,f,g); ROUND256(h,a,b,c,d,e,f,g);
@ -493,65 +492,74 @@ SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len)
} }
void void
SHA256_Final(u_int8_t digest[], SHA256_CTX *context)
SHA256_Pad(SHA256_CTX *context)
{ {
u_int32_t *d = (u_int32_t *)digest;
unsigned int usedspace; unsigned int usedspace;
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) {
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
/* Convert FROM host byte order */
REVERSE64(context->bitcount,context->bitcount);
/* Convert FROM host byte order */
REVERSE64(context->bitcount, context->bitcount);
#endif #endif
if (usedspace > 0) {
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */
memset(&context->buffer[usedspace], 0, SHA256_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA256_BLOCK_LENGTH) {
memset(&context->buffer[usedspace], 0, SHA256_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
SHA256_Transform(context, context->buffer);
/* And set-up for the last transform: */
memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
}
} else {
if (usedspace > 0) {
/* Begin padding with a 1 bit: */
context->buffer[usedspace++] = 0x80;
if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
/* Set-up for the last transform: */ /* Set-up for the last transform: */
memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
memset(&context->buffer[usedspace], 0,
SHA256_SHORT_BLOCK_LENGTH - usedspace);
} else {
if (usedspace < SHA256_BLOCK_LENGTH) {
memset(&context->buffer[usedspace], 0,
SHA256_BLOCK_LENGTH - usedspace);
}
/* Do second-to-last transform: */
SHA256_Transform(context, context->buffer);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
/* Prepare for last transform: */
memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
} }
/* Set the bit count: */
*(u_int64_t *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
} else {
/* Set-up for the last transform: */
memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
/* Final transform: */
SHA256_Transform(context, context->buffer);
/* Begin padding with a 1 bit: */
*context->buffer = 0x80;
}
/* Store the length of input data (in bits): */
*(u_int64_t *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
/* Final transform: */
SHA256_Transform(context, context->buffer);
/* Clean up: */
usedspace = 0;
}
void
SHA256_Final(u_int8_t digest[], SHA256_CTX *context)
{
u_int32_t *d = (u_int32_t *)digest;
SHA256_Pad(context);
/* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) {
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE32(context->state[j],context->state[j]);
*d++ = context->state[j];
}
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE32(context->state[j], context->state[j]);
*d++ = context->state[j];
} }
#else #else
memcpy(d, context->state, SHA256_DIGEST_LENGTH); memcpy(d, context->state, SHA256_DIGEST_LENGTH);
#endif #endif
} }
/* Clean up state data: */
/* Zero out state data */
memset(context, 0, sizeof(*context)); memset(context, 0, sizeof(*context));
usedspace = 0;
} }
@ -599,9 +607,11 @@ void
SHA512_Transform(SHA512_CTX *context, const u_int8_t *data) SHA512_Transform(SHA512_CTX *context, const u_int8_t *data)
{ {
u_int64_t a, b, c, d, e, f, g, h, s0, s1; u_int64_t a, b, c, d, e, f, g, h, s0, s1;
u_int64_t T1, *W512 = (u_int64_t *)context->buffer;
u_int64_t T1, *W512;
int j; int j;
W512 = (u_int64_t *)context->buffer;
/* Initialize registers with the prev. intermediate value */ /* Initialize registers with the prev. intermediate value */
a = context->state[0]; a = context->state[0];
b = context->state[1]; b = context->state[1];
@ -614,6 +624,7 @@ SHA512_Transform(SHA512_CTX *context, const u_int8_t *data)
j = 0; j = 0;
do { do {
/* Rounds 0 to 15 (unrolled): */
ROUND512_0_TO_15(a,b,c,d,e,f,g,h); ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
ROUND512_0_TO_15(h,a,b,c,d,e,f,g); ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
ROUND512_0_TO_15(g,h,a,b,c,d,e,f); ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
@ -656,9 +667,11 @@ void
SHA512_Transform(SHA512_CTX *context, const u_int8_t *data) SHA512_Transform(SHA512_CTX *context, const u_int8_t *data)
{ {
u_int64_t a, b, c, d, e, f, g, h, s0, s1; u_int64_t a, b, c, d, e, f, g, h, s0, s1;
u_int64_t T1, T2, *W512 = (u_int64_t *)context->buffer;
u_int64_t T1, T2, *W512;
int j; int j;
W512 = (u_int64_t *)context->buffer;
/* Initialize registers with the prev. intermediate value */ /* Initialize registers with the prev. intermediate value */
a = context->state[0]; a = context->state[0];
b = context->state[1]; b = context->state[1];
@ -777,7 +790,7 @@ SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len)
} }
void void
SHA512_Last(SHA512_CTX *context)
SHA512_Pad(SHA512_CTX *context)
{ {
unsigned int usedspace; unsigned int usedspace;
@ -817,6 +830,9 @@ SHA512_Last(SHA512_CTX *context)
/* Final transform: */ /* Final transform: */
SHA512_Transform(context, context->buffer); SHA512_Transform(context, context->buffer);
/* Clean up: */
usedspace = 0;
} }
void void
@ -824,19 +840,16 @@ SHA512_Final(u_int8_t digest[], SHA512_CTX *context)
{ {
u_int64_t *d = (u_int64_t *)digest; u_int64_t *d = (u_int64_t *)digest;
SHA512_Pad(context);
/* If no digest buffer is passed, we don't bother doing this: */ /* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) { if (digest != NULL) {
SHA512_Last(context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE64(context->state[j],context->state[j]);
*d++ = context->state[j];
}
/* Convert TO host byte order */
int j;
for (j = 0; j < 8; j++) {
REVERSE64(context->state[j],context->state[j]);
*d++ = context->state[j];
} }
#else #else
memcpy(d, context->state, SHA512_DIGEST_LENGTH); memcpy(d, context->state, SHA512_DIGEST_LENGTH);
@ -865,24 +878,23 @@ SHA384_Update(SHA384_CTX *context, const u_int8_t *data, size_t len)
SHA512_Update((SHA512_CTX *)context, data, len); SHA512_Update((SHA512_CTX *)context, data, len);
} }
__weak_alias(SHA384_Pad, SHA512_Pad);
void void
SHA384_Final(u_int8_t digest[], SHA384_CTX *context) SHA384_Final(u_int8_t digest[], SHA384_CTX *context)
{ {
u_int64_t *d = (u_int64_t *)digest; u_int64_t *d = (u_int64_t *)digest;
SHA384_Pad(context);
/* If no digest buffer is passed, we don't bother doing this: */ /* If no digest buffer is passed, we don't bother doing this: */
if (digest != NULL) { if (digest != NULL) {
SHA512_Last((SHA512_CTX *)context);
/* Save the hash data for output: */
#if BYTE_ORDER == LITTLE_ENDIAN #if BYTE_ORDER == LITTLE_ENDIAN
{
/* Convert TO host byte order */
int j;
for (j = 0; j < 6; j++) {
REVERSE64(context->state[j],context->state[j]);
*d++ = context->state[j];
}
/* Convert TO host byte order */
int j;
for (j = 0; j < 6; j++) {
REVERSE64(context->state[j],context->state[j]);
*d++ = context->state[j];
} }
#else #else
memcpy(d, context->state, SHA384_DIGEST_LENGTH); memcpy(d, context->state, SHA384_DIGEST_LENGTH);


Loading…
Cancel
Save