Skip to content

Commit

Permalink
Add SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 functions to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed Jun 15, 2024
1 parent 14c2e4b commit 510ca0e
Show file tree
Hide file tree
Showing 7 changed files with 1,261 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ nobase_include_HEADERS = \
libimobiledevice-glue/termcolors.h \
libimobiledevice-glue/cbuf.h \
libimobiledevice-glue/opack.h \
libimobiledevice-glue/tlv.h
libimobiledevice-glue/tlv.h \
libimobiledevice-glue/sha.h
86 changes: 86 additions & 0 deletions include/libimobiledevice-glue/sha.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#ifndef __SHA_H
#define __SHA_H

#include <stddef.h>
#include <stdint.h>

#include <libimobiledevice-glue/glue.h>

/* LibTomCrypt, modular cryptographic library -- Tom St Denis
*
* LibTomCrypt is a library that provides various cryptographic
* algorithms in a highly modular and flexible manner.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, [email protected], http://libtom.org
*/

/* SHA-1 */
typedef struct sha1_context_ {
uint64_t length;
uint32_t state[5];
size_t curlen;
unsigned char buf[64];
} sha1_context;

#define SHA1_DIGEST_LENGTH 20

LIMD_GLUE_API int sha1_init(sha1_context * md);
LIMD_GLUE_API int sha1_final(sha1_context * md, unsigned char *out);
LIMD_GLUE_API int sha1_update(sha1_context * md, const void *data, size_t inlen);
LIMD_GLUE_API int sha1(const unsigned char *message, size_t message_len, unsigned char *out);

/* SHA-256 */
typedef struct sha256_context_ {
uint64_t length;
uint32_t state[8];
size_t curlen;
unsigned char buf[64];
int num_dwords;
} sha256_context;

#define SHA256_DIGEST_LENGTH 32

LIMD_GLUE_API int sha256_init(sha256_context * md);
LIMD_GLUE_API int sha256_final(sha256_context * md, unsigned char *out);
LIMD_GLUE_API int sha256_update(sha256_context * md, const void *data, size_t inlen);
LIMD_GLUE_API int sha256(const unsigned char *message, size_t message_len, unsigned char *out);

/* SHA-224 */
#define sha224_context sha256_context

#define SHA224_DIGEST_LENGTH 28

LIMD_GLUE_API int sha224_init(sha224_context * md);
LIMD_GLUE_API int sha224_final(sha224_context * md, unsigned char *out);
LIMD_GLUE_API int sha224_update(sha224_context * md, const void *data, size_t inlen);
LIMD_GLUE_API int sha224(const unsigned char *message, size_t message_len, unsigned char *out);

/* SHA-512 */
typedef struct sha512_context_ {
uint64_t length, state[8];
size_t curlen;
unsigned char buf[128];
int num_qwords;
} sha512_context;

#define SHA512_DIGEST_LENGTH 64

LIMD_GLUE_API int sha512_init(sha512_context * md);
LIMD_GLUE_API int sha512_final(sha512_context * md, unsigned char *out);
LIMD_GLUE_API int sha512_update(sha512_context * md, const void *data, size_t inlen);
LIMD_GLUE_API int sha512(const unsigned char *message, size_t message_len, unsigned char *out);

/* SHA-384 */
#define sha384_context sha512_context

#define SHA384_DIGEST_LENGTH 48

LIMD_GLUE_API int sha384_init(sha384_context * md);
LIMD_GLUE_API int sha384_final(sha384_context * md, unsigned char *out);
LIMD_GLUE_API int sha384_update(sha384_context * md, const void *data, size_t inlen);
LIMD_GLUE_API int sha384(const unsigned char *message, size_t message_len, unsigned char *out);

#endif
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ libimobiledevice_glue_1_0_la_SOURCES = \
cbuf.c \
opack.c \
tlv.c \
sha1.c \
sha256.c \
sha512.c \
fixedint.h \
common.h

if WIN32
Expand Down
72 changes: 72 additions & 0 deletions src/fixedint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Portable header to provide the 32 and 64 bits type.
Not a compatible replacement for <stdint.h>, do not blindly use it as such.
*/

#if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined(__WATCOMC__) && (defined(_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_) || defined(__UINT_FAST64_TYPE__)) )) && !defined(FIXEDINT_H_INCLUDED)
#include <stdint.h>
#define FIXEDINT_H_INCLUDED

#if defined(__WATCOMC__) && __WATCOMC__ >= 1250 && !defined(UINT64_C)
#include <limits.h>
#define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX))
#endif
#endif


#ifndef FIXEDINT_H_INCLUDED
#define FIXEDINT_H_INCLUDED

#include <limits.h>

/* (u)int32_t */
#ifndef uint32_t
#if (ULONG_MAX == 0xffffffffUL)
typedef unsigned long uint32_t;
#elif (UINT_MAX == 0xffffffffUL)
typedef unsigned int uint32_t;
#elif (USHRT_MAX == 0xffffffffUL)
typedef unsigned short uint32_t;
#endif
#endif


#ifndef int32_t
#if (LONG_MAX == 0x7fffffffL)
typedef signed long int32_t;
#elif (INT_MAX == 0x7fffffffL)
typedef signed int int32_t;
#elif (SHRT_MAX == 0x7fffffffL)
typedef signed short int32_t;
#endif
#endif


/* (u)int64_t */
#if (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC__ && __STDC_VERSION__ >= 199901L)
typedef long long int64_t;
typedef unsigned long long uint64_t;

#define UINT64_C(v) v ##ULL
#define INT64_C(v) v ##LL
#elif defined(__GNUC__)
__extension__ typedef long long int64_t;
__extension__ typedef unsigned long long uint64_t;

#define UINT64_C(v) v ##ULL
#define INT64_C(v) v ##LL
#elif defined(__MWERKS__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__APPLE_CC__) || defined(_LONG_LONG) || defined(_CRAYC)
typedef long long int64_t;
typedef unsigned long long uint64_t;

#define UINT64_C(v) v ##ULL
#define INT64_C(v) v ##LL
#elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || defined(__alpha) || defined(__DECC)
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;

#define UINT64_C(v) v ##UI64
#define INT64_C(v) v ##I64
#endif
#endif
Loading

0 comments on commit 510ca0e

Please sign in to comment.