sm4demo.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/bin/bash -x
  2. set -e
  3. gmssl sm4 -help
  4. # CBC
  5. echo hello | gmssl sm4 -cbc -encrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -out ciphertext.bin
  6. gmssl sm4 -cbc -decrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in ciphertext.bin
  7. # CTR
  8. echo hello | gmssl sm4 -ctr -encrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -out ciphertext.bin
  9. gmssl sm4 -ctr -decrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in ciphertext.bin
  10. # GCM IV length = 12
  11. echo hello | gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -out ciphertext.bin
  12. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -in ciphertext.bin
  13. # GCM IV length = 1
  14. echo hello | gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 11 -out ciphertext.bin
  15. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 11 -in ciphertext.bin
  16. # GCM IV length = 16
  17. echo hello | gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -out ciphertext.bin
  18. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in ciphertext.bin
  19. # GCM IV length = 32
  20. echo hello | gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 1122334455667788112233445566778811223344556677881122334455667788 -out ciphertext.bin
  21. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 1122334455667788112233445566778811223344556677881122334455667788 -in ciphertext.bin
  22. # CBC-SM3-HMAC
  23. echo hello | gmssl sm4 -cbc_sm3_hmac -encrypt \
  24. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  25. -iv 11223344556677881122334455667788 -out ciphertext.bin
  26. gmssl sm4 -cbc_sm3_hmac -decrypt \
  27. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  28. -iv 11223344556677881122334455667788 -in ciphertext.bin
  29. # CTR-SM3-HMAC
  30. echo hello | gmssl sm4 -ctr_sm3_hmac -encrypt \
  31. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  32. -iv 11223344556677881122334455667788 -out ciphertext.bin
  33. gmssl sm4 -ctr_sm3_hmac -decrypt \
  34. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  35. -iv 11223344556677881122334455667788 -in ciphertext.bin
  36. # encrypt/decrypt long text file
  37. cat << EOF > plaintext.txt
  38. -----BEGIN CERTIFICATE-----
  39. MIICzzCCAnKgAwIBAgIFEzY5M3AwDAYIKoEcz1UBg3UFADAlMQswCQYDVQQGEwJD
  40. TjEWMBQGA1UECgwNQ0ZDQSBTTTIgT0NBMTAeFw0yMTA2MTEwOTA1MjBaFw0yNjA2
  41. MTkwODE2NTZaMIGRMQswCQYDVQQGEwJDTjEPMA0GA1UECAwG5YyX5LqsMQ8wDQYD
  42. VQQHDAbljJfkuqwxJzAlBgNVBAoMHuS4reWbvemTtuihjOiCoeS7veaciemZkOWF
  43. rOWPuDERMA8GA1UECwwITG9jYWwgUkExDDAKBgNVBAsMA1NTTDEWMBQGA1UEAwwN
  44. ZWJzc2VjLmJvYy5jbjBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABPsNUnoZQM9C
  45. SnvC57TbvdfyOTCuPOSlZmPAyxBKFj+Y1QH/xlubHdVf5XqHrO1jCDRi7aN5IKGX
  46. QF1492c803OjggEeMIIBGjAfBgNVHSMEGDAWgBRck1ggWiRzVhAbZFAQ7OmnygdB
  47. ETAMBgNVHRMBAf8EAjAAMEgGA1UdIARBMD8wPQYIYIEchu8qAQEwMTAvBggrBgEF
  48. BQcCARYjaHR0cDovL3d3dy5jZmNhLmNvbS5jbi91cy91cy0xNC5odG0wNwYDVR0f
  49. BDAwLjAsoCqgKIYmaHR0cDovL2NybC5jZmNhLmNvbS5jbi9TTTIvY3JsNTYxOC5j
  50. cmwwGAYDVR0RBBEwD4INZWJzc2VjLmJvYy5jbjAOBgNVHQ8BAf8EBAMCBsAwHQYD
  51. VR0OBBYEFJ6oFo/OrKgDhHFORpaq04kX7T1KMB0GA1UdJQQWMBQGCCsGAQUFBwMC
  52. BggrBgEFBQcDATAMBggqgRzPVQGDdQUAA0kAMEYCIQCvhSvbv5h6ERl1YcCLg+fz
  53. 9UleQbaPfBYwUjUD2dAHVQIhAMRC4k9S/mSC0UpUvCqh/DQC2Ui8Tccd5G2IgYSs
  54. cnUN
  55. -----END CERTIFICATE-----
  56. EOF
  57. # CBC
  58. gmssl sm4 -cbc -encrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in plaintext.txt -out ciphertext.bin
  59. gmssl sm4 -cbc -decrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in ciphertext.bin
  60. # CTR
  61. gmssl sm4 -ctr -encrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in plaintext.txt -out ciphertext.bin
  62. gmssl sm4 -ctr -decrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in ciphertext.bin
  63. # GCM IV length = 12
  64. gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -in plaintext.txt -out ciphertext.bin
  65. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -in ciphertext.bin
  66. # GCM IV length = 1
  67. gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 11 -in plaintext.txt -out ciphertext.bin
  68. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 11 -in ciphertext.bin
  69. # GCM IV length = 16
  70. gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in plaintext.txt -out ciphertext.bin
  71. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 11223344556677881122334455667788 -in ciphertext.bin
  72. # GCM IV length = 32
  73. gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 1122334455667788112233445566778811223344556677881122334455667788 -in plaintext.txt -out ciphertext.bin
  74. gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 1122334455667788112233445566778811223344556677881122334455667788 -in ciphertext.bin
  75. # CBC-SM3-HMAC
  76. gmssl sm4 -cbc_sm3_hmac -encrypt \
  77. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  78. -iv 11223344556677881122334455667788 -in plaintext.txt -out ciphertext.bin
  79. gmssl sm4 -cbc_sm3_hmac -decrypt \
  80. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  81. -iv 11223344556677881122334455667788 -in ciphertext.bin
  82. # CTR-SM3-HMAC
  83. gmssl sm4 -ctr_sm3_hmac -encrypt \
  84. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  85. -iv 11223344556677881122334455667788 -in plaintext.txt -out ciphertext.bin
  86. gmssl sm4 -ctr_sm3_hmac -decrypt \
  87. -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \
  88. -iv 11223344556677881122334455667788 -in ciphertext.bin
  89. rm -fr plaintext.txt
  90. rm -fr ciphertext.bin