lib: Adapt argon2 library for U-Boot

Rename argon.c to argon_wrapper.c so we can use 'argon' as the library
name. Move the include file into the normal place.

Add SPDX tags but otherwise keep the files as is. The code style uses
spaces instead of tabs and has other differences with U-Boot

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-11-11 03:37:46 -07:00
parent 3c7c70b0d2
commit f1514752a5
10 changed files with 65 additions and 54 deletions

View File

@@ -1,3 +1,4 @@
/* SPDX-License-Identifier: Apache-2.0 OR CC0-1.0 */
/*
* Argon2 reference source code package - reference C implementations
*
@@ -18,10 +19,20 @@
#ifndef ARGON2_H
#define ARGON2_H
#include <stdint.h>
#include <stddef.h>
#include <linux/types.h>
#include <limits.h>
/* U-Boot: Define missing integer constant macros */
#ifndef UINT32_C
#define UINT32_C(c) c ## U
#endif
#ifndef UINT64_C
#define UINT64_C(c) c ## ULL
#endif
/* U-Boot: Disable threading */
#define ARGON2_NO_THREADS
#if defined(__cplusplus)
extern "C" {
#endif

10
lib/argon2/Makefile Normal file
View File

@@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-2.0+
#
# (C) Copyright 2025 Canonical Ltd
#
# Argon2 password hashing library
# Based on PHC winner: https://github.com/P-H-C/phc-winner-argon2
obj-$(CONFIG_ARGON2) += argon2.o
argon2-y := argon2_wrapper.o core.o ref.o blake2/blake2b.o

View File

@@ -1,3 +1,4 @@
// SPDX-License-Identifier: Apache-2.0 OR CC0-1.0
/*
* Argon2 reference source code package - reference C implementations
*
@@ -15,12 +16,12 @@
* software. If not, they may be obtained at the above URLs.
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* U-Boot: Use U-Boot headers */
#include <linux/string.h>
#include <linux/types.h>
#include <malloc.h>
#include "argon2.h"
#include "encoding.h"
#include "core.h"
const char *argon2_type2string(argon2_type type, int uppercase) {
@@ -161,6 +162,15 @@ int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
memcpy(hash, out, hashlen);
}
/* U-Boot: encoding not supported (requires encoding.c) */
#ifdef __UBOOT__
/* Return error if encoding requested */
if (encoded && encodedlen) {
clear_internal_memory(out, hashlen);
free(out);
return ARGON2_ENCODING_FAIL;
}
#else
/* if encoding requested, write it */
if (encoded && encodedlen) {
if (encode_string(encoded, encodedlen, &context, type) != ARGON2_OK) {
@@ -170,6 +180,7 @@ int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
return ARGON2_ENCODING_FAIL;
}
}
#endif
clear_internal_memory(out, hashlen);
free(out);
@@ -236,6 +247,8 @@ int argon2id_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
ARGON2_VERSION_NUMBER);
}
/* U-Boot: verify functions not needed */
#ifndef __UBOOT__
static int argon2_compare(const uint8_t *b1, const uint8_t *b2, size_t len) {
size_t i;
uint8_t d = 0U;
@@ -364,6 +377,7 @@ int argon2i_verify_ctx(argon2_context *context, const char *hash) {
int argon2id_verify_ctx(argon2_context *context, const char *hash) {
return argon2_verify_ctx(context, hash, Argon2_id);
}
#endif /* U-Boot: verify functions */
const char *argon2_error_message(int error_code) {
switch (error_code) {
@@ -444,9 +458,12 @@ const char *argon2_error_message(int error_code) {
}
}
/* U-Boot: encodedlen not needed */
#ifndef __UBOOT__
size_t argon2_encodedlen(uint32_t t_cost, uint32_t m_cost, uint32_t parallelism,
uint32_t saltlen, uint32_t hashlen, argon2_type type) {
return strlen("$$v=$m=,t=,p=$$") + strlen(argon2_type2string(type, 0)) +
numlen(t_cost) + numlen(m_cost) + numlen(parallelism) +
b64len(saltlen) + b64len(hashlen) + numlen(ARGON2_VERSION_NUMBER) + 1;
}
#endif /* U-Boot: encodedlen */

View File

@@ -1,3 +1,4 @@
/* SPDX-License-Identifier: Apache-2.0 OR CC0-1.0 */
/*
* Argon2 reference source code package - reference C implementations
*
@@ -18,12 +19,11 @@
#ifndef PORTABLE_BLAKE2_IMPL_H
#define PORTABLE_BLAKE2_IMPL_H
#include <stdint.h>
#include <string.h>
/* U-Boot includes */
#include <linux/types.h>
#include <linux/string.h>
#ifdef _WIN32
#define BLAKE2_INLINE __inline
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__GNUC__) || defined(__clang__)
#define BLAKE2_INLINE __inline__
#else
#define BLAKE2_INLINE

View File

@@ -1,3 +1,4 @@
/* SPDX-License-Identifier: Apache-2.0 OR CC0-1.0 */
/*
* Argon2 reference source code package - reference C implementations
*

View File

@@ -1,3 +1,4 @@
// SPDX-License-Identifier: Apache-2.0 OR CC0-1.0
/*
* Argon2 reference source code package - reference C implementations
*
@@ -15,9 +16,9 @@
* software. If not, they may be obtained at the above URLs.
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
/* U-Boot includes */
#include <linux/types.h>
#include <linux/string.h>
#include "blake2.h"
#include "blake2-impl.h"

View File

@@ -1,3 +1,4 @@
/* SPDX-License-Identifier: Apache-2.0 OR CC0-1.0 */
/*
* Argon2 reference source code package - reference C implementations
*

View File

@@ -1,3 +1,4 @@
// SPDX-License-Identifier: Apache-2.0 OR CC0-1.0
/*
* Argon2 reference source code package - reference C implementations
*
@@ -15,32 +16,14 @@
* software. If not, they may be obtained at the above URLs.
*/
/*For memory wiping*/
#ifdef _WIN32
#include <windows.h>
#include <winbase.h> /* For SecureZeroMemory */
#endif
#if defined __STDC_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#define VC_GE_2005(version) (version >= 1400)
/* for explicit_bzero() on glibc */
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* U-Boot includes */
#include <linux/string.h>
#include <malloc.h>
#include "core.h"
#include "thread.h"
#include "blake2/blake2.h"
#include "blake2/blake2-impl.h"
#ifdef GENKAT
#include "genkat.h"
#endif
#if defined(__clang__)
#if __has_attribute(optnone)
#define NOT_OPTIMIZED __attribute__((optnone))
@@ -123,25 +106,10 @@ void free_memory(const argon2_context *context, uint8_t *memory,
}
}
#if defined(__OpenBSD__)
#define HAVE_EXPLICIT_BZERO 1
#elif defined(__GLIBC__) && defined(__GLIBC_PREREQ)
#if __GLIBC_PREREQ(2,25)
#define HAVE_EXPLICIT_BZERO 1
#endif
#endif
void NOT_OPTIMIZED secure_wipe_memory(void *v, size_t n) {
#if defined(_MSC_VER) && VC_GE_2005(_MSC_VER) || defined(__MINGW32__)
SecureZeroMemory(v, n);
#elif defined memset_s
memset_s(v, n, 0, n);
#elif defined(HAVE_EXPLICIT_BZERO)
explicit_bzero(v, n);
#else
/* Use volatile pointer to prevent compiler optimization */
static void *(*const volatile memset_sec)(void *, int, size_t) = &memset;
memset_sec(v, 0, n);
#endif
}
/* Memory clear flag defaults to true. */

View File

@@ -1,3 +1,4 @@
/* SPDX-License-Identifier: Apache-2.0 OR CC0-1.0 */
/*
* Argon2 reference source code package - reference C implementations
*

View File

@@ -1,3 +1,4 @@
// SPDX-License-Identifier: Apache-2.0 OR CC0-1.0
/*
* Argon2 reference source code package - reference C implementations
*
@@ -15,9 +16,9 @@
* software. If not, they may be obtained at the above URLs.
*/
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
/* U-Boot includes */
#include <linux/types.h>
#include <linux/string.h>
#include "argon2.h"
#include "core.h"