Skip to main content
Code Review

Return to Question

replaced https://tools.ietf.org/html/rfc with https://www.rfc-editor.org/rfc/rfc
Source Link

This code is supposed to implement RFC 3394 RFC 3394 which also described in this PDF at page 11.

Are there any weak places or formatting issues in attached source? Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

I know, that ECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

This code is supposed to implement RFC 3394 which also described in this PDF at page 11.

Are there any weak places or formatting issues in attached source? Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

I know, that ECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

This code is supposed to implement RFC 3394 which also described in this PDF at page 11.

Are there any weak places or formatting issues in attached source? Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

I know, that ECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

Tweeted twitter.com/StackCodeReview/status/729351890899763201
deleted 13 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

AES Wrap - are there any problems in this key wrap function?

This code is supposed to implement RFC 3394 which also described in this pdfPDF at page 11.

Are there any weak places or formatting issues in attached source?

Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

P.S. I know, that ECBECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

AES Wrap - are there any problems in this key wrap function?

This code is supposed to implement RFC 3394 which also described in this pdf at page 11.

Are there any weak places or formatting issues in attached source?

Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

P.S. I know, that ECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

AES Wrap function

This code is supposed to implement RFC 3394 which also described in this PDF at page 11.

Are there any weak places or formatting issues in attached source? Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

I know, that ECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

Source Link

AES Wrap - are there any problems in this key wrap function?

This code is supposed to implement RFC 3394 which also described in this pdf at page 11.

Are there any weak places or formatting issues in attached source?

Or maybe some places are not explicit enough?

void wrap( const unsigned char *key, const unsigned char *plaintext, unsigned int plaintext_length,
 unsigned char *ciphertext, unsigned int *ciphertext_length, unsigned int semiblocksize)
{
 int i, j, k;
 unsigned int n, t, tt;
 unsigned char *A, B[16], *R;
 int targetlevel = 0, p = semiblocksize;
 while (p >>= 1) ++targetlevel;
 n = plaintext_length >> targetlevel;
 A = B;
 memset(A, 0xa6, semiblocksize);
 memcpy(ciphertext+semiblocksize, plaintext, plaintext_length);
 for(j=0, t=1; j<=5; j++)
 {
 for(i=1, R=ciphertext+semiblocksize; i<=n; i++, t++, R+=semiblocksize)
 {
 memcpy(B+semiblocksize, R, semiblocksize);
 AES128_ECB_encrypt(B, key, B);
 
 for(k=semiblocksize - 1, tt=t; (k>=0) && (tt>0); k--, tt>>=semiblocksize)
 {
 A[k] ^= (unsigned char) (tt & 0xFF);
 }
 memcpy(R, B+semiblocksize, semiblocksize);
 }
 }
 memcpy(ciphertext, A, semiblocksize);
 *ciphertext_length = plaintext_length + semiblocksize;
}

P.S. I know, that ECB mode should not be used in production, but this is only for ensuring output correctness by checking coincidence with the RFC.

lang-c

AltStyle によって変換されたページ (->オリジナル) /